Select to view content in your preferred language

Snapping Enforcement/Confirmation 4.19

769
2
Jump to solution
05-04-2021 05:03 AM
FrederickPowers
Emerging Contributor

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.

Tags (3)
0 Kudos
1 Solution

Accepted Solutions
ReneRubalcava
Honored Contributor

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.

View solution in original post

0 Kudos
2 Replies
ReneRubalcava
Honored Contributor

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.

0 Kudos
FrederickPowers
Emerging Contributor

Thank you for the guidance.  This case required validating snapping a point to a line, so the steps that were implemented were:

  1. Do a spatial query on the feature layer with a buffer from the graphic geometry.
  2. If there are no geometries returned the point wasn't snapped.
  3. If at least one geometry was returned, test the graphic geometry for intersection for each feature.
  4. If one of the features returned intersects the graphic geometry the point was snapped.

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);
    });
}

 

0 Kudos