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:
[ 2.4177089  17.74342804  9.55563228 10.74277248 15.7755092   1.32435981
 16.57396995 11.22093132 17.40637961  3.43774554  4.86803598 15.34549823
  7.45777139 16.31054535 17.66321699 18.23596982  1.1997522   4.20189414
  7.52260573 14.41083633  5.02559198 17.67133417  3.67975034  7.85225183
  5.4750676   5.53694011  4.45843255  7.72575762 16.62167734 16.81132598
  2.05078991 18.96500733 18.40965507 12.02260703  6.95380244  2.70549274
  5.22334409 17.64541474  7.76047918 19.02649633  4.33588181  6.06666406
  9.15191572 13.79301972 12.57686097  2.62571048 13.23317151  7.01466407
 13.72643067 13.90672214]

coordinate 1:
[-32.5334006  -44.15704028 -40.79884142 -44.13201605 -31.99843545
 -31.88456309 -37.75362223 -41.70915329 -36.37714232 -37.4072767
 -34.28220966 -36.00130321 -39.34689818 -39.40957691 -36.75172356
 -38.34885382 -34.68764443 -32.12425969 -42.93363522 -33.19255232
 -35.32222508 -41.8679883  -39.67829282 -40.98271    -42.90486758
 -32.10328692 -39.42230833 -30.74977957 -41.35998566 -44.42355542
 -44.81609647 -30.32927369 -32.33461035 -39.32426537 -38.37506386
 -43.55622172 -43.04457915 -42.79465612 -43.08133598 -34.62966287
 -42.48481965 -32.16834341 -41.17074573 -33.28399805 -42.80766203
 -33.13199631 -35.59923787 -33.21549132 -31.64017605 -37.95781079]

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!