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:
[ 0.66995387  3.94395169  8.09690139 12.96208419  3.00564564 10.0161943
  8.03486798  2.07401702 10.49453269  9.69497723 12.97050144  8.17608063
  5.52980382  7.94322856  7.2060831  10.68480467 12.04099588 10.18423581
  4.59661911  1.14625078  3.42488851 11.87737014 16.35371236 18.66219895
 11.43122765 10.73095817  0.32028798  4.54845283  5.60417225  5.02787599
  1.49476792  1.9313251   0.75448784  8.13843036 11.8706712  15.50785131
  7.12741106  6.40263943 13.6992685  15.21538158  2.3658221   5.59404551
  5.72075536 15.31846255 12.21070609  2.88068576  0.32083861  0.51966511
  1.95807932  2.60741765]

coordinate 1:
[-32.55537806 -43.01047202 -39.77240239 -40.62187394 -37.20569999
 -35.90876798 -35.34380798 -42.01335134 -36.42887583 -41.08622907
 -38.84480779 -33.16222285 -35.53814308 -43.05726996 -34.86975014
 -36.1085447  -34.98715186 -38.24234843 -42.34000605 -31.09848941
 -40.21284927 -31.30744387 -33.84694473 -37.9367792  -34.38314703
 -40.64119746 -35.02073583 -41.17359874 -44.8833103  -35.99353897
 -33.59971482 -34.70753451 -35.5713828  -34.56086929 -35.98567269
 -31.55363475 -44.68415555 -33.03224885 -34.93890092 -30.84774033
 -34.67413697 -43.98633065 -33.51617416 -39.85698651 -42.61962685
 -36.00130579 -31.81228465 -30.77636004 -35.23358296 -32.46320523]

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!