Select to view content in your preferred language

JavaScript Spatial Query for Multiple Layer Types

5326
4
01-26-2015 08:17 AM
JustinShepard
Deactivated User

I'm contemplating how to set up my function so that it accepts a geometry (the user will be able to use the draw toolbar to create the search geometry) and will 'query' any layer in the map that is visible.

* All results will get written to a central graphics array so that I can put the results in a side panel, display 'selected' graphics on the map, and highlight the current attributes being viewed in the side panel.

* The map uses different layer types that all need to be queried at once and the results managed together: ArcGISDynamicMapServiceLayer, FeatureLayer, CSVLayer (from local user file), GraphicsLayer (build on demand from JSON string)

* Search geometry could be a point, line, or polygon

* Map layers could contain point, line, or polygon features/graphics

  1. Loop through all of the layers in the map to determine the type of layer
    1. Using layerIds, graphicsLayerIds, and basemapLayerIds gets me part of the way there
    2. I also test if the 'capabilities' property is present and if it supports Query or Identify
  2. If the layer ID is in layerIds and Identify is supported then create and run an IdentifyTask
  3. If the layer ID is in graphicsLayerIds and Query is supported then I'm thinking it would be a feature layer and I could use featureLayer.selectFeatures
  4. Anything else in the graphicsLayerIds I would test if graphics > 0
    1. I was considering using the relation geometry service but I've seen a lot comments that people abandoned this type of approach because of asynchronous timing issues and not being able to trace back to the original layer(s) to get the full graphic result
    2. I also considered creating 'local' feature layer, one that is built from a FeatureCollection and doesn't come from ArcGIS Server, but when you do that it doesn't seem to support the Query capability

Suggestions on any of this would be appreciated.

0 Kudos
4 Replies
JoshHevenor
Frequent Contributor
OwenEarley
Frequent Contributor

You have a couple of issues to resolve to get this to work:

1. Creating the selection geometry:

This is the easy bit - just use the draw toolbar.

2. Query visible map layers:

This is where things start to get tricky. There is no way you can query them all at once. Your function will need to use a DeferedList to track when all queryTasks are completed. This post has some great information: How to use dojo.Deferred, dojo.DeferredList, and .then. When all results are returned then you can add them to your central graphics array.

Note that you will also need to develop slightly different query tasks depending on the layer type and selection geometry.

0 Kudos
JustinShepard
Deactivated User

I've got the selection geometry working and am testing if the layer has supports Query capability (this gets me results for Dynamic and Feature layers). I'm hung up on how to handle graphics and csv layers though. Any suggestions? I was thinking that I'd have to find the extent of the search geometry and then run something like

var filteredGraphics = dojo.filter(featureLayer.graphics, function (graphic) {

                        return searchExtent.contains(graphic.geometry);

                    });

0 Kudos
OwenEarley
Frequent Contributor

For graphics layers you have the right approach using the the extent.contains() method, or the extent.intersects() method. This sample shows using the draw toolbar to capture the selection extent and then uses the extent.contains() method to return points in the extent.

You should be able to use the same approach with a CSVLayer by looping through the CSVLayer.graphics array and using the extent.contains() method.

0 Kudos