Select multiple layers and export

617
3
Jump to solution
12-23-2022 01:55 AM
Vakhtang_Zubiashvili
Occasional Contributor III

Hi Guys, 

On my map i want to select multiple layers by drawing rectangle and than export selected features fields. i have this code thanks to @BlakeTerhune   (From my previous topic😞

 

 

// create a new sketch view model set its layer
        const sketchViewModell = new SketchViewModel({
          view: view,
          layer: polygonGraphicsLayerr
        });
        // Once user is done drawing a rectangle on the map
        // use the rectangle to select features on the map and table
        sketchViewModell.on("create", async (event) => {
          if (event.state === "complete") {
            // this polygon will be used to query features that intersect it
            const geometriess = polygonGraphicsLayerr.graphics.map(function(graphic){
              return graphic.geometry
            });
            const queryGeometry = await geometryEngineAsync.union(geometriess.toArray());
            selectFeatures(queryGeometry);
          }
        });
        // This function is called when user completes drawing a rectangle
        // on the map. Use the rectangle to select features in the layer and table
        function selectFeatures(geometry) {
            // create a query and set its geometry parameter to the
            // rectangle that was drawn on the view
            var query = {
              geometry: geometry,
              outFields: ["*"]
            };
            
            map.allLayers.forEach(layer => {
            // Query all layers data
                layer.queryFeatures(query)
                .then((results) => {
                    // do something with query results
                  console.log(results)
                })
                .catch(errorCallback);
            });
        };

 

 

 

When i draw rectangle in logs i get error:  layer.queryFeatures is not a function;

what i do wrong? What is wrong here and i can not see? Help

0 Kudos
1 Solution

Accepted Solutions
JoelBennett
MVP Regular Contributor

Your code is assuming that all layers in your map are FeatureLayers, but that is unlikely.  You should therefore check the layer type before trying to execute a query on it.  One simple check would be like so:

map.allLayers.forEach(layer => {
	// Query all layers data
	if (typeof layer.queryFeatures == "function") {
		layer.queryFeatures(query).then((results) => {
			// do something with query results
			console.log(results)
		}).catch(errorCallback);
	}
});

 

View solution in original post

3 Replies
JoelBennett
MVP Regular Contributor

Your code is assuming that all layers in your map are FeatureLayers, but that is unlikely.  You should therefore check the layer type before trying to execute a query on it.  One simple check would be like so:

map.allLayers.forEach(layer => {
	// Query all layers data
	if (typeof layer.queryFeatures == "function") {
		layer.queryFeatures(query).then((results) => {
			// do something with query results
			console.log(results)
		}).catch(errorCallback);
	}
});

 

LefterisKoumis
Occasional Contributor III

JoelBennett is correct.

map.allLayers.forEach(layer => {
            // Query all layers data
			if (layer.type === "feature" && layer.visible){
                layer.queryFeatures(query)
                .then((results) => {
                    // do something with query results
                  console.log(results)
                })
                .catch(errorCallback);
				}
            });
Vakhtang_Zubiashvili
Occasional Contributor III

Thank You guys @JoelBennett @LefterisKoumis , both code works fine, simply checking layer type 😉 Thanks again for help.

0 Kudos