Survey123 - get first element of a javascript array

819
3
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
3 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
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