-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathdetector_platform_client.py
More file actions
1006 lines (876 loc) · 34.5 KB
/
Copy pathdetector_platform_client.py
File metadata and controls
1006 lines (876 loc) · 34.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""
Handles interfacing with the detection platform api v2 documented at:
https://app.picterra.ch/public/apidocs/v2/
Note that that Detector platform is a separate product from the Plots Analysis platform and so
an API key which is valid for one may encounter permissions issues if used with the other
"""
from __future__ import annotations
import json
import logging
import sys
import tempfile
import warnings
if sys.version_info >= (3, 8):
from typing import Literal
else:
from typing_extensions import Literal
from typing import Any
import requests
from picterra.base_client import (
APIError,
BaseAPIClient,
Feature,
FeatureCollection,
_download_to_file,
_upload_file_to_blobstore,
)
logger = logging.getLogger()
class DetectorPlatformClient(BaseAPIClient):
def __init__(self, **kwargs):
super().__init__("public/api/v2/", **kwargs)
def who_am_i(self):
resp = self.sess.get(self._full_url("users/me/"))
if not resp.ok:
raise APIError(resp.text)
return resp.json()
def upload_raster(
self,
filename: str,
name: str,
folder_id: str | None = None,
captured_at: str | None = None,
identity_key: str | None = None,
multispectral: bool = False,
cloud_coverage: int | None = None,
user_tag: str | None = None,
) -> str:
"""
Upload a raster to picterra.
Args:
filename: Local filename of raster to upload
name: A human-readable name for this raster
folder_id: Id of the folder this raster
belongs to; if not provided, the raster will be put in the
"Picterra API Project" folder
captured_at: ISO-8601 date and time at which this
raster was captured, YYYY-MM-DDThh:mm[:ss[.uuuuuu]][+HH:MM|-HH:MM|Z];
e.g. "2020-01-01T12:34:56.789Z"
identity_key: Personal identifier for this raster.
multispectral: If True, the raster is in multispectral mode and can have
an associated band specification
cloud_coverage: Raster cloud coverage %.
user_tag (beta): Raster tag
Returns:
raster_id: The id of the uploaded raster
"""
data: dict[str, Any] = {"name": name, "multispectral": multispectral}
if folder_id is not None:
data.update({"folder_id": folder_id})
if captured_at is not None:
data.update({"captured_at": captured_at})
if identity_key is not None:
data.update({"identity_key": identity_key})
if cloud_coverage is not None:
data.update({"cloud_coverage": cloud_coverage})
if user_tag is not None:
data.update({"user_tag": user_tag})
resp = self.sess.post(self._full_url("rasters/upload/file/"), json=data)
if not resp.ok:
raise APIError(resp.text)
data = resp.json()
upload_url = str(data["upload_url"])
raster_id: str = data["raster_id"]
_upload_file_to_blobstore(upload_url, filename)
resp = self.sess.post(self._full_url("rasters/%s/commit/" % raster_id))
if not resp.ok:
raise APIError(resp.text)
self._wait_until_operation_completes(resp.json())
return raster_id
def list_folder_detectors(self, folder_id: str, page_number: int | None = None):
"""
List of detectors assigned to a given folder, see `ResultsPage`
for the pagination access pattern.
This a **beta** function, subject to change.
Args:
folder_id: The id of the folder to obtain the detectors for
page_number: Optional page (from 1) of the list we want to retrieve
Returns:
A ResultsPage object that contains a slice of the list of detector dictionaries,
plus methods to retrieve the other pages
Example:
::
{
"id": "id1",
"name": "detector1",
"is_runnable": True,
"user_tag": "tag1",
},
{
"id": "id2",
"name": "detector2",
"is_runnable": False,
"user_tag": "tag2",
}
"""
return self._return_results_page(
"folders/%s/detectors" % folder_id,
{"page_number": page_number} if page_number is not None else None,
)
def list_rasters(
self,
folder_id: str | None = None,
search_string: str | None = None,
user_tag: str | None = None,
max_cloud_coverage: int | None = None,
captured_before: str | None = None,
captured_after: str | None = None,
has_vector_layers: bool | None = None,
page_number: int | None = None,
):
"""
List of rasters metadata, see `ResultsPage` for the pagination access pattern.
Args:
folder_id: The id of the folder to search rasters in
search_string: The search term used to filter rasters by name
user_tag: [beta] The user tag to filter rasters by
max_cloud_coverage: [beta] The max_cloud_coverage of the rasters (between 0 and 100)
captured_before: ISO 8601 -formatted date / time of capture
we want to list the rasters since
captured_after: ISO 8601 -formatted date / time of capture
we want to list the rasters from
has_vector_layers: [beta] Whether or not the rasters have at least one vector layer
page_number: Optional page (from 1) of the list we want to retrieve
Returns:
A list of rasters dictionaries
Example:
::
{
'id': '42',
'status': 'ready',
'name': 'raster1',
'folder_id': 'abc'
},
{
'id': '43',
'status': 'ready',
'name': 'raster2',
'folder_id': 'def'
}
"""
params: dict[str, Any] = {}
if folder_id:
params["folder"] = folder_id
if search_string:
params["search"] = search_string
if user_tag is not None:
params["user_tag"] = user_tag.strip()
if max_cloud_coverage is not None:
params["max_cloud_coverage"] = max_cloud_coverage
if captured_before is not None:
params["captured_before"] = captured_before
if captured_after is not None:
params["captured_after"] = captured_after
if has_vector_layers is not None:
params["has_vector_layers"] = bool(has_vector_layers)
if page_number is not None:
params["page_number"] = page_number
return self._return_results_page("rasters", params)
def get_raster(self, raster_id: str) -> dict[str, Any]:
"""
Get raster information
Args:
raster_id: id of the raster
Raises:
APIError: There was an error while getting the raster information
Returns:
dict: Dictionary of the information
"""
resp = self.sess.get(self._full_url("rasters/%s/" % raster_id))
if not resp.ok:
raise APIError(resp.text)
return resp.json()
def edit_raster(
self,
raster_id: str,
name: str | None = None,
folder_id: str | None = None,
captured_at: str | None = None,
identity_key: str | None = None,
multispectral_band_specification: dict | None = None,
cloud_coverage: int | None = None,
user_tag: str | None = None,
):
"""
Edits an already existing raster.
Args:
name: New human-readable name for this raster
folder_id: Id of the new folder for this raster (move is in another project)
captured_at: new ISO-8601 date and time at which this
raster was captured, YYYY-MM-DDThh:mm[:ss[.uuuuuu]][+HH:MM|-HH:MM|Z];
e.g. "2020-01-01T12:34:56.789Z"
identity_key: New personal identifier for this raster.
multispectral_band_specification: The new band specification,
see https://docs.picterra.ch/advanced-topics/multispectral
cloud_coverage: Raster cloud coverage new percentage
user_tag (beta): Raster tag
Returns:
raster_id: The id of the edited raster
"""
data: dict[str, Any] = {}
if name:
data.update({"name": name})
if folder_id is not None:
data.update({"folder_id": folder_id})
if captured_at is not None:
data.update({"captured_at": captured_at})
if identity_key is not None:
data.update({"identity_key": identity_key})
if multispectral_band_specification is not None:
data.update(
{"multispectral_band_specification": multispectral_band_specification}
)
if cloud_coverage is not None:
data.update({"cloud_coverage": cloud_coverage})
if user_tag:
data.update({"user_tag": user_tag})
resp = self.sess.put(self._full_url("rasters/%s/" % raster_id), json=data)
if not resp.ok:
raise APIError(resp.text)
return raster_id
def delete_raster(self, raster_id: str):
"""
Deletes a given raster by its identifier
Args:
raster_id: The id of the raster to delete
Raises:
APIError: There was an error while trying to delete the raster
"""
resp = self.sess.delete(self._full_url("rasters/%s/" % raster_id))
if not resp.ok:
raise APIError(resp.text)
def download_raster_to_file(self, raster_id: str, filename: str):
"""
Downloads a raster to a local file
Args:
raster_id: The id of the raster to download
filename: The local filename where to save the raster image
Raises:
APIError: There was an error while trying to download the raster
"""
resp = self.sess.get(self._full_url("rasters/%s/download/" % raster_id))
if not resp.ok:
raise APIError(resp.text)
raster_url = resp.json()["download_url"]
logger.debug("Trying to download raster %s from %s.." % (raster_id, raster_url))
_download_to_file(raster_url, filename)
def set_raster_detection_areas_from_file(self, raster_id: str, filename: str):
"""
This is an experimental feature
Set detection areas from a GeoJSON file
Args:
raster_id: The id of the raster to which to assign the detection areas
filename: The filename of a GeoJSON file. This should contain a FeatureCollection
of Polygon/MultiPolygon
Raises:
APIError: There was an error uploading the file to cloud storage
"""
# Get upload URL
resp = self.sess.post(
self._full_url("rasters/%s/detection_areas/upload/file/" % raster_id)
)
if not resp.ok:
raise APIError(resp.text)
data = resp.json()
upload_url = data["upload_url"]
upload_id = data["upload_id"]
# Upload to blobstore
_upload_file_to_blobstore(upload_url, filename)
# Commit upload
resp = self.sess.post(
self._full_url(
"rasters/%s/detection_areas/upload/%s/commit/" % (raster_id, upload_id)
)
)
if not resp.ok:
raise APIError(resp.text)
self._wait_until_operation_completes(resp.json())
def remove_raster_detection_areas(self, raster_id: str):
"""
This is an experimental feature
Remove the detection areas of a raster
Args:
raster_id: The id of the raster whose detection areas will be removed
Raises:
APIError: There was an error during the operation
"""
resp = self.sess.delete(
self._full_url("rasters/%s/detection_areas/" % raster_id)
)
if not resp.ok:
raise APIError(resp.text)
def add_raster_to_detector(self, raster_id: str, detector_id: str):
"""
Associate a raster to a detector
This a **beta** function, subject to change.
Args:
detector_id: The id of the detector
raster_id: The id of the raster
Raises:
APIError: There was an error uploading the file to cloud storage
"""
resp = self.sess.post(
self._full_url("detectors/%s/training_rasters/" % detector_id),
json={"raster_id": raster_id},
)
if not resp.status_code == 201:
raise APIError(resp.text)
def create_detector(
self,
name: str | None = None,
detection_type: str = "count",
output_type: str = "polygon",
training_steps: int = 500,
backbone: str = "resnet34",
tile_size: int = 256,
background_sample_ratio: float = 0.25,
) -> str:
"""
Creates a new detector
This a **beta** function, subject to change.
Please note that depending on your plan some setting cannot be different
from the default ones
Args:
name: Name of the detector
detection_type: Type of the detector (one of 'count', 'segmentation')
output_type: Output type of the detector (one of 'polygon', 'bbox')
training_steps: Training steps the detector (integer between 500 & 40000)
backbone: detector backbone (one of 'resnet18', 'resnet34', 'resnet50')
tile_size: tile size (see HTTP API docs for the allowed values)
background_sample_ratio: bg sample ratio (between 0 and 1)
Returns:
The id of the detector
Raises:
APIError: There was an error while creating the detector
"""
# Build request body
body_data: dict[str, Any] = {"configuration": {}}
if name:
body_data["name"] = name
for i in (
"detection_type",
"output_type",
"training_steps",
"backbone",
"tile_size",
"background_sample_ratio",
):
body_data["configuration"][i] = locals()[i]
# Call API and check response
resp = self.sess.post(self._full_url("detectors/"), json=body_data)
if not resp.status_code == 201:
raise APIError(resp.text)
return resp.json()["id"]
def get_detector(self, detector_id: str):
resp = self.sess.get(self._full_url("detectors/%s/" % detector_id))
if not resp.status_code == 200:
raise APIError(resp.text)
return resp.json()
def list_detectors(
self,
search_string: str | None = None,
user_tag: str | None = None,
is_shared: bool | None = None,
page_number: int | None = None,
):
"""
List all the detectors the user can access, see `ResultsPage`
for the pagination access pattern.
Args:
search_string: The term used to filter detectors by name
user_tag: [beta] User tag to filter detectors by
is_shared: [beta] Share status to filter detectors by
page_number: Optional page (from 1) of the list we want to retrieve
Returns:
A list of detectors dictionaries
Example:
::
{
'id': '42',
'name': 'cow detector',
'configuration': {
'detection_type': 'count',
'output_type': 'bbox',
'training_steps': 787
}
},
{
'id': '43',
'name': 'test5',
'configuration': {
'detection_type': 'segmentation',
'output_type': 'polygon',
'training_steps': 500
}
}
"""
data: dict[str, Any] = {}
if search_string is not None:
data["search"] = search_string.strip()
if user_tag is not None:
data["user_tag"] = user_tag.strip()
if is_shared is not None:
data["is_shared"] = is_shared
if page_number is not None:
data["page_number"] = page_number
return self._return_results_page("detectors", data)
def edit_detector(
self,
detector_id: str,
name: str | None = None,
detection_type: str | None = None,
output_type: str | None = None,
training_steps: int | None = None,
backbone: str | None = None,
tile_size: int | None = None,
background_sample_ratio: float | None = None,
):
"""
Edit a detector
This a **beta** function, subject to change.
Please note that depending on your plan some settings may not be editable.
Args:
detector_id: identifier of the detector
name: Name of the detector
detection_type: The type of the detector (one of 'count', 'segmentation')
output_type: The output type of the detector (one of 'polygon', 'bbox')
training_steps: The training steps the detector (int in [500, 40000])
backbone: detector backbone (one of 'resnet18', 'resnet34', 'resnet50')
tile_size: tile size (see HTTP API docs for the allowed values)
background_sample_ratio: bg sample ratio (between 0 and 1)
Raises:
APIError: There was an error while editing the detector
"""
# Build request body
body_data: dict[str, Any] = {"configuration": {}}
if name:
body_data["name"] = name
for i in (
"detection_type",
"output_type",
"training_steps",
"backbone",
"tile_size",
"background_sample_ratio",
):
if locals()[i]:
body_data["configuration"][i] = locals()[i]
# Call API and check response
resp = self.sess.put(
self._full_url("detectors/%s/" % detector_id), json=body_data
)
if not resp.status_code == 204:
raise APIError(resp.text)
def delete_detector(self, detector_id: str):
"""
Deletes a given detector by its identifier
Args:
detector_id: The id of the detector to delete
Raises:
APIError: There was an error while trying to delete the detector
"""
resp = self.sess.delete(self._full_url("detectors/%s/" % detector_id))
if not resp.ok:
raise APIError(resp.text)
def run_detector(
self, detector_id: str, raster_id: str, secondary_raster_id: str | None = None
) -> str:
"""
Runs a detector on a raster: predictions are subject to a minimum charge
of 10 MP.
Args:
detector_id: The id of the detector
raster_id: The id of the raster
secondary_raster_id: The id of the secondary raster. This needs to be provided to
run change detectors.
Returns:
operation_id: The id of the operation. You typically want to pass this
to `download_result_to_feature_collection`
"""
body = {"raster_id": raster_id}
if secondary_raster_id is not None:
body["secondary_raster_id"] = secondary_raster_id
resp = self.sess.post(
self._full_url("detectors/%s/run/" % detector_id),
json=body,
)
if not resp.ok:
raise APIError(resp.text)
operation_response = resp.json()
self._wait_until_operation_completes(operation_response)
return operation_response["operation_id"]
def download_result_to_feature_collection(self, operation_id: str, filename: str):
"""
Downloads the results from a detection operation to a local GeoJSON file.
Results are stored as a FeatureCollection of Multipolygon. Each feature has a 'class_name'
property indicating the corresponding class name
Args:
operation_id: The id of the operation to download. This should be a
detect operation
filename: The local filename where to save the results
"""
results = self.get_operation_results(operation_id)
# We download results to a temporary directory and then assemble them into a
# FeatureCollection
fc: FeatureCollection = {"type": "FeatureCollection", "features": []}
for class_result in results["by_class"]:
with tempfile.NamedTemporaryFile() as f:
self.download_vector_layer_to_file(
class_result["result"]["vector_layer_id"], f.name)
with open(f.name) as fr:
vl_polygon_fc: FeatureCollection = json.load(fr)
mp_feature: Feature = {
"type": "Feature",
"properties": {"class_name": class_result["class"]["name"]},
"geometry": {
"type": "MultiPolygon",
"coordinates": []
}
}
for poly_feat in vl_polygon_fc["features"]:
mp_feature["geometry"]["coordinates"].append(
poly_feat["geometry"]["coordinates"]
)
fc["features"].append(mp_feature)
with open(filename, "w") as f:
json.dump(fc, f)
def download_result_to_file(self, operation_id: str, filename: str):
"""
Downloads a set of results to a local GeoJSON file
.. deprecated:: 1.0.0
Use `download_result_to_feature_collection` instead
Args:
operation_id: The id of the operation to download
filename: The local filename where to save the results
"""
warnings.warn(
"This function is deprecated. Use download_result_to_feature_collection instead",
DeprecationWarning,
)
result_url = self.get_operation_results(operation_id)["url"]
logger.debug("Trying to download result %s.." % result_url)
_download_to_file(result_url, filename)
def set_annotations(
self,
detector_id: str,
raster_id: str,
annotation_type: Literal[
"outline", "training_area", "testing_area", "validation_area"
],
annotations: dict[str, Any],
class_id: str | None = None,
):
"""
Replaces the annotations of type 'annotation_type' with 'annotations', for the
given raster-detector pair.
Args:
detector_id: The id of the detector
raster_id: The id of the raster
annotation_type: One of (outline, training_area, testing_area, validation_area)
annotations: GeoJSON representation of the features to upload
class_id: The class id to which to associate the new annotations. Only valid if
annotation_type is "outline"
"""
# Get an upload url
create_upload_resp = self.sess.post(
self._full_url(
"detectors/%s/training_rasters/%s/%s/upload/bulk/"
% (detector_id, raster_id, annotation_type)
)
)
if not create_upload_resp.ok:
raise APIError(create_upload_resp.text)
upload = create_upload_resp.json()
upload_url = upload["upload_url"]
upload_id = upload["upload_id"]
# Given we do not use self.sess the timeout is disabled (requests default), and this
# is good as file upload can take a long time
upload_resp = requests.put(upload_url, json=annotations)
if not upload_resp.ok:
logger.error(
"Error when sending annotation upload %s to blobstore at url %s"
% (upload_id, upload_url)
)
raise APIError(upload_resp.text)
# Commit upload
body = {}
if class_id is not None:
body["class_id"] = class_id
commit_upload_resp = self.sess.post(
self._full_url(
"detectors/%s/training_rasters/%s/%s/upload/bulk/%s/commit/"
% (detector_id, raster_id, annotation_type, upload_id)
),
json=body,
)
if not commit_upload_resp.ok:
raise APIError(commit_upload_resp.text)
# Poll for operation completion
self._wait_until_operation_completes(commit_upload_resp.json())
def train_detector(self, detector_id: str):
"""
Start the training of a detector
Args:
detector_id: The id of the detector
"""
resp = self.sess.post(self._full_url("detectors/%s/train/" % detector_id))
if not resp.ok:
raise APIError(resp.text)
return self._wait_until_operation_completes(resp.json())
def run_dataset_recommendation(self, detector_id: str):
"""
This is an **experimental** feature
Runs dataset recommendation on a detector. Note that you currently have to use
the UI to be able to view the recommendation markers/report.
Args:
detector_id: The id of the detector
"""
resp = self.sess.post(
self._full_url("detectors/%s/dataset_recommendation/" % detector_id)
)
if not resp.ok:
raise APIError(resp.text)
return self._wait_until_operation_completes(resp.json())
def run_advanced_tool(
self, tool_id: str, inputs: dict[str, Any], outputs: dict[str, Any]
):
"""
This is an experimental feature
Runs a tool and waits for its execution, returning the finished operation metadata
Args:
tool_id: The id of the tool to run
inputs: tool inputs
outputs: tool outputs
Raises:
APIError: There was an error while launching and executing the tool
"""
resp = self.sess.post(
self._full_url("advanced_tools/%s/run/" % tool_id),
json={"inputs": inputs, "outputs": outputs},
)
if not resp.ok:
raise APIError(resp.text)
return self._wait_until_operation_completes(resp.json())
def upload_vector_layer(
self,
raster_id: str,
filename: str,
name: str | None = None,
color: str | None = None,
) -> str:
"""
Uploads a vector layer from a GeoJSON file
This a **beta** function, subject to change.
Args:
raster_id: The id of the raster we want to attach the vector layer to
filename: Path to the local GeoJSOn file we want to upload
name: Optional name to give to the vector layer
color: Optional color of the vector layer, has an HTML hex color code (eg "#aabbcc")
Returns;
the vector layer unique identifier
"""
resp = self.sess.post(self._full_url("vector_layers/%s/upload/" % raster_id))
if not resp.ok:
raise APIError(resp.text)
upload = resp.json()
upload_id, upload_url = upload["upload_id"], upload["upload_url"]
_upload_file_to_blobstore(upload_url, filename)
data = {}
if name is not None:
data["name"] = name
if color is not None:
data["color"] = color
resp = self.sess.post(
self._full_url(
"vector_layers/%s/upload/%s/commit/" % (raster_id, upload_id)
),
json=data,
)
if not resp.ok:
raise APIError(resp.text)
op = self._wait_until_operation_completes(resp.json())
return op["results"]["vector_layer_id"]
def edit_vector_layer(
self, vector_layer_id: str, name: str | None = None, color: str | None = None
):
"""
Edits a vector layer
This a **beta** function, subject to change.
Args:
vector_layer_id: The id of the vector layer to remove
name: new name
color: new color
"""
data = {}
if name:
data.update({"name": name})
if color is not None:
data.update({"color": color})
resp = self.sess.put(
self._full_url("vector_layers/%s/" % vector_layer_id), json=data
)
if not resp.ok:
raise APIError(resp.text)
def delete_vector_layer(self, vector_layer_id: str):
"""
Removes a vector layer
This a **beta** function, subject to change.
Args:
vector_layer_id: The id of the vector layer to remove
"""
resp = self.sess.delete(self._full_url("vector_layers/%s/" % vector_layer_id))
if not resp.ok:
raise APIError(resp.text)
def download_vector_layer_to_file(self, vector_layer_id: str, filename: str):
"""
Downloads a vector layer
This a **beta** function, subject to change.
Args:
vector_layer_id: The id of the vector layer to download
filename: existing file to save the vector layer in, as a feature collection of polygons
"""
resp = self.sess.post(self._full_url("vector_layers/%s/download/" % vector_layer_id))
if not resp.ok:
raise APIError(resp.text)
op = self._wait_until_operation_completes(resp.json())
_download_to_file(op["results"]["download_url"], filename)
def list_raster_markers(
self,
raster_id: str,
page_number: int | None = None,
):
"""
This a **beta** function, subject to change.
List all the markers on a raster, see `ResultsPage` for the pagination access pattern.
Args:
raster_id: The id of the raster
page_number: Optional page (from 1) of the list we want to retrieve
"""
return self._return_results_page(
"rasters/%s/markers/" % raster_id,
{"page_number": page_number} if page_number is not None else None,
)
def create_marker(
self,
raster_id: str,
detector_id: str | None,
lng: float,
lat: float,
text: str,
) -> dict[str, Any]:
"""
This is an **experimental** (beta) feature
Creates a marker
Args:
raster_id: The id of the raster (belonging to detector) to create the marker on
detector_id: The id of the detector to create the marker on. If this is None, the marker
is created associated with the raster only
Raises:
APIError: There was an error while creating the marker
"""
if detector_id is None:
url = "rasters/%s/markers/" % raster_id
else:
url = "detectors/%s/training_rasters/%s/markers/" % (detector_id, raster_id)
data = {
"marker": {"type": "Point", "coordinates": [lng, lat]},
"text": text,
}
resp = self.sess.post(self._full_url(url), json=data)
if not resp.ok:
raise APIError(resp.text)
return resp.json()
def import_raster_from_remote_source(
self,
raster_name: str,
folder_id: str,
source_id: str,
aoi_filename: str,
method: Literal["streaming"] = "streaming",
) -> str:
"""
Import a raster from a remote imagery source given a GeoJSON file for the AOI
Args:
raster_name: Name of the new raster
folder_id: The id of the folder / project the raster will live in
source_id: The id of the remote imagery source to import from
filename: The filename of a GeoJSON file. This should contain a FeatureCollection of
Polygon/MultiPolygon representing the AOI of the new raster
Raises:
APIError: There was an error during import
"""
# Get upload URL
resp = self.sess.post(self._full_url("rasters/import/"))
if not resp.ok:
raise APIError(resp.text)
data = resp.json()
upload_url = data["upload_url"]
upload_id = data["upload_id"]
# Upload to blobstore
_upload_file_to_blobstore(upload_url, aoi_filename)
# Commit upload
resp = self.sess.post(
self._full_url(f"rasters/import/{upload_id}/commit/"),
json={
"method": method,
"source_id": source_id,
"folder_id": folder_id,
"name": raster_name,
},
)
if not resp.ok:
raise APIError(resp.text)
# Poll operation and get raster identifier
operation = self._wait_until_operation_completes(resp.json())
return operation["metadata"]["raster_id"]
def list_raster_vector_layers(
self,
raster_id: str,
search: str | None = None,
detector_id: str | None = None,
page_number: int | None = None,
):
"""
This a **beta** function, subject to change.
List all the vector layers on a raster, see `ResultsPage`
for the pagination access pattern.
Args:
raster_id: The id of the raster
search: Optional string to search layers by name
page_number: Optional page (from 1) of the list we want to retrieve
"""
params: dict[str, str | int] = {}
if search is not None:
params["search"] = search
if detector_id is not None:
params["detector"] = detector_id
if page_number is not None:
params["page_number"] = page_number
url = "rasters/%s/vector_layers/" % raster_id
return self._return_results_page(url, params)
def list_detector_rasters(
self,
detector_id: str,
page_number: int | None = None,
):
"""
This a **beta** function, subject to change.
List rasters of a detector, see `ResultsPage` for the pagination access pattern.
Args:
detector_id: The id of the detector
page_number: Optional page (from 1) of the list we want to retrieve