Skip to content

Commit 7063902

Browse files
committed
add support of pathlib.Path in mask, execute_batch, download_results
1 parent 1813a36 commit 7063902

2 files changed

Lines changed: 17 additions & 10 deletions

File tree

openeo/rest/imagecollectionclient.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import datetime
2+
import pathlib
23
import time
34
import typing
45
from typing import List, Dict, Union, Tuple
@@ -762,6 +763,7 @@ def mask(self, polygon: Union[Polygon, MultiPolygon,str]=None, srs="EPSG:4326",
762763
No data values will be left untouched by the masking operation.
763764
764765
# TODO: just provide a single `mask` argument and detect the type: polygon or process graph
766+
# TODO: also see `mask` vs `mask_polygon` processes in https://github.com/Open-EO/openeo-processes/pull/110
765767
766768
:param polygon: A polygon, provided as a :class:`shapely.geometry.Polygon` or :class:`shapely.geometry.MultiPolygon`, or a filename pointing to a valid vector file
767769
:param srs: The reference system of the provided polygon, by default this is Lat Lon (EPSG:4326).
@@ -773,9 +775,11 @@ def mask(self, polygon: Union[Polygon, MultiPolygon,str]=None, srs="EPSG:4326",
773775
mask = None
774776
new_collection = None
775777
if polygon is not None:
776-
if isinstance(polygon, str):
778+
if isinstance(polygon, (str, pathlib.Path)):
779+
# TODO: default to loading file client side?
780+
# TODO: change read_vector to load_uploaded_files https://github.com/Open-EO/openeo-processes/pull/106
777781
new_collection = self.graph_add_process('read_vector', args={
778-
'filename': polygon
782+
'filename': str(polygon)
779783
})
780784

781785
mask = {
@@ -966,7 +970,7 @@ def tiled_viewing_service(self,**kwargs) -> Dict:
966970

967971
def execute_batch(
968972
self,
969-
outputfile: str, out_format: str = None,
973+
outputfile: Union[str, pathlib.Path], out_format: str = None,
970974
print=print, max_poll_interval=60, connection_retry_interval=30,
971975
job_options=None, **format_options):
972976
"""

openeo/rest/job.py

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
1+
import datetime
12
import logging
3+
import pathlib
4+
import time
25
import typing
36
import urllib.request
4-
from typing import List
5-
import datetime
6-
import time
7-
from requests import ConnectionError
7+
from typing import List, Union
88

99
from openeo.job import Job, JobResult
1010
from openeo.rest import OpenEoClientException
11+
from requests import ConnectionError
1112

1213
if hasattr(typing, 'TYPE_CHECKING') and typing.TYPE_CHECKING:
1314
# Only import this for type hinting purposes. Runtime import causes circular dependency issues.
@@ -78,7 +79,7 @@ def list_results(self, type=None):
7879
# GET /jobs/{job_id}/results
7980
pass
8081

81-
def download_results(self, target):
82+
def download_results(self, target: Union[str, pathlib.Path]) -> pathlib.Path:
8283
""" Download job results."""
8384
results_url = "/jobs/{}/results".format(self.job_id)
8485
r = self.connection.get(results_url, expected_status=200)
@@ -88,7 +89,8 @@ def download_results(self, target):
8889
raise OpenEoClientException("Expected 1 result file to download, but got {c}".format(c=len(links)))
8990
file_url = links[0]["href"]
9091

91-
with open(target, 'wb') as handle:
92+
target = pathlib.Path(target)
93+
with target.open('wb') as handle:
9294
response = self.connection.get(file_url, stream=True)
9395
for block in response.iter_content(1024):
9496
if not block:
@@ -117,7 +119,8 @@ def results(self) -> List[RESTJobResult]:
117119
return [RESTJobResult(link['href']) for link in self.connection.job_results(self.job_id)['links']]
118120

119121
@classmethod
120-
def run_synchronous(cls, job, outputfile: str, print=print, max_poll_interval=60, connection_retry_interval=30):
122+
def run_synchronous(cls, job, outputfile: Union[str, pathlib.Path],
123+
print=print, max_poll_interval=60, connection_retry_interval=30):
121124
job.start_job()
122125

123126
job_id = job.job_id

0 commit comments

Comments
 (0)