Skip to content
Snippets Groups Projects

Add an API helper to update an element

Merged Manon Blanco requested to merge partial-update-element-helper into master
All threads resolved!
2 files
+ 229
0
Compare changes
  • Side-by-side
  • Inline
Files
2
@@ -275,6 +275,59 @@ class ElementMixin(object):
return created_ids
def update_element(
self,
element: Element,
type: Optional[str] = None,
name: Optional[str] = None,
polygon: Optional[List[List[Union[int, float]]]] = None,
confidence: Optional[float] = None,
) -> dict:
"""
Partially update an element through the API.
:param Element element: The element to update.
:param type: Optional new slug type of the element.
:param name: Optional new name of the element.
:param polygon: Optional new polygon of the element.
:param confidence: Optional new confidence score, between 0.0 and 1.0.
:returns: A dict from the ``PartialUpdateElement`` API endpoint,
"""
assert element and isinstance(
element, Element
), "element shouldn't be null and should be of type Element"
assert type is None or isinstance(type, str), "type should be None or a str"
assert name is None or isinstance(name, str), "name should be None or a str"
assert polygon is None or isinstance(
polygon, list
), "polygon should be None or a list"
if polygon:
assert len(polygon) >= 3, "polygon should have at least three points"
assert all(
isinstance(point, list) and len(point) == 2 for point in polygon
), "polygon points should be lists of two items"
assert all(
isinstance(coord, (int, float)) for point in polygon for coord in point
), "polygon points should be lists of two numbers"
assert confidence is None or (
isinstance(confidence, float) and 0 <= confidence <= 1
), "confidence should be None or a float in [0..1] range"
if self.is_read_only:
logger.warning("Cannot update element as this worker is in read-only mode")
return
return self.request(
"PartialUpdateElement",
id=element.id,
body={
"type": type,
"name": name,
"polygon": polygon,
"confidence": confidence,
},
)
def list_element_children(
self,
element: Union[Element, CachedElement],
Loading