Question about geometry property when using queryTask to do spatial query

657
3
Jump to solution
05-07-2019 08:34 AM
FranklinAlexander
Occasional Contributor III

I have a web app that is pulling in two layers from ArcGIS REST, a point layer and a polygon layer. I already used queryTask to filter out features from the point layer and return a max value based on a statistical calc, and then set a layer definition so that only those points with the max value will render in the map. Now I want to use those points to do a spatial query that filters out all but the features from the polygon layer that intersect a point in the point layer. I am doing something wrong with the geometry because I get a type error: "Cannot read property 'wkid' of undefined" when trying to run the spatial query:

//run spatial query on polygon layer using geometry from point layer then pushes
//resulting feature id to array
function getSubRegions(geometry, srArray) {
        var subRegUrl = 
           "https://ocean.floridamarine.org/arcgis/rest/services/.../MapServer/2";
        var queryTask = new QueryTask(subRegUrl);
        var subRegQuery = new Query();
        subRegQuery.where = "1=1";
        subRegQuery.outFields = ["*"];
        subRegQuery.geometry = geometry;
        subRegQuery.spatialRelationship = Query.SPATIAL_REL_INTERSECTS;
        subRegQuery.returnGeometry = true;
        //console.log("subregion query ", subRegQuery);
        queryTask.execute(subRegQuery).then(function(result) {
            if(result.features[0] != undefined) {
                var subRegID = result.features[0].attributes.OBJECTID;
                if(srArray.indexOf(subRegID) === -1) {
                    srArray.push(subRegID);
                }
            }
        });
}

//gets point layer and loops through to select intersecting polygons. Resolves polygon
//feature ids
function getSitesGeo(currentBatch, sitesUrl) {
        var queryTask = new QueryTask(sitesUrl);
        var batchQuery = new Query();
        batchQuery.where = "Batch = " + currentBatch;
        batchQuery.outFields = ["*"];
        batchQuery.returnGeometry = true;
        return new Promise((resolve, reject) => {
            queryTask.execute(batchQuery).then(function(result) {
                //console.log("sites results ", result);
                let srArray = [];
                let features = result.features;
                features.forEach(function(feature) {
                    var geometry = feature.geometry;
                    if(isNaN(parseFloat(geometry.x)) === false) {
                        getSubRegions(geometry, srArray);
                    }
                });
                resolve(srArray);
            });
        });
}

//filters points by max field value, uses resulting points as geometry for spatial //query on polygon layer. Returns values for definition query.
function getBatchNumnber() {
        var sitesUrl = 
           "https://ocean.floridamarine.org/arcgis/rest/services/.../MapServer/0";
        var queryTask = new QueryTask(sitesUrl);
        var batchQuery = new Query();
        var statDef = new StatisticDefinition();
        statDef.statisticType = "max";
        statDef.onStatisticField = "Batch";
        statDef.outStatisticsFieldName = "CurrentBatch";
        batchQuery.where = "1=1";
        batchQuery.outFields = ["*"];
        batchQuery.outStatistics = [statDef];
        batchQuery.returnGeometry = true;
        queryTask.execute(batchQuery).then((result) => {
            var currentBatch = result.features[0].attributes.MAX_Batch;
            window.currentBatch = currentBatch;
            getSitesGeo(currentBatch, sitesUrl)
            .then(result => {
                window.subRegIds = result;
            })
        });
}

//create def query in Local Layer widget.js
lLayer = new ArcGISDynamicMapServiceLayer(layer.url, lOptions);
            if (layer.hasOwnProperty('definitionQueries')) {
                var definitionQueries;
                if(window.currentBatch) {
                    definitionQueries = {
                        0: "Batch = " + window.currentBatch,
                        1: "OBJECTID IN (" + window.subRegIds.join(',') + ')',
                        2: "OBJECTID IN (" + window.subRegIds.join(',') + ')'
                    }
             }

Everything is working fine except for the points geometry type error. I am not sure if I have the geometry configured wrong (it is in an array), or if I am miss-understanding how the spatial query is supposed to work

I think this is something fairly simple that I am doing wrong, just need a little help seeing it. Thanks!! 

0 Kudos
1 Solution

Accepted Solutions
RobertScheitlin__GISP
MVP Emeritus

Franklin,

   A Query objects geometry property is only designed to take a single geometry, Not an array. So you will have to combine the array of points into one MultiPoint or run the query on each point individually.

View solution in original post

0 Kudos
3 Replies
RobertScheitlin__GISP
MVP Emeritus

Franklin,

   A Query objects geometry property is only designed to take a single geometry, Not an array. So you will have to combine the array of points into one MultiPoint or run the query on each point individually.

0 Kudos
FranklinAlexander
Occasional Contributor III

Awesome, I thought it was something of that nature. I will look into combining the points to one multipoint, it sounds like that may be the best solution. 

thanks again!

0 Kudos
FranklinAlexander
Occasional Contributor III

Robert, 

That worked. I couldn't get the multipoint layer to work, so I just looped through the points and ran the spatial query on each one. I corrected the above code block to reflect what is working. 

Thanks! 

0 Kudos