使用ArcGIS JS中的“要素/多边形”的子查询

时间:2017-02-13 05:23:39

标签: jquery arcgis arcgis-js-api

基本上,我尝试对先前查询的一组返回结果执行第二次空间查询。源运行,但它没有在第二个for循环中返回 floodCalcs.businessF 的任何结果。我没有从中得到任何错误,我确实检查以确保多边形已加载到多边形(这是一个全局变量)。

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

                query = new Query();                    
                select = shapes[v];
                query.geometry = shapes[v].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[v].getExtent();
                    q.where = "1=1";
                    q.outSpatialReference = map.spatialReference;
                    q.outFields = ["*"];
                    q.returnGeometry = true;
                    var qTask = new QueryTask("ommitted 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[i].geometry.rings, "spatialReference": f.featureSet.features[i].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[c];
                        if (shapes[v].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[b];
                            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;
                }

            }


        }

1 个答案:

答案 0 :(得分:1)

QueryTask.execute函数返回Deferred。这实际上意味着在执行函数之前执行queryBlock之后的for循环。所以当你循环polygons时,它将是空的。

此外,f.featureSet.features[i].geometry已经是Polygon,您不需要再次创建新的。{/ p>

尝试这样的事情

&#13;
&#13;
var q = new Query();
q.geometry = shapes[v].getExtent();
q.where = "1=1";
q.outSpatialReference = map.spatialReference;
q.outFields = ["*"];
q.returnGeometry = true;
var qTask = new QueryTask("ommitted URL");
var promise = 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(f.featureSet.features[i].geometry);
  }                          
}
promise.then(function(){
  //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[c];
    if (shapes[v].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[b];
      if (poly.contains(feature.geometry)) {
        floodCalcs.businessF += 1;
      }
    }
  }
}
&#13;
&#13;
&#13;