[GIS] Generating random lat/long coordinates

coordinatesleaflet-rrrandomunited-kingdom

Dedicated page on use of leaflet in R provides a very useful example on generating random latitude and longitude values for Central Europe:

cbind(rnorm(40) * 2 + 13, rnorm(40) + 48)

which results in neatly distributed points:

picture from leaflet page

(Source: Leaflet official page)

I'm interested in generating a similar set of randomly generated points by, broadly, corresponding to the UK. For instance, how can I use the latitude and longitude map available through Maps of World to arrive at the X, Y and Z values in the formula below so the generated dots would, roughly, fell across the British Isles?

cbind(rnorm(40) * X + Y, rnorm(40) + Z)

Lat/Long Map


I'm interested in a rough approximation, if some points will fall in the see or outside the UK that's fine.

Best Answer

What you want to do is to generate a random set of numbers within the following approximated box:

[Longitude,      Latitude]
[-10.8544921875, 49.82380908513249],
[-10.8544921875, 59.478568831926395],
[2.021484375,    59.478568831926395],
[2.021484375,    49.82380908513249]

Source for these points is https://gist.github.com/UsabilityEtc/6d2059bd4f0181a98d76 , but it could just as well be taken from Google Earth or similar.

The method for choosing random points that you suggest relies calculations and offsets. I suggest a different approach called runif.

Instead of using:

cbind(rnorm(40) * 2 + 13, rnorm(40) + 48)

You could be using:

cbind(runif(40,-10.8544921875,2.021484375),runif(40,49.82380908513249,59.478568831926395))

Main difference that you'll see is more scattered data, which is caused by the fact that ruinf creates randomly distributed data, while rnorm creates normally distributed data.

If you want normally distributed data, instead of using the scaling and offset tricks that the leaflet example uses, you could simply scale the output from rnorm between the values indicated in my formula.

Related Question