当前位置: 首页 > news >正文

【PythonRS】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

二、导入库和函数

        这些都是我后面代码需要使用到的函数,注意要导入,别到时候报错。

import os
from pyrsgis import raster, convert, ml

三、基础操作代码展示

1)获取影像基本信息

def Get_data(filepath):# 获取影像基本信息ds, data_arr = raster.read(filepath)  # 基础信息资源和数组ds_bands = ds.RasterCount  # 波段数ds_width = ds.RasterXSize  # 宽度ds_height = ds.RasterYSize  # 高度ds_bounds = ds.bbox  # 四至范围ds_geo = ds.GeoTransform  # 仿射地理变换参数ds_prj = ds.Projection  # 投影坐标系print("影像的宽度为:" + str(ds_width))print("影像的高度为:" + str(ds_height))print("仿射地理变换参数为:" + str(ds_geo))print("投影坐标系为:" + str(ds_prj))

2)计算NDVI

        这里给大家介绍一个经典案例,就是NDVI的计算。通过这个应该很容易就能理解Pyrsgis库的数据结构了。

def Get_NDVI(filepath):# 计算NDVIds, data_arr = raster.read(filepath)red_arr = data_arr[3, :, :]nir_arr = data_arr[4, :, :]result_arr = (nir_arr - red_arr) / (nir_arr + red_arr)# result_arr = (data_arr[4, :, :] - data_arr[3, :, :]) / (data_arr[4, :, :] + data_arr[3, :, :])output_file = r'E:/path_to_your_file/landsat8_result.tif'raster.export(result_arr, ds, output_file, dtype='float32', bands="all", nodata=0, compress="LZW")# 写入的数组,基础信息,路径,格式,波段,无效值,压缩方式

3)空间位置裁剪

        这里的裁剪主要是按照输入的空间矩形进行裁剪,并没有演示如何使用shp进行裁剪。这个可以应用于分幅裁剪、滑动裁剪等。空行分割的是实现这个功能的两种函数的使用方式。

def Clip_data(filepath):# 按掩膜提取ds, data_arr = raster.read(filepath)print('Original bounding box:', ds.bbox)print('Original shape of raster:', data_arr.shape)new_ds, clipped_arr = raster.clip(ds, data_arr, x_min=770000, x_max=790000, y_min=1420000, y_max=1440000)raster.export(clipped_arr, new_ds, r'E:/path_to_your_file/clipped_file.tif')infile = r'E:/path_to_your_file/your_file.tif'outfile = r'E:/path_to_your_file/clipped_file.tif'raster.clip_file(infile, x_min=770000, x_max=790000, y_min=1420000, y_max=1440000, outfile=outfile)

4)移除无效值

        这里的函数是移除无效值的,如-9999之类的,理论上应该也可以修改其他的DN值,但我自己没去试过,大家可以自行尝试。

def Modify_data(filepath):# 修改影像数组,如移除无效值ds, data_arr = raster.read(filepath)new_ds, new_arr = raster.trim(ds, data_arr, remove=-9999)print('Shape of the input array:', data_arr.shape)print('Shape of the trimmed array:', new_arr.shape)ds, data_arr = raster.read(filepath)new_arr = raster.trim_array(data_arr, remove=-9999)print('Shape of the input array:', data_arr.shape)print('Shape of the trimmed array:', new_arr.shape)infile = r'E:/path_to_your_file/your_file.tif'outfile = r'E:/path_to_your_file/trimmed_file.tif'raster.trim_file(infile, -9999, outfile)

5)平移影像

        按照x、y方向进行影像平移,可选像素和坐标进行平移。

def Shift_data(filepath):# 平移影像ds, data_arr = raster.read(filepath)new_ds = raster.shift(ds, x=10, y=10)  # x,y方向偏移量。按栅格的投影单位移动数据源 或分别按细胞数print('Original bounding box:', ds.bbox)print('Modified bounding box:', new_ds.bbox)new_ds = raster.shift(ds, x=10, y=10, shift_type='cell')  # shift_type='coordinate'print('Modified bounding box:', new_ds.GeoTransform)raster.export(data_arr, new_ds, r'E:/path_to_your_file/shifted_file.tif')infile = r'E:/path_to_your_file/your_file.tif'outfile = r'E:/path_to_your_file/shifted_file.tif'raster.shift_file(infile, x=10, y=10, outfile=outfile, shift_type='cell')

