Skip to content

Open In Colab

Converting Kili label format to GeoJSON

In this recipe, we will see how to convert Kili label format to GeoJSON format.

First, let's install and import the required libraries:

%pip install kili
import json
import urllib.request

from kili.client import Kili
kili = Kili(
    # api_endpoint="https://cloud.kili-technology.com/api/label/v2/graphql",
    # the line above can be uncommented and changed if you are working with an on-premise version of Kili
)

Data

For this tutorial, we will use a GeoTiff file created from Copernicus Sentinel-2A data.

urllib.request.urlretrieve(
    "https://github.com/mommermi/geotiff_sample/raw/master/sample.tif", "sample.tif"
)

Kili project creation

We will create a project with a single asset, the GeoTiff file we just downloaded.

json_interface = {
    "jobs": {
        "BBOX_DETECTION_JOB": {
            "content": {
                "categories": {"B_BOX_A": {"children": [], "color": "#472CED", "name": "BBox A"}},
                "input": "radio",
            },
            "instruction": "BBox job",
            "mlTask": "OBJECT_DETECTION",
            "required": 1,
            "tools": ["rectangle"],
            "isChild": False,
        },
        "POINT_DETECTION_JOB": {
            "content": {
                "categories": {"POINT_A": {"children": [], "color": "#D33BCE", "name": "Point A"}},
                "input": "radio",
            },
            "instruction": "Point job",
            "mlTask": "OBJECT_DETECTION",
            "required": 1,
            "tools": ["marker"],
            "isChild": False,
        },
        "POLYGON_DETECTION_JOB": {
            "content": {
                "categories": {
                    "POLYGON_A": {"children": [], "color": "#3BCADB", "name": "Polygon A"}
                },
                "input": "radio",
            },
            "instruction": "Polygon job",
            "mlTask": "OBJECT_DETECTION",
            "required": 1,
            "tools": ["polygon"],
            "isChild": False,
        },
        "LINE_DETECTION_JOB": {
            "content": {
                "categories": {"LINE_A": {"children": [], "color": "#5CE7B7", "name": "Line A"}},
                "input": "radio",
            },
            "instruction": "Line job",
            "mlTask": "OBJECT_DETECTION",
            "required": 1,
            "tools": ["polyline"],
            "isChild": False,
        },
        "SEGMENTATION_JOB": {
            "content": {
                "categories": {
                    "SEGMENTATION_A": {"children": [], "color": "#FB753C", "name": "Segmentation A"}
                },
                "input": "radio",
            },
            "instruction": "Segmentation job",
            "mlTask": "OBJECT_DETECTION",
            "required": 1,
            "tools": ["semantic"],
            "isChild": False,
        },
    }
}
project_id = kili.create_project(
    input_type="IMAGE", title="[Kili SDK Notebook]: Geojson tutorial", json_interface=json_interface
)["id"]
kili.append_many_to_dataset(project_id, content_array=["sample.tif"], external_id_array=["sample"])

At this point, we can visualize the asset in Kili: