Arcade expression for string of LatLon coordinates from Polygon in ArcGIS Online

1830
8
02-01-2021 10:13 AM
TaylorSchyrbiak
New Contributor II

I have an arcade expression as follows in the add-ons of a polygon hosted feature layer on arcgis online:

 

var geom = Geometry($feature);

 

var firstpart = geom.rings[0];

 

var outparts = [];

 

var counter = 0;

 

function metersToLatLon(mx, my) {

 

   var originShift = 2.0 * PI * 6378137.0 / 2.0;

 

   var lon = (firstpart[pt].x / originShift) * 180.0;

 

   var lat = (firstpart[pt].y / originShift) * 180.0;

 

   lat = 180.0 / PI * (2.0 * Atan(Exp(lat * PI / 180.0)) - PI / 2.0);

return Concatenate(lat, " ", lon);

 

}

 

for (var pt in firstpart){

 

    var ptstr = metersToLatLon(firstpart[pt].x, firstpart[pt].y);

 

    outparts[counter]= ptstr;

 

    counter = counter + 1;  

 

}

 

return Concatenate(outparts, ";")

 

 

The expression works as intended when tested, however, when I try to view in the popup or attributes table, nothing appears. It also has not successfully saved in the layer. Ideas??

The goal of this workflow is to have a polygon shape passed through URL to a survey123 form geoshape question, hence the metersToLatLon function. 

0 Kudos
8 Replies
XanderBakker
Esri Esteemed Contributor

Hi @TaylorSchyrbiak ,

Have you manually tried to pass the geoshape to the URL to see if it works? How are you consuming the URL with the geoshape? In a browser or using the Survey123 app? What version do you have? If I looked correctly I see that the geoshape was first introduced at version 3.6.137:

https://doc.arcgis.com/en/survey123/faq/pdf/whatsnewarchive.pdf 

However, I am not sure if there is support for geoshapes in the URL when a Survey is edited in the browser...

 

Since you already have the geometry would using the inbox be a solution for you? https://doc.arcgis.com/en/survey123/desktop/create-surveys/prepareforediting.htm 

Tags (1)
0 Kudos
TaylorSchyrbiak
New Contributor II

I went a step further and set up the URL with my survey... and using my arcade expression above, I successfully passed a polygon shape from collector for arcgis to my geoshape question in the survey123 field app. Not sure why it won't show in the pop up or attribute table though. And indeed, the web app does not appear to have this capability for geoshapes. 

0 Kudos
XanderBakker
Esri Esteemed Contributor

Hi @TaylorSchyrbiak ,

There can be several reasons. Maybe the different text sections are not structured in a way to pass a valid URL. In Arcade you have the UrlEncode function that will replace spaces by %20 and other characters by something that is the valid representation in a URL.

It is also necessary to explain how you create the URL that is presented in the pop up. 

It would be good if you could post an example of the URL that is formed at this moment to see what is going wrong. 

0 Kudos
TaylorSchyrbiak
New Contributor II

