0

I have a geojson with multiple arrays, such as:

[[10.77932197941255,52.03584196145097],[10.77932197941255,52.03575906909681],[10.779636389761993,52.03575906909681],[10.779636389761993,52.03584196145097],[10.77932197941255,52.03584196145097]]

I need to write some code, which iterates each array and switches both elements in each sub-array (are these called like that?)

The result should look like this:

[[52.03584196145097,10.77932197941255],[52.03575906909681,10.77932197941255,],[52.03575906909681,10.779636389761993],[52.03584196145097,10.779636389761993],[52.03584196145097,10.77932197941255]]

1 Answer 1

1
var oldArray = [[10.77932197941255,52.03584196145097],
[10.77932197941255,52.03575906909681],
[10.779636389761993,52.03575906909681],
[10.779636389761993,52.03584196145097],
[10.77932197941255,52.03584196145097]];

var newArray = [];

for (var i = 0; i < oldArray.length; i++) {

  var oldCoordinate = oldArray[i];
  var newCoordinate = [oldCoordinate[1], oldCoordinate[0]];

  newArray.push(newCoordinate);

}

"newArray" now has all the switched coordinates. See the fiddle here for testing: jsfiddle.net/8rLhj0o3/

3
  • another question: lets say i have a multipolygon and I want to iterate through all polygons like in your answer. when i try this, I only get one huge array with connected geometries. how would i do loop this correctly?
    – Revo
    Jun 21, 2017 at 8:14
  • jsfiddle.net/dr0m62yn/1
    – Revo
    Jun 21, 2017 at 8:24
  • 1
    You need to run multiple for loops inside each other, depending on the data. See this fiddle with a working example: jsfiddle.net/xyg2Le47
    – Zeronull
    Jun 21, 2017 at 9:56

Not the answer you're looking for? Browse other questions tagged or ask your own question.