Skip to content

Commit 30907e5

Browse files
committed
#334 compute geometry bbox in target crs if possible
1 parent a29d2dc commit 30907e5

2 files changed

Lines changed: 79 additions & 45 deletions

File tree

openeo_driver/datacube.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -744,13 +744,15 @@ def buffer_points(self, distance: float = 10) -> "DriverVectorCube":
744744
bufferer = GeometryBufferer.from_meter_for_crs(
745745
distance=distance, crs=self.get_crs()
746746
)
747-
748-
return DriverVectorCube.from_geometry(
749-
[
747+
buffered = GeoSeries([
750748
bufferer.buffer(g) if isinstance(g, shapely.geometry.Point) else g
751749
for g in self.get_geometries()
752-
]
753-
)
750+
],crs = self.get_crs())
751+
752+
updated_df = self._geometries.set_geometry(buffered,inplace=False)
753+
return DriverVectorCube.from_geodataframe(updated_df)
754+
755+
754756

755757
def apply_dimension(
756758
self,

tests/test_dry_run.py

Lines changed: 72 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -589,10 +589,11 @@ def test_aggregate_spatial_only(dry_run_env, dry_run_tracer):
589589
assert len(source_constraints) == 1
590590
src, constraints = source_constraints[0]
591591
assert src == ("load_collection", ("S2_FOOBAR", ()))
592+
592593
assert constraints == {
593-
"spatial_extent": {"west": 0.0, "south": 0.0, "east": 8.0, "north": 5.0, "crs": "EPSG:4326"},
594+
"spatial_extent": {'crs': 32631, 'east': 1056748.2872412915, 'north': 552664.2968779367, 'south': 0.0, 'west': 166021.44308054057},
594595
"aggregate_spatial": {"geometries": DriverVectorCube.from_geojson(polygon)},
595-
"weak_spatial_extent": {"west": 0.0, "south": 0.0, "east": 8.0, "north": 5.0, "crs": "EPSG:4326"},
596+
"weak_spatial_extent": {'crs': 32631, 'east': 1056748.2872412915, 'north': 552664.2968779367, 'south': 0.0, 'west': 166021.44308054057},
596597
}
597598
(geometries,) = dry_run_tracer.get_geometries()
598599
assert isinstance(geometries, DriverVectorCube)
@@ -615,13 +616,11 @@ def test_aggregate_spatial_only(dry_run_env, dry_run_tracer):
615616
(
616617
{"type": "Point", "coordinates": (2, 3)},
617618
approxify(
618-
{
619-
"west": 1.99991,
620-
"south": 2.99991,
621-
"east": 2.0000897,
622-
"north": 3.0000897,
623-
"crs": "EPSG:4326",
624-
}
619+
{'crs': 32631,
620+
'east': 388880.8418,
621+
'north': 331653.859,
622+
'south': 331634.017,
623+
'west': 388860.89}
625624
),
626625
),
627626
(
@@ -630,39 +629,43 @@ def test_aggregate_spatial_only(dry_run_env, dry_run_tracer):
630629
shapely.geometry.Point(4, 5),
631630
),
632631
approxify(
633-
{
634-
"west": 1.99991,
635-
"south": 2.99991,
636-
"east": 4.0000897,
637-
"north": 5.0000897,
638-
"crs": "EPSG:4326",
639-
}
632+
{'crs': 32631,
633+
'east': 610869.8770073673,
634+
'north': 552758.5425218772,
635+
'south': 331634.0169357686,
636+
'west': 388860.8935124034}
640637
),
641638
),
642639
(
643640
{"type": "Polygon", "coordinates": [[(0, 0), (3, 5), (8, 2), (0, 0)]]},
644-
{"west": 0.0, "south": 0.0, "east": 8.0, "north": 5.0, "crs": "EPSG:4326"},
641+
{'crs': 32631,
642+
'east': 1056748.2872412915,
643+
'north': 552664.2968779367,
644+
'south': 0.0,
645+
'west': 166021.44308054057},
645646
),
646647
(
647648
as_geojson_feature_collection(
648649
shapely.geometry.Polygon.from_bounds(2, 3, 5, 8),
649650
shapely.geometry.Polygon.from_bounds(3, 5, 8, 13),
650651
),
651-
{"west": 2.0, "south": 3.0, "east": 8.0, "north": 13.0, "crs": "EPSG:4326"},
652+
{'crs': 32631,
653+
'east': 1054970.5285543476,
654+
'north': 1442473.7600420578,
655+
'south': 331643.9380733739,
656+
'west': 388870.8676425279},
652657
),
653658
(
654659
as_geojson_feature_collection(
655660
shapely.geometry.Point(2, 3),
656661
shapely.geometry.Polygon.from_bounds(3, 5, 8, 13),
657662
),
658663
approxify(
659-
{
660-
"west": 1.99991,
661-
"south": 2.99991,
662-
"east": 8,
663-
"north": 13,
664-
"crs": "EPSG:4326",
665-
}
664+
{'crs': 32631,
665+
'east': 1054970.528,
666+
'north': 1442473.760,
667+
'south': 331634.017,
668+
'west': 388860.894}
666669
),
667670
),
668671
],
@@ -832,13 +835,21 @@ def test_aggregate_spatial_apply_dimension(dry_run_env, dry_run_tracer):
832835
src, constraints = source_constraints[0]
833836
assert src == ("load_collection", ("S2_FOOBAR", (),('B04', 'B08', 'B11', 'SCL')))
834837
assert constraints == {
835-
"spatial_extent": {"west": 0.0, "south": 0.0, "east": 8.0, "north": 5.0, "crs": "EPSG:4326"},
838+
"spatial_extent": {'crs': 32631,
839+
'east': 1056748.2872412915,
840+
'north': 552664.2968779367,
841+
'south': 0.0,
842+
'west': 166021.44308054057},
836843
"process_type": [ProcessType.GLOBAL_TIME],
837844
"bands": ["B04", "B08", "B11", "SCL"],
838845
"custom_cloud_mask": {"method": "mask_scl_dilation", 'scl_band_name': 'SCL'},
839846
"aggregate_spatial": {"geometries": DriverVectorCube.from_geojson(polygon)},
840847
"temporal_extent": ("2018-11-01", "2020-02-01"),
841-
"weak_spatial_extent": {"west": 0.0, "south": 0.0, "east": 8.0, "north": 5.0, "crs": "EPSG:4326"},
848+
"weak_spatial_extent": {'crs': 32631,
849+
'east': 1056748.2872412915,
850+
'north': 552664.2968779367,
851+
'south': 0.0,
852+
'west': 166021.44308054057},
842853
}
843854
geometries, = dry_run_tracer.get_geometries()
844855
assert isinstance(geometries, DriverVectorCube)
@@ -872,7 +883,11 @@ def test_aggregate_spatial_and_filter_bbox(dry_run_env, dry_run_tracer):
872883
assert constraints == {
873884
"spatial_extent": bbox,
874885
"aggregate_spatial": {"geometries": DriverVectorCube.from_geojson(polygon)},
875-
"weak_spatial_extent": {"west": 0.0, "south": 0.0, "east": 8.0, "north": 5.0, "crs": "EPSG:4326"},
886+
"weak_spatial_extent": {'crs': 32631,
887+
'east': 1056748.2872412915,
888+
'north': 552664.2968779367,
889+
'south': 0.0,
890+
'west': 166021.44308054057},
876891
}
877892
geometries, = dry_run_tracer.get_geometries()
878893
assert isinstance(geometries, DriverVectorCube)
@@ -906,10 +921,18 @@ def test_multiple_filter_spatial(dry_run_env, dry_run_tracer):
906921
assert src == ("load_collection", ("S2_FOOBAR", ()))
907922
geometries = dry_run_tracer.get_last_geometry(operation="filter_spatial")
908923
assert constraints == {
909-
"spatial_extent": {'crs': 'EPSG:4326','east': 8.0,'north': 5.0,'south': 0.0,'west': 0.0},
924+
"spatial_extent": {'crs': 32631,
925+
'east': 1056748.2872412915,
926+
'north': 552664.2968779367,
927+
'south': 0.0,
928+
'west': 166021.44308054057},
910929
"filter_spatial": {"geometries": DummyVectorCube.from_geometry(shapely.geometry.shape(polygon1))},
911930
"resample": {"method": "near", "resolution": [0.25, 0.25], "target_crs": 4326},
912-
"weak_spatial_extent": {"west": 0.0, "south": 0.0, "east": 8.0, "north": 5.0, "crs": "EPSG:4326"},
931+
"weak_spatial_extent": {'crs': 32631,
932+
'east': 1056748.2872412915,
933+
'north': 552664.2968779367,
934+
'south': 0.0,
935+
'west': 166021.44308054057},
913936
}
914937

