Select to view content in your preferred language

Query Dynamic Map Service Layers by Current Map Extent

3296
2
Jump to solution
08-13-2015 07:58 AM
RichardHughes
Occasional Contributor

In order to request a feature service from the server I need to know what layers from the dynamic map service are visible in the current map extent.  For the life of me I cannot find a method in the api for either extent, map, feature layer, graphicUtil, or arcgisdynamicmapservicelayer that will do this. 

We want to maintain the unique symbology of our map service while having the feature access to take onto the device. 

The question that this raises is "Is it possible to perform analysis on feature service geometries without adding the feaure services to the map?" 

0 Kudos
1 Solution

Accepted Solutions
JakeSkinner
Esri Esteemed Contributor

Hi Richard,

One way you can determine what layers are visible is by querying the layers to see if they intersect the current extent using the Query and QueryTask classes.  Here is a fiddle example.  The number of features for each layer are reported to the browser console when the extent changes.

View solution in original post

0 Kudos
2 Replies
JakeSkinner
Esri Esteemed Contributor

Hi Richard,

One way you can determine what layers are visible is by querying the layers to see if they intersect the current extent using the Query and QueryTask classes.  Here is a fiddle example.  The number of features for each layer are reported to the browser console when the extent changes.

0 Kudos
RichardHughes
Occasional Contributor

Hi Jake,

What I did in this code is use the queryIds method on a feature layer with the query.geometry = map.extent.  Then set a definition expression on the layer using the returned OIDs.  I feed the layer with expression into an array.  When all 33 layers are queried I add the array to the map.  This is fast, but there are some odd symbology and attribute issues that I am finding.

P.s> Here is the widget's method / Could you offer any criticism please?

 startFeatureDownload: function(param, callback) {
                var downloadTiles = dom.byId('downloadTiles');
                var downloadFeatures = dom.byId('downloadFeatures');
                var clearButton = dom.byId('clearButton');
                var buttons = [downloadTiles, downloadFeatures, clearButton];


                var map = this.map;
                var featureUrls = this.testUrls;


                offlineWidget.clearMap(null, function(evt) {
                    
                    var extent = map.extent;
                    var i = [];
                    var featureLayers = [];
                    var index = 0;
                    arrayUtils.forEach(featureUrls, function(item) {
                        var request = new esriRequest({
                            url: item,
                            content: {f: "json"},
                            handleAs: "json",
                            callbackParamName: "callback"
                        });
                        request.then(function(response) {
                         if (response.type === "Feature Layer") {
                            index += 1;
                            var drawingInfo = response.drawingInfo;
                            var geometryType = response.geometryType;
                            var displayField = response.displayField;
                            var typeIdField = response.typeIdField;
                            var types = response.types;
                            var fields = response.fields;
                            var id = response.id;
                            var name = response.name;
                          
                            // var queryTask = new QueryTask(item);
                            var query = new Query();
                            query.returnGeometry = false;
                            //query.outFields = ["*"];
                            query.geometry = extent;
                            query.spatialRelationship = Query.SPATIAL_REL_INTERSECT;
                            // create the field info array for the feature layer
                            var fieldinfo = [];
                            var count;
                            for (count=0; count < fields.length; count ++) {
                                
                                var f = fields.shift();
                                var entry = {
                                    fieldName: f.name,
                                    label: f.alias,
                                    visible: true
                                };


                                fieldinfo.push(entry);
                            }


                            var popupTemplate = new PopupTemplate({
                                title: response.name,
                                fieldInfos: fieldinfo
                            });


                             var layer = new FeatureLayer(item, {
                                mode: FeatureLayer.MODE_SNAPSHOT,
                                infoTemplate: popupTemplate,
                                outFields: ["*"]
                            });
                            
                            var queryLayer = function(layer) {
                                layer.queryIds(query, function(result) {
                                console.log(result);
                                if (result !== null) {
                                    i.push(1);
                                    layer.setDefinitionExpression("OBJECTID IN (" + result.join(',') + ")");
                                    featureLayers.push(layer);
                                } else {
                                    i.push(0);
                                }


                                var sum = function add(i) {
                                    var cnt = 0;
                                    arrayUtils.forEach(i, function(e) {
                                        cnt += e;
                                    });
                                    return cnt;
                                };
                                var x = sum(i);


                                if (i.length === index) {
                                    var newlayerlist = [];
                                    if (featureLayers.length > 0 && featureLayers.length === x) {
                                        var layerholder = {
                                            polys: {},
                                            lines: {},
                                            points:{}
                                        };
                                        arrayUtils.forEach(featureLayers, function(lyr) {
                                            var layerid = lyr.layerId;
                                            var geo = lyr.geometryType;
                                            switch (geo) {
                                                case "esriGeometryPolygon":
                                                    layerholder.polys[layerid] = lyr;
                                                    break;
                                                case "esriGeometryPolyline":
                                                    layerholder.lines[layerid] = lyr;
                                                    break;
                                                case "esriGeometryPoint":
                                                    layerholder.points[layerid] = lyr;
                                                    break;
                                            }
                                        });


                                        var lists = [layerholder.polys, layerholder.lines, layerholder.points];
                                        
                                        var newlists = [[], [], []];
                                        arrayUtils.forEach(lists, function(list) {
                                            var keys = Object.keys(list);
                                            keys.sort();
                                            arrayUtils.forEach(keys, function(key) {
                                                var item = lists.indexOf(list);
                                                newlists[item].push(list[key]);
                                            });
                                        });
                                        var finalLayerList = newlists[0].concat(newlists[1], newlists[2]);
                                  


                                        var _maplisten = map.on('layers-add-result', function(evt) {
                                                _maplisten.remove();
                                                offlineWidget.initOfflineDatabase(finalLayerList);
                                        });
                                        map.addLayers(finalLayerList);
                                    }
                                }
                            });
                           };


                           queryLayer(layer);
                       }
                    });
                });
             });
            },
0 Kudos