简体   繁体   English

如何使用gdal导入tif?

[英]How do I import tif using gdal?

How do I import tif using gdal? 如何使用gdal导入tif?

I'm trying to get my tif file in a usable format in Python, so I can analyze the data. 我正在尝试以可用格式在Python中获取tif文件,以便可以分析数据。 However, every time I import it, I just get an empty list. 但是,每次导入时,我只会得到一个空列表。 Here's my code: 这是我的代码:

xValues = [447520.0, 432524.0, 451503.0]
yValues = [4631976.0, 4608827.0, 4648114.0]

gdal.AllRegister()
dataset = gdal.Open('final_snow.tif', GA_ReadOnly)

if dataset is None:
    print 'Could not open image'
    sys.exit(1)

data = np.array([gdal.Open(name, gdalconst.GA_ReadOnly).ReadAsArray() for name, descr in       dataset.GetSubDatasets()])
print 'this is data ', data`

It always prints an empty list, but it doesn't throw an error. 它总是打印一个空列表,但不会引发错误。 I checked out other questions, such as [this] ( Create shapefile from tif file using GDAL ) What might be the problem? 我检查了其他问题,例如[this]( 使用GDAL从tif文件创建shapefile )可能是什么问题?

For osgeo.gdal , it should look like this: 对于osgeo.gdal ,它应如下所示:

from osgeo import gdal
gdal.UseExceptions()  # not required, but a good idea
dataset = gdal.Open('final_snow.tif', gdal.GA_ReadOnly)
data = dataset.ReadAsArray()

Where data is either a 2D array for 1-banded rasters, or a 3D array for multiband. 其中data是用于1波段栅格的2D数组,或者是用于多波段的3D数组。


An alternative with rasterio looks like: 使用rasterio的替代方法如下所示:

import rasterio
with rasterio.open('final_snow.tif', 'r') as r:
    data = r.read()

Where data is always a 3D array, with the first dimension as band index. 其中data始终是3D数组,第一维为band索引。

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

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