在网格数据的4D numpy数组中查找不规则区域(纬度/经度)

冰壶

我有一个很大的4维温度数据集[时间,压力,纬度,经度]。我需要查找经纬度索引定义的区域内的所有网格点,并计算该区域的平均值,以使我得到二维数组。

如果我的区域是矩形(或正方形),我知道如何执行此操作,但是如何使用不规则的多边形来完成此操作?

下面的图像显示了我需要平均的区域以及数据在阵列中的经纬度网格

纬度/经度网格,平均面积

朱利安

我相信这应该可以解决您的问题。

下面的代码在由顶点列表定义的多边形中生成所有像元。它逐行“扫描”多边形,跟踪您(重新)输入或退出多边形的过渡列。

def row(x, transitions):
    """ generator spitting all cells in a row given a list of transition (in/out) columns."""

    i = 1
    in_poly = True
    y = transitions[0]
    while i < len(transitions):
        if in_poly:
            while y < transitions[i]:
                yield (x,y)
                y += 1
            in_poly = False
        else:
            in_poly = True
            y = transitions[i]
        i += 1


def get_same_row_vert(i, vertices):
    """ find all vertex columns in the same row as vertices[i], and return next vertex index as well."""

    vert = []
    x = vertices[i][0]
    while i < len(vertices) and vertices[i][0] == x:
        vert.append(vertices[i][1])
        i += 1
    return vert, i


def update_transitions(old, new):
    """ update old transition columns for a row given new vertices. 

    That is: merge both lists and remove duplicate values (2 transitions at the same column cancel each other)"""

    if old == []:
        return new
    if new == []:
        return old
    o0 = old[0]
    n0 = new[0]
    if o0 == n0:
        return update_transitions(old[1:], new[1:])
    if o0 < n0:
        return [o0] + update_transitions(old[1:], new)
    return [n0] + update_transitions(old, new[1:])


def polygon(vertices):
    """ generator spitting all cells in the polygon defined by given vertices."""

    vertices.sort()
    x = vertices[0][0]
    transitions, i = get_same_row_vert(0, vertices)
    while i < len(vertices):
        while x < vertices[i][0]:            
            for cell in row(x, transitions):
                yield cell
            x += 1
        vert, i = get_same_row_vert(i, vertices)
        transitions = update_transitions(transitions, vert)


# define a "strange" polygon (hook shaped)
vertices = [(0,0),(0,3),(4,3),(4,0),(3,0),(3,2),(1,2),(1,1),(2,1),(2,0)]

for cell in polygon(vertices):
    print cell
    # or do whatever you need to do

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

使用 2 个谷歌地图点(纬度和经度)从 mongodb 查询中查找方形区域数据

来自分类Dev

数据帧到4D数组

来自分类Dev

如何使用CSS在网站中添加和显示4D图像

来自分类Dev

numpy-在4D矩阵中搜索(又称混乱网格物体)

来自分类Dev

使用 matplotlib 绘制 4D numpy 数据

来自分类Dev

4D数据库中的差异备份

来自分类Dev

在3D numpy数组中查找区域的中心坐标

来自分类Dev

WPF在网格中显示数据

来自分类Dev

数据未显示在网格中

来自分类Dev

从返回的模型在网格中显示数据

来自分类Dev

广播4D numpy数组

来自分类Dev

过滤 4D numpy 数组

来自分类Dev

极坐标投影到规则的纬度/经度网格

来自分类Dev

Matplotlib底图+带不规则2d numpy数组的轮廓线

来自分类Dev

R中的不规则形状的网格(ggplot)

来自分类Dev

Numpy对称4D矩阵构造

来自分类Dev

优化4D Numpy阵列结构

来自分类Dev

如何使用 Laravel 在数据库列中查找不规则字符串

来自分类Dev

Python-总和4D数组

来自分类Dev

如何在网格中显示移动服务数据?MVC

来自分类Dev

在网格中的数据周围绘制圆(MATLAB)

来自分类Dev

不想在网格中显示数据ID

来自分类Dev

显示SlickGrid,但未在网格中填充数据

来自分类Dev

我在网格中的数据没有更新

来自分类Dev

Freemarker:使用引导程序在网格中显示数据

来自分类Dev

在网格中显示来自数据库的图像 - laravel

来自分类Dev

从纬度/经度坐标中减去数据

来自分类Dev

4D阵列中的连续差异

来自分类Dev

Numpy 4D数组到tf.data.dataset