Hello ESRI community, I'm a python developer who's new to ArcGIS Pro and trying to get started on developing a tool. I read through a bunch of documentation and resources but still struggling to sketch out an architecture for what I'm trying to achieve and I'm hoping a veteran can point me in the right direction!
Here's the outline of what I'm trying to build, and I'm happy to provide more detail or context as required:
rules=[
{'min_val':0, 'max_val':10, 'cat':'Low'},
{'min_val':10, 'max_val':25, 'cat':'Medium'},
{'min_val':25, 'max_val':100, 'cat':'High'}
]
where the min_val and max_val determine a category (values between 0 and 10 are 'Low' etc)
I see how to set default values in a python Toolbox, but I'm wondering how (and whether) it's possible to dynamically set defaults based on previous runs.
Thanks to anyone who can provide pointers!
Solved! Go to Solution.
Ha! You've discovered the stateless nature of GP Tools. It can be infuriating. Three thoughts:
aprx = arcpy.mp.ArcGISProject("CURRENT")
location = aprx.homeFolder
I did something like this years ago in desktop.
You could get fancier by allowing the user to choose multiple state-keeping files. This would require a new tool parameter to list the files and a catalog (such as a folder) to store the multiple files.
Tool Validation topics:
Ha! You've discovered the stateless nature of GP Tools. It can be infuriating. Three thoughts:
aprx = arcpy.mp.ArcGISProject("CURRENT")
location = aprx.homeFolder
I did something like this years ago in desktop.
You could get fancier by allowing the user to choose multiple state-keeping files. This would require a new tool parameter to list the files and a catalog (such as a folder) to store the multiple files.
Tool Validation topics:
Thanks for the guidance! I was also thinking about somehow loading parameters from a file but figured there must be a more graceful solution (it would seem there isn't!). Using the project filepath seems like a good way to go for a stable location.
In scanning the doc, I found there are some caveats to the project directory
Seems like filePath is a better option than homeFolder
https://pro.arcgis.com/en/pro-app/latest/arcpy/mapping/arcgisproject-class.htm
The aprx.filePath seems to return a path to an actual file like this (at least when working in an environment with an already saved project):
'C:\\test_project\\test_project.aprx'
whereas aprx.homeFolder returns an actual directory:
'C:\\test_project'
so I'm not sure how to use filePath - maybe just go up one level from the file itself to get its parent directory?
I have some logic to detect if the the parameter file exists, and if it doesn't I create it with default values, so if the user's effective homeFolder changes and they lose their current parameters, (for my purposes at least) it's not a big deal to start over again once with default values.
The potential homeFolder lock is a little more concerning - I'm just creating this tool for my own team, and I'm not anticipating releasing to a wider audience, but if I ever do I'll have to account for that. Very much appreciate the heads up!
You could import Python's os.path module -- it has tons of methods for busting apart and constructing pathnames.