Last active
November 21, 2020 17:16
-
-
Save jdbcode/9e94659cf995fd38f787977b28b8dbc8 to your computer and use it in GitHub Desktop.
geemap_cartoee_global_age_old_young_ratio.ipynb
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"nbformat": 4, | |
"nbformat_minor": 0, | |
"metadata": { | |
"colab": { | |
"name": "geemap_cartoee_global_age_old_young_ratio.ipynb", | |
"provenance": [], | |
"collapsed_sections": [], | |
"include_colab_link": true | |
}, | |
"kernelspec": { | |
"name": "python3", | |
"display_name": "Python 3" | |
} | |
}, | |
"cells": [ | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "view-in-github", | |
"colab_type": "text" | |
}, | |
"source": [ | |
"<a href=\"https://colab.research.google.com/gist/jdbcode/9e94659cf995fd38f787977b28b8dbc8/geemap_cartoee_global_age_old_young_ratio.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>" | |
] | |
}, | |
{ | |
"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", | |
"age ratio of old persons (>= 65) to young persons (20-49) using the [Gridded Population of the World Version 4.11](https://developers.google.com/earth-engine/datasets/catalog/CIESIN_GPWv411_GPW_Basic_Demographic_Characteristics) Earth Engine dataset.\n", | |
"\n", | |
"See these notebooks for other geemap/cartoee 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": "xi8UDYfG1Pjs" | |
}, | |
"source": [ | |
"# Import GPW demographics dataset.\n", | |
"gpw_demog = (ee.ImageCollection('CIESIN/GPWv411/GPW_Basic_Demographic_Characteristics')\n", | |
" .select('basic_demographic_characteristics'))\n", | |
"\n", | |
"# Get \"young\" population; filter 20 - 39 years old and sum.\n", | |
"young = (gpw_demog.filter(ee.Filter.expression(\n", | |
" 'Age_Group == \"20-24\" || ' +\n", | |
" 'Age_Group == \"25-29\" || ' +\n", | |
" 'Age_Group == \"30-34\" || ' +\n", | |
" 'Age_Group == \"35-39\"'))\n", | |
" .filter(ee.Filter.expression('Sex == \"b\"'))\n", | |
" .sum()\n", | |
" .selfMask())\n", | |
"\n", | |
"# Get \"old\" population; filter >= 65 years old.\n", | |
"old = (gpw_demog.filter(ee.Filter.expression(\n", | |
" 'Age_Group == \"65 - \"'))\n", | |
" .filter(ee.Filter.expression('Sex == \"b\"'))\n", | |
" .first()\n", | |
" .selfMask())\n", | |
"\n", | |
"# Calculate ratio of old persons to young persons and apply vis params.\n", | |
"old_young_ratio_vis = {\n", | |
" 'min': 0,\n", | |
" 'max': 2,\n", | |
" 'palette': '#5ab4ac,#ffffff,#d8b365'\n", | |
"}\n", | |
"old_young_ratio = old.divide(young).visualize(**old_young_ratio_vis);\n", | |
"\n", | |
"# Prepare land/water layer.\n", | |
"mask_fill = ee.Image(1).visualize(**{'palette': '#B0B0B0'});\n", | |
"land_water = (ee.Image('MODIS/MOD44W/MOD44W_005_2000_02_24')\n", | |
" .select('water_mask')\n", | |
" .visualize(**{'palette': '#282828,#B0B0B0', 'min': 0, 'max': 1}))\n", | |
"land_water = mask_fill.blend(land_water)\n", | |
"\n", | |
"# Blend land/water with old-young ratio.\n", | |
"final_img = land_water.blend(old_young_ratio)\n", | |
"\n", | |
"# Set figure size and background color.\n", | |
"fig = plt.figure(figsize=(15,9.5), facecolor='#ffffff')\n", | |
"\n", | |
"# Create a new Interrupted Goode Homolosine projection.\n", | |
"projection = ccrs.InterruptedGoodeHomolosine()\n", | |
"\n", | |
"# Define bounding box to request data.\n", | |
"region = [-180, -90, 180, 90]\n", | |
"\n", | |
"# Plot the result with cartoee using Interrupted Goode Homolosine projection.\n", | |
"ax = cartoee.get_map(final_img, region=region, proj=projection)\n", | |
"\n", | |
"# Add a colorbar.\n", | |
"cb = cartoee.add_colorbar(\n", | |
" ax, loc='right', vis_params=old_young_ratio_vis,\n", | |
" label='Population age ratio (old/young)')\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