This is the URL I currently have set up (other than I've just listed the surveyID as 123):

arcgis-survey123://?itemID=123&field:spray_status={spray_status}&field:vineyard_={vineyard_}&field:block_={block_}&field:polygon_={expression/expr0}

This is the URL is currently produces:

arcgis-survey123://?itemID=123&field:spray_status=requested&field:vineyard_=Bullpine&field:block_=BullpineD&field:polygon_=

And this is an example of what I am trying to pass to the polygon_ question from the arcade expression first mentioned in this post:

49.076299867222936 -119.49206557279504;49.07729539411242 -119.49157106101092;49.07748055058896 -119.49015584073926;49.076918323830725 -119.48940482221228;49.07629733102711 -119.48848015844256;49.07563048532977 -119.48942211118825;49.07576573916958 -119.49133601232523;49.07553254850802 -119.49236478454819;49.076299867222936 -119.49206557279504

 

Also, even though it doesn't show in the url, it does transfer to the survey123 field app properly. 

0 Kudos
XanderBakker
Esri Esteemed Contributor

Hi @TaylorSchyrbiak ,

When you construct a URL there can be multiple interpretations of the URL. For a URL to be valid I suppose you would have to apply the UrlEncode function (see: https://developers.arcgis.com/arcade/function-reference/text_functions/#urlencode ) to the geoshape in order to replace the spaces by %20 and the semicolons by %3B. 

I have also had some problems in the past using arcgis-survey123:// to create a valid link to Survey123 and have used "https://survey123.arcgis.app?itemID=yourID&... with better results in the past.

 

In order to validate, I would have to create a survey and a web map with polygons to test this out and I am not sure when I will have time to do this. 

0 Kudos
TaylorSchyrbiak
New Contributor II

I tried applying the UrlEncode function to the arcade expression, and while it again showed successfully in the test,

(e.g. 49.076299867222936%20-119.49206557279504%3B49.07729539411242%20-119.49157106101092%3B49.07748055058896%20-119.49015584073926%3B49.076918323830725%20-119.48940482221228%3B49.07629733102711%20-119.48848015844256%3B49.07563048532977%20-119.48942211118825%3B49.07576573916958%20-119.49133601232523%3B49.07553254850802%20-119.49236478454819%3B49.076299867222936%20-119.49206557279504)

it still didn't show in the pop-up nor the URL that was formed. 

Thanks for your input. The fact that it still passes to the survey123 field app means that I can get by for now. 

0 Kudos
tendtreesTC
New Contributor III

Hi TaylorSchyrbiak,

This is something I have been trying to do without any success.  Would you be able to post the entire expression?

0 Kudos
JoshConrad1
New Contributor III

All:

I have been experiencing the same issues described in this thread: trying to create an attribute expression that calculates lat/long coordinates from a hosted feature layer, then integrate Field Maps with Survey123 to open a form and have the geopoint plot the submitted survey on top of the feature using the result of the attribute expression using a custom URL via the custom URL popup (&center=).

I have been using the "Expression/expr2 - Code to calculate geometry" formula called out in this post (and a multitude of others on the community page) and it was correctly calculating the coordinates in ArcGIS Online Web Viewer Classic, but then when we tested this functionality in the field, the point was not mapping.

I found that the original formula in the "Expression/expr2 - Code to calculate geometry" results in an Array data type, with associated brackets. These brackets do not show up in ArcGIS Online, but when you view the popup in Field Maps, you can see the brackets. So because of this, i tested further and in the original function, i removed the brackets in the result line, which worked but was not perfect. It was not perfect because the coordinate results ended up being truncated to 2 decimals, and even though the submitted form was executing, the point was being plotted hundred of feet away.

After 10 hours of testing and researching these threads (which by the way, all of the contributions are EXPONENTIALLY HELPFUL AND I AM SO GRATEFUL OF YOUR KNOWLEDGE) i was able to find the CORRECT SOLUTION:

1.) Create an attribute expression for latitude:

var lat;
var originShift = 2.0 * PI * 6378137.0 / 2.0;

lat = number(Geometry($feature).y / originShift) * 180.0;
lat = 180.0 / PI * (2.0 * Atan( Exp( lat * PI / 180.0)) - PI / 2.0);
return lat;

2.) Create an attribute expression for longitude:

var lon;
var originShift = 2.0 * PI * 6378137.0 / 2.0;

lon = number(Geometry($feature).x / originShift) * 180.0;
return lon;

3.) Because of the "number" identifier, In ArcGIS Online Web Map Viewer Classic go to Configure Popups, make sure your pop up display is custom, then the important step is to click Configure Attributes. Navigate to the newly created expressions and expand the decimal places to a minimum of 5.

4.) Lastly, in the custom URL make the "&center={expression/expr3},{expression/expr4} where expr 3 is Lat and expr 4 is Long.

I have been testing since i discovered this and it has been running smoothly. Happy Mapping!

0 Kudos