{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "#!pip install piexif\n",
    "import piexif\n",
    "from PIL import Image\n",
    "import os\n",
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Open source path and destination path"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "orgPath='../sensordata/'\n",
    "destPath='../images/'"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Open position data as pandas dataframe"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>latitude</th>\n",
       "      <th>longitude</th>\n",
       "      <th>altitude</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>imageName</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>DSC00038.JPG</th>\n",
       "      <td>-11.929715</td>\n",
       "      <td>-37.662185</td>\n",
       "      <td>167.057</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>DSC00039.JPG</th>\n",
       "      <td>-11.929877</td>\n",
       "      <td>-37.662186</td>\n",
       "      <td>167.417</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>DSC00040.JPG</th>\n",
       "      <td>-11.930037</td>\n",
       "      <td>-37.662191</td>\n",
       "      <td>167.231</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>DSC00041.JPG</th>\n",
       "      <td>-11.930192</td>\n",
       "      <td>-37.662196</td>\n",
       "      <td>166.544</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>DSC00042.JPG</th>\n",
       "      <td>-11.930352</td>\n",
       "      <td>-37.662197</td>\n",
       "      <td>167.226</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "               latitude  longitude  altitude\n",
       "imageName                                   \n",
       "DSC00038.JPG -11.929715 -37.662185   167.057\n",
       "DSC00039.JPG -11.929877 -37.662186   167.417\n",
       "DSC00040.JPG -11.930037 -37.662191   167.231\n",
       "DSC00041.JPG -11.930192 -37.662196   166.544\n",
       "DSC00042.JPG -11.930352 -37.662197   167.226"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "gpsPos = pd.read_csv('../txt/telemetry.dat',index_col=0)\n",
    "gpsPos.head()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Definition of working function for lat and lon parsing"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "def convertLat(lat):\n",
    "    if lat > 0:\n",
    "        latMarker = b'N'\n",
    "    else: \n",
    "        latMarker = b'S'\n",
    "    \n",
    "    lat = abs(lat)\n",
    "    degress = int(lat)\n",
    "    \n",
    "    tempMinutes = (lat % 1)*60\n",
    "    minutes = int(tempMinutes)\n",
    "    \n",
    "    tempSeconds = (tempMinutes % 1)*60   \n",
    "    seconds = int(tempSeconds * 10000)\n",
    "    \n",
    "    latTuple = ((degress, 1), (minutes, 1), (seconds, 10000))\n",
    "    return latMarker, latTuple"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "def convertLon(lon):\n",
    "    if lon > 0:\n",
    "        lonMarker = b'E'\n",
    "    else: \n",
    "        lonMarker = b'W'\n",
    "    \n",
    "    lon = abs(lon)\n",
    "    degress = int(lon)\n",
    "    \n",
    "    tempMinutes = (lon % 1)*60\n",
    "    minutes = int(tempMinutes)\n",
    "    \n",
    "    tempSeconds = (tempMinutes % 1)*60   \n",
    "    seconds = int(tempSeconds * 10000)\n",
    "    \n",
    "    lonTuple = ((degress, 1), (minutes, 1), (seconds, 10000))\n",
    "    \n",
    "    return lonMarker, lonTuple"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Loop over files to save geolocated version"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "for index, row in gpsPos.iterrows():\n",
    "    #working with the exif data\n",
    "    exifDict = {'GPS':\n",
    "                {0: (2, 4, 0, 0),\n",
    "                 5: 0}\n",
    "               }\n",
    "    latMarker, latTuple = convertLat(row.latitude)\n",
    "    lonMarker, lonTuple = convertLon(row.longitude)\n",
    "    exifDict['GPS'][1] = latMarker\n",
    "    exifDict['GPS'][2] = latTuple\n",
    "    exifDict['GPS'][3] = lonMarker\n",
    "    exifDict['GPS'][4] = lonTuple\n",
    "    exifDict['GPS'][6] = (int(row.altitude*1000),1000)\n",
    "    #working with the file\n",
    "    #opening the original file\n",
    "    orgName=os.path.join(orgPath,index)\n",
    "    img=Image.open(orgName)\n",
    "    #write the destination file\n",
    "    destNname=os.path.join(destPath,index)\n",
    "    exifBytes = piexif.dump(exifDict)\n",
    "    img.save(destNname, exif=exifBytes)"
   ]
  },
  {
   "cell_type": "raw",
   "metadata": {},
   "source": [
    "# ODM command in docker to run the drone image processing\n",
    "docker run -it --rm -v \"$(pwd)/images:/code/images\" -v \"$(pwd)/odm_orthophoto:/code/odm_orthophoto\" -v \"$(pwd)/odm_texturing:/code/odm_texturing\" opendronemap/odm"
   ]
  }
 ],
 "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.8.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
