Hello Everybody,
I have a huge Shapefile in which I want to remove sliver polygons with the eliminate tool in python. However, repeatedly get the following error. At first i tried to execute without the "layer", but otherwise, the "SelectLayerByAttribute_management" returns an error (altough it works with the GUI on the Shapefile directly).
I am quite new to ArcGIS scripting and really hope to get some hints.
thank you and regards,
Ludwig
#################################################################
"File "remove_slivers_and_gaps.py", line 26, in <module>
arcpy.Eliminate_management(in_features = "layer", out_feature_class = output
shapefile, selection = "AREA", ex_where_clause = ' "Vegetation" = 5 ')
File "C:\Program Files (x86)\ArcGIS\Desktop10.2\arcpy\arcpy\management.py", line 4233, in Eliminate
raise e
arcgisscripting.ExecuteError: Failed to execute. Parameters are not valid.
ERROR 000670: output Output Feature Class is same as input Input Layer
failed to execute (Eliminate)."
################################################################
My Script ist here:
inputshapefile = "Vegetation_25832.shp"
workshapefile = "work_25832.shp"
outputshapefile = "Vegetation_rsag_25832.shp"
env.workspace = "D:\\R_working_directory\\remove_gaps_slivers"
### Repair geometry
arcpy.RepairGeometry_management(in_features = inputshapefile)
### Multipart to Singlepart
arcpy.MultipartToSinglepart_management(in_features = inputshapefile,
out_feature_class = workshapefile)
### Add new Field for area
arcpy.AddField_management(in_table = workshapefile, field_name = "area",
field_type = "FLOAT", field_precision = 10, field_scale = 2)
### Calculate new Field with area
arcpy.CalculateField_management(in_table = workshapefile,
field = "area", expression = '!shape.area@meters!', expression_type = "PYTHON")
### Execute SelectLayerByAttribute to define features to be eliminated
arcpy.MakeFeatureLayer_management(in_features = workshapefile, out_layer = "layer")
arcpy.SelectLayerByAttribute_management(in_layer_or_view = "layer",
selection_type = "NEW_SELECTION", where_clause = ' "area" < 1 ')
### Execute Eliminate
arcpy.Eliminate_management(in_features = "layer", out_feature_class = outputshapefile,
selection = "AREA", ex_where_clause = ' "Vegetation" = 5 ')
Message was edited by: Curtis Price - added python code highlighting and wrapped some lines
Solved! Go to Solution.
I agree with James, it's a lot easier to take advantage of argument position - saves typing and makes for easier to debug code. I only use variable names when a tool has many parameters I can skip and let default. If a tool only has a handful of parameters it is easier to read code that just lists them i norder like in Jim's example. Especially if you follow the python code and use really good variable names.
It's funny your code doesn't make it look like your output and input are the same as the error message says. Do you have two layers loaded (in memory from dinking with python or in ArcMap), both named "layer"? Generic layer names like that can be dangerous as if you have two layers in memory with the same name the first one arcpy finds in memory is the one it uses. This is especially an issue when pasting code in the ArcMap Python prompt.
Another reason I try really hard to always delete layers when I'm done with them in a finally block.
Re: Cleaning up temporary layers created by python toolbox script
I haven't tested this or even looked up the Eliminate tool, but this doesn't look quite right:
### Execute Eliminate
arcpy.Eliminate_management(in_features = "layer", out_feature_class = outputshapefile, selection = "AREA", ex_where_clause = ' "Vegetation" = 5 ')
Seems like you just need to replace with your layer variables:
### Execute Eliminate
arcpy.Eliminate_management(inputshapefile, outputshapefile, "AREA", ' "Vegetation" = 5 ')
I agree with James, it's a lot easier to take advantage of argument position - saves typing and makes for easier to debug code. I only use variable names when a tool has many parameters I can skip and let default. If a tool only has a handful of parameters it is easier to read code that just lists them i norder like in Jim's example. Especially if you follow the python code and use really good variable names.
It's funny your code doesn't make it look like your output and input are the same as the error message says. Do you have two layers loaded (in memory from dinking with python or in ArcMap), both named "layer"? Generic layer names like that can be dangerous as if you have two layers in memory with the same name the first one arcpy finds in memory is the one it uses. This is especially an issue when pasting code in the ArcMap Python prompt.
Another reason I try really hard to always delete layers when I'm done with them in a finally block.
Re: Cleaning up temporary layers created by python toolbox script
Did you check the error message?
The output parameter and the input parameter cannot share an identical path and name. The output must be unique from the input.
Create a unique name for your output dataset.
Hello everybody,
thank you very much for all your quick comments. I deliberately had used the names of the arguments as i am a beginner and like to see in my code exactly what parameter is set on what.
James, I had tried your solution already, but if I do so the SelectLayerByAttribute_management returns an error (although it works in the GUI). This is why I added the MakeFeatureLayer_managementto work with the generic "layer". The script ran today in the same form as I posted here so I guess that the problem with same layers in the memory could have been an issue. I am never using the python window, but always a standalone script I start via cmd and python filename.py but I never closed the cmd. Could same file names in memory be an issue there too? I always clean up the results written to disk before running the script again, but that should not be a problem.
Thank you again,
Ludwig
> Could same file names in memory be an issue there [in a standalone Python session]?
Duplicate layer names are even more insidious outside of ArcMap because you can't really see those layers and I don't know a way to even detect them!
All layers are deleted when the python session (script) ends. But if you are running from an IDE the Python session continues so this can be an issue there.