1212import datetime
1313import logging
1414import pathlib
15+ import re
1516import typing
17+ import urllib .parse
1618import warnings
1719from builtins import staticmethod
1820from typing import Any , Callable , Dict , Iterable , List , Optional , Sequence , Tuple , Union
@@ -584,7 +586,9 @@ def filter_bbox(
584586 )
585587
586588 @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 :
588592 """
589593 Limits the data cube over the spatial dimensions to the specified geometries.
590594
@@ -597,10 +601,24 @@ def filter_spatial(self, geometries) -> DataCube:
597601 More specifically, pixels outside of the bounding box of the given geometry will not be available after filtering.
598602 All pixels inside the bounding box that are not retained will be set to null (no data).
599603
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+
601616 :return: A data cube restricted to the specified geometries. The dimensions and dimension properties (name,
602617 type, labels, reference system and resolution) remain unchanged, except that the spatial dimensions have less
603618 (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.
604622 """
605623 valid_geojson_types = [
606624 "Point" , "MultiPoint" , "LineString" , "MultiLineString" ,
@@ -1052,15 +1070,29 @@ def _get_geometry_argument(
10521070 :param crs: value that encodes a coordinate reference system.
10531071 See :py:func:`openeo.util.normalize_crs` for more details about additional normalization that is applied to this argument.
10541072 """
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+
10551091 if isinstance (geometry , (str , pathlib .Path )):
10561092 # Assumption: `geometry` is path to polygon is a path to vector file at backend.
10571093 # TODO #104: `read_vector` is non-standard process.
10581094 # TODO: If path exists client side: load it client side?
10591095 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 ()
10641096
10651097 if isinstance (geometry , shapely .geometry .base .BaseGeometry ):
10661098 geometry = mapping (geometry )
@@ -1107,8 +1139,18 @@ def aggregate_spatial(
11071139 Aggregates statistics for one or more geometries (e.g. zonal statistics for polygons)
11081140 over the spatial dimensions.
11091141
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+
11121154 :param reducer: the "child callback":
11131155 the name of a single openEO process,
11141156 or a callback function as discussed in :ref:`callbackfunctions`,
@@ -1128,10 +1170,13 @@ def aggregate_spatial(
11281170 By default, longitude-latitude (EPSG:4326) is assumed.
11291171 See :py:func:`openeo.util.normalize_crs` for more details about additional normalization that is applied to this argument.
11301172
1131- :param context: Additional data to be passed to the reducer process.
1132-
11331173 .. note:: this ``crs`` argument is a non-standard/experimental feature, only supported by specific back-ends.
11341174 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.
11351180 """
11361181 valid_geojson_types = [
11371182 "Point" , "MultiPoint" , "LineString" , "MultiLineString" ,
@@ -1461,8 +1506,18 @@ def apply_polygon(
14611506 the GeometriesOverlap exception is thrown.
14621507 Each sub data cube is passed individually to the given process.
14631508
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+
14661521 :param process: "child callback" function, see :ref:`callbackfunctions`
14671522 :param mask_value: The value used for pixels outside the polygon.
14681523 :param context: Additional data to be passed to the process.
@@ -1473,6 +1528,9 @@ def apply_polygon(
14731528 Argument ``polygons`` was renamed to ``geometries``.
14741529 While deprecated, the old name ``polygons`` is still supported
14751530 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.
14761534 """
14771535 # TODO drop support for legacy `polygons` argument:
14781536 # remove `kwargs, remove default `None` value for `geometries` and `process`
@@ -1957,14 +2015,27 @@ def mask_polygon(
19572015 The pixel values are replaced with the value specified for `replacement`,
19582016 which defaults to `no data`.
19592017
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+
19622030 :param srs: The spatial reference system of the provided polygon.
19632031 By default longitude-latitude (EPSG:4326) is assumed.
19642032
19652033 .. note:: this ``srs`` argument is a non-standard/experimental feature, only supported by specific back-ends.
19662034 See https://github.com/Open-EO/openeo-processes/issues/235 for details.
19672035 :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.
19682039 """
19692040 valid_geojson_types = ["Polygon" , "MultiPolygon" , "GeometryCollection" , "Feature" , "FeatureCollection" ]
19702041 mask = self ._get_geometry_argument (mask , valid_geojson_types = valid_geojson_types , crs = srs )
0 commit comments