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.

  • Could you not check the file extension before doing anything else? – Darren Cope Jan 8 '13 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 '13 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 '13 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. – bugmenot123 Aug 14 '17 at 12:49

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.

| improve this answer | |

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.