取消引用时如何防止 GDAL 将数据源写入磁盘
How to prevent GDAL from writing data source to disk when de-referenced
我需要从文件中提取栅格(存储为 numpy 数组)。遵循非常流行的 OGR Cookbook, I am reading in an OGR layer (geojson) and then rasterizing the vectors. I read that array using GDAL's ReadAsArray() 函数。一切正常,我可以对它做各种麻木的事情。但是,GDAL 会自动写出我创建的 GDAL 数据集,因为它会在程序结束后自动取消引用。我不need/want输出这个文件,因为它在磁盘上没有用,我只需要内存中的数据。您如何防止这种情况发生?
我试过不调用FlushCache()函数,但最后还是输出了文件。
代码:
...
# Create the destination data source
target = gdal.GetDriverByName('GTiff').Create(output_raster_path, source_raster.RasterXSize, source_raster.RasterYSize, 1, gdal.GDT_UInt16)
target.SetGeoTransform(source_raster.GetGeoTransform())
target.SetProjection(source_raster.GetProjection())
band = target.GetRasterBand(1)
band.SetNoDataValue(no_data_value)
gdal.RasterizeLayer(target, [1], source_layer, options=["ATTRIBUTE=BuildingID"])
raster = band.ReadAsArray()
return raster
之后,一旦程序完成,一个geotiff被写入output_raster_path
,我刚刚设置为"temp.tif"。
您可以使用 In-Memory Driver 来做类似的事情。
mem_drv = gdal.GetDriverByName('MEM')
target = mem_drv.Create('', source_raster.RasterXSize, source_raster.RasterYSize, 1, gdal.GDT_UInt16)
我需要从文件中提取栅格(存储为 numpy 数组)。遵循非常流行的 OGR Cookbook, I am reading in an OGR layer (geojson) and then rasterizing the vectors. I read that array using GDAL's ReadAsArray() 函数。一切正常,我可以对它做各种麻木的事情。但是,GDAL 会自动写出我创建的 GDAL 数据集,因为它会在程序结束后自动取消引用。我不need/want输出这个文件,因为它在磁盘上没有用,我只需要内存中的数据。您如何防止这种情况发生?
我试过不调用FlushCache()函数,但最后还是输出了文件。 代码:
...
# Create the destination data source
target = gdal.GetDriverByName('GTiff').Create(output_raster_path, source_raster.RasterXSize, source_raster.RasterYSize, 1, gdal.GDT_UInt16)
target.SetGeoTransform(source_raster.GetGeoTransform())
target.SetProjection(source_raster.GetProjection())
band = target.GetRasterBand(1)
band.SetNoDataValue(no_data_value)
gdal.RasterizeLayer(target, [1], source_layer, options=["ATTRIBUTE=BuildingID"])
raster = band.ReadAsArray()
return raster
之后,一旦程序完成,一个geotiff被写入output_raster_path
,我刚刚设置为"temp.tif"。
您可以使用 In-Memory Driver 来做类似的事情。
mem_drv = gdal.GetDriverByName('MEM')
target = mem_drv.Create('', source_raster.RasterXSize, source_raster.RasterYSize, 1, gdal.GDT_UInt16)