Rasterio's Github page

Keywords: Python github

https://github.com/mapbox/rasterio

Rasterio

Rasterio reads and writes geospatial raster data.
Rasterio reads and writes geospatial grid data.

Geographic information systems use GeoTIFF and other formats to organize and store gridded, or raster, datasets.
Rasterio reads and writes these formats and provides a Python API based on N-D arrays.
Geographic information systems use GeoTIFF and other formats to organize and store grid or raster data. Rasterio reads and writes these formats and provides pyhton API based on N-D arrays.

Rasterio 1.2 works with Python versions 3.6 through 3.9, Numpy versions 1.15 and newer, and GDAL versions 2.4 through 3.3.
Official binary packages for Linux and Mac OSX with most built-in format drivers plus HDF5, netCDF, and OpenJPEG2000 are available
on PyPI. Unofficial binary packages for Windows are available through other channels.
Rasterio 1.2 applies to Python versions 3.6 to 3.9, Numpy version 1.15 to newer versions, and GDAL versions 2.4 to 3.3.
Official binary packages for linux and Mac OS X (with most of the built-in formats drivers and HDF5, netCDF, and OpenJPEG2000) are available in PyPI.
Unofficial binary packages for Windows can be obtained from other channels.

Read the documentation for more details: https://rasterio.readthedocs.io/.
Read the documentation for more details: https://rasterio.readthedocs.io/.

Example

Here's an example of some basic features that Rasterio provides. Three bands are read from an image and averaged to produce something like a panchromatic band. This new band is then written to a new single band TIFF.
Here is an example of some basic functions provided by Rasterio. Read three bands from an image and average them to produce something like a panchromatic band. This new band is then written as a new single band TIFF.

import numpy as np
import rasterio
# Read raster bands directly to Numpy arrays.
# Directly read the grid band as Numpy array
with rasterio.open('./imgs/T10TGS.tif') as src:
    r, g, b = src.read()  # Read single band 1d array
    # r = src.read()  # Read 3-band 3d array
    profile = src.profile
    
print(r.shape, g.shape, b.shape) 
# (9950, 10980) (9950, 10980) (9950, 10980)
print(r.dtype, g.dtype, b.dtype)
# uint16 uint16 uint16

# Combine arrays in place. Expecting that the sum will temporarily exceed the 8-bit integer range, 
# initialize it as a 64-bit float (the numpy default) array. 
# Combine arrays. It is expected that: and will exceed the 8-bit integer range. Initialize it to 64 bit float (numpy default) array.
# Adding other arrays to it in-place converts those arrays "up" and preserves the type of the total array.
# Add other arrays to it, convert those arrays"up", and save the type of array.

total = np.zeros(r.shape)

for band in r, g, b:
    total += band
print(total.shape) # (9950, 10980)
print(total.dtype) #  float64

total /= 3
print(total.shape) # (9950, 10980)
print(total.dtype) #  float64

# Write the product as a raster band to a new 8-bit file. 
# Write the product as a grid band to a new 8-bit file.
# For the new file's profile, we start with the meta attributes of the source file, 
# but then change the band count to 1, set the dtype to uint8, and specify LZW compression.
# For the profile configuration of the new file, we start with the meta attribute of the source file, but then change the number of bands to 1, set dtype to uint8, and specify LZW compression.

# profile.update(dtype=rasterio.uint8, count=1) # No compression
profile.update(dtype=rasterio.uint8, count=1, compress='lzw') # lzw compression
# LZW compression is a lossless compression method that compresses files into small files based on table query algorithm. Two common file formats used by LZW compression are GIF image format and TIFF image format for websites.

with rasterio.open('./imgs/T10TGS-lzw.tif', 'w', **profile) as dst:
    dst.write(r)

# Image file size here is mainly related to two factors: whether the storage is compressed and the data type.

# Whether the storage is compressed: compress='lzw '. However, the compressed file is not necessarily smaller than that before compression. Compression is usually selected to reduce the file size.
# Arcgis provides a variety of compression methods (including lzw), including lossless compression and lossy compression. JPEG format itself is a compression type.
# See: https://desktop.arcgis.com/zh-cn/arcmap/10.3/manage-data/raster-and-images/raster-compression.htm
# Arcgis suggests: however, in some cases, raster data can be stored without compression; However, compression is usually recommended. If you are not sure which compression method to use, at least the default LZ77 (lossless) should be selected.
# Select lzw compression in rasterio.
# Only GIF and TIFF file formats are original files, and others are compressed files.
# PNG is the most common compression format, but it can not be used for geographic grid data. See: https://zhuanlan.zhihu.com/p/19570424

