I am trying to generate random points on a route from one point to another. What I have till now is, given a source and a destination, I can get a route using Google's API. But how do I get the coordinates of random points on the route?


I'm not sure what tool you are using, but look into linear referencing methods.

For example, if you are using PostGIS, here are 10 random points along a linestring:

SELECT ST_Line_Interpolate_Point('LINESTRING (10 20, 50 60, 80 30, 140 60)', random())
FROM generate_series(1,10)

Or if using Shapely:

from random import random
from shapely.geometry import LineString
line = LineString([(10, 20), (50, 60), (80, 30), (140, 60)])
for i in range(10):
    pt = line.interpolate(random(), True)
  • I am using Google JavaScript directions API. Right now, I am planning to use a python sdk and pass the directions data to shapely. Apr 18 '12 at 15:54

Write a function that returns total length of a route, and another that interpolates a point at an arbitrary distance along a route. Use a random number generator that generates a random distance d along the route such that 0.0 < d < length and passes it to the second function.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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