AnsweredAssumed Answered

Question about geometry property when using queryTask to do spatial query

Question asked by franklin.alexander on May 7, 2019
Latest reply on May 8, 2019 by franklin.alexander

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!! 

Outcomes