1

I'm trying to read the raster (tif) file using Python. What I want is to read the raster at a specific request size rather than its original resolution.

Example: If a raster has a cellsize of 5 x 5, I would want to read the raster at a lower resolution of suppose 50 x 50 which would reduce the total number of cols and rows. Basically read the pyramid of the raster which has the closet number cols to that of the request size.

Is there a way to read the raster at a particular request size specified through the ArcPy module or gdal module?

  • 1
    Could you please give an example of a "particular request size"? – Aaron Jun 1 '15 at 13:50
  • overviews (or pyramids, with extension .ovr) are normally a (paged) tif image. You can even open them with InfraView if you wish.Each page represents a level. Knowing that i think it shouldn't be difficult to find an appropriate python module to read your target pixel values. – nickves Jun 2 '15 at 5:53
  • The overviews (or pyramids in my case) are not built for the rasters that I intend to read. All the rasters are scanned maps whose spatial references are not known. I'm not sure how will the above method work – rsumbaly Jun 2 '15 at 6:31
1

I believe you're after re-sampling a raster on the fly which I don't believe is possible (though I'd be happy to be proved wrong).

You'll have to create a new dataset at the required sample level. In ArcGIS you can do this using the Resample tool (which you can add to a python script). This does create a new file on your disk which depending on the size of your raster can take up a lot of unnecessary room, especially if you have to do this many times for many rasters.

Happily given that you have GDAL installed you can use the GDAL Virtual Raster Format (.vrt) as your intermediate file. This is an XML file that describes a new raster by pointing to an existing file (or set of files) on disk. This can then be read using the GDAL Python bindings (or better yet, rasterio - a much nicer Python GDAL wrapper). The easiest way to create this VRT is to use the gdal_translate command line utility:

gdal_translate -outsize x y -r nearest -of VRT in_raster.tif out_vrt.vrt

x and y are either a percentage of the original resolution (e.g. 50% 50% gives a cell twice the width and twice the length of the input), or the number of columns and the number of rows. The resample method (-r) is nearest neighbour by default, but can be any one of: nearest, bilinear, cubic, cubicspline, lanczos, average, mode.

The VRT can now be read in Python for analysis using your method of choice;

import rasterio

with rasterio.open("out_vrt.vrt") as src:
    data = src.read(1)
    # analysis here

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.