7

I am writing a script using the GDAL/OGR Python bindings and it is possible that the user may provide a raster dataset or a vector dataset as input. If the user provides a vector dataset, the script will convert it to raster.

Is there a way to determine whether the dataset is raster or vector before opening them?

I could just try to open the dataset with OGR first and if it returns None I could try to open with GDAL, but I am looking for a more simple way like gdal.IsRaster(filepath) if it exists.

4
  • Could you not check the file extension before doing anything else? Jan 8, 2013 at 18:58
  • @RagiYaserBurhum is spot on. If you want a 'clean' looking line, how about wrapping your try except into a function and calling that function. Also, I believe GDAL 2.0 was/is considering a merge of gdal and ogr.
    – Jay Laura
    Jan 8, 2013 at 19:17
  • @DarrenCope - I could check the file extension, but then my script would have to know exactly which file extensions are raster and which are vector. Thats a lot of constants to include in the script.
    – Brian
    Jan 9, 2013 at 15:50
  • File extensions are meaningless. They are just indicators for humans to see. You can name files however you like without changing their contents. Aug 14, 2017 at 12:49

1 Answer 1

5

As of right now, there are two separate set of APIs for GDAL and OGR. Thus, the method you describe (try to open with OGR first and if it fails, open with GDAL) would be current way of doing it.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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