Survey123 - get first element of a javascript array

1412
4
09-03-2019 10:50 PM
DSIProvince_Nord
New Contributor III

Hi,

I succeeded in converting coordinates from WGS84 to a local coord. system using the pulldata @javascript function.

The javascript function I use (from GitHub - proj4js/proj4js ) takes a geopoint and return what seems to be an array of coordinates (348465.3828225266 283140.21524795296 for exemple). I can see it as a string value.

I need to extract X and Y from this array in separate fields but I can't do a pulldata("@geopoint" ... "x") because it is not recognized as a geopoint.

How can I extract 1st and 2nd values in decimal fields ?

Thanks

0 Kudos
4 Replies
IsmaelChivite
Esri Notable Contributor

Hi.  Here is one way to do it:

Make your custom JS function return a JSON object. Then use pulldata("@json") to extract the X and Y.

Here is a hyper-simplified JS Function showing the creation of your JSON object from the output you have from proj4js.

function projectCoords(location){
    var coords = "348465.3828225266 283140.21524795296";
    var coordsArray = coords.split(' ');
    return '{"x":"' + coordsArray[0] + '" , "y":"' + coordsArray[1] + '" }';
}

The result will put the projected coords in separate fields as shown below.

You may want to set the bind::esri:fieldType XLSForm column to null, so the JSON output of your JS function is not unnecessarily stored in your feature service.

0 Kudos
DSIProvince_Nord
New Contributor III

Thank you very much Ismael it works like a charm.

It is not related, but I noticed that your coordinates now have a comma ( , ) as 1000 separator. In my case I have a space, and I get a message "invalid number".

I tried to do an int(value) or number(value) but doesn't work.

Anyway at the end the value sent is valid but it looks weird for the end user.

0 Kudos
SarahLQuick1
New Contributor

HI IsmaelChivite

I need to use proj4.js offline to convert my geopoint from survey123 to local coordinate system (WKID 21036).

Ive set up the custom javascript function and saved the .js file in the scripts folder within the survey123 project. 

Ive also copied proj4.js file from the dist folder of the latest release to my project scripts folder.

when i test the script in survey123 I get "error: proj4 not found". How to I reference the proj4.js library locally and make the coordinate transformation function available offline.

 

Thanks

Sarah

 

 

0 Kudos
MarcGraham2
Occasional Contributor III

Hi there,

Sorry this is an old thread, but if anyone had an example script incorporating proj4js into survey123 they could share that would be much appreciated.

regards,

Marc

0 Kudos