0

I need it to stress test some location based web service. The input is 4 pairs of lat/lon defining a bounding rectangle or a set of points defining a polygon.

Are there any libraries/algorithms for generating random point on a map? (Python/java)

Dima
  • 1,774
  • 5
  • 21
  • 31

6 Answers6

3

In java you can use Math.random()

For example, if you want to generate a random number between 1 and 10:

int randomNumGenerated = (int)(Math.Random()*10) + 1;

You can apply this to the issue you are trying to solve easily.

bstack
  • 2,466
  • 3
  • 25
  • 38
  • It's not clear how to apply this efficiently when dealing with 4 points that don't necessarily represent a rectangle. – Lior Aug 28 '12 at 09:00
  • You only need to randomly generate two points for Long and Lat e.g. (a,b) and (a,y) where x co-ordinate is the same. Then randomly generate the rectangle breadth i.e br. 3rd rectangle point is (a, b+br). 4th rectangle point is (a, y+br) – bstack Aug 28 '12 at 10:54
  • Look at the question - the randomized points should be part of a *general polygon*, not necessarily a rectangle. – Lior Aug 28 '12 at 12:54
1

Take a look at this question, which deals with generating points inside an arbitrary 4-point convex polygon.

Random points inside a 4-sided Polygon

Community
  • 1
  • 1
Lior
  • 2,531
  • 1
  • 15
  • 15
1

This article, on sphere point picking explains far better than I could why the naive approach of generating 2 random numbers on the interval [0,1) will lead to a poor distribution of points across the surface of the sphere. That may or may not be a concern of OP.

However, it ought to be of concern to OP that randomly generating a set of 4 points on the surface of the Earth might necessitate some tricky programming. Consider the case of the 'polygon' defined by the points (lat/long, all in degrees) (+5,90),(+5,-90),(-5,-90),(-5,90). Does the point (0,0) lie inside this polygon or outside it ? What about the point (0,180) ? It's very easy to generate such ambiguous polygons -- the surface of a sphere is not well modelled by the Euclidean plane.

I'd take a completely different approach -- generate 1 point at random, then generate lat and long offsets. This will give you a quasi-rectangular patch on the surface, and you can tune the generation of the offsets to avoid ambiguous polygons. If you want to generate polygons which are not quasi-rectangular, generate a series of points and angles which, when combined, define a polygon which suits your needs.

High Performance Mark
  • 77,191
  • 7
  • 105
  • 161
0

Why wouldn't you just generate the latitude as a random number between -90 and 90, and the longitude as another random number between -180 and 180?

Then you have a point. Yo can then generate as many points as you need to make a polygon.

You can generate a random number between a and b with something like:

rnum = a + rnd() * (b-a); // where rnd() gives a number from 0 to 1
paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
0

Simple: Generate two random numbers, one for latitude and one for longitude, inside the bounding rectangle of the map, for each point.

aib
  • 45,516
  • 10
  • 73
  • 79
0

double longitude = Math.random() * Math.PI * 2;

or use

public static LatLng random(Random r) {            

return new LatLng((r.nextDouble() * -180.0) + 90.0,
                                (r.nextDouble() * -360.0) + 180.0);
        }
Mohammod Hossain
  • 4,134
  • 2
  • 26
  • 37