使用rethinkdb计算多边形的面积

时间:2016-05-10 09:50:43

标签: rethinkdb

我希望对小于某个区域的多边形进行过滤。这可以使用rethinkdb实现而不事先计算区域吗?

1 个答案:

答案 0 :(得分:2)

我使用了一些自己的数据,但是这个的一小部分应该可行。在这里,我提供了一个过滤函数来计算区域并返回truefalse,具体取决于它比REQ_AREA更重要。您还可以创建一个索引,通过将匿名函数传递给.createIndex('area', function(doc) { ... }然后使用该索引执行getAll来自动执行此计算。

.sliceprepend | appending只是旋转x和y坐标以实现更简单的乘法映射。

这里的ReQL区域计算看起来像:

r.db('geography').table('area_polygons').filter((doc) => {

  // Retrieve just the points of the polygon
  var coords = doc('polygon').toGeojson()('coordinates').nth(0)

  var x_coords = coords.map((point) => {return point.nth(0)}).coerceTo('array');
  var y_coords = coords.map((point) => {return point.nth(1)}).coerceTo('array');

  // Move item from beginning to end
    y_coords = y_coords.append(y_coords.slice(0,1).nth(0)).deleteAt(0);
  var x = r.map(x_coords, y_coords, (l, r) => { return l.mul(r) }).sum();

  // Reset y and now move first x item to end
  y_coords = y_coords.prepend(y_coords.slice(-1).nth(0)).deleteAt(-1);
  x_coords = x_coords.append(x_coords.slice(0,1).nth(0)).deleteAt(0);
  var y = r.map(x_coords, y_coords, (l, r) => { return l.mul(r) }).sum();

  // Return area
  return x.sub(y).div(2) > REQ_SIZE ? true : false;
})