2017-05-31 22 views
0

我有一個只存在於內存中的數據集GDAL(inmem_ds):使用gdal.Open打開內存中的數據集GDAL

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') 

現在我想直接通過這個數據集到外部Python庫使用gdal.Open()打開其輸入數據集。所以我需要一個物理文件路徑,我可以傳遞給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> 

回答

0

由於你已經看到,gdal.Open需要一個文件路徑,而內存數據集沒有文件路徑。 VRT等數據強制技術仍需要文件路徑(<SourceFilename>)。根據我的經驗和對這個問題的研究,我認爲沒有辦法將數據集(內存或基於文件)傳遞到gdal.Open。您可能必須寫入磁盤或嘗試修改外部庫...

0

您可以使用前綴vsimem將VRT寫入內存,請注意數據實際上被寫入內存,因此除VRT之外的任何格式可以顯着增加內存,這取決於實際的文件大小。

另請注意,即使在關閉數據集句柄後,內存文件仍然保留。所以當你完成使用它時,你需要自己解除鏈接,否則它會保持記憶。

因此,請跟蹤您正在使用的in-mem位置,以便您可以正確取消鏈接。寫入現有的vsimem位置,在其「未鏈接」位置之前將不起作用,並保留原始文件。這可能有點困難。

要輸出無論是在磁盤上的文件,或者一個已經打開的數據集,以一個在-MEM 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解釋器通過'外部',它可能無法工作。

+0

這適用於gdal.Translate()的輸入數據集對應於磁盤上的物理文件。但是,如果我使用僅存在於內存中的輸入數據集(如我的問題中所示),GDAL會在/vsimem/inmem.vrt中正確創建VRT。但是,當我嘗試使用gdal.Open('/ vsimem/inmem.vrt')重新打開它時,gdal.Open返回None,我得到GDAL異常「VRTRasterBand中缺少元素。」 – MoTSCHIGGE

相關問題