{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Delair.ai SDK Tutorial\n", "\n", "In this notebook, you will be guided to :\n", "- Create a **project**, **mission** and **survey**\n", "- Create and upload **datasets** (image, rasters, point cloud, mesh, vector and file)\n", "- Create an **annotation** and attach a graph to it" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## ⚠️ Make sure to create a copy of this notebook before modifying it\n", "\n", "To do that `File > Make a Copy...`" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Requirements" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- Delair Python SDK : `pip install python-delairstack`" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### You may execute this notebook one cell after the other with `Shift + Enter`" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "For more options, explore `Cell` in the top menu bar, or read a [tutorial](https://www.dataquest.io/blog/jupyter-notebook-tutorial/)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import os\n", "from delairstack import DelairStackSDK" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import getpass\n", "platform_url = 'https://www.delair.ai'\n", "login = input('Enter your email ')\n", "password = getpass.getpass('Enter your password ')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "sdk = DelairStackSDK(url=platform_url, user=login, password=password)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Download sample files (images, mesh, raster...)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "An archive `Banana.zip` containing sample files will be downloaded (if not found in the current directory)." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import urllib.request\n", "import zipfile\n", "\n", "try: working_dir\n", "except NameError: working_dir = os.getcwd()\n", "%cd {working_dir}\n", "\n", "if not os.path.exists('Banana'):\n", " print('\"Banana\" folder not found')\n", " if not os.path.exists('Banana.zip'):\n", " print('\"Banana.zip\" not found')\n", " print('Downloading it...', end=' ')\n", " url = 'https://delair-transfer.s3-eu-west-1.amazonaws.com/sdks/sample-data/Banana.zip'\n", " filename, _ = urllib.request.urlretrieve(url, 'Banana.zip')\n", " print('OK')\n", "\n", " print('Extracting \"Banana.zip\"...', end=' ')\n", " with zipfile.ZipFile(filename, 'r') as zip_ref:\n", " zip_ref.extractall('.')\n", " print('OK')\n", "else:\n", " print('\"Banana\" folder found. No need to download it again.')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "sample_path = './Banana'" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "%cd {sample_path}\n", "!ls ." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Create the project" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "my_project = sdk.projects.create(\n", " name='SDK Tutorial',\n", " geometry={\"coordinates\": [[\n", " [12.397750168471589,-6.0021893390703696],\n", " [12.39799683152841,-6.0021893390703696],\n", " [12.39799683152841,-6.001515644167185],\n", " [12.397750168471589,-6.001515644167185],\n", " [12.397750168471589,-6.0021893390703696]]],\n", " \"type\": \"Polygon\"})" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "print('We just created the project {!r} with id {!r}'.format(\n", " my_project.name, my_project.id))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### List the available project properties" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "dir(my_project)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Create the survey (mission + flight)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "my_flight, my_mission = sdk.missions.create_survey(\n", " name='My survey',\n", " coordinates=[\n", " [12.398218168471589,-6.002041339094632],\n", " [12.398506831528413,-6.002041339094632],\n", " [12.398506831528413,-6.0014106608166315],\n", " [12.398218168471589,-6.0014106608166315],\n", " [12.398218168471589,-6.002041339094632]],\n", " project=my_project.id,\n", " survey_date='2015-01-31T00:00:00.000Z',\n", " number_of_images=5)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "print('We just created the mission {!r} with id {!r}'.format(\n", " my_mission.name, my_mission.id))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Create the datasets" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Image 1" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "default_horizontal_srs_wkt = 'GEOGCS[\"WGS 84\",DATUM[\"WGS_1984\",SPHEROID[\"WGS 84\",6378137,298.257223563,AUTHORITY\\\n", "[\"EPSG\",\"7030\"]],AUTHORITY[\"EPSG\",\"6326\"]],PRIMEM[\"Greenwich\",0,AUTHORITY[\"EPSG\",\"8901\"]],\\\n", "UNIT[\"degree\",0.01745329251994328,AUTHORITY[\"EPSG\",\"9122\"]],AUTHORITY[\"EPSG\",\"4326\"]]'" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "image1_dataset = sdk.datasets.create_image_dataset(\n", " name='00001499',\n", " project=my_project.id,\n", " mission=my_mission.id,\n", " flight=my_flight.id,\n", " geometry={\"type\": \"Point\",\n", " \"coordinates\": [12.397907, -6.001604983333333]},\n", " horizontal_srs_wkt=default_horizontal_srs_wkt,\n", " width=2448,\n", " height=2048\n", ")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "sdk.datasets.upload_file(\n", " dataset=image1_dataset.id,\n", " component='image',\n", " file_path='00001499.jpg')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "print('Image dataset {!r} uploaded successfully'.format(image1_dataset.name))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### List the available dataset properties" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "dir(image1_dataset)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Same for the other images" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "def create_and_upload_image_dataset(filename, coords):\n", " dataset_name = '.'.join(filename.split('.')[:-1]) # Name is filename without extension\n", " dataset = sdk.datasets.create_image_dataset(\n", " name=dataset_name,\n", " project=my_project.id,\n", " mission=my_mission.id,\n", " flight=my_flight.id,\n", " geometry={\"type\": \"Point\",\n", " \"coordinates\": coords},\n", " horizontal_srs_wkt=default_horizontal_srs_wkt,\n", " width=2448,\n", " height=2048\n", " )\n", "\n", " sdk.datasets.upload_file(\n", " dataset=dataset.id,\n", " component='image',\n", " file_path=filename)\n", " return sdk.datasets.describe(dataset=dataset.id)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "image2_dataset = create_and_upload_image_dataset(filename='00001500.jpg', coords=[12.397885, -6.001729])\n", "image3_dataset = create_and_upload_image_dataset(filename='00001501.jpg', coords=[12.397861, -6.001856])\n", "image4_dataset = create_and_upload_image_dataset(filename='00001502.jpg', coords=[12.397843983333333, -6.001977])\n", "image5_dataset = create_and_upload_image_dataset(filename='00001503.jpg', coords=[12.39784, -6.0021])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Complete the survey upload\n", "*Otherwise the upload progression will still be visible on the platform*" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "sdk.missions.complete_survey_upload(flight=my_flight.id)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Create the raster datasets" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Orthomosaic" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "ortho_dataset = sdk.datasets.create_raster_dataset(\n", " name='Orthomosaic',\n", " project=my_project.id,\n", " mission=my_mission.id,\n", " dataset_format='geotiff',\n", " categories=['orthomosaic'])\n", "\n", "sdk.datasets.upload_file(\n", " dataset=ortho_dataset.id,\n", " component='raster',\n", " file_path='Orthomosaic.tif')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## DSM (Digital Surface Model)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "dsm_dataset = sdk.datasets.create_raster_dataset(\n", " name='DSM',\n", " project=my_project.id,\n", " mission=my_mission.id,\n", " dataset_format='geotiff',\n", " categories=['dsm'])\n", "\n", "sdk.datasets.upload_file(\n", " dataset=dsm_dataset.id,\n", " component='raster',\n", " file_path='DSM.tif')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Create the point cloud dataset" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "pcl_dataset = sdk.datasets.create_pcl_dataset(\n", " name='PointCloud',\n", " project=my_project.id,\n", " mission=my_mission.id,\n", " dataset_format='las')\n", "\n", "sdk.datasets.upload_file(\n", " dataset=pcl_dataset.id,\n", " component='pcl',\n", " file_path='Point Cloud.las')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Create the mesh dataset" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "mesh_dataset = sdk.datasets.create_mesh_dataset(\n", " name='Mesh',\n", " project=my_project.id,\n", " mission=my_mission.id,\n", " dataset_format='obj',\n", " texture_count=1)\n", "\n", "sdk.datasets.upload_file(\n", " dataset=mesh_dataset.id,\n", " component='mesh',\n", " file_path='Model.obj')\n", "\n", "sdk.datasets.upload_file(\n", " dataset=mesh_dataset.id,\n", " component='material',\n", " file_path='Model.mtl')\n", "\n", "sdk.datasets.upload_file(\n", " dataset=mesh_dataset.id,\n", " component='texture',\n", " file_path='Model_0.jpg')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Create the vector dataset" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "vector_dataset = sdk.datasets.create_vector_dataset(\n", " name='ContoursLines',\n", " project=my_project.id,\n", " mission=my_mission.id,\n", " dataset_format='geojson')\n", "\n", "sdk.datasets.upload_file(\n", " dataset=vector_dataset.id,\n", " component='vector',\n", " file_path='Contours.geojson')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Create an annotation" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "my_annotation = sdk.annotations.create(\n", " project=my_project.id,\n", " mission=my_mission.id,\n", " geometry={\n", " \"type\": \"Point\",\n", " \"coordinates\": [12.397626065125216, -6.001589143208957]\n", " },\n", " name='New annotation',\n", " type='2d',\n", " icon=sdk.annotations.Icons.CONVEYOR\n", ")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# The end" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from IPython.display import Markdown, display\n", "from urllib.parse import urljoin\n", "\n", "def printmd(string):\n", " display(Markdown(string))\n", "\n", "printmd('### 💥 You can have a look at your project on the platform')\n", "project_url = urljoin(platform_url, 'app/project/{}/view;leftPanel=layers'.format(my_project.id))\n", "print(project_url)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.6.9" } }, "nbformat": 4, "nbformat_minor": 2 }