Top Related Projects
Rasterio reads and writes geospatial raster datasets
Python tools for geographic data
GDAL is an open source MIT licensed translator library for raster and vector geospatial data formats.
Python interface to PROJ (cartographic projections and coordinate transformations library)
Fiona reads and writes geographic data files
Image processing in Python
Quick Overview
Rasterio is a powerful Python library for reading and writing geospatial raster data. It provides a high-level interface to the GDAL library, allowing users to work with various raster formats efficiently. Rasterio is designed to be fast, simple, and Pythonic, making it an essential tool for geospatial data processing and analysis.
Pros
- Easy-to-use and Pythonic API for working with geospatial raster data
- Excellent performance due to its use of Numpy arrays and GDAL under the hood
- Supports a wide range of raster formats and coordinate reference systems
- Well-documented with comprehensive examples and tutorials
Cons
- Requires some understanding of geospatial concepts and raster data structures
- Installation can be challenging on some systems due to GDAL dependencies
- Limited support for vector data operations (primarily focused on raster data)
Code Examples
Reading a raster file and accessing its metadata:
import rasterio
with rasterio.open('path/to/raster.tif') as src:
print(src.meta)
print(src.crs)
print(src.bounds)
Reading raster data into a Numpy array:
import rasterio
import numpy as np
with rasterio.open('path/to/raster.tif') as src:
raster_data = src.read() # Read all bands
band1_data = src.read(1) # Read only the first band
Writing a new raster file:
import rasterio
from rasterio.transform import from_origin
data = np.random.randint(0, 255, size=(1, 100, 100)).astype(np.uint8)
transform = from_origin(0, 0, 1, 1)
with rasterio.open('new_raster.tif', 'w', driver='GTiff',
height=data.shape[1], width=data.shape[2],
count=1, dtype=data.dtype,
crs='+proj=latlong', transform=transform) as dst:
dst.write(data)
Getting Started
To get started with Rasterio, first install it using pip:
pip install rasterio
Then, you can import and use Rasterio in your Python scripts:
import rasterio
import numpy as np
import matplotlib.pyplot as plt
# Open a raster file
with rasterio.open('path/to/raster.tif') as src:
# Read the first band
band1 = src.read(1)
# Plot the raster data
plt.imshow(band1, cmap='viridis')
plt.colorbar()
plt.title('Raster Data')
plt.show()
This example opens a raster file, reads the first band, and displays it using Matplotlib. Adjust the file path and customize the visualization as needed for your specific use case.
Competitor Comparisons
Rasterio reads and writes geospatial raster datasets
Pros of rasterio
- More active development and frequent updates
- Larger community and better documentation
- Wider range of supported formats and operations
Cons of rasterio
- Potentially more complex for simple use cases
- May have a steeper learning curve for beginners
Code Comparison
rasterio:
import rasterio
with rasterio.open('example.tif') as src:
data = src.read()
profile = src.profile
rasterio>:
import rasterio
with rasterio.open('example.tif') as src:
data = src.read()
profile = src.profile
The code comparison shows that both repositories use the same basic syntax for opening and reading raster files. This is because rasterio> is likely a fork or alternative version of rasterio, maintaining similar core functionality.
Given the similarity in code and the lack of significant differences between the two repositories, it's important to note that rasterio is the main, actively maintained project. The rasterio> repository may be an experimental branch or an outdated fork, and users are generally recommended to use the primary rasterio project for most geospatial data processing tasks.
Python tools for geographic data
Pros of GeoPandas
- Seamless integration with pandas for tabular data manipulation
- Built-in support for various vector data formats (e.g., Shapefile, GeoJSON)
- Easy-to-use spatial operations and analysis tools
Cons of GeoPandas
- Limited support for raster data processing
- Performance can be slower for large datasets compared to Rasterio
- Lacks some advanced geospatial functionalities present in Rasterio
Code Comparison
GeoPandas example:
import geopandas as gpd
gdf = gpd.read_file("data.shp")
gdf.plot()
gdf.to_file("output.geojson", driver="GeoJSON")
Rasterio example:
import rasterio
with rasterio.open("data.tif") as src:
data = src.read()
profile = src.profile
rasterio.plot.show(data)
Both libraries serve different purposes in geospatial data processing. GeoPandas excels in vector data handling and integration with pandas, while Rasterio specializes in raster data processing with high performance. The choice between them depends on the specific requirements of your geospatial project and the type of data you're working with.
GDAL is an open source MIT licensed translator library for raster and vector geospatial data formats.
Pros of GDAL
- Comprehensive support for a wide range of geospatial data formats
- Robust command-line utilities for data processing and conversion
- Extensive documentation and long-standing industry support
Cons of GDAL
- Steeper learning curve, especially for Python users
- More complex installation process, particularly on Windows
- Lower-level API, requiring more code for basic operations
Code Comparison
GDAL (Python bindings):
from osgeo import gdal
dataset = gdal.Open("example.tif")
band = dataset.GetRasterBand(1)
data = band.ReadAsArray()
Rasterio:
import rasterio
with rasterio.open("example.tif") as src:
data = src.read(1)
Rasterio provides a more Pythonic interface, making it easier for Python developers to work with raster data. However, GDAL offers more comprehensive functionality and supports a broader range of formats. The choice between the two often depends on the specific project requirements and the developer's familiarity with geospatial programming.
Python interface to PROJ (cartographic projections and coordinate transformations library)
Pros of pyproj
- Specialized in coordinate transformations and geodetic calculations
- Supports a wide range of projection systems and datums
- Lightweight and focused on projection-related tasks
Cons of pyproj
- Limited functionality for raster data handling
- Lacks advanced geospatial analysis capabilities
- Not designed for reading or writing geospatial file formats
Code Comparison
pyproj example:
from pyproj import Transformer
transformer = Transformer.from_crs("EPSG:4326", "EPSG:3857")
x, y = transformer.transform(51.5074, -0.1278)
rasterio example:
import rasterio
with rasterio.open('path/to/raster.tif') as src:
band1 = src.read(1)
transform = src.transform
Summary
pyproj excels in coordinate transformations and geodetic calculations, offering a wide range of projection systems. It's lightweight and focused on projection tasks. However, it lacks raster data handling capabilities and advanced geospatial analysis features.
rasterio, on the other hand, is designed for working with raster data, providing robust functionality for reading, writing, and manipulating geospatial raster files. It offers more comprehensive geospatial analysis capabilities but may be considered heavier for simple projection tasks.
Choose pyproj for projection-specific tasks and rasterio for broader raster data handling and analysis needs.
Fiona reads and writes geographic data files
Pros of Fiona
- Specializes in vector data handling, offering robust support for various vector formats
- Provides a simpler, more Pythonic API for working with vector data
- Integrates well with other geospatial libraries in the Python ecosystem
Cons of Fiona
- Limited to vector data processing, lacking support for raster operations
- May have slower performance for large datasets compared to Rasterio's optimized raster processing
Code Comparison
Fiona (vector data reading):
import fiona
with fiona.open('data.shp', 'r') as source:
for feature in source:
# Process vector features
Rasterio (raster data reading):
import rasterio
with rasterio.open('image.tif') as src:
data = src.read()
# Process raster data
Both libraries provide Pythonic interfaces for geospatial data handling, but they focus on different aspects of geospatial processing. Fiona excels in vector data manipulation, while Rasterio specializes in raster data operations. The choice between the two depends on the specific requirements of your geospatial project and the type of data you're working with.
Image processing in Python
Pros of scikit-image
- Broader scope for general image processing tasks
- Extensive collection of algorithms for various image analysis operations
- Strong integration with the scientific Python ecosystem (NumPy, SciPy, etc.)
Cons of scikit-image
- Less specialized for geospatial data and raster operations
- May have slower performance for large raster datasets
- Limited support for georeferencing and coordinate systems
Code Comparison
scikit-image:
from skimage import io, filters
image = io.imread('image.tif')
edges = filters.sobel(image)
rasterio:
import rasterio
with rasterio.open('image.tif') as src:
image = src.read()
transform = src.transform
scikit-image focuses on general image processing tasks, while rasterio specializes in geospatial raster data handling. scikit-image provides a wide range of image analysis algorithms, making it versatile for various image processing applications. However, rasterio excels in working with georeferenced data, offering better support for coordinate systems and efficient handling of large raster datasets. The code comparison illustrates the difference in approach, with scikit-image emphasizing image processing operations and rasterio focusing on geospatial data reading and metadata handling.
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual CopilotREADME
======== Rasterio
Rasterio reads and writes geospatial raster data.
.. image:: https://github.com/rasterio/rasterio/actions/workflows/tests.yaml/badge.svg :target: https://github.com/rasterio/rasterio/actions/workflows/tests.yaml
.. image:: https://github.com/rasterio/rasterio/actions/workflows/test_gdal_latest.yaml/badge.svg :target: https://github.com/rasterio/rasterio/actions/workflows/test_gdal_latest.yaml
.. image:: https://github.com/rasterio/rasterio/actions/workflows/test_gdal_tags.yaml/badge.svg :target: https://github.com/rasterio/rasterio/actions/workflows/test_gdal_tags.yaml
.. image:: https://img.shields.io/pypi/v/rasterio :target: https://pypi.org/project/rasterio/
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.
Rasterio 1.4 works with Python >= 3.9, Numpy >= 1.24, and GDAL >= 3.5. Official binary packages for Linux, macOS, and Windows with most built-in format drivers plus HDF5, netCDF, and OpenJPEG2000 are available on PyPI.
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.
.. code-block:: python
import numpy as np
import rasterio
# Read raster bands directly to Numpy arrays.
#
with rasterio.open('tests/data/RGB.byte.tif') as src:
r, g, b = src.read()
# 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. Adding other
# arrays to it in-place converts those arrays "up" and
# preserves the type of the total array.
total = np.zeros(r.shape)
for band in r, g, b:
total += band
total /= 3
# Write the product as a raster 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.
profile = src.profile
profile.update(dtype=rasterio.uint8, count=1, compress='lzw')
with rasterio.open('example-total.tif', 'w', **profile) as dst:
dst.write(total.astype(rasterio.uint8), 1)
The output:
.. image:: http://farm6.staticflickr.com/5501/11393054644_74f54484d9_z_d.jpg :width: 640 :height: 581
API Overview
Rasterio gives access to properties of a geospatial raster file.
.. code-block:: python
with rasterio.open('tests/data/RGB.byte.tif') as src:
print(src.width, src.height)
print(src.crs)
print(src.transform)
print(src.count)
print(src.indexes)
# Printed:
# (791, 718)
# {u'units': u'm', u'no_defs': True, u'ellps': u'WGS84', u'proj': u'utm', u'zone': 18}
# Affine(300.0379266750948, 0.0, 101985.0,
# 0.0, -300.041782729805, 2826915.0)
# 3
# [1, 2, 3]
A rasterio dataset also provides methods for getting read/write windows (like extended array slices) given georeferenced coordinates.
.. code-block:: python
with rasterio.open('tests/data/RGB.byte.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=791.0000000000002, height=718.0)
# (3, 718, 791)
Rasterio CLI
Rasterio's command line interface, named "rio", is documented at cli.rst <https://github.com/rasterio/rasterio/blob/master/docs/cli.rst>
__. Its rio insp
command opens the hood of any raster dataset so you can poke around
using Python.
.. code-block:: pycon
$ rio insp tests/data/RGB.byte.tif
Rasterio 0.10 Interactive Inspector (Python 3.4.1)
Type "src.meta", "src.read(1)", or "help(src)" for more information.
>>> src.name
'tests/data/RGB.byte.tif'
>>> src.closed
False
>>> src.shape
(718, 791)
>>> src.crs
{'init': 'epsg:32618'}
>>> b, g, r = src.read()
>>> b
masked_array(data =
[[-- -- -- ..., -- -- --]
[-- -- -- ..., -- -- --]
[-- -- -- ..., -- -- --]
...,
[-- -- -- ..., -- -- --]
[-- -- -- ..., -- -- --]
[-- -- -- ..., -- -- --]],
mask =
[[ True True True ..., True True True]
[ True True True ..., True True True]
[ True True True ..., True True True]
...,
[ True True True ..., True True True]
[ True True True ..., True True True]
[ True True True ..., True True True]],
fill_value = 0)
>>> np.nanmin(b), np.nanmax(b), np.nanmean(b)
(0, 255, 29.94772668847656)
Rio Plugins
Rio provides the ability to create subcommands using plugins. See
cli.rst <https://github.com/rasterio/rasterio/blob/master/docs/cli.rst#rio-plugins>
__
for more information on building plugins.
See the
plugin registry <https://github.com/rasterio/rasterio/wiki/Rio-plugin-registry>
__
for a list of available plugins.
Installation
See docs/installation.rst <docs/installation.rst>
__
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.
Please do not bring these questions to Rasterio's issue tracker, which we want to reserve for bug reports and other actionable issues.
Development and Testing
See CONTRIBUTING.rst <CONTRIBUTING.rst>
__.
Documentation
See docs/ <docs/>
__.
License
See LICENSE.txt <LICENSE.txt>
__.
Authors
The rasterio
project was begun at Mapbox and was transferred to the rasterio
Github organization in October 2021.
See AUTHORS.txt <AUTHORS.txt>
__.
Changes
See CHANGES.txt <CHANGES.txt>
__.
Who is Using Rasterio?
See here <https://libraries.io/pypi/rasterio/usage>
__.
Top Related Projects
Rasterio reads and writes geospatial raster datasets
Python tools for geographic data
GDAL is an open source MIT licensed translator library for raster and vector geospatial data formats.
Python interface to PROJ (cartographic projections and coordinate transformations library)
Fiona reads and writes geographic data files
Image processing in Python
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual Copilot