Select to view content in your preferred language

fail to build polygon geometry from Lat/Long

2337
11
Jump to solution
09-23-2016 01:00 PM
KevinBell
Deactivated User
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)
0 Kudos
1 Solution

Accepted Solutions
DarrenWiens2
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])‍‍‍‍‍‍‍‍‍‍‍

View solution in original post

11 Replies
DanPatterson_Retired
MVP Emeritus

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)

0 Kudos
KevinBell
Deactivated User

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)
                     ])
0 Kudos
DanPatterson_Retired
MVP Emeritus

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)

0 Kudos
KevinBell
Deactivated User

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

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

0 Kudos
DarrenWiens2
MVP Honored Contributor

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

0 Kudos
KevinBell
Deactivated User

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

0 Kudos
DarrenWiens2
MVP Honored Contributor

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

0 Kudos
KevinBell
Deactivated User

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

0 Kudos