转换PostGis Table(PostgreSQL)到Geojson

问题描述 投票:0回答:2

I访问了中显示的问题是如何从PostgreSql转换为Geojson格式的? this PostGIS SQL将整个桌子转换为Geojson结果:

SELECT row_to_json(fc) AS geojson FROM (SELECT 'FeatureCollection' As type, array_to_json(array_agg(f)) As features FROM (SELECT 'Feature' As type, ST_AsGeoJSON((lg.geometry),15,0)::json As geometry, row_to_json((id, name)) As properties FROM imposm3_restaurants As lg) As f ) As fc;

我已经在那里发现,在结果中,我们没有获得字段的名称。 我期望输出为 “ properties”:{“ id”:6323,“名称”:“餐厅ninaia”

但实际输出是
 “属性”:{“ f1”:6323,“ f2”:“餐厅ninaia”

我阅读了row_to_json指令的规范,所以我决定更改最后一个row_to_json指令

SELECT row_to_json(fc) AS geojson FROM (SELECT 'FeatureCollection' As type, array_to_json(array_agg(f)) As features FROM (SELECT 'Feature' As type, ST_AsGeoJSON((lg.geometry),15,0)::json As geometry, row_to_json((lg)) As properties FROM imposm3_restaurants As lg) As f ) As fc;

但是现在,Geojson也将几何场作为属性进行了检索。 我的意思是,在结果中,我可以看到以geojson格式形成的几何形状,并再次以gis后格式形成(不需要第二个几何形状,我可以浪费它),因此,如果第一个结果是1200kb,则第二个几何形状,第二个几何形状大约为2300kb。

我该怎么办?任何替代方案

row_to_json((id, name)) As properties
or

row_to_json((lg)) As properties

我也尝试了

row_to_json(('id',lg.id ,'masa',lg.masa ,'parcela',lg.parcela)) As properties
和其他任何人,但没有结果(只有SQL错误)

谢谢你
	

您需要做什么,首先选择列,然后选择row_to_json此选择。 有了您的价值观,这将提供以下示例:

SELECT
    row_to_json(fc)
FROM (
    SELECT
        'FeatureCollection' AS type
        , array_to_json(array_agg(f)) AS features
    FROM (
        SELECT
            'feature' AS type
            , ST_AsGeoJSON(geom)::json as geometry
            , (
                SELECT
                    row_to_json(t)
                FROM (
                    SELECT
                        id
                        , name
                    ) AS t
                ) AS properties
        FROM imposm3_restaurants
    ) AS f
) AS fc

对于路人,您不需要子征服,而JSONB的性能稍好一些。话虽如此,这在将11K记录倾倒在宽阔的桌子上时剃光了约150毫秒。为了使这个规模肯定需要

LIMIT
postgresql postgis geojson
2个回答
10
投票
OFFSET

或扔在

WHERE
中,以滤除不需要的东西

0
投票

您可以在控制台中使用OGR2OGR执行此代码
cd C:\\OSGeo4W64\\bin && ogr2ogr -f "GeoJSON" C:\Users\Documents\nameFile.json "PG:dbname=nameBD schemas=NameSchema host='localhost' port='5432' user='postgres' password='**'" -sql "select * from public.tableName"
	

最新问题
© www.soinside.com 2019 - 2025. All rights reserved.