4

Let's say we have two addresses that are provided. I grabbed two random ones from the web below:

# Google headquarters:
google_lat = 37.422131
google_lon = -122.084801

# Apple headquarters
apple_lat = 37.33467267707233
apple_lon = -122.0089722675975

Now, lets say I'd like to interpolate a series of points between these two addresses. Maybe for starters a simple linear interpolation (or one that takes into account the spherical nature of the Earth) would work. In practice, a road route like provided via Google Maps would work if I could obtain an array of lat/longs for every n miles between each point. Might there be a REST API to obtain points between two addresses?

Just to get started on some ideas, how could I start approaching a task like this if I cannot go the API approach above? Bare minimum I would want is an array of lat/longs between each address with the points therein.

An example in Python would be nice but a language agnostic discussion is welcome too.

2
  • You'll want to look up haversine formula for calculating distance between two points. Beyond that, you're talking about routing or cost path which there are plenty of APIs out there.
    – KHibma
    Commented Jun 17, 2022 at 0:16
  • @KHibma care to share some good ones? My org has a HERE maps subscription but their documentation isn't super clear on whether I can generate intermediate points between a start and end point.
    – Coldchain9
    Commented Jun 17, 2022 at 0:41

1 Answer 1

8

A simple linear interpolation solution

This solution generates N-number of random points lying on a straight line between two initial locations. Initial latitude and longitude were transformed to x and y of the EPSG:7131.

The code below uses:

This piece of code

from random import uniform
from pyproj import CRS, Transformer
from shapely.geometry import Point, LineString

# Start Point : Google headquarters:
google_lat = 37.422131
google_lon = -122.084801

# End Point : Apple headquarters
apple_lat = 37.33467267707233
apple_lon = -122.0089722675975

def reproject_point(lon, lat):
    in_crs = CRS.from_epsg(4326)
    out_crs = CRS.from_epsg(7131)
    proj = Transformer.from_crs(in_crs, out_crs, always_xy=True)
    x, y = proj.transform(lon, lat)
    return x, y

def generate_random_points_on_line(number, line):
    points = []
    while len(points) < number:
        point = line.interpolate(uniform(0, line.length)).wkt
        points.append(point)
    return points    

start_point = Point(reproject_point(google_lon, google_lat))
end_point = Point(reproject_point(apple_lon, apple_lat))

straight_line = LineString([start_point, end_point]) 

print(generate_random_points_on_line(5, straight_line))

results into a list with five points:

['POINT (83630.95248767719 -17059.66002342093)', 'POINT (82463.24493028085 -15387.31134972617)', 'POINT (80809.27255433255 -13018.55173031226)', 'POINT (86585.54062188574 -21291.11465113199)', 'POINT (86776.78696433621 -21565.01076931689)']

result


References:

Not the answer you're looking for? Browse other questions tagged or ask your own question.