0

我有一个带有普通 json 格式地理数据的服务器,我需要将其更改为 geojson 格式,以便 Mapbox 可以读取它。你怎么做到这一点?

例如,如何转换:

[
  {
    "id": 0,
    "name": "Hotel",
    "icon": "Sleep",
    "address": "SampleStreet 34",
    "latitude": 12,
    "longitude": 55
  }
]

进入这个:

{
"type": "FeatureCollection",
  "features": [
    {
    "id": 0,
    "type": "Feature",
    "properties": {
      "placeID": 0,
      "name": "Hotel",
      "icon": "sleep",
      "addressFormatted": "SampleStreet 34"
    },
    "geometry": {
      "type": "Point",
      "coordinates": [
        12,
        55
      ]
    }
}
4

3 回答 3

1

如果您使用 ES6,那么这样的东西可能对您JSON.stringify有用,如果您可以直接使用生成的对象,则可能不需要。

const data = [
  {
    id: "0",
    name: "Hotel",
    icon: "Sleep",
    address: "SampleStreet 34",
    latitude: "12",
    longitude: "55"
  },
  {
    id: "1",
    name: "Landmark",
    icon: "Star",
    address: "SampleStreet 1234",
    latitude: "99",
    longitude: "100"
  }
];

const geojson = {
  type: "FeatureCollection",
  features: data.map(item => {
    return {
      id: item.id,
      type: "Feature",
      properties: {
        placeID: item.id,
        name: item.name,
        icon: item.icon,
        addressFormatted: item.address
      },
      geometry: {
        type: "Point",
        coordinates: [item.latitude, item.longitude]
      }
    };
  })
};

console.log(geojson);
console.log(JSON.stringify(geojson));
于 2019-04-12T20:50:33.450 回答
0

我使用 GeoJson ( https://www.npmjs.com/package/geojson ) 来解析我的数据,它看起来像以下效果很好:

import GeoJSON from 'geojson';
import jsonData from './jsonData.json';

const json = jsonData;

const data = GeoJSON.parse(json, {
  Point: ['latitude', 'longitude'],
  include: ['name', 'icon', 'addressFormatted']
});

export default data;

然而,我现在缺少的是我的 feature.id。有谁知道如何将其纳入?我不希望我的 id 在属性下。

于 2019-04-04T13:26:12.030 回答
0

有 @turf 库可以帮助解决这个问题,因此您可以执行以下操作:

import { lineString as makeLineString } from '@turf/helpers';

然后像这样使用它(注意经度在前)

var coords = [];
var dataarray = JSON.parse(thejson);
for (var i=0; i < dataarray.length; i++){
    obj = dataarray[i];
    coords.push([obj.long, obj.latitude]);    
}

let mapboxpoints = makeLineString(coords)

您应该在这里查看地图框示例:https ://github.com/nitaliano/react-native-mapbox-gl/tree/master/example/src

于 2019-04-03T14:33:33.187 回答