Pulldata JS to spatial join a Geopoint within a repeat

883
3
11-09-2021 08:30 AM
JoshStiller
New Contributor

Hello,

I am building a citizen science form that I need to spatial join the geopoint with a custom feature layer to assign a wildlife management unit, township, and county.  I have it set up so the user only needs to put their personal information in once and then there is a repeat for each observation they encounter.  For each observation, they need to identify a geopoint location.  I then call that geopoint within the repeat and use JSON pull data to get the 3 attributes I'm interested in.

If I enter a single observation, everything functions as desired.  However, if I enter two observations, the JSON query only works on the first observation and the second one returns "null".

Anyone know if the Java script I'm running isn't compatible with repeats? Thanks for any help!

 

 

/*

* JavaScript functions for Survey123

*/

 

 

 

function queryWMU(location,fields,token,debugmode){

if (location===""){

return (debugmode? "Location Object is empty":"");

}

var featureLayer = "https://services6.arcgis.com/DZHaqZm9cxOD4CWM/arcgis/rest/services/Wildlife_Management_Units/Feature...";

var coordsArray = location.split(" ");

var coords = coordsArray[1] + "," + coordsArray[0]

 

var xmlhttp = new XMLHttpRequest();

var url = featureLayer + "/query?geometry=" + coords + "&geometryType=esriGeometryPoint&inSR=4326&spatialRel=esriSpatialRelIntersects&outFields=" + fields + "&returnGeometry=false&returnCount=1&f=json"

 

xmlhttp.open("GET",url,false);

xmlhttp.send();

 

if (xmlhttp.status!==200){

return (debugmode? xmlhttp.status:"");

} else {

var responseJSON=JSON.parse(xmlhttp.responseText)

if (responseJSON.error){

return (debugmode? JSON.stringify(responseJSON.error):"");

} else {

if (responseJSON.features[0]){

return JSON.stringify(responseJSON.features[0]);

}

else{

return (debugmode? "No Features Found":"");

}

}

}

}

function queryTown(location,fields,token,debugmode){

if (location===""){

return (debugmode? "Location Object is empty":"");

}

var featureLayer = "https://gisservices.its.ny.gov/arcgis/rest/services/NYS_Civil_Boundaries/FeatureServer/5";

var coordsArray = location.split(" ");

var coords = coordsArray[1] + "," + coordsArray[0]

 

var xmlhttp = new XMLHttpRequest();

var url = featureLayer + "/query?geometry=" + coords + "&geometryType=esriGeometryPoint&inSR=4326&spatialRel=esriSpatialRelIntersects&outFields=" + fields + "&returnGeometry=false&returnCount=1&f=json"

 

xmlhttp.open("GET",url,false);

xmlhttp.send();

 

if (xmlhttp.status!==200){

return (debugmode? xmlhttp.status:"");

} else {

var responseJSON=JSON.parse(xmlhttp.responseText)

if (responseJSON.error){

return (debugmode? JSON.stringify(responseJSON.error):"");

} else {

if (responseJSON.features[0]){

return JSON.stringify(responseJSON.features[0]);

}

else{

return (debugmode? "No Features Found":"");

}

}

}

}

function queryCounty(location,fields,token,debugmode){

if (location===""){

return (debugmode? "Location Object is empty":"");

}

var featureLayer = "https://gisservices.its.ny.gov/arcgis/rest/services/NYS_Civil_Boundaries/FeatureServer/2";

var coordsArray = location.split(" ");

var coords = coordsArray[1] + "," + coordsArray[0]

 

var xmlhttp = new XMLHttpRequest();

var url = featureLayer + "/query?geometry=" + coords + "&geometryType=esriGeometryPoint&inSR=4326&spatialRel=esriSpatialRelIntersects&outFields=" + fields + "&returnGeometry=false&returnCount=1&f=json"

 

xmlhttp.open("GET",url,false);

xmlhttp.send();

 

if (xmlhttp.status!==200){

return (debugmode? xmlhttp.status:"");

} else {

var responseJSON=JSON.parse(xmlhttp.responseText)

if (responseJSON.error){

return (debugmode? JSON.stringify(responseJSON.error):"");

} else {

if (responseJSON.features[0]){

return JSON.stringify(responseJSON.features[0]);

}

else{

return (debugmode? "No Features Found":"");

}

}

}

}

 

 

3 Replies
DougBrowning
MVP Esteemed Contributor

Have you looked at the new search appearance type for this?  It seems like it would be easier if it is a arc service but not sure.

https://community.esri.com/t5/arcgis-survey123-blog/dynamic-choice-lists-using-search-appearance/ba-...

 

JoshStiller
New Contributor

Thank you for the suggestion.  That is so close to what I need, but not quite there.  I need it to autoassign the value based on the geopoint being within the polygon.  The option described in that post allows me to populate a drop-down list, but it still requires the user to select the management unit (it will narrow it down from 95 to only 1).  I want the field to be hidden and to populate based on the geopoint. 

 

Am I missing something obvious on how to do that?  

0 Kudos
CarlosSousaFerreira
New Contributor III

@JoshStiller Just to say thank you, cause this code helped me a lot! I was trying to query a service not in WGS84 and the inSR parameter wasn't doing anything using the pulldata("@layer", "getRecordAt",... function. But this code worked very well! So thank you! 😊,

0 Kudos