Getting Polygon points as Lat and Lon

1229
5
Jump to solution
06-02-2022 03:54 AM
Aeseir
by
Occasional Contributor

I am trying to get polygon points as Lat and Long coordinates, but cannot find doco that explains how.

 

this is how far i got to:

var polygonGeo = graphicGeometry as Polygon;
            console.log(polygonGeo.rings.toString());


            for (var i = 0; i < polygonGeo.rings.length; i++) {
              for (var p = 0; p < polygonGeo.rings[i].length; p++) {
                var LatLon = String(polygonGeo.rings[i][p]);
                console.log(LatLon);
              }
            }

 

All this does it outputs it as:

16816274.629485022,-4041754.114785292,16822695.33986097,-4041754.114785292,16822695.33986097,-4045050.461630086,16816274.629485022,-4045050.461630086

 

How can i get to convert it to Lat and Long?

0 Kudos
1 Solution

Accepted Solutions
SiyabongaKubeka
Occasional Contributor

Something like this

Also do not forget to add WebmercatorUtils on your require statements and function statements

 

for (var i = 0; i < gra.geometry.rings.length; i++) {
                  for (var p = 0; p < gra.geometry.rings[i].length; p++) {
                    var pnt = gra.geometry.getPoint(i, p);
                    crmLatitude = String(pnt.latitude);
                    crmLongitude = String(pnt.longitude);
                    var LatLon = String(pnt);
                    console.log(LatLon);
                    console.log(crmLatitude);
                    console.log(crmLongitude);
                    latlon = LatLon;
                  }

 

View solution in original post

5 Replies
SiyabongaKubeka
Occasional Contributor

Hi @Aeseir , remember, you are drawing a polygon  and a polygon has multiple vertices, to this output is a longitude and latitude of each vertex (corner). You can try using  WebmercatorUtils to convert the geometry to Geographic .

0 Kudos
Aeseir
by
Occasional Contributor

What would this look like for this example then?

0 Kudos
SiyabongaKubeka
Occasional Contributor

Something like this

Also do not forget to add WebmercatorUtils on your require statements and function statements

 

for (var i = 0; i < gra.geometry.rings.length; i++) {
                  for (var p = 0; p < gra.geometry.rings[i].length; p++) {
                    var pnt = gra.geometry.getPoint(i, p);
                    crmLatitude = String(pnt.latitude);
                    crmLongitude = String(pnt.longitude);
                    var LatLon = String(pnt);
                    console.log(LatLon);
                    console.log(crmLatitude);
                    console.log(crmLongitude);
                    latlon = LatLon;
                  }

 

Aeseir
by
Occasional Contributor

Interesting approach, any advice on how to reverse the process?

 

I just discovered webMercatorUtils.xyToLngLat as well

ReneRubalcava
Frequent Contributor

I have a utility that might help too if you don't want to manually pull vertices out, will convert polygons to points you can pull lat/lon off too. https://github.com/odoe/explode