Intersections of Great Circles¶
Overview¶
A great circle path crosses the entire planet and any two valid great circle paths will always intersect.
- Find the intersection of two great circle paths (always exists)
- Find the intersection of two great circle arcs (if it exists) (TODO)
Prerequisites¶
Concepts | Importance | Notes |
---|---|---|
Numpy | Necessary | Used to work with large arrays |
Pandas | Necessary | Used to read in and organize data (in particular dataframes) |
Intro to Cartopy | Helpful | Will be used for adding maps to plotting |
Matplotlib | Helpful | Will be used for plotting |
- Time to learn: 40 minutes
Imports¶
- Import Packages
- Setup location dataframe with coordinates
import pandas as pd # reading in data for location information from text file
import numpy as np # working with arrays, vectors, cross/dot products, and radians
from pyproj import Geod # working with the Earth as an ellipsod (WGS-84)
import geopy.distance # working with the Earth as an ellipsod
import matplotlib.pyplot as plt # plotting a graph
from cartopy import crs as ccrs, feature as cfeature # plotting a world map
# Get all Coordinates for Locations
location_df = pd.read_csv("../location_full_coords.txt")
location_df = location_df.rename(columns=lambda x: x.strip()) # strip excess white space from column names and values
location_df.head()
Loading...
location_df.index = location_df["name"]
Find the intersection of two great circle paths¶
The intersection of two great circle paths always exists at two positions on the globe if both paths are valid great circle paths (not meridians).
Math of intersection¶
TODO
# Generate Latitude Coordinates based on Longitude Coordinates
def generate_latitude_along_gc(start_point=None, end_point=None, number_of_lon_pts=360):
lon1 = np.deg2rad(location_df.loc[start_point, "longitude"])
lat1 = np.deg2rad(location_df.loc[start_point, "latitude"])
lon2 = np.deg2rad(location_df.loc[end_point, "longitude"])
lat2 = np.deg2rad(location_df.loc[end_point, "latitude"])
# Verify not meridian (longitude passes through the poles)
if np.sin(lon1 - lon2) == 0:
print("Invalid inputs: start/end points are meridians")
# plotting meridians at 0 longitude through all latitudes
meridian_lat = np.arange(-90, 90, 180/len(longitude_lst)) # split in n number
meridians = []
for lat in meridian_lat:
meridians.append((lat, 0))
return meridians
# verify not anitpodal (diametrically opposite, points)
if lat1 + lat2 == 0 and abs(lon1-lon2) == np.pi:
print("Invalid inputs: start/end points are antipodal")
return []
# note: can be expanded to handle input of np arrays by filter out antipodal/merdiain points
# generate n total number of longitude points along the great circle
# https://github.com/rspatial/geosphere/blob/master/R/greatCircle.R#L18C3-L18C7
gc_lon_lst = []
for lon in range(1, number_of_lon_pts+1):
new_lon = (lon * (360/number_of_lon_pts) - 180)
gc_lon_lst.append(np.deg2rad(new_lon))
# Intermediate points on a great circle: https://edwilliams.org/avform147.htm"
gc_lat_lon = []
for gc_lon in gc_lon_lst:
num = np.sin(lat1)*np.cos(lat2)*np.sin(gc_lon-lon2)-np.sin(lat2)*np.cos(lat1)*np.sin(gc_lon-lon1)
den = np.cos(lat1)*np.cos(lat2)*np.sin(lon1-lon2)
new_lat = np.arctan(num/den)
gc_lat_lon.append((np.rad2deg(new_lat), np.rad2deg(gc_lon)))
return gc_lat_lon
lat_lon_pts = generate_latitude_along_gc("boulder", "boston", 360)
def intersection_of_gc(start_gc1=None, end_gc1=None,
start_gc2=None, end_gc2=None):
# get normal of planes containing great circles
# cross product of vectors
normal_one = np.cross([location_df.loc[start_gc1, "cart_x"],
location_df.loc[start_gc1, "cart_y"],
location_df.loc[start_gc1, "cart_z"]],
[location_df.loc[end_gc1, "cart_x"],
location_df.loc[end_gc1, "cart_y"],
location_df.loc[end_gc1, "cart_z"]])
normal_two = np.cross([location_df.loc[start_gc2, "cart_x"],
location_df.loc[start_gc2, "cart_y"],
location_df.loc[start_gc2, "cart_z"]],
[location_df.loc[end_gc2, "cart_x"],
location_df.loc[end_gc2, "cart_y"],
location_df.loc[end_gc2, "cart_z"]])
# intersection of planes, normal to the poles of each plane
line_of_intersection = np.cross(normal_one, normal_two)
# intersection points (one on each side of the earth)
x1 = line_of_intersection / np.sqrt(line_of_intersection[0]**2 + line_of_intersection[1]**2 + line_of_intersection[2]**2)
x2 = -x1
lat1 = np.rad2deg(np.arctan2(x1[2], np.sqrt(pow(x1[0],2)+pow(x1[1],2))))
lon1 = np.rad2deg(np.arctan2(x1[1], x1[0]))
lat2 = np.rad2deg(np.arctan2(x2[2], np.sqrt(pow(x2[0],2)+pow(x2[1],2))))
lon2 = np.rad2deg(np.arctan2(x2[1], x2[0]))
return [(lat1, lon1), (lat2, lon2)]
intersect_pts = intersection_of_gc("boulder", "boston", "greenwich", "cairo")
intersect_pts
[(np.float64(42.13833707967324), np.float64(-92.3589541022366)),
(np.float64(-42.13833707967324), np.float64(87.6410458977634))]
Plot Intersections with Great Circle Paths¶
def interpolate_points_along_gc(start_point=None, end_point=None,
distance_between_points_meter=0):
geodesic = Geod(ellps="WGS84")
lat_start = location_df.loc[start_point, "latitude"]
lon_start = location_df.loc[start_point, "longitude"]
lat_end = location_df.loc[end_point, "latitude"]
lon_end = location_df.loc[end_point, "longitude"]
lat_lon_points = [(lat_start, lon_start)]
# move to next point when distance between points is less than the equal distance
move_to_next_point = True
while(move_to_next_point):
forward_bearing, _, distance_meters = geodesic.inv(lon_start,
lat_start,
lon_end,
lat_end)
if distance_meters < distance_between_points_meter:
# ends before overshooting
move_to_next_point = False
else:
start_point = geopy.Point(lat_start, lon_start)
distance_to_move = geopy.distance.distance(
kilometers=distance_between_points_meter /
1000) # distance to move towards the next point
final_position = distance_to_move.destination(
start_point, bearing=forward_bearing)
lat_lon_points.append((final_position.latitude, final_position.longitude))
# new starting position is newly found end position
lon_start, lat_start = final_position.longitude, final_position.latitude
lat_lon_points.append((lat_end, lon_end))
return lat_lon_points
def arc_points(start_point=None, end_point=None,
n_total_points=10):
start_lat = location_df.loc[start_point, "latitude"]
start_lon = location_df.loc[start_point, "longitude"]
end_lat = location_df.loc[end_point, "latitude"]
end_lon = location_df.loc[end_point, "longitude"]
geodesic = Geod(ellps="WGS84")
_, _, distance_meter = geodesic.inv(start_lon,
start_lat,
end_lon,
end_lat)
distance_between_points_meter = distance_meter / (n_total_points + 1)
points_along_arc = interpolate_points_along_gc(start_point, end_point,
distance_between_points_meter)
return points_along_arc
def plot_gc_with_intersection(start_gc1=None, end_gc1=None,
start_gc2=None, end_gc2=None,
lon_west=-180, lon_east=180,
lat_south=-90, lat_north=90):
# Set up world map plot
fig = plt.subplots(figsize=(15, 10))
projection_map = ccrs.PlateCarree()
ax = plt.axes(projection=projection_map)
ax.set_extent([lon_west, lon_east, lat_south, lat_north], crs=projection_map)
ax.coastlines(color="black")
ax.add_feature(cfeature.STATES, edgecolor="black")
# Plot Great Circle Path
gc_one_lat_pts = generate_latitude_along_gc(start_gc1, end_gc1)
longitudes = [x[1] for x in gc_one_lat_pts] # longitude
latitudes = [x[0] for x in gc_one_lat_pts] # latitude
plt.plot(longitudes, latitudes)
gc_two_lat_pts = generate_latitude_along_gc(start_gc2, end_gc2)
longitudes = [x[1] for x in gc_two_lat_pts] # longitude
latitudes = [x[0] for x in gc_two_lat_pts] # latitude
plt.plot(longitudes, latitudes)
# Plot intersection point
intersection_point = intersection_of_gc(start_gc1, end_gc1,
start_gc2, end_gc2)
longitudes = [x[1] for x in intersection_point] # longitude
latitudes = [x[0] for x in intersection_point] # latitude
plt.scatter(longitudes, latitudes, s=200, c="purple", label="intersection")
# Plot Great Circle Arc
gc_one_arc_pts = arc_points(start_gc1, end_gc1)
longitudes = [x[1] for x in gc_one_arc_pts] # longitude
latitudes = [x[0] for x in gc_one_arc_pts] # latitude
plt.plot(longitudes, latitudes, c="pink", label="GC 1")
plt.scatter(longitudes[0], latitudes[0], s=100, c="green", label="Arc Start")
plt.scatter(longitudes[-1], latitudes[-1],s=100, c="red", label="Arc End")
gc_two_arc_pts = arc_points(start_gc2, end_gc2)
longitudes = [x[1] for x in gc_two_arc_pts] # longitude
latitudes = [x[0] for x in gc_two_arc_pts] # latitude
plt.plot(longitudes, latitudes, c="cyan", label="GC 2")
plt.scatter(longitudes[0], latitudes[0],s=100, c="green")
plt.scatter(longitudes[-1], latitudes[-1],s=100, c="red")
plt.legend(loc="lower left")
plt.title(f"Intersection Point = {intersection_point}")
plt.show()
plot_gc_with_intersection("boulder", "boston", "greenwich", "cairo")
/home/runner/micromamba/envs/cookbook-gc/lib/python3.13/site-packages/cartopy/io/__init__.py:241: DownloadWarning: Downloading: https://naturalearth.s3.amazonaws.com/110m_physical/ne_110m_coastline.zip
warnings.warn(f'Downloading: {url}', DownloadWarning)
/home/runner/micromamba/envs/cookbook-gc/lib/python3.13/site-packages/cartopy/io/__init__.py:241: DownloadWarning: Downloading: https://naturalearth.s3.amazonaws.com/110m_cultural/ne_110m_admin_1_states_provinces_lakes.zip
warnings.warn(f'Downloading: {url}', DownloadWarning)

plot_gc_with_intersection("arecibo", "zambezi", "johannesburg", "reykjavík")

Find the intersection of two great circle arcs (TODO)¶
The intersection of two great circle paths always exists at two positions on the globem but intersections do not always exists along the great circle arcs.