views:

17

answers:

1

I'm interested in pulling street view data at random within a country.

I can only figure out how to generate a point within a bounding box. Often it pulls data from nearby nations. I'm using a GIS server right now, but I'm interested in removing it from the workflow since it's a pain.

Anybody know of any javascript mapping API that can randomly generate a point within a complex polygon, such as a country boundary?

Phil

+2  A: 

I don't know of any practical algorithm to do this for an arbitrary, potentially-concave polygon, and still maintain uniform probability for each possible point.

(I mean, you could pick an axis crossing the polygon at random, find the crossing-points and pick a position from somewhere inside odd crossing-point-ranges. But that would leave some points more likely to be picked then others.)

I would go with the approach of picking a random point inside the rectangular bounding box, checking for collision against the country's vector data and generating another one on a miss.

bobince
Thanks, that's what I'm working on now, but I'm hoping there was a way that was easier.
pinnacler
Yeah, you can do it without making a new geoip request for each point (as in your first comment), though. Hit-checking a point against a polygon is easy. Thought: I guess you could generate a good-enough random point in a polygon by using a randomly-offset rasterisation across the bounding box, picking the start-end ranges along the raster lines in that rasterisation and choosing a random point along the linear sum of all the ranges. As you increase the number of lines you make the imperfectly-uniform probability much less obvious than the example with one axis line.
bobince
(OTOH this is unlikely to be any faster than the discard-and-try-again method.)
bobince