Hi guys,
I have four layers ( names: csvLayer, csvLayer1, csvLayer2, csvLayer3) on my map which have same fields and i have custom html table where i add features selected on map using polygon, but i can add features only from one layer, which is selected in code. In this code i can path one layer, but how can i path all four layers to select features from them? Here is code i use for selecting fatures:
let features = [];
let csvLayerView;
csvLayer.when(() => {
view.whenLayerView(csvLayer).then(function (layerView) {
csvLayerView = layerView;
});
})
.catch(errorCallback);
// polygonGraphicsLayer will be used by the sketchviewmodel
// show the polygon being drawn on the view
const polygonGraphicsLayer = new GraphicsLayer();
map.add(polygonGraphicsLayer);
// add the select by rectangle button the view
//view.ui.add("select-by-rectangle", "top-left");
const selectButton = document.getElementById("select-by-rectangle");
// click event for the select by rectangle button
selectButton.addEventListener("click", () => {
view.popup.close();
sketchViewModel.create("polygon");
});
// add the clear selection button the view
//view.ui.add("clear-selection", "top-left");
//document.getElementById("clear-selection").addEventListener("click", () => {
//featureTable.clearSelection();
//featureTable.filterGeometry = null;
//polygonGraphicsLayer.removeAll();
//});
// create a new sketch view model set its layer
const sketchViewModel = new SketchViewModel({
view: view,
layer: polygonGraphicsLayer
});
// Once user is done drawing a rectangle on the map
// use the rectangle to select features on the map and table
sketchViewModel.on("create", async (event) => {
if (event.state === "complete") {
// this polygon will be used to query features that intersect it
const geometries = polygonGraphicsLayer.graphics.map(function(graphic){
return graphic.geometry
});
const queryGeometry = await geometryEngineAsync.union(geometries.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) {
if (csvLayerView) {
// create a query and set its geometry parameter to the
// rectangle that was drawn on the view
const query = {
geometry: geometry,
outFields: ["*"]
};
// query graphics from the csv layer view. Geometry set for the query
// can be polygon for point features and only intersecting geometries are returned
csvLayerView.queryFeatures(query)
.then((results) => {
//const raro = [];
if (results.features.length === 0) {
clearSelection();
} else {
// მონიშნული ობიექტებისთვის მონაცემების წამოღება და ცხრილში ასახვა
results.features.forEach((feature)=>{
var tablemili = document.getElementById("tablemili");
newRow = tablemili.insertRow(tablemili.length),
cell1 = newRow.insertCell(0),
cell2 = newRow.insertCell(1),
cell3 = newRow.insertCell(2),
cell4 = newRow.insertCell(3),
cell5 = newRow.insertCell(4),
cell1.innerHTML = feature.attributes["milis_ID"];
cell2.innerHTML = feature.attributes["diametri"];
cell3.innerHTML = feature.attributes["dasaxeleba"];
cell4.innerHTML = feature.attributes["masala"];
cell5.innerHTML = feature.attributes["qselis_kategoria"];
});
polygonGraphicsLayer.removeAll();
}
})
.catch(errorCallback);
}
}
function errorCallback(error) {
console.log("error happened:", error.message);
}
Solved! Go to Solution.
Sorry if I'm misunderstanding, but can you loop over all your layers to do the query on line 57 in selectFeatures()?
map.allLayers.forEach(layer => {
// query graphics from the csv layer view. Geometry set for the query
// can be polygon for point features and only intersecting geometries are returned
layer.queryFeatures(query)
.then((results) => {
// do something with query results
})
.catch(errorCallback);
});
Sorry if I'm misunderstanding, but can you loop over all your layers to do the query on line 57 in selectFeatures()?
map.allLayers.forEach(layer => {
// query graphics from the csv layer view. Geometry set for the query
// can be polygon for point features and only intersecting geometries are returned
layer.queryFeatures(query)
.then((results) => {
// do something with query results
})
.catch(errorCallback);
});
Thanks, but it does not work form me. I took another way to solve my issue. Thanks again.
Glad you got it figured out, @Vakhtang_Zubiashvili. Can you post the solution to help other people with a similar problem?