使用matplotlib Basemap绘制.tif GDAL栅格
我在这里找到了一段代码,用来绘制一个地理信息的TIFF图像(比如Landsat的图像):
from mpl_toolkits.basemap import Basemap
from osgeo import osr, gdal
import matplotlib.pyplot as plt
import numpy as np
# Read the data and metadata
ds = gdal.Open(filename)
data = ds.ReadAsArray()
gt = ds.GetGeoTransform()
proj = ds.GetProjection()
xres = gt[1]
yres = gt[5]
# get the edge coordinates and add half the resolution
# to go to center coordinates
xmin = gt[0] + xres * 0.5
xmax = gt[0] + (xres * ds.RasterXSize) - xres * 0.5
ymin = gt[3] + (yres * ds.RasterYSize) + yres * 0.5
ymax = gt[3] - yres * 0.5
ds = None
# create a grid of xy coordinates in the original projection
xy_source = np.mgrid[xmin:xmax+xres:xres, ymax+yres:ymin:yres]
# Create the figure and basemap object
m = Basemap(projection='robin', lon_0=0, resolution='c')
# Create the projection objects for the convertion
inproj = osr.SpatialReference()
inproj.ImportFromWkt(proj)
# Get the target projection from the basemap object
outproj = osr.SpatialReference()
outproj.ImportFromProj4(m.proj4string)
size = xy_source[0,:,:].size
ct = osr.CoordinateTransformation(inproj, outproj)
xy_target = np.array(ct.TransformPoints(xy_source.reshape(2, size).T))
但是,它在执行ct.TransformPoints(xy_source.reshape(2, size).T))
的时候出错了,我不太明白为什么。错误信息是:
TypeError: in method 'CoordinateTransformation_TransformPoints', argument 1 of type 'OSRCoordinateTransformationShadow *'
这个我看不懂。有懂OSR的大神吗?
谢谢大家的阅读。
编辑 1 我这个.TIFF文件的投影信息
>>> print proj
Out[20]: 'PROJCS["WGS 84 / UTM zone 34N",GEOGCS["WGS 84",DATUM["WGS_1984",
SPHEROID["WGS 84",6378137,298.257223563,AUTHORITY["EPSG","7030"]],
AUTHORITY["EPSG","6326"]],PRIMEM["Greenwich",0],
UNIT["degree",0.0174532925199433],AUTHORITY["EPSG","4326"]],
PROJECTION["Transverse_Mercator"],PARAMETER["latitude_of_origin",0],
PARAMETER["central_meridian",21],PARAMETER["scale_factor",0.9996],
PARAMETER["false_easting",500000],PARAMETER["false_northing",0],
UNIT["metre",1,AUTHORITY["EPSG","9001"]],AUTHORITY["EPSG","32634"]]'
还有,
>>> m.proj4string
Out[43]: '+lon_0=0.0 +y_0=8615499.05007 +R=6370997.0 +proj=robin +x_0=16986796.165 +units=m '
1 个回答
1
解决办法是安装 proj4
这个软件包。否则,osr
就无法理解输入的投影信息。
conda install -c https://conda.binstar.org/jgomezdans proj4