Skip to content
Snippets Groups Projects
Notebook_Classification_S2.ipynb 7.33 KiB
Newer Older
{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# <center>Classification Sentinel 2 using wps</center>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "1) Import Librairies"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import requests\n",
    "import json\n",
    "import sys\n",
    "import os\n",
    "import xml.etree.ElementTree as ET\n",
    "import time\n",
    "\n",
    "import sys\n",
    "sys.path.insert(0, '/projects/demo-scripts/common/')\n",
    "from owslib.wps import WebProcessingService\n",
    "\n",
    "import configparser"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#Load the file tha contains maap values\n",
    "config = configparser.ConfigParser()\n",
    "config.read('/projects/.maap/maap.ini')\n",
    "#Retrieve maap values\n",
    "copa_backend_url = config['maap']['copa_backend_url']\n",
    "CLIENT_ID = config['maap']['client_id']\n",
    "url_token = config['maap']['url_token']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#Load the file tha contains auth values\n",
    "config = configparser.ConfigParser()\n",
    "config.read('/projects/.maap/auth.ini')\n",
    "\n",
    "#Location of the credentials\n",
    "email = config['auth']['email']\n",
    "password = config['auth']['password']\n",
    "user_id = config['auth']['user_id'] "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#retrieve a token\n",
    "response = requests.post(url_token, data={'client_id': CLIENT_ID, 'username': email, 'password': password, \"grant_type\": \"password\", \"scope\": \"profile\"})\n",
    "#Convert the string to json to fecth access_token\n",
    "data = json.loads(response.text)\n",
    "token = data['access_token']\n",
    "oauth_token = token"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "source": [
    "wps = WebProcessingService(copa_backend_url + 'wps/capabilities', \\\n",
    "                           headers = {'Authorization': 'Bearer ' + oauth_token}, version =\"2.0.0\")\n",
    "\n",
    "print(\"--- GET CAPABILITIES---\")\n",
    "print(\"--- Service identification ---\")\n",
    "print('title: ',wps.identification.title)\n",
    "print('type: ',wps.identification.type, wps.identification.version)\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "source": [
    "print(\"--- Service Provider ---\")\n",
    "print('provider: ',wps.provider.name,wps.provider.contact.email,wps.provider.url) "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "source": [
    "print(\"--- Service Operations ---\")\n",
    "for operation in wps.operations:\n",
    "    print(operation.name)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "source": [
    "print(\"--- Service Processes ---\")\n",
    "print(\"there are \"+str(len(wps.processes))+\" processes available for execution\")\n",
    "for process in wps.processes:\n",
    "    print(process.identifier, process.title, process.abstract)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "source": [
    "def wps_execute(user_id, algo_wps_xml):\n",
    "    \n",
    "    print(\"--- EXECUTE---\")\n",
    "    \n",
    "    url = copa_backend_url + 'wps?userId=' + user_id\n",
    "    tree = ET.parse(algo_wps_xml)\n",
    "    \n",
    "    response = requests.post(url, headers = \\\n",
    "                             {'Authorization': 'Bearer '+oauth_token, 'Content-Type': 'application/xml'},  \\\n",
    "                             data=ET.tostring(tree.getroot()))\n",
    "    print(response.text)\n",
    "    tree = ET.fromstring(response.text)\n",
    "    \n",
    "    return tree.find('{http://www.opengis.net/wps/2.0}JobID').text\n",
    "\n",
    "dirname = os.getcwd()\n",
    "read_xml = os.path.join(dirname, 'wps_compact.xml')\n",
    "\n",
    "print(\"--- EXECUTE---\")\n",
    "\n",
    "i = 0\n",
    "\n",
    "time1 = time.time()\n",
    "job = []\n",
    "\n",
    "while i < 6:\n",
    "    jobId = wps_execute(user_id, read_xml)\n",
    "    job.append(jobId)\n",
    "    jobIds.append(jobId)\n",
    "    time.sleep(1)\n",
    "    i += 1\n",
    "\n",
    "\n",
    "while len(job) != 0:\n",
    "    \n",
    "    for Id in job:\n",
    "        \n",
    "        url = copa_backend_url + 'wps/getStatus?userId=' + user_id\n",
    "        body = { \"jobID\": Id }\n",
    "        response = requests.post(url, headers = {'Authorization': 'Bearer '+oauth_token},  json = body)\n",
    "        parsed = json.loads(response.text)\n",
    "        if parsed[\"status\"] == \"SUCCEEDED\":\n",
    "            job.remove(Id)\n",
    "        \n",
    "        \n",
    "    time.sleep(1)\n",
    "    \n",
    "    \n",
    "time2 = time.time()\n",
    "final_time = (time2 - time1)\n",
    "print('execution time was', final_time)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "source": [
    "print(\"--- GetStatus---\")\n",
    "for Id in jobIds:      \n",
    "    #FIXME retrieve the id of the user\n",
    "    url = copa_backend_url + 'wps/getStatus?userId=' + user_id\n",
    "    body = { \"jobID\": Id }\n",
    "    response = requests.post(url, headers = {'Authorization': 'Bearer '+oauth_token},  json = body)\n",
    "    parsed = json.loads(response.text)\n",
    "    print(json.dumps(parsed, indent=2, sort_keys=True))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "source": [
    "print(\"--- GetResult---\")\n",
    "for Id in jobIds:      \n",
    "    #FIXME retrieve the id of the user\n",
    "    url = copa_backend_url + 'wps/'+ Id +'?userId=' + user_id\n",
    "    response = requests.get(url, headers = {'Authorization': 'Bearer '+oauth_token})\n",
    "    print(response.text)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "print(\"--- Delete all jobs---\")\n",
    "for Id in jobIds:      \n",
    "    url = copa_backend_url + 'wps/dismiss?userId=' + user_id\n",
    "    body = { \"jobID\": Id }\n",
    "    response = requests.post(url, headers = {'Authorization': 'Bearer '+oauth_token},  json = body)\n",
    "    parsed = json.loads(response.text)\n",
    "    print(json.dumps(parsed, indent=2, sort_keys=True))\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
  }
 },
 "nbformat": 4,