Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/277.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何使用matplotlib basemap正确投影tif图像_Python_Matplotlib_Gdal_Matplotlib Basemap - Fatal编程技术网

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

Python 如何使用matplotlib basemap正确投影tif图像,python,matplotlib,gdal,matplotlib-basemap,Python,Matplotlib,Gdal,Matplotlib Basemap,我尝试使用gdal和matplotlib basemap显示光栅图像 我在这里解释我尝试使用basemap.interp函数的过程,有关我的过程的总体结构概述,请参阅我的。 首先,我的代码将加载并投影光栅 # Load Raster pathToRaster = r'I:\Data\anomaly//ano_DOY2002170.tif' raster = gdal.Open(pathToRaster, gdal.GA_ReadOnly) array = raster.GetRasterBand

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

我在这里解释我尝试使用basemap.interp函数的过程,有关我的过程的总体结构概述,请参阅我的。 首先,我的代码将加载并投影光栅

# 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')
这将绘制以下图形:

特别清楚的是,在北美和南美的东海岸,光栅数据和海岸线之间存在偏移

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


值得一提的是:(如果你下载它,它会在“a”和“no”之间加一个“-”,在“a-no-DOY.”之后加上“ano-DOY.”)

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

contourf
接受
latlon
关键字,如果为true,则接受lat/lon输入并自动将其转换为地图投影。因此:

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
定义,以删除输入光栅的翻转,这只是个人喜好

谢谢你的回答。看起来很棒!但是我无法复制你的结果。。我的地图保持白色,除了“添加更多信息”部分。您使用哪个版本的Basemap?我已经从pythonxy网站的附加插件安装了1.02版。在1.05版中引入了
latlon
关键字(im使用1.06)。我已经更新了我的答案以进行“手动”坐标转换:
xx,yy=m(lons,lats)
。这对你有用吗?是的!那很好用。目前,我们将使用它,并将很快更新到新版本,因为我看到1.02已经过时了