Open/save a raster

Open/save a raster#

This example demonstrates opening a raster with open_raster() and saving it with the rst accessor.

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.open_raster(filename_rast)
rast
<xarray.DataArray (y: 655, x: 800)> Size: 2MB
[524000 values with dtype=float32]
Coordinates:
  * y            (y) float64 5kB 3.108e+06 3.108e+06 ... 3.089e+06 3.089e+06
  * x            (x) float64 6kB 4.78e+05 4.78e+05 ... 5.02e+05 5.02e+05
    band         int64 8B 1
    spatial_ref  int64 8B 0
Attributes:
    AREA_OR_POINT:             Point
    scale_factor:              1.0
    add_offset:                0.0
    overview_factors:          []
    overview_selected_level:   -1
    overview_selected_factor:  1
    block_shape:               (10, 800)


A raster is composed of four main attributes: a data array, an affine transform, a coordinate reference system 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.

Note

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

We can print more info on the raster.

Driver:               GTiff
Filename:             /home/docs/checkouts/readthedocs.org/user_builds/geoutils/checkouts/latest/geoutils/example_data/Everest_Landsat/LE71400412000304SGS00_B4.tif
Loaded?               False
Grid size:            800, 655
Number of bands:      1
Data types:           float32
Coordinate system:    ['WGS 84 / UTM zone 45N']
Nodata value:         None
Pixel interpretation: Point
Pixel size:           30.0, 30.0
Upper left corner:    478000.0, 3108140.0
Lower right corner:   502000.0, 3088490.0

The data will be loaded explicitly by any function requiring its values, such as plotting.

rast.rst.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.open_raster(gu.examples.get_path("everest_landsat_rgb"), downsample=4).sel(band=2)
rast
<xarray.DataArray (y: 163, x: 200)> Size: 130kB
[32600 values with dtype=float32]
Coordinates:
  * y            (y) float64 1kB 3.108e+06 3.108e+06 ... 3.089e+06 3.089e+06
  * x            (x) float64 2kB 4.781e+05 4.782e+05 ... 5.018e+05 5.019e+05
    band         int64 8B 2
    spatial_ref  int64 8B 0
Attributes:
    scale_factor:              1.0
    add_offset:                0.0
    overview_factors:          []
    overview_selected_level:   -1
    overview_selected_factor:  1
    block_shape:               (3, 800)


The data is not loaded by default, even if when specifying a band or re-sampling. We can load it explicitly by calling load().

<xarray.DataArray (y: 163, x: 200)> Size: 130kB
array([[255., 255., 255., ..., 255., 255., 255.],
       [255., 255., 255., ..., 255., 255., 255.],
       [255., 255., 255., ..., 255., 255., 255.],
       ...,
       [ 45.,  67.,  52., ..., 121., 150., 164.],
       [ 54.,  67.,  93., ..., 120., 145., 161.],
       [ 86.,  37.,  32., ..., 121., 142., 167.]],
      shape=(163, 200), dtype=float32)
Coordinates:
  * y            (y) float64 1kB 3.108e+06 3.108e+06 ... 3.089e+06 3.089e+06
  * x            (x) float64 2kB 4.781e+05 4.782e+05 ... 5.018e+05 5.019e+05
    band         int64 8B 2
    spatial_ref  int64 8B 0
Attributes:
    scale_factor:              1.0
    add_offset:                0.0
    overview_factors:          []
    overview_selected_level:   -1
    overview_selected_factor:  1
    block_shape:               (3, 800)


Finally, a raster is saved using to_file():

rast.rst.to_file("myraster.tif")
/home/docs/checkouts/readthedocs.org/user_builds/geoutils/conda/latest/lib/python3.14/site-packages/sphinx_gallery/gen_rst.py:1053: SerializationWarning: saving variable None with floating point data as an integer dtype without any _FillValue to use for NaNs
  mem_last, _ = call_memory(

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

Gallery generated by Sphinx-Gallery