使用 GeoJSON (jsonb) 的 Postgis 查询 ST_Intersects
Postgis Query ST_Intersects with GeoJSON (jsonb)
给定一个 PostgreSQL 9.4 jsonb 列 'location',其内容如下:
{"type": "FeatureCollection","features": [
{
"properties": {},
"geom": "01030000000100000004000000000000000000244000000000000049400000000000002840000000000000494000000000000026400000000000004A4000000000000024400000000000004940",
"type": "Feature",
"geometry": {"type": "Polygon", "coordinates": [[[10,50],[12,50],[11,52],[10,50]]]}
},
{..},
{..}
]}
要检索功能键 'geom' 我使用此查询:
SELECT geom
FROM (
SELECT jsonb_array_elements(t.location -> 'features') -> 'geom' AS geom
FROM my_object t) AS g
WHERE geom IS NOT NULL;
有效。但是现在我想像这样进行 Postgis ST_Intersects
查询:
SELECT ST_Intersects(g.geom, ST_GeomFromText('POINT(11 51)'))
FROM (
SELECT t.iid, (jsonb_array_elements(t.location -> 'features') -> 'geom') AS geom
FROM my_object t) AS g;
不起作用,因为 g.geom 交付为 jsonb
:function st_intersects(jsonb, geometry) does not exist
。我尝试转换为 text
,然后错误是 function st_intersects(text, geometry) is not unique
。我如何处理 jsonb
结果作为 Postgis 函数的输入?
要从 geojson 创建 postgis 几何类型,您可以使用函数 ST_GeomFromGeoJSON
例如:
SELECT ST_GeomFromGeoJSON('{"type":"Point","coordinates":[-48.23456,20.12345]}');
首先,函数jsonb_array_elements()
设置为returns所以你应该把它当成table function in a FROM
clause。其次,您应该使用 ->>
运算符获取 jsonb
字段的文本表示,然后将其转换为 geometry
,然后在 PostGIS 函数中使用。
检索不属于 NULL
的几何:
SELECT f.value ->> 'geom' AS geom
FROM my_object t
JOIN LATERAL jsonb_array_elements(t.location -> 'features') f ON true
WHERE f.value -> 'geom' IS NOT NULL;
相交:
SELECT t.iid, ST_Intersects((f.value->>'geom')::geometry, ST_GeomFromText('POINT(11 51)'))
FROM my_object t
JOIN LATERAL jsonb_array_elements(t.location -> 'features') f ON true;
您可能想将一些 properties
添加到 select 列表中,以便您可以区分 table.
中每一行的多个几何图形
给定一个 PostgreSQL 9.4 jsonb 列 'location',其内容如下:
{"type": "FeatureCollection","features": [
{
"properties": {},
"geom": "01030000000100000004000000000000000000244000000000000049400000000000002840000000000000494000000000000026400000000000004A4000000000000024400000000000004940",
"type": "Feature",
"geometry": {"type": "Polygon", "coordinates": [[[10,50],[12,50],[11,52],[10,50]]]}
},
{..},
{..}
]}
要检索功能键 'geom' 我使用此查询:
SELECT geom
FROM (
SELECT jsonb_array_elements(t.location -> 'features') -> 'geom' AS geom
FROM my_object t) AS g
WHERE geom IS NOT NULL;
有效。但是现在我想像这样进行 Postgis ST_Intersects
查询:
SELECT ST_Intersects(g.geom, ST_GeomFromText('POINT(11 51)'))
FROM (
SELECT t.iid, (jsonb_array_elements(t.location -> 'features') -> 'geom') AS geom
FROM my_object t) AS g;
不起作用,因为 g.geom 交付为 jsonb
:function st_intersects(jsonb, geometry) does not exist
。我尝试转换为 text
,然后错误是 function st_intersects(text, geometry) is not unique
。我如何处理 jsonb
结果作为 Postgis 函数的输入?
要从 geojson 创建 postgis 几何类型,您可以使用函数 ST_GeomFromGeoJSON
例如:
SELECT ST_GeomFromGeoJSON('{"type":"Point","coordinates":[-48.23456,20.12345]}');
首先,函数jsonb_array_elements()
设置为returns所以你应该把它当成table function in a FROM
clause。其次,您应该使用 ->>
运算符获取 jsonb
字段的文本表示,然后将其转换为 geometry
,然后在 PostGIS 函数中使用。
检索不属于 NULL
的几何:
SELECT f.value ->> 'geom' AS geom
FROM my_object t
JOIN LATERAL jsonb_array_elements(t.location -> 'features') f ON true
WHERE f.value -> 'geom' IS NOT NULL;
相交:
SELECT t.iid, ST_Intersects((f.value->>'geom')::geometry, ST_GeomFromText('POINT(11 51)'))
FROM my_object t
JOIN LATERAL jsonb_array_elements(t.location -> 'features') f ON true;
您可能想将一些 properties
添加到 select 列表中,以便您可以区分 table.