diff --git a/docs/workshops/Alaska_2024_Part2.ipynb b/docs/workshops/Alaska_2024_Part2.ipynb index 7bc5951ce9..862ee2a647 100644 --- a/docs/workshops/Alaska_2024_Part2.ipynb +++ b/docs/workshops/Alaska_2024_Part2.ipynb @@ -744,13 +744,1588 @@ "source": [ "## Working with local geospatial data\n", "\n", + "### Raster data\n", + "\n", + "#### Single-band raster" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "57", + "metadata": {}, + "outputs": [], + "source": [ + "url = \"https://github.com/giswqs/data/raw/main/raster/srtm90.tif\"\n", + "filename = \"dem.tif\"\n", + "geemap.download_file(url, filename)" + ] + }, + { + "cell_type": "markdown", + "id": "58", + "metadata": {}, + "source": [ + "#### Multi-band raster" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "59", + "metadata": {}, + "outputs": [], + "source": [ + "m = geemap.Map()\n", + "m.add_raster(filename, cmap=\"terrain\", layer_name=\"DEM\")\n", + "vis_params = {\"min\": 0, \"max\": 4000, \"palette\": \"terrain\"}\n", + "m.add_colorbar(vis_params, label=\"Elevation (m)\")\n", + "m" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "60", + "metadata": {}, + "outputs": [], + "source": [ + "url = \"https://github.com/giswqs/data/raw/main/raster/cog.tif\"\n", + "filename = \"cog.tif\"\n", + "geemap.download_file(url, filename)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "61", + "metadata": {}, + "outputs": [], + "source": [ + "m = geemap.Map()\n", + "m.add_raster(filename, indexes=[4, 1, 2], layer_name=\"False color\")\n", + "m" + ] + }, + { + "cell_type": "markdown", + "id": "62", + "metadata": {}, + "source": [ + "### Vector data\n", + "\n", + "#### GeoJSON" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "63", + "metadata": {}, + "outputs": [], + "source": [ + "in_geojson = (\n", + " \"https://github.com/opengeos/datasets/releases/download/vector/cables.geojson\"\n", + ")\n", + "m = geemap.Map()\n", + "m.add_geojson(in_geojson, layer_name=\"Cable lines\", info_mode=\"on_hover\")\n", + "m" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "64", + "metadata": {}, + "outputs": [], + "source": [ + "m = geemap.Map()\n", + "m.add_basemap(\"CartoDB.DarkMatter\")\n", + "callback = lambda feat: {\"color\": feat[\"properties\"][\"color\"], \"weight\": 2}\n", + "m.add_geojson(in_geojson, layer_name=\"Cable lines\", style_callback=callback)\n", + "m" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "65", + "metadata": {}, + "outputs": [], + "source": [ + "url = \"https://github.com/opengeos/datasets/releases/download/world/countries.geojson\"\n", + "m = geemap.Map()\n", + "m.add_geojson(\n", + " url, layer_name=\"Countries\", fill_colors=[\"red\", \"yellow\", \"green\", \"orange\"]\n", + ")\n", + "m" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "66", + "metadata": {}, + "outputs": [], + "source": [ + "import random\n", + "\n", + "m = geemap.Map()\n", + "\n", + "\n", + "def random_color(feature):\n", + " return {\n", + " \"color\": \"black\",\n", + " \"weight\": 3,\n", + " \"fillColor\": random.choice([\"red\", \"yellow\", \"green\", \"orange\"]),\n", + " }\n", + "\n", + "\n", + "m.add_geojson(url, layer_name=\"Countries\", style_callback=random_color)\n", + "m" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "67", + "metadata": {}, + "outputs": [], + "source": [ + "m = geemap.Map()\n", + "\n", + "style = {\n", + " \"stroke\": True,\n", + " \"color\": \"#0000ff\",\n", + " \"weight\": 2,\n", + " \"opacity\": 1,\n", + " \"fill\": True,\n", + " \"fillColor\": \"#0000ff\",\n", + " \"fillOpacity\": 0.1,\n", + "}\n", + "\n", + "hover_style = {\"fillOpacity\": 0.7}\n", + "\n", + "m.add_geojson(url, layer_name=\"Countries\", style=style, hover_style=hover_style)\n", + "m" + ] + }, + { + "cell_type": "markdown", + "id": "68", + "metadata": {}, + "source": [ + "#### Shapefile" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "69", + "metadata": {}, + "outputs": [], + "source": [ + "url = \"https://github.com/opengeos/datasets/releases/download/world/countries.zip\"\n", + "geemap.download_file(url, overwrite=True)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "70", + "metadata": {}, + "outputs": [], + "source": [ + "m = geemap.Map()\n", + "in_shp = \"countries.shp\"\n", + "m.add_shp(in_shp, layer_name=\"Countries\")\n", + "m" + ] + }, + { + "cell_type": "markdown", + "id": "71", + "metadata": {}, + "source": [ + "#### GeoDataFrame" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "72", + "metadata": {}, + "outputs": [], + "source": [ + "import geopandas as gpd\n", + "\n", + "m = geemap.Map(center=[40, -100], zoom=4)\n", + "gdf = gpd.read_file(\"countries.shp\")\n", + "m.add_gdf(gdf, layer_name=\"Countries\")\n", + "m" + ] + }, + { + "cell_type": "markdown", + "id": "73", + "metadata": {}, + "source": [ + "#### GeoPackage" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "74", + "metadata": {}, + "outputs": [], + "source": [ + "m = geemap.Map()\n", + "data = \"https://github.com/opengeos/datasets/releases/download/world/countries.gpkg\"\n", + "m.add_vector(data, layer_name=\"Countries\")\n", + "m" + ] + }, + { + "cell_type": "markdown", + "id": "75", + "metadata": {}, + "source": [ + "#### CSV to vector" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "76", + "metadata": {}, + "outputs": [], + "source": [ + "data = \"https://github.com/gee-community/geemap/blob/master/examples/data/us_cities.csv\"\n", + "geemap.csv_to_df(data)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "77", + "metadata": {}, + "outputs": [], + "source": [ + "geemap.csv_to_geojson(\n", + " data, \"cities.geojson\", latitude=\"latitude\", longitude=\"longitude\"\n", + ")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "78", + "metadata": {}, + "outputs": [], + "source": [ + "geemap.csv_to_shp(data, \"cities.shp\", latitude=\"latitude\", longitude=\"longitude\")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "79", + "metadata": {}, + "outputs": [], + "source": [ + "geemap.csv_to_vector(data, \"cities.gpkg\", latitude=\"latitude\", longitude=\"longitude\")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "80", + "metadata": {}, + "outputs": [], + "source": [ + "gdf = geemap.csv_to_gdf(data, latitude=\"latitude\", longitude=\"longitude\")\n", + "gdf" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "81", + "metadata": {}, + "outputs": [], + "source": [ + "cities = (\n", + " \"https://github.com/gee-community/geemap/blob/master/examples/data/us_cities.csv\"\n", + ")\n", + "m = geemap.Map(center=[40, -100], zoom=4)\n", + "m.add_points_from_xy(cities, x=\"longitude\", y=\"latitude\")\n", + "m" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "82", + "metadata": {}, + "outputs": [], + "source": [ + "regions = \"https://github.com/gee-community/geemap/blob/master/examples/data/us_regions.geojson\"" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "83", + "metadata": {}, + "outputs": [], + "source": [ + "m = geemap.Map(center=[40, -100], zoom=4)\n", + "m.add_geojson(regions, layer_name=\"US Regions\")\n", + "m.add_points_from_xy(\n", + " cities,\n", + " x=\"longitude\",\n", + " y=\"latitude\",\n", + " layer_name=\"US Cities\",\n", + " color_column=\"region\",\n", + " icon_names=[\"gear\", \"map\", \"leaf\", \"globe\"],\n", + " spin=True,\n", + " add_legend=True,\n", + ")\n", + "m" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "84", + "metadata": {}, + "outputs": [], + "source": [ + "m = geemap.Map(center=[40, -100], zoom=4)\n", + "m.add_circle_markers_from_xy(\n", + " data,\n", + " x=\"longitude\",\n", + " y=\"latitude\",\n", + " radius=8,\n", + " color=\"blue\",\n", + " fill_color=\"black\",\n", + " fill_opacity=0.5,\n", + ")\n", + "m" + ] + }, + { + "cell_type": "markdown", + "id": "85", + "metadata": {}, + "source": [ "## Accessing Cloud Optimized GeoTIFFs\n", "\n", - "## Exporting Earth Engine data\n", + "### COG" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "86", + "metadata": {}, + "outputs": [], + "source": [ + "m = geemap.Map()\n", + "url = \"https://opendata.digitalglobe.com/events/california-fire-2020/pre-event/2018-02-16/pine-gulch-fire20/1030010076004E00.tif\"\n", + "m.add_cog_layer(url, name=\"Fire (pre-event)\")\n", + "m" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "87", + "metadata": {}, + "outputs": [], + "source": [ + "geemap.cog_center(url)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "88", + "metadata": {}, + "outputs": [], + "source": [ + "geemap.cog_bands(url)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "89", + "metadata": {}, + "outputs": [], + "source": [ + "url2 = \"https://opendata.digitalglobe.com/events/california-fire-2020/post-event/2020-08-14/pine-gulch-fire20/10300100AAC8DD00.tif\"\n", + "m.add_cog_layer(url2, name=\"Fire (post-event)\")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "90", + "metadata": {}, + "outputs": [], + "source": [ + "m = geemap.Map(center=[39.4568, -108.5107], zoom=12)\n", + "m.split_map(left_layer=url2, right_layer=url)\n", + "m" + ] + }, + { + "cell_type": "markdown", + "id": "91", + "metadata": {}, + "source": [ + "### SpatioTemporal Asset Catalog (STAC)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "92", + "metadata": {}, + "outputs": [], + "source": [ + "url = \"https://tinyurl.com/22vptbws\"" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "93", + "metadata": {}, + "outputs": [], + "source": [ + "geemap.stac_bounds(url)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "94", + "metadata": {}, + "outputs": [], + "source": [ + "geemap.stac_center(url)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "95", + "metadata": {}, + "outputs": [], + "source": [ + "geemap.stac_bands(url)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "96", + "metadata": {}, + "outputs": [], + "source": [ + "m = geemap.Map()\n", + "m.add_stac_layer(url, bands=[\"pan\"], name=\"Panchromatic\")\n", + "m.add_stac_layer(url, bands=[\"B3\", \"B2\", \"B1\"], name=\"False color\")\n", + "m" + ] + }, + { + "cell_type": "markdown", + "id": "97", + "metadata": {}, + "source": [ + "## Exporting Earth Engine data\n", + "\n", + "### Exporting images\n", + "\n", + "Add a Landsat image to the map." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "98", + "metadata": {}, + "outputs": [], + "source": [ + "m = geemap.Map()\n", + "\n", + "image = ee.Image(\"LANDSAT/LC08/C02/T1_TOA/LC08_044034_20140318\").select(\n", + " [\"B5\", \"B4\", \"B3\"]\n", + ")\n", + "\n", + "vis_params = {\"min\": 0, \"max\": 0.5, \"gamma\": [0.95, 1.1, 1]}\n", + "\n", + "m.center_object(image)\n", + "m.add_layer(image, vis_params, \"Landsat\")\n", + "m" + ] + }, + { + "cell_type": "markdown", + "id": "99", + "metadata": {}, + "source": [ + "Add a rectangle to the map." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "100", + "metadata": {}, + "outputs": [], + "source": [ + "region = ee.Geometry.BBox(-122.5955, 37.5339, -122.0982, 37.8252)\n", + "fc = ee.FeatureCollection(region)\n", + "style = {\"color\": \"ffff00ff\", \"fillColor\": \"00000000\"}\n", + "m.add_layer(fc.style(**style), {}, \"ROI\")" + ] + }, + { + "cell_type": "markdown", + "id": "101", + "metadata": {}, + "source": [ + "To local drive" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "102", + "metadata": {}, + "outputs": [], + "source": [ + "geemap.ee_export_image(image, filename=\"landsat.tif\", scale=30, region=region)" + ] + }, + { + "cell_type": "markdown", + "id": "103", + "metadata": {}, + "source": [ + "Check image projection." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "104", + "metadata": {}, + "outputs": [], + "source": [ + "projection = image.select(0).projection().getInfo()\n", + "projection" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "105", + "metadata": {}, + "outputs": [], + "source": [ + "crs = projection[\"crs\"]\n", + "crs_transform = projection[\"transform\"]" + ] + }, + { + "cell_type": "markdown", + "id": "106", + "metadata": {}, + "source": [ + "Specify region, crs, and crs_transform." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "107", + "metadata": {}, + "outputs": [], + "source": [ + "geemap.ee_export_image(\n", + " image,\n", + " filename=\"landsat_crs.tif\",\n", + " crs=crs,\n", + " crs_transform=crs_transform,\n", + " region=region,\n", + ")" + ] + }, + { + "cell_type": "markdown", + "id": "108", + "metadata": {}, + "source": [ + "To Google Drive" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "109", + "metadata": {}, + "outputs": [], + "source": [ + "geemap.ee_export_image_to_drive(\n", + " image, description=\"landsat\", folder=\"export\", region=region, scale=30\n", + ")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "110", + "metadata": {}, + "outputs": [], + "source": [ + "geemap.download_ee_image(image, \"landsat.tif\", scale=90)" + ] + }, + { + "cell_type": "markdown", + "id": "111", + "metadata": {}, + "source": [ + "### Exporting image collections" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "112", + "metadata": {}, + "outputs": [], + "source": [ + "point = ee.Geometry.Point(-99.2222, 46.7816)\n", + "collection = (\n", + " ee.ImageCollection(\"USDA/NAIP/DOQQ\")\n", + " .filterBounds(point)\n", + " .filterDate(\"2008-01-01\", \"2018-01-01\")\n", + " .filter(ee.Filter.listContains(\"system:band_names\", \"N\"))\n", + ")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "113", + "metadata": {}, + "outputs": [], + "source": [ + "collection.aggregate_array(\"system:index\")" + ] + }, + { + "cell_type": "markdown", + "id": "114", + "metadata": {}, + "source": [ + "To local drive" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "115", + "metadata": {}, + "outputs": [], + "source": [ + "geemap.ee_export_image_collection(collection, out_dir=\".\", scale=10)" + ] + }, + { + "cell_type": "markdown", + "id": "116", + "metadata": {}, + "source": [ + "To Google Drive" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "117", + "metadata": {}, + "outputs": [], + "source": [ + "geemap.ee_export_image_collection_to_drive(collection, folder=\"export\", scale=10)" + ] + }, + { + "cell_type": "markdown", + "id": "118", + "metadata": {}, + "source": [ + "### Exporting feature collections" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "119", + "metadata": {}, + "outputs": [], + "source": [ + "m = geemap.Map()\n", + "states = ee.FeatureCollection(\"TIGER/2018/States\")\n", + "fc = states.filter(ee.Filter.eq(\"NAME\", \"Alaska\"))\n", + "m.add_layer(fc, {}, \"Alaska\")\n", + "m.center_object(fc, 4)\n", + "m" + ] + }, + { + "cell_type": "markdown", + "id": "120", + "metadata": {}, + "source": [ + "To local drive" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "121", + "metadata": {}, + "outputs": [], + "source": [ + "geemap.ee_to_shp(fc, filename=\"Alaska.shp\")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "122", + "metadata": {}, + "outputs": [], + "source": [ + "geemap.ee_export_vector(fc, filename=\"Alaska.shp\")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "123", + "metadata": {}, + "outputs": [], + "source": [ + "geemap.ee_to_geojson(fc, filename=\"Alaska.geojson\")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "124", + "metadata": {}, + "outputs": [], + "source": [ + "geemap.ee_to_csv(fc, filename=\"Alaska.csv\")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "125", + "metadata": {}, + "outputs": [], + "source": [ + "gdf = geemap.ee_to_gdf(fc)\n", + "gdf" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "126", + "metadata": {}, + "outputs": [], + "source": [ + "df = geemap.ee_to_df(fc)\n", + "df" + ] + }, + { + "cell_type": "markdown", + "id": "127", + "metadata": {}, + "source": [ + "To Google Drive" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "128", + "metadata": {}, + "outputs": [], + "source": [ + "geemap.ee_export_vector_to_drive(\n", + " fc, description=\"Alaska\", fileFormat=\"SHP\", folder=\"export\"\n", + ")" + ] + }, + { + "cell_type": "markdown", + "id": "129", + "metadata": {}, + "source": [ + "## Creating timelapse animations\n", + "\n", + "### Landsat timelapse" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "130", + "metadata": {}, + "outputs": [], + "source": [ + "m = geemap.Map(center=[64.838721, -147.763366], zoom=11)\n", + "m" + ] + }, + { + "cell_type": "markdown", + "id": "131", + "metadata": {}, + "source": [ + "Pan and zoom the map to an area of interest. Use the drawing tools to draw a rectangle on the map. If no rectangle is drawn, the default rectangle shown below will be used." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "132", + "metadata": {}, + "outputs": [], + "source": [ + "roi = m.user_roi\n", + "if roi is None:\n", + " roi = ee.Geometry.BBox(-147.9701, 64.7733, -147.5849, 64.8717)\n", + " m.add_layer(roi)\n", + " m.center_object(roi)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "133", + "metadata": {}, + "outputs": [], + "source": [ + "timelapse = geemap.landsat_timelapse(\n", + " roi,\n", + " out_gif=\"Fairbanks.gif\",\n", + " start_year=2000,\n", + " end_year=2023,\n", + " start_date=\"06-01\",\n", + " end_date=\"09-01\",\n", + " bands=[\"SWIR1\", \"NIR\", \"Red\"],\n", + " frames_per_second=5,\n", + " title=\"Landsat Timelapse\",\n", + " progress_bar_color=\"blue\",\n", + " mp4=True,\n", + ")\n", + "geemap.show_image(timelapse)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "134", + "metadata": {}, + "outputs": [], + "source": [ + "m = geemap.Map(center=[64.838721, -147.763366], zoom=11)\n", + "m.add_gui(\"timelapse\")\n", + "m" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "135", + "metadata": {}, + "outputs": [], + "source": [ + "m = geemap.Map()\n", + "roi = ee.Geometry.BBox(-115.5541, 35.8044, -113.9035, 36.5581)\n", + "m.add_layer(roi)\n", + "m.center_object(roi)\n", + "m" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "136", + "metadata": {}, + "outputs": [], + "source": [ + "timelapse = geemap.landsat_timelapse(\n", + " roi,\n", + " out_gif=\"las_vegas.gif\",\n", + " start_year=1984,\n", + " end_year=2023,\n", + " bands=[\"NIR\", \"Red\", \"Green\"],\n", + " frames_per_second=5,\n", + " title=\"Las Vegas, NV\",\n", + " font_color=\"blue\",\n", + ")\n", + "geemap.show_image(timelapse)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "137", + "metadata": {}, + "outputs": [], + "source": [ + "m = geemap.Map()\n", + "roi = ee.Geometry.BBox(113.8252, 22.1988, 114.0851, 22.3497)\n", + "m.add_layer(roi)\n", + "m.center_object(roi)\n", + "m" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "138", + "metadata": {}, + "outputs": [], + "source": [ + "timelapse = geemap.landsat_timelapse(\n", + " roi,\n", + " out_gif=\"hong_kong.gif\",\n", + " start_year=1990,\n", + " end_year=2022,\n", + " start_date=\"01-01\",\n", + " end_date=\"12-31\",\n", + " bands=[\"SWIR1\", \"NIR\", \"Red\"],\n", + " frames_per_second=3,\n", + " title=\"Hong Kong\",\n", + ")\n", + "geemap.show_image(timelapse)" + ] + }, + { + "cell_type": "markdown", + "id": "139", + "metadata": {}, + "source": [ + "### Sentinel-2" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "140", + "metadata": {}, + "outputs": [], + "source": [ + "m = geemap.Map(center=[64.838721, -147.763366], zoom=11)\n", + "m" + ] + }, + { + "cell_type": "markdown", + "id": "141", + "metadata": {}, + "source": [ + "Pan and zoom the map to an area of interest. Use the drawing tools to draw a rectangle on the map. If no rectangle is drawn, the default rectangle shown below will be used." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "142", + "metadata": {}, + "outputs": [], + "source": [ + "roi = m.user_roi\n", + "if roi is None:\n", + " roi = ee.Geometry.BBox(-147.9701, 64.7733, -147.5849, 64.8717)\n", + " m.add_layer(roi)\n", + " m.center_object(roi)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "143", + "metadata": {}, + "outputs": [], + "source": [ + "timelapse = geemap.sentinel2_timelapse(\n", + " roi,\n", + " out_gif=\"sentinel2.gif\",\n", + " start_year=2017,\n", + " end_year=2023,\n", + " start_date=\"06-01\",\n", + " end_date=\"09-01\",\n", + " frequency=\"year\",\n", + " bands=[\"SWIR1\", \"NIR\", \"Red\"],\n", + " frames_per_second=3,\n", + " title=\"Sentinel-2 Timelapse\",\n", + ")\n", + "geemap.show_image(timelapse)" + ] + }, + { + "cell_type": "markdown", + "id": "144", + "metadata": {}, + "source": [ + "### MODIS\n", + "\n", + "MODIS vegetation indices" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "145", + "metadata": {}, + "outputs": [], + "source": [ + "m = geemap.Map()\n", + "m" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "146", + "metadata": {}, + "outputs": [], + "source": [ + "roi = m.user_roi\n", + "if roi is None:\n", + " roi = ee.Geometry.BBox(-18.6983, -36.1630, 52.2293, 38.1446)\n", + " m.add_layer(roi)\n", + " m.center_object(roi)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "147", + "metadata": {}, + "outputs": [], + "source": [ + "timelapse = geemap.modis_ndvi_timelapse(\n", + " roi,\n", + " out_gif=\"ndvi.gif\",\n", + " data=\"Terra\",\n", + " band=\"NDVI\",\n", + " start_date=\"2000-01-01\",\n", + " end_date=\"2022-12-31\",\n", + " frames_per_second=3,\n", + " title=\"MODIS NDVI Timelapse\",\n", + " overlay_data=\"countries\",\n", + ")\n", + "geemap.show_image(timelapse)" + ] + }, + { + "cell_type": "markdown", + "id": "148", + "metadata": {}, + "source": [ + "MODIS temperature" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "149", + "metadata": {}, + "outputs": [], + "source": [ + "m = geemap.Map()\n", + "m" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "150", + "metadata": {}, + "outputs": [], + "source": [ + "roi = m.user_roi\n", + "if roi is None:\n", + " roi = ee.Geometry.BBox(-171.21, -57.13, 177.53, 79.99)\n", + " m.add_layer(roi)\n", + " m.center_object(roi)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "151", + "metadata": {}, + "outputs": [], + "source": [ + "timelapse = geemap.modis_ocean_color_timelapse(\n", + " satellite=\"Aqua\",\n", + " start_date=\"2018-01-01\",\n", + " end_date=\"2020-12-31\",\n", + " roi=roi,\n", + " frequency=\"month\",\n", + " out_gif=\"temperature.gif\",\n", + " overlay_data=\"continents\",\n", + " overlay_color=\"yellow\",\n", + " overlay_opacity=0.5,\n", + ")\n", + "geemap.show_image(timelapse)" + ] + }, + { + "cell_type": "markdown", + "id": "152", + "metadata": {}, + "source": [ + "### GOES" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "153", + "metadata": {}, + "outputs": [], + "source": [ + "roi = ee.Geometry.BBox(167.1898, -28.5757, 202.6258, -12.4411)\n", + "start_date = \"2022-01-15T03:00:00\"\n", + "end_date = \"2022-01-15T07:00:00\"\n", + "data = \"GOES-17\"\n", + "scan = \"full_disk\"" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "154", + "metadata": {}, + "outputs": [], + "source": [ + "timelapse = geemap.goes_timelapse(\n", + " roi, \"goes.gif\", start_date, end_date, data, scan, framesPerSecond=5\n", + ")\n", + "geemap.show_image(timelapse)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "155", + "metadata": {}, + "outputs": [], + "source": [ + "roi = ee.Geometry.BBox(-159.5954, 24.5178, -114.2438, 60.4088)\n", + "start_date = \"2021-10-24T14:00:00\"\n", + "end_date = \"2021-10-25T01:00:00\"\n", + "data = \"GOES-17\"\n", + "scan = \"full_disk\"" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "156", + "metadata": {}, + "outputs": [], + "source": [ + "timelapse = geemap.goes_timelapse(\n", + " roi, \"hurricane.gif\", start_date, end_date, data, scan, framesPerSecond=5\n", + ")\n", + "geemap.show_image(timelapse)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "157", + "metadata": {}, + "outputs": [], + "source": [ + "roi = ee.Geometry.BBox(-121.0034, 36.8488, -117.9052, 39.0490)\n", + "start_date = \"2020-09-05T15:00:00\"\n", + "end_date = \"2020-09-06T02:00:00\"\n", + "data = \"GOES-17\"\n", + "scan = \"full_disk\"" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "158", + "metadata": {}, + "outputs": [], + "source": [ + "timelapse = geemap.goes_fire_timelapse(\n", + " roi, \"fire.gif\", start_date, end_date, data, scan, framesPerSecond=5\n", + ")\n", + "geemap.show_image(timelapse)" + ] + }, + { + "cell_type": "markdown", + "id": "159", + "metadata": {}, + "source": [ + "## Time series analysis\n", + "\n", + "### Visualizing forest cover\n", "\n", - "## Creating timelapse animations\n", + "We will use the [Hansen Global Forest Change v1.10 (2000-2022) dataset](https://developers.google.com/earth-engine/datasets/catalog/UMD_hansen_global_forest_change_2022_v1_10)." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "160", + "metadata": {}, + "outputs": [], + "source": [ + "dataset = ee.Image(\"UMD/hansen/global_forest_change_2022_v1_10\")\n", + "dataset.bandNames()" + ] + }, + { + "cell_type": "markdown", + "id": "161", + "metadata": {}, + "source": [ + "Select the imagery for 2000." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "162", + "metadata": {}, + "outputs": [], + "source": [ + "m = geemap.Map()\n", + "first_bands = [\"first_b50\", \"first_b40\", \"first_b30\"]\n", + "first_image = dataset.select(first_bands)\n", + "m.add_layer(first_image, {\"bands\": first_bands, \"gamma\": 1.5}, \"Landsat 2000\")" + ] + }, + { + "cell_type": "markdown", + "id": "163", + "metadata": {}, + "source": [ + "Select the imagery for 2022." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "164", + "metadata": {}, + "outputs": [], + "source": [ + "last_bands = [\"last_b50\", \"last_b40\", \"last_b30\"]\n", + "last_image = dataset.select(last_bands)\n", + "m.add_layer(last_image, {\"bands\": last_bands, \"gamma\": 1.5}, \"Landsat 2022\")" + ] + }, + { + "cell_type": "markdown", + "id": "165", + "metadata": {}, + "source": [ + "Select the tree cover imagery for 2000." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "166", + "metadata": {}, + "outputs": [], + "source": [ + "treecover = dataset.select([\"treecover2000\"])\n", + "treeCoverVisParam = {\"min\": 0, \"max\": 100, \"palette\": [\"black\", \"green\"]}\n", + "name = \"Tree cover (%)\"\n", + "m.add_layer(treecover, treeCoverVisParam, name)\n", + "m.add_colorbar(treeCoverVisParam, label=name, layer_name=name)\n", + "m.add(\"layer_manager\")\n", + "m" + ] + }, + { + "cell_type": "markdown", + "id": "167", + "metadata": {}, + "source": [ + "Extract tree cover 2000 by using the threshold of 10%." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "168", + "metadata": {}, + "outputs": [], + "source": [ + "threshold = 10\n", + "treecover_bin = treecover.gte(threshold).selfMask()\n", + "treeVisParam = {\"palette\": [\"green\"]}\n", + "m.add_layer(treecover_bin, treeVisParam, \"Tree cover bin\")" + ] + }, + { + "cell_type": "markdown", + "id": "169", + "metadata": {}, + "source": [ + "### Visualizing forest gain and loss\n", + "\n", + "Visualize forest loss." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "170", + "metadata": {}, + "outputs": [], + "source": [ + "m = geemap.Map(center=[64.864983, -147.840441], zoom=4)\n", + "m.add_basemap(\"Esri.WorldImagery\")\n", + "treeloss_year = dataset.select([\"lossyear\"])\n", + "treeLossVisParam = {\"min\": 0, \"max\": 22, \"palette\": [\"yellow\", \"red\"]}\n", + "layer_name = \"Tree loss year\"\n", + "m.add_layer(treeloss_year, treeLossVisParam, layer_name)\n", + "m.add_colorbar(treeLossVisParam, label=layer_name, layer_name=layer_name)\n", + "m.add(\"layer_manager\")\n", + "m" + ] + }, + { + "cell_type": "markdown", + "id": "171", + "metadata": {}, + "source": [ + "Compare forest loss and gain." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "172", + "metadata": {}, + "outputs": [], + "source": [ + "m = geemap.Map(center=[64.864983, -147.840441], zoom=4)\n", + "m.add_basemap(\"Esri.WorldImagery\")\n", + "treeloss = dataset.select([\"loss\"]).selfMask()\n", + "treegain = dataset.select([\"gain\"]).selfMask()\n", + "m.add_layer(treeloss, {\"palette\": \"red\"}, \"Tree loss\")\n", + "m.add_layer(treegain, {\"palette\": \"yellow\"}, \"Tree gain\")\n", + "m.add(\"layer_manager\")\n", + "m" + ] + }, + { + "cell_type": "markdown", + "id": "173", + "metadata": {}, + "source": [ + "### Calculating forest cover change\n", "\n", - "## Time series analysis" + "Compute zonal statistics to find out which county in Alaska has the largest forest area in 2000.\n", + "\n", + "Add a county boundary layer to the map." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "174", + "metadata": {}, + "outputs": [], + "source": [ + "counties = ee.FeatureCollection(\"TIGER/2018/Counties\").filter(\n", + " ee.Filter.eq(\"STATEFP\", \"02\")\n", + ")\n", + "df = geemap.ee_to_df(counties)\n", + "df.head()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "175", + "metadata": {}, + "outputs": [], + "source": [ + "style = {\"color\": \"0000FFFF\", \"fillColor\": \"00000000\"}\n", + "m.add_layer(counties, {}, \"Counties Vector\", False)\n", + "m.add_layer(counties.style(**style), {}, \"Counties Raster\")\n", + "m" + ] + }, + { + "cell_type": "markdown", + "id": "176", + "metadata": {}, + "source": [ + "Compute zonal statistics by county." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "177", + "metadata": {}, + "outputs": [], + "source": [ + "geemap.zonal_stats(\n", + " treecover_bin,\n", + " counties,\n", + " \"forest_cover.csv\",\n", + " stat_type=\"SUM\",\n", + " denominator=1e6,\n", + " scale=300,\n", + ")" + ] + }, + { + "cell_type": "markdown", + "id": "178", + "metadata": {}, + "source": [ + "Create a pie chart to visualize the forest area by county." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "179", + "metadata": {}, + "outputs": [], + "source": [ + "geemap.pie_chart(\n", + " \"forest_cover.csv\", names=\"NAME\", values=\"sum\", max_rows=20, height=400\n", + ")" + ] + }, + { + "cell_type": "markdown", + "id": "180", + "metadata": {}, + "source": [ + "Create a bar chart to visualize the forest area by county." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "181", + "metadata": {}, + "outputs": [], + "source": [ + "geemap.bar_chart(\n", + " \"forest_cover.csv\",\n", + " x=\"NAME\",\n", + " y=\"sum\",\n", + " max_rows=20,\n", + " x_label=\"County\",\n", + " y_label=\"Forest area (km2)\",\n", + ")" + ] + }, + { + "cell_type": "markdown", + "id": "182", + "metadata": {}, + "source": [ + "Calculate the forest loss area by county." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "183", + "metadata": {}, + "outputs": [], + "source": [ + "geemap.zonal_stats(\n", + " treeloss.gt(0),\n", + " counties,\n", + " \"treeloss.csv\",\n", + " stat_type=\"SUM\",\n", + " denominator=1e6,\n", + " scale=300,\n", + ")" + ] + }, + { + "cell_type": "markdown", + "id": "184", + "metadata": {}, + "source": [ + "Create a bar chart to visualize the forest loss area by county." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "185", + "metadata": {}, + "outputs": [], + "source": [ + "geemap.pie_chart(\"treeloss.csv\", names=\"NAME\", values=\"sum\", max_rows=20, height=600)" + ] + }, + { + "cell_type": "markdown", + "id": "186", + "metadata": {}, + "source": [ + "Create a bar chart to visualize the forest loss area by county." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "187", + "metadata": {}, + "outputs": [], + "source": [ + "geemap.bar_chart(\n", + " \"treeloss.csv\",\n", + " x=\"NAME\",\n", + " y=\"sum\",\n", + " max_rows=20,\n", + " x_label=\"County\",\n", + " y_label=\"Forest loss area (km2)\",\n", + ")" ] } ], diff --git a/examples/workshops/Alaska_2024_Part2.ipynb b/examples/workshops/Alaska_2024_Part2.ipynb index 7bc5951ce9..862ee2a647 100644 --- a/examples/workshops/Alaska_2024_Part2.ipynb +++ b/examples/workshops/Alaska_2024_Part2.ipynb @@ -744,13 +744,1588 @@ "source": [ "## Working with local geospatial data\n", "\n", + "### Raster data\n", + "\n", + "#### Single-band raster" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "57", + "metadata": {}, + "outputs": [], + "source": [ + "url = \"https://github.com/giswqs/data/raw/main/raster/srtm90.tif\"\n", + "filename = \"dem.tif\"\n", + "geemap.download_file(url, filename)" + ] + }, + { + "cell_type": "markdown", + "id": "58", + "metadata": {}, + "source": [ + "#### Multi-band raster" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "59", + "metadata": {}, + "outputs": [], + "source": [ + "m = geemap.Map()\n", + "m.add_raster(filename, cmap=\"terrain\", layer_name=\"DEM\")\n", + "vis_params = {\"min\": 0, \"max\": 4000, \"palette\": \"terrain\"}\n", + "m.add_colorbar(vis_params, label=\"Elevation (m)\")\n", + "m" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "60", + "metadata": {}, + "outputs": [], + "source": [ + "url = \"https://github.com/giswqs/data/raw/main/raster/cog.tif\"\n", + "filename = \"cog.tif\"\n", + "geemap.download_file(url, filename)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "61", + "metadata": {}, + "outputs": [], + "source": [ + "m = geemap.Map()\n", + "m.add_raster(filename, indexes=[4, 1, 2], layer_name=\"False color\")\n", + "m" + ] + }, + { + "cell_type": "markdown", + "id": "62", + "metadata": {}, + "source": [ + "### Vector data\n", + "\n", + "#### GeoJSON" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "63", + "metadata": {}, + "outputs": [], + "source": [ + "in_geojson = (\n", + " \"https://github.com/opengeos/datasets/releases/download/vector/cables.geojson\"\n", + ")\n", + "m = geemap.Map()\n", + "m.add_geojson(in_geojson, layer_name=\"Cable lines\", info_mode=\"on_hover\")\n", + "m" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "64", + "metadata": {}, + "outputs": [], + "source": [ + "m = geemap.Map()\n", + "m.add_basemap(\"CartoDB.DarkMatter\")\n", + "callback = lambda feat: {\"color\": feat[\"properties\"][\"color\"], \"weight\": 2}\n", + "m.add_geojson(in_geojson, layer_name=\"Cable lines\", style_callback=callback)\n", + "m" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "65", + "metadata": {}, + "outputs": [], + "source": [ + "url = \"https://github.com/opengeos/datasets/releases/download/world/countries.geojson\"\n", + "m = geemap.Map()\n", + "m.add_geojson(\n", + " url, layer_name=\"Countries\", fill_colors=[\"red\", \"yellow\", \"green\", \"orange\"]\n", + ")\n", + "m" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "66", + "metadata": {}, + "outputs": [], + "source": [ + "import random\n", + "\n", + "m = geemap.Map()\n", + "\n", + "\n", + "def random_color(feature):\n", + " return {\n", + " \"color\": \"black\",\n", + " \"weight\": 3,\n", + " \"fillColor\": random.choice([\"red\", \"yellow\", \"green\", \"orange\"]),\n", + " }\n", + "\n", + "\n", + "m.add_geojson(url, layer_name=\"Countries\", style_callback=random_color)\n", + "m" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "67", + "metadata": {}, + "outputs": [], + "source": [ + "m = geemap.Map()\n", + "\n", + "style = {\n", + " \"stroke\": True,\n", + " \"color\": \"#0000ff\",\n", + " \"weight\": 2,\n", + " \"opacity\": 1,\n", + " \"fill\": True,\n", + " \"fillColor\": \"#0000ff\",\n", + " \"fillOpacity\": 0.1,\n", + "}\n", + "\n", + "hover_style = {\"fillOpacity\": 0.7}\n", + "\n", + "m.add_geojson(url, layer_name=\"Countries\", style=style, hover_style=hover_style)\n", + "m" + ] + }, + { + "cell_type": "markdown", + "id": "68", + "metadata": {}, + "source": [ + "#### Shapefile" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "69", + "metadata": {}, + "outputs": [], + "source": [ + "url = \"https://github.com/opengeos/datasets/releases/download/world/countries.zip\"\n", + "geemap.download_file(url, overwrite=True)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "70", + "metadata": {}, + "outputs": [], + "source": [ + "m = geemap.Map()\n", + "in_shp = \"countries.shp\"\n", + "m.add_shp(in_shp, layer_name=\"Countries\")\n", + "m" + ] + }, + { + "cell_type": "markdown", + "id": "71", + "metadata": {}, + "source": [ + "#### GeoDataFrame" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "72", + "metadata": {}, + "outputs": [], + "source": [ + "import geopandas as gpd\n", + "\n", + "m = geemap.Map(center=[40, -100], zoom=4)\n", + "gdf = gpd.read_file(\"countries.shp\")\n", + "m.add_gdf(gdf, layer_name=\"Countries\")\n", + "m" + ] + }, + { + "cell_type": "markdown", + "id": "73", + "metadata": {}, + "source": [ + "#### GeoPackage" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "74", + "metadata": {}, + "outputs": [], + "source": [ + "m = geemap.Map()\n", + "data = \"https://github.com/opengeos/datasets/releases/download/world/countries.gpkg\"\n", + "m.add_vector(data, layer_name=\"Countries\")\n", + "m" + ] + }, + { + "cell_type": "markdown", + "id": "75", + "metadata": {}, + "source": [ + "#### CSV to vector" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "76", + "metadata": {}, + "outputs": [], + "source": [ + "data = \"https://github.com/gee-community/geemap/blob/master/examples/data/us_cities.csv\"\n", + "geemap.csv_to_df(data)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "77", + "metadata": {}, + "outputs": [], + "source": [ + "geemap.csv_to_geojson(\n", + " data, \"cities.geojson\", latitude=\"latitude\", longitude=\"longitude\"\n", + ")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "78", + "metadata": {}, + "outputs": [], + "source": [ + "geemap.csv_to_shp(data, \"cities.shp\", latitude=\"latitude\", longitude=\"longitude\")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "79", + "metadata": {}, + "outputs": [], + "source": [ + "geemap.csv_to_vector(data, \"cities.gpkg\", latitude=\"latitude\", longitude=\"longitude\")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "80", + "metadata": {}, + "outputs": [], + "source": [ + "gdf = geemap.csv_to_gdf(data, latitude=\"latitude\", longitude=\"longitude\")\n", + "gdf" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "81", + "metadata": {}, + "outputs": [], + "source": [ + "cities = (\n", + " \"https://github.com/gee-community/geemap/blob/master/examples/data/us_cities.csv\"\n", + ")\n", + "m = geemap.Map(center=[40, -100], zoom=4)\n", + "m.add_points_from_xy(cities, x=\"longitude\", y=\"latitude\")\n", + "m" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "82", + "metadata": {}, + "outputs": [], + "source": [ + "regions = \"https://github.com/gee-community/geemap/blob/master/examples/data/us_regions.geojson\"" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "83", + "metadata": {}, + "outputs": [], + "source": [ + "m = geemap.Map(center=[40, -100], zoom=4)\n", + "m.add_geojson(regions, layer_name=\"US Regions\")\n", + "m.add_points_from_xy(\n", + " cities,\n", + " x=\"longitude\",\n", + " y=\"latitude\",\n", + " layer_name=\"US Cities\",\n", + " color_column=\"region\",\n", + " icon_names=[\"gear\", \"map\", \"leaf\", \"globe\"],\n", + " spin=True,\n", + " add_legend=True,\n", + ")\n", + "m" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "84", + "metadata": {}, + "outputs": [], + "source": [ + "m = geemap.Map(center=[40, -100], zoom=4)\n", + "m.add_circle_markers_from_xy(\n", + " data,\n", + " x=\"longitude\",\n", + " y=\"latitude\",\n", + " radius=8,\n", + " color=\"blue\",\n", + " fill_color=\"black\",\n", + " fill_opacity=0.5,\n", + ")\n", + "m" + ] + }, + { + "cell_type": "markdown", + "id": "85", + "metadata": {}, + "source": [ "## Accessing Cloud Optimized GeoTIFFs\n", "\n", - "## Exporting Earth Engine data\n", + "### COG" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "86", + "metadata": {}, + "outputs": [], + "source": [ + "m = geemap.Map()\n", + "url = \"https://opendata.digitalglobe.com/events/california-fire-2020/pre-event/2018-02-16/pine-gulch-fire20/1030010076004E00.tif\"\n", + "m.add_cog_layer(url, name=\"Fire (pre-event)\")\n", + "m" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "87", + "metadata": {}, + "outputs": [], + "source": [ + "geemap.cog_center(url)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "88", + "metadata": {}, + "outputs": [], + "source": [ + "geemap.cog_bands(url)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "89", + "metadata": {}, + "outputs": [], + "source": [ + "url2 = \"https://opendata.digitalglobe.com/events/california-fire-2020/post-event/2020-08-14/pine-gulch-fire20/10300100AAC8DD00.tif\"\n", + "m.add_cog_layer(url2, name=\"Fire (post-event)\")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "90", + "metadata": {}, + "outputs": [], + "source": [ + "m = geemap.Map(center=[39.4568, -108.5107], zoom=12)\n", + "m.split_map(left_layer=url2, right_layer=url)\n", + "m" + ] + }, + { + "cell_type": "markdown", + "id": "91", + "metadata": {}, + "source": [ + "### SpatioTemporal Asset Catalog (STAC)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "92", + "metadata": {}, + "outputs": [], + "source": [ + "url = \"https://tinyurl.com/22vptbws\"" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "93", + "metadata": {}, + "outputs": [], + "source": [ + "geemap.stac_bounds(url)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "94", + "metadata": {}, + "outputs": [], + "source": [ + "geemap.stac_center(url)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "95", + "metadata": {}, + "outputs": [], + "source": [ + "geemap.stac_bands(url)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "96", + "metadata": {}, + "outputs": [], + "source": [ + "m = geemap.Map()\n", + "m.add_stac_layer(url, bands=[\"pan\"], name=\"Panchromatic\")\n", + "m.add_stac_layer(url, bands=[\"B3\", \"B2\", \"B1\"], name=\"False color\")\n", + "m" + ] + }, + { + "cell_type": "markdown", + "id": "97", + "metadata": {}, + "source": [ + "## Exporting Earth Engine data\n", + "\n", + "### Exporting images\n", + "\n", + "Add a Landsat image to the map." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "98", + "metadata": {}, + "outputs": [], + "source": [ + "m = geemap.Map()\n", + "\n", + "image = ee.Image(\"LANDSAT/LC08/C02/T1_TOA/LC08_044034_20140318\").select(\n", + " [\"B5\", \"B4\", \"B3\"]\n", + ")\n", + "\n", + "vis_params = {\"min\": 0, \"max\": 0.5, \"gamma\": [0.95, 1.1, 1]}\n", + "\n", + "m.center_object(image)\n", + "m.add_layer(image, vis_params, \"Landsat\")\n", + "m" + ] + }, + { + "cell_type": "markdown", + "id": "99", + "metadata": {}, + "source": [ + "Add a rectangle to the map." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "100", + "metadata": {}, + "outputs": [], + "source": [ + "region = ee.Geometry.BBox(-122.5955, 37.5339, -122.0982, 37.8252)\n", + "fc = ee.FeatureCollection(region)\n", + "style = {\"color\": \"ffff00ff\", \"fillColor\": \"00000000\"}\n", + "m.add_layer(fc.style(**style), {}, \"ROI\")" + ] + }, + { + "cell_type": "markdown", + "id": "101", + "metadata": {}, + "source": [ + "To local drive" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "102", + "metadata": {}, + "outputs": [], + "source": [ + "geemap.ee_export_image(image, filename=\"landsat.tif\", scale=30, region=region)" + ] + }, + { + "cell_type": "markdown", + "id": "103", + "metadata": {}, + "source": [ + "Check image projection." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "104", + "metadata": {}, + "outputs": [], + "source": [ + "projection = image.select(0).projection().getInfo()\n", + "projection" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "105", + "metadata": {}, + "outputs": [], + "source": [ + "crs = projection[\"crs\"]\n", + "crs_transform = projection[\"transform\"]" + ] + }, + { + "cell_type": "markdown", + "id": "106", + "metadata": {}, + "source": [ + "Specify region, crs, and crs_transform." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "107", + "metadata": {}, + "outputs": [], + "source": [ + "geemap.ee_export_image(\n", + " image,\n", + " filename=\"landsat_crs.tif\",\n", + " crs=crs,\n", + " crs_transform=crs_transform,\n", + " region=region,\n", + ")" + ] + }, + { + "cell_type": "markdown", + "id": "108", + "metadata": {}, + "source": [ + "To Google Drive" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "109", + "metadata": {}, + "outputs": [], + "source": [ + "geemap.ee_export_image_to_drive(\n", + " image, description=\"landsat\", folder=\"export\", region=region, scale=30\n", + ")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "110", + "metadata": {}, + "outputs": [], + "source": [ + "geemap.download_ee_image(image, \"landsat.tif\", scale=90)" + ] + }, + { + "cell_type": "markdown", + "id": "111", + "metadata": {}, + "source": [ + "### Exporting image collections" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "112", + "metadata": {}, + "outputs": [], + "source": [ + "point = ee.Geometry.Point(-99.2222, 46.7816)\n", + "collection = (\n", + " ee.ImageCollection(\"USDA/NAIP/DOQQ\")\n", + " .filterBounds(point)\n", + " .filterDate(\"2008-01-01\", \"2018-01-01\")\n", + " .filter(ee.Filter.listContains(\"system:band_names\", \"N\"))\n", + ")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "113", + "metadata": {}, + "outputs": [], + "source": [ + "collection.aggregate_array(\"system:index\")" + ] + }, + { + "cell_type": "markdown", + "id": "114", + "metadata": {}, + "source": [ + "To local drive" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "115", + "metadata": {}, + "outputs": [], + "source": [ + "geemap.ee_export_image_collection(collection, out_dir=\".\", scale=10)" + ] + }, + { + "cell_type": "markdown", + "id": "116", + "metadata": {}, + "source": [ + "To Google Drive" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "117", + "metadata": {}, + "outputs": [], + "source": [ + "geemap.ee_export_image_collection_to_drive(collection, folder=\"export\", scale=10)" + ] + }, + { + "cell_type": "markdown", + "id": "118", + "metadata": {}, + "source": [ + "### Exporting feature collections" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "119", + "metadata": {}, + "outputs": [], + "source": [ + "m = geemap.Map()\n", + "states = ee.FeatureCollection(\"TIGER/2018/States\")\n", + "fc = states.filter(ee.Filter.eq(\"NAME\", \"Alaska\"))\n", + "m.add_layer(fc, {}, \"Alaska\")\n", + "m.center_object(fc, 4)\n", + "m" + ] + }, + { + "cell_type": "markdown", + "id": "120", + "metadata": {}, + "source": [ + "To local drive" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "121", + "metadata": {}, + "outputs": [], + "source": [ + "geemap.ee_to_shp(fc, filename=\"Alaska.shp\")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "122", + "metadata": {}, + "outputs": [], + "source": [ + "geemap.ee_export_vector(fc, filename=\"Alaska.shp\")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "123", + "metadata": {}, + "outputs": [], + "source": [ + "geemap.ee_to_geojson(fc, filename=\"Alaska.geojson\")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "124", + "metadata": {}, + "outputs": [], + "source": [ + "geemap.ee_to_csv(fc, filename=\"Alaska.csv\")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "125", + "metadata": {}, + "outputs": [], + "source": [ + "gdf = geemap.ee_to_gdf(fc)\n", + "gdf" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "126", + "metadata": {}, + "outputs": [], + "source": [ + "df = geemap.ee_to_df(fc)\n", + "df" + ] + }, + { + "cell_type": "markdown", + "id": "127", + "metadata": {}, + "source": [ + "To Google Drive" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "128", + "metadata": {}, + "outputs": [], + "source": [ + "geemap.ee_export_vector_to_drive(\n", + " fc, description=\"Alaska\", fileFormat=\"SHP\", folder=\"export\"\n", + ")" + ] + }, + { + "cell_type": "markdown", + "id": "129", + "metadata": {}, + "source": [ + "## Creating timelapse animations\n", + "\n", + "### Landsat timelapse" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "130", + "metadata": {}, + "outputs": [], + "source": [ + "m = geemap.Map(center=[64.838721, -147.763366], zoom=11)\n", + "m" + ] + }, + { + "cell_type": "markdown", + "id": "131", + "metadata": {}, + "source": [ + "Pan and zoom the map to an area of interest. Use the drawing tools to draw a rectangle on the map. If no rectangle is drawn, the default rectangle shown below will be used." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "132", + "metadata": {}, + "outputs": [], + "source": [ + "roi = m.user_roi\n", + "if roi is None:\n", + " roi = ee.Geometry.BBox(-147.9701, 64.7733, -147.5849, 64.8717)\n", + " m.add_layer(roi)\n", + " m.center_object(roi)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "133", + "metadata": {}, + "outputs": [], + "source": [ + "timelapse = geemap.landsat_timelapse(\n", + " roi,\n", + " out_gif=\"Fairbanks.gif\",\n", + " start_year=2000,\n", + " end_year=2023,\n", + " start_date=\"06-01\",\n", + " end_date=\"09-01\",\n", + " bands=[\"SWIR1\", \"NIR\", \"Red\"],\n", + " frames_per_second=5,\n", + " title=\"Landsat Timelapse\",\n", + " progress_bar_color=\"blue\",\n", + " mp4=True,\n", + ")\n", + "geemap.show_image(timelapse)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "134", + "metadata": {}, + "outputs": [], + "source": [ + "m = geemap.Map(center=[64.838721, -147.763366], zoom=11)\n", + "m.add_gui(\"timelapse\")\n", + "m" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "135", + "metadata": {}, + "outputs": [], + "source": [ + "m = geemap.Map()\n", + "roi = ee.Geometry.BBox(-115.5541, 35.8044, -113.9035, 36.5581)\n", + "m.add_layer(roi)\n", + "m.center_object(roi)\n", + "m" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "136", + "metadata": {}, + "outputs": [], + "source": [ + "timelapse = geemap.landsat_timelapse(\n", + " roi,\n", + " out_gif=\"las_vegas.gif\",\n", + " start_year=1984,\n", + " end_year=2023,\n", + " bands=[\"NIR\", \"Red\", \"Green\"],\n", + " frames_per_second=5,\n", + " title=\"Las Vegas, NV\",\n", + " font_color=\"blue\",\n", + ")\n", + "geemap.show_image(timelapse)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "137", + "metadata": {}, + "outputs": [], + "source": [ + "m = geemap.Map()\n", + "roi = ee.Geometry.BBox(113.8252, 22.1988, 114.0851, 22.3497)\n", + "m.add_layer(roi)\n", + "m.center_object(roi)\n", + "m" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "138", + "metadata": {}, + "outputs": [], + "source": [ + "timelapse = geemap.landsat_timelapse(\n", + " roi,\n", + " out_gif=\"hong_kong.gif\",\n", + " start_year=1990,\n", + " end_year=2022,\n", + " start_date=\"01-01\",\n", + " end_date=\"12-31\",\n", + " bands=[\"SWIR1\", \"NIR\", \"Red\"],\n", + " frames_per_second=3,\n", + " title=\"Hong Kong\",\n", + ")\n", + "geemap.show_image(timelapse)" + ] + }, + { + "cell_type": "markdown", + "id": "139", + "metadata": {}, + "source": [ + "### Sentinel-2" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "140", + "metadata": {}, + "outputs": [], + "source": [ + "m = geemap.Map(center=[64.838721, -147.763366], zoom=11)\n", + "m" + ] + }, + { + "cell_type": "markdown", + "id": "141", + "metadata": {}, + "source": [ + "Pan and zoom the map to an area of interest. Use the drawing tools to draw a rectangle on the map. If no rectangle is drawn, the default rectangle shown below will be used." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "142", + "metadata": {}, + "outputs": [], + "source": [ + "roi = m.user_roi\n", + "if roi is None:\n", + " roi = ee.Geometry.BBox(-147.9701, 64.7733, -147.5849, 64.8717)\n", + " m.add_layer(roi)\n", + " m.center_object(roi)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "143", + "metadata": {}, + "outputs": [], + "source": [ + "timelapse = geemap.sentinel2_timelapse(\n", + " roi,\n", + " out_gif=\"sentinel2.gif\",\n", + " start_year=2017,\n", + " end_year=2023,\n", + " start_date=\"06-01\",\n", + " end_date=\"09-01\",\n", + " frequency=\"year\",\n", + " bands=[\"SWIR1\", \"NIR\", \"Red\"],\n", + " frames_per_second=3,\n", + " title=\"Sentinel-2 Timelapse\",\n", + ")\n", + "geemap.show_image(timelapse)" + ] + }, + { + "cell_type": "markdown", + "id": "144", + "metadata": {}, + "source": [ + "### MODIS\n", + "\n", + "MODIS vegetation indices" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "145", + "metadata": {}, + "outputs": [], + "source": [ + "m = geemap.Map()\n", + "m" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "146", + "metadata": {}, + "outputs": [], + "source": [ + "roi = m.user_roi\n", + "if roi is None:\n", + " roi = ee.Geometry.BBox(-18.6983, -36.1630, 52.2293, 38.1446)\n", + " m.add_layer(roi)\n", + " m.center_object(roi)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "147", + "metadata": {}, + "outputs": [], + "source": [ + "timelapse = geemap.modis_ndvi_timelapse(\n", + " roi,\n", + " out_gif=\"ndvi.gif\",\n", + " data=\"Terra\",\n", + " band=\"NDVI\",\n", + " start_date=\"2000-01-01\",\n", + " end_date=\"2022-12-31\",\n", + " frames_per_second=3,\n", + " title=\"MODIS NDVI Timelapse\",\n", + " overlay_data=\"countries\",\n", + ")\n", + "geemap.show_image(timelapse)" + ] + }, + { + "cell_type": "markdown", + "id": "148", + "metadata": {}, + "source": [ + "MODIS temperature" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "149", + "metadata": {}, + "outputs": [], + "source": [ + "m = geemap.Map()\n", + "m" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "150", + "metadata": {}, + "outputs": [], + "source": [ + "roi = m.user_roi\n", + "if roi is None:\n", + " roi = ee.Geometry.BBox(-171.21, -57.13, 177.53, 79.99)\n", + " m.add_layer(roi)\n", + " m.center_object(roi)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "151", + "metadata": {}, + "outputs": [], + "source": [ + "timelapse = geemap.modis_ocean_color_timelapse(\n", + " satellite=\"Aqua\",\n", + " start_date=\"2018-01-01\",\n", + " end_date=\"2020-12-31\",\n", + " roi=roi,\n", + " frequency=\"month\",\n", + " out_gif=\"temperature.gif\",\n", + " overlay_data=\"continents\",\n", + " overlay_color=\"yellow\",\n", + " overlay_opacity=0.5,\n", + ")\n", + "geemap.show_image(timelapse)" + ] + }, + { + "cell_type": "markdown", + "id": "152", + "metadata": {}, + "source": [ + "### GOES" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "153", + "metadata": {}, + "outputs": [], + "source": [ + "roi = ee.Geometry.BBox(167.1898, -28.5757, 202.6258, -12.4411)\n", + "start_date = \"2022-01-15T03:00:00\"\n", + "end_date = \"2022-01-15T07:00:00\"\n", + "data = \"GOES-17\"\n", + "scan = \"full_disk\"" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "154", + "metadata": {}, + "outputs": [], + "source": [ + "timelapse = geemap.goes_timelapse(\n", + " roi, \"goes.gif\", start_date, end_date, data, scan, framesPerSecond=5\n", + ")\n", + "geemap.show_image(timelapse)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "155", + "metadata": {}, + "outputs": [], + "source": [ + "roi = ee.Geometry.BBox(-159.5954, 24.5178, -114.2438, 60.4088)\n", + "start_date = \"2021-10-24T14:00:00\"\n", + "end_date = \"2021-10-25T01:00:00\"\n", + "data = \"GOES-17\"\n", + "scan = \"full_disk\"" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "156", + "metadata": {}, + "outputs": [], + "source": [ + "timelapse = geemap.goes_timelapse(\n", + " roi, \"hurricane.gif\", start_date, end_date, data, scan, framesPerSecond=5\n", + ")\n", + "geemap.show_image(timelapse)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "157", + "metadata": {}, + "outputs": [], + "source": [ + "roi = ee.Geometry.BBox(-121.0034, 36.8488, -117.9052, 39.0490)\n", + "start_date = \"2020-09-05T15:00:00\"\n", + "end_date = \"2020-09-06T02:00:00\"\n", + "data = \"GOES-17\"\n", + "scan = \"full_disk\"" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "158", + "metadata": {}, + "outputs": [], + "source": [ + "timelapse = geemap.goes_fire_timelapse(\n", + " roi, \"fire.gif\", start_date, end_date, data, scan, framesPerSecond=5\n", + ")\n", + "geemap.show_image(timelapse)" + ] + }, + { + "cell_type": "markdown", + "id": "159", + "metadata": {}, + "source": [ + "## Time series analysis\n", + "\n", + "### Visualizing forest cover\n", "\n", - "## Creating timelapse animations\n", + "We will use the [Hansen Global Forest Change v1.10 (2000-2022) dataset](https://developers.google.com/earth-engine/datasets/catalog/UMD_hansen_global_forest_change_2022_v1_10)." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "160", + "metadata": {}, + "outputs": [], + "source": [ + "dataset = ee.Image(\"UMD/hansen/global_forest_change_2022_v1_10\")\n", + "dataset.bandNames()" + ] + }, + { + "cell_type": "markdown", + "id": "161", + "metadata": {}, + "source": [ + "Select the imagery for 2000." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "162", + "metadata": {}, + "outputs": [], + "source": [ + "m = geemap.Map()\n", + "first_bands = [\"first_b50\", \"first_b40\", \"first_b30\"]\n", + "first_image = dataset.select(first_bands)\n", + "m.add_layer(first_image, {\"bands\": first_bands, \"gamma\": 1.5}, \"Landsat 2000\")" + ] + }, + { + "cell_type": "markdown", + "id": "163", + "metadata": {}, + "source": [ + "Select the imagery for 2022." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "164", + "metadata": {}, + "outputs": [], + "source": [ + "last_bands = [\"last_b50\", \"last_b40\", \"last_b30\"]\n", + "last_image = dataset.select(last_bands)\n", + "m.add_layer(last_image, {\"bands\": last_bands, \"gamma\": 1.5}, \"Landsat 2022\")" + ] + }, + { + "cell_type": "markdown", + "id": "165", + "metadata": {}, + "source": [ + "Select the tree cover imagery for 2000." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "166", + "metadata": {}, + "outputs": [], + "source": [ + "treecover = dataset.select([\"treecover2000\"])\n", + "treeCoverVisParam = {\"min\": 0, \"max\": 100, \"palette\": [\"black\", \"green\"]}\n", + "name = \"Tree cover (%)\"\n", + "m.add_layer(treecover, treeCoverVisParam, name)\n", + "m.add_colorbar(treeCoverVisParam, label=name, layer_name=name)\n", + "m.add(\"layer_manager\")\n", + "m" + ] + }, + { + "cell_type": "markdown", + "id": "167", + "metadata": {}, + "source": [ + "Extract tree cover 2000 by using the threshold of 10%." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "168", + "metadata": {}, + "outputs": [], + "source": [ + "threshold = 10\n", + "treecover_bin = treecover.gte(threshold).selfMask()\n", + "treeVisParam = {\"palette\": [\"green\"]}\n", + "m.add_layer(treecover_bin, treeVisParam, \"Tree cover bin\")" + ] + }, + { + "cell_type": "markdown", + "id": "169", + "metadata": {}, + "source": [ + "### Visualizing forest gain and loss\n", + "\n", + "Visualize forest loss." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "170", + "metadata": {}, + "outputs": [], + "source": [ + "m = geemap.Map(center=[64.864983, -147.840441], zoom=4)\n", + "m.add_basemap(\"Esri.WorldImagery\")\n", + "treeloss_year = dataset.select([\"lossyear\"])\n", + "treeLossVisParam = {\"min\": 0, \"max\": 22, \"palette\": [\"yellow\", \"red\"]}\n", + "layer_name = \"Tree loss year\"\n", + "m.add_layer(treeloss_year, treeLossVisParam, layer_name)\n", + "m.add_colorbar(treeLossVisParam, label=layer_name, layer_name=layer_name)\n", + "m.add(\"layer_manager\")\n", + "m" + ] + }, + { + "cell_type": "markdown", + "id": "171", + "metadata": {}, + "source": [ + "Compare forest loss and gain." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "172", + "metadata": {}, + "outputs": [], + "source": [ + "m = geemap.Map(center=[64.864983, -147.840441], zoom=4)\n", + "m.add_basemap(\"Esri.WorldImagery\")\n", + "treeloss = dataset.select([\"loss\"]).selfMask()\n", + "treegain = dataset.select([\"gain\"]).selfMask()\n", + "m.add_layer(treeloss, {\"palette\": \"red\"}, \"Tree loss\")\n", + "m.add_layer(treegain, {\"palette\": \"yellow\"}, \"Tree gain\")\n", + "m.add(\"layer_manager\")\n", + "m" + ] + }, + { + "cell_type": "markdown", + "id": "173", + "metadata": {}, + "source": [ + "### Calculating forest cover change\n", "\n", - "## Time series analysis" + "Compute zonal statistics to find out which county in Alaska has the largest forest area in 2000.\n", + "\n", + "Add a county boundary layer to the map." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "174", + "metadata": {}, + "outputs": [], + "source": [ + "counties = ee.FeatureCollection(\"TIGER/2018/Counties\").filter(\n", + " ee.Filter.eq(\"STATEFP\", \"02\")\n", + ")\n", + "df = geemap.ee_to_df(counties)\n", + "df.head()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "175", + "metadata": {}, + "outputs": [], + "source": [ + "style = {\"color\": \"0000FFFF\", \"fillColor\": \"00000000\"}\n", + "m.add_layer(counties, {}, \"Counties Vector\", False)\n", + "m.add_layer(counties.style(**style), {}, \"Counties Raster\")\n", + "m" + ] + }, + { + "cell_type": "markdown", + "id": "176", + "metadata": {}, + "source": [ + "Compute zonal statistics by county." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "177", + "metadata": {}, + "outputs": [], + "source": [ + "geemap.zonal_stats(\n", + " treecover_bin,\n", + " counties,\n", + " \"forest_cover.csv\",\n", + " stat_type=\"SUM\",\n", + " denominator=1e6,\n", + " scale=300,\n", + ")" + ] + }, + { + "cell_type": "markdown", + "id": "178", + "metadata": {}, + "source": [ + "Create a pie chart to visualize the forest area by county." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "179", + "metadata": {}, + "outputs": [], + "source": [ + "geemap.pie_chart(\n", + " \"forest_cover.csv\", names=\"NAME\", values=\"sum\", max_rows=20, height=400\n", + ")" + ] + }, + { + "cell_type": "markdown", + "id": "180", + "metadata": {}, + "source": [ + "Create a bar chart to visualize the forest area by county." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "181", + "metadata": {}, + "outputs": [], + "source": [ + "geemap.bar_chart(\n", + " \"forest_cover.csv\",\n", + " x=\"NAME\",\n", + " y=\"sum\",\n", + " max_rows=20,\n", + " x_label=\"County\",\n", + " y_label=\"Forest area (km2)\",\n", + ")" + ] + }, + { + "cell_type": "markdown", + "id": "182", + "metadata": {}, + "source": [ + "Calculate the forest loss area by county." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "183", + "metadata": {}, + "outputs": [], + "source": [ + "geemap.zonal_stats(\n", + " treeloss.gt(0),\n", + " counties,\n", + " \"treeloss.csv\",\n", + " stat_type=\"SUM\",\n", + " denominator=1e6,\n", + " scale=300,\n", + ")" + ] + }, + { + "cell_type": "markdown", + "id": "184", + "metadata": {}, + "source": [ + "Create a bar chart to visualize the forest loss area by county." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "185", + "metadata": {}, + "outputs": [], + "source": [ + "geemap.pie_chart(\"treeloss.csv\", names=\"NAME\", values=\"sum\", max_rows=20, height=600)" + ] + }, + { + "cell_type": "markdown", + "id": "186", + "metadata": {}, + "source": [ + "Create a bar chart to visualize the forest loss area by county." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "187", + "metadata": {}, + "outputs": [], + "source": [ + "geemap.bar_chart(\n", + " \"treeloss.csv\",\n", + " x=\"NAME\",\n", + " y=\"sum\",\n", + " max_rows=20,\n", + " x_label=\"County\",\n", + " y_label=\"Forest loss area (km2)\",\n", + ")" ] } ],