4

python使用gdal

 3 years ago
source link: https://hkvision.cn/2021/02/26/python%E4%BD%BF%E7%94%A8gdal/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

python使用gdal

2021年2月26日
|24 阅读

本文为原创文章,转载注明出处,欢迎关注网站https://hkvision.cn

一个项目需要用GDAL这个库,然后去稍微深入看了下这玩意,感觉还是有很多坑的。

就不介绍了,需要用这玩意的肯定知道这是啥

最重要的肯定是读写

from osgeo import gdal
ds = gdal.Open("{image_path}")
"""
有很多种Open的方法
OpenShared
OpenEx
还可以设置只读
flags=gdal.GA_ReadOnly
"""
drv = gdal.GetDriverByName("GTiff")
drv.Create("{filepath}", xsize, ysize, bds_num, eType. options=[])

看起来一切都很美好,但是这里面蕴含着一个很大的坑,读影像还好,但是写影像需要指定路径,并且,gdal没有额外的方式能创建一个空的DataSet,这意味着如果你想弄一些临时的DataSet(这非常普遍,稍微复杂一点的算法都会需要有这个),你必须要存在硬盘上一份文件,这简直太蠢了

当然这也不是没法解决的,GDAL其实有解决方案,那就是VRT文件,这个是GDAL的一种虚拟文件,是对影像的一种抽象表述,这个设定是很有意思的,给大家看个VRT文件的例子

<VRTDataset rasterXSize="4000" rasterYSize="4000" subClass="VRTPansharpenedDataset">
  <Metadata domain="IMAGE_STRUCTURE">
    <MDI key="INTERLEAVE">PIXEL</MDI>
  </Metadata>
  <VRTRasterBand dataType="Byte" band="1" subClass="VRTPansharpenedRasterBand">
    <ColorInterp>Red</ColorInterp>
  </VRTRasterBand>
  <VRTRasterBand dataType="Byte" band="2" subClass="VRTPansharpenedRasterBand">
    <ColorInterp>Green</ColorInterp>
  </VRTRasterBand>
  <VRTRasterBand dataType="Byte" band="3" subClass="VRTPansharpenedRasterBand">
    <ColorInterp>Blue</ColorInterp>
  </VRTRasterBand>
  <BlockXSize>512</BlockXSize>
  <BlockYSize>512</BlockYSize>
  <PansharpeningOptions>
    <Algorithm>WeightedBrovey</Algorithm>
    <AlgorithmOptions>
      <Weights>0.3333333333333333,0.3333333333333333,0.3333333333333333</Weights>
    </AlgorithmOptions>
    <Resampling>Cubic</Resampling>
    <SpatialExtentAdjustment>Union</SpatialExtentAdjustment>
    <PanchroBand>
      <SourceFilename relativeToVRT="1">pan_mask_opencv.tif</SourceFilename>
      <SourceBand>1</SourceBand>
    </PanchroBand>
    <SpectralBand dstBand="1">
      <SourceFilename relativeToVRT="1">origin_mask_opencv.tif</SourceFilename>
      <SourceBand>1</SourceBand>
    </SpectralBand>
    <SpectralBand dstBand="2">
      <SourceFilename relativeToVRT="1">origin_mask_opencv.tif</SourceFilename>
      <SourceBand>2</SourceBand>
    </SpectralBand>
    <SpectralBand dstBand="3">
      <SourceFilename relativeToVRT="1">origin_mask_opencv.tif</SourceFilename>
      <SourceBand>3</SourceBand>
    </SpectralBand>
  </PansharpeningOptions>
</VRTDataset>

这里我们定义了一个Pansharpened文件,也就是一个影像融合之后的影像,但是实际上在硬盘上的只是一个这样的描述文件,我如果想把他变成真正的影像文件,我可以这样做

from osgeo import gdal

ds = gdal.Open("pansharpened.vrt")
drv = gdal.GetDriverByName("GTiff")
drv.CreateCopy("pansharpened.tif", ds)

这样我就将一个很小的vrt文件变成了tif文件。

聪明的你肯定看出来了,这个vrt文件在实际编程的时候和这个tif文件没啥区别,但是不一样的是,vrt文件只占用这么小的空间,这是很爽的事情。

那么我们可以更进一步,这个vrt文件我都不想看到,怎么办?也可以,你只需要这样做

# doing something

# now we want to create a new DataSet
drv = gdal.GetDriverByName("VRT")
ds = drv.Create("", xoff, yoff, bds_num, eType. options=[])

是的,如果是vrt文件,大可以在filename这个参数上填空串,这样就不会出现这个文件了。至于这个文件是放在了内存中还是再怎么样,我也就不关心了(GDAL也有内存文件,可以参考这个

GDAL除了提供库以外,还提供了一些命令行程序,可以对影像进行一些简单的操作,具体的内容就不细说了,看这里

B站上还有个视频教程,不过是全英文无字幕的,看着操作还是能明白是什么意思,在这里

part1

part2


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK