0

After doing the funtion: cirkelFeatures[1].getCoordinates().toString() I'am receiving the coordinates of the polygon (they are in epsg 31370)

"140015.12392378994,147993.86462185346,146949.24235978903,143983.79136079177,146934.0790524634,135989.98578868713,139984.87561409094,132006.24716971722,133050.7963502835,136016.25281185843,133065.88134080853,144010.058368247,140015.12392378994,147993.86462185346"

How is it possible to push this in a array? I'am thinking it would be something with .split(',') but it should split after each SECOND commma...

3 Answers 3

1

This should work.

var yourString = '0.0,0.0,0.0,0.0,0.0,0.0';
var string = yourString.split(',');

// Create array of float for each pair of coordinate
var a = string.length; 
for (i = 0; i < a; i++) {
    string[i] = parseFloat(string[i]);
}

// Initialize an array to store the new values
var b = string.length / 2;
var array = [];
for (i = 0; i < b; i++) {
        array[i] = [0, 0];
}

// Create an array of array of coordinates
var k = 0;
for (i = 0; i < b; i++) {
    for (j = 0; j < 2; j++) {
        array[i][j] = string[k];
        k++;
    }
}

console.log(array);

Or in ES6:

const chunk = (str) => {
    return  str.split(',')
        .map(parseFloat)
        .reduce((acc, val, i, arr) =>
            (i % 2) ? acc
            : [...acc, arr.slice(i, i + 2)]
        , []) 
}
const arrayOfArrays = chunk(yourString)
1

You could use the zip function in Python like this (not sure if this helps though since it is Python):

coordString = "140015.12392378994,147993.86462185346,146949.24235978903,143983.79136079177,146934.0790524634,135989.98578868713,139984.87561409094,132006.24716971722,133050.7963502835,136016.25281185843,133065.88134080853,144010.058368247,140015.12392378994,147993.86462185346"

coordList = coordString.split(',')

coords = zip(coordList,coordList[1:])[::2]

How about this function in Javascript

function coordArray(coordString) {
  var coords = coordString.split(",")
  var temp = coords.slice();
  var arr = [];

  while (temp.length) {
    arr.push(temp.splice(0,2));
  }

  return arr;
}
1
  • I prefer Javascript if it's possible..
    – niels
    Commented May 18, 2016 at 13:34
0

You can use the function string.split(",") in javascript it returns an array of in which you can find your coordinates as @Peter mentioned, you can also get your coordinates with

cirkelFeatures[1].getGeometry().getFirstCoordinates();

in OL 3

1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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