当前位置: 首页 > 面试题库 >

如何从PostgreSQL转换为GeoJSON格式?

宗政松
2023-03-14
问题内容

我有一个名为“
imposm3_restaurant”的简单表,其列为[id,name,geometry],我想将这些数据转换为geoJSON,我正在使用此功能

CREATE VIEW imposm3_restaurants_geojson AS 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;

结果是这样的:

{"type":"FeatureCollection","features":[{"type":"Feature","geometry":{"type":"LineString","coordinates":[[2615020.47191046,5899232.25158985],[2615034.50527113,5899231.67978097],[2615033.86145338,5899215.4513157],[2615032.35921198,5899215.51938806],[2615031.96732292,5899205.64890158],[2615034.97180572,5899205.51275702],[2615034.36531075,5899190.07397728],[2615018.19522163,5899190.71385561],[2615018.77372453,5899205.40384137],[2615020.47191046,5899205.32215463],[2615020.91045298,5899216.48601561],[2615019.83742341,5899216.52685903],[2615020.47191046,5899232.25158985]]},"properties":{"f1":2719,"f2":"Atelierul de Pizza"}},{"type":"Feature","geometry":{"type":"Polygon","coordinates":[[[2615018.19522163,5899190.71385561],[2615018.77372453,5899205.40384137],[2615020.47191046,5899205.32215463],[2615020.91045298,5899216.48601561],[2615019.83742341,5899216.52685903],[2615020.47191046,5899232.25158985],[2615034.50527113,5899231.67978097],[2615033.86145338,5899215.4513157],[2615032.35921198,5899215.51938806],[2615031.96732292,5899205.64890158],[2615034.97180572,5899205.51275702],[2615034.36531075,5899190.07397728],[2615018.19522163,5899190.71385561]]]},"properties":{"f1":2720,"f2":"Atelierul de Pizza"}},{"type":"Feature","geometry":{"type":"LineString","coordinates":[[2624875.82864931,5903443.39761349],[2624897.49451598,5903452.78251964],[2624901.44139867,5903443.67003443],[2624879.78486269,5903434.29875908],[2624875.82864931,5903443.39761349]]},"properties":{"f1":2986,"f2":"Pizza Acrobatica"}},{"type":"Feature","geometry":{"type":"Polygon","coordinates":[[[2624875.82864931,5903443.39761349],[2624897.49451598,5903452.78251964],[2624901.44139867,5903443.67003443],[2624879.78486269,5903434.29875908],[2624875.82864931,5903443.39761349]]]},"properties":{"f1":2988,"f2":"Pizza Acrobatica"}},{"type":"Feature","geometry":{"type":"LineString","coordinates":[[2622460.22447654,5904586.41424973],[2622479.10046632,5904587.95362911],[2622480.25747212,5904573.81314552],[2622461.39081303,5904572.26014582],[2622460.22447654,5904586.41424973]]},"properties":{"f1":3248,"f2":"Casa Vikingilor"}},{"type":"Feature","geometry":{"type":"Polygon","coordinates":[[[2622460.22447654,5904586.41424973],[2622479.10046632,5904587.95362911],[2622480.25747212,5904573.81314552],[2622461.39081303,5904572.26014582],[2622460.22447654,5904586.41424973]]]},"properties":{"f1":3249,"f2":"Casa Vikingilor"}},{"type":"Feature","geometry":{"type":"LineString","coordinates":[[2625201.09657005,5897608.45120294],[2625224.46062264,5897614.30435379],[2625241.33051365,5897576.653689],[2625213.43174478,5897570.82778714],[2625201.09657005,5897608.45120294]]},"properties":{"f1":6152,"f2":"Silva"}},{"type":"Feature","geometry":{"type":"Polygon","coordinates":[[[2625201.09657005,5897608.45120294],[2625224.46062264,5897614.30435379],[2625241.33051365,5897576.653689],[2625213.43174478,5897570.82778714],[2625201.09657005,5897608.45120294]]]},"properties":{"f1":6153,"f2":"Silva"}},{"type":"Feature","geometry":{"type":"LineString","coordinates":[[2622825.25980629,5904372.27967993],[2622826.15555271,5904353.45341631],[2622834.51585268,5904353.1673446],[2622854.22227404,5904346.00193242],[2622860.03529512,5904362.26715407],[2622856.61093118,5904374.66361634],[2622825.25980629,5904372.27967993]]},"properties":{"f1":6322,"f2":"Restaurant Sinaia"}},{"type":"Feature","geometry":{"type":"Polygon","coordinates":[[[2622825.25980629,5904372.27967993],[2622856.61093118,5904374.66361634],[2622860.03529512,5904362.26715407],[2622854.22227404,5904346.00193242],[2622834.51585268,5904353.1673446],[2622826.15555271,5904353.45341631],[2622825.25980629,5904372.27967993]]]},"properties":{"f1":6323,"f2":"Restaurant Sinaia"}}]}

