一个用于读写GeoTIFF文件的noGDAL工具
项目描述
geotiff
一个用于读取GeoTIFF文件的noGDAL工具
警告: 此包处于开发中,某些功能可能不稳定。请谨慎使用。
请支持此项目,在GitHub上给它star!
什么是noGDAL?
noGDAL 是一种在Python中开发地理空间程序而不使用GDAL的哲学。
安装
安装此包就像这样简单
pip install geotiff
还有一个基于Anaconda的包,发布在 conda-forge
conda install -c conda-forge python-geotiff
从源代码进行本地开发时,您可以使用以下方式安装geotiff及其开发需求:
git clone git@github.com:KipCrossing/geotiff.git
cd geotiff
pip install -e .[dev]
用法
创建GeoTiff对象
from geotiff import GeoTiff
tiff_file = "path/to/tiff/file"
geo_tiff = GeoTiff(tiff_file)
这将检测crs代码。如果它是“用户定义”的,并且您知道它应该是什么,您可以提供crs代码
geo_tiff = GeoTiff(tiff_file, crs_code=4326)
默认情况下,坐标将以WGS 84表示,但可以使用as_crs
参数指定。
geo_tiff = GeoTiff(tiff_file, as_crs=7844)
或者,您可以通过将as_crs
设置为None
来使用原始的crs。
geo_tiff = GeoTiff(tiff_file, as_crs=None)
如果geotiff文件有多个波段,您可以指定要使用哪个波段。
geo_tiff = GeoTiff(tiff_file, band=1)
默认波段是0。
获取geotiff的信息(属性)。
# the original crs code
geo_tiff.crs_code
# the current crs code
geo_tiff.as_crs
# the shape of the tiff
geo_tiff.tif_shape
# the bounding box in the as_crs CRS
geo_tiff.tif_bBox
# the bounding box as WGS 84
geo_tiff.tif_bBox_wgs_84
# the bounding box in the as_crs converted coordinates
geo_tiff.tif_bBox_converted
获取点/像素的坐标。
i=5
j=6
# in the as_crs coords
geo_tiff.get_coords(i, j)
# in WGS 84 coords
geo_tiff.get_wgs_84_coords(i, j)
读取数据。
要读取数据,请使用.read()
方法。这通常会返回一个Zarr数组,因为geotiff文件通常无法适应内存。
zarr_array = geo_tiff.read()
如果您确信数据可以适应内存,您可以将其转换为numpy数组。
import numpy as np
array = np.array(zarr_array)
读取大tiff文件的一部分。
在许多情况下,您可能只对tiff文件的一部分感兴趣。为了方便起见,您可以使用.read_box()
方法。这将返回一个numpy数组。
警告 如果您使用的框太大且数据无法适应内存,这将失败。
from geotiff import GeoTiff
# in WGS 84
area_box = [(138.632071411, -32.447310785), (138.644218874, -32.456979174)]
geo_tiff = GeoTiff(tiff_file)
array = geo_tiff.read_box(area_box)
注意 对于
area_box
,请使用与as_crs
相同的crs。
在某些情况下,您可能希望在area_box
的外围有一些额外的点/像素。如果您想对area_box边界附近的点进行插值,这可能很有用。要实现这一点,请使用outer_points
参数。
array = geo_tiff.read_box(area_box, outer_points=2)
这将在外围获得2个额外的点围成的area_box
。
获取边界框信息。
还有一些辅助方法可以获取结果裁剪数组的边界框。
# col and row indexes of the cut area
int_box = geo_tiff.get_int_box(area_box)
# lon and lat coords of the cut points/pixels
wgs_84_box = geo_tiff.get_bBox_wgs_84(area_box)
同样,您还可以获取直接围绕area_box
的额外n层点/像素的边界框。
# col and row indexes of the cut area
int_box = geo_tiff.get_int_box(area_box, outer_points = 2)
# lon and lat coords of the cut points/pixels
wgs_84_box = geo_tiff.get_bBox_wgs_84(area_box, outer_points = 2)
获取点/像素的坐标。
您可能希望获取数组中某个值的坐标。
i=int_box[0][0] + 5
j=int_box[0][1] + 6
geo_tiff.get_wgs_84_coords(i, j)
获取数组的坐标。
您可能只想获取数组中的所有坐标。
array = geo_tiff.read_box(area_box, outer_points=2)
lon_array, lat_array = geo_tiff.get_coord_arrays(area_box, outer_points=2)
这将返回两个与read_box()
方法返回的数组形状相同的数组。输出坐标将在as_crs
crs中。
如果您的tiff文件较小且可以适应内存,则简单地进行操作。
lon_array, lat_array = geo_tiff.get_coord_arrays()
贡献
如果您想为这个项目做出贡献,请将此仓库fork并提交带有您补丁的PR。
您可以通过在项目讨论板上说“hi”来加入对话。
为了帮助用户和其他贡献者,请确保:
- 如果适当,制作docstrings和文档块。
- 尽可能使用Python类型。
- 使用Black格式化您的代码。
注意 持续集成具有使用
mypy
进行lint检查,因此在提交PR之前请确保您自己进行检查。
项目路线图
核心功能
- 读取tiff文件(包括BigTiff)
- 写入tiff文件(包括BigTiff)
- 在epsg坐标系统之间转换
- 从tiff文件中读取用户定义的CRS
32767
- 裁剪tiff文件的一部分(边界框)
- 将数据转换为numpy数组
附加功能
- (50%) 完整测试覆盖率
- 使用mypy进行类型检查和lint检查
- 使用black格式化
- 文档:doc块
- 文档:readthedocs
项目详情
下载文件
下载适用于您平台的文件。如果您不确定选择哪个,请了解更多关于 安装包 的信息。
源代码分发
构建分发
geotiff-0.2.10.tar.gz 的散列值
算法 | 散列摘要 | |
---|---|---|
SHA256 | 36da356d3e2f6e3719bad32212283fe99049373391c7f576c2d6022642ad88d6 |
|
MD5 | 8e1bfa8ac5516404fa28fd7a74749c97 |
|
BLAKE2b-256 | ba6e00d51cd7ade2fd0e960722f090cd3bad0dbbc7206baf2871448a903e40f3 |
geotiff-0.2.10-py3-none-any.whl 的散列值
算法 | 散列摘要 | |
---|---|---|
SHA256 | 1c238b1115884a82b0e8b462384828b1bfe2819194f9f16384f517d07ab12e68 |
|
MD5 | a1341db414e5ddfeb8d3382b16e2fd1b |
|
BLAKE2b-256 | 42451f3fd283de696f421f99dc04d23251dfd10204c098684c3267935118b1cc |