TypeError: value #1 - unsupported type: Polygon

723
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
15 Replies
OrRubinstein
New Contributor II

Thank to some of the advices here the error has been resolved, but the feature don't  includes coordinates and doesn't appear on the map... i can't really understand why.

this is all of the code:

import csv
import arcpy
import traceback

#Create Polygon layer from csv table

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

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

arcpy.AddField_management (outshp, 'Floor_area', "LONG")
arcpy.AddField_management (outshp, 'SHAPE@', "DOUBLE")

try:

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

    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)
        polygon = arcpy.Polygon(arr)
        new_row.append(polygon)
        cur.insertRow(new_row)
        cnt += 1
    del cur
        
        

except:
    traceback.print_exc()
    del cur
    

 

the table includes the 'floor area' field but not the spatial data.

OrRubinstein_0-1626280643737.png

 

0 Kudos
BlakeTerhune
MVP Regular Contributor

Could you post some sample rows from the CSV?

0 Kudos
OrRubinstein
New Contributor II

OrRubinstein_0-1626280969553.png

 

0 Kudos
BlakeTerhune
MVP Regular Contributor

It's hard to get the full picture of what's happening with this screenshot. It would help if you could post the raw text of a few rows (with header row).

From what I can see here, column A is your "Id" field? Is there a variable number of point coordinate columns? Also, I see you're creating a "SHAPE@" field in the shapefile. That's not how that's intended to work. It's what's called a field token. It's not a field name that actually exists.

InsertCursor—ArcMap | Documentation (arcgis.com)

Here's some updated code but I'm not certain it will solve your problem.

import csv
import arcpy
import traceback

#Create Polygon layer from csv table

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

try:
    outshp = arcpy.CreateFeatureclass_management(
        outpath, outshp, geometry_type='POLYGON',
        spatial_reference=arcpy.SpatialReference(4326)
    )

    arcpy.AddField_management (outshp, 'Id', "TEXT")
    arcpy.AddField_management (outshp, 'Floor_area', "DOUBLE")

    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, polygon_geometry))
except:
    traceback.print_exc()

Try grabbing a single row of point coordinates from your CSV and hard coding them into your script (no CSV looping). Use those values to try inserting a single record into the shapefile. Start small to troubleshoot.

OrRubinstein
New Contributor II

thease are the first 2 rows, the coloumns are from left to right:

180178168.85730000027,642304.1140000001178228.20880000014,642355.0789000001178319.57019999996,642248.6835999992178260.21879999992,642197.7185999993178168.85730000027,642304.1140000001
180178303.8627000004,642163.7553000003178358.5828999998,642219.6634999998178458.8058000002,642121.5704999994178404.08559999987,642065.6622000001178303.8627000004,642163.7553000003

 

Column A is Floor area and the rest are coodinates (x,y in every cell). the Id added automatically to the table, i'm not sure how but i can see that on arcGis pro. The variable for the id it "cnt".

there are varaibles for the coordinates and i'm looping on them here:

 

        for p in line[1:]: #looping the coordinates in every line
            pnt = p.split(',') #split x and y coordinates
            point = arcpy.Point(pnt[0], 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

 

 

I'm not familiar with the commands you wrote in the code, but the table i get is partial and i get this error:


  File "<ipython-input-26-e60a0c54162d>", line 17, in <module>
    arcpy.AddField_management (outshp, 'Id', "TEXT")
  File "C:\Program Files\ArcGIS\Pro\Resources\ArcPy\arcpy\management.py", line 4337, in AddField
    raise e
  File "C:\Program Files\ArcGIS\Pro\Resources\ArcPy\arcpy\management.py", line 4334, in AddField
    retval = convertArcObjectToPythonObject(gp.AddField_management(*gp_fixargs((in_table, field_name, field_type, field_precision, field_scale, field_length, field_alias, field_is_nullable, field_is_required, field_domain), True)))
  File "C:\Program Files\ArcGIS\Pro\Resources\ArcPy\arcpy\geoprocessing\_base.py", line 511, in <lambda>
    return lambda *args: val(*gp_fixargs(args, True))
arcgisscripting.ExecuteError: Failed to execute. Parameters are not valid.
ERROR 000012: Id already exists
Failed to execute (AddField).

 

 

 

0 Kudos
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