{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "01564245-66da-4368-bd0d-09b9669ab06c",
   "metadata": {},
   "outputs": [],
   "source": [
    "from shapely.wkt import loads\n",
    "geom = loads(\"POLYGON((-1.26 51.75, -1.25 51.75, -1.25 51.76, -1.26 51.76, -1.26 51.75))\")\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "a7299dc8-dcb5-45a1-9a8e-1e1f7b371e41",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "POLYGON ((-1.26 51.75, -1.25 51.75, -1.25 51.76, -1.26 51.76, -1.26 51.75))\n",
      "POLYGON ((451181.8067423534 205959.42761765362, 451872.1365434999 205966.47458434716, 451860.70613464457 207078.6670373804, 451170.5288039437 207071.62065761362, 451181.8067423534 205959.42761765362))\n"
     ]
    }
   ],
   "source": [
    "import geopandas as gpd\n",
    "buffer_m=1000\n",
    "gdf_latlon = gpd.GeoDataFrame(geometry=[geom], crs=\"EPSG:4326\")\n",
    "print(gdf_latlon.iloc[0].geometry)\n",
    "gdf_ukgrid=gdf_latlon.to_crs(27700)\n",
    "print(gdf_ukgrid.iloc[0].geometry)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "83b6c19e-5ce4-44fe-8eb0-18da642233bc",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "0adcd724-8b5d-4f7d-8d54-5843e59ef51e",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "POLYGON ((449202.1904339386 203939.11526180914, 449150.35352661525 209051.0998678343, 453840.0482665709 209098.97940351022, 453892.58599731687 203986.99536384118, 449202.1904339386 203939.11526180914))\n"
     ]
    }
   ],
   "source": [
    "gdf_ukgrid[\"geometry\"] = gdf_ukgrid.buffer(buffer_m,resolution=4, join_style=\"mitre\")\n",
    "print(gdf_ukgrid.iloc[0].geometry)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "06179dda-8765-4d8a-8a35-9cba58219cbf",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "POLYGON ((-1.2889586296821363 51.73201354199302, -1.2889880438846308 51.777976757023254, -1.2210119164828335 51.777976650545504, -1.2210413306757637 51.7320136485258, -1.2889586296821363 51.73201354199302))\n"
     ]
    }
   ],
   "source": [
    "gdf_buffered_latlon = gdf_ukgrid.to_crs(4326)\n",
    "wkt_with_buffer = gdf_buffered_latlon.iloc[0].geometry\n",
    "print(wkt_with_buffer)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "8454068e-a274-486e-a0b7-97ec03aabec3",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div style=\"width:100%;\"><div style=\"position:relative;width:100%;height:0;padding-bottom:60%;\"><span style=\"color:#565656\">Make this Notebook Trusted to load map: File -> Trust Notebook</span><iframe srcdoc=\"&lt;!DOCTYPE html&gt;\n",
       "&lt;html&gt;\n",
       "&lt;head&gt;\n",
       "    \n",
       "    &lt;meta http-equiv=&quot;content-type&quot; content=&quot;text/html; charset=UTF-8&quot; /&gt;\n",
       "    &lt;script src=&quot;https://cdn.jsdelivr.net/npm/leaflet@1.9.3/dist/leaflet.js&quot;&gt;&lt;/script&gt;\n",
       "    &lt;script src=&quot;https://code.jquery.com/jquery-3.7.1.min.js&quot;&gt;&lt;/script&gt;\n",
       "    &lt;script src=&quot;https://cdn.jsdelivr.net/npm/bootstrap@5.2.2/dist/js/bootstrap.bundle.min.js&quot;&gt;&lt;/script&gt;\n",
       "    &lt;script src=&quot;https://cdnjs.cloudflare.com/ajax/libs/Leaflet.awesome-markers/2.0.2/leaflet.awesome-markers.js&quot;&gt;&lt;/script&gt;\n",
       "    &lt;link rel=&quot;stylesheet&quot; href=&quot;https://cdn.jsdelivr.net/npm/leaflet@1.9.3/dist/leaflet.css&quot;/&gt;\n",
       "    &lt;link rel=&quot;stylesheet&quot; href=&quot;https://cdn.jsdelivr.net/npm/bootstrap@5.2.2/dist/css/bootstrap.min.css&quot;/&gt;\n",
       "    &lt;link rel=&quot;stylesheet&quot; href=&quot;https://netdna.bootstrapcdn.com/bootstrap/3.0.0/css/bootstrap-glyphicons.css&quot;/&gt;\n",
       "    &lt;link rel=&quot;stylesheet&quot; href=&quot;https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free@6.2.0/css/all.min.css&quot;/&gt;\n",
       "    &lt;link rel=&quot;stylesheet&quot; href=&quot;https://cdnjs.cloudflare.com/ajax/libs/Leaflet.awesome-markers/2.0.2/leaflet.awesome-markers.css&quot;/&gt;\n",
       "    &lt;link rel=&quot;stylesheet&quot; href=&quot;https://cdn.jsdelivr.net/gh/python-visualization/folium/folium/templates/leaflet.awesome.rotate.min.css&quot;/&gt;\n",
       "    \n",
       "            &lt;meta name=&quot;viewport&quot; content=&quot;width=device-width,\n",
       "                initial-scale=1.0, maximum-scale=1.0, user-scalable=no&quot; /&gt;\n",
       "            &lt;style&gt;\n",
       "                #map_05df2a629c437263816b474c31e3c4f5 {\n",
       "                    position: relative;\n",
       "                    width: 100.0%;\n",
       "                    height: 100.0%;\n",
       "                    left: 0.0%;\n",
       "                    top: 0.0%;\n",
       "                }\n",
       "                .leaflet-container { font-size: 1rem; }\n",
       "            &lt;/style&gt;\n",
       "\n",
       "            &lt;style&gt;html, body {\n",
       "                width: 100%;\n",
       "                height: 100%;\n",
       "                margin: 0;\n",
       "                padding: 0;\n",
       "            }\n",
       "            &lt;/style&gt;\n",
       "\n",
       "            &lt;style&gt;#map {\n",
       "                position:absolute;\n",
       "                top:0;\n",
       "                bottom:0;\n",
       "                right:0;\n",
       "                left:0;\n",
       "                }\n",
       "            &lt;/style&gt;\n",
       "\n",
       "            &lt;script&gt;\n",
       "                L_NO_TOUCH = false;\n",
       "                L_DISABLE_3D = false;\n",
       "            &lt;/script&gt;\n",
       "\n",
       "        \n",
       "    \n",
       "                    &lt;style&gt;\n",
       "                        .foliumtooltip {\n",
       "                            \n",
       "                        }\n",
       "                       .foliumtooltip table{\n",
       "                            margin: auto;\n",
       "                        }\n",
       "                        .foliumtooltip tr{\n",
       "                            text-align: left;\n",
       "                        }\n",
       "                        .foliumtooltip th{\n",
       "                            padding: 2px; padding-right: 8px;\n",
       "                        }\n",
       "                    &lt;/style&gt;\n",
       "            \n",
       "&lt;/head&gt;\n",
       "&lt;body&gt;\n",
       "    \n",
       "    \n",
       "            &lt;div class=&quot;folium-map&quot; id=&quot;map_05df2a629c437263816b474c31e3c4f5&quot; &gt;&lt;/div&gt;\n",
       "        \n",
       "&lt;/body&gt;\n",
       "&lt;script&gt;\n",
       "    \n",
       "    \n",
       "            var map_05df2a629c437263816b474c31e3c4f5 = L.map(\n",
       "                &quot;map_05df2a629c437263816b474c31e3c4f5&quot;,\n",
       "                {\n",
       "                    center: [51.75499514950813, -1.2549999801837322],\n",
       "                    crs: L.CRS.EPSG3857,\n",
       "                    ...{\n",
       "  &quot;zoom&quot;: 10,\n",
       "  &quot;zoomControl&quot;: true,\n",
       "  &quot;preferCanvas&quot;: false,\n",
       "}\n",
       "\n",
       "                }\n",
       "            );\n",
       "            L.control.scale().addTo(map_05df2a629c437263816b474c31e3c4f5);\n",
       "\n",
       "            \n",
       "\n",
       "        \n",
       "    \n",
       "            var tile_layer_86095d769bd7e6243bfa0c5202c732a5 = L.tileLayer(\n",
       "                &quot;https://tile.openstreetmap.org/{z}/{x}/{y}.png&quot;,\n",
       "                {\n",
       "  &quot;minZoom&quot;: 0,\n",
       "  &quot;maxZoom&quot;: 19,\n",
       "  &quot;maxNativeZoom&quot;: 19,\n",
       "  &quot;noWrap&quot;: false,\n",
       "  &quot;attribution&quot;: &quot;\\u0026copy; \\u003ca href=\\&quot;https://www.openstreetmap.org/copyright\\&quot;\\u003eOpenStreetMap\\u003c/a\\u003e contributors&quot;,\n",
       "  &quot;subdomains&quot;: &quot;abc&quot;,\n",
       "  &quot;detectRetina&quot;: false,\n",
       "  &quot;tms&quot;: false,\n",
       "  &quot;opacity&quot;: 1,\n",
       "}\n",
       "\n",
       "            );\n",
       "        \n",
       "    \n",
       "            tile_layer_86095d769bd7e6243bfa0c5202c732a5.addTo(map_05df2a629c437263816b474c31e3c4f5);\n",
       "        \n",
       "    \n",
       "            map_05df2a629c437263816b474c31e3c4f5.fitBounds(\n",
       "                [[51.73201354199302, -1.2889880438846308], [51.777976757023254, -1.2210119164828335]],\n",
       "                {}\n",
       "            );\n",
       "        \n",
       "    \n",
       "        function geo_json_6fead0dde23582111fbc644e7e2566e8_styler(feature) {\n",
       "            switch(feature.id) {\n",
       "                default:\n",
       "                    return {&quot;fillOpacity&quot;: 0.5, &quot;weight&quot;: 2};\n",
       "            }\n",
       "        }\n",
       "        function geo_json_6fead0dde23582111fbc644e7e2566e8_highlighter(feature) {\n",
       "            switch(feature.id) {\n",
       "                default:\n",
       "                    return {&quot;fillOpacity&quot;: 0.75};\n",
       "            }\n",
       "        }\n",
       "        function geo_json_6fead0dde23582111fbc644e7e2566e8_pointToLayer(feature, latlng) {\n",
       "            var opts = {\n",
       "  &quot;stroke&quot;: true,\n",
       "  &quot;color&quot;: &quot;#3388ff&quot;,\n",
       "  &quot;weight&quot;: 3,\n",
       "  &quot;opacity&quot;: 1.0,\n",
       "  &quot;lineCap&quot;: &quot;round&quot;,\n",
       "  &quot;lineJoin&quot;: &quot;round&quot;,\n",
       "  &quot;dashArray&quot;: null,\n",
       "  &quot;dashOffset&quot;: null,\n",
       "  &quot;fill&quot;: true,\n",
       "  &quot;fillColor&quot;: &quot;#3388ff&quot;,\n",
       "  &quot;fillOpacity&quot;: 0.2,\n",
       "  &quot;fillRule&quot;: &quot;evenodd&quot;,\n",
       "  &quot;bubblingMouseEvents&quot;: true,\n",
       "  &quot;radius&quot;: 2,\n",
       "};\n",
       "            \n",
       "            let style = geo_json_6fead0dde23582111fbc644e7e2566e8_styler(feature)\n",
       "            Object.assign(opts, style)\n",
       "            \n",
       "            return new L.CircleMarker(latlng, opts)\n",
       "        }\n",
       "\n",
       "        function geo_json_6fead0dde23582111fbc644e7e2566e8_onEachFeature(feature, layer) {\n",
       "\n",
       "            layer.on({\n",
       "                mouseout: function(e) {\n",
       "                    if(typeof e.target.setStyle === &quot;function&quot;){\n",
       "                            geo_json_6fead0dde23582111fbc644e7e2566e8.resetStyle(e.target);\n",
       "                    }\n",
       "                },\n",
       "                mouseover: function(e) {\n",
       "                    if(typeof e.target.setStyle === &quot;function&quot;){\n",
       "                        const highlightStyle = geo_json_6fead0dde23582111fbc644e7e2566e8_highlighter(e.target.feature)\n",
       "                        e.target.setStyle(highlightStyle);\n",
       "                    }\n",
       "                },\n",
       "            });\n",
       "        };\n",
       "        var geo_json_6fead0dde23582111fbc644e7e2566e8 = L.geoJson(null, {\n",
       "                onEachFeature: geo_json_6fead0dde23582111fbc644e7e2566e8_onEachFeature,\n",
       "            \n",
       "                style: geo_json_6fead0dde23582111fbc644e7e2566e8_styler,\n",
       "                pointToLayer: geo_json_6fead0dde23582111fbc644e7e2566e8_pointToLayer,\n",
       "            ...{\n",
       "}\n",
       "        });\n",
       "\n",
       "        function geo_json_6fead0dde23582111fbc644e7e2566e8_add (data) {\n",
       "            geo_json_6fead0dde23582111fbc644e7e2566e8\n",
       "                .addData(data);\n",
       "        }\n",
       "            geo_json_6fead0dde23582111fbc644e7e2566e8_add({&quot;bbox&quot;: [-1.2889880438846308, 51.73201354199302, -1.2210119164828335, 51.777976757023254], &quot;features&quot;: [{&quot;bbox&quot;: [-1.2889880438846308, 51.73201354199302, -1.2210119164828335, 51.777976757023254], &quot;geometry&quot;: {&quot;coordinates&quot;: [[[-1.2889586296821363, 51.73201354199302], [-1.2889880438846308, 51.777976757023254], [-1.2210119164828335, 51.777976650545504], [-1.2210413306757637, 51.7320136485258], [-1.2889586296821363, 51.73201354199302]]], &quot;type&quot;: &quot;Polygon&quot;}, &quot;id&quot;: &quot;0&quot;, &quot;properties&quot;: {}, &quot;type&quot;: &quot;Feature&quot;}], &quot;type&quot;: &quot;FeatureCollection&quot;});\n",
       "\n",
       "        \n",
       "    \n",
       "    geo_json_6fead0dde23582111fbc644e7e2566e8.bindTooltip(\n",
       "    function(layer){\n",
       "    let div = L.DomUtil.create(&#x27;div&#x27;);\n",
       "    \n",
       "    return div\n",
       "    }\n",
       "    ,{\n",
       "  &quot;sticky&quot;: true,\n",
       "  &quot;className&quot;: &quot;foliumtooltip&quot;,\n",
       "});\n",
       "                     \n",
       "    \n",
       "            geo_json_6fead0dde23582111fbc644e7e2566e8.addTo(map_05df2a629c437263816b474c31e3c4f5);\n",
       "        \n",
       "&lt;/script&gt;\n",
       "&lt;/html&gt;\" style=\"position:absolute;width:100%;height:100%;left:0;top:0;border:none !important;\" allowfullscreen webkitallowfullscreen mozallowfullscreen></iframe></div></div>"
      ],
      "text/plain": [
       "<folium.folium.Map at 0x16baed700>"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "gdf_buffered_latlon.explore()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "333e112c-8e37-412f-800e-8ac4a3ea5732",
   "metadata": {},
   "source": [
    "These are teh recognisable lists we are interested in for the MVP\n",
    "\n",
    "Protected lists \n",
    "WCA Schedule 5  (dr2402)\n",
    "Schedule 1 (Birds)  (dr2413)\n",
    "Schedule 8 (Plants) (dr2414)\n",
    "\n",
    "Priority lists\n",
    "NERC Section 41 (England) (dr590)\n",
    "UK BAP Priority Species (dr583)\n",
    "\n",
    "\n",
    "Now get the species counts for all these lists\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "cc4194ef-9ed6-4773-85ac-0bd32ad0b8f2",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "https://records-ws.nbnatlas.org/occurrence/facets?facets=taxon_name&flimit=0&q=((species_list_uid%3A\"dr2402\" OR species_list_uid%3A\"dr2413\" OR species_list_uid%3A\"dr2414\" OR species_list_uid%3A\"dr590\" OR species_list_uid%3A\"dr583\") AND -(occurrence_status%3A\"absent\" OR identification_verification_status%3A\"Unconfirmed\" OR identification_verification_status%3A\"Unconfirmed - not reviewed\" OR identification_verification_status%3A\"Unconfirmed - plausible\"))&wkt=POLYGON((-1.26 51.75, -1.25 51.75, -1.25 51.76, -1.26 51.76, -1.26 51.75))\n"
     ]
    }
   ],
   "source": [
    "import requests\n",
    "QUERY_A_TOP_SPECIES_COUNTS_BASE_URL = 'https://records-ws.nbnatlas.org/occurrence/facets?facets=taxon_name&flimit=2&q=((species_list_uid%3A\"dr2402\" OR species_list_uid%3A\"dr2413\" OR species_list_uid%3A\"dr2414\" OR species_list_uid%3A\"dr590\" OR species_list_uid%3A\"dr583\") AND -(occurrence_status%3A\"absent\" OR identification_verification_status%3A\"Unconfirmed\" OR identification_verification_status%3A\"Unconfirmed - not reviewed\" OR identification_verification_status%3A\"Unconfirmed - plausible\"))'\n",
    "url = QUERY_A_TOP_SPECIES_COUNTS_BASE_URL + '&wkt=POLYGON((-1.26 51.75, -1.25 51.75, -1.25 51.76, -1.26 51.76, -1.26 51.75))'\n",
    "print(url)\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4a766bb4-681a-4eeb-848a-5b72dbc0fea8",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "810ddfa1-fc14-4a7c-b59b-7c6487024e85",
   "metadata": {},
   "outputs": [],
   "source": [
    "response = requests.get(url)\n",
    "data = response.json()\n",
    "\n",
    "for fieldResult in data[0][\"fieldResult\"]:\n",
    "    print(f'{fieldResult[\"label\"]} {fieldResult[\"count\"]}')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "66474e6c-0b1f-4b41-8d59-7d418ae3369c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[{'fieldName': 'taxon_name', 'fieldResult': [], 'count': 45}]\n"
     ]
    }
   ],
   "source": [
    "print(data)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "id": "5c7ed4bd-8993-4135-87c3-a910c822ac3b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[{'fieldName': 'taxon_name', 'fieldResult': [], 'count': 45}]\n"
     ]
    }
   ],
   "source": [
    "QUERY_A2_UNIQUE_SPECIES_COUNT_BASE_URL = 'https://records-ws.nbnatlas.org/occurrence/facets?facets=taxon_name&flimit=0&q=((species_list_uid%3A\"dr2402\" OR species_list_uid%3A\"dr2413\" OR species_list_uid%3A\"dr2414\" OR species_list_uid%3A\"dr590\" OR species_list_uid%3A\"dr583\") AND -(occurrence_status%3A\"absent\" OR identification_verification_status%3A\"Unconfirmed\" OR identification_verification_status%3A\"Unconfirmed - not reviewed\" OR identification_verification_status%3A\"Unconfirmed - plausible\"))'\n",
    "url = QUERY_A2_UNIQUE_SPECIES_COUNT_BASE_URL+f'&species_list_uid=dr2402&wkt={wkt_with_buffer}'\n",
    "response = requests.get(url)\n",
    "\n",
    "data = response.json()\n",
    "print(data)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "id": "0d9d6966-4a4a-44c8-a099-dab566824920",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'firstname': 'john', 'lastname': 'jones', 'age': 21}\n"
     ]
    }
   ],
   "source": [
    "tmp_dict = {'firstname':'john', 'lastname':'jones'}\n",
    "new_dict = {**tmp_dict,'age':21}\n",
    "print(new_dict)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7c98fc1d-9f68-4b21-b6b7-c90a77f54291",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python (species-alert-report)",
   "language": "python",
   "name": "species-alert-report"
  },
  "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.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
