1

I've opened a GeoTiff with rasterio:

import rasterio
with rasterio.open('Africa.tif') as src:
    transform = src.meta['transform']
    array = src.read(1)


array([[203, 203, 203, ...,   0,   0,   0],
       [203, 203, 203, ...,   0,   0,   0],
       [203, 203, 203, ...,   0,   0,   0],
       ..., 
       [  0,   0,   0, ...,   0,   0,   0],
       [  0,   0,   0, ...,   0,   0,   0],
       [  0,   0,   0, ...,   0,   0,   0]], dtype=uint8)

Then I want to open this array data in GDAL:

gdal.Open(array,GA_ReadOnly)

but I get the following error:

---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)
<ipython-input-65-ac65027e8ea3> in <module>()
----> 1 gdal.Open(array,GA_ReadOnly)

RuntimeError: not a string

However, opening the original Tiff in GDAL works fine:

gdal.Open('Africa.tif')

<osgeo.gdal.Dataset; proxy of <Swig Object of type 'GDALDatasetShadow *' at 0x152e91450> >

I'm following the computations here: http://nbviewer.ipython.org/github/perrygeo/python-raster-stats/blob/master/docs/notebooks/Integrating%20with%20GeoPandas%20and%20Numpy.ipynb

but I want to do some masking of the input array before running the zonal computations on it (otherwise it's not necessary to open the image in rasterio at all).

Here is the link to the Tiff file I'm using, in case that's the issue: https://drive.google.com/file/d/0B7dNXVF349Jsbm5mQUt5c3JCSDA/view?usp=sharing

5
  • It says you need to put in the mode: "mode string must be one of 'r', 'r+', or 'w', I'd say either the example is incorrect and you need to tell it if you want to open it for reading/writing or the copy of rasterio is slightly broken. I've not used that particular library.. is there a reason why you want to use rasterio and not GDAL directly? Based on the examples pypi.python.org/pypi/rasterio I'd say your copy is slightly broken, download and reinstall - ensure you get the correct version for your GDAL/python. May 25 '15 at 21:34
  • I'm still having problems after reinstalling, so I'll stick with GDAL, like you suggested. Rasterio wasn't really necessary, I just wanted to try it out. May 26 '15 at 20:07
  • Another feather in the cap? why not I say! That's how I started with GDAL (and was unable to get a spatial analyst license as someone else was using it) and I don't regret trying it. You do need to try new things and in this case it hasn't worked, but you know how these things are constantly changing, I'd set a reminder to go back in a few months and try again to see if the bug has been fixed; conversely you might find that GDAL by itself addresses all your requirements. May 26 '15 at 21:40
  • 3
    For what purpose are you trying to use both rasterio and GDAL? You should use one or the other only, as they do the same thing. The error you are getting is because gdal.Open expects the filename of a raster, not a numpy array as you're passing it. Jun 30 '15 at 16:02
  • You're right about that error - it's so obvious, now that you point it out! I was just wanting to replicate the example that the rasterstats creator posted in his ipynb above, but at least with the rasterstats/rasterio versions I was using, his example doesn't work. Jun 30 '15 at 16:54

Your Answer

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

Browse other questions tagged or ask your own question.