Skip to content

Instantly share code, notes, and snippets.

@jdbcode
Last active November 20, 2020 20:49
Show Gist options
  • Save jdbcode/155f23a33e581c5d12ec328f2aa4137f to your computer and use it in GitHub Desktop.
Save jdbcode/155f23a33e581c5d12ec328f2aa4137f to your computer and use it in GitHub Desktop.
geemap_cartoee_global_cropland_robinson_proj.ipynb
Display the source blob
Display the rendered blob
Raw
{
"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"colab": {
"name": "geemap_cartoee_global_cropland_robinson_proj.ipynb",
"provenance": [],
"collapsed_sections": []
},
"kernelspec": {
"name": "python3",
"display_name": "Python 3"
}
},
"cells": [
{
"cell_type": "markdown",
"metadata": {
"id": "i3bNbfgxpQzm"
},
"source": [
"This notebook uses [geemap](https://geemap.org/) with [cartoee](https://cartoee.readthedocs.io/en/latest/introduction.html) to make a figure of global\n",
"croplands using the Robinson projection based on Earth Engine tiles.\n",
"\n",
"See these notebooks for other useage examples:\n",
"\n",
"- https://github.com/giswqs/geemap/blob/master/examples/notebooks/cartoee_colab.ipynb\n",
"- https://github.com/giswqs/geemap/blob/master/examples/notebooks/cartoee_projections.ipynb\n",
"- https://github.com/giswqs/geemap/blob/master/examples/notebooks/cartoee_quickstart.ipynb"
]
},
{
"cell_type": "code",
"metadata": {
"id": "U8XgkNZjFaLk"
},
"source": [
"# Only run the install once, after install then restart session and proceed.\n",
"\n",
"# Install the Proj and GEOS libraries.\n",
"!apt-get install libproj-dev proj-bin\n",
"!apt-get install libgeos-dev\n",
"\n",
"# Install cartopy and geemap with all of the dependencies prebuilt.\n",
"!pip install cartopy geemap"
],
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "Y8k0PSDGHfYA"
},
"source": [
"# Import packages.\n",
"import matplotlib.patches as mpatches\n",
"import ee\n",
"import geemap.eefolium as geemap\n",
"from geemap import cartoee\n",
"import cartopy.crs as ccrs\n",
"\n",
"%pylab inline\n",
"Map = geemap.Map()"
],
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "KKpiC5FpwmcN"
},
"source": [
"# Prepare global cropland layer.\n",
"cropland = ee.Image('USGS/GFSAD1000_V1').select('landcover')\n",
"cropland_mask = cropland.gt(0)\n",
"cropland_vis = {\n",
" 'min': 1,\n",
" 'max': 5,\n",
" 'palette': '#ffa500,#964b00,#02a50f,#008B00,#FFFF00',\n",
"};\n",
"cropland = cropland.updateMask(cropland).visualize(**cropland_vis)\n",
"\n",
"# Prepare land/water layer.\n",
"mask_fill = ee.Image(1).visualize(**{'palette': '#00468b'});\n",
"land_water = (ee.Image('MODIS/MOD44W/MOD44W_005_2000_02_24')\n",
" .select('water_mask')\n",
" .visualize(**{'palette': '#282828,#00468b', 'min': 0, 'max': 1}))\n",
"land_water = mask_fill.blend(land_water)\n",
"\n",
"# Blend land/water with cropland\n",
"surface = land_water.blend(cropland).visualize(**{'opacity': 0.75})\n",
"\n",
"# Prepare global hillshade layer.\n",
"elev = (ee.Image('NOAA/NGDC/ETOPO1').select('bedrock')\n",
" .multiply(25).resample('bicubic'))\n",
"hillshade = ee.Terrain.hillshade(elev)\n",
"\n",
"# Blend hillshade with surface.\n",
"final_img = hillshade.blend(surface)\n",
"\n",
"# Increase figure size.\n",
"fig = plt.figure(figsize=(15,15))\n",
"\n",
"# Create a new Robinson projection.\n",
"projection = ccrs.Robinson()\n",
"\n",
"# Define bounding box to request data.\n",
"region = [-180, -90, 180, 90]\n",
"\n",
"# Plot the result with cartoee using the Robinson projection.\n",
"ax = cartoee.get_map(final_img, region=region, proj=projection)\n",
"\n",
"# Deal with legend.\n",
"class_labels = ['Croplands: irrigation major', 'Croplands: irrigation minor',\n",
" 'Croplands: rainfed', 'Croplands: rainfed, minor fragments',\n",
" 'Croplands: rainfed, very minor fragments']\n",
"pal = cropland_vis['palette'].split(',')\n",
"handles = [mpatches.Patch(color=c, label=l) for c, l in zip(pal, class_labels)]\n",
"plt.legend(loc=\"lower center\", bbox_to_anchor=(0.62, 0.022), handles=handles)\n",
"\n",
"plt.show()"
],
"execution_count": null,
"outputs": []
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment