|
60 | 60 | "import numpy as np\n",
|
61 | 61 | "from owslib.wms import WebMapService\n",
|
62 | 62 | "from IPython.display import Image, display\n",
|
| 63 | + "import rasterio\n", |
| 64 | + "import geopandas as gpd\n", |
| 65 | + "from shapely.geometry import box\n", |
| 66 | + "import urllib.request\n", |
| 67 | + "from rasterio.mask import mask\n", |
| 68 | + "import fiona\n", |
63 | 69 | "%matplotlib inline"
|
64 | 70 | ]
|
65 | 71 | },
|
|
367 | 373 | "print('') "
|
368 | 374 | ]
|
369 | 375 | },
|
| 376 | + { |
| 377 | + "cell_type": "markdown", |
| 378 | + "id": "50cca222", |
| 379 | + "metadata": {}, |
| 380 | + "source": [ |
| 381 | + "You can also use rasterio to access the properties of a geospatial raster file \n" |
| 382 | + ] |
| 383 | + }, |
| 384 | + { |
| 385 | + "cell_type": "code", |
| 386 | + "execution_count": null, |
| 387 | + "id": "4dd19a30", |
| 388 | + "metadata": {}, |
| 389 | + "outputs": [], |
| 390 | + "source": [ |
| 391 | + "proj4326 = 'https://gibs.earthdata.nasa.gov/wms/epsg4326/best/wms.cgi?\\\n", |
| 392 | + "version=1.3.0&service=WMS&request=GetMap&\\\n", |
| 393 | + "format=image/jpeg&STYLE=default&bbox=-90,-180,90,180&CRS=EPSG:4326&\\\n", |
| 394 | + "HEIGHT=512&WIDTH=512&TIME=2021-11-25&layers=MODIS_Terra_SurfaceReflectance_Bands143'\n", |
| 395 | + "with rasterio.open(proj4326) as src:\n", |
| 396 | + " print(src.width, src.height)\n", |
| 397 | + " print(src.crs)\n", |
| 398 | + " print(src.transform)\n", |
| 399 | + " print(src.count)\n", |
| 400 | + " print(src.indexes)" |
| 401 | + ] |
| 402 | + }, |
370 | 403 | {
|
371 | 404 | "attachments": {},
|
372 | 405 | "cell_type": "markdown",
|
|
3229 | 3262 | "plt.setp(axes.get_xticklabels(), rotation=30, horizontalalignment='right');"
|
3230 | 3263 | ]
|
3231 | 3264 | },
|
| 3265 | + { |
| 3266 | + "cell_type": "markdown", |
| 3267 | + "id": "aefa506c", |
| 3268 | + "metadata": {}, |
| 3269 | + "source": [ |
| 3270 | + "### Overlaying\n", |
| 3271 | + "You can overlay multiple images and change the transparency to get your desired outcome " |
| 3272 | + ] |
| 3273 | + }, |
| 3274 | + { |
| 3275 | + "cell_type": "code", |
| 3276 | + "execution_count": null, |
| 3277 | + "id": "581736a2", |
| 3278 | + "metadata": {}, |
| 3279 | + "outputs": [], |
| 3280 | + "source": [ |
| 3281 | + "background_image_url = 'https://gibs.earthdata.nasa.gov/wms/epsg4326/best/wms.cgi?\\\n", |
| 3282 | + "version=1.3.0&service=WMS&request=GetMap&\\\n", |
| 3283 | + "format=image/jpeg&STYLE=default&bbox=-45,-90,0,-45&CRS=EPSG:4326&\\\n", |
| 3284 | + "HEIGHT=512&WIDTH=512&TIME=2021-11-25&layers=MODIS_Terra_SurfaceReflectance_Bands143'\n", |
| 3285 | + "\n", |
| 3286 | + "VIIRS_overlay_image_url = 'https://gibs.earthdata.nasa.gov/wms/epsg4326/best/wms.cgi?\\\n", |
| 3287 | + "version=1.3.0&service=WMS&request=GetMap&\\\n", |
| 3288 | + "format=image/jpeg&STYLE=default&bbox=-45,-90,0,-45&CRS=EPSG:4326&\\\n", |
| 3289 | + "HEIGHT=512&WIDTH=512&TIME=2021-11-25&layers=VIIRS_SNPP_DayNightBand_At_Sensor_Radiance'\n", |
| 3290 | + "\n", |
| 3291 | + "background = plimg.open(urllib.request.urlopen(background_image_url))\n", |
| 3292 | + "overlay = plimg.open(urllib.request.urlopen(VIIRS_overlay_image_url))\n", |
| 3293 | + "\n", |
| 3294 | + "background = background.convert(\"RGBA\")\n", |
| 3295 | + "overlay = overlay.convert(\"RGBA\")\n", |
| 3296 | + "\n", |
| 3297 | + "# .75 is the transparency for this example\n", |
| 3298 | + "VIIRS_overlay = plimg.blend(background, overlay, 0.75)\n", |
| 3299 | + "VIIRS_overlay.save(\"VIIRSOverlay.png\",\"PNG\")" |
| 3300 | + ] |
| 3301 | + }, |
| 3302 | + { |
| 3303 | + "cell_type": "markdown", |
| 3304 | + "id": "5604ba18", |
| 3305 | + "metadata": {}, |
| 3306 | + "source": [ |
| 3307 | + "You can also crop a TIFF file to be just within the bounds of a Shapefile " |
| 3308 | + ] |
| 3309 | + }, |
| 3310 | + { |
| 3311 | + "cell_type": "code", |
| 3312 | + "execution_count": null, |
| 3313 | + "id": "e868d1da", |
| 3314 | + "metadata": {}, |
| 3315 | + "outputs": [], |
| 3316 | + "source": [ |
| 3317 | + "# Create a box Shapefile \n", |
| 3318 | + "\n", |
| 3319 | + "# Define the bounding box over the continental US\n", |
| 3320 | + "minx, miny, maxx, maxy = -125, 25, -66, 50 \n", |
| 3321 | + "\n", |
| 3322 | + "# Create a GeoDataFrame with the bounding box\n", |
| 3323 | + "geometry = [box(minx, miny, maxx, maxy)]\n", |
| 3324 | + "gdf = gpd.GeoDataFrame({\"id\": [1]}, geometry=geometry, crs=\"EPSG:4326\")\n", |
| 3325 | + "\n", |
| 3326 | + "gdf.to_file(\"us_box.shp\")\n", |
| 3327 | + "\n", |
| 3328 | + "# Save a global extents tiff file\n", |
| 3329 | + "# Connect to GIBS WMS Service\n", |
| 3330 | + "wms = WebMapService('https://gibs.earthdata.nasa.gov/wms/epsg4326/best/wms.cgi?', version='1.1.1')\n", |
| 3331 | + "\n", |
| 3332 | + "# Configure request for MODIS_Terra_SurfaceReflectance_Bands143\n", |
| 3333 | + "img = wms.getmap(layers=['MODIS_Terra_SurfaceReflectance_Bands143'], # Layers\n", |
| 3334 | + " srs='epsg:4326', # Map projection\n", |
| 3335 | + " bbox=(-180,-90,180,90), # Bounds\n", |
| 3336 | + " size=(1200, 600), # Image size\n", |
| 3337 | + " time='2021-11-25', # Time of data\n", |
| 3338 | + " format='image/tiff', # Image format\n", |
| 3339 | + " transparent=True) # Nodata transparency\n", |
| 3340 | + "\n", |
| 3341 | + "# Save output TIFF to a file\n", |
| 3342 | + "out = open('global_extents.tiff', 'wb')\n", |
| 3343 | + "out.write(img.read())\n", |
| 3344 | + "out.close()\n" |
| 3345 | + ] |
| 3346 | + }, |
| 3347 | + { |
| 3348 | + "cell_type": "code", |
| 3349 | + "execution_count": null, |
| 3350 | + "id": "28a84e80", |
| 3351 | + "metadata": {}, |
| 3352 | + "outputs": [], |
| 3353 | + "source": [ |
| 3354 | + "# Crop and plot the global extents TIFF file using the US box Shapefile \n", |
| 3355 | + "with fiona.open(\"us_box.shp\", \"r\") as shapefile:\n", |
| 3356 | + " shapes = [feature[\"geometry\"] for feature in shapefile]\n", |
| 3357 | + "\n", |
| 3358 | + "with rasterio.open(\"global_extents.tiff\") as src:\n", |
| 3359 | + " out_image, out_transform = rasterio.mask.mask(src, shapes, crop=True)\n", |
| 3360 | + " out_meta = src.meta\n", |
| 3361 | + "\n", |
| 3362 | + "plt.imshow(out_image.transpose(1, 2, 0)) # Transpose to (height, width, channels) for plotting\n", |
| 3363 | + "plt.show()" |
| 3364 | + ] |
| 3365 | + }, |
3232 | 3366 | {
|
3233 | 3367 | "attachments": {},
|
3234 | 3368 | "cell_type": "markdown",
|
|
3441 | 3575 | "print(\"Number of images downloaded:\", len(os.listdir(outdir)))"
|
3442 | 3576 | ]
|
3443 | 3577 | },
|
| 3578 | + { |
| 3579 | + "cell_type": "markdown", |
| 3580 | + "id": "b77926e4", |
| 3581 | + "metadata": {}, |
| 3582 | + "source": [ |
| 3583 | + "**NOTE** The available options for the format parameter are: `image/png`, `image/tiff`, `image/jpeg`" |
| 3584 | + ] |
| 3585 | + }, |
3444 | 3586 | {
|
3445 | 3587 | "attachments": {},
|
3446 | 3588 | "cell_type": "markdown",
|
|
0 commit comments