Select to view content in your preferred language

Feature layer query

6099
21
Jump to solution
01-06-2016 08:03 PM
deleted-user-x7XmeRtVHyGE
Deactivated User

Hello all,

I'm looking for some strategic advice and sample code if possible.

I'm working with a few very large feature services. We are serving these out to a web-based app. Layer1 is a polygon layer and Layer2 is a line layer, with those lines falling within the polygons in Layer1.

Right now our application passes an ID to Layer1 that we then use Layer1.setDefinitionExpression() to segment the data. Now we need to take that boundary and use it to query and display the data from Layer2 where it is within the subset of Layer1.

Any advice on how to proceed? Feature services are necessary for both because of the need for editing.

Enjoy the day,

Mike

0 Kudos
1 Solution

Accepted Solutions
RobertScheitlin__GISP
MVP Emeritus

Michael,

  Try this:

You need to add the features geometry and not just the feature to the geoms array.

function queryGo() {
        var query = new Query();
        query.where = "EntityID = 11828";
        query.outfields = ["*"];
        query.returnGeometry = true;
        query.spatialReference = sr;
        query.outSpatialReference = sr;
        var geoms = [];
        var union;
        //geoms.SpatialReference = sr;
        featureLayer.queryFeatures(query, function (featureSet) {
          for (var i = 0; i < featureSet.features.length; i++) {
            var feature = featureSet.features;
            geoms.push(feature.geometry);
          }
          union = geometryEngine.union(geoms);
          //console.log(union);
          var query2 = new Query();
          query2.geometry = union;
          query2.spatialRelationship = Query.SPATIAL_REL_CONTAINS;
          pondLayer.selectFeatures(query2, FeatureLayer.SELECTION_NEW);
        });
      }

View solution in original post

21 Replies
ChrisSmith7
Honored Contributor

You might want to check out esri/geometry/geometryEngineAsync | API Reference | ArcGIS API for JavaScript

There are quite a few overlay tools, depending on your needs, you can call directly on the client-side now to extract the features of layer2 using boundary from layer1.

RobertScheitlin__GISP
MVP Emeritus

Michael,

   Based on you using an ID to set the DefinitionExpression of Layer1 it sounds like this is a single polygon. If that is the case then you can simply get the geometry of the ID using FeatureLayer queryFeatures and then use that geometry as the Query.geometry and then Query Layer 2 using selectFeatures method. If it is more than on feature in Layer1 then you would use geometryEngine Union to union the features geometries first and then use that unioned geometry to do the selectFeatures on Layer2.

deleted-user-x7XmeRtVHyGE
Deactivated User

Robert and Chris,

As always, thanks for the guidance. I'm trying to implement and am stuck a bit now. Here is a snip of the code I'm using. I think this is on track, but I'm getting ReferenceError: featureSet is not defined.  Any clue where I'm going wrong?

featureLayer.setDefinitionExpression("EntityID = 11828");

var geom = new Polygon(featureLayer);

var query = new Query();

query.where = "EntityID = 11828";

pondLayer.queryFeatures(query, function(featureset){

    for (var i = 0; i < featureSet.features.geometry; i  ) {

    var feature = featureSet.features;};

    geometryEngineAsync.union(feature);

    var query2 = new Query();

    query2.geometry = feature.geometry;

    query2.spatialRelationship = Query.SPATIAL_REL_CONTAINS;

    pondLayer.selectFeatures(query2, FeatureLayer.SELECTION_NEW)

})

//query.spatialRelationship = Query.SPATIAL_REL_ENVELOPEINTERSECTS;

//query.returnGeometry = true;

//query.outFields = ["*"];

//auto zoom           

dojo.connect(featureLayer, "onUpdateEnd", function () {

    var zoomExtent = esri.graphicsExtent(featureLayer.graphics);

    map.setExtent(zoomExtent, true);

});

map.addLayers([featureLayer, pondLayer]);

0 Kudos
KenBuja
MVP Esteemed Contributor

You're declaring "featureset" in the queryFeatures function but then use "featureSet". JavaScript is case sensitive.

deleted-user-x7XmeRtVHyGE
Deactivated User

OK. Now I'm at this point, but I receive the following error: TypeError: a is undefined. Any clue?

Sorry if this is elementary.

var geom = new Polygon(featureLayer);

var query = new Query();

query.where = "EntityID = 11828";

pondLayer.queryFeatures(query, function(featureset){

    for (var i = 0; i < featureset.features.geometry; i  ) {

    var feature = featurset.features;};

    geometryEngineAsync.union(feature);

    var query2 = new Query();

    query2.geometry = feature.geometry;

    query2.spatialRelationship = Query.SPATIAL_REL_CONTAINS;

    pondLayer.selectFeatures(query2, FeatureLayer.SELECTION_NEW)

})

0 Kudos
deleted-user-x7XmeRtVHyGE
Deactivated User

OK. Now I'm at this point, but I receive the following error: TypeError: a is undefined. Any clue?

Sorry if this is elementary.

var geom = new Polygon(featureLayer);

var query = new Query();

query.where = "EntityID = 11828";

pondLayer.queryFeatures(query, function(featureset){

    for (var i = 0; i < featureset.features.geometry; i  ) {

    var feature = featurset.features;};

    geometryEngineAsync.union(feature);

    var query2 = new Query();

    query2.geometry = feature.geometry;

    query2.spatialRelationship = Query.SPATIAL_REL_CONTAINS;

    pondLayer.selectFeatures(query2, FeatureLayer.SELECTION_NEW)

})

0 Kudos
RobertScheitlin__GISP
MVP Emeritus

Michael,

   How many features doe you have coming back from the query.where = "EntityID = 11828"? If it is only one feature is the polygon then you do not need the union.

0 Kudos
deleted-user-x7XmeRtVHyGE
Deactivated User

We have 4 features returned from that EntityID. Overall it varies though, from 1 to ~15, depending on the EntityID.


Thanks, Robert.

I'll need to get you a beer at the User Conference this year!

0 Kudos
RobertScheitlin__GISP
MVP Emeritus

Michael,

  Something more like this:

var geoms = [];
pondLayer.queryFeatures(query, function(featureset){
    for (var i = 0; i < featureset.features.length; i++) {
        var feature = featurset.features;
        geoms.push(feature);
    })
    var union = geometryEngine.uniongeoms(geoms); //Notice I am using geometryEngine and not geometryEngineAsync
    var query2 = new Query();
    query2.geometry = union;
    query2.spatialRelationship = Query.SPATIAL_REL_CONTAINS;
    pondLayer.selectFeatures(query2, FeatureLayer.SELECTION_NEW);
});

Edited!! based on Kens catch.