I’m trying to write Python script to convert points (vertices) with Lat/Long to Polygons. The points are stored in Excel sheet and these points representing the edges of an area structure. I have the Structure ID’s, the order no of the points, Lat and Long. The no of points that can draw a polygon vary from structures to another, for example in case of a soccer filed there will be 4 points but in case of an outdoor recreation area I might be having 8 – 10 points. So the no of points is not fixes for every area structure (polygon). Thanks for your help.
Solved! Go to Solution.
The trickiest part may be getting your boundaries into a python list. If the data are in excel you will probably need to structure them in a table format so ArcGIS can read them that way,
FLD1 FLD2
poly1 1
x y
x y
x y
x y
poly2 2
x y
...
These would come in as two string fields, which you would then need run a SearchCursor loop over to construct Python data in a list like this:
["poly1", 1, [[x,y], [x,y], [x,y], [x,y]]]
["poly2", 2, [[x,y], .... ]]
Then, if there aren't holes involved, writing polygon geometry is pretty straight forward if you follow the steps:
I believe the examples in the help are useful but way too sparse. Here's a script I posted to GeoNet that worked for me:
There are also many other examples on GeoNet and elsewhere to consult:
arcpy - Python script that creates polygon feature class with given coordinates (nice simple example)
So assuming you can bring in the points to a useable format into arcmap, where are you along the process of coding the writing of the geometries?
Writing geometries—Help | ArcGIS for Desktop
Your script so far would help to guide
The trickiest part may be getting your boundaries into a python list. If the data are in excel you will probably need to structure them in a table format so ArcGIS can read them that way,
FLD1 FLD2
poly1 1
x y
x y
x y
x y
poly2 2
x y
...
These would come in as two string fields, which you would then need run a SearchCursor loop over to construct Python data in a list like this:
["poly1", 1, [[x,y], [x,y], [x,y], [x,y]]]
["poly2", 2, [[x,y], .... ]]
Then, if there aren't holes involved, writing polygon geometry is pretty straight forward if you follow the steps:
I believe the examples in the help are useful but way too sparse. Here's a script I posted to GeoNet that worked for me:
There are also many other examples on GeoNet and elsewhere to consult:
arcpy - Python script that creates polygon feature class with given coordinates (nice simple example)
If you are starting with x-y data in Excel, here's a possible workflow. I like to use Excel's concatenate function to write code (Python, SQL, etc.). I have also used shapefile.py found at GeospatialPython to create shapefiles.
The attached file contains an example of this process. The script to create the shapefile looks like this:
import shapefile
filename = 'myshape'
w = shapefile.Writer(shapefile.POLYGON)
# edit field definitions as needed
w.field('NAME','C',40,0)
w.poly(parts=[[[25.9, 29.2], [22.942, 29.2], [22.942, 34.533], [25.9, 34.533], [25.9, 29.2]]])
w.record('one')
...
# Save shapefile
w.save(filename)
print "Done."