2

I would like to pull the longitudes and latitudes from an ESRI geometry and concatenate them in to a long string (to be used in an API call).

I am struggling with how to accomplish this

The ESRI documentation for geometry (geometry specs) shows the structure of the object but my API call needs the latitude/longitudes in the following format:

long1,lat1,long2,lat2,long3,lat3 ...  long1, lat1

All I have to do is process the long/lats a little bit. Making a very simple example from the ESRI documentation

        MyTest = {
                    "rings": [
                        [
                            [-97.06138, 32.837],
                            [-97.06133, 32.836],
                            [-97.06124, 32.834],
                            [-97.06127, 32.832],
                            [-97.06138, 32.837]
                        ]
                    ],
                    "spatialReference": {
                        "wkid": 4326
                    }
                };

        alert(JSON.stringify(MyTest.rings[0]));

Will give me the rings (the Longitudes/Latitudes) (notice the first long/lat is repeated as the last long/lat)

I cannot seem to figure out how to strip off the [ and ] to create a string with just the longitudes and latitudes. For instance:

myTest2 = MyTest.rings[0];  // get the longitudes and latitudes
myTest3 = JSON.stringify(myTest2);
myTest4 = myTest3.replace("[","");

alert(JSON.stringify(myTest2));

alert(JSON.stringify(myTest4));

The replace will strip off one of the brackets but I cannot get it to do a global replace like this post stack javascript replace because my programming environment is all within ColdFusion and I need the quotes around the pattern.

Can someone point out my error please ? Thanks !

Community
  • 1
  • 1
user918967
  • 2,049
  • 4
  • 28
  • 43
  • just as a sidenote, valid polygons do need closed rings. That's why the last vertex is the same as the first one. – ffflabs Jun 16 '14 at 22:46

4 Answers4

2

You may try this (Example) using join():

MyTest = { "rings": [...] };
var str = MyTest.rings.join();

Result (in str) would be:

-97.06138,32.837,-97.06133,32.836,-97.06124,32.834,-97.06127,32.832,-97.06138,32.837

The Alpha
  • 143,660
  • 29
  • 287
  • 307
  • Because `rings` is a nested array, I don't think `join()` is being used. It seems that the separator parameter has no effect and the same result can be achieved by `toString()`. See [updated example](http://jsfiddle.net/8ZSv5/3/). – showdev Nov 09 '17 at 17:55
1

You have an object, use it - don't play with the stringified version.

var coords = MyTest.rings[0];
var list = [];

for ( var i = 0; i < coords.length; ++i )
  {
    list.push(coords[i][0]);
    list.push(coords[i][1]);
  }

var str = list.join(',');

// str is now "-97.06138,32.837,-97.06133...(etc.)"
Paul Roub
  • 36,322
  • 27
  • 84
  • 93
0

These are absolute basics, but you don't need to replace anything. [] is just an JavaScript array and not a string.

MyTest.rings.map(function (r) { return r.join(','); }).join(',');
// "-97.06138,32.837,-97.06133,32.836,-97.06124,32.834,-97.06127,32.832,-97.06138,32.837"

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/join

jgillich
  • 71,459
  • 6
  • 57
  • 85
0

DEMO: http://jsfiddle.net/gunderjt/B5wsK/2/

MyTest.rings[0].join()

and join with no parameters delineates with commas automatically

JTG
  • 8,587
  • 6
  • 31
  • 38