1

What is the correct method to determine if the resulting object is a raster and/or a vector from the result of a gdal.OpenEx call?

The "old" way (GDAL/OGR Determine if dataset is raster or vector) was to open a file with either gdal.Open or ogr.Open and observe if the result was None to infer the type.

Is it possible to directly query something about the resulting Dataset object to determine the primary GIS type of gdal.OpenEx?

3 Answers 3

2

This might not be the most desired way, but I have found it to be reliable in answering this question. There may be edge cases or drivers that can handle both vector and raster formats.

The short answer is to investigate the driver of the opened GDALDataset, and particularly the driver metadata which may include the fields DCAP_RASTER and DCAP_VECTOR.

I assume this is using python --

import gdal
for fname in ['example_raster.tif', 'example_vector.geo.json', 'example_vector.shp']:
    ds = gdal.OpenEx(fname)
    try:
        metadata = ds.GetDriver().GetMetadata()
    except:
        print(fname, 'ERROR')
    else:
        raster_capable = 'DCAP_RASTER' in metadata
        vector_capable = 'DCAP_VECTOR' in metadata
        print(fname)
        print(f'raster: {raster_capable}, vector: {vector_capable}')

1
  • Thanks for you answer, but you're right this doesn't work for the cases of datatypes that could be both. i.e. a geopackage that only has a vector in it. i.e. this only tells you about the capabilities of the driver, not the state of the data.
    – Rich
    Jul 31, 2019 at 17:16
1

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

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

However, some datasets can contain both raster and vector data... So depending on your usecase you may need to account for this:

if ds.RasterCount > 0 and ds.GetLayerCount() > 0:
    #  Raster and vector
elif ds.RasterCount > 0:
    # Raster
elif ds.GetLayerCount() > 0:
    # Vector
else:
    # Something else, empty dataset perhaps
2
  • Hi, I tried this and I think it's close. At least for GDAL 2.4.1 there is no GetRasterCount in the Python API, but there is a RasterCount field. So I think the correct answer is check for Dataset.GetLayerCount() != 0 for vector, and Dataset.RasterCount != 0 for raster. This is what works for me. If it looks okay to you, would you mind editing your answer so I can "accept" it?
    – Rich
    Jul 31, 2019 at 17:20
  • 1
    @Rich updated answer
    – user2856
    Aug 1, 2019 at 4:12
0

I would just match the name of the driver to a simple look up table. Most geospatial applications will only be working with a couple expected data types, so this is pretty easy to implement.

from osgeo import gdal


dtypes = {
    'raster': ['GTiff', 'JPEG'],
    'vector': ['ESRI Shapefile', 'GeoJSON']
}

def check_type(infile):
    ds = gdal.OpenEx(infile)
    driver_name = ds.GetDriver().LongName

    if driver_name in dtypes['raster']:
        return 'raster'
    elif driver_name in dtypes['vector']:
        return 'vector'
    else:
        print(f"WARNING: driver {driver_name} is not registered")

GDAL has an extensive list of raster and vector drivers.

1
  • Thanks for this, but I am trying to get every possible GIS type that GDAL supports. I think @user2856's answer is close.
    – Rich
    Jul 31, 2019 at 17:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.