# Data types: uint8, uint16 and float64 are getting larger and larger. There is uint1 in arcgis (only 0 and 1), and the file is the smallest.

API Overview

Rasterio gives access to properties of a geospatial raster file.
Rasterio can get the properties of geogrid data.

with rasterio.open('./imgs/T10TGS.tif') as src:
    print(src.width, src.height)
    print(src.bounds)
    print(src.crs)
    print(src.transform)
    print(src.count)
    print(src.indexes)

# Printed:
# 10980 9950
# BoundingBox(left=699960.0, bottom=5100520.0, right=809760.0, top=5200020.0)
# EPSG:32610
# | 10.00, 0.00, 699960.00|
# | 0.00,-10.00, 5200020.00|
# | 0.00, 0.00, 1.00|
# 3
# (1, 2, 3)

A rasterio dataset also provides methods for getting read/write windows (like extended array slices) given georeferenced coordinates.
rasterio dataset also provides a method to obtain read / write windows (such as extended array slices) in a given geo reference coordinate.

with rasterio.open('./imgs/T10TGS.tif') as src:
    window = src.window(*src.bounds)
    print(window)
    print(src.read(window=window).shape)

# Printed:
# Window(col_off=0.0, row_off=0.0, width=10980.0, height=9950.0)
# (3, 9950, 10980)

Rasterio CLI(command line interface)

Rasterio's command line interface, named "rio", is documented at cli.rst. Its rio insp command opens the hood of any raster dataset so you can poke around using Python.
Rasterio's command line interface is called "rio" and is recorded in cli.rst. Its rio insp command opens the hood of any raster dataset so that you can use Python.

(Geo) H:\Resource_Learning>rio insp Rasterio_MY\imgs\T10TGS.tif
Rasterio 1.2.6 Interactive Inspector (Python 3.7.6)
Type "src.meta", "src.read(1)", or "help(src)" for more information.
>>> src.name
'H:/Resource_Learning/Rasterio_MY/imgs/T10TGS.tif'
>>> src.closed
False
>>> src.shape
(9950, 10980)
>>> src.crs
CRS.from_epsg(32610)
>>> b, g, r = src.read()
>>> b
array([[1932, 1822, 1764, ...,  809,  874,  830],
       [1804, 1714, 1694, ...,  766,  791,  745],
       [1604, 1740, 1780, ...,  742,  708,  673],
       ...,
       [ 371,  335,  316, ...,  140,  161,  149],
       [ 309,  357,  451, ...,  189,  121,  149],
       [   0,    0,    0, ...,    0,    0,    0]], dtype=uint16)
>>>  np.nanmin(b), np.nanmax(b), np.nanmean(b)
  File "<console>", line 1
    np.nanmin(b), np.nanmax(b), np.nanmean(b)
    ^
IndentationError: unexpected indent
>>> np.nanmin(b), np.nanmax(b), np.nanmean(b)
(0, 17632, 715.7098386101729)

Rio Plugins plug-in

Rio provides the ability to create subcommands using plugins. See cli.rst for more information on building plugins.
Rio provides the ability to create subcommands using plug-ins. For more information about building plug-ins, see cli.rst..
See the plugin registry for a list of available plugins.
For a list of available plug-ins, see the plug-in registry.

Installation

ellipsis

Support

The primary forum for questions about installation and usage of Rasterio is https://rasterio.groups.io/g/main. The authors and other users will answer questions when they have expertise to share and time to explain. Please take the time to craft a clear question and be patient about responses.
Main forums on Rasterio installation and use: https://rasterio.groups.io/g/main . When authors and other users have expertise to share and have time to explain, they will answer questions. Please take the time to ask a clear question and be patient with the answer.

Please do not bring these questions to Rasterio's issue tracker, which we want to reserve for bug reports and other actionable issues.
Please do not bring these problems to Rasterio's problem tracker. We want to keep these problems for bug reports and other operable problems.

While Rasterio's repo is in the Mapbox GitHub organization, Mapbox's Support team is focused on customer support for its commercial platform and Rasterio support requests may be perfunctorily closed with or without a link to https://rasterio.groups.io/g/main. It's better to bring questions directly to the main Rasterio group at groups.io.
Although Rasterio's repo is located in the Mapbox GitHub organization, Mapbox's support team focuses on customer support for its business platform, and Rasterio support requests may or may not be linked to https://rasterio.groups.io/g/main The case was perfunctorily closed. It is best to take the problem directly to the main Rasterio group on groups.io.

Posted by djddb on Sat, 11 Sep 2021 17:26:23 -0700