Setting workspace dynamically as part of out path

644
5
07-21-2013 06:13 AM
DonalCasey
New Contributor
Hi all,
I've successfully managed to set my workspace as a 'arcpy.GetParameterAsText(0)'. I then created a file GDB and I'm now trying to create a rasterdataset in this file GDB. The rasterdataset wants  an 'out_path' to be set. I tried what I have below:

# Create Raster Dataset
out_path = "Prog.gdb"
out_name = "R_Dataset"
# Run the Create Raster Dataset Script
arcpy.CreateRasterDataset_management (out_path, out_name, "", "8_BIT_UNSIGNED", "", "1", "", "PYRAMIDS -1 NEAREST DEFAULT 75 NO_SKIP", "128 128", "LZ77", "")
# Mosaic Rasters into a the Raster Dataset
inputs = arcpy.ListRasters("", "TIF")
# Set the target to the Raster Dataset that was already created
target = "R_Dataset"
# Run the Mosaic tool
arcpy.Mosaic_management (inputs, target, "LAST", "FIRST", "", "", "NONE", "0", "NONE")
# Set local variables
in_raster = "R_Dataset"
in_mask_data = "Study_area_extent"
#Extract by Mask
outExtractByMask = arcpy.sa.ExtractByMask (in_raster, in_mask_data)


I want to know is there anyway for the original workspace to become part of the out_path when another user runs the code, so it would be something like 'Workspace + Prog.gdb".

Thanks
Tags (2)
0 Kudos
5 Replies
T__WayneWhitley
Frequent Contributor
Sure.  The out_path parameter for this tool can be a workspace or raster catalog...so to set this to your file gdb, typically with the way you've already set things up in your description (not apparent in your code snippet), do something like this:

import os  #typically this statement is at the beginning of your script

# then if you have a statement similar to this to set the workspace to a dir:
arcpy.env.workspace = arcpy.GetParameterAsText(0)

# ...you can follow up with this to 'append' the file gdb:
out_path = os.path.join(arcpy.env.workspace, <your gdb>)


Or...if you set the workspace to an already existing gdb, then the out_path param in the tool would simply be the workspace.

Hope that helps,
Wayne

EDIT:  I removed the misleading out_path parameter and replaced that with <your gdb>.
I originally expressed that as out_path because that's the variable reference in your script snippet...but it isn't really a path so that's bad form.  Anyway, this should clear things up a bit.  Note that you can (and should) enforce the parameter var types allowed when you set up your script tool.
0 Kudos
DonalCasey
New Contributor
Sure. The out_path parameter for this tool can be a workspace or raster catalog...so to set this to your file gdb, typically with the way you've already set things up in your description (not apparent in your code snippet), do something like this: 

import os #typically this statement is at the beginning of your script 

# then if you have a statement similar to this to set the workspace to a dir: 
arcpy.env.workspace = arcpy.GetParameterAsText(0) 

# ...you can follow up with this to 'append' the file gdb: 
out_path = os.path.join(arcpy.env.workspace, out_path) 


Or...if you set the workspace to an already existing gdb, then the out_path param in the tool would simply be the workspace. 

Hope that helps, 
Wayne


Thanks Wayne, I tried the below based on your advice:

out_path = os.path.join(arcpy.env.workspace, Prog_Assign.gdb)
out_name = "R_Dataset"
# Run the Create Raster Dataset Script
arcpy.CreateRasterDataset_management (out_path, out_name, "", "8_BIT_UNSIGNED", "", "1", "", "PYRAMIDS -1 NEAREST DEFAULT 75 NO_SKIP", "128 128", "LZ77", "")


But I get an error in the script tool stating:

out_path = os.path.join(arcpy.env.workspace, Prog_Assign.gdb)
NameError: name 'Prog_Assign' is not defined


Is my synthax perhaps messed up?

Thanks
0 Kudos
T__WayneWhitley
Frequent Contributor
Sounds like either your gdb hasn't been created yet...or the name is wrong.  (Or the path to it is wrong.)

Not sure of your workflow - do you want to set the workspace to an existing gdb?  ...or create the gdb 'dynamically'?


EDIT:  Post your entire code.
0 Kudos
T__WayneWhitley
Frequent Contributor
oops, if you have a variable defined for your gdb, enter that.  Or, if explicitly naming it, you need quotes (because it is a string).

In other words 'NameError: name 'Prog_Assign' is not defined' means the interpeter thinks you're referencing a defined variable.

...so this is needed in the way you are using it:

out_path = os.path.join(arcpy.env.workspace, "Prog_Assign.gdb")


Sorry 'bout that, not paying attention!
0 Kudos
DonalCasey
New Contributor
Sounds like either your gdb hasn't been created yet...or the name is wrong.  (Or the path to it is wrong.)

Not sure of your workflow - do you want to set the workspace to an existing gdb?  ...or create the gdb 'dynamically'?


EDIT:  Post your entire code.


Thanks for all your help so far, I want to set the workspace to the gdb that will be created at the start of the script and will have shps imported into it!
0 Kudos