I'm writing a script tool to take a large data set select by location export and project it. I cannot get the for loop to work and I am at a loss because the script runs without errors, but creates no outputs except for the buffer used for selection. When debugging I've found that when I put the beginning part of the script before the for loop in there and have it try to print(Layers) it returns no values.
See attached script. Any help is greatly appreciated.
between lines 25 and 26, add a
<SPAN class="keyword token">print</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="string token">"Layers found {}"</SPAN><SPAN class="punctuation token">.</SPAN>format<SPAN class="punctuation token">(</SPAN>Input_workspace<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="line-numbers-rows"><SPAN></SPAN></SPAN>
If you get nothing, then the input workspace isn't any good, or it isn't a full path.
Also add a
<SPAN class="keyword token">print</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="string token">"Layer name {}"</SPAN><SPAN class="punctuation token">.</SPAN>format<SPAN class="punctuation token">(</SPAN>Layer<SPAN class="punctuation token">.</SPAN>name<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="line-numbers-rows"><SPAN></SPAN></SPAN>
on line 26.5
Also
/blogs/dan_patterson/2016/08/14/script-formatting
so people can copy and paste your code and have it formatted properly. Screen grabs can be use.
ListFeatureClasses—Help | Documentation is designed to have the workspace already set:
The workspace environment must be set before using several of the list functions, including ListDatasets, ListFeatureClasses, ListFiles, ListRasters, ListTables, and ListWorkspaces.
For starters, you have to set the workspace. Secondly, passing a workspace as a wildcard search will likely result in nothing being returned even if the workspace is set.
I had a feeling this was the problem, I need to have multiple workspaces in the script and it's causing issues I think I need a larger for loop to go through the workspaces instead of setting up the for loop for one workspace. Thanks for pointing it out.
Most, if not all, geoprocessing tools accept full paths to data sets, and I strongly encourage passing/using full paths instead of relying on the workspace being set.
I've made the following correction to list through multiple workspaces but the arcpy.MakeFeatureLayer is not running it's erroring out at the project step because there are no inputs.
You state:
the arcpy.MakeFeatureLayer is not running it's erroring out at the project step because there are no inputs.
So is Make Feature Layer not working or Project? It is always helpful to paste the error and full traceback to avoid confusion like this on what exactly is generating the error.
Sorry I can clarify, above is the error I am getting but also when I check my results folder it doesn't appear to be creating any outputs. Since Project is the last function I thought the error was coming from a lack of inputs from Make Feature Layer and Select Layer by Location appear to not be running.
Hi Caroline, On line 38 where you're calling arcpy.Project_managment() try switching the order of your second and third arguments.
Arcpy is expecting input dataset, followed by output dataset, then coordinate system (see: Project—Data Management toolbox | Documentation ). While the script is currently specifying input dataset, followed by coordinate system, then output dataset. The error 'cannot set input into parameter out_coor_system is basically complaining that it can't figure out how to interpret the contents of the out_name2 variable as a coordinate system.
What we should end up with on line 38 will be
arpcy.Project_management(out_name, out_name2, Coordinate)<SPAN class="line-numbers-rows"><SPAN></SPAN></SPAN>
If we still have issues with the call to Project_management after making this change, we'll likely need to take a look at what input is being provided on line 18 with Coordinate = arcpy.GetParameterAsText(7)
Thanks for your help once I fixed my inputs the tool ran but now I am getting this error, even though the output does exist and I'm not sure why it would not be supported.
When errors relate to parameters, it is helpful to share the actual values stored in the variables being passed to the tool (and workspace if you are relying on relative paths). I am guessing the issue is will out_name, but I am not sure since I don't know what string is in the variable.
You are correct I'm having trouble with the paths, it seems to be defaulting to C:\Python27\ArcGIS10.7\ but the features are being created in my output workspace path. I can't share my folder paths the data I'm working with is proprietary. Right now the script is looking at a path where there is no output and I'm working on trying to re-path my variables so that out_name = Output_workspace + fc +"_SELECTION". I'm having trouble with this because in the syntax for FeatureClassToFeatureClass includes Output_workspace so when I try to define out_name I get the path duplicated (i.e. C:\Data\Selection.shp\C:\Data\Selection.shp)
It might be worth searching out some blog posts or tutorials on using file paths in Python since you are struggling with it. In the short term, you can try saving the Result object from Feature Class to Feature Class and passing that to project instead of trying to reconstruct the path yourself:
res <SPAN class="operator token">=</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>FeatureClassToFeatureClass_conversion<SPAN class="punctuation token">(</SPAN>out_layer<SPAN class="punctuation token">,</SPAN> out_folder_path<SPAN class="punctuation token">,</SPAN> out_name<SPAN class="punctuation token">)</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>Project_management<SPAN class="punctuation token">(</SPAN>res<SPAN class="punctuation token">,</SPAN> Coordinate<SPAN class="punctuation token">,</SPAN> out_name2<SPAN class="punctuation token">)</SPAN><SPAN class="line-numbers-rows"><SPAN></SPAN><SPAN></SPAN></SPAN>
Les membres connectés peuvent publier, suivre les mises à jour, et plus encore. Nouveau ici ? Inscrivez-vous gratuitement.
Find useful guides, FAQs, and documents to help you navigate and make the most of Esri Community.