Skip to content

Commit 1486dca

Browse files
Convert MultiPolygon to FeatureCollection when downloading vector layer results
1 parent ed55353 commit 1486dca

File tree

3 files changed

+31
-6
lines changed

3 files changed

+31
-6
lines changed

src/picterra/base_client.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,12 @@
66
import sys
77
import time
88
from collections.abc import Callable
9+
910
if sys.version_info >= (3, 8):
1011
from typing import Literal, TypedDict
1112
else:
1213
from typing_extensions import Literal, TypedDict
14+
1315
from typing import Any, Generic, Iterator, TypeVar
1416
from urllib.parse import urlencode, urljoin
1517

@@ -74,6 +76,19 @@ def _upload_file_to_blobstore(upload_url: str, filename: str):
7476
raise APIError(resp.text)
7577

7678

79+
def multipolygon_to_feature_collection(mp):
80+
return {
81+
"type": "FeatureCollection",
82+
"features": [{
83+
"type": "Feature",
84+
"properties": {},
85+
"geometry": {
86+
"type": "Polygon",
87+
"coordinates": p
88+
}
89+
} for p in mp]
90+
}
91+
7792
T = TypeVar("T")
7893

7994

src/picterra/detector_platform_client.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,12 @@
1212
import sys
1313
import tempfile
1414
import warnings
15+
1516
if sys.version_info >= (3, 8):
1617
from typing import Literal
1718
else:
1819
from typing_extensions import Literal
20+
1921
from typing import Any
2022

2123
import requests
@@ -26,6 +28,7 @@
2628
FeatureCollection,
2729
_download_to_file,
2830
_upload_file_to_blobstore,
31+
multipolygon_to_feature_collection,
2932
)
3033

3134
logger = logging.getLogger()
@@ -836,13 +839,19 @@ def download_vector_layer_to_file(self, vector_layer_id: str, filename: str):
836839
837840
Args:
838841
vector_layer_id: The id of the vector layer to download
839-
filename: existing file to save the vector layer in
842+
filename: existing file to save the vector layer in, as a feature collection
840843
"""
841844
resp = self.sess.post(self._full_url("vector_layers/%s/download/" % vector_layer_id))
842845
if not resp.ok:
843846
raise APIError(resp.text)
844847
op = self._wait_until_operation_completes(resp.json())
845-
_download_to_file(op["results"]["download_url"], filename)
848+
# The operation results is a multipolygon that we convert to a feature collection
849+
with tempfile.NamedTemporaryFile() as tmp:
850+
_download_to_file(op["results"]["download_url"], tmp.name)
851+
with open(tmp.name) as f:
852+
mp = json.load(f)
853+
with open(filename, "wt") as out_f:
854+
json.dump(multipolygon_to_feature_collection(mp), out_f)
846855

847856
def list_raster_markers(
848857
self,

tests/test_client.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import responses
88
from requests.exceptions import ConnectionError
99

10+
from picterra.base_client import multipolygon_to_feature_collection
1011
from picterra.detector_platform_client import DetectorPlatformClient
1112
from tests.utils import (
1213
OP_RESP,
@@ -399,7 +400,7 @@ def add_mock_vector_layer_download_responses(layer_id, num_features):
399400
url,
400401
body=json.dumps(mp),
401402
)
402-
return mp
403+
return multipolygon_to_feature_collection(mp)
403404

404405

405406
def make_geojson_multipolygon(npolygons=1):
@@ -999,11 +1000,11 @@ def test_download_vector_layer_to_file(monkeypatch):
9991000
client = _client(monkeypatch)
10001001
with tempfile.NamedTemporaryFile() as fp:
10011002
client.download_vector_layer_to_file("foobar", fp.name)
1002-
mp = json.load(fp)
1003+
fc = json.load(fp)
10031004
assert (
1004-
mp["type"] == "MultiPolygon"
1005+
fc["type"] == "FeatureCollection"
10051006
)
1006-
assert mp == expected_content and len(mp["coordinates"]) == 2
1007+
assert fc == expected_content and len(fc["features"]) == 2
10071008
assert len(responses.calls) == 3 # POST /download, GET /operations, GET url
10081009

10091010

0 commit comments

Comments
 (0)