Skip to content

Commit

Permalink
make mapchete._validate public (#231)
Browse files Browse the repository at this point in the history
  • Loading branch information
ungarj committed Dec 2, 2019
1 parent 44c61bd commit e7c6bb7
Show file tree
Hide file tree
Showing 10 changed files with 37 additions and 24 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.rst
Expand Up @@ -7,6 +7,7 @@ Changelog
----
* don't raise exception when one of the registered processes cannot be imported (#225)
* don't close pool between zoom levels (#227)
* ``_validate`` module renamed to ``validate``

----
0.30
Expand Down
2 changes: 1 addition & 1 deletion mapchete/_core.py
Expand Up @@ -11,7 +11,7 @@
from mapchete._processing import _run_on_single_tile, _run_area, ProcessInfo, TileProcess
from mapchete.tile import count_tiles
from mapchete._timer import Timer
from mapchete._validate import validate_tile, validate_zooms
from mapchete.validate import validate_tile, validate_zooms

logger = logging.getLogger(__name__)

Expand Down
2 changes: 1 addition & 1 deletion mapchete/_processing.py
Expand Up @@ -13,7 +13,7 @@
from mapchete.errors import MapcheteNodataTile, MapcheteProcessException
from mapchete.io import raster
from mapchete._timer import Timer
from mapchete._validate import deprecated_kwargs
from mapchete.validate import deprecated_kwargs

logger = logging.getLogger(__name__)

Expand Down
2 changes: 1 addition & 1 deletion mapchete/cli/default/convert.py
Expand Up @@ -20,7 +20,7 @@
from mapchete.io import read_json, get_best_zoom_level
from mapchete.io.vector import reproject_geometry
from mapchete.tile import BufferedTilePyramid
from mapchete._validate import validate_zooms
from mapchete.validate import validate_zooms

logger = logging.getLogger(__name__)
OUTPUT_FORMATS = available_output_formats()
Expand Down
2 changes: 1 addition & 1 deletion mapchete/cli/utils.py
Expand Up @@ -12,7 +12,7 @@
from mapchete.formats import available_output_formats
from mapchete.index import zoom_index_gen
from mapchete.log import set_log_level, setup_logfile
from mapchete._validate import validate_bounds, validate_zooms
from mapchete.validate import validate_bounds, validate_zooms


logger = logging.getLogger(__name__)
Expand Down
2 changes: 1 addition & 1 deletion mapchete/config.py
Expand Up @@ -27,7 +27,7 @@
from tilematrix._funcs import Bounds
import warnings

from mapchete._validate import (
from mapchete.validate import (
validate_bounds, validate_zooms, validate_values, validate_bufferedtilepyramid
)
from mapchete.errors import (
Expand Down
2 changes: 1 addition & 1 deletion mapchete/formats/default/gtiff.py
Expand Up @@ -52,7 +52,7 @@
extract_from_array, read_raster_window
)
from mapchete.tile import BufferedTile
from mapchete._validate import deprecated_kwargs
from mapchete.validate import deprecated_kwargs


logger = logging.getLogger(__name__)
Expand Down
2 changes: 1 addition & 1 deletion mapchete/io/_geometry_operations.py
Expand Up @@ -8,7 +8,7 @@
from shapely.validation import explain_validity

from mapchete.errors import GeometryTypeError
from mapchete._validate import validate_crs
from mapchete.validate import validate_crs


CRS_BOUNDS = {
Expand Down
16 changes: 4 additions & 12 deletions mapchete/io/raster.py
Expand Up @@ -21,6 +21,7 @@

from mapchete.tile import BufferedTile
from mapchete.io import path_is_remote, get_gdal_options, path_exists
from mapchete.validate import validate_write_window_params


logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -360,7 +361,7 @@ def __init__(
):
"""Prepare data & profile."""
out_tile = out_tile or in_tile
_validate_write_window_params(in_tile, out_tile, in_data, out_profile)
validate_write_window_params(in_tile, out_tile, in_data, out_profile)
self.data = extract_from_array(
in_raster=in_data,
in_affine=in_tile.affine,
Expand Down Expand Up @@ -415,7 +416,7 @@ def write_raster_window(
"Please use RasterWindowMemoryFile."
)
out_tile = out_tile or in_tile
_validate_write_window_params(in_tile, out_tile, in_data, out_profile)
validate_write_window_params(in_tile, out_tile, in_data, out_profile)

# extract data
window_data = extract_from_array(
Expand Down Expand Up @@ -468,15 +469,6 @@ def _write_tags(dst, tags):
dst.update_tags(**{k: v})


def _validate_write_window_params(in_tile, out_tile, in_data, out_profile):
if any([not isinstance(t, BufferedTile) for t in [in_tile, out_tile]]):
raise TypeError("in_tile and out_tile must be BufferedTile")
if not isinstance(in_data, ma.MaskedArray):
raise TypeError("in_data must be ma.MaskedArray")
if not isinstance(out_profile, dict):
raise TypeError("out_profile must be a dictionary")


def extract_from_array(in_raster=None, in_affine=None, out_tile=None):
"""
Extract raster data window array.
Expand Down Expand Up @@ -766,7 +758,7 @@ def _shift_required(tiles):
else:
# look at column gaps and try to determine the smallest distance
def gen_groups(items):
"""Groups tile columns by sequence."""
"""Group tile columns by sequence."""
j = items[0]
group = [j]
for i in items[1:]:
Expand Down
30 changes: 25 additions & 5 deletions mapchete/_validate.py → mapchete/validate.py
@@ -1,3 +1,7 @@
"""Convenience validator functions for core and extension packages."""


import numpy.ma as ma
from rasterio.crs import CRS
from tilematrix._funcs import Bounds
import warnings
Expand All @@ -22,6 +26,8 @@ def validate_zooms(zooms, expand=True):
Parameters
----------
zoom : dict, int or list
expand : bool
Return full list of zoom levels instead of [min, max]
Returns
-------
Expand Down Expand Up @@ -49,6 +55,8 @@ def validate_zooms(zooms, expand=True):

def validate_zoom(zoom):
"""
Return validated zoom.
Assert zoom value is positive integer.
Returns
Expand All @@ -66,6 +74,8 @@ def validate_zoom(zoom):

def validate_bounds(bounds):
"""
Return validated bounds.
Bounds must be a list or tuple with exactly four elements.
Parameters
Expand All @@ -89,7 +99,7 @@ def validate_bounds(bounds):

def validate_values(config, values):
"""
Validate whether value is found in config and has the right type.
Return True if all values are given and have the desired type.
Parameters
----------
Expand Down Expand Up @@ -118,7 +128,7 @@ def validate_values(config, values):

def validate_tile(tile, pyramid):
"""
Validate tile and return BufferedTile object
Return BufferedTile object.
Parameters
----------
Expand All @@ -144,7 +154,7 @@ def validate_tile(tile, pyramid):

def validate_bufferedtilepyramid(pyramid):
"""
Pyramid is BufferedTilePyramid.
Return BufferedTilePyramid.
Parameters
----------
Expand All @@ -166,7 +176,7 @@ def validate_bufferedtilepyramid(pyramid):

def validate_crs(crs):
"""
Validate crs is rasterio.crs.CRS.
Return crs as rasterio.crs.CRS.
Parameters
----------
Expand All @@ -192,12 +202,22 @@ def validate_crs(crs):
raise TypeError("invalid CRS given")


def validate_write_window_params(in_tile, out_tile, in_data, out_profile):
"""Raise Exception if write window parameters are invalid."""
if any([not isinstance(t, BufferedTile) for t in [in_tile, out_tile]]):
raise TypeError("in_tile and out_tile must be BufferedTile")
if not isinstance(in_data, ma.MaskedArray):
raise TypeError("in_data must be ma.MaskedArray")
if not isinstance(out_profile, dict):
raise TypeError("out_profile must be a dictionary")


##############
# decorators #
##############

def deprecated_kwargs(func):
"""Decorator for open() functions warning of keyword argument usage"""
"""Decorator for open() functions warning of keyword argument usage."""
def func_wrapper(*args, **kwargs):
if kwargs:
warnings.warn(
Expand Down

0 comments on commit e7c6bb7

Please sign in to comment.