2017-08-31 16 views
2

我有一個與(非空間)表格具有一對多關係的醫院(點)的空間表格,價格表(cirurgy,urgency等)在Postgis中如何從空間表格和相關(非空間)表格之間的一對多關係構建Geojson

hospital(id,name,geom); (id,name,valence)

「name」是常用字段。

如何在PostgreSQL/Postgis中構建一個有效的Geojson,其中每個醫院(點)可以有一個或多個價位?

我已經試過這個查詢的一些變體,但總是給出「用作表達式的子查詢返回的多個行」的錯誤。

SELECT row_to_json(fc) FROM 
( 
    SELECT 'FeatureCollection' As type, array_to_json(array_agg(f)) As features from 

    (
    SELECT 'Feature' As type, ST_AsGeoJSON(lg.geom)::json As geometry, row_to_json 

     ((select l from (select 
     v.* 
       FROM valence v 
       inner join hospital lg on lg."name" = v."name") As l 
     )) As properties 
     from hospital as lg) 

    As f 
) As fc; 

非常感謝!

回答

2

與此查詢,「價」的列表返回一個JSON數組中的財產properties.valences

SELECT 
    json_build_object(
    'type', 'FeatureCollection', 
    'features', json_agg(
     json_build_object(
     'type', 'Feature', 
     'geometry', ST_AsGeoJSON(h.geom)::json, 
     'properties', json_build_object(
      'name', h.name, 
      'valences', (
      -- Generate json array of "valences": 
      SELECT array_to_json(array_agg(v.valence)) 
      FROM valence v 
      WHERE v.name = h.name 
      GROUP BY v.name 
     ) 
     ) 
    ) 
    ) 
) json 
FROM 
    hospital h 

返回GeoJSON的對象根據http://geojsonlint.com/

+1

很多,很多的感謝是有效的,thibautg! –