Calculating a geopoint from a feature service response json object

267
3
Jump to solution
12-02-2020 06:48 PM
KimberlyMcCallum
New Contributor III

I have survey with a custom JavaScript function that fetches a point feature by a user-provided fire incident ID using a pulldata calculation in Survey123 Connect (latest beta version). I'd like to get the attributes and the geopoint location to populate in my survey questions. I have been able to follow the sample provided to get a JSON object and populate my text and date fields but I am struggling to parse the object to extract and populate the geopoint. I reviewed this post on using calculations to get geopoints but I'm not getting the calculation right... 

Below is the json feature object I am working with (I used JS to add the geopointCoords property thinking this would work but it didn't help):

{
  "attributes": {
    "IncidentName": "Beachie Creek",
    "POOState": "US-OR",
    "POOCounty": "Marion",
    "FireDiscoveryDateTime": 1597601880000
  },
  "geometry": {
    "x": -13601905.941048512,
    "y": 5593402.744904984,
    "geopointCoords": "5593402.744904984 -13601905.941048512"
  }
}


Below are a few things I tried in the calculation field to populate the geopoint but both are wrong and result in an Error caused by java.lang.NumberFormatException.

pulldata("@json", ${irwinFeatureData}, concat("geometry.y", " ", "geometry.x"))

and 

pulldata("@json", ${irwinFeatureData}, "geometry.geopointCoords")

If anyone has any insight, I'd be very grateful! 

Reply
0 Kudos
1 Solution

Accepted Solutions
IsmaelChivite
Esri Frequent Contributor

Try this syntax to extract the coordinates:

pulldata("@json", ${irwinFeatureData},"geometry.x") and pulldata("@json", ${irwinFeatureData},"geometry.y")

Once you have the X an Y you could use the concat function. For example, if you want everything in one line:

concat(pulldata("@json", ${irwinFeatureData},"geometry.x")," ",pulldata("@json", ${irwinFeatureData},"geometry.y"))

If you plan to use this from the Survey123 web app it will be best that you use one hidden question for XY coordinates and then you reference them in your concat. The Survey123 web app like pulldata functions to be used alone.

You can use esriFieldType null in your XLSForm to make sure your intermediate calculations are not stored in your database.

Also, please bear in mind that to properly center a geopoint question you must use lat/lon values.

View solution in original post

Reply
0 Kudos
3 Replies
IsmaelChivite
Esri Frequent Contributor

Try this syntax to extract the coordinates:

pulldata("@json", ${irwinFeatureData},"geometry.x") and pulldata("@json", ${irwinFeatureData},"geometry.y")

Once you have the X an Y you could use the concat function. For example, if you want everything in one line:

concat(pulldata("@json", ${irwinFeatureData},"geometry.x")," ",pulldata("@json", ${irwinFeatureData},"geometry.y"))

If you plan to use this from the Survey123 web app it will be best that you use one hidden question for XY coordinates and then you reference them in your concat. The Survey123 web app like pulldata functions to be used alone.

You can use esriFieldType null in your XLSForm to make sure your intermediate calculations are not stored in your database.

Also, please bear in mind that to properly center a geopoint question you must use lat/lon values.

View solution in original post

Reply
0 Kudos
KimberlyMcCallum
New Contributor III

Hi @IsmaelChivite , Thank you for your prompt answer! It took me to get back to it but unfortunately, I wasn't able implement your solution... I added a helper function in the scripts>functions.js to convert the coordinates to lat/lon. I modified the request function to call the coordinate conversion function and add the formatted string as a property to the json object returned from the request. This seemed cleaner to me and I wasn't able to get the hidden questions suggestion to work. Unfortunately, I am still getting a parsing error even though, if I copy the property value (e.g., "44.82111000000005 -122.188") into the geopoint calculation, it works. I have attached my survey folder. If you or anyone is able to take a look and help me spot my error, I'd be greatly appreciate! 

If it's easier to look at my JavaScript functions outside of Survey123, I put them in this repl.it: https://repl.it/@kmccallum/EsriFeatureServiceJS#script.js

Best, Kim 

Reply
0 Kudos
KimberlyMcCallum
New Contributor III

I came back this morning with fresh eyes and was able to fix my error. I also found this helpful solution from @JamesTedrick and realized that I was wrong thinking that I could pass a formatted string to a geopoint and that I have to explicitly cast it to numeric. After implementing the following changes, I was able to make the solution provided by Ismael work: 

1. Changed my helper coordinate conversion function to return an array of lat, lon and added these to my object properties in my request function. 

2 . In my XLS form, added the hidden lat and lon fields as Ismael suggested to pull out lat/lon values. I added an bind::esri::fieldType of null. 

3. Then I added my geopoint question with the calculation syntax of: concat(number(${lat}),' ', number(${lon}))

Thank you all for your help!

Reply
0 Kudos