简体   繁体   English

获取几何的外环,并将其放入geojson对象-Postgis-SQL

[英]Get exterior ring of geometries, and put it in geojson object - Postgis - SQL

I have been searching for a few days for a SQL query that would allow me to create a GeoJSON that would give me the outline of polygons in my database. 我一直在寻找SQL查询的几天,这将允许我创建一个GeoJSON来给我数据库中多边形的轮廓。

That first query is working correctly, it permits to get 1 Geojson Object with all the multipolygons in only one cell. 第一个查询工作正常,它只允许在一个单元格中获得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

Now i'm trying to get the same GeoJson object but with Linestring instead of multipolygons. 现在,我试图获取相同的GeoJson对象,但使用Linestring而不是multipolygons。 It seems that the function ST_ExteriorRing() doesn't work with the function json_agg() ? 看来函数ST_ExteriorRing()与函数json_agg()不兼容? I don't know what I have to change to make it works ... Here is the last query i tried, which doesn't work : 我不知道我必须进行哪些更改才能使其起作用...这是我尝试的最后一个查询,该查询不起作用:

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

If i suppress the json_agg() function it works fine, but it this case i don't get a single cell in output. 如果我抑制json_agg()函数,它可以正常工作,但是在这种情况下,我在输出中没有得到一个单元格。 If i supress the ExteriorRing() function it works also fine, but I have MultiPolygons and not LineString like i would like ... 如果我抑制ExteriorRing()函数,它也可以正常工作,但是我有MultiPolygons而不是LineString,就像我想要的...

Any ideas how to solve that problem ? 任何想法如何解决该问题?

I found it months later, so here is the answer : 几个月后我找到了,所以答案是:

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.

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