# fail to build polygon geometry from Lat/Long

1566
11
09-23-2016 01:00 PM
Occasional Contributor III
import arcpy
array = arcpy.Array([arcpy.Point(0, 0),
arcpy.Point(0, 1000),
arcpy.Point(1000, 1000),
arcpy.Point(1000, 0)
])
polygon = arcpy.Polygon(array)
cursor = arcpy.da.InsertCursor(r'C:\path\to\your\geodatabase.gdb\polygon', ['SHAPE@'])
cursor.insertRow([polygon])

‍‍‍‍‍‍‍‍‍‍The code below works as expected, but fails if I substitute Lat/Longs in the arcpy.Point.  Is that expected?
Tags (2)
1 Solution

Accepted Solutions
MVP Honored Contributor

Can you try this?


import arcpy
fc = r'C:\path\to\your\geodatabase.gdb\polygon'
sr = arcpy.Describe(fc).spatialReference # get spatial reference
array = arcpy.Array([arcpy.Point(-111.8256, 40.691085),
arcpy.Point(-111.825347, 40.691614),
arcpy.Point(-111.824706, 40.691896),
arcpy.Point(-111.825592, 40.68703)
])
polygon = arcpy.Polygon(array,sr) # create polygon with spatial reference
cursor = arcpy.da.InsertCursor(fc, ['SHAPE@'] )
cursor.insertRow([polygon])‍‍‍‍‍‍‍‍‍‍‍
11 Replies
MVP Legendary Contributor

Kevin can you format your code using syntax highlighting  /blogs/dan_patterson/2016/08/14/script-formatting

EDIT  What are the longitudes and latitude?  some example?  longitude first (X), latitude 2nd (Y)

Occasional Contributor III

this doesn't work:

array = arcpy.Array([arcpy.Point(-111.8256, 40.691085),
arcpy.Point(-111.825347, 40.691614),
arcpy.Point(-111.824706, 40.691896),
arcpy.Point(-111.825592, 40.68703)
])‍‍‍‍‍
MVP Legendary Contributor

is the polygon ring (aka points) clockwise in orientation?  and non-intersecting? (i'm not going to draw it out... the numbers just got weird)

Occasional Contributor III

I drew it out and it looks like it's clockwise, non-intersecting.

MVP Honored Contributor

How does it fail? With an error? If so, what's the full error?

I suspect it's because you don't set a spatial reference anywhere, but we'll see.

MVP Honored Contributor

Can you provide an example where it fails? Or does your given example fail?

Occasional Contributor III

when I pass in the -111.8256, 40.691085 as above a record is created in the table, but w/ empty geometry.

MVP Honored Contributor

What is the spatial reference of your 'polygon' feature class?

Occasional Contributor III

GCS_WGS_1984 is the spref of the FC I'm inserting into.