4. Random coordinates#

Sometimes, we need to generate a random spread of points in 1 or more dimensions. This can be used for random sampling methods or simply to generate some synthetic data on which to test new methods. Generating uniformly distributed values using numpy.random is great but doing so in more than 1 dimension involves some boilerplate code that could be automated. Bordado offers functions random_coordinates and random_coordinates_spherical for this purpose. This is how they work.

import bordado as bd
import matplotlib.pyplot as plt
import pygmt

4.1. Generating random points#

To generate uniformly distributed random 2D point coordinates, we use bordado.random_coordinates like so:

region = (0, 20, -45, -30)
coordinates = bd.random_coordinates(region, size=50)

for i, c in enumerate(coordinates):
    print(f"coordinate {i}:\n{c}\n")
coordinate 0:
[13.851226   18.86743456  6.85254235  9.14749292  9.68360909 19.85369042
  5.9218079  16.6867765  18.59367959  4.26228055  3.5345776   1.12814685
  1.50061692 18.00092064 11.47983782 19.27486169  1.48931837 19.94298981
 14.24559762 11.43167914 19.28129286  7.56621536  5.35130853 11.88925283
  4.2632096  11.71852816 12.65592026 15.87145432  7.57766832  4.03153395
 11.88951223  6.86000908  2.00523894 12.44232023  3.09518088 13.94661678
 15.41193917 11.67185943  6.02252383 15.44341424 10.06382953 13.26207668
 16.72327948  5.3092205  14.58333869 16.17917352 16.60243498 14.63832515
  0.52413416 11.2189477 ]

coordinate 1:
[-40.97179594 -43.85841046 -41.56631612 -31.67582517 -37.65570828
 -33.18761227 -39.3540294  -42.39275302 -38.7481797  -43.19237284
 -31.4486356  -30.88446694 -39.84591223 -43.32353931 -33.35287113
 -37.26163277 -35.23589831 -42.92775428 -31.09350863 -33.91621944
 -38.23222999 -30.69931607 -32.53240422 -32.03809986 -38.32179326
 -44.53961352 -40.6111465  -36.03539316 -41.87612915 -41.76068501
 -30.54388006 -34.09750465 -31.91848029 -44.75365054 -30.33248722
 -31.54693832 -30.46910842 -32.45253883 -32.47195956 -37.21321012
 -44.80484603 -36.92709486 -42.02248324 -32.30866156 -40.41264249
 -33.15805139 -40.29826462 -30.98783973 -44.17286509 -43.13119917]

Since the region has 4 elements, it is assumed that there are two dimensions in our problem and so 2 coordinates arrays are generated. Because the points are uniformly distributed, there is only a size argument and no spacing like in bordado.grid_coordinates or bordado.line_coordinates.

Hint

The values above will be different every time we run the function because it will instantiate a new numpy.random.default_rng every time. We can control the randomness by passing a random_seed argument. This can be an integer seed or a numpy.random.Generator.

Let’s plot these points to see their distribution:

fig, ax = plt.subplots(1, 1, figsize=(8, 6), layout="constrained")
ax.plot(*coordinates, "o")
ax.set_aspect("equal")
ax.set_xlim(*region[:2])
ax.set_ylim(*region[2:])
ax.set_xlabel("Easting")
ax.set_ylabel("Northing")
ax.grid()
plt.show()
../_images/random_2_0.png

As expected, they are random and with no bias inside the given region (hence, uniformly distributed).

4.2. Random points on the sphere#

The method above is useful, but it shouldn’t be used with geographic longitude and latitude coordinates. Let’s illustrate why with an example. We’ll generate random points distributed globally:

region = (0, 360, -90, 90)  # W, E, S, N in degrees
size = 3000

coordinates = bd.random_coordinates(region, size)

fig, ax = plt.subplots(1, 1, figsize=(8, 6), layout="constrained")
ax.plot(*coordinates, ".")
ax.set_aspect("equal")
ax.set_xlim(*region[:2])
ax.set_ylim(*region[2:])
ax.set_xlabel("Longitude")
ax.set_ylabel("Latitude")
ax.grid()
plt.show()
../_images/random_3_0.png

Plotted like this, things seem fine. But this type of plot can be very misleading for geographic data. If I plot them on a map using a cartographic projection, a pattern will appear:

fig = pygmt.Figure()
fig.coast(land="#cccccc", region="g", projection="W20c", frame="afg")
fig.plot(x=coordinates[0], y=coordinates[1], style="c0.1c", fill="seagreen")
fig.show()
../_images/random_4_0.png

Now we can see that the concentration of points is larger at the poles than at the equator. This is because the meridians of longitude converge at the poles. Hence, a degree of longitude corresponds to a smaller and smaller physical size on the surface of the Earth towards the poles.

See also

The map above was generated with a Mollweide projection, which is an equal-area projection.

To generate random points on a sphere that have a uniform distribution, we can use bordado.random_coordinates_spherical, which accounts for this convergence of meridians:

coordinates_sphere = bd.random_coordinates_spherical(region, size)

fig = pygmt.Figure()
fig.coast(land="#cccccc", region="g", projection="W20c", frame="afg")
fig.plot(x=coordinates_sphere[0], y=coordinates_sphere[1], style="c0.1c", fill="seagreen")
fig.show()
../_images/random_5_0.png

Notice that now the point concentration is uniform on the Earth.

4.3. What’s next#

Now that we can make some synthetic data with random points, let’s see how we can split and segment the data based on spatial blocks and windows in “Splitting points into blocks”.

Have questions?

Please ask on any of the Fatiando a Terra community channels!