|
| 1 | +from typing import Dict |
| 2 | + |
| 3 | + |
| 4 | +def enrich_geojson_with_kili_properties(feature_collection: Dict, json_interface: Dict) -> Dict: |
| 5 | + """Enrich GeoJSON features with kili properties when missing. |
| 6 | +
|
| 7 | + Args: |
| 8 | + feature_collection: The GeoJSON feature collection to enrich |
| 9 | + json_interface: The project's JSON interface containing job definitions |
| 10 | +
|
| 11 | + Returns: |
| 12 | + The enriched feature collection with kili properties added where needed |
| 13 | + """ |
| 14 | + object_detection_jobs = {} |
| 15 | + if "jobs" in json_interface: |
| 16 | + for job_name, job_config in json_interface["jobs"].items(): |
| 17 | + if job_config.get("mlTask") == "OBJECT_DETECTION": |
| 18 | + object_detection_jobs[job_name] = job_config |
| 19 | + |
| 20 | + marker_job = None |
| 21 | + polyline_job = None |
| 22 | + polygon_job = None |
| 23 | + semantic_job = None |
| 24 | + |
| 25 | + for job_name, job_config in object_detection_jobs.items(): |
| 26 | + tools = job_config.get("tools", []) |
| 27 | + if "marker" in tools and marker_job is None: |
| 28 | + marker_job = (job_name, job_config) |
| 29 | + if "polyline" in tools and polyline_job is None: |
| 30 | + polyline_job = (job_name, job_config) |
| 31 | + if "polygon" in tools and polygon_job is None: |
| 32 | + polygon_job = (job_name, job_config) |
| 33 | + if "semantic" in tools and semantic_job is None: |
| 34 | + semantic_job = (job_name, job_config) |
| 35 | + |
| 36 | + def get_first_category_name(job_config): |
| 37 | + categories = job_config.get("content", {}).get("categories", {}) |
| 38 | + if categories: |
| 39 | + first_category_key = next(iter(categories.keys())) |
| 40 | + return first_category_key |
| 41 | + return None |
| 42 | + |
| 43 | + def create_kili_property(job_name, job_config, annotation_type): |
| 44 | + category_name = get_first_category_name(job_config) |
| 45 | + if category_name: |
| 46 | + return { |
| 47 | + "kili": { |
| 48 | + "children": {}, |
| 49 | + "categories": [{"name": category_name}], |
| 50 | + "type": annotation_type, |
| 51 | + "job": job_name, |
| 52 | + } |
| 53 | + } |
| 54 | + return None |
| 55 | + |
| 56 | + enriched_features = [] |
| 57 | + |
| 58 | + for feature in feature_collection.get("features", []): |
| 59 | + # Skip if feature already has kili properties |
| 60 | + if feature.get("properties", {}).get("kili") is not None: |
| 61 | + enriched_features.append(feature) |
| 62 | + continue |
| 63 | + |
| 64 | + # Skip features with null geometry unless they have kili properties |
| 65 | + if feature.get("geometry") is None: |
| 66 | + continue |
| 67 | + |
| 68 | + geometry_type = feature.get("geometry", {}).get("type") |
| 69 | + kili_property = None |
| 70 | + |
| 71 | + if geometry_type == "Point" and marker_job: |
| 72 | + job_name, job_config = marker_job |
| 73 | + kili_property = create_kili_property(job_name, job_config, "marker") |
| 74 | + |
| 75 | + elif geometry_type == "LineString" and polyline_job: |
| 76 | + job_name, job_config = polyline_job |
| 77 | + kili_property = create_kili_property(job_name, job_config, "polyline") |
| 78 | + |
| 79 | + elif geometry_type == "Polygon": |
| 80 | + if polygon_job: |
| 81 | + job_name, job_config = polygon_job |
| 82 | + kili_property = create_kili_property(job_name, job_config, "polygon") |
| 83 | + elif semantic_job: |
| 84 | + job_name, job_config = semantic_job |
| 85 | + kili_property = create_kili_property(job_name, job_config, "semantic") |
| 86 | + |
| 87 | + elif geometry_type == "MultiPolygon" and semantic_job: |
| 88 | + job_name, job_config = semantic_job |
| 89 | + kili_property = create_kili_property(job_name, job_config, "semantic") |
| 90 | + |
| 91 | + if kili_property: |
| 92 | + if "properties" not in feature: |
| 93 | + feature["properties"] = {} |
| 94 | + feature["properties"].update(kili_property) |
| 95 | + enriched_features.append(feature) |
| 96 | + else: |
| 97 | + continue |
| 98 | + |
| 99 | + enriched_collection = feature_collection.copy() |
| 100 | + enriched_collection["features"] = enriched_features |
| 101 | + return enriched_collection |
0 commit comments