Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/411.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Rails jBuilder从GeoJSON生成另一个json_Javascript_Ruby On Rails_Json_Geojson_Jbuilder - Fatal编程技术网

Javascript Rails jBuilder从GeoJSON生成另一个json

Javascript Rails jBuilder从GeoJSON生成另一个json,javascript,ruby-on-rails,json,geojson,jbuilder,Javascript,Ruby On Rails,Json,Geojson,Jbuilder,我试图获取一系列GeoJSON行字符串,并将它们放在地图上。我的jBuilder和Rails控制器组合没有生成用于放置在web地图上的格式正确的json。这是相关代码 overview\u data.json.builder json.type "FeatureCollection" json.features @segments do |street| if (street.extent_json) # only if item has a line drawn json.type

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

overview\u 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
概述\u controller.rb

class OverviewController < ApplicationController
  def index
  end
  def overview_data
    @segments = Street.all
  end
end
输出如
http://localhost:3000/overview/overview_data.json
。目前,大约有10个项目具有扩展名。以下是前几项:

{
  "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\”
和closing
}“
。否则我觉得没关系

在jBuilder中,我最初在json.geometry do循环中使用了
json.type“LineString”
,更糟糕的是添加了:
“geometry”:{“type”:“LineString”,“coordinates”:“{“type\”:“LineString\”,“coordinates\”

正如他所指出的,需要替换类似的行。正如他所指出的,我一定有一些格式错误的
json
输入。一旦清理完毕,一切都正常了

他还指出,“在jbuilder模板中,所有内容都必须使用纯Ruby,但当Rails再次尝试将json字符串转换为json时,您会传递一个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
            ]
          ]
        }
      }
    },
额外的
{

“坐标":
geometry

之后,问题在于
extent\u json
方法中,它以json字符串的形式返回一个对象。要解决此问题,请避免对\u json重复调用

来重申此问题:从数据库中获取一系列geoJSON,并使用jBuilder将满足特定条件的所有项编译为GeoJSON(用于Mapbox /小叶网络地图)。离线提供答案,但我想记录它,以确保我理解它,并帮助任何其他人有类似的问题。它有助于考虑JSON作为哈希和JBu建器正在制作另一个哈希。 输入有两个键:
类型
坐标

输出具有
类型
属性
几何图形
的键。
属性
值是一个键
标题
geometry
值是两个键
type
coordinates
。因此
overview\u 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\u json
以将字符串转换为散列对象。然后从散列中提取
坐标
,将其放入输出json。

不太清楚您的意思。我没有显式调用
到\u json
,所以它在哪里lled?我在我的
streets
数据库中看到了
extend_json
作为一个字段。感谢您的回复。
765:在“:”LineString“,”坐标“:[[-118.23927494951936,34.00801308572366],-118.23688905239013,34.00806596837443],-118.23280623377325,34.008055391846895],-118.230331024977,34.008076590061]]“
在进行建议的更改后生成。
street.extent_json
在PostGIS数据库中为“json”
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