pyrsgis库是一个用于处理地理信息系统(GIS)数据的Python库。它提供了一组功能强大的工具,可以帮助开发人员使用Python语言创建、处理、分析和可视化GIS数据。通过使用pyrsgis库,开发人员可以更轻松地理解和利用地理信息。
pyrsgis库是一个用于处理地理信息系统(GIS)数据的Python库。它提供了一组功能强大的工具,可以帮助开发人员使用Python语言创建、处理、分析和可视化GIS数据。通过使用pyrsgis库,开发人员可以更轻松地理解和利用地理信息。
pyrsgis库包含了许多常见的GIS操作和功能,例如读取和写入shapefile文件、转换坐标系、执行空间查询、计算地理特征属性等。它提供了许多方便使用的类和方法,例如GeoPandas、Shapely、Fiona、Rasterio、Pyproj和GDAL等,这些都可以帮助开发人员更高效地处理GIS数据。
一、Pyrsgis库安装
Pyrsgis可以直接通过pip install pyrsgis安装,同样也可以下载压缩包然后本地安装。PyPI中Pyrsgis包下载地址:pyrsgis · PyPI
二、导入库和函数
这些都是我后面代码需要使用到的函数,注意要导入,别到时候报错。
三、基础操作代码展示
1)获取影像基本信息
2)计算NDVI
这里给大家介绍一个经典案例,就是NDVI的计算。通过这个应该很容易就能理解Pyrsgis库的数据结构了。
3)空间位置裁剪
这里的裁剪主要是按照输入的空间矩形进行裁剪,并没有演示如何使用shp进行裁剪。这个可以应用于分幅裁剪、滑动裁剪等。空行分割的是实现这个功能的两种函数的使用方式。
4)移除无效值
这里的函数是移除无效值的,如-9999之类的,理论上应该也可以修改其他的DN值,但我自己没去试过,大家可以自行尝试。
5)平移影像
按照x、y方向进行影像平移,可选像素和坐标进行平移。
6)数组、表、CSV互转(包含剔除值)
这里的函数是数组、表、CSV互转,在转换的同时可以通过参数移除某些DN值。
def Convert_data(filepath):
input_file = r'E:/path_to_your_file/raster_file.tif'
ds, data_arr = raster.read(input_file)
data_table = convert.array_to_table(data_arr)
input_file = r'E:/path_to_your_file/raster_file.tif'
ds, data_arr = raster.read(input_file)
data_table = convert.array_to_table(data_arr)
print('Shape of the input array:', data_arr.shape)
print('Shape of the reshaped array:', data_table.shape)
input_file = r'E:/path_to_your_file/raster_file.tif'
new_data_arr = convert.table_to_array(data_table, n_rows=ds.RasterYSize, n_cols=ds.RasterXSize)
print('Shape of the array with newly added bands:', new_data_arr.shape)
new_data_arr = convert.table_to_array(data_table[:, -2:], n_rows=ds.RasterYSize, n_cols=ds.RasterXSize)
print('Shape of the array with newly added bands:', new_data_arr.shape)
input_file = r'E:/path_to_your_file/raster_file.tif'
output_file = r'E:/path_to_your_file/tabular_file.csv'
convert.raster_to_csv(input_file, filename=output_file)
input_dir = r'E:/path_to_your_file/'
output_file = r'E:/path_to_your_file/tabular_file.csv'
convert.raster_to_csv(input_dir, filename=output_file)
convert.raster_to_csv(input_dir, filename=output_file, negative=False, remove=[10, 54, 127], badrows=False)
input_file = r'E:/path_to_your_file/raster_file.tif'
out_csvfile = input_file.replace('.tif', '.csv')
convert.raster_to_csv(input_file, filename=out_csvfile, negative=False)
new_csvfile = r'E:/path_to_your_file/predicted_file.tif'
out_tiffile = new_csvfile.replace('.csv', '.tif')
convert.csv_to_raster(new_csvfile, ref_raster=input_file, filename=out_tiffile, compress='DEFLATE')
convert.csv_to_raster(new_csvfile, ref_raster=input_file, filename=out_tiffile,
cols=['Blue', 'Green', 'KMeans', 'RF_Class'], compress='DEFLATE')
convert.csv_to_raster(new_csvfile, ref_raster=input_file, filename=out_tiffile,
cols=['Blue', 'Green', 'KMeans', 'RF_Class'], stacked=False, compress='DEFLATE')

7)制作深度学习标签
此函数根据单波段或多波段栅格阵列生成影像片。图像芯片可以用作深度学习模型的直接输入(例如。卷积神经网络),输出格式:(4198376, 7, 7, 6)
8)翻转影像
按照东西或南北方向翻转影像
四、总结
Pyrsgis库之前使用的时候是因为要进行卷积神经网络的深度学习,然后里面制作深度学习标签的函数还是不错的,可以用一行代码实现标签的制作。但是如果数据过大,内存就会溢出报错,这个是Pyrsgis库没有解决的,当然我也没解决=。=大家可以自己尝试一下,有解决办法可以和我分享一下。总的来说Pyrsgis和Rasterio这两个库都还不错,都在GDAL的基础上进行了二开,方便了很多操作。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· DeepSeek 开源周回顾「GitHub 热点速览」
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了