使用geojson文件时剪辑栅格时出错

发布于 2025-01-23 19:27:50 字数 970 浏览 6 评论 0原文

我正在尝试用Geojson文件夹住栅格文件。两个文件都有相同的坐标系统和重叠。这是我的代码,我会收到错误(TypeError:不能乘以“仿射”类型的非元素乘以序列)

shape_geojson=path of jeojson
input_file= path of geotiff filesenter code here



with open(shape_geojson) as data_file:    
    geoms= json.load(data_file)

geoms = [{'type': 'Polygon', 'coordinates': [[[372187.1496072686, 5068258.904150674, 0.0], 
[373626.0661280414, 5068286.477440579, 0.0], [373548.73687596567, 5069922.161341429, 0.0], 
[372152.12599016255, 5069847.257662993, 0.0], [372187.1496072686, 5068258.904150674, 0.0]]]}]


for file in os.listdir(input_file):
        with rio.open(os.path.join(input_file, file)) as src:
        out_image, out_transform = rio.mask.mask(src, geoms, crop=True)
        out_meta = src.meta.copy()
        out_meta.update({"driver": "GTiff",
        "height": out_image.shape[1],
        "width": out_image.shape[2],
        "transform": out_transform})         

错误

typeerror:不能通过非启动序列乘以“仿射”类型的序列

I am trying to clip the raster file with geojson file. both files have same coordinates system and overlap. here is my code and i am getting the error ( TypeError: can't multiply sequence by non-int of type 'Affine')

shape_geojson=path of jeojson
input_file= path of geotiff filesenter code here



with open(shape_geojson) as data_file:    
    geoms= json.load(data_file)

geoms = [{'type': 'Polygon', 'coordinates': [[[372187.1496072686, 5068258.904150674, 0.0], 
[373626.0661280414, 5068286.477440579, 0.0], [373548.73687596567, 5069922.161341429, 0.0], 
[372152.12599016255, 5069847.257662993, 0.0], [372187.1496072686, 5068258.904150674, 0.0]]]}]


for file in os.listdir(input_file):
        with rio.open(os.path.join(input_file, file)) as src:
        out_image, out_transform = rio.mask.mask(src, geoms, crop=True)
        out_meta = src.meta.copy()
        out_meta.update({"driver": "GTiff",
        "height": out_image.shape[1],
        "width": out_image.shape[2],
        "transform": out_transform})         

Error

TypeError: can't multiply sequence by non-int of type 'Affine'

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

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

发布评论

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

评论(2

思慕 2025-01-30 19:27:50

非常不寻常的多边形在3D坐标中定义。将它们更改为2D,错误消失了。提供光栅的URL,可以显示完整的MWE。

geoms2d = [
    {
        "type": g["type"],
        "coordinates": [[xyz[0:2] for xyz in p] for p in g["coordinates"]],
    }
    for g in geoms
]

out_image, out_transform = rasterio.mask.mask(src, geoms2d, crop=True)

Very unusually your polygons are defined in 3D coordinates. Change them to 2D and the error goes away. Provide the URL of your raster and a full MWE can be shown.

geoms2d = [
    {
        "type": g["type"],
        "coordinates": [[xyz[0:2] for xyz in p] for p in g["coordinates"]],
    }
    for g in geoms
]

out_image, out_transform = rasterio.mask.mask(src, geoms2d, crop=True)
伤感在游骋 2025-01-30 19:27:50

罗布的回答使我接近了,但是我的坐标嵌套了。

最终必须这样做:

def is_mappable_of_floats(x):
    try:
        float(x[0])
        return True
    except:
        return False

def coords_3d_to_2d(coords):
    _coords = []
    for coord in coords:
        if is_mappable_of_floats(coord):
            _coords.append((coord[0], coord[1]))
        else:
            _coords.append(coords_3d_to_2d(coord))
    return _coords


geoms = [
    {
        "type": g["type"],
        "coordinates": coords_3d_to_2d(g["coordinates"]),
    }
    for g in geoms
]

Rob's answer got me close, but my coords were nested.

Ended up having to do this:

def is_mappable_of_floats(x):
    try:
        float(x[0])
        return True
    except:
        return False

def coords_3d_to_2d(coords):
    _coords = []
    for coord in coords:
        if is_mappable_of_floats(coord):
            _coords.append((coord[0], coord[1]))
        else:
            _coords.append(coords_3d_to_2d(coord))
    return _coords


geoms = [
    {
        "type": g["type"],
        "coordinates": coords_3d_to_2d(g["coordinates"]),
    }
    for g in geoms
]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文