Is there a way to test whether a point created with the Sketch or Editor widgets has been placed as a snapped point to a particular feature? The feature and vertex highlighting are very useful, but I'm looking for a way to enforce snapping to a particular feature and notify the user if a point wasn't placed as a snapped point.
Solved! Go to Solution.
You could use various methods of the geometryEngine to validate that geometries intersect or that a vertex matches a point
https://developers.arcgis.com/javascript/latest/api-reference/esri-geometry-geometryEngine.html
It might require breaking up polygons -> polylines -> points, but should be doable.
You could use various methods of the geometryEngine to validate that geometries intersect or that a vertex matches a point
https://developers.arcgis.com/javascript/latest/api-reference/esri-geometry-geometryEngine.html
It might require breaking up polygons -> polylines -> points, but should be doable.
Thank you for the guidance. This case required validating snapping a point to a line, so the steps that were implemented were:
The code for this is as follows. It watches the Sketch for "on creation complete", and passes the geometry to a function that does the spatial query and tests the intersection of any features returned.
sketch.on("create", function (event) {
if (event.state === "complete") {
let pointBuffer = geometryEngine.buffer(event.graphic.geometry, 1);
queryFeaturelayer(pointBuffer);
}
});
function queryFeaturelayer(geometry) {
const featureQuery = {
spatialRelationship: "intersects",
geometry: geometry,
outFields: ['*'],
returnGeometry: true
};
featureLayer.queryFeatures(featureQuery)
.then((results) => {
if ( results.features.length === 0 ) {
alert("Point not snapped");
}
else {
let snapped = false;
results.features.forEach(feature => {
if (geometryEngine.intersects(geometry.centroid, feature.geometry)){
snapped = true;
}
});
if ( snapped ) {
alert("Point snapped");
} else {
alert("Point not snapped");
}
}
}).catch((error) => {
console.log(error);
});
}