TypeError: value #1 - unsupported type: Polygon

2334
15
Jump to solution
07-14-2021 06:52 AM
OrRubinstein
New Contributor II

Hey,

I'm trying to add new rows into empty polygon feature.

this is the code:

try:

cur = arcpy.da.InsertCursor(outshp, ["Id" , 'Floor_area'])
file = open(csvfile)
reader = csv.reader(file)
line = next(reader)
cnt = 0

for line in reader:
new_row = [line[0]]
points = []


for i in line[1:]:
p = i.split(',')
point = arcpy.Point(p[0], p[1])
points.append(point)
arr = arcpy.Array(points)
polygn = arcpy.Polygon(arr)
new_row.append(polygn)
cur.insertRow(new_row)
cnt += 1
del cur

except:
traceback.print_exc()
del cur

 

sadly, I'm getting this error:

 
 
 
 
Traceback (most recent call last):
  File "<ipython-input-8-44580ffd528c>", line 21, in <module>
    cur.insertRow(new_row)
TypeError: value #1 - unsupported type: Polygon
 
Can you understand why?
 
Thanks

 

0 Kudos
1 Solution

Accepted Solutions
OrRubinstein
New Contributor II

Turns out that the problem was with the projection.

thanks for the help.

this is the code that works:

import csv
import arcpy
import traceback

#Create polygon feature from csv file

csvfile = r'C:\Geography\Spatial Python\final\Final_Ex\Buildings_alternative_2.csv'
outpath = r'C:\geography\Spatial Python\final'
outshp =  'build.shp'

outshp = arcpy.CreateFeatureclass_management (
    outpath, outshp, geometry_type='POLYGON',
    spatial_reference=arcpy.SpatialReference(2039))

arcpy.AddField_management (outshp, 'Floor_area', "LONG") #add field for Floor area

try:

    cur = arcpy.da.InsertCursor(outshp, ['Id', 'Floor_area', 'SHAPE@'])
    f = open(csvfile)
    reader = csv.reader(f)
    lne = next(reader)
    cnt = 1
    

    for line in reader: #looping lines in the csv file
        new_row = [cnt, line[0]] # index of the row and the floor area field
        points = []
        
        
        for p in line[1:]: #looping the coordinates in every line
            pnt = p.split(',') #split x and y coordinates
            point = arcpy.Point(float(pnt[0]), float(pnt[1])) #create a point feature
            points.append(point) #add to the point list
        arr = arcpy.Array(points) 
        polygon = arcpy.Polygon(arr)
        new_row.append(polygon)
        cur.insertRow(new_row)
        cnt += 1
        
    del cur
        

View solution in original post

0 Kudos
15 Replies
DavidPike
MVP Frequent Contributor

I'm finding the code very hard to read without indentation.

0 Kudos
OrRubinstein
New Contributor II

Sorry, had trouble copying the code/

here it is:

 

try:

    cur = arcpy.da.InsertCursor(outshp, ["Id" , 'Floor_area'])
    file = open(csvfile)
    reader = csv.reader(file)
    line = next(reader)
    cnt = 0

    for line in reader:
        new_row = [line[0]]
        points = []


        for i in line[1:]:
            p = i.split(',')
            point = arcpy.Point(p[0], p[1])
            points.append(point)
        arr = arcpy.Array(points)
        polygn = arcpy.Polygon(arr)
        new_row.append(polygn)
        cur.insertRow(new_row)
        cnt += 1
    del cur

except:
    traceback.print_exc()
    del cur

0 Kudos
DanPatterson
MVP Esteemed Contributor

you don't provide a value for your "Id" just for the polygon..


... sort of retired...
0 Kudos
OrRubinstein
New Contributor II

i rewrote it:

 

try:

    cur = arcpy.da.InsertCursor(outshp, ["Id" , 'Floor_area'])
    file = open(csvfile)
    reader = csv.reader(file)
    line = next(reader)
    cnt = 0

    for line in reader:
        new_row = [cnt, line[0]]
        points = []
        
        
        for i in line[1:]:
            p = i.split(',')
            point = arcpy.Point(p[0], p[1])
            points.append(point)
        arr = arcpy.Array(points)
        polygn = arcpy.Polygon(arr)
        new_row.append(polygn)
        cur.insertRow(new_row)
        cnt += 1
    del cur

