Select to view content in your preferred language

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

11594
14
Jump to solution
11-12-2015 07:33 AM
KarolinaKorzeniowska
Deactivated User

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
1 Solution

Accepted Solutions
LukeSturtevant
Frequent Contributor

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)  

View solution in original post

14 Replies
LukeSturtevant
Frequent Contributor

Are you talking about referencing your current workspace that is set in your environment settings in ArcMap? If so then arcpy.env.workspace would correctly reference your default geodatabase.

0 Kudos
KarolinaKorzeniowska
Deactivated User

Thank you for your answer. Yes, I am trying to reference to my default geodatabase. From your link I have found that I should use:

arcpy.env.workspace = path

but here I also have to specify the path to the "path". How to do this? When I change the computer the script will not work, because the "path" will be different.

0 Kudos
BlakeTerhune
MVP Regular Contributor

You could do it based on the directory the script is being run from using os.getcwd(), then just build the rest of your directory from there. The requirement would be that you have to run the script from the same starting folder location on each computer.

0 Kudos
KarolinaKorzeniowska
Deactivated User

Thank you for suggestion, but I think it is not what I am looking for. I would like to build a python toolbox which run like standard ArcGIS toolboxes - when you open the toolbox and do not specify the path then the results are automatically saved in default geodatabase.

0 Kudos
DarrenWiens2
MVP Honored Contributor

That is how you would set the workspace environment. If you just want to get the workspace environment, switch the equation:

>>> path = arcpy.env.workspace
>>> print path # workspace location

Some other peripheral ideas would be to reference the script location:

>>> import sys
>>> print sys.argv[0] # script location

... or the current mxd location:

>>> print arcpy.mapping.MapDocument("CURRENT").filePath # current mxd location
LukeSturtevant
Frequent Contributor

As Darren said, if you are running your tool from ArcMap or ArcCatalog you do not need to explicitly set arcpy.env.workspace to a path. It will automatically be set to your default geodatabase as I mentioned earlier.

0 Kudos
KarolinaKorzeniowska
Deactivated User

Okay, thanks for answer. But then how to use the output file as an input in the next step of my script? I have to tell the script where the file is located right?

0 Kudos
LukeSturtevant
Frequent Contributor

Its hard telling not knowing what you are trying to do exactly, but you can set the output as a variable for example:

feature = arcpy.CreateFeatureclass_management (env.workspace, "test1","POINT","","","",26919)

arcpy.Append_management (inputs, feature, "NO_TEST")

Here the new feature class is set to the variable 'feature' and then it can be referenced as feature throughout your script.

KarolinaKorzeniowska
Deactivated User

I am trying to build a toolbox where at first I am evaluating Focal Statistics from DEM (raster) and then I am subtracting (Raster Calculator) both rasters. That I why I need a path to the results from my first evaluation.

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

# Set the input data
inRaster = arcpy.GetParameterAsText(0)
# Set the output data
outRaster = arcpy.GetParameterAsText(1)
# Set the path to workspace location
path = arcpy.env.workspace
print path


# Evaluate mean DEM
inRaster = "DEM.tif"
neighborhood = NbrRectangle(3, 3, "CELL")
# Check out the ArcGIS Spatial Analyst extension license
arcpy.CheckOutExtension("Spatial")
# Execute FocalStatistics
outFocalStatistics = FocalStatistics(inRaster, neighborhood, "MEAN","")
# Save the output 
outFocalStatistics.save("DEM_mean")
#
# Subtraction
inRaster1 = Raster("DEM")
inRaster2 = Raster("DEM_mean")
# Execute Minus
outMinus = inRaster1 - inRaster2
# Save the output
outMinus.save("result")
0 Kudos