Skip to content

Instantly share code, notes, and snippets.

@pigeonflight
Last active December 21, 2016 15:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pigeonflight/a4b5e9939d8f189f2340444220ccc2f7 to your computer and use it in GitHub Desktop.
Save pigeonflight/a4b5e9939d8f189f2340444220ccc2f7 to your computer and use it in GitHub Desktop.
Demo of using a square bounding box
import numpy as np
def in_bounds(bounds,point):
top_left = bounds['top_left']
bottom_right = bounds['bottom_right']
north = top_left['north']
west = top_left['west']
south = bottom_right['north']
east = bottom_right['west']
lon = point[0]
lat = point[1]
in_bounds = np.logical_and(np.logical_and(
lat >= west, # filter the latitude
lat <= east), # that are within the boundaries
np.logical_and(
lon <= north, # filter the longititude
lon >= south)) # that are within the boundaries
return in_bounds
if __name__ == '__main__':
top_left = {'north':40.859311, 'west':-108.525174}
bottom_right = {'north':36.625119, 'west':-99.898083}
bounds= {
'top_left':top_left,
'bottom_right':bottom_right
}
point = [39.733283, -106.670038] #correct
print in_bounds(bounds,point)
@vincentfretin
Copy link

You reversed latitude and longitude.
point[0] is latitude and go with north and south comparison.
point[1] is longitude and go with west and east comparison.

@hannosch
Copy link

If you use Geo software, it's more common to order the coordinates as lon/lat, as this matches using an x/y axis (x-axis would be the equator/longitude). There are also examples of using a reverse z/y/x scheme, with z being the zoom level. But that's mostly just used for some display libraries.

As Vincent said, longitude is west/east from -180 to +180 degrees. Latitude is south/north from -90 to +90 degrees. Though if you want to display points on a map, those typically use Web Mercator projection, which can only deal with -85.05 to +85.05 degrees, so filtering your inputs might make sense.

If you use bounding boxes you might also have to deal with boxes crossing the +/-180 degrees line. It's easiest to just split those boxes into two distinct ones and figure out all points in each of them and than combine the results.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment