GeoJSON文件拆分

时间:2015-02-25 16:42:36

标签: javascript arrays json

我有以下代码:

function addData() {
    var arrayData = [];
    if (response != null) {
        for (i = 0; i < response.results.features.length; i++) {
            arrayData.push(response.results.features[i]);
        }
    }
    return arrayData;
}

var arrayDataOP = addData();

function splitLines(arrayDataOP) {
    var arrayPOINTjson = [];
    for (var i = 0; i < arrayDataOP.length; i++) {
        console.log(arrayData[i].geometry);
        if (arrayDataOP[i].geometry.type == "LineString") {
            for (key in arrayDataOP[i].geometry.coordinates.length) {
                arrayPOINTjson.push({
                    "type": "Feature",
                    "properties": {
                        "wheelchair": "yes",
                    },
                    "geometry": {
                        "type": "Point",
                        "coordinates": arrayData[i].geometry.coordinates[key]
                    }
                });
            }
        }
    }
    console.log(arrayPOINTjson);
    return arrayPOINTjson;
}
addData();
splitLines(arrayDataOP);

它会引发错误:

Uncaught TypeError: Cannot read property 'geometry' of undefined

我不知道为什么如果我console.log(arrayData[i].geometry);然后我看到了结果。

但那只是一个错误

我的观点是,我想将我的geoJSON文件中的LineString拆分为点,如下所示:

{
    "type":"Feature",
    "properties":{
        "wheelchair":"yes",
    },
    "geometry":{
        "type":"Point",
        "coordinates":arrayData[i].geometry.coordinates[key]
    }
}

我的geoSJON文件示例:

{
    "type": "FeatureCollection",
    "generator": "JOSM",
    "bbox": [
        23.4668,
        58.9198,
        23.6412,
        58.974
    ],
    "features": [
        {
            "type": "Feature",
            "properties": {
                "wheelchair": "yes",
                "smoothness": "intermediate",
                "highway": "footway"
            },
            "geometry": {
                "type": "LineString",
                "coordinates": [
                    [],
                    [
                        23.5410936,
                        58.9408252
                    ],
                    [
                        23.541092,
                        58.9408358
                    ],
                    [
                        23.5410706,
                        58.9410896
                    ],
                    [
                        23.5410448,
                        58.9412609
                    ],
                    [
                        23.541028,
                        58.9413309
                    ],
                    [
                        23.5409993,
                        58.9414512
                    ],
                    [
                        23.5408984,
                        58.9416477
                    ],
                    [
                        23.5408677,
                        58.9416962
                    ],
                    [
                        23.5407571,
                        58.9418706
                    ],
                    [
                        23.5405886,
                        58.9421204
                    ],
                    [
                        23.5405302,
                        58.9422071
                    ],
                    [
                        23.5403894,
                        58.9423888
                    ],
                    [
                        23.5401636,
                        58.9426413
                    ],
                    [
                        23.5400953,
                        58.9426593
                    ],
                    [
                        23.5399336,
                        58.9428447
                    ],
                    [
                        23.5399287,
                        58.9428504
                    ],
                    [
                        23.5399434,
                        58.9428895
                    ],
                    [
                        23.5394702,
                        58.9434341
                    ],
                    [
                        23.5394296,
                        58.943468
                    ],
                    [
                        23.5389324,
                        58.9439879
                    ],
                    [
                        23.5384256,
                        58.9445103
                    ],
                    [
                        23.5381597,
                        58.9447992
                    ],
                    [
                        23.5377425,
                        58.9452314
                    ],
                    [
                        23.5375449,
                        58.9454551
                    ]
                ]
            }
        },
        {
            "type": "Feature",
            "properties": {
                "wheelchair": "yes",
                "highway": "footway"
            },
            "geometry": {
                "type": "LineString",
                "coordinates": [
                    [
                        23.5408677,
                        58.9416962
                    ],
                    [
                        23.541045,
                        58.9417267
                    ],
                    [
                        23.5412157,
                        58.9417564
                    ]
                ]
            }
        }
    ]
}

但我希望我的代码最终输出是geoJSON对象的数组 所有LineString都会被拆分为point

0 个答案:

没有答案
相关问题