Skip to content

Commit

Permalink
Add stitched geometry creation (#12)
Browse files Browse the repository at this point in the history
* add static layer download and geometry creation

* add more types, fix pre-commits

* add tests and fix failing

* add tests and fix failing

* spacing

* try to fix pytet warning, io is optional

* install rastserio too

* try reverse

* try this version

* install deps correctly

* add another warning ignore, gdal 3.4

* gdal min is 3.5

* missing future import

* use `lru_cache` for py38

* add changelog, bump to py39

* remove `cast`

* back to `@cache`
  • Loading branch information
scottstanie authored Dec 11, 2023
1 parent 199f2ce commit 628ece4
Show file tree
Hide file tree
Showing 16 changed files with 1,218 additions and 34 deletions.
12 changes: 8 additions & 4 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,17 @@ jobs:
matrix:
os: [ubuntu-latest, macos-latest]
deps:
# Note: for now we're manually adding tophu deps,
# will change once it's on conda forge
- label: Latest
spec: ""
spec: >-
rasterio
gdal
asf_search
- label: Minimum
spec: >-
python=3.8
python=3.9
rasterio=1.3
gdal=3.5
asf_search=6.7.2
fail-fast: false
name: ${{ matrix.os }} • ${{ matrix.deps.label }}
Expand Down
22 changes: 22 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# [Unreleased](https://github.com/opera-adt/opera-utils/compare/v0.1.4...main)

**Added**
- Based raster metadata convenience functions as `_io.get_raster_*`
- Ability to download CSLC static layers and stitch into line-of-sight rasters


**Requirements**
Minimum python version is 3.9

- click>=7.0
- h5py>=1.10
- numpy>=1.20
- pooch>=1.7
- pyproj>=3.3
- shapely>=1.8
- typing_extensions>=4

For other interactions with geospatial rasters (i.e. reading metadata, creating geometries from Static Layers):
- asf_search>=6.7.2
- gdal>=3.5
- rasterio>=1.3
36 changes: 35 additions & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,3 +1,36 @@
This software is licensed under either the BSD-3-Clause or Apache-2.0 license. Users may
choose which license to apply. The terms of each license are reproduced below.

---------------------------------------------------------------------------------------

Copyright (c) 2023 California Institute of Technology ("Caltech"). U.S. Government
sponsorship acknowledged.

All rights reserved.

Redistribution and use in source and binary forms, with or without modification, are
permitted provided that the following conditions are met:

* Redistributions of source code must retain the above copyright notice, this list of
conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice, this list of
conditions and the following disclaimer in the documentation and/or other materials
provided with the distribution.
* Neither the name of Caltech nor its operating division, the Jet Propulsion Laboratory,
nor the names of its contributors may be used to endorse or promote products derived
from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

---------------------------------------------------------------------------------------

Apache License
Version 2.0, January 2004
Expand Down Expand Up @@ -187,7 +220,8 @@
same "printed page" as the copyright notice for easier
identification within third-party archives.

Copyright 2023 Scott Staniewicz
Copyright 2023 California Institute of Technology (“Caltech”).
U.S. Government sponsorship acknowledged.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand Down
6 changes: 3 additions & 3 deletions environment-geo.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
channels:
- conda-forge
dependencies:
- pyogrio>=0.5
- gdal>=3.3
- geopandas-base>=0.12
- asf_search>=6.7
- gdal>=3.5
- rasterio>=1.3
2 changes: 1 addition & 1 deletion environment.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: opera-utils-env
channels:
- conda-forge
dependencies:
- python>=3.8
- python>=3.9
- pip>=21.3 # https://pip.pypa.io/en/stable/reference/build-system/pyproject-toml/#editable-installation
- git # for pip install, due to setuptools_scm
- click>=7.0
Expand Down
12 changes: 8 additions & 4 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,12 @@ authors = [
]
description = "Miscellaneous utilities for working with OPERA data products"
readme = { file = "README.md", content-type = "text/markdown" }
requires-python = ">=3.8"
requires-python = ">=3.9"

classifiers = [
"Intended Audience :: Developers",
"Intended Audience :: Science/Research",
"Programming Language :: Python :: 3",
"Programming Language :: Python :: 3.8",
"Programming Language :: Python :: 3.9",
"Programming Language :: Python :: 3.10",
"Programming Language :: Python :: 3.11",
Expand Down Expand Up @@ -71,7 +70,7 @@ preview = true

[tool.isort]
profile = "black"
known_first_party = [" opera_utils"]
known_first_party = ["opera_utils"]

[tool.mypy]
python_version = "3.10"
Expand All @@ -84,4 +83,9 @@ ignore = "D100,D102,D104,D105,D106,D107,D203,D204,D213,D413"
[tool.pytest.ini_options]
doctest_optionflags = "NORMALIZE_WHITESPACE NUMBER"
addopts = " --cov=opera_utils --doctest-modules --randomly-seed=1234 --ignore=scripts --ignore=docs"
filterwarnings = ["error"]
filterwarnings = [
"error",
"ignore:h5py is running against HDF5.*:UserWarning",
# https://github.com/dateutil/dateutil/pull/1285 will be released, but not yet
"ignore:datetime.datetime.utcfromtimestamp.*:DeprecationWarning",
]
1 change: 0 additions & 1 deletion src/opera_utils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
from __future__ import annotations

from ._dates import *
from ._io import *
from ._version import version as __version__
from .bursts import *
from .burst_frame_db import *
Expand Down
16 changes: 1 addition & 15 deletions src/opera_utils/_dates.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
import itertools
import re
from collections import defaultdict
from pathlib import Path
from typing import Iterable, overload

from ._types import DateOrDatetime, Filename, PathLikeT
from ._utils import _get_path_from_gdal_str

__all__ = [
"get_dates",
Expand Down Expand Up @@ -145,20 +145,6 @@ def _parse_date(datestr: str, fmt: str = DATE_FORMAT) -> datetime.datetime:
return datetime.datetime.strptime(datestr, fmt)


def _get_path_from_gdal_str(name: Filename) -> Path:
s = str(name)
if s.upper().startswith("DERIVED_SUBDATASET"):
# like DERIVED_SUBDATASET:AMPLITUDE:slc_filepath.tif
p = s.split(":")[-1].strip('"').strip("'")
elif ":" in s and (s.upper().startswith("NETCDF") or s.upper().startswith("HDF")):
# like NETCDF:"slc_filepath.nc":subdataset
p = s.split(":")[1].strip('"').strip("'")
else:
# Whole thing is the path
p = str(name)
return Path(p)


def _date_format_to_regex(date_format: str) -> re.Pattern:
r"""Convert a python date format string to a regular expression.
Expand Down
122 changes: 122 additions & 0 deletions src/opera_utils/_io.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
from __future__ import annotations

from typing import Any

import numpy as np
import rasterio as rio
from affine import Affine
from pyproj import CRS

from ._types import Bbox, PathOrStr


def get_raster_nodata(filename: PathOrStr, band: int = 1) -> float | None:
"""Get the nodata value from a file.
Parameters
----------
filename : PathOrStr
Path to the file to load.
band : int, optional
Band to get nodata value for, by default 1.
Returns
-------
Optional[float]
Nodata value, or None if not found.
"""
nodatas = _get_dataset_attr(filename, "nodatavals")
return nodatas[band - 1]


def get_raster_crs(filename: PathOrStr) -> CRS:
"""Get the CRS from a file.
Parameters
----------
filename : PathOrStr
Path to the file to load.
Returns
-------
CRS
pyproj CRS for `filename`
"""
return _get_dataset_attr(filename, "crs")


def get_raster_transform(filename: PathOrStr) -> Affine:
"""Get the rasterio `Affine` transform from a file.
Parameters
----------
filename : PathOrStr
Path to the file to load.
Returns
-------
List[float]
6 floats representing a GDAL Geotransform.
"""
return _get_dataset_attr(filename, "transform")


def get_raster_gt(filename: PathOrStr) -> list[float]:
"""Get the gdal geotransform from a file.
Parameters
----------
filename : PathOrStr
Path to the file to load.
Returns
-------
Affine
Two dimensional affine transform for 2D linear mapping.
"""
return get_raster_transform(filename).to_gdal()


def get_raster_dtype(filename: PathOrStr, band: int = 1) -> np.dtype:
"""Get the numpy data type from a raster file.
Parameters
----------
filename : PathOrStr
Path to the file to load.
band : int, optional
Band to get nodata value for, by default 1.
Returns
-------
np.dtype
Data type.
"""
dtype_per_band = _get_dataset_attr(filename, "dtypes")
return np.dtype(dtype_per_band[band - 1])


def get_raster_driver(filename: PathOrStr) -> str:
"""Get the GDAL driver `ShortName` from a file.
Parameters
----------
filename : PathOrStr
Path to the file to load.
Returns
-------
str
Driver name.
"""
return _get_dataset_attr(filename, "driver")


def get_raster_bounds(filename: PathOrStr) -> Bbox:
"""Get the (left, bottom, right, top) bounds of the image."""
return _get_dataset_attr(filename, "bounds")


def _get_dataset_attr(filename: PathOrStr, attr_name: str) -> Any:
with rio.open(filename) as src:
return getattr(src, attr_name)
Loading

0 comments on commit 628ece4

Please sign in to comment.