【问题标题】:Convert PostGIS polygon as GeoJSON in rails在 Rails 中将 PostGIS 多边形转换为 GeoJSON
【发布时间】:2017-01-10 01:06:05
【问题描述】:

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

这是 postgis 中的多边形示例

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

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

【问题讨论】:

  • PostGIS 提供了ST_AsGeoJSON 函数。
  • @DmitriGoldring 你能举个例子,这是如何在 Rails 中使用的吗?
  • 恐怕我不是 Rails 人,但这个 package 可能会让你入门。

标签: ruby-on-rails postgresql postgis geojson


【解决方案1】:

我最终只是用 .exterior_ring.points.map{|point|{lng:point.x, lat:point.y}} 创建了一些 JSON,并以与示例 https://developers.google.com/maps/documentation/javascript/examples/polyline-simple 中使用的 flightPlanCoordenants 相同的方式在谷歌地图上显示它

【讨论】:

    【解决方案2】:

    https://github.com/rgeo/rgeo-geojson 是用于此目的的特定宝石。

    您的代码应如下所示:

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

    【讨论】:

      猜你喜欢
      • 2014-04-29
      • 1970-01-01
      • 2017-01-12
      • 2013-10-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多