Select to view content in your preferred language

Arcpy writes a shapefile with wrong coordinates

965
3
02-25-2021 06:15 AM
OlesiaIgnateva
New Contributor

Hello!

I want to write a polygon shapefile from a dictionary with vertices coordinates using arcpy, here is my code:

def _getShp (inFC, outFC, vertices):

# this is the method using arcpy
# Create a new polygon feature class using coordinates.
# Each coordinate entry is in the format of {ID: [(X1,Y1), (X2,Y2)....]}
cur = None
try:
    # Create the output feature class
    arcpy.CreateFeatureclass_management(os.path.dirname(outFC),
    os.path.basename(outFC),
    "POLYGON")
    # Access spatial reference of template to define spatial
    # reference of geometries
    spatial_reference = arcpy.Describe(inFC).spatialReference
    # Open an insert cursor for the new feature class
    cur = arcpy.da.InsertCursor(outFC, ["SHAPE@"])
    # Create an array object needed to create features
    # Initialize a variable for keeping track of a feature's ID.
    # ID = -1
    for fid in range(len(vertices.keys())):
        array = arcpy.Array()
        # Add the point to the feature's array of points
        # If the ID has changed, create a new feature
        for i in range(len(vertices[fid])):
            array.add(arcpy.Point(vertices[fid][i][0], vertices[fid][i][1],  ID=fid))

        polygon = arcpy.Polygon(array, spatial_reference)
        cur.insertRow([polygon])
except Exception as e:
    print(e)
finally:
# Clean up the cursor if necessary
    if cur:
       del cur

 

The problem is that it writes my file wrong. Instead of positive number of X coordinate it writes a negative number, while the Y coordinate stays the same, e.g. the coordinates were around +381462 and became -367560. The coordinate system is WGS 39N. In the end I have got a wrong location. What's wrong could be in the code? Please find a screenshots in attached, but don't look at the polygons' shape, it is another problem I am aware of it 🙂

Снимок экрана 2021-02-25 151242.jpg

Снимок экрана 2021-02-25 151218.jpg

 

 

0 Kudos
3 Replies
DavidPike
MVP Frequent Contributor

not too sure, can you try assigning the spatial reference with an EPSG code rather than a template?

also in:

array.add(arcpy.Point(vertices[fid][i][0], vertices[fid][i][1], ID=fid))

I guess it should be "ID" not ID (which you have as a variable set to -1 - also not sure what that ID of -1 is meant to be doing).

 

Finally please use the code formatting available to make it readable, all the indents are gone in what you've posted.

0 Kudos
DanPatterson
MVP Esteemed Contributor

Code formatting ... the Community Version - GeoNet, The Esri Community is the link for formatting


... sort of retired...
OlesiaIgnateva
New Contributor

Thank you very much! 🙂

0 Kudos