6)数组、表、CSV互转(包含剔除值)

        这里的函数是数组、表、CSV互转,在转换的同时可以通过参数移除某些DN值。

def Convert_data(filepath):# 数组转表、CSV,修改值input_file = r'E:/path_to_your_file/raster_file.tif'ds, data_arr = raster.read(input_file)  # Shape of the input array: (6, 800, 400)data_table = convert.array_to_table(data_arr)  # Shape of the reshaped array: (320000, 6)# 该函数将单波段或多波段栅格数组转换为表,其中 列表示输入波段,每行表示一个单元格。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)  # Shape of the input array: (6, 800, 400)print('Shape of the reshaped array:', data_table.shape)  # Shape of the reshaped array: (320000, 6)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)# Shape of the array with newly added bands: (8, 800, 400)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)# Shape of the array with newly added bands: (2, 800, 400)# 表转数组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')# 将每列导出为单独的波段,请将参数设置为 。stacked=False

7)制作深度学习标签

        此函数根据单波段或多波段栅格阵列生成影像片。图像芯片可以用作深度学习模型的直接输入(例如。卷积神经网络),输出格式:(4198376, 7, 7, 6)

def Create_CNN(filepath):# 此函数根据单波段或多波段栅格阵列生成影像片。图像 芯片可以用作深度学习模型的直接输入(例如。卷积神经网络)# -----------------------------数组生成深度学习芯片-----------------------------infile = r'E:/path_to_your_file/your_file.tif'ds, data_arr = raster.read(infile)image_chips = ml.array_to_chips(data_arr, y_size=7, x_size=7)print('Shape of input array:', data_arr.shape)  # Shape of input array: (6, 2054, 2044)print('Shape of generated image chips:', image_chips.shape)  # Shape of generated image chips: (4198376, 7, 7, 6)infile = r'E:/path_to_your_file/your_file.tif'ds, data_arr = raster.read(infile)image_chips = ml.array2d_to_chips(data_arr, y_size=5, x_size=5)print('Shape of input array:', data_arr.shape)  # Shape of input array: (2054, 2044)print('Shape of generated image chips:', image_chips.shape)  # Shape of generated image chips: (4198376, 5, 5)# ----------------------------影像直接生成深度学习芯片----------------------------infile_2d = r'E:/path_to_your_file/your_2d_file.tif'image_chips = ml.raster_to_chips(infile_2d, y_size=7, x_size=7)print('Shape of single band generated image chips:', image_chips.shape)# Shape of single bandgenerated image chips: (4198376, 7, 7)infile_3d = r'E:/path_to_your_file/your_3d_file.tif'image_chips = ml.raster_to_chips(infile_3d, y_size=7, x_size=7)print('Shape of multiband generated image chips:', image_chips.shape)# Shape of multiband generated image chips: (4198376, 7, 7, 6)

8)翻转影像

        按照东西或南北方向翻转影像

def Reverse_Image(filepath):# 按照东西、南北方向反转影像# -------------------------------北向、东向翻转--------------------------------input_file = r'E:/path_to_your_file/your_file.tif'ds, data_arr = raster.read(input_file)north_arr, east_arr = raster.north_east(data_arr)print(north_arr.shape, east_arr.shape)north_arr, east_arr = raster.north_east(data_arr, flip_north=True, flip_east=True)north_arr = raster.north_east(data_arr, layer='north')from matplotlib import pyplot as pltplt.imshow(north_arr)plt.show()plt.imshow(east_arr)plt.show()input_file = r'E:/path_to_your_file/your_file.tif'ds, data_arr = raster.read(input_file)north_arr, east_arr = raster.north_east(data_arr)print(north_arr.shape, east_arr.shape)north_arr = raster.north_east(data_arr, layer='north')from matplotlib import pyplot as pltplt.imshow(north_arr)plt.show()plt.imshow(east_arr)plt.show()raster.export(north_arr, ds, r'E:/path_to_your_file/northing.tif', dtype='float32')raster.export(east_arr, ds, r'E:/path_to_your_file/easting.tif', dtype='float32')# -------------------------使用参考.tif文件生成北向栅格----------------------------reference_file = r'E:/path_to_your_file/your_file.tif'raster.northing(file1, r'E:/path_to_your_file/northing_number.tif', flip=False, value='number')raster.northing(file1, r'E:/path_to_your_file/northing_normalised.tif', value='normalised')  # 输出栅格进行归一化raster.northing(file1, r'E:/path_to_your_file/northing_coordinates.tif', value='coordinates')raster.northing(file1, r'E:/path_to_your_file/northing_number_compressed.tif', compress='DEFLATE')reference_file = r'E:/path_to_your_file/your_file.tif'raster.easting(file1, r'E:/path_to_your_file/easting_number.tif', flip=False, value='number')raster.easting(file1, r'E:/path_to_your_file/easting_normalised.tif', value='normalised')raster.easting(file1, r'E:/path_to_your_file/easting_normalised.tif', value='normalised')raster.easting(file1, r'E:/path_to_your_file/easting_number_compressed.tif', compress='DEFLATE')

