Wednesday 7 June 2017

shapefile - Export to SHP with ArcPy with query



I have a shapefile with table with area code (field areacode).


How can I export with ArcPy each unique area code so I get a SHP file for each one?


It takes too long to select the layer and query each area code and then export manually to SHP file.



Answer



You can use a SearchCursor to export features in a shapefile to new shapefiles. The SHAPE@ token allows you to access individual feature geometry. The Feature Class To Feature Class (Conversion) tool does the conversion to shapefile. You can also specify a subset of features you would like to export by specifying a query


query = """"areacode" > 2"""




import arcpy

shp = r'C:\path\to\your\shapefile'
outpath = r'C:\out\path'

query = """"areacode" > 2"""

with arcpy.da.SearchCursor(shp, ["SHAPE@", "areacode"], query) as cursor:
for row in cursor:
# Note* using the "areacode" to name the output

arcpy.FeatureClassToFeatureClass_conversion (row[0], outpath, row[1])

No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...