generated from linz/template-python-hello-world
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
* feat: add capture-area.geojson to the STAC Collection TDE-965 * fix: removed code has been accidently brought back * fix: formatting fails * refactor: simplify gdal_footprint arguments * feat: save capture-area.geojson checksum * refactor: move add capture area logic to collection.py * docs: grammar Co-authored-by: Victor Engmark <vengmark@linz.govt.nz> * test: improve capture area UT * fix: unused 'type:ignore' comments * build: fix poetry did update every libs when adding a new one * build: poetry lock with geojson * revert: "fix: unused 'type:ignore' comments" This reverts commit 9707c62. * fix: use list instead of tuple for file suffixes * test: add a test for list_files_in_uri * refactor: store suffixes in const * feat: save capture-area.json file size in the collection.json * fix: allow margin of error to the polygon geometry * feat: create temporary script to generate capture-area from existing published dataset * refactor: remove test script * fix: enum value * feat: round the geometry using the gsd * fix: typo * docs: comment for gsd * fix: typo file name * fix: typo file name * refactor: simplify comparison Co-authored-by: Victor Engmark <vengmark@linz.govt.nz> * refactor: simplify code in tests * refactor: rename variable for readability * refactor: avoid temporary capture-area file * refactor: remove condition Co-authored-by: Victor Engmark <vengmark@linz.govt.nz> * refactor: store EPSG codes in constants * test: replace assert not by assert * test: simplify test polygons * refactor: re-use variable Co-authored-by: Victor Engmark <vengmark@linz.govt.nz> * docs: language Co-authored-by: Victor Engmark <vengmark@linz.govt.nz> * docs: move comment to docstring --------- Co-authored-by: Victor Engmark <vengmark@linz.govt.nz>
- Loading branch information
1 parent
83c9a68
commit 75df081
Showing
17 changed files
with
393 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
import json | ||
from typing import Any, Dict, List | ||
|
||
from shapely import BufferCapStyle, BufferJoinStyle, Geometry, to_geojson, union_all | ||
from shapely.geometry import Polygon | ||
|
||
DECIMAL_DEGREES_1M = 0.00001 | ||
""" | ||
Degree precision of ~1m (decimal places 5, https://en.wikipedia.org/wiki/Decimal_degrees) | ||
""" | ||
|
||
|
||
def to_feature(geometry: Geometry) -> Dict[str, Any]: | ||
"""Transform a Geometry to a GeoJSON feature. | ||
Args: | ||
geometry: a Geometry | ||
Returns: | ||
a GeoJSON document. | ||
""" | ||
return {"geometry": json.loads(to_geojson(geometry)), "type": "Feature", "properties": {}} | ||
|
||
|
||
def merge_polygons(polygons: List[Polygon], buffer_distance: float) -> Geometry: | ||
"""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. | ||
Args: | ||
polygons: list of polygons to merge | ||
buffer_distance: decimal places to use to buffer the polygons | ||
Returns: | ||
A single Geometry. | ||
""" | ||
buffered_polygons = [] | ||
for poly in polygons: | ||
# Buffer each polygon to round up to the `buffer_distance` | ||
buffered_poly = poly.buffer(buffer_distance, cap_style=BufferCapStyle.flat, join_style=BufferJoinStyle.mitre) | ||
buffered_polygons.append(buffered_poly) | ||
union_buffered = union_all(buffered_polygons) | ||
# Negative buffer back in the polygons | ||
union_unbuffered = union_buffered.buffer(-buffer_distance, cap_style=BufferCapStyle.flat, join_style=BufferJoinStyle.mitre) | ||
union_simplified = union_unbuffered.simplify(buffer_distance) | ||
|
||
return union_simplified | ||
|
||
|
||
def generate_capture_area(polygons: List[Polygon], gsd: float) -> Dict[str, Any]: | ||
"""Generate the capture area from a list of polygons. | ||
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). | ||
If we don't apply this rounding, we could get a very small gaps between tiffs | ||
which would result in a capture area having gaps. | ||
The `gsd` (in meter) is multiplied by the 1m degree of precision. | ||
Note that all the polygons are buffered which means a gap bigger than the gsd, | ||
but < gsd*2) will be closed. | ||
Args: | ||
polygons: list of polygons of the area | ||
gsd: Ground Sample Distance in meter | ||
Returns: | ||
The capture-area geojson document. | ||
""" | ||
buffer_distance = DECIMAL_DEGREES_1M * gsd | ||
merged_polygons = merge_polygons(polygons, buffer_distance) | ||
|
||
return to_feature(merged_polygons) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
Oops, something went wrong.