Geofencing with Pytrack
-
Ok, I’ve tried lots of different things in the meanwhile. But I just don’t get it to work. Frustrating!
@jcaron @Gijs I know I probably pushed the limits of your willingness to help a newbie with his questions. Really appreciate you got me this far! I feel like I’m so close though to get this to work. Perhaps you can have another look?
I’ve put the earlier PiP error to the side for now, as I can circumvent it by including the function directly in main.py.
However I’m going in circles regarding the coordinates error:
Traceback (most recent call last): File "main.py", line 60, in <module> File "main.py", line 55, in check_all_fences AttributeError: 'str' object has no attribute 'coordinates'Line 55 is the third line below:
def check_all_fences(coords, fences): for fence in fences: if PiP(coords, fence.coordinates): return fence return None coords = l76.coordinates() found_fence = check_all_fences(coords, polygondata) if (found_fence != None): print ("Coords in fence {}").format(found_fence.name) else: print ("Coords not in any fence")This is the PiP function:
def PiP(x: int, y: int, poly) -> bool: num = len(poly) i = 0 j = num - 1 c = False for i in range(num): if ((poly[i][1] > y) != (poly[j][1] > y)) and \ (x < poly[i][0] + (poly[j][0] - poly[i][0]) * (y - poly[i][1]) / (poly[j][1] - poly[i][1])): c = not c j = i return cI've created a new JSON file in the meanwhile: polygon.py (saved as .py as I can't upload json)
Please let me know if you need any more info.
Thank you!
-
@newkit Your JSON is not structured as it was in your previous post. It had
coordinatesas a direct attribute of each fence, while it is now one level down insidegeometry:{ "type": "Feature", "properties": { "name": "Fence 1" }, "geometry": { "type": "Polygon", "coordinates": [So it should be
fence.geometry.coordinatesinstead. Of course the code assumes you only have polygons at this stage, if you have other types the code would need to be adapted.Likewise, the name is also one level down inside
properties, so it should befound_fence.properties.namea bit further down. -
Thanks @jcaron, that makes sense. But it just seems to shift the error to geometry:
Traceback (most recent call last): File "main.py", line 60, in <module> File "main.py", line 55, in check_all_fences AttributeError: 'str' object has no attribute 'geometry'The previous json file showed the same coordinates error as in the previous post. Initially I thought my json format was the issue, therefore I created the new file.
I also tried various changes around this code:
with open('polygon.json') as j: polygondata = json.load(j)E.g. json.loads(j), json.load(‘j’), or
input_file = open ('polygon.json') polygondata = json.load(input_file)It doesn't solve the issue though.
-
@newkit OK, so a few issues:
- My python is indeed rusty and I used Javascript object syntax rather than python dict. So it's
fence['geometry']['coordinates'], notfence.geometry.coordinatesandfound_fence['properties']['name'], notfound_fence.properties.name. - The list of coordinates is in an inner array inside
coordinates(I suppose your JSON file can have multiple polygons for a single feature?), so it's reallyfence['geometry']['coordinates'][0] - The list of fences is not at the top level in the JSON, but within
features, so it'spolygondata['features'], notpolygondata. - PiP expects
xandyas separate args, not an array, so it'sPiP(coords[0], coords[1], fence['geometry']['coordinates'][0]) ()where not in the same place in the print when a match is found.
Final result (tested on python 3.7, don't have a Pycom device at hand right now, you'll have to change the json import):
import json input_file = open ('polygon.json') polygondata = json.load(input_file) def PiP(x: int, y: int, poly) -> bool: num = len(poly) i = 0 j = num - 1 c = False for i in range(num): if ((poly[i][1] > y) != (poly[j][1] > y)) and \ (x < poly[i][0] + (poly[j][0] - poly[i][0]) * (y - poly[i][1]) / (poly[j][1] - poly[i][1])): c = not c j = i return c def check_all_fences(coords, fences): for fence in fences: if PiP(coords[0], coords[1], fence['geometry']['coordinates'][0]): return fence return None coords = (48, 0) #coords = l76.coordinates() found_fence = check_all_fences(coords, polygondata['features']) if (found_fence != None): print ("Coords in fence {}".format(found_fence['properties']['name'])) else: print ("Coords not in any fence")Note that in addition to the previous caveats, it only tests again the first set of coordinates in a feature.
- My python is indeed rusty and I used Javascript object syntax rather than python dict. So it's
-
@jcaron that seems to have fixed it. Initial results look positive, will do some more testing in the next days.
Thank you so much for sorting this out!
Hello! It looks like you're interested in this conversation, but you don't have an account yet.
Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.
With your input, this post could be even better 💗
Register Login