Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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.

share|improve this question
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
up vote 3 down vote accepted

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.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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