Problem - Zero length and area for polygon generated with Python script using insert cursor to ArcPro map

2067
5
Jump to solution
09-28-2021 07:40 AM
DougEaton
Emerging Contributor

I'm having a problem with the following script.  It writes the feature successfully with all of the attributes, but it does not draw the polygon, as the shape length and area are both zero.  This script worked previously with ArcPro 2.5, but that project became corrupted so I re-created it in ArcPro 2.8.  I'm not sure that has anything to do with the issue.

In reading other posts, it appears the two most significant factors for this script to work are setting the spatial reference and also using "Shape@" to write the polygon coordinates from the array.  The spatial reference of the map and the feature class are the same- "NAD 1983 (2011) StatePlane Illinois East FIPS 1201 (US Feet)."

Any help is very much appreciated.  

import arcpy

NWLongitude = 41.936811
NWLatitude = -88.754027
NELongitude = 41.936811
NELatitude = -88.750635
SWLongitude = 41.934087
SWLatitude = -88.754027
SELongitude = 41.934087
SELatitude = -88.750635

Item = "Test Area"

fc = r'G:\Arc\ArcPro\Public Works\MyProject6\MyProject6.gdb\Test_Polygon'

# Important: the spatial reference must be set, or the geometry will not locate properly.

sr = arcpy.Describe(fc).spatialReference # ***Since Lat/Long Coordinates are provided, need Geographic
print("{0} : {1}".format(fc, sr.name))

# Define the coordinate array for the 4-point polygon that is passed to the feature to map it.
# Note that the feature is plotted with the 4 points in a clockwise direction - NW, NE, SE, SW.

coordinateArray = arcpy.Array([arcpy.Point(NWLongitude, NWLatitude),
arcpy.Point(NELongitude, NELatitude),
arcpy.Point(SELongitude, SELatitude),
arcpy.Point(SWLongitude, SWLatitude)])
polygon = arcpy.Polygon(coordinateArray, sr)

# Create Insert Cursor to write feature using polygon coordinateArray above.
# "SHAPE@" is a special designation that passes through the polygon geometry statement directly above.
# Note that these are the exact field names in the Test_Polygon feature.

cursorFC = arcpy.da.InsertCursor(fc, ["SHAPE@", "NWlat", "NWlong", "NElat", "NElong", "SWlat", "SWlong", "SElat",
"SElong", "Item"])

# Loading the variables

cursorFC.insertRow(
[polygon, NWLatitude, NWLongitude, NELatitude, NELongitude, SWLatitude, SWLongitude, SELatitude, SELongitude,
Item])

# Delete cursor object
del cursorFC

 

Tags (2)
0 Kudos
1 Solution

Accepted Solutions
DonMorrison1
Frequent Contributor

Your code worked OK for me when I ran it against a feature class with a spatial reference whose unit of measure was degrees (NAD 1983).  At least the polygon that it generated looks correct.

View solution in original post

0 Kudos
5 Replies
DavidPike
MVP Frequent Contributor

Sorry I've not checked the script thoroughly, but it seems that you've not closed the polygon (clockwise array should end with a repeat of the first coordinates).

0 Kudos
DougEaton
Emerging Contributor

Thank you for the suggestion David.  I changed the code to add the first coordinate pair again at the end, however I am getting the same result.

0 Kudos
DavidPike
MVP Frequent Contributor

Oh wait, what's up with the projection? The geometry has to be in the same units as the projection, you cant just use lat longs, or am I missing something?

0 Kudos
DonMorrison1
Frequent Contributor

Your code worked OK for me when I ran it against a feature class with a spatial reference whose unit of measure was degrees (NAD 1983).  At least the polygon that it generated looks correct.

0 Kudos
DougEaton
Emerging Contributor

David and Don, you are right.  I was using a State Plane Coordinate System, which is a projected coordinate system, but using Lat/Long coordinates I need a geographic coordinate system like NAD 83.  Thank you!

0 Kudos