915938
assert geometries == DummyVectorCube.from_geometry(shapely.geometry.shape(polygon2))
@@ -1000,10 +1023,18 @@ def test_resample_filter_spatial(dry_run_env, dry_run_tracer):
10001023
assert src == ("load_collection", ("S2_FOOBAR", ()))
10011024
geometries, = dry_run_tracer.get_geometries(operation="filter_spatial")
10021025
assert constraints == {
1003-
"spatial_extent": {'crs': 'EPSG:4326','east': 8.0,'north': 5.0,'south': 0.0,'west': 0.0},
1026+
"spatial_extent": {'crs': 32631,
1027+
'east': 1056748.2872412915,
1028+
'north': 552664.2968779367,
1029+
'south': 0.0,
1030+
'west': 166021.44308054057},
10041031
"filter_spatial": {"geometries": DummyVectorCube.from_geometry(shapely.geometry.shape(polygon))},
10051032
"resample": {"method": "near", "resolution": [0.25, 0.25], "target_crs": 4326},
1006-
"weak_spatial_extent": {"west": 0.0, "south": 0.0, "east": 8.0, "north": 5.0, "crs": "EPSG:4326"},
1033+
"weak_spatial_extent": {'crs': 32631,
1034+
'east': 1056748.2872412915,
1035+
'north': 552664.2968779367,
1036+
'south': 0.0,
1037+
'west': 166021.44308054057},
10071038
}
10081039
assert isinstance(geometries,DummyVectorCube)
10091040
assert shapely.geometry.mapping(geometries.to_multipolygon()) == {
@@ -1096,7 +1127,7 @@ def test_global_bounds_from_weak_spatial_extent(dry_run_env, dry_run_tracer):
10961127
def test_aggregate_spatial_read_vector(dry_run_env, dry_run_tracer):
10971128
geometry_path = str(get_path("geojson/GeometryCollection01.json"))
10981129
pg = {
1099-
"lc": {"process_id": "load_collection", "arguments": {"id": "S2_FOOBAR"}},
1130+
"lc": {"process_id": "load_collection", "arguments": {"id": "ESA_WORLDCOVER_10M_2020_V1"}},
11001131
"vector": {"process_id": "read_vector", "arguments": {"filename": geometry_path}},
11011132
"agg": {
11021133
"process_id": "aggregate_spatial",
@@ -1120,7 +1151,8 @@ def test_aggregate_spatial_read_vector(dry_run_env, dry_run_tracer):
11201151
assert len(source_constraints) == 1
11211152
src, constraints = source_constraints[0]
11221153
(geometries,) = dry_run_tracer.get_geometries()
1123-
assert src == ("load_collection", ("S2_FOOBAR", ()))
1154+
assert src == ("load_collection", ("ESA_WORLDCOVER_10M_2020_V1", ()))
1155+
11241156
assert constraints == {
11251157
"spatial_extent": {"west": 5.05, "south": 51.21, "east": 5.15, "north": 51.3, "crs": "EPSG:4326"},
11261158
"aggregate_spatial": {"geometries": geometries},
@@ -1133,7 +1165,7 @@ def test_aggregate_spatial_get_geometries_feature_collection(
11331165
dry_run_env, dry_run_tracer
11341166
):
11351167
pg = {
1136-
"lc": {"process_id": "load_collection", "arguments": {"id": "S2_FOOBAR"}},
1168+
"lc": {"process_id": "load_collection", "arguments": {"id": "ESA_WORLDCOVER_10M_2020_V1"}},
11371169
"vector": {
11381170
"process_id": "get_geometries",
11391171
"arguments": {
@@ -1178,7 +1210,7 @@ def test_aggregate_spatial_get_geometries_feature_collection(
11781210
source_constraints = dry_run_tracer.get_source_constraints(merge=True)
11791211
assert len(source_constraints) == 1
11801212
src, constraints = source_constraints[0]
1181-
assert src == ("load_collection", ("S2_FOOBAR", ()))
1213+
assert src == ("load_collection", ("ESA_WORLDCOVER_10M_2020_V1", ()))
11821214

11831215
expected_geometry_collection = DriverVectorCube.from_geojson(
11841216
pg["vector"]["arguments"]["feature_collection"]
@@ -2166,10 +2198,10 @@ def test_complex_extract_load_stac(dry_run_env,dry_run_tracer):
21662198

21672199
print(loadparams)
21682200
expected_extent = {'crs': 'EPSG:32633',
2169-
'east': 400450,
2170-
'north': 4700510,
2171-
'south': 4679580,
2172-
'west': 379840}
2201+
'east': 400380,
2202+
'north': 4700400,
2203+
'south': 4679610,
2204+
'west': 380120}
21732205
assert(loadparams.global_extent == expected_extent)
21742206
assert loadparams.bands == [ 'B02', 'B03', 'B04', 'B05', 'B06', 'B07', 'B08', 'B8A', 'B11', 'B12']
21752207
assert loadparams.pixel_buffer == None

0 commit comments

Comments
 (0)