Editing shapefile with Python

2748
6
Jump to solution
04-11-2014 05:52 AM
João_PauloPereira
New Contributor II
I'm new here at the Forum and I tried looking for something like my problem, and I got nothing. If there is something, please, forgive me.

I have a binary polygon shapefile, with a feature called GRIDCODE with 0s and 1s for earch polygon.
I want to edit this shapefile and delete every polygon with the number 1. In other words, I want only the polygons with GRIDCODE number 0 in my shapefile. To do that in Editor mode is quite simple, but I want to do it through a python code. Does anymore here know how to do it?

Thanks
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
Luke_Pinner
MVP Regular Contributor
0 Kudos
6 Replies
IanMurray
Frequent Contributor
I'd probably use an update cursor to evaluate the value of the field, then delete the row if it the field value equals 1.  If you need some starter code let me know.
0 Kudos
João_PauloPereira
New Contributor II
I'd probably use an update cursor to evaluate the value of the field, then delete the row if it the field value equals 1.  If you need some starter code let me know.


iamurray, thanks for the help. If you could send me the code I would appreciate. I'm new using python in ArcGIS, so every help is welcome.

Thanks
0 Kudos
IanMurray
Frequent Contributor
import arcpy
from arcpy import env

env.workspace = #"put the file path to the folder your shapefile is in here as a string"

cursor =  arcpy.da.UpdateCursor(#"shapefile name" , "Field name that has the 1's and 0's")
    
for row in cursor:
    if row[0] == 1:
        cursor.deleteRow()

del row 
del cursor



I'd make a copy of your shapefile first to test this on, there is no way to undo the delete row, so I would test it on the copy before applying it to the actual file.
0 Kudos
João_PauloPereira
New Contributor II
import arcpy
from arcpy import env

env.workspace = #"put the file path to the folder your shapefile is in here as a string"

cursor =  arcpy.da.UpdateCursor(#"shapefile name" , "Field name that has the 1's and 0's")
    
for row in cursor:
    if row[0] == 1:
        cursor.deleteRow()

del row 
del cursor



I'd make a copy of your shapefile first to test this on, there is no way to undo the delete row, so I would test it on the copy before applying it to the actual file.


Ok...I'll try. Thanks
0 Kudos
Luke_Pinner
MVP Regular Contributor
Use arcpy.SelectLayerByAttribute_management then arcpy.DeleteFeatures_management

Code examples are in the help:
http://resources.arcgis.com/en/help/main/10.2/index.html#//001700000071000000
http://resources.arcgis.com/en/help/main/10.2/index.html#//001700000036000000
0 Kudos
João_PauloPereira
New Contributor II
Use arcpy.SelectLayerByAttribute_management then arcpy.DeleteFeatures_management

Code examples are in the help:
http://resources.arcgis.com/en/help/main/10.2/index.html#//001700000071000000
http://resources.arcgis.com/en/help/main/10.2/index.html#//001700000036000000


Thanks for the answer again Ipinner...
Your tip worked perfectly. The code turned like this:

arcpy.SelectLayerByAttribute_management ("GAP", "NEW_SELECTION", " GRIDCODE=1 ")
arcpy.DeleteFeatures_management("GAP")

Quite easy and everything worked really nice...
0 Kudos