1

I need to create a string like this to make works the mapserver request:

filterobj = "POLYGON((507343.9 182730.8, 507560.2 182725.19999999998, 507568.60000000003 182541.1, 507307.5 182563.5, 507343.9 182730.8))";

Where the numbers are map coordinates x y of a polygon, the problem is with Javascript and OpenLayer what I have back is an array of numbers, How can I remove just the ODD commas (first, third, fifth...)?

At the moment I've created the string in this way:

filterobj = "POLYGON((" +  
Dsource.getFeatures()[0].getGeometry().getCoordinates() + " ))";

And the result is:

POLYGON((507343.9, 182730.8,507560.2, 182725.19999999998, 507568.60000000003, 182541.1, 507307.5, 182563.5,507343.9, 182730.8));

It's almost what I need but, I need to remove the ODD commas from the Dsource.getFeatures()[0].getGeometry().getCoordinates() array to make the request work, how can I do that?

5
  • What should be the final output? Commented Mar 8, 2018 at 9:38
  • the first string In the question: filterobj = "POLYGON((507343.9 182730.8, 507560.2 182725.19999999998, 507568.60000000003 182541.1, 507307.5 182563.5, 507343.9 182730.8))"; Commented Mar 8, 2018 at 9:39
  • So, "1,2,3,4,5,6" should become "1 2,3 4,5 6"? Have you tried anything so far? Commented Mar 8, 2018 at 9:41
  • yes correct, no I didn't try anything, I was thinking about the join method but how can i iterate through the array and join arr[0] arr[1], arr[2] arr[3], etc ? Commented Mar 8, 2018 at 9:43
  • What does .getCoordinates() return, for start? I.e. what is displayed if you write alert(JSON.stringify(x.getCoordinates()))? Commented Mar 8, 2018 at 10:11

5 Answers 5

2

The format that you need is WKT, and OpenLayers comes with a class that allows you to parse its geometries as WKT easily, as below:

var wktFormatter = new ol.format.WKT();
var formatted = wktFormatter.writeFeature(Dsource.getFeatures()[0]);
console.log(formatted); // POLYGON((1189894.0370893013 -2887048.988883849,3851097.783993299...
Sign up to request clarification or add additional context in comments.

1 Comment

thanks, I will save your answer for future usage. if you are expert with OpenLayers maybe you can help me with this question ;) stackoverflow.com/questions/49029299/…
0

Look at code snippet :

Help method : setCharAt , Take all commas , take all odds commas with i % 2 == 0

// I need to start from somewhere
function setCharAt(str,index,chr) {
    if(index > str.length-1) return str;
    return str.substr(0,index) + chr + str.substr(index+1);
}

var POLYGON = [507343.9, 182730.8,507560.2, 182725.19999999998, 507568.60000000003, 182541.1, 507307.5, 182563.5,507343.9, 182730.8];

var REZ = "";
REZ = POLYGON.toString();

var all_comma = [];

for(var i=0; i<REZ.length;i++) {
  if (REZ[i] === ",") all_comma.push(i);
}

for(var i=0; i<all_comma.length;i++) {
  if (i % 2 == 0 ) {
    REZ = setCharAt(REZ,all_comma[i],' ');
  }    
}

console.log(REZ);

// let return nee element intro POLYGON

// reset
POLYGON = REZ.split(',');

console.log(POLYGON);

3 Comments

If POLYGON is an array, then this is really a weird way to solve this problem.
Other answers are also correct and good but this is most simplest for understanding ! Ok @Groo i will fix that
POLYGON is array now !
0

What about this:

const str = Dsource.getFeatures()[0].getGeometry().getCoordinates()
// str = "1,2,3,4,5,6"
str.split(',').map((v, i) => {
  return (i % 2) ? v : v + ',' 
}).join(' ')
// "1, 2 3, 4 5, 6"

5 Comments

It's in the right direction, but has a syntax error, and it seems it would remove even commas instead.
it's true, if the solution can be ES5 it will be better, I have compatibility problems with older browsers
I'm still having str.split is not a function
Are you sure that Dsource.getFeatures()[0].getGeometry().getCoordinates() returns a string? If not you can remove the split function
0

There are a couple of ways to go, both involve getting rid of whitespace first. The first matches coordinate pairs, removes the comma, then pastes them back together.

The second splits into individual numbers, then formats them with reduce. Both should be ECMA-262 ed5 (2011) compatible but I don't have an old enough browser to test them.

var s = '507343.9, 182730.8,507560.2, 182725.19999999998, 507568.60000000003, 182541.1, 507307.5, 182563.5,507343.9, 182730.8';
var re = /\d+\.?\d*,\d+\.?\d*/g;
  
// Solution 1
var x = s.replace(/\s/g,'').match(re).map(function(x){return x.replace(',',' ')}).join();
console.log(x);

// Solution 2
var t = s.replace(/\s/g,'').split(',').reduce(function (acc, v, i) {
  i%2? (acc[acc.length - 1] += ' ' + v) : acc.push(v);
  return acc;
}, []).join(',');

console.log(t);

Comments

0

One approach would be using Array.reduce():

var input = '1.0, 2.0, 3.0, 4.0, 5.0, 6.0';

var output = input
  .split(',')
  .reduce((arr, num, idx) => { 
     arr.push(idx % 2 ? arr.pop() + ' ' + num : num);
     return arr;
   }, [])
  .join(',');

// output = '1.0 2.0, 3.0 4.0, 5.0 6.0'

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.