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.
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