Converting seismic bin grids

A bin grid can be described by four corners, or by an origin, spacing and rotation. The first example checks and squares a four-corner grid; the second transforms every node of an already defined grid to WGS 84.

Checking, squaring and converting a four-corner grid

convert_bin_grid() is the computation behind the OSDU service’s POST v3/convertBinGrid. It checks four corners, optionally converts them to another CRS, fits the SDU rectangle through them there, reports how far the corners were from it, and gives the squared corners in WGS 84:

from geodetic_engine.bingrid import convert_bin_grid

corners = [  # (inline, crossline, easting, northing), in any order
    (1, 1000, 500000.0, 3000000.0),
    (1, 2000, 500000.0, 3100000.0),
    (101, 1000, 600000.0, 3000000.0),
    (101, 2000, 600000.0, 3100000.0),
]
result = convert_bin_grid(
    corners,
    "EPSG:32615",                  # WGS 84 / UTM zone 15N
    target_crs="EPSG:32064",       # NAD27 / BLM 14N (ftUS)
    operation="EPSG:15851",        # NAD27 to WGS 84 (79): the datum change, named
    wgs84_operation="EPSG:15851",
)
result.max_mislocation.dj_bins     # the corners are bent off a rectangle
0.3804250327589216
result.parameters                  # P6 parameters, anchored at squared corner A
P6Parameters(origin_i=1.0, origin_j=1000.0, origin_easting=3593536.461973964, origin_northing=9888463.879350627, bin_width_i=3282.0244517859605, bin_width_j=328.1999201372211, bearing_j=357.2135341924561, handedness=<Handedness.RIGHT: 'right'>, scale_factor=1.004708667810153, increment_i=1, increment_j=1)
result.squared_corners.coordinates     # corners of the fitted rectangle, in ftUS
array([[ 3593536.46197396,  9888463.87935063],
       [ 3577506.27840285, 10217819.30905921],
       [ 3922894.42561396,  9904494.18625168],
       [ 3906864.24204284, 10233849.61596026]])
result.converted_corners.coordinates   # the input corners converted, before squaring
array([[ 3593524.86063259,  9888579.9325561 ],
       [ 3577518.4042509 , 10217694.30737898],
       [ 3922906.0357305 ,  9904378.13369153],
       [ 3906852.10741962, 10233974.61699517]])
result.wgs84_corners               # ((lon, lat), ...) of the squared corners
((-92.99998151650868, 27.122150508865495),
 (-93.00001836105922, 28.02559868316208),
 (-91.99107323293299, 27.119164573393302),
 (-91.9827521204577, 28.021146674532318))
result.outline.labels              # counterclockwise
('A', 'C', 'D', 'B', 'A')
result.applied_operations()        # what was done, step by step, with EPSG codes
('Converted the corners from WGS 84 / UTM zone 15N to NAD27 / BLM 14N (ftUS) using Inverse of NAD27 to WGS 84 (79) [EPSG:15851]',
 'Squared up the bin grid in NAD27 / BLM 14N (ftUS): dI=0.00 inline numbers, dJ=0.38 crossline numbers',
 'Computed WGS 84 coordinates of the squared corners from NAD27 / BLM 14N (ftUS) using NAD27 to WGS 84 (79) [EPSG:15851]')

The conversion goes through Transformation, so it follows the same rules: a datum change needs a named operation or a bound CRS, ballpark results and missing grids are refused, an operation that reads the coordinate epoch needs coordinate_epoch, and both conversions are kept on the result as TransformationResult objects. A target CRS on the grid’s own map grid – the same datum, projection and linear unit, whatever its axis order and whether or not it is bound to WGS 84 – means no conversion; a bound target then names the operation the WGS 84 step applies, which is how an OSDU BoundProjected reference of the grid’s CRS is meant to be used. An EPSG CRS that declares northing first and its ESRI WKT, which declares easting first, are one map grid: values are in xy order whichever is given.

result.wgs84_outline visits the corners in the order of result.outline, each edge the short way round in longitude. For a grid across the antimeridian its longitudes continue past 180° (-179.9 becomes 180.1), so that the ring goes round the grid and not round the rest of the world; GeoJSON (RFC 7946) asks for such a polygon to be cut in two at the antimeridian. A ring round a pole closes along the pole’s latitude, through two points labelled pole.

Everything that cannot be a bin grid is refused with a specific error, all subclasses of BinGridError:

Error

Raised when

InvalidCornersError

There are not four corners, their numbers are not the four combinations of two inlines and two crosslines, or they are not whole node increments apart.

DegenerateBinGridError

Coordinates are not finite, corners coincide, or the outline A-B-D-C is not convex (swapped or collinear corners).

InvalidParameterError

A P6 parameter is out of range, for example a scale factor that is not positive.

UnsupportedCRSError

