I am trying to query, by geometry, a FeatureLayer. The Feature layer is a "client-side" Feature layer-- its created from a bunch of graphics added to its `source` property. When I call the `queryFeatures` or `queryObjectIds` method to get features within an overlapping extent or shape I never get any results back.
Here is a codepen demonstrating the problem. The below code snippet shows my approach:
let graphics = [
new Graphic({
attributes: {
ogc_fid: 1,
val: "one"
},
geometry: new Point({
latitude: 44.99,
longitude: -68.99,
spatialReference: {latestWkid: 4326, wkid: 4326}
})
}),
new Graphic({
attributes: {
ogc_fid: 2,
val: "two"
},
geometry: new Point({
latitude: 44.98,
longitude: -68.98,
spatialReference: {latestWkid: 4326, wkid: 4326}
})
}),
]
let myLayer = new FeatureLayer({
source: graphics,
objectIdField: "ogc_fid",
fields: [{
name: "ogc_fid",
type: "oid"
}, {
name: "val",
type: "string"
}],
renderer: {
symbol: {
color: "red",
size: 6,
type: "simple-marker",
},
// overrides the layer's default renderer
type: "simple",
}
});
// .....
// try to query client-side featureLayer by an extent:
let extent = new Extent({
spatialReference: { wkid: 4326},
xmax: 45.5,
xmin: 44.7,
ymax: -68.5,
ymin: -69.5
})
myLayer.queryObjectIds({
geometry: extent,
spatialRelationship: "intersects",
//there are no features returned
}).then(result => console.log(result))
The above will return no results. I can get results via an attribute query but geometry-ones all return nothing. Can anyone spot what I am doing wrong?
I've looked at other posts like this one where Robert's approach looks about the same as mine, besides him creating a special Query object. I've been able to get similar to above to work on a FeatureLayer based on a featureService (aka: url property).
As shown in the CodePen, I've also tried running the queries on the FeatureLayerView. Interestingly, no results return there under any circumstance which is also unexpected.. bonus points if you can explain that.
Solved! Go to Solution.
Hi there,
Several things going on in your codepen.
First, your polygon geometry does not intersect with the points. You have the x and ys reversed. If you update that then the layer.queryFeatures will return the correct results. However the layerView will still return empty array because you are running the layerView query when the app loads. In this case you have to wait until the layerView is done updating.
I updated your codepen to reflect the changes I mentioned above: https://codepen.io/U_B_U/pen/GRXrKJq?editors=0011
Hi there,
Several things going on in your codepen.
First, your polygon geometry does not intersect with the points. You have the x and ys reversed. If you update that then the layer.queryFeatures will return the correct results. However the layerView will still return empty array because you are running the layerView query when the app loads. In this case you have to wait until the layerView is done updating.
I updated your codepen to reflect the changes I mentioned above: https://codepen.io/U_B_U/pen/GRXrKJq?editors=0011
Thank you @UndralBatsukh for the prompt response. I am embarrassed that I missed the issue with the extent's coordinates. Thank you for catching that obvious error. I was also not aware (or had forgotten) that a layerView's queryFeatures request will only work after the features are completely drawn in the map and its own state is not set to 'updating'.
No worries at all. Happens to all of us.