counting points that intersect inside a polygon feature

1790
8
Jump to solution
06-12-2019 07:52 AM
deleted-user-wcpelUUf_XXx
New Contributor III

lets say i have a polygon layer and a point layer what is the best way to return the number of points that intersect with each  polygon feature?

the executeForCount query returns the number of features in the layer.

and the intersect returns a new geometry.

is the only way to do that is intersecting  and then counting the points on every polygon feature?

is there something like aggregate-points for the api?

0 Kudos
1 Solution

Accepted Solutions
RobertScheitlin__GISP
MVP Emeritus

This is the only way that I am aware of unless you have some attribute in your data that you can summarize on (of example I have a municipality field on one of my point layers that gets the municipality name assigned when that point is created. I can then query for count on that point layer using a StatisticDefinition of count to get the point count by municipality).

View solution in original post

8 Replies
RobertScheitlin__GISP
MVP Emeritus

Segev,

  You will have to loop through the polygons in your layer and do a executeForCount on each polygon geometry.

deleted-user-wcpelUUf_XXx
New Contributor III

but is this the right way to count intersecting features?

0 Kudos
RobertScheitlin__GISP
MVP Emeritus

This is the only way that I am aware of unless you have some attribute in your data that you can summarize on (of example I have a municipality field on one of my point layers that gets the municipality name assigned when that point is created. I can then query for count on that point layer using a StatisticDefinition of count to get the point count by municipality).

deleted-user-wcpelUUf_XXx
New Contributor III

I am having a problem looping through the features.

I saw on another qustion that i should use the LayerView but its not defined can you help me get this to the popup

https://codepen.io/segev-salman/pen/gNpYOQ

0 Kudos
RobertScheitlin__GISP
MVP Emeritus

Segev,

   I am not sure what you are trying to do with the popup, but here is a sample that shows the state count in the console:

<html>

<head>
  <meta charset="utf-8">
  <meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no">
  <title>ArcGIS JavaScript Tutorials: Create a Starter App</title>
  <style>
    html,
    body,
    #viewDiv {
      padding: 0;
      margin: 0;
      height: 100%;
      width: 100%;
    }
  </style>

  <link rel="stylesheet" href="https://js.arcgis.com/4.11/esri/css/main.css">
  <script src="https://js.arcgis.com/4.11/"></script>

  <script>
    require([
      "esri/Map",
      "esri/views/MapView",
      "esri/layers/FeatureLayer",
      "esri/geometry/geometryEngine",
      "esri/tasks/QueryTask",
    ], function (Map, MapView, FeatureLayer, geometryEngine, QueryTask) {

      var points = new FeatureLayer({
        url: "https://services3.arcgis.com/1pxU2hJU9ZszJDcX/ArcGIS/rest/services/Young_Adult_Treatment_Facilities/FeatureServer/0"
      });

      var states = new FeatureLayer({
        url: "https://services.arcgis.com/P3ePLMYs2RVChkJx/ArcGIS/rest/services/USA_States_Generalized/FeatureServer/0",
        popupTemplate: {
          title: "{STATE_NAME}",
          content: "layerView"
        }
      });

      var map = new Map({
        basemap: "gray",
        layers: [states, points]
      });

      var view = new MapView({
        container: "viewDiv",
        map: map,
        center: [-118.80543, 34.02700],
        zoom: 13
      });

      var query = states.createQuery();
      states.queryFeatures(query).then(function(result){
        result.features.map(function(feat){
          var sName = feat.attributes.STATE_NAME;
          var query2 = points.createQuery();
          query2.geometry = feat.geometry;
          points.queryFeatureCount(query2).then(function (Qresults) {
            console.log(sName + " has: " + Qresults);
          });
        });
      });
    });
  </script>
</head>

<body>
  <div id="viewDiv"></div>
</body>

</html>
deleted-user-wcpelUUf_XXx
New Contributor III

I want to return the query result inside the popup.

I thought LayerView is the way to loop through features in your layer.

querying the points inside the polygon geometry seems like a much better way, thank you for that.

0 Kudos
RobertScheitlin__GISP
MVP Emeritus

LayerView is for querying the feature that are currently visible in the view

Abdel-haqqItito
New Contributor

 Hey!

Did you post your solution on codepen. I am trying to see how you solved it thank you. 

0 Kudos