1

I've created polygons from a table that has two columns- an OBJECTID and a string of coordinates (Polygon_Coordinates). The resulting polygons (called "testing" in the code below) are in the correct locations, but I can't seem to figure out how to carry over the ID, which I need for a join later in the workflow.

Any idea on how to build geometry, but maintain the attributes?

fields = ['Polygon_Coordinates', 'OBJECTID']

with arcpy.da.SearchCursor(table, fields) as table_cursor:
    for row in table_cursor:
        #skip null rows
        if row[0] != '-9999':
            
            #turn string of coordinates into a list but split them at the commas
            coords= list(row[0].split(','))
            
            #break up list into list of pairs
            pairs = [coords[i:i + 2] for i in range(0, len(coords), 2)]
            coord_ints = [[float(i) for i in p] for p in pairs]

            #create insert cursor for geometry creation
            geocursor = arcpy.da.InsertCursor(testing, ["SHAPE@"])
            array = arcpy.Array()
            
            #grab pairs from list, put into array
            for a in pairs:
                array.add(arcpy.Point(a[1],a[0], ID=row[1]))
            
            #set crs, build polygon 
            wgs = arcpy.SpatialReference(4326)
            poly = arcpy.Polygon(array, wgs)
            geocursor.insertRow([poly])
            
1
  • 1
    Add a new field and calculate newfield = ObjectID in the original table then transfer this to the new
    – BERA
    May 22 at 6:42
1

OBJECTID fields are a unique type of field that will not carry over well when changing up a feature class or creating a new feature class. It's why having a unique identifier for each row is highly encouraged for record keeping as OBJECTID should not be used for that.

Without an alternative UID to use for joins, BERA's comment to have a new field and enter the objectID into that new field is really the only way to do it.

0

So you have an empty featurclass called "testing", where that is stored the object ID field is either called objectid in a geodatabase featureclass or FID in a shapefile, either way they are fields maintained by ArcGIS. You need to create your empty featureclass with an extra field, call in "PID" for polygon ID and make sure its type long.

Your main search cursor returns the list of coordinates and an ID value, this would be row[1] so grab that value and store it in a variable, say pid = row[1].

Your code is currently inserting the ID into the geometry and not as an attribute so correct that to just array.add(arcpy.Point(a[1],a[0]))

Finally the insert cursor inserts a row which is typically a tuple. So update that to:

geocursor.insertRow((poly,pid))

As a side note, convention is to return from your search cursor, ID then geometry in the field list and insert it in that order. If everyone follows the same convention it gets easier to read other people's code.

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.