A CRS is not a 2D projected CRS with easting and northing axes in one linear unit, or k is to be derived from a projection that is not conformal at the grid. It is a geodetic_engine.geodesy.UnsupportedCRSError too.

derive_p6() and square_up() expose the fit itself. They take a BinGridCorners, which from_corners() labels from corners given in any order.

Seismic bin grid to WGS84

A project grid can be described by an origin, column and row spacings, dimensions, a rotation and a projected coordinate reference system (CRS). Convert its grid indices to map coordinates with P6 parameters, then transform those coordinates to WGS 84 longitude and latitude.

This example uses an early-bound OSDU CRS for a grid in the North Sea. The CRS contains its transformation to WGS 84, so the caller does not need to pass a separate transformation code.

Note

The official OSDU catalogue ships with the engine, but an OSDU CRS record ID needs a proj.db built from that catalogue and active on PROJ’s data path. See Custom database from OSDU for building and activating it. The standard PROJ database alone does not contain the OSDU record IDs.

Define the project grid

The origin and spacings below are in metres in an early-bound ED50 / UTM zone 31N CRS. Its OSDU record ID combines the projected CRS EPSG:23031 and the bound transformation EPSG:1613. The rotation is in degrees anticlockwise from the map’s easting axis to the direction of increasing columns.

import numpy as np

from geodetic_engine.bingrid import Handedness, P6Parameters
from geodetic_engine.geodesy import CoordinateReferenceSystem, Transformation

project = {
  "crs": "BoundProjected:EPSG::23031_EPSG::1613",
  "origin_easting": 470000.0,
  "origin_northing": 6680000.0,
  "column_spacing": 2500.0,
  "row_spacing": 2000.0,
  "ncol": 21,
  "nrow": 17,
  "rotation": 30.0,
}

Position the grid nodes

Columns are inline indices and rows are crossline indices, both starting at zero. This grid is right-handed: its row axis is 90 degrees anticlockwise from its column axis. P6 measures the J-axis bearing clockwise from north, so the bearing is the negative of the project grid’s rotation.

The spacings are distances on the map grid. Setting the P6 scale factor to 1 makes the P6 bin widths those same map distances; it does not assert that the projection’s physical scale factor is 1. Adjust the handedness and bearing if the project’s grid uses different axis conventions.

grid = P6Parameters(
  origin_i=0,
  origin_j=0,
  origin_easting=project["origin_easting"],
  origin_northing=project["origin_northing"],
  bin_width_i=project["column_spacing"],
  bin_width_j=project["row_spacing"],
  bearing_j=-project["rotation"],
  handedness=Handedness.RIGHT,
  scale_factor=1.0,
)
columns, rows = np.meshgrid(np.arange(project["ncol"]), np.arange(project["nrow"]))
nodes = grid.to_map(columns.ravel(), rows.ravel()).reshape(
  project["nrow"], project["ncol"], 2
)
print("Origin (easting, northing):", nodes[0, 0])
print("Next column:", nodes[0, 1])
print("Next row:", nodes[1, 0])
Origin (easting, northing): [ 470000. 6680000.]
Next column: [ 472165.06350946 6681250.        ]
Next row: [ 469000.         6681732.05080757]

The resulting array has shape (nrow, ncol, 2): the first index is the row, the second the column, and the final pair is easting and northing.

Transform to WGS 84

Resolve the project CRS by its record ID from the OSDU database. The ID must exist in the catalogue; the engine does not construct a CRS from its spelling. Its bound definition contains the transformation to WGS 84, which the engine reads and applies without a separate operation argument:

source_crs = CoordinateReferenceSystem.from_osdu_id(project["crs"])
print("Source:", source_crs.name)
print("Early-bound:", source_crs.crs.is_bound)

to_wgs84 = Transformation(source_crs, "EPSG:4326")
node_result = to_wgs84.transform(nodes[..., 0].ravel(), nodes[..., 1].ravel())
lonlat = node_result.coordinates.to_numpy().reshape(nodes.shape)
print("Operation:", node_result.operation.authority_code, node_result.operation.name)
print("Origin (longitude, latitude):", lonlat[0, 0])
Source: ED50 / UTM zone 31N
Early-bound: True
Operation: EPSG:1613 ED50 to WGS 84 (24)
Origin (longitude, latitude): [ 2.45634166 60.25365044]

lonlat[row, column] is the node’s longitude and latitude in degrees. The result reports the operation the CRS supplied; for this CRS it is EPSG:1613, ED50 to WGS 84 (24). The engine uses the operation already bound to the CRS, not an automatically guessed datum transformation. A late-bound CRS on a different datum requires an explicitly named operation; see Bound CRSs.

An OSDU persistableReference payload can also be passed directly to Transformation, or resolved with CoordinateReferenceSystem.from_user_input; it carries its own definition and does not require the OSDU database. See Reading, transforming and writing payloads. For large grids, reuse to_wgs84 and transform the flattened nodes in batches, preserving their order before reshaping the result.