read Geometry pass to insert cursor

1186
9
Jump to solution
03-25-2020 04:19 PM
jaykapalczynski
Frequent Contributor

I am trying to read a FC geometry which has one record in it

Grab this geometry and use it in an INSERT cursor with specific attributes

Think I am close but cant get the geometry of the FCs feature...the FC has only one....

geometries = arcpy.CopyFeatures_management('Counties_3_dissolved', arcpy.Geometry())
length = sum([g.length for g in geometries])
print('Total length: {}'.format(length))

arcpy.env.overwriteOutput = True


# Create a feature class 
result1 = arcpy.management.CreateFeatureclass(
    arcpy.env.scratchGDB, 
    "esri_square", "POLYGON", spatial_reference=26917)
feature_class = result1[0]

inputFeatureclass = "C:\Data\SelectCounties.gdb\Counties_4_final_append"
# Write feature to new feature class
with arcpy.da.InsertCursor(inputFeatureclass, ['SHAPE@', 'ADDRESS', 'DISTANCE', 'UNIQUEID', 'COUNTYLIST']) as cursor:
    cursor.insertRow([geometries, searchAddress, searchDistance, searchid, txt_list])

# Delete cursor object
del cursor
0 Kudos
1 Solution

Accepted Solutions
JoshuaBixby
MVP Esteemed Contributor

Taking what Dan said about a list being returned, what about changing the update line:

cursor.insertRow([geometries[0], searchAddress, searchDistance, searchid, txt_list])

View solution in original post

0 Kudos
9 Replies
DanPatterson_Retired
MVP Emeritus

which help topic did you get this line from?

geometries = arcpy.CopyFeatures_management('Counties_3_dissolved', arcpy.Geometry())

is it specific to the arcgis api for python?

0 Kudos
DanPatterson_Retired
MVP Emeritus

it returns a list even if there is one. Did you try adding them in the insertRow one at a time?

0 Kudos
JoshuaBixby
MVP Esteemed Contributor

Taking what Dan said about a list being returned, what about changing the update line:

cursor.insertRow([geometries[0], searchAddress, searchDistance, searchid, txt_list])

0 Kudos
JakeSkinner
Esri Esteemed Contributor

Not sure if you're getting all the variables you're trying to insert to the new feature class from the Counties_3_dissolved feature class, but if you are you could use something like below:

with arcpy.da.SearchCursor('Counties_3_dissolved', ["SHAPE@", "address", "distance", "id", "txt_list"]) as cursor:
        for row in cursor:
            geom = row[0]
            searchAddress = row[1]
            searchDistance = row[2]
            searchid = row[3]
            txt_list = row[4]
del cursor

# Write feature to new feature class
inputFeatureclass = r"C:\Data\SelectCounties.gdb\Counties_4_final_append"
with arcpy.da.InsertCursor(inputFeatureclass, ['SHAPE@', 'ADDRESS', 'DISTANCE', 'UNIQUEID', 'COUNTYLIST']) as cursor:
    cursor.insertRow((geom, searchAddress, searchDistance, searchid, txt_list))
del cursor
0 Kudos
jaykapalczynski
Frequent Contributor

I get this to work BUT i have a GlobalID in my feature class that does not get written too....how do I create the globalID

# GET THE GEOMETRY FROM THE DISSOLVED FC and PASS THAT TO THE INSERT CURSOR BELOW ALONG WITH THE ATTRIBUTE VALUES
geometries = arcpy.CopyFeatures_management('Counties_3_dissolved', arcpy.Geometry())
arcpy.env.overwriteOutput = True

inputFeatureclass = "C:\Work\CountySelection\Data\SelectCounties.gdb\Counties_4_final_append"

## Write feature to new feature class
with arcpy.da.InsertCursor(inputFeatureclass, ['SHAPE@', 'ADDRESS', 'DISTANCE', 'UNIQUEID', 'COUNTYLIST']) as cursor:
cursor.insertRow([geometries[0], searchAddress, searchDistance, searchid, txt_list])
# Delete cursor object
del cursor
0 Kudos
JoshuaBixby
MVP Esteemed Contributor

So you have a field of GlobalID type in inputFeatureclass and nothing is getting populated when you insert a record using the cursor?

0 Kudos
jaykapalczynski
Frequent Contributor

Yea I figured when I add a new row the GlobalID would be populated automatically

0 Kudos
jaykapalczynski
Frequent Contributor

think I fixed it....delete the old one and created globalIDs again and it seems to be working....weird