|
65 | 65 | "from shapely.geometry import box\n",
|
66 | 66 | "import urllib.request\n",
|
67 | 67 | "from rasterio.mask import mask\n",
|
| 68 | + "from rasterio.warp import calculate_default_transform, reproject, Resampling\n", |
68 | 69 | "import fiona\n",
|
69 | 70 | "%matplotlib inline"
|
70 | 71 | ]
|
|
700 | 701 | "print('')"
|
701 | 702 | ]
|
702 | 703 | },
|
| 704 | + { |
| 705 | + "cell_type": "markdown", |
| 706 | + "id": "4b6ca9ca", |
| 707 | + "metadata": {}, |
| 708 | + "source": [ |
| 709 | + "### Visualize WMS Raster Data In Any Projection\n", |
| 710 | + "\n", |
| 711 | + "<DIV align=\"left\" style=\"line-height:1.5em;\">\n", |
| 712 | + "<p>\n", |
| 713 | + " \n", |
| 714 | + "This example shows how to reproject WMS Raster Data to the projection of your choice using rasterio\n", |
| 715 | + " \n", |
| 716 | + "</p>\n", |
| 717 | + "</DIV>" |
| 718 | + ] |
| 719 | + }, |
| 720 | + { |
| 721 | + "cell_type": "code", |
| 722 | + "execution_count": null, |
| 723 | + "id": "3641a183", |
| 724 | + "metadata": {}, |
| 725 | + "outputs": [], |
| 726 | + "source": [ |
| 727 | + "# Here you can set the new projection to whatever you like \n", |
| 728 | + "dst_crs = 'EPSG:2261'\n", |
| 729 | + "\n", |
| 730 | + "# Save a global extents tiff file\n", |
| 731 | + "# Connect to GIBS WMS Service\n", |
| 732 | + "wms = WebMapService('https://gibs.earthdata.nasa.gov/wms/epsg4326/best/wms.cgi?', version='1.1.1')\n", |
| 733 | + "\n", |
| 734 | + "# Configure request for MODIS_Terra_SurfaceReflectance_Bands143\n", |
| 735 | + "img = wms.getmap(layers=['MODIS_Terra_SurfaceReflectance_Bands143'], # Layers\n", |
| 736 | + " srs='epsg:4326', # Map projection\n", |
| 737 | + " bbox=(-180,-90,180,90), # Bounds\n", |
| 738 | + " size=(1200, 600), # Image size\n", |
| 739 | + " time='2021-11-25', # Time of data\n", |
| 740 | + " format='image/tiff', # Image format\n", |
| 741 | + " transparent=True) # Nodata transparency\n", |
| 742 | + "\n", |
| 743 | + "# Save output TIFF to a file\n", |
| 744 | + "out = open('global_extents.tiff', 'wb')\n", |
| 745 | + "out.write(img.read())\n", |
| 746 | + "out.close()\n", |
| 747 | + "\n", |
| 748 | + "with rasterio.open('global_extents.tiff') as src:\n", |
| 749 | + " transform, width, height = calculate_default_transform(\n", |
| 750 | + " src.crs, dst_crs, src.width, src.height, *src.bounds)\n", |
| 751 | + " kwargs = src.meta.copy()\n", |
| 752 | + " kwargs.update({\n", |
| 753 | + " 'crs': dst_crs,\n", |
| 754 | + " 'transform': transform,\n", |
| 755 | + " 'width': width,\n", |
| 756 | + " 'height': height\n", |
| 757 | + " })\n", |
| 758 | + "\n", |
| 759 | + " with rasterio.open(os.getcwd()+'/reprojectedImage.byte.tif', 'w', **kwargs) as dst:\n", |
| 760 | + " for i in range(1, src.count + 1):\n", |
| 761 | + " reproject(\n", |
| 762 | + " source=rasterio.band(src, i),\n", |
| 763 | + " destination=rasterio.band(dst, i),\n", |
| 764 | + " src_transform=src.transform,\n", |
| 765 | + " src_crs=src.crs,\n", |
| 766 | + " dst_transform=transform,\n", |
| 767 | + " dst_crs=dst_crs,\n", |
| 768 | + " resampling=Resampling.nearest)" |
| 769 | + ] |
| 770 | + }, |
703 | 771 | {
|
704 | 772 | "attachments": {},
|
705 | 773 | "cell_type": "markdown",
|
|
0 commit comments