python - 如何使用 matplotlib-basemap 正确投影 tif 图像

标签 python matplotlib gdal matplotlib-basemap

我尝试使用 gdal 和 matplotlib-basemap 显示光栅图像。

我在这里解释了我使用 basemap.interp 函数的尝试,有关我的流程的总体结构化概述,请查看我的 IPython Notebook 。 首先是我的代码来加载和投影光栅。

# Load Raster
pathToRaster = r'I:\Data\anomaly//ano_DOY2002170.tif'
raster = gdal.Open(pathToRaster, gdal.GA_ReadOnly)
array = raster.GetRasterBand(1).ReadAsArray()
msk_array = np.ma.masked_equal(array, value = 65535)
print 'Raster Projection:\n', raster.GetProjection()
print 'Raster GeoTransform:\n', raster.GetGeoTransform()

# Project raster image using Basemap and the basemap.interp function
map = Basemap(projection='robin',resolution='c',lat_0=0,lon_0=0)

datain = np.flipud( msk_array )

nx = raster.RasterXSize
ny = raster.RasterYSize

xin = np.linspace(map.xmin,map.xmax,nx) # nx is the number of x points on the grid
yin = np.linspace(map.ymin,map.ymax,ny) # ny in the number of y points on the grid

lons = np.arange(-180,180,0.25) #from raster.GetGeoTransform()
lats  = np.arange(-90,90,0.25) 

lons, lats = np.meshgrid(lons,lats) 
xout,yout = map(lons, lats)
dataout = mpl_toolkits.basemap.interp(datain, xin, yin, xout, yout, order=1)

levels = [-1000,-800,-600,-400,-200,0,200,400,600,800,1000]
cntr = map.contourf(xout,yout,dataout, levels,cmap=cm.RdBu)
cbar = map.colorbar(cntr,location='bottom',pad='15%')

# Add some more info to the map
cstl = map.drawcoastlines(linewidth=.5)
meri = map.drawmeridians(np.arange(0,360,60), linewidth=.2, labels=[1,0,0,1], labelstyle='+/-', color='grey' ) 
para = map.drawparallels(np.arange(-90,90,30), linewidth=.2, labels=[1,0,0,1], labelstyle='+/-', color='grey')
boun = map.drawmapboundary(linewidth=0.5, color='grey')

这将绘制以下内容:

data is offset compare to coastlines

特别明显的是,在北美和南美东海岸,栅格数据和海岸线存在偏移。

我不知道如何调整我的代码,以便我的数据将在正确的投影中进行转换。

它的值(value):My used raster tif file (如果您下载,则在“a”和“no”之间、“ano_DOY..”之前、“a-no_DOY..”之后放置一个“-”)

最佳答案

我不确定你自己的插值/重投影做错了什么,但它可以做得更简单。

contourf 接受 latlon 关键字,如果该关键字为 true,则接受 lat/lon 输入并自动将其转换为 map 投影。所以:

datain = msk_array

fig = plt.figure(figsize=(12,5))
map = Basemap(projection='robin',resolution='c',lat_0=0,lon_0=0)

ny, nx = datain.shape

xin = np.linspace(map.xmin,map.xmax,nx) # nx is the number of x points on the grid
yin = np.linspace(map.ymin,map.ymax,ny) # ny in the number of y points on the grid

lons = np.arange(-180,180,0.25) #from raster.GetGeoTransform()
lats  = np.arange(90,-90,-0.25) 

lons, lats = np.meshgrid(lons,lats)

xx, yy = m(lons,lats)

levels = [-1000,-800,-600,-400,-200,0,200,400,600,800,1000]
cntr = map.contourf(xx, yy,datain, levels,cmap=cm.RdBu)

cbar = map.colorbar(cntr,location='bottom',pad='15%')

# Add some more info to the map
cstl = map.drawcoastlines(linewidth=.5)
meri = map.drawmeridians(np.arange(0,360,60), linewidth=.2, labels=[1,0,0,1], labelstyle='+/-', color='grey' ) 
para = map.drawparallels(np.arange(-90,90,30), linewidth=.2, labels=[1,0,0,1], labelstyle='+/-', color='grey')
boun = map.drawmapboundary(linewidth=0.5, color='grey')

enter image description here

请注意,我更改了 lats 定义,以消除输入栅格的翻转,这只是个人喜好。

关于python - 如何使用 matplotlib-basemap 正确投影 tif 图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19043846/

相关文章:

python - 我如何将 gdal_grid 与点一起使用

python - Python pandas 的条件选择

python - Pytorch:如何找到 2D 张量的每一行中第一个非零元素的索引?

python - 如何解决 Spacy POS 属性 E1005 错误

python - 在 virtualenvwrapper 环境中安装 GDAL

Python GDAL/OGR : how to create a MapInfo TAB file?

Python给定一个N个整数的数组A,以O(n)的时间复杂度返回A中没有出现的最小正整数(大于0)

python - 绘制 Matplotlib 中的缺陷

python - pyplot hist() 每个堆叠组件的单独线条样式

python - 带有错误栏的空圆圈