Skip to content

Commit

Permalink
unpinned xarray and updated code for new versions of xarray and geopa…
Browse files Browse the repository at this point in the history
…ndas (#1020)
  • Loading branch information
Tjalling-dejong authored Jul 5, 2024
1 parent 5eb0cc3 commit cd6d2b8
Show file tree
Hide file tree
Showing 7 changed files with 11,177 additions and 10,746 deletions.
3 changes: 2 additions & 1 deletion hydromt/gis_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
# -*- coding: utf-8 -*-

"""GIS related convenience functions."""

from __future__ import annotations

import glob
Expand Down Expand Up @@ -204,7 +205,7 @@ def filter_gdf(gdf, geom=None, bbox=None, crs=None, predicate="intersects"):
elif gdf.crs is not None and geom.crs != gdf.crs:
geom = geom.to_crs(gdf.crs)
# convert geopandas to geometry
geom = geom.unary_union
geom = geom.union_all()
idx = np.sort(gdf.sindex.query(geom, predicate=predicate))
return idx

Expand Down
7 changes: 5 additions & 2 deletions hydromt/io.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Implementations for all of pythe necessary IO for HydroMT."""

import glob
import io as pyio
import logging
Expand Down Expand Up @@ -631,9 +632,11 @@ def open_vector(
bbox_reader = gis_utils.bbox_from_file_and_filters(
str(fn), bbox_shapely, geom, crs
)
gdf = read_dataframe(str(fn), bbox=bbox_reader, mode=mode, **kwargs)
gdf = read_dataframe(str(fn), bbox=bbox_reader, **kwargs)
else:
gdf = gpd.read_file(str(fn), bbox=bbox, mask=geom, mode=mode, **kwargs)
if isinstance(bbox, list):
bbox = tuple(bbox)
gdf = gpd.read_file(str(fn), bbox=bbox, mask=geom, **kwargs)

# check geometry type
if assert_gtype is not None:
Expand Down
3 changes: 1 addition & 2 deletions hydromt/models/model_grid.py
Original file line number Diff line number Diff line change
Expand Up @@ -439,7 +439,6 @@ def write_grid(


class GridModel(GridMixin, Model):

"""Model class Grid Model for gridded models in HydroMT."""

_CLI_ARGS = {"region": "setup_grid"}
Expand Down Expand Up @@ -583,7 +582,7 @@ def setup_grid(
)
)
else: # rotated
geomu = geom.unary_union
geomu = geom.union_all()
x0, y0, mmax, nmax, rot = workflows.grid.rotated_grid(
geomu, res, dec_origin=dec_origin, dec_rotation=dec_rotation
)
Expand Down
6 changes: 2 additions & 4 deletions hydromt/vector.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Implementation of the vector workloads."""

from __future__ import annotations

import logging
Expand All @@ -20,7 +21,6 @@


class GeoBase(raster.XGeoBase):

"""Base accessor class for geo data."""

def __init__(self, xarray_obj) -> None:
Expand Down Expand Up @@ -332,7 +332,7 @@ def update_geometry(
geom_name = self.attrs.get("geom_name", "geometry")
elif self.geom_name != geom_name:
drop_vars.append(self.geom_name)
coords = {geom_name: (index_dim, geometry.values)}
coords = {geom_name: (index_dim, geometry.values.to_numpy())}
elif geom_format == "wkt":
if geom_name is None:
geom_name = self.attrs.get("geom_name", "ogc_wkt")
Expand Down Expand Up @@ -663,7 +663,6 @@ def to_netcdf(

@xr.register_dataarray_accessor("vector")
class GeoDataArray(GeoBase):

"""Accessor class for vector based geo data arrays."""

def __init__(self, xarray_obj):
Expand Down Expand Up @@ -836,7 +835,6 @@ def from_netcdf(

@xr.register_dataset_accessor("vector")
class GeoDataset(GeoBase):

"""Implementation for a vectorased geo dataset."""

def __init__(self, xarray_obj):
Expand Down
21,896 changes: 11,164 additions & 10,732 deletions pixi.lock

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ dependencies = [
"tomli", # parsing toml files
"tomli-w", # writing toml files
"universal_pathlib>=0.2", # provides path compatability between different filesystems
"xarray==2024.3.0", # ndim data arrays
"xarray", # ndim data arrays
"xmltodict", # xml parser also used to read VRT
"zarr", # zarr
]
Expand Down
6 changes: 2 additions & 4 deletions tests/test_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,7 @@
from hydromt import _compat, raster


@pytest.mark.parametrize("engine", ["fiona", "pyogrio"])
def test_open_vector(engine, tmpdir, df, geodf, world):
gpd.io_engine = engine
def test_open_vector(tmpdir, df, geodf, world):
fn_csv = str(tmpdir.join("test.csv"))
fn_parquet = str(tmpdir.join("test.parquet"))
fn_xy = str(tmpdir.join("test.xy"))
Expand Down Expand Up @@ -100,7 +98,7 @@ def test_open_vector(engine, tmpdir, df, geodf, world):
def test_open_vector_s3(geodf: gpd.GeoDataFrame):
m = MagicMock()
m.return_value = geodf
with patch("geopandas.io.file._read_file_fiona", m):
with patch("geopandas.io.file._read_file_pyogrio", m):
df = hydromt.open_vector(UPath("s3://fake_url/file.geojson"))
assert np.all(geodf == df)

Expand Down

0 comments on commit cd6d2b8

Please sign in to comment.