I'm working in ArcGIS 10.4.1 and trying to write a script (in Python) for making a polygon shapefile from latitude/longitude.



env.workspace = en



arcpy.CreateFeatureclass_management(en, FCpolygon, "polygon","#","#","#",sp)
cursor =arcpy.da.InsertCursor (FCpolygon, ["SHAPE@"])
for line in fileinput.input(txtFile):
    point.ID, point.X, point.Y=line.split()
polygon = arcpy.Polygon(array)
del cursor

For beginning, I test script on XY coordinates. It works fine. First, I choose some projected coordinate system manually (in Data Frame Properties of Map), then I run my script and created shapefile is correct.

But when I input longitude and latitude - no errors while script is working, but something wrong with polygon's geometry in output shapefile.

Where should I set a right spatialReference of polygon?

  • 4
    Never, ever use the Polygon constructor without a SpatialReference (spatial_reference is the second parameter). But your problem is failure to create new Point objects, so the Array just has one repeating vertex. The code samples provide the template, and they create new Point objects for each loop iteration (it's best practice to close the ring). desktop.arcgis.com/en/arcmap/latest/analyze/python/… – Vince Nov 25 '19 at 14:52
  • ie, move your point=arcpy.Point() line into the for loop – Son of a Beach Nov 25 '19 at 22:04
  • How does your input file looks like, containing lat and lon? – Andreas Müller Nov 27 '19 at 9:46

To answer yout question about how to set a spatialReference of a polygon layer:

arcpy.CreateFeatureclass_management(path, fc, "POLYGON")
# arcpy.AddField_management(fc, "NAME", "TEXT")
sr = arcpy.SpatialReference("the_name_of_file_of_the_crs")
arcpy.DefineProjection_management(fc, sr)

This is, what I use to define the spatial reference. It takes a name, known by arcgis or a file-path with a .prj extension. Other options are decribed in the documentation linked above. After that use DefineProjection to store it into the FeatureClass.

  • path would be your workspace
  • fc is the name of the feature class
  • AddField_management is optional
  • Andreas Müller, thanks for you answer, but my code contains already a spatial reference of feature class. The question was about spatial reference of polygon. PolyGeo♦ answered above and my problem is solved, I just can't mark his comment as answer. – Alex Nov 27 '19 at 22:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.