将JSON转换为与Mapbox Studio兼容的GeoJSON

时间:2018-02-17 03:23:26

标签: json dataset mapbox geojson mapbox-gl-js

我尝试使用SpaceX API中的this JSON,使用Mapbox-GL在Mapbox地图上显示所有SpaceX启动站点的位置。当我尝试将其加载到Mapbox Studio中的数据集时,我收到一条错误消息:输入失败。 "类型"第1行需要会员。

我认为这是由于JSON的结构方式,即它没有GeoJSON属性。

如何轻松调整此JSON并将其转换为适用于Mapbox的GeoJSON?

1 个答案:

答案 0 :(得分:1)

您提供的JSON文件不是有效的GeoJSON。您可以在此处详细了解格式规范:http://geojson.org/

您需要一个小脚本将SpaceX JSON文件转换为有效的GeoJSON。目前单个记录如下所示:

{
  "id": "ccafs_slc_40",
  "full_name": "Cape Canaveral Air Force Station Space Launch Complex 40",
  "status": "active",
  "location": {
    "name": "Cape Canaveral",
    "region": "Florida",
    "latitude": 28.5618571,
    "longitude": -80.577366
  },
  "vehicles_launched": [
    "Falcon 9"
  ],
  "details": "SpaceX primary Falcon 9 launch pad, where all east coast Falcon 9s launched prior to the AMOS-6 anomaly. Initially used to launch Titan rockets for Lockheed Martin. Back online since CRS-13 on 2017-12-15."
}

您可能需要的是Feature,其几何类型为Point,如下所示:

{
  "type": "Feature",
  "geometry": {
    "type": "Point",
    "coordinates": [-80.577366, 28.5618571]
  },
  "properties": {
    "id": "ccafs_slc_40",
    "full_name": "Cape Canaveral Air Force Station Space Launch Complex 40",
    "status": "active",
    "location": {
      "name": "Cape Canaveral",
      "region": "Florida"
    },
    "vehicles_launched": ["Falcon 9"],
    "details":
      "SpaceX primary Falcon 9 launch pad, where all east coast Falcon 9s launched prior to the AMOS-6 anomaly. Initially used to launch Titan rockets for Lockheed Martin. Back online since CRS-13 on 2017-12-15."
  }
}

转换原始数组的每条记录后,需要将它们包装在FeatureCollection中,以便mapbox-gl呈现它:

{
  "type": "FeatureCollection",
  "features": [
    //...
  ]
}