Dear ESRI,
we built an application in Javascript where we are selecting a bunch of points on the ESRI map and retrieving selected points through the intersection with the graphic layer in the following way:
sketchViewModel.on("create", (event) => {
if (event.state === "complete") {
const extent = event.graphic.geometry.extent;
const intersection = graphicsLayer.graphics.filter((graphic) =>
extent.intersects(graphic.geometry)
);
console.log("Items", intersection._items)
graphicsLayer.remove(event.graphic);
that.fireEvent("select", { event: intersection._items });
}
})
However, we noticed a strange behavior. We want to select all the points on a given street. If we proceed by selecting small blocks very close to the points, the process works fine, but if we select all the points together the intersection seems to take more points than requested, probably involving also the nearest points.
Look at this case. First we select 3 points
and all seems fine:
Three points are selected correctly. In the console you see 4 because one is always junk and not used.
Then we take 4 other points on the same street:
Even in this case the result is correct:
(One point is always junk and not used)
Finally, we select the remaining 2 points
and in this case too, all is correct.
So, in total, I've selected 9 points (consider that for each selection one is always dummy)
But, if select all the points together using a segmented line
it brings me more than the ones I selected
How can I fix this strange behavior? Why it's not so accurate when I extract the selected points?
Thanks
Others are far more proficient in 4.x than I am but could it be that you're using event.graphic.geometry.extent which might be treated as a bounding box for the graphic rather than it's actual shape?
In the selections that work, you'll notice that there aren't other points located within the square of your selection but if you look at your polygon selection, and use the xmin,ymin.xmax,ymax rectangle of the polygon, you'll see that many other points would be selected based on that rectangular shape.
Hi Steve, thanks for your answer!
However, I'm not sure this is the real cause of the problem. Indeed, if I rotate a little bit the map and use only the rectangular selection, I have the same issue. Look at this new example.
I have 9 points here on the same street with a shorter selection:
But as soon as I extend the selection on the right, without including other points, the number of the points returned is greater:
Very strange behaviour. I hope that someone can help me on this.
Andrea
Andrea,
if you rotated the map then the extent might not be what you think it is. As mentioned by Steve, you should not use the extent as it will not provide the correct intersection results since the geometry and the extent are different. Maybe something like the code below might help. Also, its hard to tell exactly what might be going on, so something that will help is if you have a CodePen or similar so we can experience the issue and help you out.
const searchGeometry = event.graphic.geometry;
const intersections = graphicsLayer.graphics.filter((graphic) =>
geometryEngine.intersects(searchGeometry,graphic.geometry)
);