Skip to content

Commit

Permalink
add align function to raster.misc
Browse files Browse the repository at this point in the history
function aligns a grid to a specified bounding box, using interpolation
  • Loading branch information
njwilson23 committed Oct 4, 2016
1 parent 80038b7 commit a0dbddd
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions karta/raster/misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import numpy as np
from .grid import RegularGrid
from .coordgen import CoordinateGenerator

def _slope(D, res=(1.0, 1.0)):
""" Return the scalar slope at each pixel using the neighbourhood method.
Expand Down Expand Up @@ -204,3 +205,36 @@ def hillshade(grid, azimuth=330.0, elevation=60.0, band=0):

return RegularGrid(grid.transform, values=dprod, crs=grid.crs)

def align(grid, bbox, resolution=None, skew=None, method="bilinear"):
""" Align a grid to a bounding box.
Parameters
----------
grid : RegularGrid
Grid instance to realign.
bbox : tuple
Bounding box (xmin, ymin, xmax, ymax) of the output grid
resolution : tuple, optional
Resolution of the output grid. Default is the same as the input grid.
skew : tuple, optional
Skew of the output grid. Default is the same as the input grid.
method : str, optional
Method to use to resample grid. See `RegularGrid.sample`. Default is
'bilinear'.
Returns
-------
RegularGrid
"""
if resolution is None:
resolution = grid.resolution
if skew is None:
skew = grid.skew
T = (bbox[0], bbox[1], resolution[0], resolution[1], skew[0], skew[1])
nx = int(np.round((bbox[2]-bbox[0]) / resolution[0]))
ny = int(np.round((bbox[3]-bbox[1]) / resolution[1]))
cg = CoordinateGenerator(T, (ny, nx))
x, y = cg[:,:]
v = grid.sample(x.ravel(), y.ravel(), method=method).reshape(ny, nx)
return karta.RegularGrid(T, values=v, crs=grid.crs)

0 comments on commit a0dbddd

Please sign in to comment.