在 rails 中将 PostGIS 多边形转换为 GeoJSON

Convert PostGIS polygon as GeoJSON in rails

我使用 postgis 在 postgres 中保存了一些多边形,现在我需要一些方法将这些数据转换为 GeoJSON 文件,以便我可以在 google 地图上显示它。

这是 postgis 中多边形的示例

"POLYGON ((-40 145, -40 145, -40 145, -40 145, -40 145, -40 145))"

如何将其转换为 GeoJSON 中的等效多边形?

我最终只是用 .exterior_ring.points.map{|point|{lng:point.x, lat:point.y}} 创建了一些 JSON 并将其显示在 google 地图上,就像在示例 https://developers.google.com/maps/documentation/javascript/examples/polyline-simple[= 中使用 flightPlanCoordenants 一样12=]

https://github.com/rgeo/rgeo-geojson 是特定的 gem 目的。

您的代码应如下所示:

RGeo::GeoJSON.encode(<your_spatial_variable>, json_parser: :json)