Select to view content in your preferred language

Select Features by Graphic

232
6
3 weeks ago
Paco
by
Occasional Contributor II

Hello!

I seem to be having issues selecting Features when drawing a Graphic?   I have a button click that adds a rectangle GraphicsLayer from the SketchViewModel,  but not sure how to get the Geometry of the rectangle to select or query the Features that intersect?

The online examples seem overly complex just for a Geometry intersection?

here is the graphics section that works-

  const graphicsrectangleselectLayer = new GraphicsLayer({
    id: "tempGraphics"
  });
  var drawRectangle = document.getElementById("rectangleselectButton");
  drawRectangle.onclick = function () {
    sketchViewModelrectangle.create("rectangle"); //polygon
  };
  const sketchViewModelrectangle = new SketchViewModel({
    view: view,
    layer: graphicsrectangleselectLayer,
  });
 
Just need the Geometry/Query/Intersect part?   any help would be appreciated.
0 Kudos
6 Replies
JamesIng
Occasional Contributor

You'll need to grab the geometry from the graphic that exists on the graphics layer (tempGraphics) and then you can use that geometry to do your intersect query against a set of features.

This sample illustrates it relatively well:
https://developers.arcgis.com/javascript/latest/sample-code/sandbox/?sample=sketch-update-validation

In this sample they do it on update of sketch - but you can tie it into whatever fits your situation.

The relevant part is:

 const graphic = event.graphics[0];
          // check if the graphic is intersecting school buffers or is
          // still contained by the boundary polygon as the graphic is being updated
          intersects = geometryEngine.intersects(buffers, graphic.geometry);
          contains = geometryEngine.contains(boundaryPolygon, graphic.geometry);


If you want to intersect against a featureLayer you can instead use the graphic.geometry and then do a layer query supplying that geometry as a parameter.

https://developers.arcgis.com/javascript/latest/sample-code/sandbox/?sample=featurelayer-query-basic

James from www.landkind.com
0 Kudos
UndralBatsukh
Esri Regular Contributor
0 Kudos
KenBuja
MVP Esteemed Contributor

Note that that sample isn't working correctly. Draw a rectangle to select features.

Snag_6b4b97.png

Clear the selection and draw another rectangle. Both the features in the current rectangle and the previously selected features are highlighted.

Snag_6bb638.png

0 Kudos
UndralBatsukh
Esri Regular Contributor

Hi there, 

Oh!!! Thank you for reporting the issue! 

 

highlightIds and features variables are set as arrays at the top of the app so they keep getting features added to them. It needs to be instantiated in the callback so it's clean. I updated the sample and here is a codepen that works - https://codepen.io/U_B_U/pen/poXdBMx?editors=100

The sample should be updated on the SDK page next week. 

 

0 Kudos
KenBuja
MVP Esteemed Contributor

What's happening with the Total and Selection counts if you select multiple rectangles without clearing the selection? I've selected just single parks with two rectangles, but the selection says 3.

Snag_dd028d.png

Snag_dd2cba.png

0 Kudos
UndralBatsukh
Esri Regular Contributor

codepen updated

0 Kudos