繁体   English   中英

Postgis查询ST_Intersects与GeoJSON(jsonb)

[英]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交付为jsonbfunction st_intersects(jsonb, geometry) does not exist 我尝试转换为text ,然后错误是function st_intersects(text, geometry) is not unique 我如何处理jsonb结果作为Postgis函数的输入?

首先,函数jsonb_array_elements()返回一个集合,因此您应该将它作为FROM子句中表函数 其次,您应该使用->>运算符获取jsonb字段的文本表示,然后将其转换为随后在PostGIS函数中使用的geometry

要检索非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以便可以区分表中每行的多个几何。

要从geojson创建postgis几何类型,可以使用函数ST_GeomFromGeoJSON

例如:

SELECT ST_GeomFromGeoJSON('{"type":"Point","coordinates":[-48.23456,20.12345]}');

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM