Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add driver for GeoDataFrame #750

Merged
merged 28 commits into from
Feb 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/refresh-cache.yml
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ jobs:
path: |
/usr/share/miniconda3
~/pycache
key: test-py${{ matrix.python-version }}-${{ matrix.os }}-${{ hashFiles('environment.yml') }}
key: test-${{ matrix.os }}-py${{ matrix.python-version }}-${{ hashFiles('environment.yml') }}
id: test-cache

rebuild-docs-cache:
Expand Down
19 changes: 16 additions & 3 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@ name: Tests

on:
workflow_dispatch:
inputs:
debug_enabled:
type: boolean
description: 'Run the build with tmate debugging enabled (https://github.com/marketplace/actions/debugging-with-tmate)'
required: false
default: false
push:
branches: [main, v1]
paths:
Expand Down Expand Up @@ -30,7 +36,7 @@ jobs:
matrix:
os: [ubuntu-latest, windows-latest]
python-version: ['3.9','3.10','3.11']
name: py ${{ matrix.python-version }}
name: py ${{ matrix.python-version }} (${{ matrix.os}})
runs-on: ${{ matrix.os }}
timeout-minutes: 30
concurrency:
Expand All @@ -53,9 +59,16 @@ jobs:
~/pycache
# the below two settings mean we'll alway srestore the cache
# but the cache hit output can tell us if we have to update afterwards
key: test-py${{ matrix.python-version }}-${{ hashFiles('environment.yml') }}
key: test-${{ matrix.os }}-py${{ matrix.python-version }}-${{ hashFiles('environment.yml') }}
restore-keys: |
test-py${{ matrix.python-version }}
test-${{ matrix.os }}-py${{ matrix.python-version }}-${{ hashFiles('environment.yml') }}
test-${{ matrix.os }}-py${{ matrix.python-version }}
test-${{ matrix.os }}

# Enable tmate debugging of manually-triggered workflows if the input option was provided
- name: Setup tmate session
uses: mxschmitt/action-tmate@v3
if: ${{ github.event_name == 'workflow_dispatch' && inputs.debug_enabled }}

- name: Fail on no cache restore
if: steps.cache.outputs.cache-matched-key == ''
Expand Down
8 changes: 8 additions & 0 deletions docs/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,14 @@ All notable changes to this project will be documented in this page.
The format is based on `Keep a Changelog`_, and this project adheres to
`Semantic Versioning`_.

V1
==

Added
-----
- Added Driver class for customizable io
- Added MetaDataResolver class for customizable metadata discovery
- Added DataSource class to represent and validate DataCatalog entries.


Unreleased
Expand Down
4 changes: 4 additions & 0 deletions hydromt/_typing/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,14 @@
Bbox,
Crs,
Data,
DataType,
DeferedFileClose,
ExportConfigDict,
GeoDataframeSource,
GeoDatasetSource,
Geom,
GeomBuffer,
GpdShapeGeom,
ModeLike,
Number,
Predicate,
Expand Down Expand Up @@ -51,7 +53,9 @@
"_exec_nodata_strat",
"Variables",
"Geom",
"GpdShapeGeom",
"Data",
"DataType",
"GeomBuffer",
"Predicate",
]
55 changes: 51 additions & 4 deletions hydromt/_typing/type_def.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,54 @@
from pathlib import Path
from typing import Any, Dict, List, Literal, Tuple, TypedDict, Union

from geopandas import GeoDataFrame, GeoSeries
import geopandas as gpd
from dateutil.parser import parse
from pydantic.functional_validators import AfterValidator, BeforeValidator
from shapely.geometry.base import BaseGeometry
from typing_extensions import Annotated
from xarray import DataArray, Dataset

from hydromt._typing.model_mode import ModelMode


def _time_tuple_from_str(
t: Tuple[Union[str, datetime], Union[str, datetime]],
) -> "TimeRange":
if isinstance(t[0], str):
t0 = parse(t[0])
else:
t0 = t[0]
if isinstance(t[1], str):
t1 = parse(t[1])
else:
t1 = t[1]
return (t0, t1)


def _timerange_validate(tr: tuple[datetime, datetime]) -> tuple[datetime, datetime]:
assert tr[0] >= tr[1], f"timerange t0: '{tr[0]}' should be less than t1: '{tr[1]}'"
return tr


def _validate_bbox(
bbox: tuple[float, float, float, float],
) -> tuple[float, float, float, float]:
assert (
bbox[0] < bbox[2]
), f"bbox minx: '{bbox[0]}' should be less than maxx: '{bbox[2]}'."
assert (
bbox[1] < bbox[3]
), f"bbox miny: '{bbox[1]}' should be less than maxy: '{bbox[3]}'."


DataType = Literal[
"DataFrame", "DataSet", "GeoDataFrame", "GeoDataSet", "RasterDataSet"
]
GeoDataframeSource = Union[str, Path]
GeoDatasetSource = Union[str, Path]
RasterDatasetSource = Union[str, Path]
Bbox = Annotated[Tuple[float, float, float, float], _validate_bbox]

StrPath = Union[str, Path]
GeoDataframeSource = StrPath
GeoDatasetSource = StrPath
Expand All @@ -17,7 +60,11 @@
Bbox = Tuple[float, float, float, float]
Crs = int
TotalBounds = Tuple[Bbox, Crs]
TimeRange = Tuple[datetime, datetime]
TimeRange = Annotated[
Tuple[datetime, datetime],
BeforeValidator(_time_tuple_from_str),
AfterValidator(_timerange_validate),
]
Number = Union[int, float]
SourceSpecDict = TypedDict(
"SourceSpecDict", {"source": str, "provider": str, "version": Union[str, int]}
Expand All @@ -38,13 +85,13 @@
"intersects", "within", "contains", "overlaps", "crosses", "touches"
]

Geom = Union[GeoDataFrame, GeoSeries]
Geom = Union[gpd.GeoDataFrame, gpd.GeoSeries]
GpdShapeGeom = Union[Geom, BaseGeometry]

Data = Union[Dataset, DataArray]

Variables = Union[str, List[str]]

GeomBuffer = int


ModeLike = Union[ModelMode, str]
4 changes: 2 additions & 2 deletions hydromt/data_adapter/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# -*- coding: utf-8 -*-
"""HydroMT data adapter."""
"""Data Adapters are generic for its HydroMT type and perform transformations."""

# TODO: correct imports after deprecation of old adapters
from .caching import cache_vrt_tiles
from .data_adapter import PREPROCESSORS, DataAdapter
from .dataframe import DataFrameAdapter
Expand Down
1 change: 1 addition & 0 deletions hydromt/data_adapter/caching.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
HYDROMT_DATADIR = join(Path.home(), ".hydromt_data")


# TODO: move this to an appropriate module (has nothing to do with caching)
def _uri_validator(uri: Union[str, Path]) -> bool:
"""Check if uri is valid."""
try:
Expand Down
10 changes: 10 additions & 0 deletions hydromt/data_adapter/data_adapter_base.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
"""Base class for data adapters."""
from pydantic import BaseModel

from hydromt.data_sources.data_source import DataSource


class DataAdapterBase(BaseModel):
"""Base class for data adapters."""

source: DataSource
Loading
Loading