{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import geopandas as gpd\n",
    "\n",
    "level = 'communes'\n",
    "search = 'hag'\n",
    "\n",
    "gdf_communes = gpd.read_file(\"../data/raster-dl.gpkg\", layer=level)\n",
    "communes = gdf_communes[gdf_communes['nom_com'].str.lower().str.contains(search)]\n",
    "communes[['nom_com', 'insee_com']]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "code = '67186'\n",
    "buffer = 500\n",
    "\n",
    "commune = gdf_communes[gdf_communes['insee_com'] == code]\n",
    "commune = commune[['geometry', 'insee_com', 'nom_com']]\n",
    "commune['geometry']= commune.buffer(buffer)\n",
    "\n",
    "commune[['nom_com', 'insee_com']]\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "commune.plot(cmap='Greens', edgecolor='black', alpha=0.5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "gdf_dalles = gpd.read_file(\"../data/raster-dl.gpkg\", layer='dalles_ortho_67')\n",
    "dalles = gdf_dalles[['geometry', 'FILE_IMG']]\n",
    "\n",
    "dalles_seleted = gpd.sjoin(dalles, commune, how='inner', predicate='intersects')\n",
    "# dalles_seleted.plot(cmap='Greens', edgecolor='black', alpha=0.5)\n",
    "\n",
    "# a.head(2)\n",
    "nb_dalles = len(dalles_seleted)\n",
    "print('Nombre de dalles:', nb_dalles)\n",
    "\n",
    "dalles_seleted[['FILE_IMG', 'insee_com']]\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "# a.explore(\"FILE_IMG\", cmap=[\"red\"], popup=True, tooltip=\"FILE_IMG\", legend=False, width=600, height=400, tiles=\"Stamen Terrain\")._repr_html_()\n",
    "dalles_seleted.explore(\"FILE_IMG\", cmap=[\"red\"], popup=True, tooltip=\"FILE_IMG\", legend=False, width=800, height=400, tiles=\"Stamen Terrain\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "   code                name  date_premi  date_derni  date_fin_c prolongati  \\\n",
      "0     5  BALLONS des VOSGES        None  2012-05-02  2024-05-04       None   \n",
      "1    24            LORRAINE  27-01-2015  2015-01-27  2027-01-29       None   \n",
      "2    30   MONTAGNE de REIMS  29-06-1976  2009-05-04  2021-04-18       None   \n",
      "3    48      VOSGES du NORD        None  2014-03-14  2026-03-17       None   \n",
      "4     2            ARDENNES  21-12-2011  2011-12-21  2023-12-21       None   \n",
      "5    14      FORET d'ORIENT        None  2010-06-11  2021-04-02       None   \n",
      "\n",
      "   date_maj_d                                           geometry  \\\n",
      "0  2012-10-16  MULTIPOLYGON (((6.60510 47.65818, 6.60542 47.6...   \n",
      "1  2015-02-05  MULTIPOLYGON (((6.76278 48.65561, 6.76252 48.6...   \n",
      "2  2012-09-01  MULTIPOLYGON (((4.19553 49.14664, 4.19574 49.1...   \n",
      "3  2014-08-27  MULTIPOLYGON (((7.24248 48.75571, 7.24285 48.7...   \n",
      "4  2012-10-18  MULTIPOLYGON (((4.23179 49.73104, 4.22936 49.7...   \n",
      "5  2012-09-11  MULTIPOLYGON (((4.32271 48.20992, 4.32311 48.2...   \n",
      "\n",
      "         validity  \n",
      "0  Valid Geometry  \n",
      "1  Valid Geometry  \n",
      "2  Valid Geometry  \n",
      "3  Valid Geometry  \n",
      "4  Valid Geometry  \n",
      "5  Valid Geometry  \n"
     ]
    }
   ],
   "source": [
    "import geopandas as gpd\n",
    "from shapely.validation import explain_validity\n",
    "\n",
    "# absolute_path_to_file = 'P:/Test/qgis_test/lines_test.shp'\n",
    "layer = gpd.read_file(\"../data/raster-dl.gpkg\", layer='pnr')\n",
    "# shp = gpd.read_file(absolute_path_to_file)\n",
    "\n",
    "layer['validity'] = layer.apply(lambda row: explain_validity(row.geometry), axis=1)\n",
    "\n",
    "print(layer)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "   code                name  date_premi  date_derni  date_fin_c prolongati  \\\n",
      "0     5  BALLONS des VOSGES        None  2012-05-02  2024-05-04       None   \n",
      "1    24            LORRAINE  27-01-2015  2015-01-27  2027-01-29       None   \n",
      "2    30   MONTAGNE de REIMS  29-06-1976  2009-05-04  2021-04-18       None   \n",
      "3    48      VOSGES du NORD        None  2014-03-14  2026-03-17       None   \n",
      "4     2            ARDENNES  21-12-2011  2011-12-21  2023-12-21       None   \n",
      "5    14      FORET d'ORIENT        None  2010-06-11  2021-04-02       None   \n",
      "\n",
      "   date_maj_d                                           geometry  \n",
      "0  2012-10-16  MULTIPOLYGON (((6.60510 47.65818, 6.60542 47.6...  \n",
      "1  2015-02-05  MULTIPOLYGON (((6.76278 48.65561, 6.76252 48.6...  \n",
      "2  2012-09-01  MULTIPOLYGON (((4.19553 49.14664, 4.19574 49.1...  \n",
      "3  2014-08-27  MULTIPOLYGON (((7.24248 48.75571, 7.24285 48.7...  \n",
      "4  2012-10-18  MULTIPOLYGON (((4.23179 49.73104, 4.22936 49.7...  \n",
      "5  2012-09-11  MULTIPOLYGON (((4.32271 48.20992, 4.32311 48.2...  \n"
     ]
    }
   ],
   "source": [
    "import geopandas as gpd\n",
    "from shapely.validation import make_valid\n",
    "\n",
    "layer = gpd.read_file(\"../data/raster-dl.gpkg\", layer='pnr')\n",
    "\n",
    "layer.geometry = layer.apply(lambda row: make_valid(row.geometry) if not row.geometry.is_valid else row.geometry, axis=1)\n",
    "\n",
    "layer.to_file(\"../data/raster-dl.gpkg\", layer='pnr2', driver=\"GPKG\")\n",
    "\n",
    "print(layer)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 102,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 29920 characters]",
      "text/plain": [
       "<Figure size 600x600 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "import matplotlib.pyplot as plt\n",
    "import geopandas as gpd\n",
    "from shapely.validation import make_valid\n",
    "\n",
    "gdf_level = gpd.read_file(\"../data/raster-dl.gpkg\", layer='pnr')\n",
    "level_feature = gdf_level[gdf_level['code'] == 48]\n",
    "level_feature = level_feature[['geometry', 'code', 'name']]\n",
    "        \n",
    "# if bbox:\n",
    "#     bbox_feature = get_bbox(level_feature, buffer)\n",
    "#     extent = bbox_feature    \n",
    "# else:\n",
    "#     level_feature['geometry']= level_feature.buffer(int(buffer))\n",
    "#     bbox_feature = get_bbox(level_feature)\n",
    "#     extent = level_feature\n",
    "extent = level_feature\n",
    "# extent.plot(cmap='Greens',edgecolor='black', alpha=0.5)\n",
    "# print(extent)\n",
    "\n",
    "gdf_products = gpd.read_file(\"../data/raster-dl.gpkg\", layer='products')\n",
    "# products_seleted = gpd.overlay(gdf_products, extent, how='difference')\n",
    "# products_seleted = gpd.overlay(extent, gdf_products, how='intersection')\n",
    "products_seleted = gpd.sjoin(gdf_products, extent, how='inner', predicate='intersects')\n",
    "# products_seleted = gpd.sjoin(extent, gdf_products, how='right', predicate='contains')\n",
    "# products_seleted = gpd.sjoin(extent, gdf_products, how='right', predicate='within')\n",
    "# products_seleted = gpd.sjoin(extent, gdf_products, how='right', predicate='touches')\n",
    "# products_seleted = gpd.sjoin(extent, gdf_products, how='right', predicate='crosses')\n",
    "# products_seleted = gpd.sjoin(extent, gdf_products, how='right', predicate='overlaps')\n",
    "# products_seleted['code'] = products_seleted['code_left']\n",
    "# products_seleted = products_seleted[['code', 'data_extent', 'data_product', 'data_date', 'data_resolution', 'data_compression', 'data_type', 'metadata_url', 'data_directory', 'data_url', 'layer_tiles', 'parent']]\n",
    "# products_seleted = products_seleted[products_seleted['code_right'] == 48]\n",
    "\n",
    "# products_seleted.head(50)\n",
    "\n",
    "# products_seleted.plot(cmap='Greens', alpha=0.5)\n",
    "# Generate plot map\n",
    "fig = plt.figure(figsize=(6, 6))\n",
    "spec = fig.add_gridspec(ncols=1, nrows=1)\n",
    "plot_map = fig.add_subplot(spec[0, 0])\n",
    "# fig.suptitle('Selection des produits sur {extent}'.format(extent=extent.iloc[0]['name']))\n",
    "extent.plot(facecolor='None', edgecolor='blue', alpha=0.5, ax=plot_map)\n",
    "products_seleted.plot(facecolor='None', edgecolor='red', alpha=0.5, ax=plot_map)\n",
    "plt.axis('off')\n",
    "# plt.savefig('./web/products.png')\n",
    "plt.show()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "venv",
   "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.10.6"
  },
  "orig_nbformat": 4,
  "vscode": {
   "interpreter": {
    "hash": "adac29f101240a8cac2f47e47a2d9747c19dcb5e071fdfcf39523cae4909c500"
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
