0

I am altering someone else's Python code to create a GIS tool that everyone can use. Currently, they have about 20-30 feature classes with paths to them in their code. The problem is that the beginning of those paths will be different depending on who is using them and where they paste the main folder with the features in it.

For example:

They had

arcpy.MakeFeatureLayer_management(r"V:\gdrs\data\fens\fgdb\biota.gdb\fens", "nearFeature")

I pulled out the path and made the path a parameter

Fens = arcpy.GetParameterAsText(5)

and switched their code to

arcpy.MakeFeatureLayer_management(Fens, "nearFeature")

Then I make it a feature class parameter in the toolbox.

However, as I'm changing this I realized there are about 20-30 features so this isn't practical to have the user browse to the file location of 30 features each time they use the tool.

It would be nice if they could just change the beginning of the path as a parameter. It would also be nice if they didn't have to change the path again after they've used to tool once.

Note: gdrs is the main folder and everything should be the same after that.

I'm new to Python, but I am assuming there is an easy way to get around this issue.

2

If the directory layout is static beyond your base directory (V:\gdrs), then just ask for the base directory as a parameter and then programmatically concatenate the rest of the directories as needed.

So from your example above:

Fens = arcpy.GetParameterAsText(5) # They select "V:\gdrs"

arcpy.MakeFeatureLayer_management(Fens + "\data\fens\fgdb\biota.gdb\fens" , "nearFeature")

So when processed, the path becomes:

arcpy.MakeFeatureLayer_management("V:\gdrs\data\fens\fgdb\biota.gdb\fens" , "nearFeature")

It would be a good idea to explore the os.path module so that any irregularities in the parameter gets corrected.

https://docs.python.org/3/library/os.path.html#os.path.join

https://www.geeksforgeeks.org/python-os-path-join-method/

Becomes:

arcpy.MakeFeatureLayer_management(os.path.join(Fens, "\data\fens\fgdb\biota.gdb\fens") , "nearFeature")
0

My reading of this is that you already know the relative paths to all ~30 datasources (i.e. ...\gdrs\data\fens\fgdb\biota.gdb\fens' etc) and the only input you need from the user is where that main gdrs folder is located on their local filesystem. If so you can get the path to the gdrs folder as a paramter and then use os.path.join to create as many full paths as you need.

import os
import arcpy

# Get path of main folder
MainFolder = arcpy.GetParameterAsText(5)

# Get full path to fens feature class and make layer
Fens = os.path.join(MainFolder, "\data\fens\fgdb\biota.gdb\fens")
arcpy.MakeFeatureLayer_management(Fens, "nearFeature")

#Do the same for any additional features ....
SomeOtherFeature = os.path.join(MainFolder, "\data\fens\fgdb\biota.gdb\otherfeature")
arcpy.MakeFeatureLayer_management(SomeOtherFeature, "anotherNearFeature")

If you make a list of all 20-30 paths you could just loop through the list so you don't have to copy paste the same code over and over.

This all assumes that everyone using the tool will always be using it with the exact same data and directory structure.

2
  • I tried both ways and I think it should work if I could just figure out why it is messing up the path name. there is a folder in the path called fgdb. for some reason it takes out the \f in the path name so the name of the folder before has gbd added to it. for example FirstFolder\fgbd\nextfolder becomes FirstFoldergbd\nextfolder.
    – hosta
    Dec 18 '20 at 21:00
  • I FIgured it out. Needed to add two backslashes instead of one. FirstFolder\\fgbd\nextfolder. Thank you both for your help.
    – hosta
    Dec 18 '20 at 21:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.