Open/save a raster

Open/save a raster#

This example demonstrates the instantiation of a raster through Raster and saving with save().

We open an example raster. The data is, by default, unloaded.

import geoutils as gu

filename_rast = gu.examples.get_path("everest_landsat_b4")
rast = gu.Raster(filename_rast)
rast
Raster(
  data=not_loaded; shape on disk (1, 655, 800); will load (655, 800)
  transform=| 30.00, 0.00, 478000.00|
            | 0.00,-30.00, 3108140.00|
            | 0.00, 0.00, 1.00|
  crs=EPSG:32645
  nodata=None)


A raster is composed of four main attributes: a data array, an affine transform, a coordinate reference system crs and a nodata value. All other attributes are derivatives of those or the file on disk, and can be found in the dedicated section of the API. See also The georeferenced raster (Raster).

Note

A raster can also be instantiated with a rasterio.io.DatasetReader or a rasterio.io.MemoryFile, see From/to Rasterio.

We can print more info on the raster.

Driver:               GTiff
Opened from file:     /home/docs/checkouts/readthedocs.org/user_builds/geoutils/checkouts/stable/examples/data/Everest_Landsat/LE71400412000304SGS00_B4.tif
Filename:             /home/docs/checkouts/readthedocs.org/user_builds/geoutils/checkouts/stable/examples/data/Everest_Landsat/LE71400412000304SGS00_B4.tif
Loaded?               False
Modified since load?  False
Grid size:            800, 655
Number of bands:      1
Data types:           uint8
Coordinate system:    ['EPSG:32645']
Nodata value:         None
Pixel interpretation: Point
Pixel size:           30.0, 30.0
Upper left corner:    478000.0, 3088490.0
Lower right corner:   502000.0, 3108140.0

The data will be loaded explicitly by any function requiring its data, such as show().

rast.plot(cmap="Greys_r")
read raster

Opening can be performed with several parameters, for instance choosing a single band with index and re-sampling with downsample, to subset a 3-band raster to its second band, and using 1 pixel out of 4.

rast = gu.Raster(gu.examples.get_path("everest_landsat_rgb"), bands=2, downsample=4)
rast
Raster(
  data=not_loaded; shape on disk (3, 655, 800); will load (164, 200)
  transform=| 120.00, 0.00, 478000.00|
            | 0.00,-120.00, 3108140.00|
            | 0.00, 0.00, 1.00|
  crs=EPSG:32645
  nodata=None)


The data is not loaded by default, even if when specifying a band or re-sampling. We can load it explicitly by calling load() (could have also passed load_data=True to Raster).

Raster(
  data=[[255 255 255 ... 255 255 255]
        [255 255 255 ... 255 255 255]
        [255 255 255 ... 255 255 255]
        ...
        [ 38  66  92 ... 114 147 157]
        [ 86  54  29 ... 122 142 171]
        [ 94  35  35 ... 150 151 223]]
  transform=| 120.00, 0.00, 478000.00|
            | 0.00,-120.00, 3108140.00|
            | 0.00, 0.00, 1.00|
  crs=EPSG:32645
  nodata=None)


Finally, a raster is saved using save():

rast.save("myraster.tif")

Total running time of the script: (0 minutes 2.001 seconds)

Gallery generated by Sphinx-Gallery