[SOLVED] How to use negative longitude?


I need to write a script in Python to find if a location falls inside an area.

Problem is, the map is split by the prime meridian (Greenwich), meaning that one of the two longitudes is negative:

42.697506,2.892532 43.296237,-0.376291

Do you know how to solve this?

Incidently, is there an online tool to draw a map by proving the top+left and bottom+right coords?

Thank you.

There are two questions: whether a point is within a region and an online tool to visualize a region by some kind of map.

Google Earth can be used to handle the second question. I presume that there are others.

There are many cases for the first question depending on the complexity of the region.

In the most complex cases, even mathematics has multiple definitions. Please, see: “How to find whether a point is inside or outside the region?”, “Nonzero-rule”, and “Even–odd rule”.

In the case of simple polygons, it becomes simpler. A simple polygon is one where none of the edges cross over each except at the defining vertices. Now for a mathematics lesson. Using analytic geometry, a line can be expression as: y=m x + b, e.g., y=(2/3)x+(5/7). We’re going to use a slightly more general form a x+b y+c=0, as this form also allow easy representation of vertical and horzontal lines. If one inserts the coordinates of an arbitary point into the left side expression and evaluates the expression, one gets a signed number. The magnitude of that number is the perpendicular distance of the point from the line. The sign of that number indicates which side of the line that the point is on. For each edge of the polygon, one can compute that expression. To determine what sign is desired, substitute in the coordinate any one of the other vertices’ coordinates. It doesn’t matter which vertex as they all have to be on one side as the shape is simple. Note the sign of the result. Then, do the point in question. If the sign is the same, then the point is on the correct side of that edge. Repeat this process for all the edge until either it fails for an edge, in ehich case, the point is outside or all edges pass, in which case the point is inside. You will need to decide whether a zero result edge test is inside or not and that is your decision as the point is on that edge. The other edge test still need to be done though.

In the case of a simple axis aligned rectangle, those edge line equations become even simpler. The four edge tests become the point’s longitude is greater than or (possibly equal to, see the comment of a zero edge test result above) the longitude of the left edge of the rectangle, the point’s longitude is less than or (possibly equal to, see the comment of a zero edge test result above) the longitude of the right edge of the rectangle, the point’s latitude is greater than or (possibly equal to, see the comment of a zero edge test result above) the latitude of the bottom edge of the rectangle, and the point’s latitude is less than or (possibly equal to, see the comment of a zero edge test result above) the longitude of the top edge of the rectangle. In the case of negative numbers, just use the usual comparison rules. Think of a number line extending from negative infinity on the left to positive infinity on the right. If a value m is to the left of a value n, then m is less than n. If a value m is to the right of a value n, then m is greater than n. If they equal they are equal.

I believe that answers the question. If it doesn’t, then try again.

I don’t see the problem?

Given the boundaries you have and suppose the location is xLat, yLon, just look if xLat is between 42.697506 and 43.296237 and yLon is between 2.892532 and -0.376291

42.9, 0, is inside the area because both are between the boundaries.
42.9, -5, longitude is outside the area because -5 is not between 2.892532 and -0.376291.

Thanks much for the infos.

I agree it’s a no-brainer, but some reason the following code fails finding locations that are within the map, even that specific location:

#Fill list with all track points
gpx = gpxpy.parse(track_file)
full_track_points = []
for track in gpx.tracks:
	for segment in track.segments:
		for point in segment.points:

#MAP min lat 42.697506, min lon 2.892532, max lat 43.296237, max lon -0.376291
track_min_latitude, track_min_longitude = min(full_track_points)
track_max_latitude, track_max_longitude = max(full_track_points)

#Check if any waypoint lies within the map
for waypoint in locations_gpx.waypoints:
	if waypoint.latitude == "43.02827" and waypoint.longitude == "0.57859":
		print("Found location")

	if ((track_min_latitude < waypoint.latitude < track_max_latitude) and (track_min_longitude < waypoint.longitude < track_max_longitude)):

It looks like Python considers a negative value as bigger than a positive.

I’ll look deeper.

Thank you.

Edit: Simple enough. Just swap longitudes:

if track_min_longitude > track_max_longitude:
	track_max_longitude,track_min_longitude = track_min_longitude,track_max_longitude