Skip to content

Instantly share code, notes, and snippets.

@devniel
Created August 9, 2013 18:49
Show Gist options
  • Select an option

  • Save devniel/6196108 to your computer and use it in GitHub Desktop.

Select an option

Save devniel/6196108 to your computer and use it in GitHub Desktop.
Little implementation of http://janmatuschek.de/LatitudeLongitudeBoundingCoordinates code for get the bounding box coordinates of a geographical point.
degLat = -12.04930
degLon = -77.08223
radLat = math.radians(degLat)
radLon = math.radians(degLon)
MIN_LAT = math.radians(-90) # -PI/2
MAX_LAT = math.radians(90) # PI/2
MIN_LON = math.radians(-180) # -PI
MAX_LON = math.radians(180) # PI
distance = 0.5
radius = 6371.01
if radius < 0 and distance < 0:
raise Exception(":o")
radDist = distance/radius
minLat = radLat - radDist
maxLat = radLat + radDist
minLon = 0
maxLon = 0
if minLat > MIN_LAT and maxLat < MAX_LAT :
deltaLon = math.asin(math.sin(radDist) / math.cos(radLat))
minLon = radLon - deltaLon
if minLon < MIN_LON:
minLon += 2 * math.pi
maxLon = radLon + deltaLon
if maxLon > MAX_LON:
maxLon -= 2 * math.pi
else:
minLat = math.max(minLat, MIN_LAT)
maxLat = math.min(maxLat, MAX_LAT)
minLon = MIN_LON
maxLon = MAX_LON
# RETURNS
"""
MIN LAT -12.053796601
MAX LAT -12.044803399
MIN LON -77.0868279003
MAX LON -77.0776320997
"""
# Show points in http://www.geoplaner.com/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment