{ "cells": [ { "cell_type": "markdown", "id": "2544f47f", "metadata": {}, "source": [ "## Interactive maps using geemap" ] }, { "cell_type": "code", "execution_count": 1, "id": "276183d9", "metadata": {}, "outputs": [], "source": [ "import ee\n", "import geemap.eefolium as geemap\n", "import matplotlib.pyplot as plt" ] }, { "cell_type": "code", "execution_count": 2, "id": "13bbaff2", "metadata": {}, "outputs": [], "source": [ "ee.Initialize()" ] }, { "cell_type": "markdown", "id": "d716bb3c", "metadata": {}, "source": [ "#### Florida mangrove region" ] }, { "cell_type": "code", "execution_count": 3, "id": "04db0ff8", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
Make this Notebook Trusted to load map: File -> Trust Notebook
" ], "text/plain": [ "" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Retrive dataset as a collection of images from Earth Engine Catalog\n", "mangrove_images_landsat = ee.ImageCollection('LANDSAT/MANGROVE_FORESTS')\n", "\n", "# Grab the first image from the collection (there's only one in the collection)\n", "mangrove_images_landsat = mangrove_images_landsat.first()\n", "\n", "# Specify visualization parameters\n", "mangrovesVis = {\n", " min: 0,\n", " max: 1.0,\n", " 'palette': ['d40115'],\n", " }\n", "\n", "# Instantiate a map\n", "mangrove_map = geemap.Map() \n", "\n", "# Add mangrove dataset as a leyer to the interactive map using the visualization parameters\n", "mangrove_map.addLayer(mangrove_images_landsat, mangrovesVis, 'Mangroves')\n", "\n", "# Center the map using longitude, latitude, zoom level\n", "mangrove_map.setCenter(-81, 25, 9)\n", "\n", "# Display the map\n", "mangrove_map" ] }, { "cell_type": "code", "execution_count": 4, "id": "817f6822", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'IMAGE_DATE': '2000-01-01',\n", " 'NOMINAL_SCALE': 30.359861978395436,\n", " 'system:asset_size': '41.133541 MB',\n", " 'system:band_names': ['1'],\n", " 'system:id': 'LANDSAT/MANGROVE_FORESTS/2000',\n", " 'system:index': '2000',\n", " 'system:time_end': '2001-01-01 00:00:00',\n", " 'system:time_start': '2000-01-01 00:00:00',\n", " 'system:version': 1506796895089836}" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Get useful information about the dataset\n", "geemap.image_props(mangrove_images_landsat).getInfo()" ] }, { "cell_type": "markdown", "id": "35156a7f", "metadata": {}, "source": [ "#### Suriname mangrove region" ] }, { "cell_type": "code", "execution_count": 5, "id": "8afd4c7d", "metadata": {}, "outputs": [], "source": [ "# define the point of interest\n", "suriname_lonlat = [-53.94, 5.61]\n", "suriname_point = ee.Geometry.Point(suriname_lonlat)" ] }, { "cell_type": "code", "execution_count": 6, "id": "02397cce", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
Make this Notebook Trusted to load map: File -> Trust Notebook
" ], "text/plain": [ "" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Instantiate a map\n", "suriname_map = geemap.Map() \n", "suriname_map.add_basemap('SATELLITE')\n", "\n", "# Add mangrove dataset as a leyer to the interactive map using the visualization parameters\n", "suriname_map.addLayer(mangrove_images_landsat, mangrovesVis, 'Mangroves')\n", "\n", "# Center the map using longitude, latitude, zoom level\n", "suriname_map.centerObject(suriname_point, 13)\n", "\n", "# Display the map\n", "suriname_map" ] }, { "cell_type": "markdown", "id": "a39c7bf4", "metadata": {}, "source": [ "#### Suriname area Landsat 8 satellite data" ] }, { "cell_type": "code", "execution_count": 7, "id": "ba32b8be", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
Make this Notebook Trusted to load map: File -> Trust Notebook
" ], "text/plain": [ "" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# select year\n", "year = 2016\n", "\n", "# Retrieve Landsat-8 SR dataset\n", "suriname_image = ee.ImageCollection('LANDSAT/LC08/C01/T1_SR') \\\n", " .filterBounds(suriname_point) \\\n", " .filterDate(f'{year}-01-01', f'{year}-12-31') \\\n", " .select('B[1-7]') \\\n", " .sort('CLOUD_COVER') \\\n", " .first()\n", " \n", "\n", "vis_params = {\n", " 'min': 0,\n", " 'max': 3000,\n", " 'bands': ['B5', 'B4', 'B3']\n", "}\n", "\n", "landsat = geemap.Map()\n", "landsat.centerObject(suriname_point, 8)\n", "landsat.addLayer(suriname_image, vis_params, \"Landsat-8\")\n", "landsat" ] }, { "cell_type": "markdown", "id": "879fb1c9", "metadata": {}, "source": [ "#### Image masking" ] }, { "cell_type": "code", "execution_count": 8, "id": "15c59b5b", "metadata": {}, "outputs": [], "source": [ "mangrove_mask = suriname_image.updateMask(mangrove_images_landsat.eq(1))\n", "non_mangrove_mask = suriname_image.updateMask(mangrove_mask.unmask().Not())" ] }, { "cell_type": "code", "execution_count": 9, "id": "98418a45", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['B1', 'B2', 'B3', 'B4', 'B5', 'B6', 'B7']" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "geemap.image_band_names(mangrove_mask).getInfo()" ] }, { "cell_type": "markdown", "id": "38c9052a", "metadata": {}, "source": [ "#### Random sampling of points" ] }, { "cell_type": "code", "execution_count": 10, "id": "3ab0d564", "metadata": {}, "outputs": [], "source": [ "mangrove_training_pts = mangrove_mask.sample(**{\n", " 'region': mangrove_mask.geometry(),\n", " 'scale': 30,\n", " 'numPixels': 10000,\n", " 'seed': 5343,\n", " 'geometries': True\n", "})" ] }, { "cell_type": "code", "execution_count": 11, "id": "c8ff5bd8", "metadata": {}, "outputs": [], "source": [ "non_mangrove_training_pts = non_mangrove_mask.sample(**{\n", " 'region': non_mangrove_mask.geometry(),\n", " 'scale': 30,\n", " 'numPixels': 5000,\n", " 'seed': 0,\n", " 'geometries': True\n", "})" ] }, { "cell_type": "code", "execution_count": 12, "id": "56e64dea", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(93, 4960)" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "mangrove_training_pts.size().getInfo(), non_mangrove_training_pts.size().getInfo()" ] }, { "cell_type": "code", "execution_count": 13, "id": "713b0a0c", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
Make this Notebook Trusted to load map: File -> Trust Notebook
" ], "text/plain": [ "" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "training_map = geemap.Map()\n", "training_map.setCenter(*suriname_lonlat, 13)\n", "\n", "vis_params = {\n", " 'min': 0,\n", " 'max': 100,\n", " 'bands': ['B4']\n", "}\n", "\n", "mangrove_color = 'eb0000'\n", "non_mangrove_color = '1c5f2c'\n", "\n", "legend_dict = {\n", " 'Mangrove Point': mangrove_color,\n", " 'Non-mangrove Point': non_mangrove_color,\n", " 'Mangrove Area': 'd1def8',\n", " 'mangrove mask': (0, 0, 0),\n", " 'non mangrove mask': (128, 128, 128)\n", "}\n", "\n", "training_map.addLayer(mangrove_mask, {'min': 0, 'max': 10000, 'bands': ['B1']}, 'mangrove mask', True)\n", "training_map.addLayer(mangrove_training_pts, {'color': mangrove_color}, 'Mangrove Sample')\n", "training_map.addLayer(non_mangrove_mask, {}, 'non mangrove mask', True)\n", "training_map.addLayer(non_mangrove_training_pts, {'color': non_mangrove_color}, 'non mangrove training', True)\n", "training_map.add_legend(legend_dict=legend_dict)\n", "training_map.centerObject(ee.Geometry.Point(-54.8451, 5.9453), 11)\n", "\n", "training_map" ] }, { "cell_type": "markdown", "id": "674c57eb", "metadata": {}, "source": [ "#### dataframe from sampled points" ] }, { "cell_type": "code", "execution_count": 14, "id": "4af2a907", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " B1 B2 B3 B4 B5 B6 B7 lon lat label\n", "0 251 326 623 535 1919 970 478 -53.952683 5.733402 1\n", "1 4354 4483 4714 4779 5898 4587 3714 -53.383394 5.559823 1\n", "2 1229 1249 1519 1455 3279 1961 1454 -53.754688 5.683997 1\n", "3 259 312 596 411 3049 1644 740 -54.781271 5.954568 1\n", "4 210 279 540 395 2689 1241 510 -54.722145 5.978066 1\n" ] } ], "source": [ "from geemap import ee_to_geopandas\n", "\n", "mangrove_gdf = ee_to_geopandas(mangrove_training_pts)\n", "mangrove_gdf[\"lon\"] = mangrove_gdf[\"geometry\"].apply(lambda p: p.x)\n", "mangrove_gdf[\"lat\"] = mangrove_gdf[\"geometry\"].apply(lambda p: p.y)\n", "mangrove_gdf[\"label\"] = 1 \n", "mangrove_gdf = mangrove_gdf.drop(\"geometry\", axis=1)\n", "print(mangrove_gdf.head())" ] }, { "cell_type": "code", "execution_count": 15, "id": "3916b514", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(93, 10)" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "mangrove_gdf.shape" ] }, { "cell_type": "code", "execution_count": 16, "id": "8994c2ce", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " B1 B2 B3 B4 B5 B6 B7 lon lat label\n", "0 101 169 386 248 3101 1277 508 -54.234656 5.501284 0\n", "1 356 398 337 285 296 292 268 -53.242286 5.944391 0\n", "2 378 409 232 88 71 87 78 -54.694873 6.638615 0\n", "3 251 273 241 203 219 229 204 -53.336881 6.381612 0\n", "4 114 166 393 229 3106 1256 474 -54.762566 5.522397 0\n" ] } ], "source": [ "non_mangrove_gdf = ee_to_geopandas(non_mangrove_training_pts)\n", "non_mangrove_gdf[\"lon\"] = non_mangrove_gdf[\"geometry\"].apply(lambda p: p.x)\n", "non_mangrove_gdf[\"lat\"] = non_mangrove_gdf[\"geometry\"].apply(lambda p: p.y)\n", "non_mangrove_gdf[\"label\"] = 0\n", "non_mangrove_gdf = non_mangrove_gdf.drop(\"geometry\", axis=1)\n", "print(non_mangrove_gdf.head())" ] }, { "cell_type": "code", "execution_count": null, "id": "591d2bbb", "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "gis", "language": "python", "name": "gis" }, "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.12" } }, "nbformat": 4, "nbformat_minor": 5 }