⚠️ Our 0.1 release refactored several early-development functions for long-term stability, to update your code see here. ⚠️
Future changes will come with deprecation warnings! 🙂

From/to Rasterio

From/to Rasterio#

This example demonstrates importing and exporting a rasterio.io.DatasetReader or rasterio.io.DatasetReader from and to a Raster.

import rasterio as rio

A raster can be imported from a rasterio.io.DatasetReader or rasterio.io.MemoryFile simply by instantiating Raster.

import geoutils as gu

filename_rast = gu.examples.get_path("exploradores_aster_dem")
ds = rio.DatasetReader(filename_rast)
rast = gu.Raster(ds)
rast
Raster(
  data=not_loaded; shape on disk (1, 618, 539); will load (618, 539)
  transform=| 30.00, 0.00, 627175.00|
            | 0.00,-30.00, 4852085.00|
            | 0.00, 0.00, 1.00|
  crs=EPSG:32718
  nodata=-9999.0)


The data is unloaded, as when instantiated with a filename. The data will be loaded explicitly by any function requiring its data, such as show().

rast.plot(cmap="terrain")
import raster

We can also pass a rasterio.io.MemoryFile during instantiation.

Raster(
  data=[[1271.3924560546875 1275.370849609375 1250.194091796875 ...
         1222.8572998046875 1220.2845458984375 1226.8214111328125]
        [1257.896484375 1259.9190673828125 1245.8509521484375 ...
         1224.7470703125 1221.00927734375 1228.203369140625]
        [1186.001953125 1160.00537109375 1154.0457763671875 ...
         1231.4954833984375 1227.606689453125 1236.4666748046875]
        ...
        [2690.725830078125 2695.724609375 2700.945556640625 ...
         1391.3909912109375 1411.371826171875 1436.488525390625]
        [2688.682373046875 2693.766845703125 2697.1826171875 ...
         1399.401123046875 1423.4818115234375 1451.060546875]
        [2686.294677734375 2692.3466796875 2694.712890625 ... 1413.9849853515625
         1437.3868408203125 1461.232177734375]]
  transform=| 30.00, 0.00, 627175.00|
            | 0.00,-30.00, 4852085.00|
            | 0.00, 0.00, 1.00|
  crs=EPSG:32718
  nodata=-9999.0)


The data is, as expected, already in memory.

Finally, we can export a Raster to a rasterio.io.DatasetReader of a rasterio.io.MemoryFile using to_rio_dataset

<open DatasetReader name='/vsimem/9a8abb55-22e8-4089-9818-1c18b944efaa/9a8abb55-22e8-4089-9818-1c18b944efaa.tif' mode='r'>

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

Gallery generated by Sphinx-Gallery