给出PostgreSQL 9.4 jsonb列' location'内容如下:
string[] arr = new string[]
{
"line1 text // random stuff",
"line2 text // random stuff",
"line3 text // random stuff",
"line4 text // random stuff",
"line5 text // random stuff"
};
arr = arr.Select(x => x.Split('/')[0].Trim()).ToArray();
要检索功能键' geom'我使用这个查询:
{"type": "FeatureCollection","features": [
{
"properties": {},
"geom": "01030000000100000004000000000000000000244000000000000049400000000000002840000000000000494000000000000026400000000000004A4000000000000024400000000000004940",
"type": "Feature",
"geometry": {"type": "Polygon", "coordinates": [[[10,50],[12,50],[11,52],[10,50]]]}
},
{..},
{..}
]}
作品。但现在我想像这样做一个Postgis SELECT geom
FROM (
SELECT jsonb_array_elements(t.location -> 'features') -> 'geom' AS geom
FROM my_object t) AS g
WHERE geom IS NOT NULL;
查询:
ST_Intersects
不起作用,因为g.geom以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;
传递:jsonb
。我尝试转换为function st_intersects(jsonb, geometry) does not exist
,然后错误为text
。我如何处理function st_intersects(text, geometry) is not unique
结果作为Postgis函数的输入?
答案 0 :(得分:3)
首先,函数jsonb_array_elements()
返回一个集合,因此你应该将它作为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
,以便区分表格中每行的多个几何图形。
答案 1 :(得分:1)
要从geojson创建postgis几何体类型,您可以使用函数ST_GeomFromGeoJSON
例如:
SELECT ST_GeomFromGeoJSON('{"type":"Point","coordinates":[-48.23456,20.12345]}');