Hilfe gesucht bei Postgis spatial join
Hallo zusammen, gegeben sei eine OSM Datenbank mit separaten Tabellen für Punkte und Polygone im osm2pgsql schema: planet_osm_hstore_polygon planet_osm_hstore_point Ziel ist es nun eine Sicht (ggfs. materialized) zu erstellen, die in etwa so aussieht: tags: hstore way: ST_Centroid(polygon) feature1: boolean feature2: boolean feature1 und feature2 sollen jeweils true sein, wenn sich Punkte aus der point Tabelle mit dieser Eigenschaft (amenity=feature1) innerhalb der Polygongeometrie befinden. Vermutlich läuft die Frage darauf raus ob man ein spatial join innerhalb eines CASE WHEN machen kann. Gruss Sven -- "If you don't make lower-resolution mapping data publicly available, there will be people with their cars and GPS devices, driving around with their laptops" (Tim Berners-Lee) /me is giggls@ircnet, http://sven.gegg.us/ on the Web
Hi Sven, ich hoffe, ich habs richtig verstanden: Also sobald auch nur ein Punktfeature mit einer bestimmten Eigenschaft innerhalb des Polygons liegt, soll der Wert true sein? Dann hätte ich folgende Abfrage mit Dummylisten zum schnellen ausprobieren: SELECT poly.id AS pid, bool_or(COALESCE(_ST_Intersects(poly.geom, pt.geom) AND pt.feat = 'foo', FALSE)) AS f1_test, bool_or(COALESCE(_ST_Intersects(poly.geom, pt.geom) AND pt.feat = 'bar', FALSE)) AS f2_test FROM (VALUES (1, 'POLYGON((0 0,2 0,2 2,0 2,0 0))'::geometry), (2, 'POLYGON((2 2,4 2,4 4,2 4,2 2))'::geometry)) AS poly(id, geom) LEFT JOIN (VALUES (1, 'foo', 'POINT(1 1)'::geometry), (2, 'bar', 'POINT(2 2)'::geometry)) AS pt(id, feat, geom) ON poly.geom && pt.geom GROUP BY poly.id; Kann grad nicht einschätzen, ob es schneller ist, ST_Centroid erst in einer übergeordneten Abfrage anzuwenden. Grüße Felix Gesendet: Montag, 24. September 2018 um 17:28 Uhr Von: "Sven Geggus" <lists@fuchsschwanzdomain.de> An: fossgis-talk-liste@fossgis.de Betreff: [FOSSGIS-Talk] Hilfe gesucht bei Postgis spatial join Hallo zusammen, gegeben sei eine OSM Datenbank mit separaten Tabellen für Punkte und Polygone im osm2pgsql schema: planet_osm_hstore_polygon planet_osm_hstore_point Ziel ist es nun eine Sicht (ggfs. materialized) zu erstellen, die in etwa so aussieht: tags: hstore way: ST_Centroid(polygon) feature1: boolean feature2: boolean feature1 und feature2 sollen jeweils true sein, wenn sich Punkte aus der point Tabelle mit dieser Eigenschaft (amenity=feature1) innerhalb der Polygongeometrie befinden. Vermutlich läuft die Frage darauf raus ob man ein spatial join innerhalb eines CASE WHEN machen kann. Gruss Sven -- "If you don't make lower-resolution mapping data publicly available, there will be people with their cars and GPS devices, driving around with their laptops" (Tim Berners-Lee) /me is giggls@ircnet, http://sven.gegg.us/ on the Web -- .................................................................... FOSSGIS und OpenStreetMap im OSGeo Park auf der Intergeo 2018 16.-18. Oktober 2018, Messe Frankfurt(M) https://www.intergeo.de[https://www.intergeo.de] FOSSGIS 2019, die Konferenz für Open Source GIS mit OpenData und OpenStreetMap in Dresden! 13.-16. März 2019 an der HTW Dresden https://fossgis-konferenz.de/2019/[https://fossgis-konferenz.de/2019/] FOSSGIS Veranstaltungen 2018 https://www.fossgis.de/node/306[https://www.fossgis.de/node/306] FOSSGIS e.V, der Verein zur Förderung von Freier Software aus dem GIS-Bereich und Freier Geodaten! https://www.fossgis.de/[https://www.fossgis.de/] https://twitter.com/fossgis_eV ____________________________________________________________________ FOSSGIS-Talk-Liste mailing list FOSSGIS-Talk-Liste@fossgis.de https://lists.fossgis.de/mailman/listinfo/fossgis-talk-liste[https://lists.fossgis.de/mailman/listinfo/fossgis-talk-liste]
Felix Kunde <felix-kunde@gmx.de> wrote:
ich hoffe, ich habs richtig verstanden: Also sobald auch nur ein Punktfeature mit einer bestimmten Eigenschaft innerhalb des Polygons liegt, soll der Wert true sein?
Genau.
Dann hätte ich folgende Abfrage mit Dummylisten zum schnellen ausprobieren:
SELECT poly.id AS pid, bool_or(COALESCE(_ST_Intersects(poly.geom, pt.geom) AND pt.feat = 'foo', FALSE)) AS f1_test, bool_or(COALESCE(_ST_Intersects(poly.geom, pt.geom) AND pt.feat = 'bar', FALSE)) AS f2_test FROM (VALUES (1, 'POLYGON((0 0,2 0,2 2,0 2,0 0))'::geometry), (2, 'POLYGON((2 2,4 2,4 4,2 4,2 2))'::geometry)) AS poly(id, geom) LEFT JOIN (VALUES (1, 'foo', 'POINT(1 1)'::geometry), (2, 'bar', 'POINT(2 2)'::geometry)) AS pt(id, feat, geom) ON poly.geom && pt.geom GROUP BY poly.id;
Danke. Das sieht doch schon mal super aus. Um das mit realen Daten auszuprobieren muss ich die Query auf eine BBox begrenzen. Normalerweise habe ich da sowas im query: SELECT ... WHERE ... and geom && SetSRID('BOX3D(x1 y1, x2 y2)'::box3d,4326) Wo müsste das hin? Hinter "&& pt.geom"? Und eine zweite Frage noch, was bedeutet der Unterstrich vor ST_Intersects? Sven -- "If you don't make lower-resolution mapping data publicly available, there will be people with their cars and GPS devices, driving around with their laptops" (Tim Berners-Lee) /me is giggls@ircnet, http://sven.gegg.us/ on the Web
Hm, die zusätzliche BBox-Filterung würde ich mit einem WITH-Block voranstellen, so dass du nur mit den gefilterten Polygonen in den JOIN mit den Punkten gehst. _ST_Intersects macht das gleiche wie ST_Intersects nur ohne zusätzlichen BoundingBox-Tests mit &&. Der passiert ja schon im JOIN, deshalb kannst du es dir im SELECT sparen. ST_Intersects ruft intern && und dann _ST_Intersects auf ;) Grüße Gesendet: Donnerstag, 27. September 2018 um 11:05 Uhr Von: "Sven Geggus" <lists@fuchsschwanzdomain.de> An: fossgis-talk-liste@fossgis.de Betreff: Re: [FOSSGIS-Talk] Hilfe gesucht bei Postgis spatial join Felix Kunde <felix-kunde@gmx.de> wrote:
ich hoffe, ich habs richtig verstanden: Also sobald auch nur ein Punktfeature mit einer bestimmten Eigenschaft innerhalb des Polygons liegt, soll der Wert true sein?
Genau.
Dann hätte ich folgende Abfrage mit Dummylisten zum schnellen ausprobieren:
SELECT poly.id AS pid, bool_or(COALESCE(_ST_Intersects(poly.geom, pt.geom) AND pt.feat = 'foo', FALSE)) AS f1_test, bool_or(COALESCE(_ST_Intersects(poly.geom, pt.geom) AND pt.feat = 'bar', FALSE)) AS f2_test FROM (VALUES (1, 'POLYGON((0 0,2 0,2 2,0 2,0 0))'::geometry), (2, 'POLYGON((2 2,4 2,4 4,2 4,2 2))'::geometry)) AS poly(id, geom) LEFT JOIN (VALUES (1, 'foo', 'POINT(1 1)'::geometry), (2, 'bar', 'POINT(2 2)'::geometry)) AS pt(id, feat, geom) ON poly.geom && pt.geom GROUP BY poly.id;
Danke. Das sieht doch schon mal super aus. Um das mit realen Daten auszuprobieren muss ich die Query auf eine BBox begrenzen. Normalerweise habe ich da sowas im query: SELECT ... WHERE ... and geom && SetSRID('BOX3D(x1 y1, x2 y2)'::box3d,4326) Wo müsste das hin? Hinter "&& pt.geom"? Und eine zweite Frage noch, was bedeutet der Unterstrich vor ST_Intersects? Sven -- "If you don't make lower-resolution mapping data publicly available, there will be people with their cars and GPS devices, driving around with their laptops" (Tim Berners-Lee) /me is giggls@ircnet, http://sven.gegg.us/ on the Web -- .................................................................... FOSSGIS und OpenStreetMap im OSGeo Park auf der Intergeo 2018 16.-18. Oktober 2018, Messe Frankfurt(M) https://www.intergeo.de[https://www.intergeo.de] FOSSGIS 2019, die Konferenz für Open Source GIS mit OpenData und OpenStreetMap in Dresden! 13.-16. März 2019 an der HTW Dresden https://fossgis-konferenz.de/2019/[https://fossgis-konferenz.de/2019/] FOSSGIS Veranstaltungen 2018 https://www.fossgis.de/node/306[https://www.fossgis.de/node/306] FOSSGIS e.V, der Verein zur Förderung von Freier Software aus dem GIS-Bereich und Freier Geodaten! https://www.fossgis.de/[https://www.fossgis.de/] https://twitter.com/fossgis_eV ____________________________________________________________________ FOSSGIS-Talk-Liste mailing list FOSSGIS-Talk-Liste@fossgis.de https://lists.fossgis.de/mailman/listinfo/fossgis-talk-liste[https://lists.fossgis.de/mailman/listinfo/fossgis-talk-liste]
Felix Kunde <felix-kunde@gmx.de> wrote:
Hm, die zusätzliche BBox-Filterung würde ich mit einem WITH-Block voranstellen, so dass du nur mit den gefilterten Polygonen in den JOIN mit den Punkten gehst.
Genau mit dem Bounding Box Ding habe ich dann wohl noch ein Problem. Das hier läuft brauchbar schnell: SELECT poly.osm_id AS osm_id, ST_Centroid(poly.way) AS way, poly.tags AS tags, bool_or(COALESCE(ST_Intersects(poly.way, pt.way) AND pt.tags->'amenity' = 'toilets', FALSE)) AS toilets, bool_or(COALESCE(ST_Intersects(poly.way, pt.way) AND pt.tags->'amenity' = 'shower', FALSE)) AS shower, bool_or(COALESCE(ST_Intersects(poly.way, pt.way) AND pt.tags->'amenity' = 'fast_food', FALSE)) AS fast_food, bool_or(COALESCE(ST_Intersects(poly.way, pt.way) AND pt.tags->'amenity' = 'restaurant', FALSE)) AS restaurant FROM planet_osm_polygon AS poly LEFT JOIN planet_osm_hstore_point AS pt ON poly.way && pt.way WHERE ((poly.tags ? 'tourism') AND (poly.tags->'tourism' = 'camp_site')) and poly.way && ST_Transform(ST_SetSRID('BOX3D(-1.27 44.54, -1.18 44.61)'::box3d,4326),900913) GROUP BY poly.osm_id, poly.way, poly.tags; GroupAggregate (cost=64559.58..197859.20 rows=1 width=305) (actual time=182.114..182.597 rows=5 loops=1) Group Key: planet_osm_hstore_polygon.osm_id, planet_osm_hstore_polygon.way, planet_osm_hstore_polygon.tags -> Sort (cost=64559.58..64876.96 rows=126952 width=386) (actual time=181.907..181.913 rows=57 loops=1) Sort Key: planet_osm_hstore_polygon.osm_id, planet_osm_hstore_polygon.way, planet_osm_hstore_polygon.tags Sort Method: quicksort Memory: 83kB -> Nested Loop Left Join (cost=5587.68..53797.91 rows=126952 width=386) (actual time=181.306..181.768 rows=57 loops=1) -> Bitmap Heap Scan on planet_osm_hstore_polygon (cost=5160.87..5168.90 rows=1 width=269) (actual time=181.172..181.233 rows=5 loops=1) Recheck Cond: ((way && '01030000A031BF0D000100000005000000620FC606FE4101C1E7AA5812542B55410000000000000000620FC606FE4101C11C62EBE00236554100000000000000005B0C3BFEE70800C11C62EBE00236554100000000000000005B0C3BFEE70800C1E7AA5812542B55410000000000000000620FC606FE4101C1E7AA5812542B55410000000000000000'::geometry) AND (tags ? 'tourism'::text)) Filter: ((tags -> 'tourism'::text) = 'camp_site'::text) Rows Removed by Filter: 1 Heap Blocks: exact=6 -> BitmapAnd (cost=5160.87..5160.87 rows=2 width=0) (actual time=181.137..181.137 rows=0 loops=1) -> Bitmap Index Scan on planet_osm_hstore_polygon_index (cost=0.00..111.02 rows=2462 width=0) (actual time=1.824..1.824 rows=1393 loops=1) Index Cond: (way && '01030000A031BF0D000100000005000000620FC606FE4101C1E7AA5812542B55410000000000000000620FC606FE4101C11C62EBE00236554100000000000000005B0C3BFEE70800C11C62EBE00236554100000000000000005B0C3BFEE70800C1E7AA5812542B55410000000000000000620FC606FE4101C1E7AA5812542B55410000000000000000'::geometry) -> Bitmap Index Scan on planet_osm_hstore_polygon_tags_index (cost=0.00..5049.60 rows=457280 width=0) (actual time=179.281..179.281 rows=481513 loops=1) Index Cond: (tags ? 'tourism'::text) -> Bitmap Heap Scan on planet_osm_hstore_point pt (cost=426.81..48502.06 rows=12695 width=117) (actual time=0.072..0.099 rows=11 loops=5) Recheck Cond: (planet_osm_hstore_polygon.way && way) Heap Blocks: exact=18 -> Bitmap Index Scan on planet_osm_hstore_point_index (cost=0.00..423.63 rows=12695 width=0) (actual time=0.059..0.059 rows=11 loops=5) Index Cond: (planet_osm_hstore_polygon.way && way) Planning time: 2.021 ms Execution time: 182.778 ms Als view geht das dann leider nicht mehr: CREATE OR REPLACE VIEW poi_campsites AS SELECT poly.osm_id AS osm_id, ST_Centroid(poly.way) AS way, poly.tags AS tags, bool_or(COALESCE(ST_Intersects(poly.way, pt.way) AND pt.tags->'amenity' = 'toilets', FALSE)) AS toilets, bool_or(COALESCE(ST_Intersects(poly.way, pt.way) AND pt.tags->'amenity' = 'shower', FALSE)) AS shower, bool_or(COALESCE(ST_Intersects(poly.way, pt.way) AND pt.tags->'amenity' = 'fast_food', FALSE)) AS fast_food, bool_or(COALESCE(ST_Intersects(poly.way, pt.way) AND pt.tags->'amenity' = 'restaurant', FALSE)) AS restaurant FROM planet_osm_polygon AS poly LEFT JOIN planet_osm_hstore_point AS pt ON poly.way && pt.way WHERE ((poly.tags ? 'tourism') AND (poly.tags->'tourism' = 'camp_site')) GROUP BY poly.osm_id, poly.way, poly.tags; explain analyze select * from poi_campsites where way && ST_Transform(ST_SetSRID('BOX3D(-1.27 44.54, -1.18 44.61)'::box3d,4326),900913); Subquery Scan on poi_campsites (cost=41128163.32..102046296.60 rows=457 width=89) (actual time=6411.384..6411.877 rows=5 loops=1) -> GroupAggregate (cost=41128163.32..102046292.03 rows=457 width=305) (actual time=6411.383..6411.872 rows=5 loops=1) Group Key: planet_osm_hstore_polygon.osm_id, planet_osm_hstore_polygon.way, planet_osm_hstore_polygon.tags -> Sort (cost=41128163.32..41273206.47 rows=58017260 width=386) (actual time=6411.219..6411.226 rows=57 loops=1) Sort Key: planet_osm_hstore_polygon.osm_id, planet_osm_hstore_polygon.way, planet_osm_hstore_polygon.tags Sort Method: quicksort Memory: 83kB -> Nested Loop Left Join (cost=5802.59..23335182.28 rows=58017260 width=386) (actual time=6030.722..6411.066 rows=57 loops=1) -> Bitmap Heap Scan on planet_osm_hstore_polygon (cost=5381.79..1601893.38 rows=457 width=269) (actual time=6030.604..6410.332 rows=5 loops=1) Recheck Cond: (tags ? 'tourism'::text) Filter: ((st_centroid(way) && '01030000A031BF0D000100000005000000620FC606FE4101C1E7AA5812542B55410000000000000000620FC606FE4101C11C62EBE00236554100000000000000005B0C3BFEE70800C11C62EBE00236554100000000000000005B0C3BFEE70800C1E7AA5812542B55410000000000000000620FC606FE4101C1E7AA5812542B55410000000000000000'::geometry) AND ((tags -> 'tourism'::text) = 'camp_site'::text)) Rows Removed by Filter: 360002 Heap Blocks: exact=361065 -> Bitmap Index Scan on planet_osm_hstore_polygon_tags_index (cost=0.00..5381.67 rows=457290 width=0) (actual time=179.533..179.533 rows=481517 loops=1) Index Cond: (tags ? 'tourism'::text) -> Bitmap Heap Scan on planet_osm_hstore_point pt (cost=420.80..47429.48 rows=12695 width=117) (actual time=0.101..0.129 rows=11 loops=5) Recheck Cond: (planet_osm_hstore_polygon.way && way) Heap Blocks: exact=18 -> Bitmap Index Scan on planet_osm_hstore_point_index (cost=0.00..417.63 rows=12695 width=0) (actual time=0.076..0.076 rows=11 loops=5) Index Cond: (planet_osm_hstore_polygon.way && way) Planning time: 6.408 ms Execution time: 6412.063 ms Lese ich das richtig, dass die Datenbank im zweiten Fall den index auf der tags spalte verwendet statt dem Geometrieindex? Denn das wäre natürlch tödlich. Gruss Sven -- "Thinking of using NT for your critical apps? Isn't there enough suffering in the world?" (Advertisement of Sun Microsystems in Wall Street Journal) /me is giggls@ircnet, http://sven.gegg.us/ on the Web
participants (2)
-
Felix Kunde -
Sven Geggus