它没有当前的几何图形,您知道功能上有什么问题吗:我使用的是Postgres 9.3和PostGIS 2.2


问题答案:

您的输出是有效的geojson文件,但是使用投影EPSG:3857投影了几何。

您可以在大多数gis桌面应用程序(例如Quantum Gis)中加载数据而不会出现问题。

geojson.io可能仅支持长/纬坐标EPSG:4326,还尝试使用函数St_Transform将几何重新投影到长/纬坐标

更改此行:

ST_AsGeoJSON((lg.geometry),15,0)::json As geometry,

在此:

ST_AsGeoJSON(ST_Transform(lg.geometry, 4326),15,0)::json As geometry,


 类似资料:
  • 我正在尝试使用python和python geojson创建一个features数组。我附加了一些功能,例如带有工作坐标的多边形。但是,当我转储时,geoJson文件中没有缩进。所有数据都在一行上,mapbox不接受数据。 F 这就是输出的样子。它应该缩进,而不是像那样聚集。 {“类型”:“特征集合”,“特征”:[{“类型”:“多边形”,“坐标”:[[[2.38,57.322],[23.194,-

  • 问题内容: 我有一些类型为int的列,但值是空的。因此,当我插入数据库时​​,我想将empty转换为null。我使用代码: 但显示错误: 为什么? 问题答案: 有NULLIF()功能: 如果var保持$ 2中的值,则NULL取而代之。 在这个例子中我替换空字符串:’‘有NULL。 有该类型的整数无空字符串。只是不可能。由于NULLIF()无法切换数据类型,因此必须清理PHP中的输入。 如果您没有定

  • 问题内容: 在遵循众多D3示例时,数据通常以flare.json中给出的格式进行格式化: 我有一个邻接列表,如下所示: 我想将其转换为以上格式。目前,我正在服务器端执行此操作,但是有没有办法使用d3的功能来实现此目的?我在这里找到了一个,但是这种方法似乎需要修改d3核心库,由于可维护性,我不赞成这样做。有什么建议? 问题答案: 有没有规定的格式,通常可以通过各种访问函数重新定义你的数据(如hier

  • 我正在尝试将列unixtime数据转换为'YYYY-MM-DD'格式,但没有得到它。“我的列”的数据类型为数字。 我尝试了以下查询,但出现错误: 查询执行失败 原因:SQL错误[42883]:错误:函数to_timestamp(数字,未知)不存在提示:没有函数匹配给定的名称和参数类型。可能需要添加显式类型转换。职位: 8 需要以下格式的结果:

  • 在下面的示例中,我们正在尝试将“日期”从一种格式转换为其他格式,例如要转换为UTC格式,例如2021-07-26T18:37:15.490Z 有人能帮忙吗。 输入XML: 现有输出: XSLT代码: 参考URL:https://xsltfiddle.liberty-development.net/pNEj9dB/1

  • 问题内容: 即 此代码 给我 : 2012-16-02 05:16:17 什么时候 返回: 2012-01-02 05:16:17.0 问题答案: 您应该改用日历: