How can I get x,y in dec degrees from a poly feat serv using arcade?

11792
35
Jump to solution
12-18-2017 03:37 PM
GusMonteverde1
New Contributor II

Greetings Dev Community-

I need help with an arcade expression. I am trying to generate x,y pairs from a tax lot polygon feature service. In my "custom" field I have used the following expression:

var pnt= Centroid(Geometry($feature));
return Text(pnt);

RESULT...

{"x":-13649119.198784793,"y":5696311.278630899,"spatialReference":{"latestWkid":3857,"wkid":102100}}

but, I need the x and y to be returned in dec degrees, and my results appear to be on UTM northing/easting

I also tried to call a member method X and Y but I am getting the following error...

 

Thanks!

35 Replies
XanderBakker
Esri Esteemed Contributor

You didn't do anything wrong. I guess something changed in Arcade with the way you can access the X and Y properties of the point. It is necessary to use pnt_centr.x, pnt_centr.y instead of pnt_centr[0], pnt_centr[1] (see line 20):

function MetersToLatLon(mx, my) {
    // Converts XY point from Spherical Mercator EPSG:900913 to lat/lon in WGS84 Datum
     // Fuente: http://www.maptiler.org/google-maps-coordinates-tile-bounds-projection/
    var originShift = 2.0 * PI * 6378137.0 / 2.0;

    var lon = (mx / originShift) * 180.0;
    var lat = (my / originShift) * 180.0;

    lat = 180.0 / PI * (2.0 * Atan( Exp( lat * PI / 180.0)) - PI / 2.0);
    return [lat, lon];
}


var poly = Geometry($feature);

var result = "";
if (!IsEmpty(poly)) {
    var pnt_centr = Centroid(poly);

    var latlon = MetersToLatLon(pnt_centr.x, pnt_centr.y);

    result = "(" + Round(latlon[0], 6) + ", " + Round(latlon[1], 6) + ")";
} else {
    result = "";
}

return result;

Result:

by Anonymous User
Not applicable

Many many thanks to you Xander Bakker‌ - you are generous with your time, and saved me a lot of it! I've been trying to find more information about the Arcade language to dig into the issue, and could only find the documentation at ArcGIS Arcade | ArcGIS for Developers - which is relatively simple and didn't get into that detail.
Can I ask how you found out what the issue was?

XanderBakker
Esri Esteemed Contributor

You 're welcome and I'm glad that it is working now. 

The site you are referencing is the source documentation for Arcade. There are however a number of blog posts and threads that have relevant information and explain in more detail how to accomplish something. You could have a log here:

https://community.esri.com/content?filterID=all~objecttype~objecttype%5Bblogpost%5D&query=arcade 

Especially the blog by Kelly Gerrow‌: https://community.esri.com/community/gis/web-gis/arcgisonline/blog/2017/07/18/conditional-field-disp... is a good read. 

I have published a number of documents related to Arcade (but most are in Spanish, although you might find it helpful):

/people/xander_bakker/content?query=arcade 

To find the issue the error message "cannot calling a member property" was an important clue. Since no line number is returned I have to include some Console statements like:

Console("pnt_centr:" + pnt_centr);

... after line 19 to see what the value of the variable is and if it is valid. 

Once I knew the line where the error was occurring, I tried reading the X and Y properties differently. The old way used to work, but it seems that something has changed along the line (perhaps at the AGOL update last month, although Arcade remained at version 1.3.0 December 2017).

by Anonymous User
Not applicable

Thanks again, Xander Bakker‌ - good tips there! Thanks again for your time 🙂

XanderBakker
Esri Esteemed Contributor

You're welcome, my pleasure!

BrianCulpepper
New Contributor III

Thanks again for your time, effort and sharing your knowledge with the community!

This worked for me but I wondered if Arcade now performs coordinate conversions?  I've not found much information,yet, but I'm glad I stumbled upon your example!

thanks Xander!

0 Kudos
XanderBakker
Esri Esteemed Contributor

Not yet, but hopefully this will be introduced in a future version.

rymaaneliunas
New Contributor II

Hi Xander,

This is exactly what I need for conversion, thank you very much.  I was hoping there is a way where I can get the x and y coordinates of the centroid as separate expressions. I would like a latitude expression and and longitude expression in decimal degrees. 

I invoke Survey123 in my pop-ups and use them in my url parameters and using a combination of lat/lon doesn't work in this case. You help is much appreciated.

0 Kudos
XanderBakker
Esri Esteemed Contributor

Hi ryma aneliunas ,

You can create separate expressions for the latitude and longitude values:

Latitude:

function MetersToLat(my) {
    // Converts XY point from Spherical Mercator EPSG:900913 to lat/lon in WGS84 Datum
    // Source: http://www.maptiler.org/google-maps-coordinates-tile-bounds-projection/
    var originShift = 2.0 * PI * 6378137.0 / 2.0;
    var lat = (my / originShift) * 180.0;
    lat = 180.0 / PI * (2.0 * Atan( Exp( lat * PI / 180.0)) - PI / 2.0);
    return lat;
}

var poly = Geometry($feature);
if (!IsEmpty(poly)) {
    var pnt_centr = Centroid(poly);
    return MetersToLat(pnt_centr.y);
} else {
    return Null;
}

Longitude:

function MetersToLon(mx) {
    // Converts XY point from Spherical Mercator EPSG:900913 to lat/lon in WGS84 Datum
    // Source: http://www.maptiler.org/google-maps-coordinates-tile-bounds-projection/
    var originShift = 2.0 * PI * 6378137.0 / 2.0;
    var lon = (mx / originShift) * 180.0;
    return lon;
}

var poly = Geometry($feature);
if (!IsEmpty(poly)) {
    var pnt_centr = Centroid(poly);
    return MetersToLon(pnt_centr.x);
} else {
    return Null;
}
rymaaneliunas
New Contributor II

Thank you very much! Works perfectly, I see what I did wrong. Cheers!