2011-06-24 99 views
1

問題:我需要爲表中的每個建築物選擇至少2個藥房和2個教育中心,方圓1公里內的所有POI(藥房,商業中心,醫療中心,教育中心,警察局,消防局),距離各自建築1公里。表結構 - >postgis高級(?)選擇查詢

建築物(ID的序列,名稱VARCHAR)

poi_category(ID的序列,CNAME VARCHAR)--cname是當然

POI(ID的序列,名稱VARCHAR,C_ID的類別名稱整數) - C_ID是FK引用poi_category(ID)

所有的座標列的類型都是幾何不是地理的(姑且稱之爲GEOM)

以下是我認爲它應該做的方式,但我不確定它甚至是正確的讓阿洛ne對此問題的最佳解決方案

SELECT r.id_b, r.id_p 
FROM (
    SELECT b.id AS id_b, p.id AS id_p, pc.id AS id_pc,pc.cname 
    FROM building AS b, poi AS p, poi_category AS pc 
    WHERE ST_DWithin(b.geom,p.geom, 1000) AND p.c_id=pc.id 
    ) AS r, 
    (
    SELECT * FROM r GROUP BY id_b 
    ) AS r1 

HAVING count (
        SELECT * 
        FROM r, r1 
        WHERE r1.id_b=r.id_b AND r.id_pc='pharmacy' 

       )>1 
      AND 
      count (
        SELECT * 
        FROM r, r1 
        WHERE r1.id_b=r.id_b AND r.id_pc='ed. centre' 

       )>1 

這是我需要的方式嗎?從性能角度來看,哪種解決方案更好?最優雅的解決方案呢? 我也在這裏發佈:http://gis.stackexchange.com/questions/11445/postgis-advanced-selection-query

回答

3

這是我闡述的解決方案。這是我能找到的最快的一個,但它仍然很慢。鑑於任務的性質,我懷疑它可以做得更快......

WITH 
building AS (
    SELECT way, osm_id 
    FROM osm_polygon 
    WHERE tags @> hstore('building','yes') 
    --ORDER BY 1 
    LIMIT 1000 
), 
pharmacy AS (
    SELECT way 
    FROM osm_poi 
    WHERE tags @> hstore('amenity','pharmacy') 
), 
school AS (
    SELECT way 
    FROM osm_poi 
    WHERE tags @> hstore('amenity','school') 
) 
SELECT ST_AsText(building.way) AS geom, building.osm_id AS label 
FROM building 
WHERE 
    (SELECT count(*) > 1 
    FROM pharmacy 
    WHERE ST_DWithin(building.way,pharmacy.way,1000)) 
    AND 
    (SELECT count(*) > 1 
    FROM school 
    WHERE ST_DWithin(building.way,school.way,1000)) 

你的。 S.