2015-08-28 34 views
1

我有一個Line對象中RethinkDB需要計算長度線公里RethinkDB

文檔

我要找計算該行的現實世界距離的簡單/高效到底。

我最初的策略是拉線從數據庫中,並計算出它裏面的Node.js但最好想辦法「盒子」

我無法找到一個方法來遍歷做到這一點ReQl內部線的座標並使用距離函數進行計算。

任何幫助,非常感謝。

+0

嘿帕特撕裂,你找到了不同的答案? – dalanmiller

回答

1

不幸的是,我可以找到如何做到這一點的唯一方法,因爲r.line對象內的點似乎不可訪問,因此將該行轉換爲GeoJSON,轉換爲點,然後返回計算結果。

使用這樣一個對象:

{ 
    id: 101, 
    route: r.line([-122.423246,37.779388], [-121.886420,37.329898]) 
} 

您的查詢應該是這樣的:

r.table('data').get(101)('route').do(function(doc){ 

    var points = doc.toGeojson()("coordinates").map(function(point){ 
    return r.point(point(0), point(1)); 
    }); 

    return { 
    "distance": points(0).distance(points(1)) 
    } 

}); 

或者,因爲你可能有一大堆線的距離來計算:

r.table('data').hasFields("route")('route').map(function(doc){ 
    var points = doc.toGeojson()("coordinates").map(function(point){ 
    return r.point(point(0), point(1)); 
    }); 

    return { 
    line: points(0).distance(points(1)) 
    } 
}); 

I've also submitted a Github issue to see if we can find an improvement!