四、总结

        Pyrsgis库之前使用的时候是因为要进行卷积神经网络的深度学习,然后里面制作深度学习标签的函数还是不错的,可以用一行代码实现标签的制作。但是如果数据过大,内存就会溢出报错,这个是Pyrsgis库没有解决的,当然我也没解决=。=大家可以自己尝试一下,有解决办法可以和我分享一下。总的来说Pyrsgis和Rasterio这两个库都还不错,都在GDAL的基础上进行了二开,方便了很多操作。

相关文章:

【PythonRS】Pyrsgis库安装+基础函数使用教程

pyrsgis库是一个用于处理地理信息系统(GIS)数据的Python库。它提供了一组功能强大的工具,可以帮助开发人员使用Python语言创建、处理、分析和可视化GIS数据。通过使用pyrsgis库,开发人员可以更轻松地理解和利用地理信息。 pyrsgis库包含了许多常见的GIS操…...

线扫相机DALSA--分频倍频计算公式及原理

分频倍频计算公式及原理 推导原理: 假设编码器脉冲精度为P;同步轮/辊周长为C,Fov为视野,Res为线扫相机分辨率,N代表N倍频编码器,分频为D,倍频为M 线扫项目常规采用N(N 4&#xff0…...

1818_ChibiOS的计数信号量

全部学习汇总: GreyZhang/g_ChibiOS: I found a new RTOS called ChibiOS and it seems interesting! (github.com) 之前见过计数信号量,也是在FreeRTOS中看到的。也看到过这样的功能在驱动设计中的应用,但是当时没有理解这个使用的方式。 1.…...

企业微信接入芋道SpringBoot项目

背景:使用芋道框架编写了一个数据看板功能需要嵌入到企业微信中,方便各级人员实时观看 接入企业微信的话肯定不能像平常pc端一样先登录再根据权限看页面,不然的话不如直接手机浏览器打开登录账号来得更为方便,所以迎面而来面临两…...

群晖上搭建teamspeak3语音服务器

什么是 TeamSpeak ? TeamSpeak (简称 TS)是一款团队语音通讯工具,但比一般的通讯工具具有更多的功能而且使用方便。它由服务器端程序和客户端程序两部分组成,如果不是想自己架设 TS 服务器,只需下载客户端程…...

Python爬虫(二十四)_selenium案例:执行javascript脚本

本章叫介绍如何使用selenium在浏览器中使用js脚本,更多内容请参考:Python学习指南 隐藏百度图片 #-*- coding:utf-8 -*- #本篇将模拟执行javascript语句from selenium import webdriver from selenium.webdriver.common.keys import Keysdriver webdri…...

Devops团队

DevOps(Development 和 Operations的缩写)是一种软件开发和运维(IT运营)之间协作和沟通的文化、原则和实践。DevOps团队是专注于实施DevOps原则的团队,旨在促进软件开发和运维之间的紧密协作,以加速软件交付…...

力扣labuladong——一刷day10

提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档 文章目录 前言一、力扣76. 最小覆盖子串二、力扣567. 字符串的排列三、力扣438. 找到字符串中所有字母异位词四、力扣3. 无重复字符的最长子串 前言 一、力扣76. 最小覆盖子串…...

https下载图片

OpenSSL用法示例 OpenSSL源码安装 对于ubuntu,懒得编译源码可以直接安装 sudo apt-get install libssl–dev /usr/include/openssl/ssl.h CMakeLists中添加 link_libraries(ssl crypto) apt-get安装不需要再制定libssl.a, libcrypto.a的路径了, 就像用libc标…...

二叉树的后续遍历(迭代法)

迭代法实现二叉树的后续遍历 1、递归版本 public static void dfs(TreeNode root){if(rootnull){return;}if(root.left!null)dfs(root.left);if(root.right!null)dfs(root.right);System.out.println(root.val); }从递归版本可以看出我们第一步需要遍历完所有的左节点 这里我…...

CVE-2021-41773/42013 apache路径穿越漏洞

影响范围 CVE-2021-41773 Apache HTTP server 2.4.49 CVE-2021-42013 Apache HTTP server 2.4.49/2.4.50 漏洞原理 Apache HTTP Server 2.4.49版本使用的ap_normalize_path函数在对路径参数进行规范化时会先进行url解码,然后判断是否存在…/的路径穿越符&#xf…...

前端性能测试工具WebPagetest

简介:一款web性能在线性能评测工具,可测试有关页面在各种条件下的性能,并且提供深入诊断信息。 WebPagetest 的主页:https://www.webpagetest.org/,也就是工具的使用界面。 注意:WebPageTest 并不是完全免…...

易语言软件定制软件开发脚本开发协议软件电脑网站APP应用视频制作工程制作

随着信息技术的不断发展,易语言软件定制开发已成为许多公司的一项重要业务。本文将探讨如何利用易语言承接软件定制软件开发脚本开发协议软件电脑网站APP应用视频制作工程制作。 一、易语言概述 易语言是一种简单易学的编程语言,它采用中文编程&#xff…...

Windows上配置IP端口转发

在通常涉及到使用网络地址转换(NAT)规则,可以使用一些工具和命令行选项来实现。以下是在Windows上配置端口转发的一般步骤: **注意:端口转发需要管理员权限,因此请确保以管理员身份运行命令行工具。** 1.…...

韦东山D1S板子——汇编启动代码第一行分析(.long 0x0300006f)

1、汇编启动源码 2、分析二进制&#xff1a;0x0300006f 2.1、反汇编代码 2.2、jal指令 jal指令的作用&#xff1a;跳转到当前PC值偏移offset处执行&#xff0c;其中offset由jal指令的bi[31:12]表示&#xff1b; 2.3、分析指令&#xff1a;j 20030 <reset> j 20030 //伪…...

了解单域名证书和通配符证书的区别,选择合适的SSL证书解决方案

随着互联网的不断发展&#xff0c;网站安全性问题一直备受关注&#xff0c;在保护网站数据安全的过程中&#xff0c;SSL证书一直发挥着至关重要的作用。而在选择SSL证书时&#xff0c;单域名证书和通配符证书是两种常见的选择。本文将详细介绍单域名证书和通配符证书的区别&…...

【LeetCode】7. 整数反转

题目链接 文章目录 Python3官方解法 ⟮ O ( ∣ x ∣ ) 、 O ( 1 ) ⟯ \lgroup O(|x|)、O(1)\rgroup ⟮O(∣x∣)、O(1)⟯写法2写法3 C官方解法 ⟮ O ( ∣ x ∣ ) 、 O ( 1 ) ⟯ \lgroup O(|x|)、O(1)\rgroup ⟮O(∣x∣)、O(1)⟯ Python3 官方解法 ⟮ O ( ∣ x ∣ ) 、 O ( 1…...

防止请求重复提交:注解+拦截器的实现方案

文章目录 了解请求重复提交解决思路具体实现 了解请求重复提交 请求重复提交是指用户在一次请求还未处理完成时&#xff0c;重复提交了相同的请求。这种情况通常发生在网络延迟、用户误操作或系统性能不佳等情况下。 请求重复提交可能会导致以下问题和影响&#xff1a; 数据不…...

C#使用mysql-connector-net驱动连接mariadb报错

给树莓派用最新的官方OS重刷了一下&#xff0c;并且用apt install mariadb-server装上“mysql”作为我的测试服务器。然后神奇的事情发生了&#xff0c;之前用得好好的程序突然就报错了&#xff0c;经过排查&#xff0c;发现在连接数据库的Open阶段就报错了。写了个最单纯的Con…...

SpringBoot 定时任务:@EnableScheduling @Scheduled

Scheduled注解参数 cron参数 这个参数是最经常使用的参数&#xff0c;表示接收一个cron参数&#xff0c;cron它是一个表达式&#xff0c;最多接收7个参数&#xff0c;从左到右分别表示&#xff1a;秒 分 时 天 月 周 年&#xff1b;参数以空格隔开&#xff0c;其中年不是必须参…...

《Playwright:微软的自动化测试工具详解》

Playwright 简介:声明内容来自网络&#xff0c;将内容拼接整理出来的文档 Playwright 是微软开发的自动化测试工具&#xff0c;支持 Chrome、Firefox、Safari 等主流浏览器&#xff0c;提供多语言 API&#xff08;Python、JavaScript、Java、.NET&#xff09;。它的特点包括&a…...

什么是库存周转?如何用进销存系统提高库存周转率?

你可能听说过这样一句话&#xff1a; “利润不是赚出来的&#xff0c;是管出来的。” 尤其是在制造业、批发零售、电商这类“货堆成山”的行业&#xff0c;很多企业看着销售不错&#xff0c;账上却没钱、利润也不见了&#xff0c;一翻库存才发现&#xff1a; 一堆卖不动的旧货…...

HBuilderX安装(uni-app和小程序开发)

下载HBuilderX 访问官方网站&#xff1a;https://www.dcloud.io/hbuilderx.html 根据您的操作系统选择合适版本&#xff1a; Windows版&#xff08;推荐下载标准版&#xff09; Windows系统安装步骤 运行安装程序&#xff1a; 双击下载的.exe安装文件 如果出现安全提示&…...

高防服务器能够抵御哪些网络攻击呢?

高防服务器作为一种有着高度防御能力的服务器&#xff0c;可以帮助网站应对分布式拒绝服务攻击&#xff0c;有效识别和清理一些恶意的网络流量&#xff0c;为用户提供安全且稳定的网络环境&#xff0c;那么&#xff0c;高防服务器一般都可以抵御哪些网络攻击呢&#xff1f;下面…...

Swagger和OpenApi的前世今生

Swagger与OpenAPI的关系演进是API标准化进程中的重要篇章&#xff0c;二者共同塑造了现代RESTful API的开发范式。 本期就扒一扒其技术演进的关键节点与核心逻辑&#xff1a; &#x1f504; 一、起源与初创期&#xff1a;Swagger的诞生&#xff08;2010-2014&#xff09; 核心…...

浪潮交换机配置track检测实现高速公路收费网络主备切换NQA

浪潮交换机track配置 项目背景高速网络拓扑网络情况分析通信线路收费网络路由 收费汇聚交换机相应配置收费汇聚track配置 项目背景 在实施省内一条高速公路时遇到的需求&#xff0c;本次涉及的主要是收费汇聚交换机的配置&#xff0c;浪潮网络设备在高速项目很少&#xff0c;通…...

SQL慢可能是触发了ring buffer

简介 最近在进行 postgresql 性能排查的时候,发现 PG 在某一个时间并行执行的 SQL 变得特别慢。最后通过监控监观察到并行发起得时间 buffers_alloc 就急速上升,且低水位伴随在整个慢 SQL,一直是 buferIO 的等待事件,此时也没有其他会话的争抢。SQL 虽然不是高效 SQL ,但…...

vulnyx Blogger writeup

信息收集 arp-scan nmap 获取userFlag 上web看看 一个默认的页面&#xff0c;gobuster扫一下目录 可以看到扫出的目录中得到了一个有价值的目录/wordpress&#xff0c;说明目标所使用的cms是wordpress&#xff0c;访问http://192.168.43.213/wordpress/然后查看源码能看到 这…...

Vite中定义@软链接

在webpack中可以直接通过符号表示src路径&#xff0c;但是vite中默认不可以。 如何实现&#xff1a; vite中提供了resolve.alias&#xff1a;通过别名在指向一个具体的路径 在vite.config.js中 import { join } from pathexport default defineConfig({plugins: [vue()],//…...

【C++】纯虚函数类外可以写实现吗?

1. 答案 先说答案&#xff0c;可以。 2.代码测试 .h头文件 #include <iostream> #include <string>// 抽象基类 class AbstractBase { public:AbstractBase() default;virtual ~AbstractBase() default; // 默认析构函数public:virtual int PureVirtualFunct…...