Verify if XY is inside polygon

9155
40
Jump to solution
02-06-2017 01:23 PM
jaykapalczynski
Frequent Contributor

Is there a way via JS or Python to simply pass an XY to a function and determine if inside or outside a polygon (from a map Service)

I need to do this for Decimal Degrees, DMS, UTM etc.

I can do it like below BUT I would have to list out EVERY node and trying to do this for a complete state

function inside(point, vs) {
    var x = point[0], y = point[1];

    var inside = false;
    for (var i = 0, j = vs.length - 1; i < vs.length; j = i++) {
        var xi = vs[i][0], yi = vs[i][1];
        var xj = vs[j][0], yj = vs[j][1];

        var intersect = ((yi > y) != (yj > y))
            && (x < (xj - xi) * (y - yi) / (yj - yi) + xi);
        if (intersect) inside = !inside;
    }

    return inside;
};

var polygon = [[4040958.21,261090.239],[4399737.773,261090.239],  [4399737.773,1004118.285],[4040958.21,1004118.285]]                                                 

<!--// The below are two examples.  One of them is inside the other is outside to bounding box
// inside -->
test = inside([ 4147263, 646445.066 ], polygon); // true
<!-- // outside -->
<!--
test = inside([ 4537048, 694061 ], polygon); // false
 -->
alert(test);
1 Solution

Accepted Solutions
RobertScheitlin__GISP
MVP Emeritus

Jay,

   Have you not seen that the Polygon class has a contains method?

https://developers.arcgis.com/javascript/3/jsapi/polygon-amd.html#contains 

View solution in original post

40 Replies
RobertScheitlin__GISP
MVP Emeritus

Jay,

   Have you not seen that the Polygon class has a contains method?

https://developers.arcgis.com/javascript/3/jsapi/polygon-amd.html#contains 

jaykapalczynski
Frequent Contributor

I think that would work....looking for an example or two...thoughts?

0 Kudos
RobertScheitlin__GISP
MVP Emeritus

Jay,

   The code would simply be something like this:

var pnt = new Point(4147263, 646445.066, new SpatialReference({ wkid: your datas wkid # }));
var polygon = new Polygon([[4040958.21,261090.239],[4399737.773,261090.239],  [4399737.773,1004118.285],[4040958.21,1004118.285]]);
if (polygon.contains(pnt)){
   console.info("point is inside polygon");
}‍‍‍‍‍‍‍‍‍else{
   console.info("point is NOT inside polygon");
}
jaykapalczynski
Frequent Contributor

Close to what I have above....my issue is that I have a state polygon that will have thousands of XY nodes...I was trying to point at a Map Service Layer instead of listing out 10s of thousands of XY coordinates.  

0 Kudos
RobertScheitlin__GISP
MVP Emeritus

Jay,

   Then you just query the state map service to get a polygon result and then you will not have to create the polygon from coordinates manually.

jaykapalczynski
Frequent Contributor

If I use Feature Vertices to Point tool to get a list of XYs for the State Boundary I end up with over 50,000 XY coordinates.  I am supposed to create a list int he code with 50,000 XY coordinates and process against that?  Just seems like there should be an easier way.

0 Kudos
RobertScheitlin__GISP
MVP Emeritus

Jay,

   There is. Just like I said in my last post. You use a Query and QueryTask to get the state polygon from the map service.

0 Kudos
jaykapalczynski
Frequent Contributor

Sorry for the stupid question...but what am I querying the Map Service for?  Pretty confused.

0 Kudos
jaykapalczynski
Frequent Contributor

SO if I did something along these lines....how do I get all the XYs to a variable or array to be used in the original code above?

require([
  "esri/tasks/query", "esri/tasks/QueryTask", ... 
], function(Query, QueryTask, ... ) {
  var query = new Query();
  var queryTask = new QueryTask( ... );
  query.outSpatialReference = {wkid:102100}; 
  query.returnGeometry = true;
  query.outFields = ["CITY_NAME"];
  queryTask.execute(query, addPointsToMap);
  ...
});
0 Kudos