Select to view content in your preferred language

Create File GDB and store as variable

2976
10
Jump to solution
03-28-2019 07:17 AM
RyanF
by
Emerging Contributor

I'm working on a script that originally had locations coded in but I want to make it where anyone in my office can use it and they can set their workspace up however they want. I've almost got it to work how I want. I think I have one more obstacle then I'm good.

I have some parameters set up for user input.

arcpy.env.workspace = arcpy.GetParameterAsText(0)

FileGDB_name = arcpy.GetParameterAsText(1)

NewLocation = arcpy.CreateFileGDB_management(arcpy.env.workspace, FildGDB_Name)

clipLayer = arcpy.Clip_analysis(RouteLayer, StateLayer, NewLocation + name + "Clip")

RouteLayer and StateLayer are additional parameters set up by the user. The script fails when it gets to the clip tool. I realize the way I have it set up, NewLocation calls the CreateFileGDB tool rather than the location of the GDB. I want to be able to create the GDB and store it as a variable so it can be used in other tools in the script. Could someone shed some light on how to do this?

Thanks!

0 Kudos
1 Solution

Accepted Solutions
MatthewDriscoll
MVP Alum
clipLayer = arcpy.Clip_analysis(RouteLayer, StateLayer, GDB + "\\" + name + "_Clip")

You need the \ in the path.

View solution in original post

10 Replies
JoshuaBixby
MVP Esteemed Contributor

ArcGIS geoprocessing tools return Result—ArcPy classes | ArcGIS Desktop  objects.  You can't append a string to a result object the way you are trying in your code.  Try changing the NewLocation line to:

NewLocation = arcpy.CreateFileGDB_management(arcpy.env.workspace, FildGDB_Name).getOutput(0)
RyanF
by
Emerging Contributor

I just tried that. The script will run but the results don't have any records. Is there another way to create the GDB and store as a variable? Maybe I'm not understanding the whole .getOutput(0) thing. Sorry, it's been a while since I've done any python. I did some in college 4 years ago but never thought I would actually be using it.

0 Kudos
MatthewDriscoll
MVP Alum

I think it's the way you worded the output, try this for your next line.

clipLayer = arcpy.Clip_analysis(RouteLayer, StateLayer, NewLocation + "/Clip")‍‍

If you want it as a variable.

GDBvar = arcpy.env.workspace + "\\" + FileGDB_name
RyanF
by
Emerging Contributor

That still gives me a result with no records.

I want to keep the name variable in there. That is holding the name of the state for each clip layer (i.e. name = "Texas"). For example, an output would be something like .......New.gdb\TexasClip which is why I thought NewLocation + name + "Clip" would work.

0 Kudos
MatthewDriscoll
MVP Alum

So it creates the feature but the feature is empty? 

0 Kudos
RyanF
by
Emerging Contributor

Correct, it creates the feature but it's empty.

0 Kudos
RyanF
by
Emerging Contributor

So here's the code I have. I just ran like this. It'll create the GDB but the result of the clip tool gets stored at the workspace level rather than being stored in the GDB. I must have been using some bad data earlier which may have been causing the output to have no records. The output now has records. However, I want a feature class stored inside the GDB rather than a shapefile.

arcpy.env.workspace = arcpy.GetParameterAsText(0)
RouteLayer = arcpy.GetParameterAsText(1)
StateLayer = arcpy.GetParameterAsText(2)
StateTaxDistricts = arcpy.GetParameterAsText(3)
FileGDB_Name = arcpy.GetParameterAsText(4)

GDB = arcpy.CreateFileGDB_management(arcpy.env.workspace, FileGDB_Name).getOutput(0)

stateName = os.path.basename(StateLayer).rstrip(os.path.splitext(StateLayer)[0])
name = stateName[:-1]

clipLayer = arcpy.Clip_analysis(RouteLayer, StateLayer, GDB + name + "_Clip")
0 Kudos
MatthewDriscoll
MVP Alum
clipLayer = arcpy.Clip_analysis(RouteLayer, StateLayer, GDB + "\\" + name + "_Clip")

You need the \ in the path.

RyanF
by
Emerging Contributor

Ha! I forgot something so simple! My script now works the way I want it to! Thanks for the help!

0 Kudos