用rasterio调整栅格阵列时如何限制小数

发布于 2025-02-04 09:42:37 字数 650 浏览 3 评论 0原文

我有多个栅格图像。当前,所有单元格都有一个数值的值。我想将此视为百分比。我使用以下代码做到这一点:

这可以正常工作,但是它为某些值创建了很多小数(33.33333333)。因为我正在使用大型横梁,所以这大大增加了文件大小。我不需要很多小数,那么如何限制它们?

import rasterio as rio

def Percentage(path_in, granule):
    path_out = os.path.join(certain_path, granule + ".jp2")

    for granule in glob.glob(r"path_to_rasters\*"):
    
        with rio.open(path_in) as src:
            # Read as numpy array
            array = src.read()
            profile = src.profile
            array = (array / count) * 100
            
        with rio.open(path_out, 'w', **profile) as dst:
            # Write to disk
            dst.write(array)

I have multiple raster images. Currently all the cells have a value which is a count of something. I would like to have this as a percentage. I did this with the following code:

This works fine, but it creates a lot of decimals for some values (33.33333333). Because I'm working with big rasters this greatly increases file size. I do not have need for a lot of decimals, so how do I limit them?

import rasterio as rio

def Percentage(path_in, granule):
    path_out = os.path.join(certain_path, granule + ".jp2")

    for granule in glob.glob(r"path_to_rasters\*"):
    
        with rio.open(path_in) as src:
            # Read as numpy array
            array = src.read()
            profile = src.profile
            array = (array / count) * 100
            
        with rio.open(path_out, 'w', **profile) as dst:
            # Write to disk
            dst.write(array)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

酒废 2025-02-11 09:42:37

尝试这个小数的三个小数:

with rio.open(path_out, 'w', decimal_precision=3, **profile) as dst:
    # Write to disk
    dst.write(array)

编写ASCII时对我有用。

Try this, for 3 decimals:

with rio.open(path_out, 'w', decimal_precision=3, **profile) as dst:
    # Write to disk
    dst.write(array)

It worked for me when writing a ASCII.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文