{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "6f18aff3",
   "metadata": {},
   "source": [
    "Sélection (path consistency algorithm )\n",
    "\n",
    "\n",
    "# Timeslice queries\n",
    "\n",
    "\n",
    "Our focus on this section concern the selection queries with, in particular the query type over intervals spatial or temporal. Given a query interval q = [q.st, q.end], the objective is to find the ids of all objects s app S, whose intervals overlap with q, i.e. they satisfy a generalized OVERLAPS relationship. In addition, we study the retrieval of data intervals that satisfy one of Allen's interval algebra relationships with q.\n",
    "\n",
    "Stabbing queries are a special class of selection queries for which q.st = q.end and the predicate is CONTAINED_BY.\n",
    "\n",
    "\n",
    "Overall picture of selection process : selection is performed by a selector \n",
    "object, containing an arbitrary number of constraints, combined by OR, AND or \n",
    "XOR operator. Since only a single operator is allowed in the selector, a \n",
    "\"global selector\" is provided to the users to combine the output of several \n",
    "individual selectors. Again, the output may be combined with OR, AND or XOR.\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "fefaf73d",
   "metadata": {},
   "source": [
    "For example, given two circles C1 and C2, and two rectangles R1 and R2, to \n",
    "select tracks crossing either C1 or C2, and either R1 or R2, we would like to \n",
    "write the following combination of constraints : F = (C1 + C2).(R1 + R2), \n",
    "where + and . operators denote OR and AND respectively. Such a constraint \n",
    "requires two different combination rules, and therefore cannot be expressed \n",
    "with a single selector. A solution is to create two disjonctive (OR) type \n",
    "selectors S1 and S2 with S1 = C1 + C2 and S2 = R1 + R2. Then S1 and S2 are \n",
    "combined in a conjunctive (AND) type global selector. Note that boolean \n",
    "algebraic rules show that it is possible as well to combine 4 conjunctive-\n",
    "type selectors (C1.R1, C1.R2, C2.R1 and C2.R2) in a disjunctive-type global \n",
    "selector. \n",
    "Constraints may be based on:\n",
    "\n",
    "- a geometrical shape (Rectangle, circle or polygon in Geometrics). This \n",
    "     is the standard type of constraint. Different modes are:\n",
    "\n",
    "     - MODE_CROSSES: tracks crossing shape interior/boundary are selected\n",
    "     - MODE_INSIDE : tracks remaining fully inside the shape are selected\n",
    "     - MODE_GETS_IN: tracks getting in (at least once) shape are selected\n",
    "     - MODE_INSIDE : tracks getting out (at least once) shape are selected\n",
    "\n",
    "- a track t as a reference. Available modes are:\n",
    "\n",
    "     - MODE_CROSSES : tracks  intersecting t (at least once) are selected\n",
    "     - MODE_PARALLEL: tracks  following t are selected\n",
    "\n",
    "- a \"toll gate\" segment, defined by two Coords objects: tracks crossing \n",
    "     (at least once) the toll gate are selected \n",
    "\n",
    "All these constraint may be provided with an additional time constraint, \n",
    "specifying the time interval (between two GPSTime dates) where crossing /\n",
    "containing / getting in / getting out... operations are tested. \n",
    "Besides, there are two types of selection:\n",
    "\n",
    "   - TYPE_SELECT: tracks abiding by constraints are returned as they are\n",
    "   - TYPE_CUT_AND_SELECT: tracks abiding by constraints are cut and returned\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "99d22274",
   "metadata": {},
   "source": [
    "General constraint syntax:\n",
    "\n",
    "* Declaration of a TimeConstraint\n",
    "t1 = TimeConstraint(initial_date, final_date, options)\n",
    "\n",
    "* Add comment here\n",
    "c1 = Constraint(shape, t1, options)\n",
    "c2 = TrackConstraint(track, t2, options)\n",
    "c3 = TollGateConstraint(shape, t3, options)\n",
    "\n",
    "* Add comment here\n",
    "s1 = Selector(c1, c2, ..., options)\n",
    "s2 = Selector(c3, c4, ..., options)\n",
    "\n",
    "* Add comment here\n",
    "selector = GlobalSelector([s1, s2, ...], options)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9956e67d",
   "metadata": {},
   "source": [
    "## Import de la librairie tracklib"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "d361bbca",
   "metadata": {},
   "outputs": [],
   "source": [
    "import matplotlib.pyplot as plt\n",
    "import os\n",
    "import sys\n",
    "\n",
    "#-------------------------------------------------------\n",
    "# Import de tracklib\n",
    "\n",
    "module_path = os.path.abspath(os.path.join('../../..'))\n",
    "if module_path not in sys.path:\n",
    "    sys.path.append(module_path)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "a023e67a",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Conjunctive selector with following constraint(s):\n",
      "   (1) Rectangle-shaped selecting constraint (mode 'INSIDE') with temporal constraint: \n",
      "\n",
      "\n"
     ]
    }
   ],
   "source": [
    "from tracklib.core.ObsCoords import ENUCoords\n",
    "import tracklib.algo.Geometrics as Geometrics\n",
    "from tracklib.algo.Selection import Constraint\n",
    "from tracklib.algo.Selection import TYPE_CUT_AND_SELECT, MODE_INSIDE\n",
    "from tracklib.algo.Selection import Selector\n",
    "\n",
    "\n",
    "Xmin = 29.72\n",
    "Xmax = 29.77\n",
    "Ymin = 62.585\n",
    "Ymax = 62.615\n",
    "ll = ENUCoords(Xmin, Ymin)\n",
    "ur = ENUCoords(Xmax, Ymax)\n",
    "bbox = Geometrics.Rectangle(ll, ur)\n",
    "\n",
    "constraintBBox = Constraint(shape = bbox, mode = MODE_INSIDE, type=TYPE_CUT_AND_SELECT)\n",
    "\n",
    "s = Selector([constraintBBox])\n",
    "print (s)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "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.9.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
