4

I have an array of coordinates

Point= [lat,long]

ListPoint= [[38.203655,-76.113281],[38.10,-76.12],..]

I wanna make a polygon that contains all these points ? There is a project developed by Flicker - The shape of Alpha but the project is not open.

Do you have any ideas how can I do that.

  • 2
    Are the coordinates the vertices of the polygon that you want to create or do you want to create a polygon that contains these points? – ustroetz Apr 11 '14 at 8:08
  • Yes, I wanna create a polygon that contains these points. Generating one. – user3378649 Apr 11 '14 at 8:33
5

The following explains (1.) how to construct technically a polygon from points and (2.) different methods to construct a polygon from points.


1. Tool to create a polygon from point coordinates

I would use the GDAL Python bindings to do that.

To give you a starting point have a look at the following script:

from osgeo import ogr

# you have a list of points
listPoint = [[13.415449261665342, 52.502674590782519],[13.416039347648621, 52.50250152147968],[13.415787220001221, 52.501845158120446],[13.416162729263306, 52.502201097675766],[13.415406346321104, 52.502334982450677],[13.415111303329468,52.50204435400651]]

enter image description here

# Add the points to the ring
ring = ogr.Geometry(ogr.wkbLinearRing)
for point in listPoint:
    lat = point[0]
    lon = point[1]
    print lat, lon
    ring.AddPoint(lat,lon)


# Add first point again to ring to close polygon
ring.AddPoint(listPoint[0][0], listPoint[0][3])

# Add the ring to the polygon
poly = ogr.Geometry(ogr.wkbPolygon)
poly.AddGeometry(ring)
print poly.ExportToJson()

enter image description here


2. Methods to create a polygon from point coordinates

There are two problems to think about when creating a polygon from points.

  • The order of the points
  • Points that might be contained by the polygon

Two methods to approach this problem:

  1. Convex Hull: It is the smallest convex set containing all points. You can see it as a rubber band wrapped around the “outside” points.

  2. Delaunay triangulation: All points are connected to each other.

  • Should that be: ring.AddPoint(listPoint[0][0], listPoint[0][1]) ? – HeikkiVesanto May 12 '17 at 11:46
1

The closest thing we have in Python to a standard built-in Polygon is defined in "A Python Protocol for Geospatial Data": https://gist.github.com/sgillies/2217756.

The most straightforward way to get a Python polygon is to do this:

# you have a list of points
listPoint = [[13.415449261665342, 52.502674590782519],[13.416039347648621, 52.50250152147968],[13.415787220001221, 52.501845158120446],[13.416162729263306, 52.502201097675766],[13.415406346321104, 52.502334982450677],[13.415111303329468,52.50204435400651]]

polygon = {'type': 'Polygon', 'coordinates': [listPoint]}

If you want to serialize this into GeoJSON for use with web mapping software, you'll need to reverse the order of coordinate pairs (longitude comes first in GeoJSON, KML, etc.):

polygon = {'type': 'Polygon', 'coordinates': [[pt[::-1] for pt in listPoint]]}

import json
print json.dumps(polygon)

Ustroetz's example does the same thing (ExportToJson() produces the same result as json.dumps(polygon) above, but indirectly through the osgeo.gdal API.

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.