Skip to content

Commit

Permalink
FIX: Sanitize imports
Browse files Browse the repository at this point in the history
  • Loading branch information
remi-braun committed Mar 26, 2024
1 parent 856a263 commit 4fe5f76
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 49 deletions.
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
- FIX: Fix `geometry.nearest_neighbors` when k is bigger than the number of candidates
- FIX: Add a `buffer_on_input` in `geometry.intersects` to manage edge cases when points on polygons boundary aren't seen as intersecting
- FIX: `rasters.read` accepts `xarray` objects as input
- FIX: Sanitize imports
- DOC: Update some examples in documentation

## 1.36.0 (2024-02-27)
Expand Down
19 changes: 6 additions & 13 deletions sertit/geometry.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,24 +21,17 @@
"""
import logging

import geopandas as gpd
import numpy as np
import shapely
from shapely import ops
from shapely.errors import GeometryTypeError
from shapely.geometry import Polygon, box
from tqdm import tqdm

from sertit.types import AnyPolygonType

try:
import geopandas as gpd
import shapely
from shapely import ops
from shapely.errors import GeometryTypeError
from shapely.geometry import Polygon, box
except ModuleNotFoundError as ex:
raise ModuleNotFoundError(
"Please install 'geopandas' and 'shapely' to use the geometry package."
) from ex

from sertit import vectors
from sertit.logs import SU_NAME
from sertit.types import AnyPolygonType

LOGGER = logging.getLogger(SU_NAME)

Expand Down
36 changes: 18 additions & 18 deletions sertit/rasters.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,32 +23,28 @@
from functools import wraps
from typing import Any, Callable, Optional, Union

import geopandas as gpd
import numpy as np
import xarray
from rioxarray.exceptions import MissingCRS

from sertit.logs import SU_NAME
from sertit.rasters_rio import MAX_CORES, PATH_ARR_DS, bigtiff_value, get_window
from sertit.types import AnyPathStrType, AnyPathType, AnyXrDataStructure
import xarray as xr
from shapely.geometry import Polygon

try:
import geopandas as gpd
import rasterio
import rioxarray
import xarray as xr
from rasterio import features
from rasterio.enums import Resampling
from shapely.geometry import Polygon
from rioxarray.exceptions import MissingCRS
except ModuleNotFoundError as ex:
raise ModuleNotFoundError(
"Please install 'rioxarray' and 'geopandas' to use the 'rasters' package."
"Please install 'rioxarray' to use the 'rasters' package."
) from ex

from sertit import geometry, logs, path, rasters_rio, vectors
from sertit.types import AnyPathStrType, AnyPathType, AnyXrDataStructure

MAX_CORES = MAX_CORES
MAX_CORES = rasters_rio.MAX_CORES
PATH_XARR_DS = Union[str, AnyXrDataStructure, rasterio.DatasetReader]
LOGGER = logging.getLogger(SU_NAME)
LOGGER = logging.getLogger(logs.SU_NAME)

# NODATAs
UINT8_NODATA = rasters_rio.UINT8_NODATA
Expand Down Expand Up @@ -233,7 +229,9 @@ def path_or_arr_or_dst_wrapper(
from rasterio import MemoryFile

with MemoryFile() as memfile:
with memfile.open(**meta, BIGTIFF=bigtiff_value(arr)) as ds:
with memfile.open(
**meta, BIGTIFF=rasters_rio.bigtiff_value(arr)
) as ds:
ds.write(arr)
out = function(ds, *args, **kwargs)
else:
Expand Down Expand Up @@ -536,7 +534,9 @@ def get_valid_vector(xds: PATH_XARR_DS, default_nodata: int = 0) -> gpd.GeoDataF


@path_xarr_dst
def get_nodata_vector(ds: PATH_ARR_DS, default_nodata: int = 0) -> gpd.GeoDataFrame:
def get_nodata_vector(
ds: rasters_rio.PATH_ARR_DS, default_nodata: int = 0
) -> gpd.GeoDataFrame:
"""
Get the nodata vector of a raster as a vector.
Expand Down Expand Up @@ -747,7 +747,7 @@ def crop(

@path_arr_dst
def read(
ds: PATH_ARR_DS,
ds: rasters_rio.PATH_ARR_DS,
resolution: Union[tuple, list, float] = None,
size: Union[tuple, list] = None,
window: Any = None,
Expand Down Expand Up @@ -810,15 +810,15 @@ def read(
"""
if window is not None:
window = get_window(ds, window)
window = rasters_rio.get_window(ds, window)

# Get new height and width
new_height, new_width, do_resampling = rasters_rio.get_new_shape(
ds, resolution, size, window
)

# Read data (and load it to discard lock)
with xarray.set_options(keep_attrs=True):
with xr.set_options(keep_attrs=True):
with rioxarray.set_options(export_grid_mapping=False):
with rioxarray.open_rasterio(
ds, default_name=path.get_filename(ds.name), chunks=chunks, **kwargs
Expand Down Expand Up @@ -966,7 +966,7 @@ def write(
kwargs["predictor"] = "2"

# Bigtiff if needed
bigtiff = bigtiff_value(xds)
bigtiff = rasters_rio.bigtiff_value(xds)

# Manage tiles
if "tiled" not in kwargs:
Expand Down
15 changes: 7 additions & 8 deletions sertit/rasters_rio.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,29 +25,28 @@
from functools import wraps
from typing import Any, Callable, Optional, Union

import geopandas as gpd
import numpy as np
from rasterio.vrt import WarpedVRT
from rasterio.windows import Window, from_bounds

from sertit.logs import SU_NAME
from sertit.types import AnyNumpyArray, AnyPathStrType, AnyPathType
from shapely.geometry import Polygon

try:
import geopandas as gpd
import rasterio
from rasterio import MemoryFile, features
from rasterio import mask as rio_mask
from rasterio import merge
from rasterio import shutil as rio_shutil
from rasterio import warp
from rasterio.enums import Resampling
from shapely.geometry import Polygon
from rasterio.vrt import WarpedVRT
from rasterio.windows import Window, from_bounds
except ModuleNotFoundError as ex:
raise ModuleNotFoundError(
"Please install 'rasterio' and 'geopandas' to use the 'rasters_rio' package."
"Please install 'rasterio' to use the 'rasters_rio' package."
) from ex

from sertit import AnyPath, geometry, logs, misc, path, strings, vectors, xml
from sertit.logs import SU_NAME
from sertit.types import AnyNumpyArray, AnyPathStrType, AnyPathType

np.seterr(divide="ignore", invalid="ignore")

Expand Down
13 changes: 3 additions & 10 deletions sertit/vectors.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,24 +29,17 @@
from contextlib import contextmanager
from typing import Any, Generator, Union

import geopandas as gpd
import numpy as np
import pandas as pd
from cloudpathlib.exceptions import AnyPathTypeError
from fiona._err import CPLE_AppDefinedError
from fiona.errors import DriverError, UnsupportedGeometryTypeError
from shapely import Polygon, wkt

from sertit import AnyPath, files, geometry, logs, misc, path, strings
from sertit.types import AnyPathStrType, AnyPathType

try:
import geopandas as gpd
from shapely import Polygon, wkt
except ModuleNotFoundError as ex:
raise ModuleNotFoundError(
"Please install 'geopandas' to use the rasters package."
) from ex

from sertit.logs import SU_NAME
from sertit.types import AnyPathStrType, AnyPathType

LOGGER = logging.getLogger(SU_NAME)

Expand Down

0 comments on commit 4fe5f76

Please sign in to comment.