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);
Solved! Go to Solution.
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
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);
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);
});
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);
});
I added the conversionMode as it looks like its required.
var params = {
conversionMode: "mgrsDefault",
conversionType: "DMS",
sr: new SpatialReference({ wkid:4326}),
strings: [GPSPoint]
};
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); });
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.
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);
});
Error says:
localhost says:
Error: Error executing fromGeoCoordinateString
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