1

I am new to Python and in the process of learning. As a self test I am trying to export all of the municipalities of a single county feature layer as their own feature layers. I am trying to name each feature layer based on the municipality label column but Python returns this error "ERROR 000210: Cannot create output" on the last line which is the CopyFeatures_management tool. How can I achieve my desired result?

import arcpy
import os
arcpy.env.overwriteOutput = True
arcpy.env.workspace = r"C:\gis\python_training\municipality iteration test\mun test.gdb"
mun = "Municipality"
dict = {}
with arcpy.da.SearchCursor(mun,["OBJECTID", "MUN_LABEL"]) as cursor:
    for row in cursor:
        dict[row[0]] = row[1]
del cursor
dict2 = {}
for key, value in dict.items():
    if value not in dict2.values():
        dict2[key] = value
arcpy.MakeFeatureLayer_management(mun,"munlyr")
for key in dict2:
    arcpy.SelectLayerByAttribute_management("munlyr", "NEW_SELECTION","OBJECTID = " + str(key))
    arcpy.CopyFeatures_management("munlyr", str(value))
  • The value variable is not modified inside your final loop. BTW: You don't neet to del something in a with – Vince Feb 19 at 15:53
  • And one municipality can be more than one record/feature/row? – BERA Feb 19 at 15:57
  • Each municipality is a single row within the initial county layer. – Daykray Feb 19 at 15:59
  • If so you dont need to use dictionaries. Use the da.SearchCursor with the OID field and municipality field. For each row Select using OID field = row[0] – BERA Feb 19 at 16:02
2

If each row is one municipality (no duplicate rows) you dont need to use dictionaries. Use the da.SearchCursor to iterate over each record, and Select to export to new feature classes:

import arcpy

arcpy.env.workspace = r"C:\gis\python_training\municipality iteration test\mun test.gdb"
mun = "Municipality"

oidfieldname = arcpy.Describe(mun).OIDFieldName
with arcpy.da.SearchCursor(mun,[oidfieldname,"MUN_LABEL"]) as cursor:
    for oid, munlabel in cursor:
        sql = "{0}={1}".format(arcpy.AddFieldDelimiters(datasource=mun,field=oidfieldname),oid)
        arcpy.Select_analysis(in_features=mun, out_feature_class=munlabel, where_clause=sql)

(The same results can be achieved by using Split By Attributes tool)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.