4

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.

5

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.

  • 1
    The problem is that the points will not actually be uniformly distributed, due to the gross distortions in the Plate Carree projection you have implicitly adopted--especially in northerly regions like England. For a truly uniform distribution, project the data using an equal area projection, generate the points, then (if needed) unproject them. There is an excellent approximation that avoids the project-unproject cycle: generate uniform longitudes and uniform cosines of the latitudes and apply the inverse cosine to those. (This is an equal-area cylindrical projection of the sphere.) – whuber Apr 11 '17 at 23:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.