简体   繁体   English

通过 python 模块在 QGIS 中导出计算栅格层

[英]Export a computed raster layer in QGIS by python module

I am using SAGA GIS and python modules in QGIS to compute the Topographic Position Index (TPI) for a group of Digital Elevation Models (DEMs) and I need to export each computed TPI in a separate raster file.我在 QGIS 中使用 SAGA GIS 和 python 模块来计算一组数字高程模型 (DEM) 的地形 Position 指数 (TPI),我需要将每个计算出的 TPI 导出到一个单独的栅格文件中。 How can I do it?我该怎么做? See my written code below:请参阅下面我编写的代码:

base_dir = 'D:/Selected DEMs'
fnames = [os.path.join(base_dir, fname) for fname in os.listdir(base_dir)]
dfs_names = (os.listdir(base_dir))

for i in range(len (fnames)):
    print (i)
    tpi = processing.run("saga:topographicpositionindextpi", {'DEM':fnames[i],'TPI':'TEMPORARY_OUTPUT','STANDARD':False,'DW_WEIGHTING':0,'DW_IDW_POWER':2,'DW_BANDWIDTH':75})

I found the answer.我找到了答案。 QGIS saves the computed TPI with SAGA GIS format (sdat) in a temp folder. QGIS 以 SAGA GIS 格式 (sdat) 将计算出的 TPI 保存在一个临时文件夹中。 The computed raster is in dict format as below:计算出的栅格采用字典格式,如下所示:

dict_items([('TPI', 'C:/Users/.../.../TPI.sdat')])). dict_items([('TPI', 'C:/Users/.../.../TPI.sdat')])).

Therefore, firstly it is required to read the TPI.sdat from the dict and then export it to Geotiff using rasterio as below:因此,首先需要从 dict 中读取 TPI.sdat,然后使用 rasterio 将其导出到 Geotiff,如下所示:

import rasterio as rio

base_dir = 'D:/Selected DEMs'
fnames = [os.path.join(base_dir, fname) for fname in os.listdir(base_dir)]
dfs_names = (os.listdir(base_dir))

for i in range(len (fnames)):
    print (i)
    tpi = processing.run("saga:topographicpositionindextpi", {'DEM':fnames[i],'TPI':'TEMPORARY_OUTPUT','STANDARD':False,'DW_WEIGHTING':0,'DW_IDW_POWER':2,'DW_BANDWIDTH':75})
    tpi.items()
    tpi['TPI']
    TPI_sdat_format = rio.open(tpi['TPI'])
    TPI_sdat_format_meta = TPI_sdat_format.profile   
    TPI_sdat_format_meta['driver'] = 'GTiff'
    TPI_data=TPI_sdat_format.read(1)
    
    with rio.open('D:/TPI_%s.tif'%i, 'w', **TPI_sdat_format_meta) as dst:
        dst.write(TPI_data, 1)

Because QGIS is used, I would use the modules available in QGIS.因为使用了 QGIS,所以我会使用 QGIS 中可用的模块。 First the file is written out as.sdat and then loaded back into QGIS as a raster layer.首先,文件被写为 .sdat,然后作为栅格层加载回 QGIS。 Then the raster can be written as a.tif file然后栅格可以写成一个.tif文件

from qgis.core import *
import os

base_dir = 'D:/Selected DEMs'
out_dir = 'D:/Precessed_DEMs'
fnames = os.listdir(base_dir)
EPSG = 'EPSG:25832' # example crs or set from layer r

for i in fnames:
    print (i)
    out_filename =  os.path.join(out_dir,os.path.splitext(i)[0] + '.sdat')
    tpi = processing.run("saga:topographicpositionindextpi",{'DEM':i,'TPI':out_filename ,'STANDARD':False,'DW_WEIGHTING':0,'DW_IDW_POWER':2,'DW_BANDWIDTH':75})

    rlayer = QgsRasterLayer(os.path.join(folder_preproc, os.path.splitext(i)[0]+".sdat"),os.path.splitext(i)[0])
    pipe = QgsRasterPipe()
    pipe.set(rlayer.dataProvider().clone())
    file_writer = QgsRasterFileWriter(os.path.join(folder_preproc, os.path.splitext(i)[0]+".tif"))
    file_writer.writeRaster(pipe, rlayer.width(),rlayer.height(), rlayer.extent(), QgsCoordinateReferenceSystem(EPSG))

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

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