Attack the Polygon

1040
0
03-18-2015 08:24 AM
Labels (1)
ReneRubalcava
Frequent Contributor
0 0 1,040

chess_globe_flag.jpg

Every now and then a question comes up in the forum about how to do selections of geometries or graphics after they've run some sort of analysis, such as a buffer or drive-time. It makes sense, you've got this nice new polygon or something added to your map after you ran your fancy-schmancy analysis and you want to make it mean something.

The thing to remember is that the results from your analysis are just more geometries and there are tools in the API to deal with it.

Polygon Circus

Let's look at a common one, the Polygon. A Polygon has some methods built it into it that let you extract geometries so you can do something with them. There is the getCentroid method, which uses the maths here. If my memory serves me correctly, you could get a point returned from outside the polygon if the polygon is some odd u-shape. I'm sure someone will correct me if I'm wrong. There is also the getExtent method, which you may have used to set the extent of the map to a polygon, this just returns a rectangular extent of the polygon. A more interesting one is the contains method. It makes sense to have this on the Polygon as it's a quick way to check if your point is inside the polygon. You could iterate over a list of geometries and check them like polygon.contains(point). Simple enough. There are lots of other interesting methods you may find useful in the Polygon module.

Just select the things

A lot of times, if i know I have a layer that will provide some sort of value to my map after I perform an analysis, I'll add it as a FeatureLayer so that I can use the selectFeatures method when I need it. So let's say you do a viewshed analysis in your map and you want to see what points, such as parcels or schools are within that viewshed. You can run your analysis, grab the geometries from the result of the analysis and select the features from the layer to display them on the map. Again, those selected features are just geometries, so you can use those geometries to do further analyses such as find the nearest hospitals or fire stations, whatever floats your boat. You could do something like this after doing a viewshed analysis.

var analysisTool = new CreateViewshed(params, "toolPane");
analysisTool.startup();
analysisTool.on("job-result", function(result) {
  analysisTool.set("disableRunAnalysis", false);
  var resultLayer = new FeatureLayer(result.value.url || result.value, {
    outFields: ['*'],
    infoTemplate: new InfoTemplate()
  });
  map.addLayer(resultLayer);
  if (result.value.featureSet) {
    // get features from the result
    var features = result.value.featureSet.features;
    // only need the geometries, not full graphic
    var geometries = features.map(function(x) {
      return x.geometry;
    });
    // union geometries with geometryEngine
    var geometry = geometryEngine.union(geometries);
    var query = new Query();
    query.geometry = geometry;
    // select the things
    census.selectFeatures(query, FeatureLayer.SELECTION_NEW);
  }
});

* This sample is available on jsbin, modified from an esri example. Requires an ArcGIS Online/Developers login for analysis tools.

That's not too difficult to do. Notice the use of the geometryEngine in there. This is still in beta, but it's in 3.13 and I'm just starting to mess around with it. In the case above, you don't really need it, but it comes in handy when you have multiple geometries of the same type and you just need to mash them together to further analysis, such as a selection. If you were not using a FeatureLayer and only dealing with graphics on the map, the geometryEngine would be the go-to tool to do stuff like this. As I use it more, I'll post more about it.

So give it a shot, there are a lot of tools and options available to you when you need to make sense of your geometries in your application. Don't be afraid to experiment and see what you can break.

For more geodev tips and tricks, check out my blog.

About the Author
Softwhere Developer at Esri working on cool stuff! Author: Introducing ArcGIS API 4 for JavaScript: Turn Awesome Maps into Awesome Apps https://amzn.to/2qwihrV ArcGIS Web Development - https://amzn.to/2EIxTOp Born and raised in East L.A.