简体   繁体   English

使用matplotlib底图绘制.tif GDAL栅格

[英]Plot .tif GDAL raster using matplotlib Basemap

I have a bit of code from here that I am using for plotting a geotiff image ( Landsat ): 我从这里有一些代码可以用来绘制geotiff图像( 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))

But, it fails at ct.TransformPoints(xy_source.reshape(2, size).T)) and I'm not sure why. 但是,它在ct.TransformPoints(xy_source.reshape(2, size).T))处失败,我不确定为什么。 The error it gives me: 它给我的错误:

TypeError: in method 'CoordinateTransformation_TransformPoints', argument 1 of type 'OSRCoordinateTransformationShadow *' TypeError:在方法“ CoordinateTransformation_TransformPoints”中,类型为“ OSRCoordinateTransformationShadow *”的参数1

Which I do not understand. 我不明白。 Any OSR guru's out there? 那里有OSR专家吗?

Thanks for reading. 谢谢阅读。

EDIT 1 The projection of my .TIFF 编辑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"]]'

Also, 也,

>>> m.proj4string
Out[43]: '+lon_0=0.0 +y_0=8615499.05007 +R=6370997.0 +proj=robin +x_0=16986796.165 +units=m '

The solution is to install the proj4 package. 解决方案是安装proj4软件包。 Otherwise, the input projection is not understood by osr . 否则, osr无法理解输入投影。

conda install -c https://conda.binstar.org/jgomezdans proj4

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM