{ "cells": [ { "cell_type": "markdown", "id": "0", "metadata": {}, "source": [ "\"Open\n", "\n", "Uncomment the following line to install [geemap](https://geemap.org) if needed." ] }, { "cell_type": "code", "execution_count": null, "id": "1", "metadata": {}, "outputs": [], "source": [ "# !pip install geemap" ] }, { "cell_type": "code", "execution_count": null, "id": "2", "metadata": {}, "outputs": [], "source": [ "import os\n", "import ee\n", "import geemap\n", "import ipywidgets as widgets\n", "from ipyleaflet import WidgetControl" ] }, { "cell_type": "code", "execution_count": null, "id": "3", "metadata": {}, "outputs": [], "source": [ "Map = geemap.Map(center=(40, -100), zoom=4, height=\"750px\")" ] }, { "cell_type": "code", "execution_count": null, "id": "4", "metadata": {}, "outputs": [], "source": [ "# Add Earth Engine dataset\n", "dem = ee.Image(\"USGS/SRTMGL1_003\")\n", "\n", "# Set visualization parameters.\n", "vis_params = {\n", " \"min\": 0,\n", " \"max\": 4000,\n", " \"palette\": [\"006633\", \"E5FFCC\", \"662A00\", \"D8D8D8\", \"F5F5F5\"],\n", "}\n", "\n", "# Add Earth Engine layers to Map\n", "Map.addLayer(dem, vis_params, \"SRTM DEM\", True, 0.7)\n", "\n", "Map" ] }, { "cell_type": "code", "execution_count": null, "id": "5", "metadata": {}, "outputs": [], "source": [ "output_widget = widgets.Output(layout={\"border\": \"1px solid black\"})\n", "output_control = WidgetControl(widget=output_widget, position=\"bottomright\")\n", "Map.add_control(output_control)" ] }, { "cell_type": "code", "execution_count": null, "id": "6", "metadata": {}, "outputs": [], "source": [ "style = {\"description_width\": \"initial\"}\n", "uploader = widgets.FileUpload(\n", " description=\"Upload data\",\n", " accept=\".zip, .json, .geojson\",\n", " multiple=False,\n", " button_style=\"primary\",\n", " style=style,\n", ")\n", "\n", "submit = widgets.Button(\n", " description=\"Display data\", button_style=\"success\", tooltip=\"Click me\", style=style\n", ")\n", "\n", "reset = widgets.Button(\n", " description=\"Reset\", button_style=\"warning\", tooltip=\"Click me\", style=style\n", ")" ] }, { "cell_type": "code", "execution_count": null, "id": "7", "metadata": {}, "outputs": [], "source": [ "with output_widget:\n", " print(\"Upload shapefile or \\ngeojson as a zip file\")\n", " display(uploader)\n", " display(submit)\n", " display(reset)" ] }, { "cell_type": "code", "execution_count": null, "id": "8", "metadata": {}, "outputs": [], "source": [ "def get_vector(upload_widget, out_dir=None):\n", " import zipfile\n", " import glob\n", "\n", " if out_dir is None:\n", " out_dir = os.path.join(os.path.expanduser(\"~\"), \"Downloads\")\n", " if not os.path.exists(out_dir):\n", " os.makedirs(out_dir)\n", "\n", " vector = None\n", "\n", " try:\n", " [uploaded_file] = upload_widget.value\n", " file = upload_widget.value[0]\n", " name = file[\"name\"]\n", " content = file[\"content\"]\n", " out_file = os.path.join(out_dir, name)\n", " with open(out_file, \"wb\") as fp:\n", " fp.write(content)\n", "\n", " if name.endswith(\".zip\"):\n", " with zipfile.ZipFile(out_file, \"r\") as zip_ref:\n", " extract_dir = os.path.join(\n", " out_dir, name[:-4] + \"_\" + geemap.random_string(3)\n", " )\n", " zip_ref.extractall(extract_dir)\n", " files = glob.glob(extract_dir + \"/*.shp\")\n", " if len(files) > 0:\n", " shp = files[0]\n", " vector = geemap.shp_to_ee(shp)\n", " else:\n", " files = glob.glob(extract_dir + \"/*.geojson\")\n", " if len(files) > 0:\n", " geojson = files[0]\n", " vector = geemap.geojson_to_ee(geojson)\n", " else:\n", " vector = geemap.geojson_to_ee(out_file)\n", "\n", " except Exception as e:\n", " print(e)\n", "\n", " return vector" ] }, { "cell_type": "code", "execution_count": null, "id": "9", "metadata": {}, "outputs": [], "source": [ "def submit_clicked(b):\n", " if len(uploader.value) > 0:\n", " Map.default_style = {\"cursor\": \"wait\"}\n", " try:\n", " fc = get_vector(uploader)\n", " layer_name = \"Layer \" + geemap.random_string(3)\n", " Map.addLayer(fc, {}, layer_name)\n", " Map.centerObject(fc)\n", " uploader.value = ()\n", " except Exception as e:\n", " print(e)\n", " Map.default_style = {\"cursor\": \"pointer\"}\n", "\n", "\n", "submit.on_click(submit_clicked)\n", "\n", "\n", "def reset_clicked(b):\n", " Map.layers = Map.layers[:3]\n", " output_widget.clear_output()\n", " with output_widget:\n", " print(\"Upload shapefile or \\ngeojson as a zip file\")\n", " display(uploader)\n", " display(submit)\n", " display(reset)\n", " uploader.value = ()\n", "\n", "\n", "reset.on_click(reset_clicked)" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" } }, "nbformat": 4, "nbformat_minor": 5 }