使用gdal.Open打开内存中的GDAL数据集

时间:2017-05-31 19:19:51

标签: python gdal

我有一个只存在于内存中的GDAL数据集(inmem_ds):

In: gdal.Info(inmem_ds)
Out:
('Driver: NUMPY/Numeric Python Array\n'
 'Files: none associated\n'
 'Size is 500, 500\n'
 'Coordinate System is:\n'
 'PROJCS["WGS 84 / UTM zone 19N",\n'
 '    GEOGCS["WGS 84",\n'
 '        DATUM["WGS_1984",\n'
 '            SPHEROID["WGS 84",6378137,298.257223563,\n'
 '                AUTHORITY["EPSG","7030"]],\n'
 '            AUTHORITY["EPSG","6326"]],\n'
 '        PRIMEM["Greenwich",0,\n'
 '            AUTHORITY["EPSG","8901"]],\n'
 '        UNIT["degree",0.0174532925199433,\n'
 '            AUTHORITY["EPSG","9122"]],\n'
 '        AUTHORITY["EPSG","4326"]],\n'
 '    PROJECTION["Transverse_Mercator"],\n'
 '    PARAMETER["latitude_of_origin",0],\n'
 '    PARAMETER["central_meridian",-69],\n'
 '    PARAMETER["scale_factor",0.9996],\n'
 '    PARAMETER["false_easting",500000],\n'
 '    PARAMETER["false_northing",0],\n'
 '    UNIT["metre",1,\n'
 '        AUTHORITY["EPSG","9001"]],\n'
 '    AXIS["Easting",EAST],\n'
 '    AXIS["Northing",NORTH],\n'
 '    AUTHORITY["EPSG","32619"]]\n'
 'Origin = (617085.000000000000000,-1512585.000000000000000)\n'
 'Pixel Size = (30.000000000000000,-30.000000000000000)\n'
 'Corner Coordinates:\n'
 'Upper Left  (  617085.000,-1512585.000) ( 67d55\' 2.38"W, 13d40\'47.58"S)\n'
 'Lower Left  (  617085.000,-1527585.000) ( 67d55\' 0.13"W, 13d48\'55.77"S)\n'
 'Upper Right (  632085.000,-1512585.000) ( 67d46\'43.12"W, 13d40\'45.26"S)\n'
 'Lower Right (  632085.000,-1527585.000) ( 67d46\'40.59"W, 13d48\'53.42"S)\n'
 'Center      (  624585.000,-1520085.000) ( 67d50\'51.55"W, 13d44\'50.54"S)\n'
 'Band 1 Block=500x1 Type=Int16, ColorInterp=Undefined\n'
 '  NoData Value=-9999\n'
 'Band 2 Block=500x1 Type=Int16, ColorInterp=Undefined\n'
 '  NoData Value=-9999\n'
 'Band 3 Block=500x1 Type=Int16, ColorInterp=Undefined\n'
 '  NoData Value=-9999\n'
 'Band 4 Block=500x1 Type=Int16, ColorInterp=Undefined\n'
 '  NoData Value=-9999\n'
 'Band 5 Block=500x1 Type=Int16, ColorInterp=Undefined\n'
 '  NoData Value=-9999\n'
 'Band 6 Block=500x1 Type=Int16, ColorInterp=Undefined\n'
 '  NoData Value=-9999\n'
 'Band 7 Block=500x1 Type=Int16, ColorInterp=Undefined\n'
 '  NoData Value=-9999\n'
 'Band 8 Block=500x1 Type=Int16, ColorInterp=Undefined\n'
 '  NoData Value=-9999\n')

现在我想将此数据集直接传递给使用gdal.Open()打开其输入数据集的外部Python库。所以我需要一个物理文件路径,我可以传递给gdal.Open()。但我不想将内存中的GDAL数据集写入磁盘以节省IO时间。

有没有办法将内存数据集传递给gdal.Open()?

我尝试使用VRT数据集,但它不起作用:

