1

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

txtFile=arcpy.GetParameterAsText(1)
shpOUTpolygon=arcpy.GetParameterAsText(2)
shpOUTpoint=arcpy.GetParameterAsText(3)

en=os.path.dirname(shpOUTpolygon)

env.workspace = en
FCpolygon=os.path.basename(shpOUTpolygon)

FCpoint=os.path.basename(shpOUTpoint)


mxd=arcpy.mapping.MapDocument('current')
df=arcpy.mapping.ListDataFrames(mxd)
dfr=df[0]
sp=dfr.spatialReference

arcpy.CreateFeatureclass_management(en, FCpolygon, "polygon","#","#","#",sp)
cursor =arcpy.da.InsertCursor (FCpolygon, ["SHAPE@"])
array=arcpy.Array()
point=arcpy.Point()
for line in fileinput.input(txtFile):
    point.ID, point.X, point.Y=line.split()
    array.add(point)
polygon = arcpy.Polygon(array)
cursor.insertRow([polygon])
fileinput.close()
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?

3
  • 5
    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, 2019 at 14:52
  • ie, move your point=arcpy.Point() line into the for loop Nov 25, 2019 at 22:04
  • How does your input file looks like, containing lat and lon? Nov 27, 2019 at 9:46

2 Answers 2

2

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
1
  • 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, 2019 at 22:08
-1

As commented by @Vince:

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). http://desktop.arcgis.com/en/arcmap/latest/analyze/python/writing-geometries.htm

When you commented:

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.

I think you thought a comment by @Vince came from me, but I made no comments on this Q&A.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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