safe.gis.interpolation2d module

Module for 2D interpolation over a rectangular mesh

This module:

  • provides piecewise constant (nearest neighbour) and bilinear interpolation
  • is fast (based on numpy vector operations)
  • depends only on numpy
  • guarantees that interpolated values never exceed the four nearest neighbours
  • handles missing values in domain sensibly using NaN
  • is unit tested with a range of common and corner cases

See end of this file for documentation of the mathematical derivation used.

safe.gis.interpolation2d.interpolate2d(x, y, z, points, mode='linear', bounds_error=False)[source]

Fundamental 2D interpolation routine

Parameters:
  • x (numpy.ndarray) – 1D array of x-coordinates of the mesh on which to interpolate
  • y (numpy.ndarray) – 1D array of y-coordinates of the mesh on which to interpolate
  • z (numpy.ndarry) – 2D array of values for each x, y pair
  • points (numpy.narray) – Nx2 array of coordinates where interpolated values are sought
  • mode (str) –

    Determines the interpolation order. Options are:

    • ‘constant’ - piecewise constant nearest neighbour interpolation
    • ‘linear’ - bilinear interpolation using the four nearest neighbours (default)
  • bounds_error (bool) – If True (default) a BoundsError exception will be raised when interpolated values are requested outside the domain of the input data. If False, nan is returned for those values
Returns:

1D array with same length as points with interpolated values

Raises:

Exception, BoundsError (see note about bounds_error)

..notes::

Input coordinates x and y are assumed to be monotonically increasing, but need not be equidistantly spaced. No such assumption regarding ordering of points is made.

z is assumed to have dimension M x N, where M = len(x) and N = len(y). In other words it is assumed that the x values follow the first (vertical) axis downwards and y values the second (horizontal) axis from left to right.

If this routine is to be used for interpolation of raster grids where data is typically organised with longitudes (x) going from left to right and latitudes (y) from left to right then user interpolate_raster in this module

safe.gis.interpolation2d.interpolate_raster(x, y, z, points, mode='linear', bounds_error=False)[source]

2D interpolation of raster data

It is assumed that data is organised in matrix z as latitudes from bottom up along the first dimension and longitudes from west to east along the second dimension.

Further it is assumed that x is the vector of longitudes and y the vector of latitudes.

See interpolate2d for details of the interpolation routine

Parameters:
  • x (numpy.ndarray) – 1D array of x-coordinates of the mesh on which to interpolate
  • y (numpy.ndarray) – 1D array of y-coordinates of the mesh on which to interpolate
  • z (numpy.ndarry) – 2D array of values for each x, y pair
  • points (numpy.narray) – Nx2 array of coordinates where interpolated values are sought
  • mode (str) –

    Determines the interpolation order. Options are:

    • ‘constant’ - piecewise constant nearest neighbour interpolation
    • ‘linear’ - bilinear interpolation using the four nearest neighbours (default)
  • bounds_error (bool) – If True (default) a BoundsError exception will be raised when interpolated values are requested outside the domain of the input data. If False, nan is returned for those values
Returns:

1D array with same length as points with interpolated values

Raises:

Exception, BoundsError (see note about bounds_error)