drv = gdal.GetDriverByName("VRT")
vrt = drv.CreateCopy("/mypath/test.vrt", inmem_ds)
type(vrt.ReadAsArray()) # numpy.ndarray -> works

gdal.Open('/mypath/test.vrt') # None, last GDAL error: Exception: Error reading file:  Missing <SourceFilename> element in VRTRasterBand.

保存的VRT文件(mypath / test.vrt)如下所示:

<VRTDataset rasterXSize="500" rasterYSize="500">
  <SRS>PROJCS["WGS 84 / UTM zone 19N",GEOGCS["WGS 84",DATUM["WGS_1984",SPHEROID["WGS 84",6378137,298.257223563,AUTHORITY["EPSG","7030"]],AUTHORITY["EPSG","6326"]],PRIMEM["Greenwich",0,AUTHORITY["EPSG","8901"]],UNIT["degree",0.0174532925199433,AUTHORITY["EPSG","9122"]],AUTHORITY["EPSG","4326"]],PROJECTION["Transverse_Mercator"],PARAMETER["latitude_of_origin",0],PARAMETER["central_meridian",-69],PARAMETER["scale_factor",0.9996],PARAMETER["false_easting",500000],PARAMETER["false_northing",0],UNIT["metre",1,AUTHORITY["EPSG","9001"]],AXIS["Easting",EAST],AXIS["Northing",NORTH],AUTHORITY["EPSG","32619"]]</SRS>
  <GeoTransform>  6.1708500000000000e+05,  3.0000000000000000e+01,  0.0000000000000000e+00, -1.5125850000000000e+06,  0.0000000000000000e+00, -3.0000000000000000e+01</GeoTransform>
  <VRTRasterBand dataType="Int16" band="1">
    <NoDataValue>-9999</NoDataValue>
    <SimpleSource>
      <SourceFilename relativeToVRT="0"></SourceFilename>
      <SourceBand>1</SourceBand>
      <SourceProperties RasterXSize="500" RasterYSize="500" DataType="Int16" BlockXSize="500" BlockYSize="1" />
      <SrcRect xOff="0" yOff="0" xSize="500" ySize="500" />
      <DstRect xOff="0" yOff="0" xSize="500" ySize="500" />
    </SimpleSource>
  </VRTRasterBand>
  <VRTRasterBand dataType="Int16" band="2">
    <NoDataValue>-9999</NoDataValue>
    <SimpleSource>
      <SourceFilename relativeToVRT="0"></SourceFilename>
      <SourceBand>2</SourceBand>
      <SourceProperties RasterXSize="500" RasterYSize="500" DataType="Int16" BlockXSize="500" BlockYSize="1" />
      <SrcRect xOff="0" yOff="0" xSize="500" ySize="500" />
      <DstRect xOff="0" yOff="0" xSize="500" ySize="500" />
    </SimpleSource>
  </VRTRasterBand>
  <VRTRasterBand dataType="Int16" band="3">
    <NoDataValue>-9999</NoDataValue>
    <SimpleSource>
      <SourceFilename relativeToVRT="0"></SourceFilename>
      <SourceBand>3</SourceBand>
      <SourceProperties RasterXSize="500" RasterYSize="500" DataType="Int16" BlockXSize="500" BlockYSize="1" />
      <SrcRect xOff="0" yOff="0" xSize="500" ySize="500" />
      <DstRect xOff="0" yOff="0" xSize="500" ySize="500" />
    </SimpleSource>
  </VRTRasterBand>
  <VRTRasterBand dataType="Int16" band="4">
    <NoDataValue>-9999</NoDataValue>
    <SimpleSource>
      <SourceFilename relativeToVRT="0"></SourceFilename>
      <SourceBand>4</SourceBand>
      <SourceProperties RasterXSize="500" RasterYSize="500" DataType="Int16" BlockXSize="500" BlockYSize="1" />
      <SrcRect xOff="0" yOff="0" xSize="500" ySize="500" />
      <DstRect xOff="0" yOff="0" xSize="500" ySize="500" />
    </SimpleSource>
  </VRTRasterBand>
  <VRTRasterBand dataType="Int16" band="5">
    <NoDataValue>-9999</NoDataValue>
    <SimpleSource>
      <SourceFilename relativeToVRT="0"></SourceFilename>
      <SourceBand>5</SourceBand>
      <SourceProperties RasterXSize="500" RasterYSize="500" DataType="Int16" BlockXSize="500" BlockYSize="1" />
      <SrcRect xOff="0" yOff="0" xSize="500" ySize="500" />
      <DstRect xOff="0" yOff="0" xSize="500" ySize="500" />
    </SimpleSource>
  </VRTRasterBand>
  <VRTRasterBand dataType="Int16" band="6">
    <NoDataValue>-9999</NoDataValue>
    <SimpleSource>
      <SourceFilename relativeToVRT="0"></SourceFilename>
      <SourceBand>6</SourceBand>
      <SourceProperties RasterXSize="500" RasterYSize="500" DataType="Int16" BlockXSize="500" BlockYSize="1" />
      <SrcRect xOff="0" yOff="0" xSize="500" ySize="500" />
      <DstRect xOff="0" yOff="0" xSize="500" ySize="500" />
    </SimpleSource>
  </VRTRasterBand>
  <VRTRasterBand dataType="Int16" band="7">
    <NoDataValue>-9999</NoDataValue>
    <SimpleSource>
      <SourceFilename relativeToVRT="0"></SourceFilename>
      <SourceBand>7</SourceBand>
      <SourceProperties RasterXSize="500" RasterYSize="500" DataType="Int16" BlockXSize="500" BlockYSize="1" />
      <SrcRect xOff="0" yOff="0" xSize="500" ySize="500" />
      <DstRect xOff="0" yOff="0" xSize="500" ySize="500" />
    </SimpleSource>
  </VRTRasterBand>
  <VRTRasterBand dataType="Int16" band="8">
    <NoDataValue>-9999</NoDataValue>
    <SimpleSource>
      <SourceFilename relativeToVRT="0"></SourceFilename>
      <SourceBand>8</SourceBand>
      <SourceProperties RasterXSize="500" RasterYSize="500" DataType="Int16" BlockXSize="500" BlockYSize="1" />
      <SrcRect xOff="0" yOff="0" xSize="500" ySize="500" />
      <DstRect xOff="0" yOff="0" xSize="500" ySize="500" />
    </SimpleSource>
  </VRTRasterBand>
