Survey123 Spatial Analysis/Overlapping polygons

623
4
11-10-2020 02:15 AM
CherylWheeler2
New Contributor

Hi,

Would anybody know whether there is way within Survey123 to check whether a new polygon being collected via a geoshape question overlaps an existing polygon/record collected in previous surveys? So, some kind of constraint for spatial functionality.

Many thanks for any suggestions.

0 Kudos
4 Replies
Ruth_JiatengXu
Esri Contributor

Hi,

There is no existing function to do this, however, Survey123 does support the custom JS function.

Here are some good resources:

 Blog: https://community.esri.com/t5/arcgis-survey123-blog/extending-survey123-smart-forms-with-custom-js-f... 

Doc: https://doc.arcgis.com/en/survey123/desktop/create-surveys/pulldatajavascript.htm (Please pay attention to the known limitations at the end of this doc)

I have also attached a sample XLSform (Please change the feature layer url in the pulldata function) and a Javascript code (Please take the code and save it as .js file, then put the js. file into the scripts folder as described in the documentation).

The following code can do simple overlap detection and returns the count of the existing polygons that are overlapping.

 

 

function overlapDetection(geometry, token, featurelayer_url) {
    // If there is no geometry input, output null
    if (!geometry) {
        return "Wait for input geometry.";
    }

    // If the survey is submitted in the webapp, convert the geometry to object
    if (typeof(geometry) != "object") {      
        var geoArray = geometry.split(';');
        geoArray.forEach(ArraytoAA);
        var geoObject = {"rings": [geoArray]};
    } else {
        var geoObject = geometry;
    }

    var geoString = JSON.stringify(geoObject);

    // Change the parameters in the query URL to work with different geometryType, spatialRel, etc.
    // Ref: https://developers.arcgis.com/rest/services-reference/query-feature-service-layer-.htm
    var url = featurelayer_url+"/query?where=1%3D1&geometry="+geoString+"&geometryType=esriGeometryPolygon&spatialRel=esriSpatialRelOverlaps&units=esriSRUnit_Meter&returnCountOnly=true&f=json";

    if (token) {
        url =  url+"&token="+token;
    }

    var xhttp = new XMLHttpRequest();
    xhttp.open("GET", url, false);
    xhttp.send();

    if (xhttp.status!==200) {
        return (xhttp.status);
    } else {
        var responseJSON=JSON.parse(xhttp.responseText)
        if (responseJSON.error) {
            return JSON.stringify(responseJSON.error);
        } else {
            return JSON.stringify(responseJSON.count);
        }
    }
}

function ArraytoAA(item,index,array) {
    var coord = item.split(' ').map(Number)
    array[index] = coord;
}

 

 

 Thanks,

Ruth 

0 Kudos
CherylWheeler2
New Contributor

Thank you very much @Ruth_JiatengXu Ruth for your reply. I will experiment with your ideas. I really appreciate your response.

0 Kudos
ZacharySutherby
Esri Contributor

Hello @CherylWheeler2

On top of what Ruth suggests another workflow would be to use the Search() appearance which we currently have support for in our 3.12 beta builds. 

The Search() appearance lets your query existing Feature Services to see if there is a spatial overlap. 

Please check out our Early Adopter Community for more information on using the Search() appearance in your survey. 

 

Thank you, 

Zach

0 Kudos
IzzyMcLees
New Contributor II

Hi @Ruth_JiatengXu ,

Your response above was incredibly helpful and I have managed to implement the JavaScript function above into my survey, however it is only able to detect an overlap when I am drawing polygons with straight edges e.g. squares, rectangles, triangles.

When I draw polygons by free-hand or that are circle (any polygon with a curved edges), it returns the error:

@javascript error:ReferenceError: xmlhttp is not defined in overlapDetection.js:overlapDetection

Which is returned at the line of code:

if (xhttp.status!==200) {

        return (xmlhttp.status);

When I changed xmlhttp.status to xhttp.status, I got a 404 error: “page not found” so I believe it’s a problem with the query URL.

Do you know why this might be and is there a work around?

Many thanks,

Izzy

0 Kudos