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?
Polygon
constructor without aSpatialReference
(spatial_reference
is the second parameter). But your problem is failure to create new Point objects, so theArray
just has one repeating vertex. The code samples provide the template, and they create newPoint
objects for each loop iteration (it's best practice to close the ring). desktop.arcgis.com/en/arcmap/latest/analyze/python/…point=arcpy.Point()
line into thefor
loop