def test_snap_bounds_to_zoom(): bounds = (-180, -90, -60, -30) for pixelbuffer in [0, 5, 10]: for metatiling in [1, 2, 4]: pyramid = BufferedTilePyramid("geodetic", pixelbuffer=pixelbuffer, metatiling=metatiling) for zoom in range(3, 5): snapped_bounds = mapchete.config.snap_bounds(bounds=bounds, pyramid=pyramid, zoom=zoom) control_bounds = unary_union([ t.bbox for t in pyramid.tiles_from_bounds(bounds, zoom) ]).bounds assert snapped_bounds == control_bounds
def test_read_raster_window_resampling(cleantopo_br_tif): """Assert various resampling options work.""" tp = BufferedTilePyramid("geodetic") with rasterio.open(cleantopo_br_tif, "r") as src: tiles = tp.tiles_from_bounds(src.bounds, 4) for tile in tiles: outputs = [ read_raster_window(cleantopo_br_tif, tile, resampling=resampling) for resampling in [ "nearest", "bilinear", "cubic", "cubic_spline", "lanczos", "average", "mode" ] ] # resampling test: assert any([ not np.array_equal(w, v) for v, w in zip(outputs[:-1], outputs[1:]) ])
class InputData(base.InputData): """ Main input class. Parameters ---------- input_params : dictionary driver specific parameters Attributes ---------- path : string path to Mapchete file pixelbuffer : integer buffer around output tiles pyramid : ``tilematrix.TilePyramid`` output ``TilePyramid`` crs : ``rasterio.crs.CRS`` object describing the process coordinate reference system srid : string spatial reference ID of CRS (e.g. "{'init': 'epsg:4326'}") """ METADATA = METADATA def __init__(self, input_params, **kwargs): """Initialize.""" super(InputData, self).__init__(input_params, **kwargs) if "abstract" in input_params: self._params = input_params["abstract"] self.path = absolute_path(path=self._params["path"], base_dir=input_params["conf_dir"]) # define pyramid self.td_pyramid = BufferedTilePyramid( self._params["type"], metatiling=self._params.get("metatiling", 1), tile_size=self._params.get("tile_size", 256), pixelbuffer=self._params.get("pixelbuffer", 0)) elif "path" in input_params: self.path = absolute_path(path=input_params["path"], base_dir=input_params.get("conf_dir")) try: params = read_json(os.path.join(self.path, "metadata.json")) except FileNotFoundError: raise MapcheteConfigError("Cannot find metadata.json in %s" % input_params["path"]) # define pyramid self.td_pyramid = BufferedTilePyramid( params["pyramid"]["grid"]["type"], metatiling=params["pyramid"].get("metatiling", 1), tile_size=params["pyramid"].get("tile_size", 256), pixelbuffer=params["pyramid"].get("pixelbuffer", 0)) output = load_output_writer(dict( params["driver"], metatiling=self.td_pyramid.metatiling, pixelbuffer=self.td_pyramid.pixelbuffer, pyramid=self.td_pyramid, type=self.td_pyramid.type, path=self.path), readonly=True) logger.debug(output) self._params = dict( path=self.path, type=params["pyramid"]["grid"]["type"], metatiling=params["pyramid"].get("metatiling", 1), pixelbuffer=params["pyramid"].get("pixelbuffer", 0), tile_size=params["pyramid"].get("tile_size", 256), extension=output.file_extension.split(".")[-1], **params["driver"]) # validate parameters validate_values(self._params, [("path", six.string_types), ("type", six.string_types), ("extension", six.string_types)]) if not self._params["extension"] in [ "tif", "vrt", "png", "jpg", "mixed", "jp2", "geojson" ]: raise MapcheteConfigError("invalid file extension given: %s" % self._params["extension"]) self._ext = self._params["extension"] # additional params self._bounds = self._params.get("bounds", self.td_pyramid.bounds) self._file_type = ("vector" if self._params["extension"] == "geojson" else "raster") if self._file_type == "raster": self._params["count"] = self._params.get( "count", self._params.get("bands", None)) validate_values(self._params, [("dtype", six.string_types), ("count", int)]) self._profile = { "nodata": self._params.get("nodata", 0), "dtype": self._params["dtype"], "count": self._params["count"] } else: self._profile = None def open(self, tile, **kwargs): """ Return InputTile object. Parameters ---------- tile : ``Tile`` Returns ------- input tile : ``InputTile`` tile view of input data """ return InputTile( tile, tiles_paths=[(_tile, _path) for _tile, _path in [( t, os.path.join(*( [self.path, str(t.zoom), str(t.row), str(t.col)])) + "." + self._ext) for t in self.td_pyramid.tiles_from_bounds( tile.bounds, tile.zoom)] if path_exists(_path)], file_type=self._file_type, profile=self._profile, **kwargs) def bbox(self, out_crs=None): """ Return data bounding box. Parameters ---------- out_crs : ``rasterio.crs.CRS`` rasterio CRS object (default: CRS of process pyramid) Returns ------- bounding box : geometry Shapely geometry object """ return reproject_geometry( box(*self._bounds), src_crs=self.td_pyramid.crs, dst_crs=self.pyramid.crs if out_crs is None else out_crs)