用于 Numpy 和 SciPy 的快速直接光栅 I/O
项目描述
Rasterio 读取和写入地理空间栅格数据。
地理信息系统使用 GeoTIFF 和其他格式来组织和存储网格或栅格数据集。Rasterio 读取和写入这些格式并提供基于 ND 数组的 Python API。
Rasterio 1.3 适用于 Python 3.8+、Numpy 1.18+ 和 GDAL 3.1+。PyPI 上提供了适用于 Linux、macOS 和 Windows 的官方二进制包,其中包含大多数内置格式驱动程序以及 HDF5、netCDF 和 OpenJPEG2000。Windows 的非官方二进制包可通过其他渠道获得。
阅读文档以获取更多详细信息:https ://rasterio.readthedocs.io/ 。
例子
这是 Rasterio 提供的一些基本功能的示例。从图像中读取三个波段并对其进行平均以产生类似于全色波段的东西。然后将这个新波段写入一个新的单波段 TIFF。
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)
输出:
API 概述
Rasterio 允许访问地理空间栅格文件的属性。
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]
栅格数据集还提供了在给定地理参考坐标的情况下获取读/写窗口(如扩展数组切片)的方法。
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)
光栅 CLI
Rasterio 的命令行界面名为“rio”,记录在cli.rst中。它的rio insp命令可以打开任何栅格数据集的引擎盖,这样您就可以使用 Python 进行探索。
$ 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 提供了使用插件创建子命令的能力。有关构建插件的更多信息,请参阅 cli.rst 。
请参阅 插件注册表 以获取可用插件的列表。
安装
请在虚拟环境中安装 Rasterio,这样它的要求就不会篡改您系统的 Python。
SSL 证书
PyPI 上的 Linux 轮子是基于 CentOS 构建的,libcurl 期望证书位于 /etc/pki/tls/certs/ca-bundle.crt 中。例如,Ubuntu 的证书位于不同的位置。您可能需要使用 CURL_CA_BUNDLE 环境变量来指定 SSL 证书在您的计算机上的位置。在 Ubuntu 系统上,如下所示设置变量。
$ export CURL_CA_BUNDLE=/etc/ssl/certs/ca-certificates.crt
依赖项
Rasterio 有一个 C 库依赖项:GDAL >= 3.1。GDAL 本身依赖于大多数主要操作系统提供的其他一些库,并且还依赖于非标准的 GEOS 和 PROJ 库。下面将解释如何满足这些要求。
Rasterio 的 Python 依赖项是(请参阅包元数据文件):
affine
attrs
certifi
click>=4.0
cligj>=0.5
numpy>=1.18
snuggs>=1.4.1
click-plugins
setuptools
[all]
hypothesis
pytest-cov>=2.2.0
matplotlib
boto3>=1.2.4
numpydoc
pytest>=2.8.2
shapely
ipython>=2.0
sphinx
packaging
ghp-import
sphinx-rtd-theme
[docs]
ghp-import
numpydoc
sphinx
sphinx-rtd-theme
[ipython]
ipython>=2.0
[plot]
matplotlib
[s3]
boto3>=1.2.4
[test]
boto3>=1.2.4
hypothesis
packaging
pytest-cov>=2.2.0
pytest>=2.8.2
shapely
开发需要 Cython 和其他软件包。
二进制分布
如果可能,请使用直接或间接提供 GDAL 的二进制发行版。
PyPI 上的光栅轮包括 GDAL 和它自己的依赖项。
拉斯特里奥 |
GDAL |
---|---|
1.2.3 |
3.2.2 |
1.2.4+ |
3.3.0 |
Linux
Rasterio 发行版可从 UbuntuGIS 和 Anaconda 的 conda-forge 频道获得。
操作系统
包含 GDAL、GEOS 和 PROJ4 库的二进制发行版可用于 OS X 版本 10.9+。要安装,请运行pip install rasterio。这些二进制轮子是较新版本的 pip 的首选。
如果您不想要这些轮子并想从源代码分发版安装,请改为运行pip install rasterio --no-binary rasterio。
包含的 GDAL 库相当小,仅提供 GDAL 附带的格式驱动程序,并且默认启用。要访问更多格式,您必须从源代码分发中构建(见下文)。
视窗
rasterio 和 GDAL 的二进制轮子由 Christoph Gohlke 创建,可从他的网站获得。
要安装 rasterio,只需为您的系统下载两个二进制文件(rasterio和GDAL)并从下载文件夹中运行类似的东西,调整您的 Python 版本。
$ pip install -U pip
$ pip install GDAL-3.1.4-cp39-cp39‑win_amd64.whl
$ pip install rasterio‑1.1.8-cp39-cp39-win_amd64.whl
您还可以使用 Anaconda 的 conda-forge 频道安装带有 conda 的 rasterio。
$ conda install -c conda-forge rasterio
源分布
Rasterio 是一个 Python C 扩展,要构建你需要一个工作编译器(OS X 上的 XCode 等)。您还需要预装 Numpy;运行 rasterio 设置脚本需要 Numpy 标头。必须先安装 Numpy(通过指示的需求文件),然后才能安装 rasterio。有关更多指导,请参阅 rasterio 的 Travis配置。
Linux
以下命令改编自 Rasterio 的 Travis-CI 配置。
$ sudo add-apt-repository ppa:ubuntugis/ppa
$ sudo apt-get update
$ sudo apt-get install gdal-bin libgdal-dev
$ pip install -U pip
$ pip install rasterio
根据您的 Linux 系统的需要调整它们。
操作系统
对于基于 Homebrew 的 Python 环境,请执行以下操作。
$ brew update
$ brew install gdal
$ pip install -U pip
$ pip install --no-binary rasterio
视窗
你可以从这里下载 GDAL 的二进制发行版。您还需要下载已编译的库和头文件(包含文件)。
在 Windows 上从源代码构建时,重要的是要知道 setup.py 不能依赖仅存在于 UNIX 系统上的 gdal-config 来发现 rasterio 编译其 C 扩展所需的头文件和库的位置。在 Windows 上,这些路径需要由用户提供。您将需要找到 gdal 的包含文件和库文件并使用 setup.py,如下所示。您还需要通过 GDAL_VERSION 环境变量指定安装的 gdal 版本。
$ python setup.py build_ext -I<path to gdal include files> -lgdal_i -L<path to gdal library> install
带点
$ pip install --no-use-pep517 --global-option -I<path to gdal include files> -lgdal_i -L<path to gdal library> .
注意:--no-use-pep517
是必需的,因为 pip 目前尚未实现在使用 PEP 517 时将可选参数传递给构建后端的方法。有关更多详细信息,请参见此处。
或者,MSVC 编译器使用的环境变量(例如 INCLUDE 和 LINK)可用于指向包含目录和库文件。
我们已经成功地使用用于编译 Python 目标版本的 Microsoft Visual Studio 的相同版本编译代码(有关此处使用的版本的更多信息。)。
注意:GDAL DLL 和 gdal-data 目录需要在您的 Windows PATH 中,否则 rasterio 将无法工作。
支持
有关 Rasterio 安装和使用问题的主要论坛是 https://rasterio.groups.io/g/main。作者和其他用户将在他们有专业知识可以分享并有时间解释时回答问题。请花时间提出一个明确的问题,并对回答保持耐心。
请不要将这些问题带到 Rasterio 的问题跟踪器,我们希望将其保留用于错误报告和其他可操作的问题。
开发和测试
请参阅CONTRIBUTING.rst。
文档
请参阅docs/。
执照
请参阅LICENSE.txt。
变化
请参阅CHANGES.txt。
谁在使用 Rasterio?
见这里。