Geofencing with Pytrack
-
@Gijs said in Geofencing with Pytrack:
Gijs
Happy Friday Gijs. Only time of the week I manage to find some time to work on this project.
Your reply looks very promising. To test this I’ve added the following section to main.py:
my_fence = picket.Fence() my_fence.add_point((-0.9558105, 51.6759616)) my_fence.add_point((0.6976318, 51.6589266)) my_fence.add_point((0.7196045, 50.9203505)) my_fence.add_point((-0.9228516, 50.9064969)) if(my_fence.check_point(l76.coordinates())): print('device inside fence') else: print('device outside fence')
However, I don't fully understand the output I get:
(5071.07, -0.5725584) (-0.9558105, 51.67596) (0.6976318, 51.65893) pointA[1] >= pointB[1] False False (2.251526, -0.5725584) (0.6976318, 51.65893) (0.7196045, 50.92035) pointA[1] >= pointB[1] False False (-6101.892, -0.5725584) (0.7196045, 50.92035) (-0.9228517, 50.9065) pointA[1] >= pointB[1] False False (1.282171, -0.5725584) (-0.9228517, 50.9065) (-0.9558105, 51.67596) pointA[1] <= pointB[1] False False [] [] device outside fence
Looking at picket.py, the output seems to correspond rather with check_in_bounds than with check_point to see if point is within polygon.
At the bottom it prints ‘device outside fence’, but my coordinates should be inside. That's the part I'm really interested in. Again my aim is to create a polygon, check if device lat/long are inside, and call if/else. Looking at the printed coordinates in the output, I can only see my latitude but not longitude.
I think my problem is that I just don’t fully understand yet how the picket.py is working, and how I can use it to achieve my aim. Perhaps I need to tweak it for my purpose.
I’m hoping you can shed some light on this.
Thank you!
-
As you can see, I added picket.py into the library folder. I hope that’s correct?
Yes its correct like that!
But how do I combine my_fence.check_point with coord = l76.coordinates()
As
l76.coordinates()
returns a tuple with information,(lat, long)
, you can directly insert that in themy_fence.check_point
function as it requires a tuple input, this will returnTrue
orFalse
for inside / outside respectively. You can test it like so:if(my_fence.check_point(l76.coordinates())): ...
(I did not test this specifically, but it should work like that)
Gijs
-
@Gijs hartelijk bedankt :) really appreciate you looking into this and helping out a beginner!
I wasn’t able to check your response before, but now I managed to replicate it. Very happy that the library is working!
I’m trying to combine the pytrack example with the picket library, so that I can check whether the lopy coordinates are in a pre-defined fence and call specific actions based on that.
My current project structure looks like this:
As you can see, I added picket.py into the library folder. I hope that’s correct?Now I’m wondering how to integrate the relevant picket element:
def test_in_fence_real_latlong_large_scale(): my_fence = picket.Fence() my_fence.add_point((44.030013, -44.309738)) my_fence.add_point((45.032447, 66.784004)) my_fence.add_point((-28.837860, 59.928536)) my_fence.add_point((-24.759721, -20.930834)) assert my_fence.check_point((0.447227, 24.244944))
into the main.py file:
while (True): coord = l76.coordinates() #f.write("{} - {}\n".format(coord, rtc.now())) print("{} - {} - {}".format(coord, rtc.now(), gc.mem_free()))
It is straightforward to replace the fence coordinates. But how do I combine
my_fence.check_point with coord = l76.coordinates()And how do I add an if true condition to that?
-
Hi,
I do not know what you are doing wrong. When I try thepicket.py
together withtest_picket.py
inserted inmain.py
and call one of the functions, it works fine for me>>> test_in_fence_easy() (3.0, 2.0) (3, 1) (3, 3) pointA[1] <= pointB[1] True True (1.0, 2.0) (1, 3) (1, 1) pointA[1] >= pointB[1] True True
Gijs
-
Anyone able to help? Thanks in advance