except:
    traceback.print_exc()
    del cur
    

 

but when I do provide value for Id get another error:

 

Traceback (most recent call last):
  File "<ipython-input-9-a3290e7f669b>", line 21, in <module>
    cur.insertRow(new_row)
TypeError: sequence size must match size of the row

 

 

 

 

0 Kudos
DanPatterson
MVP Esteemed Contributor

check the examples in InsertCursor—ArcGIS Pro | Documentation

example 3 is what you are providing... just a shape, but you need to provide an Id value since you specified it in your fields

 ["Id" , 'Floor_area']

 


... sort of retired...
0 Kudos
DanPatterson
MVP Esteemed Contributor

Code formatting ... the Community Version - Esri Community will help with that and provide line numbers


... sort of retired...
BlakeTerhune
MVP Regular Contributor

First, I recommend using a with statement for opening files and cursors so they get closed automatically when exiting (with or without an error). Second, it looks like you're trying to insert a row with polygon geometry (shape) without specifying a shape field when you open the cursor. If Floor_area is just that, a double field representing the area measurement of a polygon, that's what you need to insert.

 

try:
    cnt = 0
    with open(file, "r") as csv_reader:
        with arcpy.da.InsertCursor(outshp, ["Id", "Floor_area"]) as cur:
            for csv_line in csv_reader:
                polygon_points = []
                for point in line[1:]:
                    x, y = point.split(",")
                    point_geometry = arcpy.Point(x, y)
                    polygon_points.append(point_geometry)
                polygon_geometry = arcpy.Polygon(arcpy.Array(polygon_points))
                polygon_geometry_sqft = polygon_geometry.getArea(units="SQUAREFEET")
                cur.insertRow((csv_line[0], polygon_geometry_sqft))
                cnt += 1
except:
    traceback.print_exc()

 

 If you do want to insert the polygon shape, you'll need to include "SHAPE@" in the fields and add the polygon_geometry into the values for insertRow().

0 Kudos
OrRubinstein
New Contributor II

Thanks.

'Floor area' field is actully not a geometry field, so I edite it:

try:
    cnt = 0
    with open(csvfile, "r") as csv_reader:
        with arcpy.da.InsertCursor(outshp, ["Id", "Floor_area", 'SHAPE@']) as cur:
            for csv_line in csv_reader:
                polygon_points = []
                for point in line[1:]:
                    x, y = point.split(",")
                    point_geometry = arcpy.Point(x, y)
                    polygon_points.append(point_geometry)
                polygon_geometry = arcpy.Polygon(arcpy.Array(polygon_points))
                polygon_geometry_sqft = polygon_geometry.getArea(units="SQUAREFEET")
                cur.insertRow((csv_line[0], polygon_geometry_sqft))
                cnt += 1
except:
    traceback.print_exc()

 

but now i get this:

Traceback (most recent call last):
  File "<ipython-input-40-433ebdb7888e>", line 12, in <module>
    polygon_geometry_sqft = polygon_geometry.getArea(units="SQUAREFEET")
  File "C:\Program Files\ArcGIS\Pro\Resources\ArcPy\arcpy\arcobjects\arcobjects.py", line 1609, in getArea
    return convertArcObjectToPythonObject(self._arc_object.GetArea(*gp_fixargs((method, units))))
TypeError: Invalid geometry type for method
0 Kudos
BlakeTerhune
MVP Regular Contributor

If you add SHAPE@ to the InsertCursor, you'll also need to include a value for that when you call insertRow(). Line 13 should be

cur.insertRow((csv_line[0], polygon_geometry_sqft, polygon_geometry))

 

 As for the TypeError: Invalid geometry type for method, make sure the points in your CSV make a valid polygon. Alternatively, just leave out Floor_area completely, then calculate that field after you've inserted all the new features.

0 Kudos