Select to view content in your preferred language

Verify if XY is inside polygon

11503
40
Jump to solution
02-06-2017 01:23 PM
jaykapalczynski
Honored 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);
40 Replies
RobertScheitlin__GISP
MVP Emeritus

Jay,

   You will have to convert DMS and DMM to DD before you can create a point geometry from those entries.

 To do this you use the GeometryService > fromGeoCoordinateString:

https://developers.arcgis.com/javascript/3/jsapi/geometryservice-amd.html#fromgeocoordinatestring 

jaykapalczynski
Honored Contributor

Think I am butchering this....but trying....how far off am I ????  Think I am destroying the "fromGeoCoordinateString"

Grabbing XY from 2 input boxes

var XValue2 = document.getElementsByName('XCoord')[0].value
var YValue2 = document.getElementsByName('YCoord')[0].value
var GPSPoint = YValue2 + "," + XValue2

var XYConvert.fromGeoCoordinateString(mgrsDefault, DMS, new SpatialReference({ wkid:4326}), GPSPoint);
alert(XYConvert);

0 Kudos
RobertScheitlin__GISP
MVP Emeritus

Jay,

   Unfortunately pretty far off.

var XValue2 = document.getElementsByName('XCoord')[0].value
var YValue2 = document.getElementsByName('YCoord')[0].value 
var GPSPoint = XValue2 + "," + YValue2

var params = {
  conversionType: "DMS",
  sr: new SpatialReference({ wkid:4326}),
  strings: [GPSPoint]
};
 
var XYConvert.fromGeoCoordinateString(params, function(result){
  alert(result.coordinates);
});
0 Kudos
jaykapalczynski
Honored Contributor

I put that in there and seems to blow up on this...if I comment out the code runs unfortunately not giving me the new coordinates

var XYConvert.fromGeoCoordinateString(params, function(result){
  alert(result.coordinates);
});
0 Kudos
jaykapalczynski
Honored Contributor

I added the conversionMode as it looks like its required.

var params = {
conversionMode: "mgrsDefault",
conversionType: "DMS",
sr: new SpatialReference({ wkid:4326}),
strings: [GPSPoint]
};

0 Kudos
RobertScheitlin__GISP
MVP Emeritus

Jay,

  So I was not sure why you had XYConvert and chat that var referenced.  So the likely issue is that you need to use 

geometryService.fromGeoCoordinateString(params, function(result){
  alert(result.coordinates);
});
0 Kudos
jaykapalczynski
Honored Contributor

I made that change and NO errors...but nothing alerted to me...  alert(result.coordinates);

Need to get the result into a variable...REALLY appreciate your help with this.

0 Kudos
RobertScheitlin__GISP
MVP Emeritus

So it seems that the conversion is failing then and we need to see the error:

geometryService.fromGeoCoordinateString(params, function(result){
  alert(result.coordinates);
}, function(error){
  alert(error);
});‍‍‍‍‍
0 Kudos
jaykapalczynski
Honored Contributor

Error says:  

localhost says:

Error:  Error executing fromGeoCoordinateString

0 Kudos
jaykapalczynski
Honored Contributor

OK hang on....dang it I had my geometry service in there....commented out and using the below

var geometryService = new GeometryService("https://sampleserver5.arcgisonline.com/arcgis/rest/services/Utilities/Geometry/GeometryServer");

I now get this error: 

undefined

0 Kudos