I’m trying to write Python script to convert points (vertices) with Lat/Long to Polygons

3604
3
Jump to solution
10-04-2016 08:11 PM
FredF
by
New Contributor

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.

Tags (2)
0 Kudos
1 Solution

Accepted Solutions
curtvprice
MVP Esteemed Contributor

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:

  • construct an array of points
  • create a polygon geometry
  • write to a shape field inside an update cursor

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) 

Bug?: Creating polygons with holes using Arcpy 

Examples of Geometry object - polygon and intersect

View solution in original post

3 Replies
DanPatterson_Retired
MVP Esteemed Contributor

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

curtvprice
MVP Esteemed Contributor

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:

  • construct an array of points
  • create a polygon geometry
  • write to a shape field inside an update cursor

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) 

Bug?: Creating polygons with holes using Arcpy 

Examples of Geometry object - polygon and intersect

RandyBurton
MVP Regular Contributor

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.

  1. Organize your data in Excel with the x-y data in columns with other attributes.
  2. Create a concatenate formula to write a line of Python code containing the x-y data and copy it down the column.
  3. Copy the column containing the code into a python editor.
  4. Clean up code as required using search and replace.
  5. Run script to create a shapefile.
  6. Add a projection file such as WGS 1984.

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."‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