Sometimes, we need to transform the original raster dataset to a different resolution (usually coarser). In this post, I will introduce the simple workflow for cell resampling using Python.
1. Data import / preprocessing
To get started, first load the required library and then import the original raster data. In this case, we downloaded the Gross Domestic Product (GDP) distribution of China in 2010 with the resolution of 1 km x 1 km as the input. What’s more, a subset of China outlined by a shapefile was also used for clipping that raster.
import warnings warnings.filterwarnings('ignore') import cartopy import matplotlib.pyplot as plt from osgeo import gdal import rasterio from rasterio.mask import mask import geopandas as gpd from shapely.geometry import mapping import skimage.transform as st
# original data import from osgeo import gdal pathToRaster = r"./cngdp2010.tif" input_raster = gdal.Open(pathToRaster) prj=input_raster.GetProjection() srs=osr.SpatialReference(wkt=prj) if srs.IsProjected: print srs.GetAttrValue('projcs') print srs.GetAttrValue('geogcs')
# transforming the projection of the input_raster output_raster = r'./cngdp2010_adjust.tif' gdal.Warp(output_raster,input_raster,dstSRS='EPSG:4326',dstNodata = -9999)
# clipping the raster by polygon input_shape = r'./boundary.shp' shapefile = gpd.read_file(input_shape) shapefile = shapefile.to_crs(epsg=4326) input_raster = r'./cngdp2010_adjust.tif'# input raster geoms = shapefile.geometry.values # list of shapely geometries output_raster = r'./2+26city_gdp.tif'# output raster geometry = geoms[0] # shapely geometry geoms = [mapping(geoms[0])] with rasterio.open(input_raster) as src: out_image, out_transform = mask(src, geoms, crop=True) ## 出现了不配套的情况,怎么办 out_meta = src.meta.copy() out_meta.update({"driver": "GTiff", "height": out_image.shape[1], "width": out_image.shape[2], "transform": out_transform}) with rasterio.open(output_raster, "w", **out_meta) as dest: dest.write(out_image)
2. Resampling the data
There are many different approaches to adjust the resolution of the raster file. Here, I use one simple method suppoerted by skikit-image package. With the resize function, you are simply create a new 2-d array with a predefined mesh grid.
1 2 3 4 5 6 7 8
pathToRaster = r'./2+26city_gdp.tif' with rasterio.open(pathToRaster) as src: arr = src.read() arr[arr ==-9999] = np.nan arr = arr[0,:,:] # In this case, the orignial resolution was adjusted to 3 km x 3 km. new_shape = (round(arr.shape[0]/3.0),round(arr.shape[1]/3.0)) newarr= st.resize(array, new_shape, mode='constant')
3. Exporting Data
When the resampling works are done, the generated numpy array can be exported to geotiff format file for further uses. Luckily, some sufficient information for geotiff file can be easily assesed from the original one.
Kommentare