有效地计算列表中每个多边形的中心

Calculate center for each polygon in a list efficiently

对于我的有限元分析输入数据的小数据挖掘项目,我已将 SQL 查询的结果导出到一个长的 python 元素角列表(三角形等) .这是一些示例数据:

import numpy as np
#element_id, coordinates, data

corners = [(1, [-7.374797, -885.3285, 33.665], 1.0),
 (1, [-427.427897, -965.4985, 596.2296], 1.0), 
 (1, [-81.743197, -126.5385, 286.8912], 1.0),
 (2, [-22.248597, -878.7285, 111.239], 0.35), 
 (1, [-74.307097, -126.5385, 272.1152], 1.0),
 (2, [-74.307097, -126.5385, 286.8912], 0.35), 
 (2, [-81.743197, -126.5385, 286.8912], 0.35),  
 (3, [0.062103, -562.0245, 81.687], 1.25)]

其中列表中每个元组的第一个值是一个元素的id,第二个值是一个角的坐标。为了进一步处理,我需要每个 element 的中心,最好是在一个 numpy 数组中,连同数据。

由于 table 相当长(~3Mio.Lines),我正在寻找一种有效的算法来计算每个元素角点坐标的平均值。

到目前为止,我的计划是按以下方式更新 numpy 数组的第 i 个元素:

x = np.zeros((3,4)) #initialize array
for c in corners:
    x[c[0],1:] = (x[c[0],0]*x[c[0],1:] + c[1])/(x[c[0],0]+1) #recursive formula for mean
    x[c[0],0] +=1

不幸的是,这有几个问题:

  1. 速度很慢
  2. 我需要知道元素
  3. 的最高和最低 ID(例如,当我 select 只是一个子集时)
  4. 如果 id 范围有间隙,我会得到 [0.,0.,0.,0.] 行。(例如,当我有 element_ids 2,4,7...)

是否有任何快速灵活的方法可以做到这一点,也许使用已经存在的 numpy 函数?什么是好的数据类型? 这在 PostgreSQL 中直接完成更好吗?

提前致谢。

I'm am looking for an efficient algorithm to calculate the the average of the coordinates of the corners for each element.

马上在 Postgres 中做:

SELECT element_id
     , ARRAY[avg(coordinates[1]), avg(coordinates[2]), avg(coordinates[3])]
FROM   tbl -- or your org. query here
GROUP  BY 1;

应该然后首先导出未聚合的数据(更多行)然后处理它。