Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve relativeToVRT sources management in VRTRawRasterBands #67

Closed
wants to merge 1 commit into from
Closed

Improve relativeToVRT sources management in VRTRawRasterBands #67

wants to merge 1 commit into from

Conversation

avalentino
Copy link
Member

The path addresses some inconsistencies relative to the SourceFilename of VRTRawRasterBands when the relativeToVRT element is set to ture.

With the current implementation datasets that are successfully created in a sub-directory can't be reopened for reading:

import os
import numpy as np
from osgeo import gdal
from osgeo.gdal_array import NumericTypeCodeToGDALTypeCode

gdal.UseExceptions()

filename = 'subdir/data.vrt'
os.makedirs('subdir', exist_ok=True)
data = np.ones((10, 10))
drv = gdal.GetDriverByName('VRT')
ysize, xsize = data.shape
gdtype = NumericTypeCodeToGDALTypeCode(data.dtype)
ds = drv.Create(filename, xsize, ysize, 0)
srcpath = os.path.splitext(filename)[0] + '.dat'
pixel_offset = gdal.GetDataTypeSize(gdtype) // 8
line_offset = xsize * pixel_offset
options = {
    'subClass': 'VRTRawRasterBand',
    'SourceFilename': srcpath,
    'ImageOffset': 0,
    'PixelOffset': pixel_offset,
    'LineOffset': line_offset,
    'relativeToVRT': 1,
}
options = ['{}={}'.format(k, v) for k, v in options.items()]
ds.AddBand(gdtype, options)
b = ds.GetRasterBand(1)
b.WriteArray(data)
ds.FlushCache()

data2 = gdal.Open(filename).ReadAsArray()

the above script gives the following error:

RuntimeError                              Traceback (most recent call last)
<ipython-input-1-84f5e1183627> in <module>()
     30 ds.FlushCache()
     31 
---> 32 data2 = gdal.Open(filename).ReadAsArray()
     33 

RuntimeError: Unable to open subdir/subdir/data.dat.
No such file or directory

@rouault
Copy link
Member

rouault commented Aug 20, 2015

Committed in trunk r29691

@rouault rouault closed this Aug 20, 2015
kwrobot pushed a commit to aashish24/gdal-svn that referenced this pull request Aug 20, 2015
…and (patch by Antonio Valentino, OSGeo/gdal#67)

git-svn-id: https://svn.osgeo.org/gdal/trunk/gdal@29691 f0d54148-0727-0410-94bb-9a71ac55c965
@avalentino avalentino deleted the VRTRawRasterBand_relativeToVrt branch August 20, 2015 17:25
This pull request was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants