0

I have function that takes a users coordinates and tests it against a polygon. When I create the fence with whole numbers such as [3,2], [3,4],[5,6],[3,2]. My function returns correctly. I tried inputting real data to create a geofence and used the chrome devtools to simulate a custom geolocation that was within the geofence. However, with real coordinates ( 40.225928, -111.660785) it does not return that the point is within the polygon. Below is my object and method.

const fence = {Fence1: turf.polygon([
  [[40.225,-111.658], [40.2229,-111.664], 
  [40.223,-111.667], [40.225,-111.668], 
  [ 40.226,-111.666], [40.225,-111.658]]
 ])    
};

module.exports = {
check_fences: (req,res,next) => {
    var pt1 = turf.point([req.body.latitude, req.body.longitude]);

for (key in fences) {
  if(turf.intersect(pt1, fences[key])) {
   console.log(key)
   fence = key
 }
}
 if(resort) {
    return res.status(200).send(resort)
} else {
    return res.status(200).send('NOT IN FENCE')
}

The req body object is coming in as: { latitude: 40.224411, longitude: -111.66478 }

Developing with ReactJS

I am at a loss for why this is not working.

1 Answer 1

0

I don't think your point is intersecting with the polygon.

enter image description here

5
  • So I just realized that again, my coordinates were flipped. So I switched the latitude to the first digits and now it is working! Ha whoops!
    – Sharkboots
    Dec 7, 2017 at 22:40
  • I do have another question going off of this. I've tried using the plugin for QGIS and I can't seem to get it to work. I put in: POLYGON (( -111.665784 40.229448, -111.653481 40.229554, -111.653481 40.229554, -111.665784 40.229448)) and then my Point coordinates but I only saw the points. Do I need anything different or do I need to pick one of the specific POLYGON options in the dropdown.Also, I hope that the switched coordinates would have fixed the issue on your end too since I'm getting back true for this query!
    – Sharkboots
    Dec 7, 2017 at 22:44
  • For other people, I think you mean the "QuickWKT" plugin? I think that polygon is poorly defined, it comes up as almost a line here. Do you add in the polygon in one entry and the point in a second entry? Dec 7, 2017 at 22:48
  • Would that explain why it comes in as true sometimes but not always. I'm manually changing the locations in small increments and doesn't seem to be constantly reading accurately. I am not sure if there is a better method of mapping out polygon coordinates. I'm using google maps to just find some points to turn into a polygon. I did 2 in one POLYGON (( -111.665784 40.229448, -111.653481 40.229554, -111.653481 40.229554, -111.665784 40.229448)) POINT (-111.660785 40.225928)
    – Sharkboots
    Dec 7, 2017 at 22:53
  • You can just draw the polygon in qgis and get the wkt values from there? Dec 8, 2017 at 2:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.