Rails 来自 GeoJSON 的 jBuilder 制作另一个 json

Rails jBuilder from GeoJSON to make another json

我正在尝试获取一系列 GeoJSON 线串并将它们放在地图上。我的 jBuilder 和 Rails 控制器组合没有生成正确格式的 json 用于放置 web 地图。这是相关代码。

overview_data.json.builder

json.type "FeatureCollection"
json.features @segments do |street|
  if (street.extent_json) # only if item has a line drawn
    json.type "Feature"
    json.properties do
       json.title "Was #{street.prevName} before #{street.dateEarliest} and now is #{street.currentName} #{street.dateLatest})"
    end
    json.geometry do
       # json.type "LineString"
       json.coordinates street.extent_json
    end # json.geometry
  end  # if
end # json.features

overview_controller.rb

class OverviewController < ApplicationController
  def index
  end
  def overview_data
    @segments = Street.all
  end
end

street.extent_json 因为它出现在网络表单和数据库中(通过 pgAdmin 的 Postgres)

    {"type":"LineString",
    "coordinates":[[-118.25712423772116,34.01007010760971],
                  [-118.25649456380442,34.01016443793837],
                  [-118.25584971702219,34.01016443793837],
                  [-118.25427932544667,34.0102021700405],
                  [-118.25213995141625,34.010227324765935]]}

http://localhost:3000/overview/overview_data.json 中所示的输出。目前大约有十个项目有extent_json。以下是前几个:

{
  "type": "FeatureCollection",
  "features": [
    {
      "type": "Feature",
      "properties": {
        "title": "Was 10th St.  before 1903 and now is part of E. 9th Place (21). 1908)"
      },
      "geometry": {
        "coordinates": "{\"type\":\"LineString\",\"coordinates\":[[-118.24982816353442,34.035546195508864],[-118.25104052200915,34.03663976724366]]}"
      }
    },
    {
      "type": "Feature",
      "properties": {
        "title": "Was 37th St before 1903 and now is part of E. 40th Place 1928)"
      },
      "geometry": {
        "coordinates": "{\"type\":\"LineString\",\"coordinates\":[[-118.25712423772116,34.01007010760971],[-118.25649456380442,34.01016443793837],[-118.25584971702219,34.01016443793837],[-118.25427932544667,34.0102021700405],[-118.25213995141625,34.010227324765935]]}"
      }
    },
    {
      "type": "Feature",
      "properties": {
        "title": "Was Brook before 1903 and now is part of S. Toluca St. (26). and second block south gone 1908)"
      },
      "geometry": {
        "coordinates": "{\"type\":\"LineString\",\"coordinates\":[[-118.25862396508458,34.06087254304104],[-118.25933206826451,34.05994816216629]]}"
      }
    },
    {
      "type": "Feature",
      "properties": {
        "title": "Was Auto Pl before 1928 and now is Commonwealth Pl and a portion abandoned 1930)"
      },
      "geometry": {
        "coordinates": "{\"type\":\"LineString\",\"coordinates\":[[-118.28558737412096,34.07543021182353],[-118.28369373455645,34.07646106299854]]}"
      }
    },
    {
      "type": "Feature",
      "properties": {
        "title": "Was 3rd St before 1921 and now is Miramar St. One block abandoned )"
      },
      "geometry": {
        "coordinates": "{\"type\":\"LineString\",\"coordinates\":[[-118.26117539280003,34.05901974362122],[-118.2593849946753,34.05823410691563],[-118.25815599257271,34.05768101430694],[-118.25759459655055,34.05717191451128],[-118.25663111959356,34.05654339202722]]}"
      }
    },
    {
      "type": "Feature",
      "properties": {
        "title": "Was Gregory Way before  and now is Gregory Way 2017)"
      },
      "geometry": {
        "coordinates": "{\"type\":\"LineString\",\"coordinates\":[[-118.37295765057208,34.06188579510917],[-118.37272698059681,34.06172580874592],[-118.37264114990832,34.06161026285129],[-118.3725660480559,34.06146805230318],[-118.37253386154772,34.061414723286084],[-118.37249631062151,34.06118363049104]]}"
      }
    },

问题是添加的"{\"type\":\"LineString\",\"coordinates\"和关闭的}"。其他我觉得还行。

在 jBuilder 中,我最初在 json.geometry do 循环中有 json.type "LineString" 并且更糟糕的是添加:"geometry":{"type":"LineString","coordinates":"{\"type\":\"LineString\",\"coordinates\".

正如 Зелёный 所指出的 json.coordinates JSON.parse(street.extent_json) 需要替换类似的行。正如他还指出的那样,我一定有一些格式错误的 json 输入。一旦清理干净,一切正常。

他还指出"in jbuilder template all things must be in plain Ruby, but you pass a json string(which comes from database) as result Rails tried to convert json string to json again."

但是输出还是有错误,这里是第一项:

{
  "type": "FeatureCollection",
  "features": [
    {
      "type": "Feature",
      "properties": {
        "title": "Was 10th St.  before 1903 and now is part of E. 9th Place (21). 1908)"
      },
      "geometry": {
        "coordinates": {
          "type": "LineString",
          "coordinates": [
            [
              -118.24982816353442,
              34.035546195508864
            ],
            [
              -118.25104052200915,
              34.03663976724366
            ]
          ]
        }
      }
    },

geometry 之后额外的 { "coordinates":

问题出在extent_json方法中,它returns一个对象作为json字符串。要解决您的问题,避免重复调用 to_json

重述问题:从数据库中取出一系列 GeoJSON,然后使用 jBuilder 将所有满足特定条件的项目编译成 GeoJSON(用于 Mapbox/Leaflet 网络地图)。 https://whosebug.com/users/2057388/Зелёный 离线提供了答案,但我想记录它以确保我理解它并帮助其他遇到类似问题的人。它有助于将 json 视为散列并且 jbuilder 正在制作另一个散列。

输入有两个键:typecoordinates

输出有typepropertiesgeometry键。 properties 值是一个键 titlegeometry 值是两个键 typecoordinates。所以 overview_data.json.builder 变成:

extent = JSON.parse(street.extent_json) # the GeoJSON 
json.type "Feature"

json.properties do
   json.title h("Was #{street.prevName} before #{street.dateEarliest} and now is #{street.currentName} #{street.dateLatest}.")
end

json.geometry do
   json.type "LineString"
   json.coordinates extent["coordinates"]
end

布置后看起来很简单。其他关键点除外。一个是解析 extent_json 以将字符串转换为哈希对象。然后从该散列中提取 coordinates 以放入输出 json.