Friday, 4 August 2017

raster - Why does GDAL open a DTED file as a vector format?


GDAL version: 2.1.1


Language: C++


Given the path of a geospatial file, I want to know whether the file is a raster or vector format. The only way I have found so far to make that distinction is to attempt to open the file as vector format first. If that succeeds, then treat it as a vector file; otherwise, attempt to open the file as raster format. The following code snippet is an example.


This worked for GeoTIFF and shape files.


However, for a DTED file, opening it with the GDAL_OF_VECTOR flag gives me a valid dataset handle. So, my code incorrectly decides that it is a vector format.


Why did GDALOpenEx() open the DTED file when the GDAL_OF_VECTOR flag was used?


void test( string const & path )
{

GDALDatasetH hDataset;
bool isRaster = false;

unsigned int openFlags = GA_ReadOnly | GDAL_OF_VECTOR;
hDataset = GDALOpenEx( path.c_str(), openFlags, NULL, NULL, NULL );
if ( hDataset )
{
printf( "Opened as vector\n" );
isRaster = false;
}

else
{
openFlags = GA_ReadOnly;
hDataset = GDALOpenEx( path.c_str(), openFlags, NULL, NULL, NULL );
if ( hDataset )
{
printf( "Opened as raster\n" );
isRaster = true;
}
else

{
printf( "Not opened, skip it\n" );
return;
}
}

// Process the file differently, depending on its format.
if ( isRaster )
{
...

}
else
{
...
}

GDALClose( hDataset );
}

Answer



You can use GetRasterCount() to test if the dataset is raster and GetLayerCount() for vector.



http://osgeo-org.1560.x6.nabble.com/Discover-whether-a-GDALDataset-is-raster-or-vector-td5270223.html


For future reference, GDAL_OF_VECTOR does mean what you thought it meant, i.e only try vector drivers. However when you attempted to open the DTED file with the GDAL_OF_VECTOR flag, the SEG-Y driver opens it "successfully". I think it's a bug in the SEG-Y driver.


For example (in python):


from osgeo import gdal
print(gdal.__version__)

f = "n43.dt0"
ds = gdal.OpenEx(f, gdal.OF_VECTOR)

drv = ds.GetDriver()

print(drv.GetMetadata())

Output:


2.1.3
{'DCAP_OPEN': 'YES',
'DCAP_VECTOR': 'YES',
'DCAP_VIRTUALIO': 'YES',
'DMD_HELPTOPIC': 'drv_segy.html',
'DMD_LONGNAME': 'SEG-Y'}

No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...