I have a set of municipality maps, with an ID for each municipality, and a Multipolygon for the geometry. In PostGIS, I've put these data into tables. Table 1 has the municipalities in year 2019 (each row is 1 muni with ID and geometry), and Table 2 has the municipalities in year 2005. Spatial indices for both tables. About 400 rows in each table.
Here are some stats on this data: All in the same projection: WGS 84
NPoints | Avg | Max | Min 2019: | 14378 | 379875 | 9 2005: | 24675 | 556334 | 5
Num Geometries | Avg | Max | Min 2019: | 63 | 4402 | 1 2005: | 131 | 9480 | 1
ST_Valid: 4 invalid geometries out of about 400 in each set.
Basically what I want to do is look at the intersection and see how similar the two geographies are across time. So, I wrote the query below.
SELECT m1.munid, ST_Intersection(m1.wkb_geometry, m2.wkb_geometry)
INTO intersect_table
FROM m2019 m1
LEFT JOIN m2005 m2 on m1.munid = m2.munid
Essentially I join based on the muni IDs (which don't change across time), then create the intersection. But this query basically doesn't finish, I've left it for 30 minutes and I can't imagine it should take longer. Am I being unreasonable or am I going about this incorrectly?
ST_Intersects
is a (boolean) spatial relation check operator; you are looking forST_Intersection
to create a geometry, which should complain about any invalid geometries...but also is not the most straight forward way to measure similarities; at least useST_Difference
. Better suited are measures like differences inST_Area
andST_Perimeter
, or using e.g.ST_FrechetDistance
on the boundaries. A spatial index is of no use here, a PRIMARY KEY or index onmunid
may be (although I doubt PG will bother using it at all for 400 rows each).ST_NumGeometries
andST_NPoints
? And check forST_IsValid
?ST_Differentce, ST_Area, ST_Perimeter, ST_FrechetDistance
table A
will get compared against each geometry in the matching MultiPolygon oftable B
-> on average, for each joined row you force the DB to compare ~8000 geometries, with ~ 1.8 million operations on vertices (this is a ballpark figure based on your averages, but the magnitude is correct). x 400!