带有python便利性的gdal包装器
greenwich的Python项目详细描述
为GDAL的奇妙世界添加python便利。
格林威治为gdal-swig-python绑定提供了一个包装器。这里的焦点是 主要向gdal/ogr围栏的光栅侧提供一些更高级别的行为。
安装
gdal python绑定需要一点帮助来找到相关的 从virtualenv内部构建时的头文件。如果通常的pip install greenwich 如果失败,请指定gdal头目录,无论该目录位于何处,例如:
CFLAGS=-I/usr/include/gdal pip install greenwich
基本用法
打开您周围的任何光栅数据集,可能是来自 WorldClim。
fromgreenwichimportGeometry,Rasterfromgreenwich.ioimportMemFileIOwithRaster('cc85tn701.tif')astmax:# Save as a NetCDF file.tmax.save('cc85tn701.nc')geom=Geometry(wkt='POLYGON((-123 47,-123 48,-122 49,-121 48,-121 47,-123 47))',srs=4326)# Clip the raster with a geometry and save the result as a GeoTIFF.withtmax.clip(geom)asclipped:clipped.save('clipped.tif')# Return a NumPy MaskedArray using nodata values for a given bounding box.m=tmax.masked_array((-120,38,-118,44))# Convert to an Erdas Imagine file in memory.imgio=MemFileIO(suffix='.img')tmax.save(imgio)imgdata=imgio.read()imgio.close()# Iterate over bands and retrieve the maximum pixel values.maxvals=[band.GetMaximum()forbandintmax]
通过将区段提供为最小/最大x,y坐标的4元组来检索特定区域的numpy数组:
arr = tmax.array((-120, 38, -118, 44))
将光栅重新投影到另一个坐标系。你可以通过EPSG密码,WKT, proj4格式化的投影,或空间引用实例作为参数:
warped = tmax.warp(3857)
也许您希望将图像重新采样到新的分辨率,该分辨率可以是 使用:
resampled = tmax.resample((100, 100))
光栅实例的行为仍然像gdal.dataset:
meta = tmax.GetMetadata()