</VRTDataset>

2 个答案:

答案 0 :(得分:1)

如您所见,gdal.Open采用文件路径,内存数据集没有文件路径。像VRT这样的数据强制技术仍然需要文件路径(<SourceFilename>)。根据我的经验和对这个问题的研究,我认为没有办法将数据集(内存或基于文件)传递到gdal.Open。您可能必须写入磁盘或尝试修改外部库...

答案 1 :(得分:1)

您可以使用vsimem前缀将VRT写入内存,请注意数据实际写入内存,因此VRT以外的任何格式都会显着增加内存,具体取决于实际的文件大小。

另请注意,即使在关闭数据集句柄后,内存文件仍然存在。因此,当您完成使用它时,您需要自己取消链接,否则它将保留内存。

因此,请跟踪您正在使用的内存位置,以便正确取消链接。写入现有的vsimem位置,在其未连接的位置之前#39;不会工作,并保持原始文件。这可能是一个问题。

要将磁盘上的文件或已打开的数据集输出到内存中VRT:

ds = gdal.Translate('/vsimem/inmem.vrt', some_file_or_dataset, format='VRT')
print(ds.ReadAsArray().shape)
ds = None

再次打开它:

ds2 = gdal.Open('/vsimem/inmem.vrt')
print(ds2.ReadAsArray().shape)
ds2 = None

从内存中释放文件:

gdal.Unlink('/vsimem/inmem.vrt')

我不完全确定vsimem的范围,所以如果你的意思是“外部”的另一个Python解释器,它可能无效。