Skip to content

Instantly share code, notes, and snippets.

@migurski
Last active January 31, 2024 11:44
Show Gist options
  • Save migurski/8d7c7749fefe5ef3893f2933379f9480 to your computer and use it in GitHub Desktop.
Save migurski/8d7c7749fefe5ef3893f2933379f9480 to your computer and use it in GitHub Desktop.
import datetime
import math
import mercantile
import pytz
import suncalc
def date(yyyy, mm, dd, hh):
dt_naive = datetime.datetime(yyyy, mm, dd, int(hh), int((60 * hh) % 60))
dt_local = dt_naive.astimezone(pytz.timezone('America/Los_Angeles'))
dt_utc = dt_local.astimezone(pytz.utc)
return dt_utc
def deg(r):
return r * 180 / math.pi
def rad(d):
return d * math.pi / 180
def get_gmaps_url(yyyy, mm, dd, hh):
sun = suncalc.get_position(date(yyyy, mm, dd, hh), LON, LAT)
altitude, azimuth = deg(sun['altitude']), deg(sun['azimuth'])
ground_distance = math.cos(rad(altitude)) * DISTANCE / math.cos(rad(LAT))
x, y = mercantile.xy(LON, LAT)
lon2, lat2 = mercantile.lnglat(
x - math.sin(rad(azimuth)) * ground_distance,
y - math.cos(rad(azimuth)) * ground_distance,
)
tpl = 'https://www.google.com/maps/@{lat:.6f},{lon:.6f},{a:.1f}a,35y,{h:.1f}h,{t:.1f}t/data=!3m1!1e3?entry=ttu'
url = tpl.format(
lon = lon2, # adjusted longitude to point at target
lat = lat2, # adjusted latitude to point at target
a = math.sin(rad(altitude)) * DISTANCE, # altitude in meters
t = 90 - altitude, # degrees from vertical
h = azimuth % 360,
)
return url
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment