The misc API

pygridtools.misc.make_poly_coords(xarr, yarr, zpnt=None, triangles=False)[source]

Makes an array for coordinates suitable for building quadrilateral geometries in shapfiles via fiona.

Parameters:
xarr, yarr : numpy arrays

Arrays (2x2) of x coordinates and y coordinates for each vertex of the quadrilateral.

zpnt : optional float or None (default)

If provided, this elevation value will be assigned to all four vertices.

triangles : optional bool (default = False)

If True, triangles will be returned

Returns:
coords : numpy array

An array suitable for feeding into fiona as the geometry of a record.

pygridtools.misc.make_record(ID, coords, geomtype, props)[source]

Creates a record to be appended to a GIS file via geopandas.

Parameters:
ID : int

The record ID number

coords : tuple or array-like

The x-y coordinates of the geometry. For Points, just a tuple. An array or list of tuples for LineStrings or Polygons

geomtype : string

A valid GDAL/OGR geometry specification (e.g. LineString, Point, Polygon)

props : dict or collections.OrderedDict

A dict-like object defining the attributes of the record

Returns:
record : dict

A nested dictionary suitable for the a geopandas.GeoDataFrame,

Notes

This is ignore the mask of a MaskedArray. That might be bad.

pygridtools.misc.interpolate_bathymetry(bathy, x_points, y_points, xcol='x', ycol='y', zcol='z')[source]

Interpolates x-y-z point data onto the grid of a Gridgen object. Matplotlib’s nearest-neighbor interpolation schema is used to estimate the elevation at the grid centers.

Parameters:
bathy : pandas.DataFrame or None

The bathymetry data stored as x-y-z points in a DataFrame.

[x|y]_points : numpy arrays

The x, y locations onto which the bathymetry will be interpolated.

xcol/ycol/zcol : optional strings

Column names for each of the quantities defining the elevation pints. Defaults are “x/y/z”.

Returns:
gridbathy : pandas.DataFrame

The bathymetry for just the area covering the grid.

pygridtools.misc.padded_stack(a, b, how='vert', where='+', shift=0, padval=nan)[source]

Merge 2-dimensional numpy arrays with different shapes.

Parameters:
a, b : numpy arrays

The arrays to be merged

how : optional string (default = ‘vert’)

The method through wich the arrays should be stacked. ‘Vert’ is analogous to numpy.vstack. ‘Horiz’ maps to numpy.hstack.

where : optional string (default = ‘+’)

The placement of the arrays relative to each other. Keeping in mind that the origin of an array’s index is in the upper-left corner, ‘+’ indicates that the second array will be placed at higher index relative to the first array. Essentially:

  • if how == ‘vert’
    • ‘+’ -> a is above (higher index) b
    • ‘-‘ -> a is below (lower index) b
  • if how == ‘horiz’
    • ‘+’ -> a is to the left of b
    • ‘-‘ -> a is to the right of b

See the examples for more info.

shift : int (default = 0)

The number of indices the second array should be shifted in axis other than the one being merged. In other words, vertically stacked arrays can be shifted horizontally, and horizontally stacked arrays can be shifted vertically.

padval : optional, same type as array (default = numpy.nan)

Value with which the arrays will be padded.

Returns:
Stacked : numpy array

The merged and padded array

Examples

>>> import pygridtools as pgt
>>> a = numpy.arange(12).reshape(4, 3) * 1.0
>>> b = numpy.arange(8).reshape(2, 4) * -1.0
>>> pgt.padded_stack(a, b, how='vert', where='+', shift=2)
array([[ 0.,  1.,  2.,   -,   -,   -],
       [ 3.,  4.,  5.,   -,   -,   -],
       [ 6.,  7.,  8.,   -,   -,   -],
       [ 9., 10., 11.,   -,   -,   -],
       [  -,   -, -0., -1., -2., -3.],
       [  -,   -, -4., -5., -6., -7.]])
>>> pgt.padded_stack(a, b, how='h', where='-', shift=-1)
array([[-0., -1., -2., -3.,   -,   -,   -],
       [-4., -5., -6., -7.,  0.,  1.,  2.],
       [  -,   -,   -,   -,  3.,  4.,  5.],
       [  -,   -,   -,   -,  6.,  7.,  8.],
       [  -,   -,   -,   -,  9., 10., 11.]])
pygridtools.misc.padded_sum(padded, window=1)[source]
pygridtools.misc.mask_with_polygon(x, y, *polyverts, inside=True)[source]

Mask x-y arrays inside or outside a polygon

Parameters:
x, y : array-like

NxM arrays of x- and y-coordinates.

polyverts : sequence of a polygon’s vertices

A sequence of x-y pairs for each vertex of the polygon.

inside : bool (default is True)

Toggles masking the inside or outside the polygon

Returns:
mask : bool array

The NxM mask that can be applied to x and y.

pygridtools.misc.gdf_of_cells(X, Y, mask, crs, elev=None, triangles=False)[source]

Saves a GIS file of quadrilaterals representing grid cells.

Parameters:
X, Y : numpy (masked) arrays, same dimensions

Attributes of the gridgen object representing the x- and y-coords.

mask : numpy array or None

Array describing which cells to mask (exclude) from the output. Shape should be N-1 by M-1, where N and M are the dimensions of X and Y.

crs : string

A geopandas/proj/fiona-compatible string describing the coordinate reference system of the x/y values.

elev : optional array or None (defauly)

The elevation of the grid cells. Shape should be N-1 by M-1, where N and M are the dimensions of X and Y (like mask).

triangles : optional bool (default = False)

If True, triangles can be included

Returns:
geopandas.GeoDataFrame
pygridtools.misc.gdf_of_points(X, Y, crs, elev=None)[source]

Saves grid-related attributes of a pygridgen.Gridgen object to a GIS file with geomtype = ‘Point’.

Parameters:
X, Y : numpy (masked) arrays, same dimensions

Attributes of the gridgen object representing the x- and y-coords.

crs : string

A geopandas/proj/fiona-compatible string describing the coordinate reference system of the x/y values.

elev : optional array or None (defauly)

The elevation of the grid cells. Array dimensions must be 1 less than X and Y.

Returns:
geopandas.GeoDataFrame