python gdal多值提取至点工具

提取shp点文件栅格目录中的栅格数值序列

import os
from osgeo import gdal, gdalconst, ogr
import pandas as pd

class ExtractByPoint(object):
    def __init__(self, x, y, tifFile) -> None:
        self.x = x
        self.y = y
        self.tifFile = tifFile

# 地理坐标转像素坐标
    @staticmethod
    def geo2pixel(geot: list, geo: list):
        px = (geot[5]*geo[0]-geot[2]*geo[1]-geot[0]*geot[5] +
              geot[2]*geot[3])/(geot[1]*geot[5]-geot[2]*geot[4])
        py = (geot[4]*geo[0]-geot[1]*geo[1]+geot[1]*geot[3] -
              geot[4]*geot[0])/(geot[2]*geot[4]-geot[1]*geot[5])
        return [int(px), int(py)]

    def getValue(self):
        ds = gdal.Open(self.tifFile, gdalconst.GA_ReadOnly)
        geot = gdal.Dataset.GetGeoTransform(ds)
        px, py = ExtractByPoint.geo2pixel(geot, [self.x, self.y])
        band = gdal.Dataset.GetRasterBand(ds, 1)
        bandArray = gdal.Band.ReadAsArray(band)
        value = bandArray[py, px]
        return format(value, "0.2f")


if __name__ == "__main__":
    # 提取所有的tif影像
    tifsdir = 'tifs'
    alltifs = dict()
    for tifsdir_ in os.listdir(tifsdir):
        tifs = os.listdir(os.path.join(tifsdir, tifsdir_))
        tifs=filter(lambda filename:filename.endswith('.tif'),tifs)
        tifs = [os.path.join(tifsdir, tifsdir_, tif) for tif in tifs]
        alltifs[tifsdir_] = tifs
    pointShp = 'cape\cape.shp'
    ogr.RegisterAll()
    pds = ogr.Open(pointShp)
    pLay = ogr.DataSource.GetLayerByIndex(pds, 0)
    ogr.Layer.ResetReading(pLay)
    # 便利所有的点
    while True:
        pFea = ogr.Layer.GetNextFeature(pLay)
        if not(pFea):
            break
        name = int(ogr.Feature.GetFieldAsDouble(pFea, "ForecastSt"))
        pGeo = ogr.Feature.GetGeometryRef(pFea)
        x = ogr.Geometry.GetX(pGeo)
        y = ogr.Geometry.GetY(pGeo)
        pdict = dict()
        # 添加日期字段
        for key in alltifs.keys():
            tifs=alltifs[key]
            for tif in tifs:
                tifname=os.path.basename(tif)
                index=tifname.find('_')
                dates.append(tifname[(index+1):-4].replace('_',''))
            break
        for key, tifs in alltifs.items():
            pvalues = []
            for tif in tifs:
                e = ExtractByPoint(x, y, tif)
                pvalue = e.getValue()
                pvalues.append(pvalue)
            pdict[key] = pvalues
        dates = []
        pdict['date']=dates
        pdf = pd.DataFrame(pdict)
        csv_name = './csvs/{}.csv'.format(name)
        pdf.to_csv(csv_name, index=False)
    
    print('ok!')

  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Python GDAL是一个用来处理地理空间数据的开源库,可以用来处理卫星数据。 GDAL(地理数据抽象库)是一个强大的地理空间数据处理库,可以读取、写入和分析各种格式的栅格和矢量数据。GDALPython中的接口被称为Python GDAL,它结合了Python的便捷性和GDAL的功能,使得处理卫星数据变得更加高效和便捷。 使用Python GDAL可以完成以下卫星数据处理任务: 1. 数据读取:Python GDAL可以读取各种格式的卫星数据,例如GeoTIFF、HDF、NetCDF等。通过打开数据集,可以获取数据的基本信息,如大小、数据类型、地理坐标系统等。 2. 数据处理:Python GDAL提供了一系列的函数和方法,可以对卫星数据进行处理和分析。例如,可以创建影像金字塔、重采样、切割、裁剪、合并、投影转换等操作。 3. 数据提取:可以通过Python GDAL提取图像中的特定区域、像素值、波段等信息。这对于进行卫星图像分类、变化检测等任务非常有用。 4. 数据写入:Python GDAL可以将处理后的卫星数据保存为各种格式,包括GeoTIFF、HDF、NetCDF等。这样可以方便地将处理结果用于其他软件或分享给他人。 Python GDAL具有广泛的功能和灵活的扩展性,可以通过结合其他Python库和工具,如NumPy、Pandas、Matplotlib等,实现更复杂的卫星数据处理和分析任务。 总之,利用Python GDAL可以方便地读取、处理和分析卫星数据,为地理空间数据的研究和应用提供了强大的工具

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值