如何合并GeoJSON属性

时间:2019-07-01 10:19:57

标签: javascript json geojson

我已将JSON API中的属性提取到不同的数组中。现在,我只需要使用我需要的属性制作一个GeoJSON列表。我现在将它们单独设置。如何将它们合并到正确的GeoJSON列表中?

目前我的GeoJSON列表如下

GeoJSON format

{
"type": "FeatureCollection",
"features": [{
"type": "Feature",
"properties": {
  "station_name": East Coast
  "id": EC1,
  "device_id": EC1,
},
"geometry": {
  "type": "Point",
  "coordinates": [-73.9066122, 40.7453924]
}
  },
{
  "station_name": West Coast
  "id": WC1,
  "device_id": WC1,
},
"geometry": {
  "type": "Point",
  "coordinates": [-72.621322, 39.73424]
} },
{
"type": "Feature",
"properties": {
"timestamp": 1/1/19
"timestamp": 2/1/19
"timestamp": 3/1/19
},
{
"type": "Feature",
"properties": {
"value": 31
"value": 22
"value": 23
"value": 35
"value": 12
"value": 42
}

我想将它们合并为这种格式

{
"type": "FeatureCollection",
"features": [{
"type": "Feature",
"properties": {
"timestamp": 1/1/19
  "station_name": East Coast
  "id": EC1,
  "device_id": EC1,
"value": 31
},
"geometry": {
  "type": "Point",
  "coordinates": [-73.9066122, 40.7453924]
}
}, {
"type": "Feature",
"properties": {
"timestamp": 1/1/19
  "station_name": West Coast
  "id": EC1,
  "device_id": EC1,
"value": 22
},
"geometry": {
  "type": "Point",
  "coordinates": [-72.621322, 39.73424]
}
},
{{
"type": "Feature",
"properties": {
"timestamp": 2/1/19
  "station_name": East Coast
  "id": EC1,
  "device_id": EC1,
"value": 23
},
"geometry": {
  "type": "Point",
  "coordinates": [-73.9066122, 40.7453924]
}
}, {{
"type": "Feature",
"properties": {
"timestamp": 2/1/19
  "station_name": West Coast
  "id": EC1,
  "device_id": EC1,
"value": 35
},
"geometry": {
  "type": "Point",
  "coordinates": [-72.621322, 39.73424]
}
},{
"type": "Feature",
"properties": {
"timestamp": 3/1/19
  "station_name": East Coast
  "id": EC1,
  "device_id": EC1,
"value": 12
},
"geometry": {
  "type": "Point",
  "coordinates": [-73.9066122, 40.7453924]
}
}, {
  {
"type": "Feature",
"properties": {
"timestamp": 3/1/19
  "station_name": West Coast
  "id": EC1,
  "device_id": EC1,
"value": 42
},
"geometry": {
  "type": "Point",
  "coordinates": [-72.621322, 39.73424]
}
}

下面是我的代码。 3个for循环后面的原因是因为数组大小不同。

var apiGeo = {
    type: "FeatureCollection",
    features: []
};

for (var x in locname){
    apiGeo.features.push({
        "type":"Feature",
        "properties": {
            "station_name": locname[x],
            "id": id[x],
            "device_id": device_id[x],                
        },
        "geometry":{
            "type": "Point",
            "coordinates": [loclong[x], loclat[x]]
        }
    });
}

for (var y in timestamp){
    apiGeo.features.push({
         "type":"Feature",
        "properties":{
            "timestamp": timestamp[y]
        }
    });
}
for (var z in value){
    apiGeo.features.push({
         "type":"Feature",
        "properties":{
            "value": value[0],
            "items_id": value[1]
        }
    });
}

0 个答案:

没有答案