|
| 1 | +"""Geometry collection conversion functions between Kili and geojson formats.""" |
| 2 | + |
| 3 | +import uuid |
| 4 | +from typing import Any, Dict, List, Optional |
| 5 | + |
| 6 | +from .line import geojson_linestring_feature_to_kili_line_annotation |
| 7 | +from .multilinestring import geojson_multilinestring_feature_to_kili_line_annotations |
| 8 | +from .multipoint import geojson_multipoint_feature_to_kili_point_annotations |
| 9 | +from .point import geojson_point_feature_to_kili_point_annotation |
| 10 | +from .polygon import geojson_polygon_feature_to_kili_polygon_annotation |
| 11 | +from .segmentation import geojson_polygon_feature_to_kili_segmentation_annotation |
| 12 | + |
| 13 | + |
| 14 | +def geojson_geometrycollection_feature_to_kili_annotations( |
| 15 | + geometrycollection: Dict[str, Any], |
| 16 | + categories: Optional[List[Dict]] = None, |
| 17 | + children: Optional[Dict] = None, |
| 18 | + mid: Optional[str] = None, |
| 19 | +) -> List[Dict[str, Any]]: |
| 20 | + """Convert a geojson geometry collection feature to Kili annotations. |
| 21 | +
|
| 22 | + Processes each geometry in the collection and converts it to the appropriate Kili annotation type. |
| 23 | + Points become marker annotations, LineStrings become polyline annotations, |
| 24 | + Polygons become polygon or semantic annotations depending on the type specified, |
| 25 | + MultiPoint and MultiLineString geometries are expanded into multiple annotations. |
| 26 | +
|
| 27 | + Args: |
| 28 | + geometrycollection: A geojson geometry collection feature. |
| 29 | + categories: The categories of the annotations. |
| 30 | + If not provided, the categories are taken from the `kili` key of the geojson feature properties. |
| 31 | + children: The children of the annotations. |
| 32 | + If not provided, the children are taken from the `kili` key of the geojson feature properties. |
| 33 | + mid: The mid of the annotations. |
| 34 | + If not provided, the mid is taken from the `id` key of the geojson feature. |
| 35 | + If no id is available, a new UUID is generated. |
| 36 | +
|
| 37 | + Returns: |
| 38 | + A list of Kili annotations corresponding to each geometry in the collection. |
| 39 | +
|
| 40 | + !!! Example |
| 41 | + ```python |
| 42 | + >>> geometrycollection = { |
| 43 | + 'type': 'Feature', |
| 44 | + 'geometry': { |
| 45 | + 'type': 'GeometryCollection', |
| 46 | + 'geometries': [ |
| 47 | + { |
| 48 | + 'type': 'Point', |
| 49 | + 'coordinates': [1.0, 2.0] |
| 50 | + }, |
| 51 | + { |
| 52 | + 'type': 'LineString', |
| 53 | + 'coordinates': [[3.0, 4.0], [5.0, 6.0]] |
| 54 | + }, |
| 55 | + { |
| 56 | + 'type': 'Polygon', |
| 57 | + 'coordinates': [[[7.0, 8.0], [9.0, 8.0], [9.0, 10.0], [7.0, 10.0], [7.0, 8.0]]] |
| 58 | + } |
| 59 | + ] |
| 60 | + }, |
| 61 | + 'id': 'complex_001', |
| 62 | + 'properties': { |
| 63 | + 'kili': { |
| 64 | + 'categories': [{'name': 'complex'}], |
| 65 | + 'children': {} |
| 66 | + } |
| 67 | + } |
| 68 | + } |
| 69 | + >>> geojson_geometrycollection_feature_to_kili_annotations(geometrycollection) |
| 70 | + [ |
| 71 | + { |
| 72 | + 'children': {}, |
| 73 | + 'point': {'x': 1.0, 'y': 2.0}, |
| 74 | + 'categories': [{'name': 'complex'}], |
| 75 | + 'mid': 'complex_001', |
| 76 | + 'type': 'marker' |
| 77 | + }, |
| 78 | + { |
| 79 | + 'children': {}, |
| 80 | + 'polyline': [{'x': 3.0, 'y': 4.0}, {'x': 5.0, 'y': 6.0}], |
| 81 | + 'categories': [{'name': 'complex'}], |
| 82 | + 'mid': 'complex_001', |
| 83 | + 'type': 'polyline' |
| 84 | + }, |
| 85 | + { |
| 86 | + 'children': {}, |
| 87 | + 'boundingPoly': [{'normalizedVertices': [{'x': 7.0, 'y': 8.0}, {'x': 9.0, 'y': 8.0}, {'x': 9.0, 'y': 10.0}, {'x': 7.0, 'y': 10.0}]}], |
| 88 | + 'categories': [{'name': 'complex'}], |
| 89 | + 'mid': 'complex_001', |
| 90 | + 'type': 'polygon' |
| 91 | + } |
| 92 | + ] |
| 93 | + ``` |
| 94 | + """ |
| 95 | + |
| 96 | + assert ( |
| 97 | + geometrycollection.get("type") == "Feature" |
| 98 | + ), f"Feature type must be `Feature`, got: {geometrycollection['type']}" |
| 99 | + assert ( |
| 100 | + geometrycollection["geometry"]["type"] == "GeometryCollection" |
| 101 | + ), f"Geometry type must be `GeometryCollection`, got: {geometrycollection['geometry']['type']}" |
| 102 | + |
| 103 | + children = children or geometrycollection["properties"].get("kili", {}).get("children", {}) |
| 104 | + categories = categories or geometrycollection["properties"]["kili"]["categories"] |
| 105 | + |
| 106 | + kili_properties = geometrycollection["properties"].get("kili", {}) |
| 107 | + annotation_type = kili_properties.get("type") |
| 108 | + |
| 109 | + annotation_mid = None |
| 110 | + if mid is not None: |
| 111 | + annotation_mid = str(mid) |
| 112 | + elif "id" in geometrycollection: |
| 113 | + annotation_mid = str(geometrycollection["id"]) |
| 114 | + else: |
| 115 | + annotation_mid = str(uuid.uuid4()) |
| 116 | + |
| 117 | + geometries = geometrycollection["geometry"]["geometries"] |
| 118 | + annotations = [] |
| 119 | + |
| 120 | + for geometry in geometries: |
| 121 | + feature = {"type": "Feature", "geometry": geometry, "properties": {"kili": kili_properties}} |
| 122 | + |
| 123 | + if geometry["type"] == "Point": |
| 124 | + if annotation_type and annotation_type != "marker": |
| 125 | + continue |
| 126 | + ann = geojson_point_feature_to_kili_point_annotation( |
| 127 | + feature, categories=categories, children=children, mid=annotation_mid |
| 128 | + ) |
| 129 | + annotations.append(ann) |
| 130 | + |
| 131 | + elif geometry["type"] == "LineString": |
| 132 | + if annotation_type and annotation_type != "polyline": |
| 133 | + continue |
| 134 | + ann = geojson_linestring_feature_to_kili_line_annotation( |
| 135 | + feature, categories=categories, children=children, mid=annotation_mid |
| 136 | + ) |
| 137 | + annotations.append(ann) |
| 138 | + |
| 139 | + elif geometry["type"] == "Polygon": |
| 140 | + if annotation_type: |
| 141 | + if annotation_type == "polygon": |
| 142 | + ann = geojson_polygon_feature_to_kili_polygon_annotation( |
| 143 | + feature, categories=categories, children=children, mid=annotation_mid |
| 144 | + ) |
| 145 | + annotations.append(ann) |
| 146 | + elif annotation_type == "semantic": |
| 147 | + anns = geojson_polygon_feature_to_kili_segmentation_annotation( |
| 148 | + feature, categories=categories, children=children, mid=annotation_mid |
| 149 | + ) |
| 150 | + annotations.extend(anns) |
| 151 | + else: |
| 152 | + ann = geojson_polygon_feature_to_kili_polygon_annotation( |
| 153 | + feature, categories=categories, children=children, mid=annotation_mid |
| 154 | + ) |
| 155 | + annotations.append(ann) |
| 156 | + |
| 157 | + elif geometry["type"] == "MultiPoint": |
| 158 | + if annotation_type and annotation_type != "marker": |
| 159 | + continue |
| 160 | + anns = geojson_multipoint_feature_to_kili_point_annotations( |
| 161 | + feature, categories=categories, children=children |
| 162 | + ) |
| 163 | + annotations.extend(anns) |
| 164 | + |
| 165 | + elif geometry["type"] == "MultiLineString": |
| 166 | + if annotation_type and annotation_type != "polyline": |
| 167 | + continue |
| 168 | + anns = geojson_multilinestring_feature_to_kili_line_annotations( |
| 169 | + feature, categories=categories, children=children |
| 170 | + ) |
| 171 | + annotations.extend(anns) |
| 172 | + |
| 173 | + elif geometry["type"] == "MultiPolygon": |
| 174 | + if annotation_type and annotation_type != "semantic": |
| 175 | + continue |
| 176 | + anns = geojson_polygon_feature_to_kili_segmentation_annotation( |
| 177 | + feature, categories=categories, children=children, mid=annotation_mid |
| 178 | + ) |
| 179 | + annotations.extend(anns) |
| 180 | + |
| 181 | + return annotations |
0 commit comments