Skip to content

Commit

Permalink
feat: Add Shapely type library TDE-1205 (#988)
Browse files Browse the repository at this point in the history
#### Motivation

For IDE recognition.

#### Checklist

- [ ] Tests updated
- [ ] Docs updated
- [x] Issue linked in Title
  • Loading branch information
l0b0 authored Jun 14, 2024
1 parent 927faf4 commit f5e97e7
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 24 deletions.
16 changes: 15 additions & 1 deletion poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 7 additions & 6 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -43,18 +43,19 @@ py-multihash = "^2.0.1"
shapely = "^2.0.4"
tifffile = "^2023.12.9"

[tool.poetry.dev-dependencies]
[tool.poetry.group.dev.dependencies]
black = "^23.11.0"
isort = "^5.12.0"
gitlint = "^0.19.1"
isort = "^5.12.0"
moto = "^5.0.5"
mypy = "^1.6"
pylint = "^3.1.0"
pre-commit = "^3.5.0"
mypy-boto3-s3 = "^1.28.55"
pre-commit = "^3.5.0"
pylint = "^3.1.0"
pytest = "^7.4.3"
pytest-dependency = "^0.5.1"
pytest-subtests = "*"
moto = "^5.0.5"
pytest-mock = "^3.12.0"
pytest-subtests = "*"
shellcheck-py = "*"
types-shapely = "*"
vulture = "^2.10"
4 changes: 1 addition & 3 deletions scripts/files/geotiff.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
from typing import Dict, List, Tuple

from shapely.geometry import Polygon

from scripts.gdal.gdalinfo import GdalInfo


def get_extents(gdalinfo_result: GdalInfo) -> Tuple[Dict[str, List[float]], Tuple[float]]:
def get_extents(gdalinfo_result: GdalInfo) -> tuple[dict[str, list[list[list[float]]]], tuple[float, float, float, float]]:
"""Get the geometry and bounding box from the `gdalinfo`.
Args:
Expand Down
2 changes: 1 addition & 1 deletion scripts/gdal/gdalinfo.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ class GdalInfo(TypedDict):
metadata: Dict[Any, Any]
cornerCoordinates: Dict[Any, Any]
extent: Dict[Any, Any]
wgs84Extent: Optional[Dict[str, List[float]]]
wgs84Extent: Optional[Dict[str, List[List[List[float]]]]]
bands: List[GdalInfoBand]
"""Coordinate system description"""
coordinateSystem: Dict[Any, Any]
Expand Down
20 changes: 10 additions & 10 deletions scripts/stac/imagery/capture_area.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import json
from typing import Any, Dict, List
from typing import Any, Dict, Sequence

from linz_logger import get_log
from shapely import BufferCapStyle, BufferJoinStyle, Geometry, to_geojson, union_all
from shapely.geometry import Polygon
from shapely import BufferCapStyle, BufferJoinStyle, to_geojson, union_all
from shapely.geometry.base import BaseGeometry

from scripts.logging.time_helper import time_in_ms

Expand Down Expand Up @@ -43,19 +43,19 @@ def get_buffer_distance(gsd: float) -> float:
return gsd * 2 * DECIMAL_DEGREES_1M


def to_feature(geometry: Geometry) -> Dict[str, Any]:
"""Transform a Geometry to a GeoJSON feature.
def to_feature(geometry: BaseGeometry) -> Dict[str, Any]:
"""Transform a BaseGeometry to a GeoJSON feature.
Args:
geometry: a Geometry
geometry: a BaseGeometry
Returns:
a GeoJSON document.
"""
return {"geometry": json.loads(to_geojson(geometry)), "type": "Feature", "properties": {}}


def merge_polygons(polygons: List[Polygon], buffer_distance: float) -> Geometry:
def merge_polygons(polygons: Sequence[BaseGeometry], buffer_distance: float) -> BaseGeometry:
"""Merge a list of polygons by converting them to a single geometry that covers the same area.
A buffer distance is used to buffer out the polygons before dissolving them together and then negative buffer them back in.
The merged geometry is simplify (rounded) to the decimal used for the buffer.
Expand All @@ -80,8 +80,8 @@ def merge_polygons(polygons: List[Polygon], buffer_distance: float) -> Geometry:
return union_simplified


def generate_capture_area(polygons: List[Polygon], gsd: float) -> Dict[str, Any]:
"""Generate the capture area from a list of polygons.
def generate_capture_area(polygons: Sequence[BaseGeometry], gsd: float) -> Dict[str, Any]:
"""Generate the capture area from a list of BaseGeometries.
Providing the `gsd` allows to round the geometry as we've seen some tiffs
geometry being slightly off, sometimes due to rounding issue in their
creation process (before delivery).
Expand All @@ -92,7 +92,7 @@ def generate_capture_area(polygons: List[Polygon], gsd: float) -> Dict[str, Any]
but < buffer_factor*2 will be closed.
Args:
polygons: list of polygons of the area
polygons: list of BaseGeometries of the area
gsd: Ground Sample Distance in meters
Returns:
Expand Down
6 changes: 3 additions & 3 deletions scripts/stac/imagery/collection.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
from datetime import datetime
from typing import Any, Callable, Dict, List, Optional

import shapely.ops
import ulid
from shapely.geometry.base import BaseGeometry

from scripts.datetimes import format_rfc_3339_datetime_string, parse_rfc_3339_datetime
from scripts.files.files_helper import ContentType
Expand Down Expand Up @@ -87,12 +87,12 @@ def __init__(

self.add_providers(providers)

def add_capture_area(self, polygons: List[shapely.geometry.shape], target: str, artifact_target: str = "/tmp") -> None:
def add_capture_area(self, polygons: List[BaseGeometry], target: str, artifact_target: str = "/tmp") -> None:
"""Add the capture area of the Collection.
The `href` or path of the capture-area.geojson is always set as the relative `./capture-area.geojson`
Args:
polygons: list of geometries
polygons: list of BaseGeometries
target: location where the capture-area.geojson file will be saved
artifact_target: location where the capture-area.geojson artifact file will be saved.
This is useful for Argo Workflow in order to expose the file to the user for testing/validation purpose.
Expand Down

0 comments on commit f5e97e7

Please sign in to comment.