Django create Polygon from list of lat/long coordinates

41 Views Asked by At

I have a list of lat/long coordinates that make up a polygon. How do I instantiate a Polygon, to be used as a PolygonField?

1

There are 1 best solutions below

0
zevloo On

first you need to convert those coordinates to meters (if they are in degrees), then create a linear ring to insert into a polygon object

from django.contrib.gis.geos import LinearRing
from django.contrib.gis.geos import GEOSGeometry
from django.contrib.gis.gdal import SpatialReference, CoordTransform
from django.contrib.gis.geos import Polygon

#transformation of coordinates
lring = LinearRing(//tuple with point coordinates//)
#example SRID 4326 to 32719
coord = SpatialReference(4326) 
mycoord = SpatialReference(32719)
trans = CoordTransform(mycoord, coord)
linear_r = GEOSGeometry(lring).coords

#then create polygon from linear ring
polygon = Polygon(linear_r)

linear rings has the same first and last point coordinates to close the ring