使用 rethinkdb 计算多边形的面积
using rethinkdb to calculate the area of a polygon
我希望对小于特定区域的多边形进行过滤。这可以使用 rethinkdb 实现而不事先计算面积吗?
我用我自己的一些数据得到了这个,但它的一个小变化应该可以工作。在这里,我提供了一个过滤函数,它计算面积和 returns true
或 false
,具体取决于面积大于 REQ_AREA
。您还可以通过将匿名函数传递给 .createIndex('area', function(doc) { ... }
然后使用该索引执行 getAll
来创建自动执行此计算的索引。
.slice
和 prepend
|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;
})
我希望对小于特定区域的多边形进行过滤。这可以使用 rethinkdb 实现而不事先计算面积吗?
我用我自己的一些数据得到了这个,但它的一个小变化应该可以工作。在这里,我提供了一个过滤函数,它计算面积和 returns true
或 false
,具体取决于面积大于 REQ_AREA
。您还可以通过将匿名函数传递给 .createIndex('area', function(doc) { ... }
然后使用该索引执行 getAll
来创建自动执行此计算的索引。
.slice
和 prepend
|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;
})