Skip to content

Commit fd23129

Browse files
committed
added the example mapping with cartopy
1 parent f6c1ac9 commit fd23129

File tree

1 file changed

+130
-7
lines changed

1 file changed

+130
-7
lines changed

docs/python-usage.ipynb

Lines changed: 130 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,24 @@
3636
},
3737
{
3838
"cell_type": "code",
39-
"execution_count": 1,
39+
"execution_count": null,
4040
"id": "5a65137f",
4141
"metadata": {},
42-
"outputs": [],
42+
"outputs": [
43+
{
44+
"ename": "",
45+
"evalue": "",
46+
"output_type": "error",
47+
"traceback": [
48+
"\u001b[1;31mJupyter cannot be started. Error attempting to locate Jupyter: Running cells with 'Python 3.13.0 64-bit' requires jupyter and notebook package.\n",
49+
"\u001b[1;31mRun the following command to install 'jupyter and notebook' into the Python environment. \n",
50+
"\u001b[1;31mCommand: 'python -m pip install jupyter notebook -U\n",
51+
"\u001b[1;31mor\n",
52+
"\u001b[1;31mconda install jupyter notebook -U'\n",
53+
"\u001b[1;31mClick <a href='https://aka.ms/installJupyterForVSCode'>here</a> for more info."
54+
]
55+
}
56+
],
4357
"source": [
4458
"import os\n",
4559
"from io import BytesIO\n",
@@ -57,7 +71,9 @@
5771
"import xml.etree.ElementTree as xmlet\n",
5872
"import lxml.etree as xmltree\n",
5973
"from PIL import Image as plimg\n",
74+
"from PIL import ImageDraw\n",
6075
"import numpy as np\n",
76+
"import pandas as pd\n",
6177
"from owslib.wms import WebMapService\n",
6278
"from IPython.display import Image, display\n",
6379
"import rasterio\n",
@@ -67,6 +83,7 @@
6783
"from rasterio.mask import mask\n",
6884
"from rasterio.warp import calculate_default_transform, reproject, Resampling\n",
6985
"import fiona\n",
86+
"from datetime import datetime, timedelta\n",
7087
"%matplotlib inline"
7188
]
7289
},
@@ -989,6 +1006,68 @@
9891006
"m"
9901007
]
9911008
},
1009+
{
1010+
"cell_type": "markdown",
1011+
"id": "8b567f79",
1012+
"metadata": {},
1013+
"source": [
1014+
"### Animated Web Map with WMS\n",
1015+
"\n",
1016+
"<DIV align=\"left\" style=\"line-height:1.5em;\">\n",
1017+
"<p>\n",
1018+
"\n",
1019+
"The next example shows how to display IMERG_Precipitation_Rate layer in an animated web map (may require additional Python libraries). \n",
1020+
"\n",
1021+
"</p>\n",
1022+
"</DIV>"
1023+
]
1024+
},
1025+
{
1026+
"cell_type": "code",
1027+
"execution_count": null,
1028+
"id": "2b7d7657",
1029+
"metadata": {},
1030+
"outputs": [],
1031+
"source": [
1032+
"wms = WebMapService('https://gibs.earthdata.nasa.gov/wms/epsg4326/best/wms.cgi?', version='1.3.0')\n",
1033+
"\n",
1034+
"layers = ['MODIS_Aqua_CorrectedReflectance_TrueColor',\n",
1035+
" 'IMERG_Precipitation_Rate',\n",
1036+
" 'Reference_Features',\n",
1037+
" 'Reference_Labels']\n",
1038+
"color = 'rgb(255,255,255)'\n",
1039+
"frames = []\n",
1040+
"\n",
1041+
"start_date = datetime(2022, 9, 25)\n",
1042+
"end_date = datetime(2022, 10, 1)\n",
1043+
"dates = pd.date_range(start_date,end_date-timedelta(days=1),freq='d')\n",
1044+
"\n",
1045+
"for day in dates:\n",
1046+
" datatime = day.strftime(\"%Y-%m-%d\")\n",
1047+
" img = wms.getmap(layers=layers, # Layers\n",
1048+
" srs='epsg:4326', # Map projection\n",
1049+
" bbox=(-87, 18, -72, 35), # Bounds\n",
1050+
" size=(600,600), # Image size\n",
1051+
" time=datatime, # Time of data\n",
1052+
" format='image/png', # Image format\n",
1053+
" transparent=True) # Nodata transparency\n",
1054+
"\n",
1055+
" image = plimg.open(img)\n",
1056+
" draw = ImageDraw.Draw(image)\n",
1057+
" (x, y) = (50, 20)\n",
1058+
" draw.text((x, y), f'IMERG Precipitation Rate - {datatime}', fill=color)\n",
1059+
" frames.append(image)\n",
1060+
" \n",
1061+
"frames[0].save('IMERG_Precipitation_Rate_Ian.gif',\n",
1062+
" format='GIF',\n",
1063+
" append_images=frames,\n",
1064+
" save_all=True,\n",
1065+
" duration=1000,\n",
1066+
" loop=0)\n",
1067+
"\n",
1068+
"Image('IMERG_Precipitation_Rate_Ian.gif')"
1069+
]
1070+
},
9921071
{
9931072
"attachments": {},
9941073
"cell_type": "markdown",
@@ -1912,6 +1991,47 @@
19121991
"print('')"
19131992
]
19141993
},
1994+
{
1995+
"cell_type": "markdown",
1996+
"id": "d5e46225",
1997+
"metadata": {},
1998+
"source": [
1999+
"<a id=\"visualize_wmts_raster_data\"></a>\n",
2000+
"\n",
2001+
"### Visualize WMTS Raster Data By Cartopy\n",
2002+
"\n",
2003+
"<DIV align=\"left\" style=\"line-height:1.5em;\">\n",
2004+
"<p>\n",
2005+
" \n",
2006+
"This example shows how to display WMTS raster data on a map using Cartopy.\n",
2007+
" \n",
2008+
"</p>\n",
2009+
"</DIV>"
2010+
]
2011+
},
2012+
{
2013+
"cell_type": "code",
2014+
"execution_count": null,
2015+
"id": "50131451",
2016+
"metadata": {},
2017+
"outputs": [],
2018+
"source": [
2019+
"# Define the WMTS URL\n",
2020+
"wmts_url = \"https://gibs.earthdata.nasa.gov/wmts/epsg4326/best/wmts.cgi\"\n",
2021+
"\n",
2022+
"# Create a map with PlateCarree projection\n",
2023+
"ax = plt.axes(projection=ccrs.PlateCarree())\n",
2024+
"ax.set_extent([-180, 180, -90, 90]) # World extent\n",
2025+
"\n",
2026+
"# Add WMTS layer with a specific date\n",
2027+
"layer = \"VIIRS_SNPP_SurfaceReflectance_BandsM11-M7-M5\"\n",
2028+
"time = \"2023-01-01\"\n",
2029+
"ax.add_wmts(wmts_url, layer_name=layer, wmts_kwargs={\"time\": time})\n",
2030+
"\n",
2031+
"plt.title('Land Surface Reflectance (Bands M11-M7-M5, Best Available, VIIRS, Suomi NPP)')\n",
2032+
"plt.show()"
2033+
]
2034+
},
19152035
{
19162036
"attachments": {},
19172037
"cell_type": "markdown",
@@ -3694,11 +3814,9 @@
36943814
],
36953815
"metadata": {
36963816
"celltoolbar": "Raw Cell Format",
3697-
"interpreter": {
3698-
"hash": "aee8b7b246df8f9039afb4144a1f6fd8d2ca17a180786b69acc140d282b71a49"
3699-
},
37003817
"kernelspec": {
3701-
"display_name": "Python 3.10.4 64-bit",
3818+
"display_name": "Python 3.13.0 ('gibsPythonNotebook2')",
3819+
"language": "python",
37023820
"name": "python3"
37033821
},
37043822
"language_info": {
@@ -3711,7 +3829,12 @@
37113829
"name": "python",
37123830
"nbconvert_exporter": "python",
37133831
"pygments_lexer": "ipython3",
3714-
"version": "3.10.4 (v3.10.4:9d38120e33, Mar 23 2022, 17:29:05) [Clang 13.0.0 (clang-1300.0.29.30)]"
3832+
"version": "3.13.0"
3833+
},
3834+
"vscode": {
3835+
"interpreter": {
3836+
"hash": "cd6b7991ad4bc00fdd3e8f0f0395227ccb9271f102bce6443e69587a933de936"
3837+
}
37153838
}
37163839
},
37173840
"nbformat": 4,

0 commit comments

Comments
 (0)