|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "id": "234a6586cee4560f", |
| 6 | + "metadata": {}, |
| 7 | + "source": [ |
| 8 | + "<a href=\"https://colab.research.google.com/github/kili-technology/kili-python-sdk/blob/main/recipes/import_labels_from_shapefiles.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>\n", |
| 9 | + "\n", |
| 10 | + "# Importing Labels from Shapefiles\n", |
| 11 | + "\n", |
| 12 | + "This tutorial explains how to use `kili.append_labels_from_shapefiles` function in the Kili SDK to import geometries\n", |
| 13 | + "from shapefile files and convert them to annotations in your Kili projects.\n", |
| 14 | + "\n", |
| 15 | + "## Introduction\n", |
| 16 | + "\n", |
| 17 | + "Shapefiles are a standard geospatial data format that stores the location, shape, and attributes of geographic features.\n", |
| 18 | + "They are commonly used in Geographic Information Systems (GIS) to represent points, lines, and polygons.\n", |
| 19 | + "\n", |
| 20 | + "The `append_labels_from_shapefiles` function automatically converts this spatial data into Kili annotations.\n", |
| 21 | + "\n", |
| 22 | + "\n", |
| 23 | + "## Prerequisites\n", |
| 24 | + "\n", |
| 25 | + "Before using this feature, make sure you have the following:\n", |
| 26 | + "\n", |
| 27 | + "- A Kili project of type `IMAGE` or `GEOSPATIAL`\n", |
| 28 | + "- One or more shapefile files (`.shp`)\n", |
| 29 | + "- Knowledge of the coordinate system (EPSG code) of your shapefiles\n", |
| 30 | + "\n", |
| 31 | + "## Installation\n", |
| 32 | + "\n", |
| 33 | + "This feature requires additional dependencies. Install them using:\n", |
| 34 | + "\n", |
| 35 | + "```bash\n", |
| 36 | + "pip install 'kili[gis]'\n", |
| 37 | + "```\n", |
| 38 | + "\n", |
| 39 | + "_This command installs the necessary libraries such as pyproj which are used for geospatial data manipulation._\n", |
| 40 | + "\n", |
| 41 | + "\n", |
| 42 | + "## Function Structure\n", |
| 43 | + "\n", |
| 44 | + "The `append_labels_from_shapefiles` function takes the following parameters:\n", |
| 45 | + "\n", |
| 46 | + "| Parameter | Type | Description |\n", |
| 47 | + "|--------------------|----------------------|------------------------------------------------------------|\n", |
| 48 | + "| `project_id` | str | The Kili project identifier |\n", |
| 49 | + "| `asset_external_id` | str | The external identifier of the asset to annotate |\n", |
| 50 | + "| `shapefile_paths` | List[str] | List of paths to shapefile files |\n", |
| 51 | + "| `job_names` | List[str] | List of job names corresponding to each shapefile |\n", |
| 52 | + "| `category_names` | List[str] | List of category names corresponding to each shapefile |\n", |
| 53 | + "| `from_epsgs` | Optional[List[int]] | Optional list of source EPSG codes for each shapefile |\n", |
| 54 | + "\n", |
| 55 | + "\n", |
| 56 | + "## Supported Geometry Types\n", |
| 57 | + "\n", |
| 58 | + "The function supports the following shapefile geometry types:\n", |
| 59 | + "\n", |
| 60 | + "- Points (Type 1) - Converted to \"marker\" type annotations in Kili\n", |
| 61 | + "- Polylines (Type 3) - Converted to \"polyline\" type annotations in Kili\n", |
| 62 | + "- Polygons (Type 5) - Converted to \"semantic\" type (mask) annotations in Kili\n", |
| 63 | + "\n", |
| 64 | + "## Basic Usage Example\n", |
| 65 | + "\n", |
| 66 | + "Here's a simple usage example:\n", |
| 67 | + "\n", |
| 68 | + "```python\n", |
| 69 | + "from kili.client import Kili\n", |
| 70 | + "\n", |
| 71 | + "# Initialize Kili client\n", |
| 72 | + "kili = Kili(api_key=\"your_api_key\")\n", |
| 73 | + "\n", |
| 74 | + "# Import labels from shapefiles\n", |
| 75 | + "kili.append_labels_from_shapefiles(\n", |
| 76 | + " project_id=\"your_project_id\",\n", |
| 77 | + " asset_external_id=\"satellite_image.tif\",\n", |
| 78 | + " shapefile_paths=[\"roads.shp\", \"buildings.shp\", \"water_points.shp\"],\n", |
| 79 | + " job_names=[\"ROADS\", \"BUILDINGS\", \"HYDROLOGY\"],\n", |
| 80 | + " category_names=[\"ROAD\", \"BUILDING\", \"WATER_POINT\"],\n", |
| 81 | + " from_epsgs=[4326, 4326, 4326] # All shapefiles are in WGS84 (EPSG:4326)\n", |
| 82 | + ")\n", |
| 83 | + "```\n", |
| 84 | + "\n", |
| 85 | + "## Advanced Example with Different Coordinate Systems\n", |
| 86 | + "\n", |
| 87 | + "If your shapefiles use different coordinate systems:\n", |
| 88 | + "\n", |
| 89 | + "```python\n", |
| 90 | + "\n", |
| 91 | + "from kili.client import Kili\n", |
| 92 | + "\n", |
| 93 | + "kili = Kili(api_key=\"your_api_key\")\n", |
| 94 | + "\n", |
| 95 | + "kili.append_labels_from_shapefiles(\n", |
| 96 | + " project_id=\"your_project_id\",\n", |
| 97 | + " asset_external_id=\"sentinel2_image.jp2\",\n", |
| 98 | + " shapefile_paths=[\"observation_points.shp\", \"parcels.shp\", \"protected_areas.shp\"],\n", |
| 99 | + " job_names=[\"OBSERVATIONS\", \"PARCELS\", \"ZONES\"],\n", |
| 100 | + " category_names=[\"OBS_POINT\", \"AGRICULTURAL_PARCEL\", \"NATURE_RESERVE\"],\n", |
| 101 | + " from_epsgs=[4326, 2154, 3857] # WGS84, Lambert93, Web Mercator\n", |
| 102 | + ")\n", |
| 103 | + "```\n", |
| 104 | + "\n", |
| 105 | + "In this example, each shapefile uses a different coordinate system. The function will automatically convert all\n", |
| 106 | + "geometries to WGS84 (EPSG:4326) before importing them into Kili.\n", |
| 107 | + "\n", |
| 108 | + "\n", |
| 109 | + "## Troubleshooting\n", |
| 110 | + "\n", |
| 111 | + "### Problem: ImportError\n", |
| 112 | + "\n", |
| 113 | + "```bash\n", |
| 114 | + "ImportError: This function requires the 'gis' extra dependencies.\n", |
| 115 | + "Install them with: pip install kili[gis] or pip install 'kili[gis]'\n", |
| 116 | + "```\n", |
| 117 | + "\n", |
| 118 | + "Solution: Install the GIS dependencies:\n", |
| 119 | + "\n", |
| 120 | + "```bash\n", |
| 121 | + "pip install 'kili[gis]'\n", |
| 122 | + "```\n", |
| 123 | + "\n", |
| 124 | + "### Problem: Incorrect Coordinates or Invisible Annotations\n", |
| 125 | + "\n", |
| 126 | + "Possible solutions:\n", |
| 127 | + "\n", |
| 128 | + "- Check that you have specified the correct EPSG code for each shapefile\n", |
| 129 | + "- Make sure the image in Kili is correctly georeferenced\n", |
| 130 | + "- Check if the image has geospatial metadata with:\n", |
| 131 | + "\n", |
| 132 | + "```\n", |
| 133 | + "asset = kili.assets(project_id=\"your_project_id\", fields=[\"jsonContent\"])[0]\n", |
| 134 | + "print(asset['jsonContent'])\n", |
| 135 | + "```\n", |
| 136 | + "\n", |
| 137 | + "### Problem: Categories Not Found\n", |
| 138 | + "\n", |
| 139 | + "Solution: Check that the category names exactly match those in your Kili ontology:\n", |
| 140 | + "\n", |
| 141 | + "```python\n", |
| 142 | + "project = kili.projects(project_id=\"your_project_id\", fields=[\"jsonInterface\"])[0]\n", |
| 143 | + "print(project[\"jsonInterface\"])\n", |
| 144 | + "```\n", |
| 145 | + "\n", |
| 146 | + "\n", |
| 147 | + "## Conclusion\n", |
| 148 | + "\n", |
| 149 | + "The `append_labels_from_shapefiles` function greatly simplifies the import of geospatial data into Kili, allowing you\n", |
| 150 | + "to easily convert your existing GIS data into annotations usable for machine learning and manual annotation.\n" |
| 151 | + ] |
| 152 | + } |
| 153 | + ], |
| 154 | + "metadata": { |
| 155 | + "language_info": { |
| 156 | + "name": "python" |
| 157 | + } |
| 158 | + }, |
| 159 | + "nbformat": 4, |
| 160 | + "nbformat_minor": 5 |
| 161 | +} |
0 commit comments