How to add a path to default database in ArcPy script?

10483
14
Jump to solution
11-12-2015 07:33 AM
KarolinaKorzeniowska
Occasional Contributor

I would like to add a path to default database in Arcpy script. The path will be a place where the results from evaluation will be saved. However, I do not want to add a full path, but a relative path, which enable to apply my script also in another computer. How to add relative path to default database?

Tags (3)
0 Kudos
14 Replies
LukeSturtevant
Occasional Contributor III

Here is an edited script. Let us know if you have any questions or if it is still not working for you. If you are planning to run this script from ArcMap and you just want to use the default geodatabase there is no reason to set path = arcpy.env.workspace unless you just want to reference it using the shorter variable name 'path' throughout your script. Otherwise all outputs that do not have a specific path will be saved to the default geodatabase.

# Import arcpy module  
import os  
import arcpy  
from arcpy import env  
from arcpy.sa import *

arcpy.CheckOutExtension("Spatial")
env.overwriteOutput= True
  
# Set the input data  
inRaster = arcpy.GetParameterAsText(0)  
# Set the output data  
outRaster = arcpy.GetParameterAsText(1)  
    
neighborhood = NbrRectangle(3, 3, "CELL")  
# Check out the ArcGIS Spatial Analyst extension license  

# Execute FocalStatistics  
outFocalStatistics = FocalStatistics(inRaster, neighborhood, "MEAN","")  
# Save the output   
outFocalStatistics.save("DEM_mean")  

# Execute Minus  
outMinus = inRaster - outFocalStatistics  
# Save the output  
outMinus.save(outRaster)  
KarolinaKorzeniowska
Occasional Contributor

Thank you. Now the script works fine. In the code I changed the line 24 from:

ourMinus = inRaster - outFocalStatistics

to

inRaster2 = Raster("DEM_mean")
outMinus = inRaster - inRaster2

and it also works. I did this, because if in default database will be some other raster with the name outFocalStatictics then this raster can be take into evaluation, am I right?

I have also one more question. How to delete intermediate results (DEM_mean) from default database? I have found the it can be done using the code below, but in my case it is not deleting anything.

# Clean up
del outFocalStatistics
0 Kudos
LukeSturtevant
Occasional Contributor III

You do not need to change the line 24. Essentially all you did was change the variable name of the outFocalStatistics to inRaster2. The name in the geodatabase will always be "DEM_mean". However, it looks like you want to treat the "DEM_mean" as intermediate data. In that case just do not save the output of outFocalStatistics like this:

Execute FocalStatistics   

outFocalStatistics = FocalStatistics(inRaster, neighborhood, "MEAN","")      

Execute Minus   

outMinus = inRaster - outFocalStatistics   

Save the output   

outMinus.save(outRaster)

This will always generate outFocalStatistics in memory and automatically delete it. On another note if you were to save the output of outFocalStatistics and want to delete it at the end of your script you would use arcpy.Delete_management(outFocalStatistics). The del function is useful when clearing variables, lists, dictionaries, and the like, but it will not delete output results.

KarolinaKorzeniowska
Occasional Contributor

Yes, you are right. Thank you for explanation. Now I understand it better, and my toolbox works like I expected.

0 Kudos
stevegourley
Occasional Contributor II

you could also use arcpy.env.scratchGDB

0 Kudos