2

I am trying to copy features a number of times based on a numeric field value.

The code below works for points when the POINT type is chosen. However, when I switched the feature type to POLYGON it correctly copies the features as many times as I want, but the polygons do not display when brought into arcmap. For example, the output attribute table (shown below) is populated, but the SHAPE_length and SHAPE_Area fields are shown as zero. Is there perhaps something in this code maybe within the input or search cursor '*' function that is not reading the shape_area and shape_length fields? The input feature class is a polygon and has a defined shape_area and shape_length for each record, which is not being carried over into the output feature class.

This table is from the output polygon feature class: enter image description here

def main():  
import arcpy
import os  
fc_in = r"input.gdb\inputfeatureclass" # this one exists  
fld_count = "PHASE_COUNT"  
fc_out = r"input.gdb\outputfeatureclass" # this one will be created
arcpy.env.overwriteOutput = True
sr = arcpy.Describe(fc_in).spatialReference  

# create the empty output featureclass  
path, name = os.path.split(fc_out)  
arcpy.CreateFeatureclass_management(path, name, "POLYGON", fc_in, "SAME_AS_TEMPLATE", "SAME_AS_TEMPLATE", sr)  

# insert the features into the output fc  
with arcpy.da.SearchCursor(fc_in, '*') as curs_in:  
    flds_in = curs_in.fields  
    idx_cnt = flds_in.index(fld_count)  
    with arcpy.da.InsertCursor(fc_out, '*') as curs_out:  
        for row in curs_in:  
            cnt = row[idx_cnt]  
            for i in range(0, cnt):  
                curs_out.insertRow(row)  

 if __name__ == '__main__':  
 main() 
  • When you run it on points, does it create the point features? – Tom Dec 29 '15 at 18:56
  • Is there a particular reason you're using cursors rather than just CopyFeatures_management? – Tom Dec 29 '15 at 18:59
  • It does run on point features no problem. I am using a cursor because I am trying to insert rows based upon a field value, in this case the PHASE_COUNT field. Copy features wouldn't necessarily copy the features as many times as is listed in the phase count field would it? – Damien Dec 29 '15 at 20:07
  • The code looks good. Are you trying to insert point features into a polygon feature class? – Emil Brundage Dec 29 '15 at 22:24
  • That's good to hear. No, I am trying to insert polygon features into a polygon feature class. – Damien Dec 29 '15 at 22:29
3

"Polygon, polyline, or multipoint features can only be created using the SHAPE@ token." This tidbit from the arcgis resource center helped. I first updated the code to list all the fields in the feature class I was trying to copy (instead of using '*'), but instead of listing the SHAPE_AREA and SHAPE_LENGTH with all the other fields I changed it say just 'SHAPE@' instead. Problem solved..

def main():  
import arcpy
import os
env = r"T:\900.Users\Kerwin\HousinStudy\D1.gdb"
from arcpy import env
fc_in = r"T:\900.Users\Kerwin\HousinStudy\D1.gdb\central_sorted" # this one exists  
fld_count = "PHASE_COUNT"  
fc_out = r"T:\900.Users\Kerwin\HousinStudy\D1.gdb\central_sorted_out" # this one will be created  
sr = arcpy.Describe(fc_in).spatialReference
path = r"T:\900.Users\Kerwin\HousinStudy\D1.gdb"
name = "central_sorted_out"
arcpy.env.overwriteOutput = True
fieldnames = [u'OBJECTID', u'SHAPE', u'APN', u'STREET', u'CITY', u'SITUSZIP', u'JURIS', u'TMSA', u'TOD_CENTER', u'YEARBLT', u'DECADE_CLASS',\
              u'ZONING', u'LU_DESC', u'LU_CLASS', u'BLDG_DESC', u'SQFEET', u'FAR', u'TMRPA_ID', u'ALT_ZONING', u'ALT_ZONING_DESCRIPTION',\
              u'DEV_CLASS', u'DU_APPROVED', u'TM', u'TM_NAME', u'PUD', u'PUD_NAME', u'BUBBLE', u'ATOMIC', u'FINAL_MAPPED', u'MF_ID', u'DU_2013',\
              u'BUILT_DENSITY', u'TOTAL_DENSITY', u'DENSITY_CLASS', u'TMRPA_USE', u'VACANCY_STATUS', u'ZONING_CLASS', u'ZONING_CLASS_DESCRIPTION',\
              u'RESIDENTIAL_CLASS1', u'RESIDENTIAL_CLASS2', u'TOTAL_ACRES', u'UNCONSTRAINED_ACRES', u'NOTES', u'C_TOD_NAME', u'RSF_1', u'RSF_2', u'RSF_3',\
              u'RSF_4', u'RSF_5', u'RSF_7', u'RSF_8', u'RSF_9', u'RSF_10', u'RSF_11', u'RSF_12', u'RSF_14', u'modeledUnits', u'Model_Type',\
              u'Overall_Res_Suitability', u'ScenarioSubArea', u'ZoneName', u'MODEL_FLAG', u'MODEL_YEAR', u'MODEL_POP', u'PHASED', u'PHASE_COUNT',\
              u'SOURCE', 'SHAPE@']


# create the empty output featureclass  
##    path, name = os.path.split(fc_out)
arcpy.MakeFeatureLayer_management("T:\900.Users\Kerwin\HousinStudy\D1.gdb\central_sorted", "input_layer")
arcpy.CreateFeatureclass_management(path, name, "POLYGON", "input_layer", "DISABLED", "DISABLED", sr)  

# insert the features into the output fc  
with arcpy.da.SearchCursor(fc_in, fieldnames) as curs_in:  
    flds_in = curs_in.fields  
    idx_cnt = flds_in.index(fld_count)  
    with arcpy.da.InsertCursor(fc_out, fieldnames) as curs_out:  
        for row in curs_in:  
            cnt = row[idx_cnt]  
            for i in range(0, cnt):  
                curs_out.insertRow(row)

if __name__ == '__main__':  
main()

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.