What I want to realize is that
1, read a raster file as numpy array;
2, perform Gaussian blur for the array;
3, convert the array to raster file.
When I convert the array to the raster file, I need the select4.tif to be the same Xmin, Xmax, Ymin, Ymax as select4.tif file. In which the function ReprojectCoords is to project the same coordinate as select3.tif to select4.tif, and GetExtent is to fetch the extent from the select3.tif so as to set the same extent value and raster size to select4.tif.
Here are respectively the select3, select4 raster files.
The original raster file looks like 
After the processing and conversion, the select3.tif and select4.tif do not have the same extent 
I think the problem lies in the function GetExtent which the source codes are from this post How to get raster corner coordinates using Python GDAL bindings?. But I can't figure out why it generated a different extent as the original select3.tif file. Could anyone help me to find out where the problem is?
The following is the codes for the whole conversion procedure.
read the raster file as numpy array and do the Gaussian blur.
import numpy as np import gdal, ogr, os, osr #gaussian blur ds = gdal.Open("C:\\Users\\select\\select3.tif") myarray = np.array(ds.GetRasterBand(1).ReadAsArray()) # Gaussian blur def gaussian_blur(in_array, size): # expand in_array to fit edge of kernel padded_array = np.pad(in_array, size, 'symmetric') # build kernel x, y = np.mgrid[-size:size + 1, -size:size + 1] g = np.exp(-(x**2 / float(size) + y**2 / float(size))) g = (g / g.sum()).astype(in_array.dtype) # do the Gaussian blur return fftconvolve(padded_array, g, mode='valid') Gblur= gaussian_blur(myarray, size=10) # return numpy array define a function to project the coordinate
def ReprojectCoords(coords,src_srs,tgt_srs): ''' Reproject a list of x,y coordinates. @type geom: C{tuple/list} @param geom: List of [[x,y],...[x,y]] coordinates @type src_srs: C{osr.SpatialReference} @param src_srs: OSR SpatialReference object @type tgt_srs: C{osr.SpatialReference} @param tgt_srs: OSR SpatialReference object @rtype: C{tuple/list} @return: List of transformed [[x,y],...[x,y]] coordinates ''' trans_coords=[] transform = osr.CoordinateTransformation( src_srs, tgt_srs) for x,y in coords: x,y,z = transform.TransformPoint(x,y) trans_coords.append([x,y]) return trans_coords to get the Xmin, Ymin of the raster file
def GetExtent(gt,cols,rows): ''' Return list of corner coordinates from a geotransform @type gt: C{tuple/list} @param gt: geotransform @type cols: C{int} @param cols: number of columns in the dataset @type rows: C{int} @param rows: number of rows in the dataset @rtype: C{[float,...,float]} @return: coordinates of each corner ''' ext=[] xarr=[0,cols] yarr=[0,rows] for px in xarr: for py in yarr: x=gt[0]+(px*gt[1])+(py*gt[2]) y=gt[3]+(px*gt[4])+(py*gt[5]) ext.append([x,y]) #print (x,y) yarr.reverse() return ext ds = gdal.Open("C:\\Users\\select\\select3.tif") gt=ds.GetGeoTransform() cols = ds.RasterXSize rows = ds.RasterYSize ext=GetExtent(gt,cols,rows) project the spatial reference to the ext
src_srs = osr.SpatialReference() src_srs.ImportFromWkt(ds.GetProjection()) tgt_srs=osr.SpatialReference() tgt_srs.ImportFromEPSG(4326) tgt_srs = src_srs.CloneGeogCS() geo_ext = ReprojectCoords(ext, src_srs, tgt_srs) print(geo_ext) originXmin = geo_ext[0][0] originYmin = geo_ext[1][1] convert array to raster file
def array2raster(newRasterfn,rasterOrigin,pixelWidth,pixelHeight,array): cols = array.shape[1] rows = array.shape[0] originX =rasterOrigin[0] # 36.65808 originY =rasterOrigin[1] # 44.36431 driver = gdal.GetDriverByName('GTiff') outRaster = driver.Create(newRasterfn, cols, rows, 1, gdal.GDT_Byte) outRaster.SetGeoTransform((originX, pixelWidth, 0, originY, 0, pixelHeight)) outband = outRaster.GetRasterBand(1) outband.WriteArray(array) outRasterSRS = osr.SpatialReference() outRasterSRS.ImportFromEPSG(4326) outRaster.SetProjection(outRasterSRS.ExportToWkt()) outband.FlushCache() def main(newRasterfn,rasterOrigin,pixelWidth,pixelHeight,array): reversed_arr = array[::-1] # reverse array so the tif looks like the array array2raster(newRasterfn,rasterOrigin,pixelWidth,pixelHeight,reversed_arr) # convert array to raster array = Gblur rasterOrigin = (originXmin,originYmin) pixelWidth = 0.0001 pixelHeight = 0.0001 newRasterfn = "C:\\Users\\select\\select4.tif" main(newRasterfn,rasterOrigin,pixelWidth,pixelHeight,array)
GetExtentsfunction before theGaussian blur, when convert array after Gaussian blur to raster, I reset the coordinate based on the original image (which is the return values byGetExtents). So although the array may be modified by the Gaussian blur, I reset it again in thearray2rasterfunction. Could you write out a solution and highlight which line of codes should be modified if you already clear where the problem is. Thank you.