Geofencing with Pytrack

  • Hi,

    I’ve got a Lopy with Pytrack and managed to print lat/long coordinates.

    As a second step, I’d like to add geofence capabilities. I’ve found the Picket library which seems ideal for this:

    Has anyone used this library before? Not 100% sure whether it is compatible with Lopy given that the library is Python and not Micropython.

    As a first test, I took one of the examples from here:

    import picket
    def test_in_fence_easy():
    	my_fence = picket.Fence()
    	my_fence.add_point((1, 1))
    	my_fence.add_point((3, 1))
    	my_fence.add_point((3, 3))
    	my_fence.add_point((1, 3))
    	assert my_fence.check_point((2, 2))

    I only get a blank line as a result when I run this with the Pymark plugin in Atom. Was hoping for something like ‘True’ or ‘False’ depending on whether the point is inside or outside the polygon.

    What am I missing? I'd really appreciate if someone can point me in the right direction.

    Thanks for your help!

    Disclaimer: I’m a complete beginner who is nonetheless fascinated by this stuff. Hoping that you can provide simple answers to my (stupid) questions :) Thank you!

  • Global Moderator

    As you can see, I added 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 the my_fence.check_point function as it requires a tuple input, this will return True or False for inside / outside respectively. You can test it like so:


    (I did not test this specifically, but it should work like that)


  • @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 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 file:

    while (True):
        coord = l76.coordinates()
        #f.write("{} - {}\n".format(coord,
        print("{} - {} - {}".format(coord,, 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?

  • Global Moderator

    I do not know what you are doing wrong. When I try the together with inserted in 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


  • Anyone able to help? Thanks in advance

Log in to reply

Pycom on Twitter