大家可能听过GDAL库,其实rasterio是基于GDAL库二次封装的,更加符合Python风格的主要用于空间栅格数据处理的Python库。所以本文就简单介绍下rasterio的安装和使用。...rasterio安装 这个第三方库不是很好安装,尝试了大半天也没安装上。rasterio依赖很多第三方库,所以比较麻烦,我们按下面的顺序依次安装即可。...rasterio 这里有两点需要注意。...rasterio使用 其实我用Python来操作影像就两个要求,一是能读取到各个影像波段的数据,二是经过一些处理后,能再将数据存为影像。...import rasterio data = rasterio.open('yingxiang.tif') print(data.count) 7 当然还有很多其他的影像属性。
在rasterio中提供了calculate_default_transform,可以直接计算目标系统中的放射变换参数和图像尺寸。 这样我们直接根据计算的结果更新目标文件的元信息即可。...代码实现 import numpy as np import rasterio from rasterio.warp import calculate_default_transform, reproject..., Resampling from rasterio import crs src_img = 'example.tif' dst_img = 'reproject.tif' # 转为地理坐标系WGS84...dst_crs = crs.CRS.from_epsg('4326') with rasterio.open(src_img) as src_ds: profile = src_ds.profile...width': dst_width, 'height': dst_height, 'nodata': 0 }) # 重投影并写入数据 with rasterio.open
方法描述 使用Rasterio创建并写入栅格数据比GDAL还简单一些,基本使用到两个函数: rasterio.open() write() 在open()函数当中,我们可以像GDAL中的Create(...)方法一样,设置数据类型,数据尺寸,投影定义,仿射变换参数等一系列信息 另外,Rasterio中的数据集提供了一个profile属性,通过该属性可以获取这些信息的集合,这样我们读取源数据文件的时候获得该属性...但是Rasterio比CreateCopy()更为强大的地方是:你可以修改profile以适配你的目标文件,而CreateCopy()通过提供的原型文件进行创建,无法直接对这些元信息进行修改。...import rasterio import numpy as np # 读入的数据是绿,红,近红外波段的合成数据 with rasterio.open('LC08_122043_20161207.tif...()函数中依次列出所有的参数 with rasterio.open('NDVI.tif', mode='w', driver='GTiff', width
Rasterio简介 有没有觉得用GDAL的Python绑定书写的代码很不Pythonic,强迫症的你可能有些忍受不了。不过,没关系,MapBox旗下的开源库Rasterio帮我们解决了这个痛点。...Rasterio是基于GDAL库二次封装的更加符合Python风格的主要用于空间栅格数据处理的Python库。...Rasterio中栅格数据模型基本和GDAL类似,需要注意的是: 在Rasterio 1.0以后,对于GeoTransform的表示弃用了GDAL风格的放射变换,而使用了Python放射变换的第三方库affine...栅格数据读取代码示例 下面的示例程序中演示了如何读取一个GeoTIFF文件并获取相关信息,需要注意的是: rasterio使用rasterio.open()函数打开一个栅格文件 rasterio使用read...with rasterio.open('example.tif') as ds: print('该栅格数据的基本数据集信息(这些信息都是以数据集属性的形式表示的):') print(
影响格式为二值图像 #import rasterio and sklearn import rasterio from rasterio.warp import reproject, Resampling...calculate_default_transform from sklearn.metrics import jaccard_score #read the first raster image with rasterio.open...tif") as src1: raster1 = src1.read(1) profile1 = src1.profile #read the second raster image with rasterio.open...read(1) profile2 = src2.profile #resample the first raster to 100m resolution resampled_raster1 = rasterio.open...height, *resampled_raster1.bounds) #reproject the first raster to WGS-84 reprojected_raster1 = rasterio.open
所以本次的代码任然有优化和改进的空间,但是感觉在hdf转tif这部中rasterio的效率比gdal高多了 import gdal, osr import numpy as np import os import...rasterio from rasterio.warp import calculate_default_transform, reproject, Resampling from rasterio...#10,5-10都是赋值给新数据 outDataRaster.FlushCache()#把缓存清理,很重要 del outDataRaster #投影转换,这部分既可以用rasterio...dst_ds.write(dst_array, i) 2NDVI: import gdal, osr import numpy as np import os import rasterio...from rasterio.warp import calculate_default_transform, reproject, Resampling from rasterio import crs
3.1 读取影像 采用rasterio进行影像读取。...代码如下: import rasterio as rio band = rio.open(path) 非常简单,只要传入影像的路径即可。...rasterio支持tif、hdf格式(亲测)。 3.2 投影转换 这是比较难的一块,需要注意很多细节。...首先将shp数据转换为geojson,然后使用rasterio进行切割。...__geo_interface__] 其中i表示的是取出元素的序号,最后都要采用[]将结果变成数组,因为rasterio最后需要传入一个数组参数。
3.图像读写 GDAL读写图像是通过RasterIO()这个函数实现的,这个函数提供了非常强大的功能,目前笔者也只总结了这以下方面的内容。...= (size_t) bufWidth * bufHeight * bandNum * depth; GByte *imgBuf = new GByte[imgBufNum]; //读取 img->RasterIO...最后得到的dst.tif如下: 3.2.16位影像读写 上述RasterIO()的写法可以兼容16为图像的读写,只不过要注意的是buf中是用2个Gbyte来表达1个16像素值的。...3.5.重采样读写 RasterIO()另外一个用法是可以自动缩放,重采样读写影像,例如这里将512X512大小的lena图像重采样成256X256大小: //申请buf size_t imgBufNum...查阅网上资料得知,RasterIO()重采样方式默认是最临近的方法,只有建立金字塔时可以设置重采样方式,但也仅限于缩小。
此外,Python 有许多库可以使使用 Sentinel 2 图像变得更加容易,包括rasterio、geopandas和matplotlib。...下面是一个示例 Python 代码,用于打开 Sentinel-2 光栅文件并使用以下方法绘制假彩色合成图rasterio: import rasterio from rasterio.plot import...然后可以使用模块show中的函数显示生成的图像rasterio.plot。 一旦我们将 Sentinel 2 图像加载到 Python 中,我们就可以开始从中提取信息。...rasterio以下是使用 计算 `Sentinel-2` 栅格文件的归一化差异植被指数 (NDVI)并绘制结果的示例 Python 代码: 代码如下: import rasterio import...numpy as np from rasterio.plot import show # Open the Sentinel-2 raster file with rasterio.open('path
之前我们讲了怎么读取单张栅格,读取完之后是一个numpy的ndarray,那么只要进行相应的矩阵拼接即可: 导入包 import rasterio import scipy.stats as ss...import numpy as np import glob from rasterio.plot import show 读取第一张图的元数据,方便最后写出结果 with rasterio.open...profile = src.profile data = src.read() 组成时空立方体 这里组成栅格立方体也非常简单,用numpy的concatenate函数拼接一下即可: ds = [rasterio.open...p_value 应用函数 slope_rs,r2,pv = np.apply_along_axis(rs_slope, 0,arr=ds) 保存结果 profile['nodata'] = 0 with rasterio.open...('slope.tif','w',**profile) as dest: dest.write(slope_rs,1) 查看结果 with rasterio.open('.
append channels 空间数据处理Python库的安装 常用的空间数据处理Python库 GDAL 全能型的基础空间数据处理库 fiona 基于GDAL的空间矢量数据处理库 rasterio...输入命令,进入虚拟环境 安装GDAL库 conda install -c conda-forge gdal 安装fiona库 conda install -c conda-forge fiona 安装rasterio...库 conda install -c conda-forge rasterio 使用pip进行库的安装 什么是pip呢?...‑2.2.4‑cp37‑cp37m‑win_amd64.whl 安装fiona库 pip install Fiona‑1.7.11.post1‑cp37‑cp37m‑win_amd64.whl 安装rasterio...库 pip install rasterio‑1.0a12‑cp37‑cp37m‑win_amd64.whl 安装Jupyter pip install jupyter 使用Jupyter Notebook
3K.A2018001.0320.061.2018003202214.hdf') 代码如下: import gdal, osr import numpy as np import os import rasterio...from rasterio.warp import calculate_default_transform, reproject, Resampling from rasterio import crs
# 调用dde模型库中的Project Raster模型 import numpy as np from rasterio.warp import calculate_default_transform..., reproject, Resampling from rasterio import crs import rasterio def pydde_rasterProject_run(src_file..., EPSG_Code, tag_file): tag_crs = crs.CRS.from_epsg(EPSG_Code) with rasterio.open(src_file)...=0) # Ignore nodata # 读取山地阴影图 hs = rxr.open_rasterio(outHS) hs = hs[0] hs = hs.where(hs.values!...max_depth=None, callback=my_callback ) outFill = temp_dir + "Fill.tif" fill_image = rxr.open_rasterio
为了处理光栅数据,我使用了rasterio库。...# import libraries import os import rasterio import matplotlib.pyplot as plt import numpy as np import...将首先使用rasterio加载一个随机图像,然后使用matplotlib功能绘制它。 # load in raster data rf = rasterio.open('....[] # loop through each raster for file in os.listdir(tsFolderPath): # read the files rf = rasterio.open
我们需要首先定义一个包含多个 TIF 文件路径的列表,并使用 rioxarray.open_rasterio 函数打开这些文件,得到相应的 xarray 数据集。...RainStormChina/RainStormChina/2019/StormLevel2019.tif'] 读取文件 In [13]: import rioxarray dataset = rioxarray.open_rasterio...2019-01-01', freq='AS') for file in file_list: for i in time_coord: xds = rioxarray.open_rasterio...神奇海螺说zip的魔术可以帮忙 xds_list1=[] for file, time in zip(file_list, time_coord): xds = rioxarray.open_rasterio
as rio from rasterio.plot import plotting_extent import geopandas as gpd import earthpy as et import...Stack 函数还返回两个对象,一个数组和一个 RasterIO 配置文件。做 肯定会在变量中同时捕获。...该功能还 需要 Rasterio 对象的空间变换,可以通过访问 Rasterio 配置文件中的 ''“transform”'' 键。...此函数获取 Rasterio 对象并将其裁剪为提供的 空间范围。...In [45]: # Open Landsat image as a Rasterio object in order to crop it stack_band_paths = glob.glob('
* imgSizeX); // 进行NDWI的计算 for (int i = 0; i < imgSizeY; i++) { raseterBandRed->RasterIO...GF_Read, 0, i, imgSizeX, 1, bufferBlockRed, imgSizeX, 1, GDT_UInt32, 0, 0); raseterBandNIR->RasterIO...bufferBlockRed[j] - bufferBlockNIR[j]) / (bufferBlockRed[j] + bufferBlockNIR[j]); outputRasterBand->RasterIO
GetProjectionRef returns nullptr" << std::endl; return false; } //GDALRasterIOExtraArg exterArg; //INIT_RASTERIO_EXTRA_ARG...bandCount; GByte *imgBuf = new GByte[imgBufNum]; memset(imgBuf, 0, imgBufNum); //读取N列瓦片影像 srcDataset->RasterIO...+ srcStart + nRowTileWidth + j * storeWidth + i * tilesize, tileSizeX); } } poDstDS->RasterIO...bandCount; GByte *imgBuf = new GByte[imgBufNum]; memset(imgBuf,0, imgBufNum); //读取N列瓦片影像 srcDataset->RasterIO...imgBuf + srcStart + nRowTileWidth + j* storeWidth + i * tilesize, tileSizeX); } } poDstDS->RasterIO
用open_rasterio函数可以读取tif数据。...>>>import xarray as xr >>>url = 'https://github.com/mapbox/rasterio/raw/master/tests/data/RGB.byte.tif...' >>>da = xr.open_rasterio(url) >>>da >>> [1703814 values
GDALTermProgress, NULL); GDALRasterBand* poOutBand; poOutBand = poDstDS->GetRasterBand(1); poOutBand->RasterIO...0); GDALRasterBand* poOutBand_2; poOutBand_2 = poDstDS->GetRasterBand(2); poOutBand_2->RasterIO...0); GDALRasterBand* poOutBand_3; poOutBand_3 = poDstDS->GetRasterBand(3); poOutBand_3->RasterIO
领取专属 10元无门槛券
手把手带您无忧上云