Subqueries using Features/Polygons in ArcGIS JS

1029
2
02-12-2017 09:08 PM
NickRobles2
New Contributor II

Essentially, I'm trying to perform a second spatial query on a set of returned results from a previous query. The source runs, but it doesn't return any results for floodCalcs.business in the second for-loop. I do not get any errors from this and I do check to make sure that polygons have been loaded into polygons

 

Just realized I don;t know how to insert formatted code in here. Help?

queryShapes = function () {
for (var v = 0; v < shapes.length; v++) {

query = new Query();
select = shapes;
query.geometry = shapes.getExtent();
query.returnGeometry = true;

businessLayer.queryFeatures(query, selectBusiness);
//featureLayer.queryFeatures(query, selectInBuffer);

//perform query on business layer using user drawn shape's bounding box (extent)
function selectBusiness(response) {
var feature;
var features = response.features;
var floodFeatures = [];
var floodFeature;
var inBuffer = [];
var floodBuffer = [];

var q = new Query();
q.geometry = shapes.getExtent();
q.where = "1=1";
q.outSpatialReference = map.spatialReference;
q.outFields = ["*"];
q.returnGeometry = true;
var qTask = new QueryTask("url");
qTask.execute(q);
qTask.on("complete", queryBlock);
//query flood layer using user drawn shapes and cast the returned features as polygons
function queryBlock(f) {
for (var i = 0; i < f.featureSet.features.length; i++) {
polygons.push(new Polygon({ "rings": f.featureSet.features.geometry.rings, "spatialReference": f.featureSet.features.geometry.spatialReference }));
}
}

//filter out features that are not actually in buffer, since we got all points in the buffer's bounding box
for (var c = 0; c < features.length; c++) {
feature = features;
if (shapes.contains(feature.geometry)) {
inBuffer.push(feature.attributes[businessLayer.objectIdField]);
}
//use flood polygons that were returned in queryBlock function to find points in flood zones
for (var b = 0; b < polygons.length; b++) {
var poly = polygons;
if (poly.contains(feature.geometry)) {
floodCalcs.businessF += 1;
}
}
}

var subQuery = new Query();
subQuery.objectIds = inBuffer;
//use a fast objectIds selection query (should not need to go to the server)
featureLayer.selectFeatures(subQuery, FeatureLayer.SELECTION_NEW, function (results) {
floodCalcs.business += sumPopulation(results);
});

}
function sumPopulation(features) {
var popTotal = 0;
popTotal = features.length;
return popTotal;
}

}


}

Tags (1)
0 Kudos
2 Replies
AndrewFarrar
Occasional Contributor

Do you get the expected results from your first query?  Thats where I would start.

I put up a fiddle of how I do nested queries here: JS_Search - JSFiddle 

(note that the services don't work, since JSFiddle requires https://, but I promise the app works normally.)

The fiddle searches for attributes using a findTask, then grabs the geometry from the results and uses that to perform a spatial search for polygon attributes.  not exactly what you are trying to do, but similar.

NickRobles2
New Contributor II

 I actually figured it out. I simply did a simple logical statement:

if (select.contains(feature.geometry) && polygon.contains(feature.geometry)) {
floodCalcs.businessF += 1;
}

My only issue now is totally different. Is there a way to convert a feature layer (of polygons) to an array of polygons?

0 Kudos