Python Forum
Finding if a location lies within a map?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Finding if a location lies within a map?
#1
Hello,

I'm using Gpxpy to read two files (a track and a set of locations), and need to keep only locations that lie within the map (top+left, bottom+right) where the track lies.

Does Python provide a way to compare two tuples?

#READ TRACK
track_file = open(TRACK, mode='rt', encoding='utf-8')
gpx = gpxpy.parse(track_file)
track_file.close()

full_track_points = []
for track in gpx.tracks:
	for segment in track.segments:
		for point in segment.points:
			full_track_points.append([point.latitude,point.longitude])

track_min_coords = min(full_track_points)
track_max_coords = max(full_track_points)

#LOOP THROUGH LOCATIONS, IGNORING THOSE LYING OUTSIDE TRACK MAP
for waypoint in locations_gpx.waypoints:
	location_coords = (waypoint.latitude,waypoint.longitude)

	#HERE
	#How to check if a waypoint lies withing a map?
Thank you.

---
Edit: This returns no hits :-/

if not (track_min_latitude < waypoint.latitude < track_max_latitude and track_min_longitude < waypoint.longitude < track_max_longitude):
	print("Not within map")
else:
	print("Within map")
Reply
#2
Is it still unsolved?
How did you compute track_min_latitude and other boundaries?
Are point.longitude, point.latitude numbers? (I don't know, but may be they are strings;)
I would recommend to open files using context manager, e.g. with open(...) as myfile:.
You can use filter instead of for loop.
Reply
#3
Python does have tuple comparisons, but they won't do what you want. You'll just need to compare the individual coordinates.

def point_in_rectangle(point, rectangle):
    return (rectangle[0][0] <= point[0] <= rectangle[1][0] and
            rectangle[0][1] <= point[1] <= rectangle[1][1])

top_left = (1, 2)
bottom_right = (9, 8)

points = [(0, 0),
          (1, 1),
          (4, 5),
          (8, 8),
          (8, 9),
          (12, 3),
         ]

for point in points:
    print(f"{point} in rectangle is {point_in_rectangle(point, (top_left, bottom_right))}")
Output:
(0, 0) in rectangle is False (1, 1) in rectangle is False (4, 5) in rectangle is True (8, 8) in rectangle is True (8, 9) in rectangle is False (12, 3) in rectangle is False
Reply
#4
def in_boundary(boundaries, location):
    for ends, loc in zip(boundaries, location):
        lower = min(ends)
        upper = max(ends)
        if not lower <= loc <= upper:
            return False
    return True


boundaries = [(50, 60), (40, 50)]
# the function get lower and upper boundary with min and max
# boundaries = [(60, 50), (50, 40)]
location =  (60, 45)

if in_boundary(boundaries, location):
    print(f"Location {location} is inside of {boundaries}")
else:
    print(f"Location {location} is outside of {boundaries}")
This can also handle 3 dimensions.
I'm not sure, if this is right for more than 3 dimensions.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#5
Thanks everyone. I figured it out: It so happens that the map sits on either side of the first meridian, and the bottom+right longitude is negative -0.376291):

TRACK MIN LAT 42.697506 TRACK MIN LON 2.892532 TRACK MAX LAT 43.296237 TRACK MAX LON -0.376291

As a result, the following location, which does lie within the map, is ignored:

<wpt lat="43.02827" lon="0.57859"></wpt>

I'll read up on how to solve this.

Thakn you!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Finding location within a premises metro17 1 1,310 Jan-20-2021, 01:24 AM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020