处理多波段栅格(Sentinel-使用 hndex 并创建索引

2024-08-24 0 405

嗨,在之前的博客中,我们讨论了如何使用 h3 索引和 postgresql 对单波段栅格进行栅格分析。在本博客中,我们将讨论如何处理多波段栅格并轻松创建索引。我们将使用 sentinel-2 图像并从处理后的 h3 细胞创建 ndvi 并可视化结果

下载哨兵2数据

我们正在从尼泊尔博卡拉地区的HTTPs://apps.sentinel-hub.com/eo-browser/下载sentinel 2数据,只是为了确保湖泊在图像网格中,以便我们可以轻松地验证 ndvi 结果

处理多波段栅格(Sentinel-使用 hndex 并创建索引

要下载所有乐队的哨兵图像:

  • 您需要创建一个帐户
  • 找到您所在区域的图像,选择覆盖您感兴趣区域的网格
  • 放大到网格,然后点击右侧竖条上的图标
  • 之后进入分析选项卡并选择图像格式为 tiff 32 位、高分辨率、wgs1984 格式的所有波段并检查所有波段

处理多波段栅格(Sentinel-使用 hndex 并创建索引

点击下载“修复打印机驱动工具”;

您还可以下载预生成的指数,例如 ndvi、仅假色 tiff 或最适合您需要的特定波段。我们正在下载所有乐队,因为我们想自己进行处理

  • 点击下载

处理多波段栅格(Sentinel-使用 hndex 并创建索引

预处理

当我们下载原始格式时,我们将所有乐队作为与哨兵分开的 tiff

处理多波段栅格(Sentinel-使用 hndex 并创建索引

  • 让我们创建一个合成图像:

这可以通过gis工具或gdal来完成

  1. 使用 gdal_merge:

我们需要将下载的文件重命名为 band1,band2 以避免文件名中出现斜杠
本次练习最多处理频段 9,您可以根据需要选择频段

1

gdal_merge.py -separate -o sentinel2_composite.tif band1.tif band2.tif band3.tif band4.tif band5.tif band6.tif band7.tif band8.tif band9.tif

  1. 使用 qgis :
  • 将所有单独的波段加载到 qgis
  • 转到光栅 > 杂项 > 合并

处理多波段栅格(Sentinel-使用 hndex 并创建索引

  • 合并时,您需要确保选中“将每个输入文件放入 sep band”

处理多波段栅格(Sentinel-使用 hndex 并创建索引

  • 现在将合并的 tiff 作为复合材料导出到原始 geotiff

家政

  • 确保您的图像采用 wgs1984 在我们的例子中,图像已经是 ws1984,所以不需要转换
  • 确保您没有任何 nodata 如果有则用 0 填充

1

gdalwarp -overwrite -dstnodata 0 "$input_file" "${output_file}_nodata.tif"

  • 最后确保你的输出图像是cog

1

gdal_translate -of cog "$input_file" "$output_file"

我正在使用 cog2h3 repo 中提供的 bash 脚本来自动化这些

1

sudo bash pre.sh sentinel2_composite.tif

h3细胞的处理和创建

现在,我们终于完成了预处理脚本,让我们继续计算复合齿轮图像中每个波段的 h3 单元格

  • 安装cog2h3

1

pip install cog2h3

1

export database_url="postgresql://user:password@host:port/database"

  • 奔跑

我们对此哨兵图像使用分辨率 10,但是您还会在脚本本身中看到,它将打印栅格的最佳分辨率,使 h3 单元小于栅格中的最小像素。

1

cog2h3 --cog sentinel2_composite_preprocessed.tif --Table sentinel --multiband --res 10

我们花了一分钟的时间来计算结果并将结果存储在 postgresql 中

日志:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

2024-08-24 08:39:43,233 - info - starting processing

2024-08-24 08:39:43,234 - info - cog file already exists at sentinel2_composite_preprocessed.tif

2024-08-24 08:39:43,234 - info - processing raster file: sentinel2_composite_preprocessed.tif

2024-08-24 08:39:43,864 - info - determined min fitting h3 resolution for band 1: 11

2024-08-24 08:39:43,865 - info - resampling original raster to: 200.786148m

2024-08-24 08:39:44,037 - info - resampling done for band 1

2024-08-24 08:39:44,037 - info - new native h3 resolution for band 1: 10

2024-08-24 08:39:44,738 - info - calculation done for res:10 band:1

2024-08-24 08:39:44,749 - info - determined min fitting h3 resolution for band 2: 11

2024-08-24 08:39:44,749 - info - resampling original raster to: 200.786148m

2024-08-24 08:39:44,757 - info - resampling done for band 2

2024-08-24 08:39:44,757 - info - new native h3 resolution for band 2: 10

2024-08-24 08:39:45,359 - info - calculation done for res:10 band:2

2024-08-24 08:39:45,366 - info - determined min fitting h3 resolution for band 3: 11

2024-08-24 08:39:45,366 - info - resampling original raster to: 200.786148m

2024-08-24 08:39:45,374 - info - resampling done for band 3

2024-08-24 08:39:45,374 - info - new native h3 resolution for band 3: 10

2024-08-24 08:39:45,986 - info - calculation done for res:10 band:3

2024-08-24 08:39:45,994 - info - determined min fitting h3 resolution for band 4: 11

2024-08-24 08:39:45,994 - info - resampling original raster to: 200.786148m

2024-08-24 08:39:46,003 - info - resampling done for band 4

2024-08-24 08:39:46,003 - info - new native h3 resolution for band 4: 10

2024-08-24 08:39:46,605 - info - calculation done for res:10 band:4

2024-08-24 08:39:46,612 - info - determined min fitting h3 resolution for band 5: 11

2024-08-24 08:39:46,612 - info - resampling original raster to: 200.786148m

2024-08-24 08:39:46,619 - info - resampling done for band 5

2024-08-24 08:39:46,619 - info - new native h3 resolution for band 5: 10

2024-08-24 08:39:47,223 - info - calculation done for res:10 band:5

2024-08-24 08:39:47,230 - info - determined min fitting h3 resolution for band 6: 11

2024-08-24 08:39:47,230 - info - resampling original raster to: 200.786148m

2024-08-24 08:39:47,239 - info - resampling done for band 6

2024-08-24 08:39:47,239 - info - new native h3 resolution for band 6: 10

2024-08-24 08:39:47,829 - info - calculation done for res:10 band:6

2024-08-24 08:39:47,837 - info - determined min fitting h3 resolution for band 7: 11

2024-08-24 08:39:47,837 - info - resampling original raster to: 200.786148m

2024-08-24 08:39:47,845 - info - resampling done for band 7

2024-08-24 08:39:47,845 - info - new native h3 resolution for band 7: 10

2024-08-24 08:39:48,445 - info - calculation done for res:10 band:7

2024-08-24 08:39:48,453 - info - determined min fitting h3 resolution for band 8: 11

2024-08-24 08:39:48,453 - info - resampling original raster to: 200.786148m

2024-08-24 08:39:48,461 - info - resampling done for band 8

2024-08-24 08:39:48,461 - info - new native h3 resolution for band 8: 10

2024-08-24 08:39:49,046 - info - calculation done for res:10 band:8

2024-08-24 08:39:49,054 - info - determined min fitting h3 resolution for band 9: 11

2024-08-24 08:39:49,054 - info - resampling original raster to: 200.786148m

2024-08-24 08:39:49,062 - info - resampling done for band 9

2024-08-24 08:39:49,063 - info - new native h3 resolution for band 9: 10

2024-08-24 08:39:49,647 - info - calculation done for res:10 band:9

2024-08-24 08:39:51,435 - info - converting h3 indices to hex strings

2024-08-24 08:39:51,906 - info - overall raster calculation done in 8 seconds

2024-08-24 08:39:51,906 - info - creating or replacing table sentinel in database

2024-08-24 08:40:03,153 - info - table sentinel created or updated successfully in 11.25 seconds.

2024-08-24 08:40:03,360 - info - processing completed

分析

现在我们的数据已经在 postgresql 中了,让我们做一些分析吧

  • 验证我们是否拥有处理过的所有频段(记住我们处理的是频段 1 到 9)

1

2

select *

from sentinel

处理多波段栅格(Sentinel-使用 hndex 并创建索引

  • 计算每个细胞的 ndvi

1

2

3

explain analyze

select h3_ix , (band8-band4)/(band8+band4) as ndvi

from public.sentinel

查询计划:

1

2

3

4

5

query plan                                                                                                       |

-----------------------------------------------------------------------------------------------------------------+

seq scan on sentinel  (cost=0.00..28475.41 rows=923509 width=16) (actual time=0.014..155.049 rows=923509 looPS=1)|

planning time: 0.080 ms                                                                                          |

execution time: 183.764 ms                                                                                       |

正如您在此处看到的那样,该区域中的所有行的计算都是即时的。对于所有其他索引都是如此,您可以使用 h3_ix 主键计算与其他表的复杂索引连接,并从中导出有意义的结果,而不必担心,因为 postgresql 能够处理复杂的查询和表连接。

可视化和验证

让我们可视化并验证计算的索引是否正确

  • 创建表格(用于在 qgis 中可视化)

1

2

3

4

create table ndvi_sentinel

as(

select h3_ix , (band8-band4)/(band8+band4) as ndvi

from public.sentinel )

  • 让我们添加几何图形来可视化 h3 细胞 这仅是在 qgis 中可视化所必需的,如果您自己构建一个最小的 API,则不需要它,因为您可以直接从查询构造几何图形

1

2

3

alter table ndvi_sentinel 

add column geometry geometry(polygon, 4326)

generated always as (h3_cell_to_boundary_geometry(h3_ix)) stored;

  • 在几何体上创建索引

1

create index on ndvi_sentinel(geometry);

  • 在qgis中连接数据库并根据ndvi值可视化表格 让我们获取费瓦湖或云附近的区域

处理多波段栅格(Sentinel-使用 hndex 并创建索引

据我们所知,-1.0 到 0.1 之间的值应该代表深水或浓密的云层
让我们看看这是否属实(使第一个类别透明以查看底层图像)

  • 检查云:

处理多波段栅格(Sentinel-使用 hndex 并创建索引

  • 检查湖

处理多波段栅格(Sentinel-使用 hndex 并创建索引
由于湖周围有云,因此附近的田野被云覆盖,这是有道理的

处理多波段栅格(Sentinel-使用 hndex 并创建索引

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

免责声明
1. 本站所有资源来源于用户上传和网络等,如有侵权请邮件联系本站整改team@lcwl.fun!
2. 分享目的仅供大家学习和交流,您必须在下载后24小时内删除!
3. 不得使用于非法商业用途,不得违反国家法律。否则后果自负!
4. 本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解!
5. 如有链接无法下载、失效或广告,请联系本站工作人员处理!
6. 本站资源售价或VIP只是赞助,收取费用仅维持本站的日常运营所需!
7. 如遇到加密压缩包,请使用WINRAR解压,如遇到无法解压的请联系管理员!
8. 因人力时间成本问题,部分源码未能详细测试(解密),不能分辨部分源码是病毒还是误报,所以没有进行任何修改,大家使用前请进行甄别!
9.本站所有源码资源都是经过本站工作人员人工亲测可搭建的,保证每个源码都可以正常搭建,但不保证源码内功能都完全可用,源码属于可复制的产品,无任何理由退款!

网站搭建学习网 Python 处理多波段栅格(Sentinel-使用 hndex 并创建索引 https://www.xuezuoweb.com/14932.html

上一篇: Python 教程 - 简介
下一篇: 每周挑战
常见问题
  • 本站所有的源码都是经过平台人工部署搭建测试过可用的
查看详情
  • 购买源码资源时购买了带主机的套餐是指可以享受源码和所选套餐型号的主机两个产品,在本站套餐里开通主机可享优惠,最高免费使用主机
查看详情

相关文章

发表评论
暂无评论
官方客服团队

为您解决烦忧 - 24小时在线 专业服务

Fa快捷助手
手机编程软件开发

在手机上用手点一点就能轻松做软件

去做软件
链未云主机
免备案香港云主机

开通主机就送域名的免备案香港云主机

去使用
链未云服务器
免备案香港云服务器

支持售后、超低价、稳定的免备案香港云服务器

去使用