Home > OS >  How to convert string to a number in a two-dimensional array?
How to convert string to a number in a two-dimensional array?

Time:04-27

So i did write a simple function to deal with my string declared in variable poly, as you can see I used a split() method and now i want to convert each of those string values to numerical values:

function toArray(polygon) {
  final = polygon.replace('POLYGON ', '').replace('((', '').replace('))', '').split(',');

  const arrOfNum = [];

  final.forEach(str => {
    arrOfNum.push(Number(str));
  });
  return arrOfNum
}

poly = 'POLYGON ((21.0446582 52.2367037, 21.0544858 52.2264265, 21.0702358 52.2307111, 21.0755573 52.2333133, 21.0771022 52.2349428, 21.0759006 52.2375447, 21.0716091 52.2421962, 21.0532413 52.238412, 21.0472331 52.2371242, 21.0446582 52.2367037))';

console.log(toArray(poly))

I'm trying to convert strings to a numerical values but I'm getting this result:

    [NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN]

Later on i want to get to this exact point:

    [[20.7218472,52.2294069],
    [20.9436337,52.0756329],
    [21.0651699,52.2134223],
    [20.7767788,52.2537934],
    [20.7218472,52.2294069]]

The main goal of those operations is that i want to use this data to find out if a Point is within a Polygon. To do this I'm using this function:

function ray_casting(point, polygon){
    var n=polygon.length,
        is_in=false,
        x=point[0],
        y=point[1],
        x1,x2,y1,y2;

    for(var i=0; i < n-1;   i){
        x1=polygon[i][0];
        x2=polygon[i 1][0];
        y1=polygon[i][1];
        y2=polygon[i 1][1];

        if(y < y1 != y < y2 && x < (x2-x1) * (y-y1) / (y2-y1)   x1){
            is_in=!is_in;
        }
    }

    return is_in;
}

Thx everyone for the help!

CodePudding user response:

You missed some spaces and need to handle the pairs

Regex

function toArray(polygon) {
  const final = polygon.match(/(\d \.\d  \d \.\d )/g)
  return final.flatMap(str => ([str.split(" ").map(str =>  str)]));
}

poly = 'POLYGON ((21.0446582 52.2367037, 21.0544858 52.2264265, 21.0702358 52.2307111, 21.0755573 52.2333133, 21.0771022 52.2349428, 21.0759006 52.2375447, 21.0716091 52.2421962, 21.0532413 52.238412, 21.0472331 52.2371242, 21.0446582 52.2367037))';

console.log(toArray(poly))

Your version extended

function toArray(polygon) {
  const final = polygon
  .replace('POLYGON ((', '')
  .replace('))', '')
  .split(", ");

  return final.flatMap(str => ([str.split(" ").map(str =>  str)]));
}

poly = 'POLYGON ((21.0446582 52.2367037, 21.0544858 52.2264265, 21.0702358 52.2307111, 21.0755573 52.2333133, 21.0771022 52.2349428, 21.0759006 52.2375447, 21.0716091 52.2421962, 21.0532413 52.238412, 21.0472331 52.2371242, 21.0446582 52.2367037))';

console.log(toArray(poly))

CodePudding user response:

This solution is equivalent to one of @mplungjan's. Another approach would be to use new Function or just Function as in the second demo.

const poly = 'POLYGON ((21.0446582 52.2367037, 21.0544858 52.2264265, 21.0702358 52.2307111, 21.0755573 52.2333133, 21.0771022 52.2349428, 21.0759006 52.2375447, 21.0716091 52.2421962, 21.0532413 52.238412, 21.0472331 52.2371242, 21.0446582 52.2367037))';

const nums = poly.replace(/POLYGON \(\(|\)\)/g, '')
.split(/, /).map(num => num.split(/ /).map(n =>  n));

console.log( nums );

USING new Function()

const poly = 'POLYGON ((21.0446582 52.2367037, 21.0544858 52.2264265, 21.0702358 52.2307111, 21.0755573 52.2333133, 21.0771022 52.2349428, 21.0759006 52.2375447, 21.0716091 52.2421962, 21.0532413 52.238412, 21.0472331 52.2371242, 21.0446582 52.2367037))',

      jstr = poly
             .replace(/POLYGON \(\(/, '[[')
             .replace(/\)\)/, ']]')
             .replace(/, /g, '],[')
             .replace(/ /g, ','),
             
      output = new Function(`return ${jstr}`)();

console.log( output );

  • Related