[英]Get exterior ring of geometries, and put it in geojson object - Postgis - SQL
我一直在寻找SQL查询的几天,这将允许我创建一个GeoJSON来给我数据库中多边形的轮廓。
第一个查询工作正常,它只允许在一个单元格中获得1个具有所有多面体的Geojson对象。
SELECT json_build_object(
'type', 'FeatureCollection',
'features', json_agg(
json_build_object(
'type', 'Feature',
'geometry', ST_AsGeoJSON(
the_geom
)::json,
'properties', json_build_object(
'name', name
)
)
)
)
FROM layer
现在,我试图获取相同的GeoJson对象,但使用Linestring而不是multipolygons。 看来函数ST_ExteriorRing()与函数json_agg()不兼容? 我不知道我必须进行哪些更改才能使其起作用...这是我尝试的最后一个查询,该查询不起作用:
SELECT json_build_object(
'type', 'FeatureCollection',
'features', json_agg(
json_build_object(
'type', 'Feature',
'geometry', ST_AsGeoJSON(
ST_ExteriorRing( -- get the exterior lines of multipolygons
ST_GeometryN(
the_geom,
generate_series(
1,
ST_NumGeometries(the_geom)
)
)
)
)::json,
'properties', json_build_object(
'name', name
)
)
)
)
FROM layer
如果我抑制json_agg()函数,它可以正常工作,但是在这种情况下,我在输出中没有得到一个单元格。 如果我抑制ExteriorRing()函数,它也可以正常工作,但是我有MultiPolygons而不是LineString,就像我想要的...
任何想法如何解决该问题?
几个月后我找到了,所以答案是:
SELECT json_build_object(
'type', 'FeatureCollection',
'features', json_agg(feature)
) as geojson
FROM (
SELECT json_build_object(
'type', 'Feature',
'geometry', ST_AsGeoJSON(
ST_ExteriorRing(
ST_GeometryN(
ST_Union(the_geom),1
)
)
)::json
) as feature
FROM layer
) t
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.