Reputation: 1794
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)
Upvotes: 0
Views: 6048
Reputation: 78316
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.
Upvotes: 1
Reputation: 2641
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
Upvotes: 1
Reputation: 881323
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
Upvotes: 0
Reputation: 4114
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);
}
Upvotes: 0
Reputation: 46921
Simple: Generate two random numbers, one for latitude and one for longitude, inside the bounding rectangle of the map, for each point.
Upvotes: 0
Reputation: 2528
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.
Upvotes: 3