Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@djq
Created November 22, 2014 21:23
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save djq/c4cb0760ca35d3850bd1 to your computer and use it in GitHub Desktop.
Save djq/c4cb0760ca35d3850bd1 to your computer and use it in GitHub Desktop.
How to union a list of geometries in GeoDjango
from django.contrib.gis.geos import GEOSGeometry
# sample data
geom_1 = GEOSGeometry('POLYGON((-71.8 42.1,-70.6 42.1,-70.5 41.2,-71.8 41.2,-71.8 42.1))')
geom_2 = GEOSGeometry('POLYGON((-71.12 42.23,-71.48 42.34,-71.52 42.55,-71.12 42.23))')
geom_3 = GEOSGeometry('POLYGON((-73.12 42.23,-71.48 42.34,-71.52 42.55,-73.12 42.23))')
polygons = [geom_1, geom_2, geom_3]
# get first polygon
polygon_union = polygons[0]
# update list
polygons = polygons[1:]
# loop through list of polygons
for poly in polygons:
polygon_union = polygon_union.union(poly)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment