Skip to content

Instantly share code, notes, and snippets.

@jczaplew
Created April 13, 2016 16:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jczaplew/95aa1ac76bead7381563f94f59a4c146 to your computer and use it in GitHub Desktop.
Save jczaplew/95aa1ac76bead7381563f94f59a4c146 to your computer and use it in GitHub Desktop.
PostGIS vs shapely union
Download this - http://www.geologyontario.mndmf.gov.on.ca/mndmfiles/pub/data/imaging/MRD126-REV1//MRD126-REV1.zip
createdb union_test
psql -c "CREATE EXTENSION postgis" union_test
ogr2ogr ontario4326.shp -t_srs "EPSG:4326" ~/Downloads/MRD126-REV1/MRD126-REV1/Shapefiles/Geology/Geopoly.shp && shp2pgsql -s 4326 ontario4326.shp public.ontario | psql union_test
psql -c "VACUUM ANALYZE" union_test
psql -c "EXPLAIN ANALYZE SELECT ST_Union(geom) FROM ontario" union_test
import time
import psycopg2
from shapely.wkt import loads
from shapely.ops import cascaded_union
connection = psycopg2.connect(dbname="union_test")
cursor = connection.cursor()
start = time.time()
cursor.execute("SELECT ST_AsText(geom) FROM ontario")
polygons = cursor.fetchall()
unioned = cascaded_union([ loads(poly[0]) for poly in polygons ])
print time.time() - start
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment