【问题标题】:How to correctly project a tif image using matplotlib-basemap如何使用 matplotlib-basemap 正确投影 tif 图像
【发布时间】:2013-09-27 06:12:49
【问题描述】:

我尝试使用 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')

这将绘制以下内容:

特别清楚地看到,在北美和南美东海岸,栅格数据和海岸线存在偏移。

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

对于它的价值:My used raster tif file(如果你下载它在 'a' 和 'no' 之间放置一个 '-',在 'ano_DOY..' 之前 'a-no_DOY..' 之后)

【问题讨论】:

    标签: python matplotlib gdal matplotlib-basemap


    【解决方案1】:

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

    contourf 接受 latlon 关键字,如果为真,则接受纬度/经度输入并自动将其转换为地图投影。所以:

    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')
    

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

    【讨论】:

    • 感谢您的回答。看起来不错!但是..我无法重现您的结果..我的地图保持白色,除了“添加更多信息”部分。您使用哪个版本的底图?我已经从 pythonxy 网站的附加插件安装了 1.02 版。
    • latlon 关键字是在 1.05 版中引入的(我使用的是 1.06)。我已更新我的答案以进行“手动”坐标转换:xx, yy = m(lons,lats)。这对你有用吗?
    • 是的!这是完美的工作。暂时会使用这个,很快就会更新到更新的版本,因为我看到 1.02 已经过时了
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多