diff --git a/docs/notebooks/contours.ipynb b/docs/notebooks/contours.ipynb new file mode 100644 index 0000000000..b12f0db6e3 --- /dev/null +++ b/docs/notebooks/contours.ipynb @@ -0,0 +1,91 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "import ee\n", + "import geemap\n", + "import geemap.colormaps as cm" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# geemap.update_package()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "Map = geemap.Map()\n", + "Map" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "image = ee.Image(\"USGS/SRTMGL1_003\")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "Map.addLayer(image, {'min': 0, \"max\": 5000, \"palette\": cm.palettes.dem}, \"dem\")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "contours = geemap.create_contours(image, 0, 5000, 200, region=None)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "Map.addLayer(contours, {'min': 0, \"max\": 5000, 'palette': cm.palettes.terrain}, 'contours')" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.8.5" + } + }, + "nbformat": 4, + "nbformat_minor": 4 +} diff --git a/examples/notebooks/contours.ipynb b/examples/notebooks/contours.ipynb new file mode 100644 index 0000000000..b12f0db6e3 --- /dev/null +++ b/examples/notebooks/contours.ipynb @@ -0,0 +1,91 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "import ee\n", + "import geemap\n", + "import geemap.colormaps as cm" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# geemap.update_package()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "Map = geemap.Map()\n", + "Map" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "image = ee.Image(\"USGS/SRTMGL1_003\")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "Map.addLayer(image, {'min': 0, \"max\": 5000, \"palette\": cm.palettes.dem}, \"dem\")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "contours = geemap.create_contours(image, 0, 5000, 200, region=None)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "Map.addLayer(contours, {'min': 0, \"max\": 5000, 'palette': cm.palettes.terrain}, 'contours')" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.8.5" + } + }, + "nbformat": 4, + "nbformat_minor": 4 +} diff --git a/geemap/common.py b/geemap/common.py index b68a0e245b..a558ce7259 100644 --- a/geemap/common.py +++ b/geemap/common.py @@ -8798,3 +8798,57 @@ def temp_file_path(extension): file_path = os.path.join(tempfile.gettempdir(), f"{file_id}{extension}") return file_path + + +def create_contours(image, min_value, max_value, interval, kernel=None, region=None): + """Creates contours from an image. Code adapted from https://mygeoblog.com/2017/01/28/contour-lines-in-gee. Credits to MyGeoBlog. + + Args: + image (ee.Image): An image to create contours. + min_value (float): The minimum value of contours. + max_value (float): The maximum value of contours. + interval (float): The interval between contours. + kernel (ee.Kernel, optional): The kernal to use for smoothing image. Defaults to None. + region (ee.Geometry | ee.FeatureCollection, optional): The region of interest. Defaults to None. + + Raises: + TypeError: The image must be an ee.Image. + TypeError: The region must be an ee.Geometry or ee.FeatureCollection. + + Returns: + ee.Image: The image containing contours. + """ + if not isinstance(image, ee.Image): + raise TypeError("The image must be an ee.Image.") + if region is not None: + if isinstance(region, ee.FeatureCollection) or isinstance(region, ee.Geometry): + pass + else: + + raise TypeError( + "The region must be an ee.Geometry or ee.FeatureCollection." + ) + + if kernel is None: + kernel = ee.Kernel.gaussian(5, 3) + + values = ee.List.sequence(min_value, max_value, interval) + + def contouring(value): + mycountour = ( + image.convolve(kernel) + .subtract(ee.Image.constant(value)) + .zeroCrossing() + .multiply(ee.Image.constant(value).toFloat()) + ) + return mycountour.mask(mycountour) + + contours = values.map(contouring) + + if region is not None: + if isinstance(region, ee.FeatureCollection): + return ee.ImageCollection(contours).mosaic().clipToCollection(region) + elif isinstance(region, ee.Geometry): + return ee.ImageCollection(contours).mosaic().clip(region) + else: + return ee.ImageCollection(contours).mosaic() diff --git a/mkdocs.yml b/mkdocs.yml index 95c309891b..18dc4f3d33 100644 --- a/mkdocs.yml +++ b/mkdocs.yml @@ -162,6 +162,7 @@ nav: - notebooks/cartoee_quickstart.ipynb - notebooks/cartoee_scalebar.ipynb - notebooks/cartoee_subplots.ipynb + - notebooks/contours.ipynb - notebooks/file_browser.ipynb - notebooks/geemap_and_earthengine.ipynb - notebooks/geemap_and_folium.ipynb