Finding a FeatureLayer in the map and calling queryFeatures

2850
5
04-15-2016 10:05 AM
RiverTaig1
Occasional Contributor

I have a map service located here: River/GDT_SDE (MapServer)  that has 10 sub layers.  I want to use queryFeatures on the "Waypoints" sublayer.  How can I loop through all of the layers in the map (sublayers too) and find that layer  by its name and then call queryFeatures on it?  In the examples I have seen, a new FeatureLayer is always created... I don't want to create a new FeatureLayer from the URL, but rather get it from the map.

(queryFeatures is documented here: FeatureLayer (legacy) | API Reference | ArcGIS API for JavaScript ). 

0 Kudos
5 Replies
thejuskambi
Occasional Contributor III

If you Layer is of type ArcGISDynamicMapServiceLayer, then it has a property layerInfos. which is array of sublayers(LayerInfo)

you can loop through this array and identify the layerid and use it to build the url.

Hope this was helpful.

RiverTaig1
Occasional Contributor

Thank you for your response. I think I tried to implement what you were suggesting below.  Could you have a look?  One thing that bothers me is having to use the URL.  You see, the queryFeatures method, according to the documentation, will try to use a client side request if it can...will it still do that if I give it a URL??  I mean, how could it since this is a new FeatureLayer constructed from a URL.  It really seems that I need the maps FeatureLayer to make a query that would work on the client only.

        function FindFeaturesInCurrentExtent(outFields, layerToQuery) {

            //var outFields = "FacilityID";

            //var layerToQuery = "Transformer";

            var esriMap = this.app.map; //a reference the esri.Map object

            for (var i = 0; i < esriMap.layerIds.length; i++) {

                var ly = esriMap.getLayer(i.toString());

                if (ly instanceof esri.layers.ArcGISDynamicMapServiceLayer) {

                    var adms = ly;

                    for (var j = 0; j < adms.layerInfos.length; j++) {

                        var li = adms.layerInfos;

                        if (li.name == layerToQuery) {

                            var url = adms.url + "/" + li.id;

                            var fl = new esri.layers.FeatureLayer(url, {

                                outFields: [outFields]

                            });

                            var ext = this.app.map.extent;

                            var query = new esri.tasks.Query();

                            query.geometry = ext;

                            //Ideally this will be a client-side request

                            fl.queryFeatures(query, function (response) {

                                alert(response.features.length.toString()); //Correctly returns the number of features

                            });

                        }

                    }

                }

            }

        };

0 Kudos
thejuskambi
Occasional Contributor III

I might have misunderstood what you were trying to do. The FeatureLayer has to be added to the map first, if you want to use it. Otherwise it will not contain any features or data to be able to query. Depending on the MODE set for the FeatureLayer, records are retrieved from the server and stored locally. which is then available for querying.

As per the documentation for ArcGISDynamicMapServiceLayer

Allows you to work with a dynamic map service resource exposed by the ArcGIS Server REST API. A dynamic map service generates images on the fly. For cached map services, see ArcGISTiledMapServiceLayer.

The data does not exist for Such layers to be able to queried on the client. If the Layer has not been added. The best option is to user queryTask.

Is there any particular reason you wouldnt want to get the result from the server?

0 Kudos
RiverTaig1
Occasional Contributor

Ultimately, I'm developing an application that will go offline which is why I am so interested in querying taking place on the client.  I'm developing on top of Latitude Geographic's HTML 5 Viewer ("Geocortex") which has all the necessary infrastructure to work in an offline mode.  Unfortunately, their viewer takes care of adding layers to the map using minified JavaScript, so it isn't readily apparent where the FeatureLayer I want to query is added to the map. I suppose if I knew where that was, I might have some success in accessing it at that point.  I would have thought I could have somehow gotten it from the map itself and then called queryFeatures on it.  Or perhaps hydrate a LayerInfo object into a full-fledged FeatureLayer. 

0 Kudos
thejuskambi
Occasional Contributor III

From what I can remember, Geocortex essentials is a configurable viewer. You should be able to look at the configuration and determine which layers has been added.

If a layer has been added the it should be available in map.layerids or map.graphicsLayerIds. As long as you know their ids you should be able to get access to them.

Check with their support, what kind of offline map features are available in the application.