12
12
import datetime
13
13
import logging
14
14
import pathlib
15
+ import re
15
16
import typing
17
+ import urllib .parse
16
18
import warnings
17
19
from builtins import staticmethod
18
20
from typing import Any , Callable , Dict , Iterable , List , Optional , Sequence , Tuple , Union
@@ -584,7 +586,9 @@ def filter_bbox(
584
586
)
585
587
586
588
@openeo_process
587
- def filter_spatial (self , geometries ) -> DataCube :
589
+ def filter_spatial (
590
+ self , geometries : Union [shapely .geometry .base .BaseGeometry , dict , str , pathlib .Path , Parameter , VectorCube ]
591
+ ) -> DataCube :
588
592
"""
589
593
Limits the data cube over the spatial dimensions to the specified geometries.
590
594
@@ -597,10 +601,24 @@ def filter_spatial(self, geometries) -> DataCube:
597
601
More specifically, pixels outside of the bounding box of the given geometry will not be available after filtering.
598
602
All pixels inside the bounding box that are not retained will be set to null (no data).
599
603
600
- :param geometries: One or more geometries used for filtering, specified as GeoJSON in EPSG:4326.
604
+ :param geometries: One or more geometries used for filtering, Can be provided in different ways:
605
+
606
+ - a shapely geometry
607
+ - a GeoJSON-style dictionary,
608
+ - a public URL to the geometries in a vector format that is supported by the backend
609
+ (also see :py:func:`Connection.list_file_formats() <openeo.rest.connection.Connection.list_file_formats>`),
610
+ e.g. GeoJSON, GeoParquet, etc.
611
+ A ``load_url`` process will automatically be added to the process graph.
612
+ - a path (that is valid for the back-end) to a GeoJSON file.
613
+ - a :py:class:`~openeo.rest.vectorcube.VectorCube` instance.
614
+ - a :py:class:`~openeo.api.process.Parameter` instance.
615
+
601
616
:return: A data cube restricted to the specified geometries. The dimensions and dimension properties (name,
602
617
type, labels, reference system and resolution) remain unchanged, except that the spatial dimensions have less
603
618
(or the same) dimension labels.
619
+
620
+ .. versionchanged:: 0.36.0
621
+ Support passing a URL as ``geometries`` argument, which will be loaded with the ``load_url`` process.
604
622
"""
605
623
valid_geojson_types = [
606
624
"Point" , "MultiPoint" , "LineString" , "MultiLineString" ,
@@ -1052,15 +1070,29 @@ def _get_geometry_argument(
1052
1070
:param crs: value that encodes a coordinate reference system.
1053
1071
See :py:func:`openeo.util.normalize_crs` for more details about additional normalization that is applied to this argument.
1054
1072
"""
1073
+ if isinstance (geometry , Parameter ):
1074
+ return geometry
1075
+ elif isinstance (geometry , _FromNodeMixin ):
1076
+ return geometry .from_node ()
1077
+
1078
+ if isinstance (geometry , str ) and re .match (r"^https?://" , geometry , flags = re .I ):
1079
+ # Geometry provided as URL: load with `load_url` (with best-effort format guess)
1080
+ url = urllib .parse .urlparse (geometry )
1081
+ suffix = pathlib .Path (url .path .lower ()).suffix
1082
+ format = {
1083
+ ".json" : "GeoJSON" ,
1084
+ ".geojson" : "GeoJSON" ,
1085
+ ".pq" : "Parquet" ,
1086
+ ".parquet" : "Parquet" ,
1087
+ ".geoparquet" : "Parquet" ,
1088
+ }.get (suffix , suffix .split ("." )[- 1 ])
1089
+ return self .connection .load_url (url = geometry , format = format )
1090
+
1055
1091
if isinstance (geometry , (str , pathlib .Path )):
1056
1092
# Assumption: `geometry` is path to polygon is a path to vector file at backend.
1057
1093
# TODO #104: `read_vector` is non-standard process.
1058
1094
# TODO: If path exists client side: load it client side?
1059
1095
return PGNode (process_id = "read_vector" , arguments = {"filename" : str (geometry )})
1060
- elif isinstance (geometry , Parameter ):
1061
- return geometry
1062
- elif isinstance (geometry , _FromNodeMixin ):
1063
- return geometry .from_node ()
1064
1096
1065
1097
if isinstance (geometry , shapely .geometry .base .BaseGeometry ):
1066
1098
geometry = mapping (geometry )
@@ -1107,8 +1139,18 @@ def aggregate_spatial(
1107
1139
Aggregates statistics for one or more geometries (e.g. zonal statistics for polygons)
1108
1140
over the spatial dimensions.
1109
1141
1110
- :param geometries: a shapely geometry, a GeoJSON-style dictionary,
1111
- a public GeoJSON URL, or a path (that is valid for the back-end) to a GeoJSON file.
1142
+ :param geometries: The geometries to aggregate in. Can be provided in different ways:
1143
+
1144
+ - a shapely geometry
1145
+ - a GeoJSON-style dictionary,
1146
+ - a public URL to the geometries in a vector format that is supported by the backend
1147
+ (also see :py:func:`Connection.list_file_formats() <openeo.rest.connection.Connection.list_file_formats>`),
1148
+ e.g. GeoJSON, GeoParquet, etc.
1149
+ A ``load_url`` process will automatically be added to the process graph.
1150
+ - a path (that is valid for the back-end) to a GeoJSON file.
1151
+ - a :py:class:`~openeo.rest.vectorcube.VectorCube` instance.
1152
+ - a :py:class:`~openeo.api.process.Parameter` instance.
1153
+
1112
1154
:param reducer: the "child callback":
1113
1155
the name of a single openEO process,
1114
1156
or a callback function as discussed in :ref:`callbackfunctions`,
@@ -1128,10 +1170,13 @@ def aggregate_spatial(
1128
1170
By default, longitude-latitude (EPSG:4326) is assumed.
1129
1171
See :py:func:`openeo.util.normalize_crs` for more details about additional normalization that is applied to this argument.
1130
1172
1131
- :param context: Additional data to be passed to the reducer process.
1132
-
1133
1173
.. note:: this ``crs`` argument is a non-standard/experimental feature, only supported by specific back-ends.
1134
1174
See https://github.com/Open-EO/openeo-processes/issues/235 for details.
1175
+
1176
+ :param context: Additional data to be passed to the reducer process.
1177
+
1178
+ .. versionchanged:: 0.36.0
1179
+ Support passing a URL as ``geometries`` argument, which will be loaded with the ``load_url`` process.
1135
1180
"""
1136
1181
valid_geojson_types = [
1137
1182
"Point" , "MultiPoint" , "LineString" , "MultiLineString" ,
@@ -1461,8 +1506,18 @@ def apply_polygon(
1461
1506
the GeometriesOverlap exception is thrown.
1462
1507
Each sub data cube is passed individually to the given process.
1463
1508
1464
- :param geometries: Polygons, provided as a shapely geometry, a GeoJSON-style dictionary,
1465
- a public GeoJSON URL, or a path (that is valid for the back-end) to a GeoJSON file.
1509
+ :param geometries: Can be provided in different ways:
1510
+
1511
+ - a shapely geometry
1512
+ - a GeoJSON-style dictionary,
1513
+ - a public URL to the geometries in a vector format that is supported by the backend
1514
+ (also see :py:func:`Connection.list_file_formats() <openeo.rest.connection.Connection.list_file_formats>`),
1515
+ e.g. GeoJSON, GeoParquet, etc.
1516
+ A ``load_url`` process will automatically be added to the process graph.
1517
+ - a path (that is valid for the back-end) to a GeoJSON file.
1518
+ - a :py:class:`~openeo.rest.vectorcube.VectorCube` instance.
1519
+ - a :py:class:`~openeo.api.process.Parameter` instance.
1520
+
1466
1521
:param process: "child callback" function, see :ref:`callbackfunctions`
1467
1522
:param mask_value: The value used for pixels outside the polygon.
1468
1523
:param context: Additional data to be passed to the process.
@@ -1473,6 +1528,9 @@ def apply_polygon(
1473
1528
Argument ``polygons`` was renamed to ``geometries``.
1474
1529
While deprecated, the old name ``polygons`` is still supported
1475
1530
as keyword argument for backwards compatibility.
1531
+
1532
+ .. versionchanged:: 0.36.0
1533
+ Support passing a URL as ``geometries`` argument, which will be loaded with the ``load_url`` process.
1476
1534
"""
1477
1535
# TODO drop support for legacy `polygons` argument:
1478
1536
# remove `kwargs, remove default `None` value for `geometries` and `process`
@@ -1957,14 +2015,27 @@ def mask_polygon(
1957
2015
The pixel values are replaced with the value specified for `replacement`,
1958
2016
which defaults to `no data`.
1959
2017
1960
- :param mask: The geometry to mask with: a shapely geometry, a GeoJSON-style dictionary,
1961
- a public GeoJSON URL, or a path (that is valid for the back-end) to a GeoJSON file.
2018
+ :param mask: The geometry to mask with.an be provided in different ways:
2019
+
2020
+ - a shapely geometry
2021
+ - a GeoJSON-style dictionary,
2022
+ - a public URL to the geometries in a vector format that is supported by the backend
2023
+ (also see :py:func:`Connection.list_file_formats() <openeo.rest.connection.Connection.list_file_formats>`),
2024
+ e.g. GeoJSON, GeoParquet, etc.
2025
+ A ``load_url`` process will automatically be added to the process graph.
2026
+ - a path (that is valid for the back-end) to a GeoJSON file.
2027
+ - a :py:class:`~openeo.rest.vectorcube.VectorCube` instance.
2028
+ - a :py:class:`~openeo.api.process.Parameter` instance.
2029
+
1962
2030
:param srs: The spatial reference system of the provided polygon.
1963
2031
By default longitude-latitude (EPSG:4326) is assumed.
1964
2032
1965
2033
.. note:: this ``srs`` argument is a non-standard/experimental feature, only supported by specific back-ends.
1966
2034
See https://github.com/Open-EO/openeo-processes/issues/235 for details.
1967
2035
:param replacement: the value to replace the masked pixels with
2036
+
2037
+ .. versionchanged:: 0.36.0
2038
+ Support passing a URL as ``geometries`` argument, which will be loaded with the ``load_url`` process.
1968
2039
"""
1969
2040
valid_geojson_types = ["Polygon" , "MultiPolygon" , "GeometryCollection" , "Feature" , "FeatureCollection" ]
1970
2041
mask = self ._get_geometry_argument (mask , valid_geojson_types = valid_geojson_types , crs = srs )
0 commit comments