Skip to content

Commit

Permalink
Added contour function #688
Browse files Browse the repository at this point in the history
  • Loading branch information
giswqs committed Oct 26, 2021
1 parent fa6964f commit 63b7694
Show file tree
Hide file tree
Showing 4 changed files with 237 additions and 0 deletions.
91 changes: 91 additions & 0 deletions 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
}
91 changes: 91 additions & 0 deletions 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
}
54 changes: 54 additions & 0 deletions geemap/common.py
Expand Up @@ -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()
1 change: 1 addition & 0 deletions mkdocs.yml
Expand Up @@ -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
Expand Down

0 comments on commit 63b7694

Please sign in to comment.