How do I obtain attributes of polygons that intersect another polygon?

1992
7
Jump to solution
06-19-2017 09:05 AM
HarlanMarshall
New Contributor III

I need to get all polygons from a given layer that intersect with a given polygon. In addition to the intersection geometry, I need to know which polygons (IDs) produce those intersections and some other attributes from the intersecting polygons. The geometryEngine.intersect() returns the intersection geometries only. What's the best way to get the attributes along with the geometries?

Thanks to anyone who can help or give me a sample.

0 Kudos
1 Solution

Accepted Solutions
ThomasSolow
Regular Contributor

Okay, you should be able to manually query the feature service for graphics (which will include attributes).

I would load all features when the page loads and then query against those features based on the user input later.  Revised sample:

// do this on page load
let featureLayer = new FeatureLayer({
  url: <FeatureLayer URL>,
  outFields: ["*"]
});

let allPolygons = [];
       
featureLayer.load().then(fl => {
  featureLayer.queryFeatures().then(results => {
    allPolygons = results.features;
  });
});‍‍‍‍‍‍‍‍‍‍‍

// call this when user submits polygon to query against all other
// polygons

function findPolygons(searchPolygon, graphics){
  // if there's no graphics then there are no intersections
  if (graphics.length === 0) return [];

  // test each of them using geometryEngine.intersects against the search polygon
  return graphics.filter(graphic => geometryEngine.intersects(graphic.geometry, searchPolygon));
}‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

I should mention that if the graphics returned by the manual query don't have attributes, you may have to pass in a Query to queryFeatures.  The query needs a .where of "1=1" and an outFields of "*."  Let me know if you run into difficulties with that.

View solution in original post

7 Replies
ThomasSolow
Regular Contributor

Are you using the 4 or 3 API?

Here's a function you could use in the 4 API:

function findPolygons(searchPolygon, layer, view){
  // get layerView, this contains the client-side graphics
  let layerView = view.allLayerViews.find(lv => lv.layer === layer);
  // if there's no layerview then there are no intersections
  if (!layerView) return [];

  // get all features in the client in the specified layer
  // test each of them using geometryEngine.intersects against the search polygon
  return layerView.loadedGraphics.filter(graphic => geometryEngine.intersects(graphic, searchPolygon)).toArray();
}‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

This will return an array of graphics, which will have the attributes.  It should be somewhat similar for 3.X, you'll also want to use the geometryEngine.intersects method.  There's not concept of a layerView in 3.X though, so you'll access the graphics in the client differently.

HarlanMarshall
New Contributor III

I'm using API 4.3. You're answer is helpful but I should have mentioned that there is no client map involved. Without a map there is no layerView, graphics, etc.

I'm creating a script that will intersect a land parcel geometry with an array of all polygon geometries in another feature class for the purpose of determining information about the intersecting polygons (attributes) and percentages of the parcel area that are intersected by each polygon. The user searches for a parcel, then chooses a polygon feature class to overlay with and receives tabular data about the polygons intersecting the parcel.

Thanks for your help.

0 Kudos
ThomasSolow
Regular Contributor

Okay, you should be able to manually query the feature service for graphics (which will include attributes).

I would load all features when the page loads and then query against those features based on the user input later.  Revised sample:

// do this on page load
let featureLayer = new FeatureLayer({
  url: <FeatureLayer URL>,
  outFields: ["*"]
});

let allPolygons = [];
       
featureLayer.load().then(fl => {
  featureLayer.queryFeatures().then(results => {
    allPolygons = results.features;
  });
});‍‍‍‍‍‍‍‍‍‍‍

// call this when user submits polygon to query against all other
// polygons

function findPolygons(searchPolygon, graphics){
  // if there's no graphics then there are no intersections
  if (graphics.length === 0) return [];

  // test each of them using geometryEngine.intersects against the search polygon
  return graphics.filter(graphic => geometryEngine.intersects(graphic.geometry, searchPolygon));
}‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

I should mention that if the graphics returned by the manual query don't have attributes, you may have to pass in a Query to queryFeatures.  The query needs a .where of "1=1" and an outFields of "*."  Let me know if you run into difficulties with that.

View solution in original post

HarlanMarshall
New Contributor III

This works. Thank you!

Since IE doesn't support the "=>" function syntax, I'm trying to replace the selection of intersecting feature code with older javascript syntax ( foo => {function code}   replace with   function(foo) {function code}) but it returns no intersections with the older syntax. Can you tell what's wrong with my change below?

// intersect overlay polygons
featureLayer.load().then(function(fl) {
     featureLayer.queryFeatures().then(function (results) {
          let allPolygons = results.features;
          // This does not work
          gIntersects = allPolygons.filter(function (graphic) { geometryEngine.intersects(graphic.geometry, parcelGeometry) });
          // This works
          gIntersects = allPolygons.filter(graphic => geometryEngine.intersects(graphic.geometry, parcelGeometry));
     });
});
0 Kudos
ThomasSolow
Regular Contributor

Try adding a return to the function body, like:

gIntersects = allPolygons.filter(function (graphic) { return geometryEngine.intersects(graphic.geometry, parcelGeometry) });

ES6 has the concept of implicit returns if you keep your function on one line without curly braces.

HarlanMarshall
New Contributor III

You’re correct. I had just figured that out and was ready to reply.

It works great. Thanks for your fast response and great answers.

Harlan Marshall

GIS Analyst – Senior

Pima County

(520) 724-6757

0 Kudos
AJRS
by
New Contributor

I came across this thread and I am trying to do the exact same thing in 3.23.  Unfortunately I have limited JavaScript skills so I am having some difficulty making this work.  What might this look like in 3.23?  Any help would be very much appreciated

Thank you!!!

0 Kudos