在Python中将数百万元组存储在内存中的有效方法是什么?

时间:2017-02-18 23:14:35

标签: python memory coordinates quadtree

我正在尝试解析包含xyz坐标的多个大型.txt文件(实际上是.xyz文件)。每个文件中都有数百万行,其中每行显示一个以逗号分隔的坐标(xyz)。我只想保留特定边界框内的某些坐标。找到这些坐标后,我想让他们做多个更具体的查找。因此,我想通过四叉树将它们存储在空间索引中。

所有文件的打开和阅读显然是花费时间,但更糟糕的是我遇到了严重的记忆问题。瓶颈似乎是将包含当前坐标和边界框的元组插入到四叉树中。处理完一些文件后,我的虚拟内存最高可达10GB甚至更多。

到目前为止,我尝试使用新的.txt文件,因此我不必将所有内容保存在内存中。但实际上这并不快。我也尝试使用sqlite数据库,但这也没有做到。这些尝试的缺点还在于我通过四叉树丢失了空间索引。有什么办法可以坚持四叉树尝试并降低内存消耗吗?

def pointcloud_thin(log, xyz_files, bbox):

   # ... 
   x_min, y_min = bbox_points[0]
   x_max, y_max = bbox_points[2]

   # Using a quertree to store values from new pointcloud for better performance
   # (xmin, ymin, xmax, ymax)
   spindex = Index(bbox=(x_min, y_min, x_max, y_max))

   for i, file in enumerate(xyz_files):
       with open(file) as f:
           for line in f:
               try:
                   #xyz = list(map(float, line.split(',')))
                   x, y, z = line.split(",")
                   if (float(x) >= x_min and float(x) <= x_max and float(y) >= y_min and float(y) <= y_max):
                       tup = (float(x), float(y), float(z))
                       spindex.insert(tup, (float(x), float(y), float(x), float(y)))
                       #new_file.write(x + "," + y + "," + z) # txt file
                       #pointcloud.save([float(x), float(y), float(z)]) # sqlite3
                   else:
                       pass
               except ValueError:
                   continue

   return spindex

0 个答案:

没有答案