Survey123 - JS fuctions - Intersected areas

288
1
10-28-2020 05:59 PM
CristianGraf
New Contributor II
Hello  We are preparing a project whose aim is to validate the presence or
absence of availability linked to an area (Cercles). An area can contain several availabilities, but when selecting it
we want to validate these availabilities. In other words, in a group of polygons in the same service, for a
selected element we want to know which polygons are intersected. This operation must be done in the Survey123 app and we try to proceed
with JS functions. We tested the JS example well using a point and it works. But how to use
it with polygons?  I'm not a programmer, and my fellow programmers are struggling with this problem.  Is it feasible to think of being able to select a polygon and have as answer a
list of intersected polygons?  Do you have examples or improvements to put on our code?

Any ideas?!  Thank you!


function buildFormData(data, boundary) {


 let body = ""
 for (let key in data){
 // console.debug(key)
 body += "--" + boundary
 + "\r\nContent-Disposition: form-data; name=" + key
 + "\r\n\r\n" + (isObject(data[key]) ? JSON.stringify(data[key]) : data[key]) + "\r\n"
 }
 body += "--" + boundary + "--\r\n"

 return body

}

// echo provides a basic check that JavaScript is working correctly
function echo(input) {
 return input.toString();
}

function isObject(obj) {
 return (typeof obj === "object" || typeof obj === "function") && obj !== null;
}

function buildFormData(data, boundary) {


 let body = ""
 for (let key in data){
 // console.debug(key)
 body += "--" + boundary
 + "\r\nContent-Disposition: form-data; name=" + key
 + "\r\n\r\n" + (isObject(data[key]) ? JSON.stringify(data[key]) : data[key]) + "\r\n"
 }
 body += "--" + boundary + "--\r\n"

 return body

}

function encodeQueryParams(data) {
 const ret = [];
 for (let d in data) 
 ret.push(encodeURIComponent(d) + "=" + encodeURIComponent(data));
 return ret.join("&");
}

function callEsriJsonApiSync(url, token, method = "GET", payload = null) {

 let boundary = "nVenJ7H4puv"
 let responseAsJson = null;
 let queryParams = null;
 let formData = null;

 const requestFormat = {
 f: "pjson"
 };

 if (method === "GET") {
 queryParams = Object.assign(
 Object.assign(requestFormat, { token: token }),
 payload
 );
 url = url + "?" + encodeQueryParams(queryParams);
 console.debug(url)
 } else {
 url = url + "?" + encodeQueryParams({ token: token });
 // formData = new FormData();
 // Object.keys(Object.assign(payload, requestFormat)).forEach(function (k) {
 // let value = isObject(payload) ? JSON.stringify(payload) : payload;
 // formData.append(k, value);
 // });
 formData = buildFormData(Object.assign(payload, requestFormat), boundary)

 }

 let xhr = new XMLHttpRequest();
 xhr.open(method, url, false);

 if (method === 'POST') {
 xhr.setRequestHeader("Content-Type", "multipart/form-data; boundary=" + boundary)
 xhr.setRequestHeader("Content-Length", formData.length)
 }
 
 xhr.send(formData);

 if (xhr.status === 200) {

 responseAsJson = JSON.parse(xhr.response);
 if (!responseAsJson.error) {
 // console.debug("response", responseAsJson);
 return responseAsJson;
 }
 console.error(responseAsJson.error);
 return null;
 } else {
 console.error(xhr.status);
 return null;
 }
}

// Query a feature layer and returns the feature that intersects the location
//function featureByLocation(layerURL, location, token) {
function featureByLocation(F_Poste) {
 // Output value. Initially set to an empty string (XLSForm null)
 let outValue = "";
 let geoValue = "";
 let token = "TOKEN HERE";
 let layerURL = "URL-SERVICE HERE";
 let poste = "125-12";//F_Poste;
 
 if (poste == null || poste === "") {
 // The function can't go forward; exit with the empty value
 return "Il n'y a pas de poste.";
 }
 
let firstUrl =
 "https://services.arcgis.com/DN2fPfpggEPlLhP6/arcgis/rest/services/Formulaire1620_100_AlexisNihon_Stantec_formulaire/FeatureServer/0/query"

let firstPayload = {
 geometryType: "esriGeometryEnvelope",
 spatialRel: "esriSpatialRelIntersects",
 outFields: "*",
 returnGeometry: "true",
 where: "1=1"
};
 let firstResult = callEsriJsonApiSync(firstUrl, token, "GET", firstPayload);
 // console.info('firstResult => ', firstResult)
 return firstResult
 
 let buffer = firstResult.features[0].geometry

let secondPayload = {
 geometryType: "esriGeometryPolygon",
 spatialRel: "esriSpatialRelIntersects",
 outFields: "*",
 returnGeometry: "false",
 where: "1=1",
 geometry: buffer,
 returnCountOnly: "true"
};

let secondResult = callEsriJsonApiSync(firstUrl, token, "POST", secondPayload);
//console.info('secondResult => ', secondResult)

let nb = secondResult.count
//console.info(nb)
 

 return nb;
}


Tags (1)
0 Kudos
1 Reply
JamesTedrick
Esri Esteemed Contributor

Hi,

Apologies for the delay in response.  The second query looks to be properly constructed; what is the message you are receiving (based on your description, I assume that it is failing?  One thing you might need to do is stringify the second geometry (convert it from an object to text) using the JSON.stringify() method.

0 Kudos