• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

Ouranosinc / miranda / 2163816198

pending completion
2163816198

Pull #24

github

GitHub
Merge 94199f2bb into 9ac032fc5
Pull Request #24: Add CMIP file structure, use pyessv controlled vocabularies, and major refactoring

242 of 1140 new or added lines in 35 files covered. (21.23%)

13 existing lines in 4 files now uncovered.

735 of 3278 relevant lines covered (22.42%)

0.67 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

25.0
/miranda/gis/subset.py
1
import logging
3✔
2
import logging.config
3✔
3
from pathlib import Path
3✔
4
from typing import List, Optional, Tuple, Union
3✔
5

6
import fiona
3✔
7
import geojson
3✔
8
import numpy as np
3✔
9
import rasterio.crs
3✔
10

11
from miranda.scripting import LOGGING_CONFIG
3✔
12

13
logging.config.dictConfig(LOGGING_CONFIG)
3✔
14

15

16
__all__ = ["subsetting_domains"]
3✔
17

18

19
def subsetting_domains(domain: str) -> np.array:
3✔
20
    """Provides the bounding box coordinates for specific domains.
21
    Parameters
22
    ----------
23
    domain: {"global", "nam", "can", "qc", "mtl"}
24
    Returns
25
    -------
26
    np.array
27
      North, West, South, and East coordinates
28
    """
29
    region = None
×
30

31
    if domain.upper() == "GLOBAL":
×
32
        region = np.array([90.0, -180.0, -90.0, 180.0])
×
33
    elif domain.upper() in ["AMNO", "NAM"]:
×
NEW
34
        region = np.array([90.0, -179.9, 10.0, -10.0])
×
35
    elif domain.upper() == "CAN":
×
36
        region = np.array([83.5, -141.0, 41.5, -52.5])
×
37
    elif domain.upper() == "QC":
×
38
        region = np.array([63.0, -80.0, 44.5, -57.0])
×
39
    elif domain.upper() == "MTL":
×
40
        region = np.array([45.75, -74.05, 45.3, -73.4])
×
41
    if region is not None:
×
42
        return region
×
43

44
    raise NotImplementedError(domain)
×
45

46

47
def _read_geometries(
3✔
48
    shape: Union[str, Path], crs: Optional[Union[str, int, dict]] = None
49
) -> Tuple[List[geojson.geometry.Geometry], rasterio.crs.CRS]:
50
    """
51
    A decorator to perform a check to verify a geometry is valid.
52
    Returns the function with geom set to the shapely Shape object.
53
    """
54
    try:
×
55
        if shape is None:
×
56
            raise ValueError
×
57
    except (KeyError, ValueError):
×
58
        logging.exception("No shape provided.")
×
59
        raise
×
60

61
    geom = list()
×
62
    geometry_types = list()
×
63
    try:
×
64
        with fiona.open(shape) as fio:
×
65
            logging.info("Vector read OK.")
×
66
            if crs:
×
67
                shape_crs = rasterio.crs.CRS.from_user_input(crs)
×
68
            else:
69
                shape_crs = rasterio.crs.CRS(fio.crs or 4326)
×
70
            for i, feat in enumerate(fio):
×
71
                g = geojson.GeoJSON(feat)
×
72
                geom.append(g["geometry"])
×
73
                geometry_types.append(g["geometry"]["type"])
×
74
    except fiona.errors.DriverError:
×
75
        logging.exception("Unable to read shape.")
×
76
        raise
×
77

78
    if len(geom) > 0:
×
79
        logging.info(f"Shapes found are: {', '.join(set(geometry_types))}.")
×
80
        return geom, shape_crs
×
81
    raise RuntimeError("No geometries found.")
×
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2024 Coveralls, Inc