数据分箱:将不规则的多边形转换为规则的网格 [英] Data binning: irregular polygons to regular mesh

查看:212
本文介绍了数据分箱:将不规则的多边形转换为规则的网格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有成千上万个以表格格式存储的多边形(给定它们的4个角坐标),它们代表了地球的小区域.此外,每个多边形都有一个数据值. 该文件看起来像这样:

I have thousands of polygons stored in a table format (given their 4 corner coordinates) which represent small regions of the earth. In addition, each polygon has a data value. The file looks for example like this:

lat1,  lat2,  lat3,  lat4,  lon1,   lon2,   lon3,   lon4,   data
57.27, 57.72, 57.68, 58.1,  151.58, 152.06, 150.27, 150.72, 13.45
56.96, 57.41, 57.36, 57.79, 151.24, 151.72, 149.95, 150.39, 56.24
57.33, 57.75, 57.69, 58.1,  150.06, 150.51, 148.82, 149.23, 24.52
56.65, 57.09, 57.05, 57.47, 150.91, 151.38, 149.63, 150.06, 38.24
57.01, 57.44, 57.38, 57.78, 149.74, 150.18, 148.5,  148.91, 84.25
...

许多多边形相交或重叠.现在,我想创建一个* m矩阵,其范围为-90°至90°纬度和-180°至180°经度,以0.25°x0.25°为步长,以存储(区域加权)平均值数据每个像素内的所有多边形的值.

Many of the polygons intersect or overlap. Now I would like to create a n*m matrix ranging from -90° to 90° latitude and -180° to 180° longitude in steps of, for instance, 0.25°x0.25° to store the (area-weighted) mean data value of all polygons that fall within each pixel.

因此,规则网格中的一个像素应获得一个或多个多边形的平均值(如果没有与该像素重叠的多边形,则没有像素).每个多边形应根据其在该像素内的面积分数来贡献该平均值.

So, one pixel in the regular mesh shall get the mean value of one or more polygons (or none if no polygon overlaps with the pixel). Each polygon should contribute to this mean value depending on its area fraction within this pixel.

基本上,常规网格和多边形看起来像这样:

Basically the regular mesh and the polygons look like this:

如果查看像素2,您会看到两个多边形位于该像素内.因此,我必须考虑两个多边形的面积分数来取它们的平均数据值.然后将结果存储在常规网格像素中.

If you look at pixel 2, you see that two polygons are inside this pixel. Thus, I have to take the mean data value of both polygons considering their area fractions. The result should be then stored in the regular mesh pixel.

我环顾四周,至今仍未找到令人满意的方法.由于我在日常工作中使用Python/Numpy,因此我想坚持下去.这可能吗?软件包 shapely 看起来很有希望,但我不知道从哪里开始. 将所有内容都移植到postgis数据库上需要花费很多精力,我想我的路上会遇到很多障碍.

I looked around the web and found no satisfactory approach for this so far. Since I am using Python/Numpy for daily work I would like to stick to it. Is this possible? The package shapely looks promising but I don't know where to begin with... Porting everything to a postgis database is an awful amount of effort and I guess there will be quite a few obstacles in my way.

推荐答案

有很多方法可以做到这一点,但是,Shapely可以提供帮助.看来您的多边形是四边形的,但是我要介绍的方法并不依赖于此.除了 box()和<来自shapely.geometry的href ="http://toblerity.github.com/shapely/manual.html#Polygon" rel ="nofollow"> Polygon().

There are plenty of ways to do it, but yes, Shapely can help. It appears that your polygons are quadrilateral, but the approach I'll sketch doesn't count on that. You won't need anything other than box() and Polygon() from shapely.geometry.

对于每个像素,通过将像素边界与每个多边形的最小边界框进行比较,找出与其大致重叠的多边形.

For each pixel, find the polygons that approximately overlap with it by comparing the pixels bounds to the minimum bounding box of each polygon.

from shapely.geometry import box, Polygon

for pixel in pixels:
    # say the pixel has llx, lly, urx, ury values.
    pixel_shape = box(llx, lly, urx, ury)

    for polygon in approximately_overlapping:
        # say the polygon has a ``value`` and a 2-D array of coordinates 
        # [[x0,y0],...] named ``xy``.
        polygon_shape = Polygon(xy)
        pixel_value += polygon_shape.intersection(pixel_shape).area * value

如果像素和多边形不相交,则它们的相交区域将为0,并且该多边形对该像素的贡献消失.

If the pixel and polygon don't intersect, the area of their intersection will be 0 and the contribution of that polygon to that pixel vanishes.

这篇关于数据分箱:将不规则的多边形转换为规则的网格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