How to generate random geodata at distance d from another geometry point?

How do I get the Random Geo-points [lat / long in decimal] placed somewhere inside a circle with a radius of 100 meters? The center of the circle is another GeoPoint link. Are there any functions / formulas that implement this?

Basically I am reading the GPS input of my android device and have to generate random geo-points around the device. [In a circle with a radius of 100 m centered on my device].

Please note that no geodata is saved in the database. I need to create all geopoints on the fly as above.


source to share

3 answers

I just wrote a Ruby method that extends Random

to provide this.

Caution : all points lie inside the field, not the circle.

class Random
  def location(lat, lng, max_dist_meters)


Called with, mid_lng, dist)

. It will return a point that is probably within the max_dist_meters


    max_radius = Math.sqrt((max_dist_meters ** 2) / 2.0)


Without adjusting, max_radius

we get points inside the square outside the circle (i.e. in the corners) where the distance is greater than max_dist_meters

. This limits us to a square within a circle, which is probably larger than what you want.

    lat_offset = rand(10 ** (Math.log10(max_radius / 1.11)-5))
    lng_offset = rand(10 ** (Math.log10(max_radius / 1.11)-5))



and 5

come from here .

    lat += [1,-1].sample * lat_offset
    lng += [1,-1].sample * lng_offset
    lat = [[-90, lat].max, 90].min
    lng = [[-180, lng].max, 180].min


We should probably get around here instead of just pressing the value, fixes are welcome.

    [lat, lng]


Comments / cleaning is appreciated!

Sample output here , which you can see well if you insert lat / lngs here .



Pick random points on a square (i.e. pairs of uniform random numbers), then discard any that do not lie inside the circle inscribed in that square. Given (x, y) pairs, the point is inside your circle if:

(x - c_x)^2 + (y - c_y)^2 < r,


where (c_x, c_y) is the center of your circle and r is its radius.



Start here: Create a random point inside the circle (evenly) . Then figure out how to center this circle on the lat / long reference. Then figure out how to map the randomly generated points to the lat / long values. Tip: you want to add the individual components (for example, x

and y

on a circle) in the center of the circle.



All Articles