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!
Solved! Go to Solution.
Try this:
Expression:
var poly = Geometry($feature);
var result = "";
if (!IsEmpty(poly)) {
var pnt_centr = Centroid(poly);
Console(pnt_centr);
result = "(" + Round(pnt_centr.X, 2) + ", " + Round(pnt_centr.Y, 2) + ")";
}
return result
Returns this result:
Try this:
Expression:
var poly = Geometry($feature);
var result = "";
if (!IsEmpty(poly)) {
var pnt_centr = Centroid(poly);
Console(pnt_centr);
result = "(" + Round(pnt_centr.X, 2) + ", " + Round(pnt_centr.Y, 2) + ")";
}
return result
Returns this result:
That worked, and gets me half the way there. I still need those coords formatted to decimal degrees. Is that possible to do using Arcade? I much appreciate all your help so far.
If you have projected coordinates you cannot currently translate (project) those to geographic coordinates. That might come in the features when more geometric functions will be implemented.
Hey Xander!
Is there a way to change the projection from one projected coordinate system to another in arcade?
Just this morning I did a test to see f I could create a URL that initiates Waze in navigation mode. In Colombia only historic traffic is available in Navigator, and our client needed real time traffic info.
To make this work the coordinates need to be provided in lat lon. At the DevSummit this month there was an announcement that the JavaScript API would will include the projection engine to do client side projections, which is very cool and fast. Not sure if we will be able to take advantage of this in the update next month (April 10th).
To be able to advance and provide some solution for this client I came up with the following expression:
function MetersToLatLon(mx, 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 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];
}
function CreateWazeURL(lat, lon) {
return "https://waze.com/ul?ll=" + lat + "," + lon + "&navigate=yes"
}
var latlon = MetersToLatLon(Geometry($feature).X, Geometry($feature).Y);
var url = CreateWazeURL(latlon[0], latlon[1]);
return url;
I did some testing with a web map on my iPad and it works like a charm. This only convert the Web Mercator Auxiliary Sphere coordinates to WGS1984 decimal degrees.
CC Gus Monteverde , I think this could solve your problem too related to obtaining the coordinate as decimal degrees:
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);
Console(pnt_centr);
var latlon = MetersToLatLon(pnt_centr[0], pnt_centr[1]);
result = "(" + Round(latlon[0], 6) + ", " + Round(latlon[1], 6) + ")";
}
return result
Hi Xander Bakker - this is terrific, however I get this error when Testing in Arcade Expression builder:
Execution Error:Runtime Error: Cannot call member property on object of this type.
- I've simply cut/pasted your code above, using on a polygon feat serv.
Any ideas why I'd get that message?
Thanks!
That's strange... Do you have multipart geometry? What coordinate system is your data using?
Is the service shared publicly? If so, can you share the URL so i can test against it?
Thanks Xander Bakker
It was multipart, but I've corrected that, and still get that same error.
This is a public view of the data:
https://services6.arcgis.com/CubtY3H3UowKJNc4/arcgis/rest/services/Buildings_and_Structures_2_1/Feat...
And when I try on another simpler (public) polygon feat serv, I get the same error also.
https://services6.arcgis.com/CubtY3H3UowKJNc4/arcgis/rest/services/Fire_History/FeatureServer
Perhaps I am doing something wrong? I am simply opening up the Attribute Expression builder and pasting in the code above (below), and hitting 'Test'