Reputation: 63
My geoJSON file: coordinates key contains normal array with coordinates, but then contains an array filled with mini-arrays of more coordinates. I cannot loop through the file to extract the center of the route in order to add my google feature layer to my google api call.
When I try to break up apart the mini arrays and add it to the overall total array, the route becomes visible, but is not correct. The route goes over the water and through buildings, it does not know the sequence. Any help would be great. The bold breaks the pattern of the whole feature.
ex: {"type":"Feature","properties":{"OBJECTID":3,"LENGTH":3919.4410000000003,"NAME":"Anacostia Riverwalk-SW","STATUS":"Open","MAINTENANC":"DDOT","Shape_Length":1194.6479669066844,"MILES":0.7423183712121213},"geometry":{"type":"MultiLineString","coordinates":[[[-77.02215895389972,38.87673054256091],[-77.02186869854079,38.87653957420972],[-77.02150193815677,38.87588299990856],[-77.0208280524279,38.874621556490816],[-77.02075697415529,38.87448850380858],[-77.02062323938105,38.874211704408985],[-77.02008845385471,38.87338800912766],[-77.01937789804083,38.87202111748801],[-77.01768265826789,38.87204526498413]],
[[-77.02594024042205,38.87974434948391],[-77.02509229875858,38.87909983220277],[-77.0237707432032,38.8780375415248],[-77.02215132050688,38.87674248295079]] ]}},
Upvotes: 1
Views: 59
Reputation: 2312
What you call mini-arrays is just the specification of geojson MultiLineStrings. http://geojson.org/geojson-spec.html#multilinestring
For type "MultiLineString", the "coordinates" member is an array of LineString coordinate arrays.
So each array is describing a part of a line and together they form the resulting route. You can iterate over them by using 2 for loops (its an array of arrays)
var geojson = {
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"properties": {},
"geometry": {
"type": "MultiLineString","coordinates":[
[[-77.02215895389972,38.87673054256091],[-77.02186869854079,38.87653957420972],[-77.02150193815677,38.87588299990856],[-77.0208280524279,38.874621556490816],[-77.02075697415529,38.87448850380858],[-77.02062323938105,38.874211704408985],[-77.02008845385471,38.87338800912766],[-77.01937789804083,38.87202111748801],[-77.01768265826789,38.87204526498413]],
[[-77.02594024042205,38.87974434948391],[-77.02509229875858,38.87909983220277],[-77.0237707432032,38.8780375415248],[-77.02215132050688,38.87674248295079]]
]
}
}
]
}
let coords = geojson.features[0].geometry.coordinates;
for (let i = 0; i < coords.length; i += 1) {
for (let j = 0; j < coords[i].length; j += 1) {
console.log(coords[i][j]);
}
}
Upvotes: 0