我正在将 OpenStreeMap 数据转储到 MongoDB 实例中,存在以下集合 nodes、ways 和 relations。
我正在查询给定地理空间点半径范围内的所有节点,并了解这些节点之间的关系我正在使用 ways 集合尝试检索包含来 self 之前的地理空间查询。
然后,我尝试使用它包含在字段 loc.nodes 中的节点 ID。连同 this answer 中提供的帮助我得到了以下代码:
package main
import (
"fmt"
mgo "gopkg.in/mgo.v2"
"gopkg.in/mgo.v2/bson"
)
// GeoJSON Holds data of geospatial points
type GeoJSON struct {
Type string `json:"-"`
Coordinates []float64 `json:"coordinates"`
}
type waynodes struct {
Type string
Coordinates []float64
Nodes []int
}
// OSMNode Represet a single point in space.
// https://wiki.openstreetmap.org/wiki/Node
//
// A node is one of the core elements in the OpenStreetMap data model. It
// consists of a single point in space defined by its latitude, longitude and
// node id. A third, optional dimension (altitude) can also be included:
// key:ele (abrev. for "elevation"). A node can also be defined as part of a
// particular layer=* or level=*, where distinct features pass over or under
// one another; say, at a bridge. Nodes can be used to define standalone point
// features, but are more often used to define the shape or "path" of a way.
type OSMNode struct {
ID int `bson:"_id"`
Location GeoJSON `bson:"loc"`
Tags map[string]interface{} `bson:"tags,omitempty"`
}
// OSMWay Represent an ordered list of nodes
// https://wiki.openstreetmap.org/wiki/Way
//
// A way is an ordered list of nodes which normally also has at least one tag
// or is included within a Relation. A way can have between 2 and 2,000 nodes,
// although it's possible that faulty ways with zero or a single node exist. A
// way can be open or closed. A closed way is one whose last node on the way is
// also the first on that way. A closed way may be interpreted either as a
// closed polyline, or an area, or both.
//
// The nodes defining the geometry of the way are enumerated in the correct
// order, and indicated only by reference using their unique identifier. These
// nodes must have been already defined separately with their coordinates.
type OSMWay struct {
ID int `bson:"_id"`
Location waynodes `bson:"loc"`
Tags map[string]interface{}
}
// km2miles convert a distance in kilometers to miles and then return the
// radius of such distance.
func km2miles(dist float64) float64 {
r := dist * 0.621371
// https://en.wikipedia.org/wiki/Earth_radius#Fixed_radius
return r / 3963.2
}
// nodes2list return a string list of node IDs from a list of OSMNode objects
func nodes2list(l []OSMNode) []int {
var list []int
for _, v := range l {
list = append(list, v.ID)
}
return list
}
// GetGeoWithinPos Return all points in a given point of Earth within the
// radius of `dist`.
func (db *DB) GetGeoWithinPos(long, lat, dist float64) ([]OSMWay, error) {
// Look at `nodes` document in our `osm` database
c := db.m.DB("osm").C("nodes")
// Query all nodes within a range from a spatial point: It should be
// equivalent to:
// db.nodes.find(
// {loc:
// {$geoWithin:
// {$centerSphere: [[-83.4995983, 10.1033002], 0.186411 / 3963.2]
// }
// }
// }, {"_id": 1});
var nodesresult []OSMNode
err := c.Find(bson.M{
"loc": bson.M{
"$geoWithin": bson.M{
"$centerSphere": []interface{}{
[]interface{}{long, lat}, km2miles(dist),
},
},
},
}).Select(bson.M{"_id": 1}).All(&nodesresult)
if err != nil {
return nil, err
} else if nodesresult == nil {
return nil, fmt.Errorf("Nodes not found on %f lat, %f long in a radius of %f km", lat, long, dist)
} else if nodesresult[0].ID == 0 {
return nil, fmt.Errorf("Nodes incorrectly unmarshall: %#v", nodesresult[0:3])
}
// Prepare a pipeline
pipe := []bson.M{
{
// Match only ways that contains the ID of the nodes
// from the query on `qsn`
"$match": bson.M{
"loc.nodes": bson.M{
"$in": nodes2list(nodesresult), // Return []int
},
},
},
{
// Now look for the nodes at `nodes` collection present
// at `loc.nodes` field...
"$lookup": bson.M{
"from": "nodes",
"localField": "loc.nodes",
"foreignField": "_id",
"as": "loc.coordinates",
},
},
{
// ...and set the field `loc.coordinates` with the
// coordinates of all nodes.
"$addField": bson.M{
"loc.coordinates": bson.M{
"$reduce": bson.M{
"input": "$loc.coordinates.loc.coordinates",
"initialValue": []float64{},
"in": bson.M{"$concatArrays": []string{"$$this", "$$value"}},
},
},
},
},
}
// Query ways collection
w := db.m.DB("osm").C("ways")
var ways []OSMWay
// Execute the pipeline ?
err = w.Pipe(pipe).All(&ways)
if ways == nil {
return nil, fmt.Errorf("Ways not found within %0.2f km/radius (%f mil/radius)", dist, km2miles(dist))
}
return ways, err
}
但是最后的管道什么都不返回。
$ go test
--- FAIL: TestFetchData (1.80s)
db_test.go:16: from -83.4995983long, 10.1033002lat: Ways not found within 1.00 km/radius (0.000157 mil/radius)
我想知道我在这里做错了什么以及为什么 mgo 不能做我想做的事。
为了完整起见,这里是测试定义:
func TestFetchData(t *testing.T) {
db, err := NewDBConn("", "", "localhost", 27017)
if err != nil {
t.Fatalf("Could not establish connection with MongoDB: %s", err)
}
// Get data from some location in my hometown
_, err := db.GetGeoWithinPos(-83.4995983, 10.1033002, 1.0)
if err != nil {
t.Fatalf("from -83.4995983long, 10.1033002lat: %s", err)
}
}
这是来自 ways 集合的示例文档:
{
"_id":492464922,
"tags":{
"maxspeed":"20",
"surface":"asphalt",
"highway":"residential",
"oneway":"yes",
"name":"Avenida 1"
},
"loc":{
"type":"Polygon",
"coordinates":[
],
"nodes":[
445848963,
4844871065,
432568566
]
}
}
这将是来自 nodes 集合的示例文档:
{
"_id":445848963,
"loc":{
"type":"Point",
"coordinates":[
-83.5047254,
10.0984515
]
}
}
这将是我希望通过我尝试传递给管道的查询返回的示例输出:
{
"_id":492464922,
"tags":{
"maxspeed":"20",
"surface":"asphalt",
"highway":"residential",
"oneway":"yes",
"name":"Avenida 1"
},
"loc":{
"type":"Polygon",
"coordinates":[
-83.5047254,
10.0984515,
-83.5052237,
10.0987132,
-83.5056339,
10.0989286
],
"nodes":[
445848963,
4844871065,
432568566
]
}
}
最佳答案
这是因为您的聚合管道中存在拼写错误。运算符(operator)称为 $addFields不是 $addField(缺少 s)。
w.Pipe() 的方法调用应该会引发类似Unrecognized pipeline stage name: '$addField' 的错误。但是,您的代码没有检查 Pipe() 返回的 err 变量。由于您只检查变量 ways 由于错误而为 nil,因此您的方法返回 (nil, "Ways not found within %0.2f km/radius (%f mil/radius )");从而掩盖管道错误。
我建议先检查内容检查 err:
err = w.Pipe(pipe).All(&ways)
if err != nil {
//handle error
}
关于mongodb - 带有聚合的 mgo,使用另一个查询和字段更改进行过滤,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48492358/
我正在学习如何使用Nokogiri,根据这段代码我遇到了一些问题:require'rubygems'require'mechanize'post_agent=WWW::Mechanize.newpost_page=post_agent.get('http://www.vbulletin.org/forum/showthread.php?t=230708')puts"\nabsolutepathwithtbodygivesnil"putspost_page.parser.xpath('/html/body/div/div/div/div/div/table/tbody/tr/td/div
我有一个Ruby程序,它使用rubyzip压缩XML文件的目录树。gem。我的问题是文件开始变得很重,我想提高压缩级别,因为压缩时间不是问题。我在rubyzipdocumentation中找不到一种为创建的ZIP文件指定压缩级别的方法。有人知道如何更改此设置吗?是否有另一个允许指定压缩级别的Ruby库? 最佳答案 这是我通过查看rubyzip内部创建的代码。level=Zlib::BEST_COMPRESSIONZip::ZipOutputStream.open(zip_file)do|zip|Dir.glob("**/*")d
类classAprivatedeffooputs:fooendpublicdefbarputs:barendprivatedefzimputs:zimendprotecteddefdibputs:dibendendA的实例a=A.new测试a.foorescueputs:faila.barrescueputs:faila.zimrescueputs:faila.dibrescueputs:faila.gazrescueputs:fail测试输出failbarfailfailfail.发送测试[:foo,:bar,:zim,:dib,:gaz].each{|m|a.send(m)resc
很好奇,就使用rubyonrails自动化单元测试而言,你们正在做什么?您是否创建了一个脚本来在cron中运行rake作业并将结果邮寄给您?git中的预提交Hook?只是手动调用?我完全理解测试,但想知道在错误发生之前捕获错误的最佳实践是什么。让我们理所当然地认为测试本身是完美无缺的,并且可以正常工作。下一步是什么以确保他们在正确的时间将可能有害的结果传达给您? 最佳答案 不确定您到底想听什么,但是有几个级别的自动代码库控制:在处理某项功能时,您可以使用类似autotest的内容获得关于哪些有效,哪些无效的即时反馈。要确保您的提
假设我做了一个模块如下:m=Module.newdoclassCendend三个问题:除了对m的引用之外,还有什么方法可以访问C和m中的其他内容?我可以在创建匿名模块后为其命名吗(就像我输入“module...”一样)?如何在使用完匿名模块后将其删除,使其定义的常量不再存在? 最佳答案 三个答案:是的,使用ObjectSpace.此代码使c引用你的类(class)C不引用m:c=nilObjectSpace.each_object{|obj|c=objif(Class===objandobj.name=~/::C$/)}当然这取决于
我正在尝试使用ruby和Savon来使用网络服务。测试服务为http://www.webservicex.net/WS/WSDetails.aspx?WSID=9&CATID=2require'rubygems'require'savon'client=Savon::Client.new"http://www.webservicex.net/stockquote.asmx?WSDL"client.get_quotedo|soap|soap.body={:symbol=>"AAPL"}end返回SOAP异常。检查soap信封,在我看来soap请求没有正确的命名空间。任何人都可以建议我
关闭。这个问题是opinion-based.它目前不接受答案。想要改进这个问题?更新问题,以便editingthispost可以用事实和引用来回答它.关闭4年前。Improvethisquestion我想在固定时间创建一系列低音和高音调的哔哔声。例如:在150毫秒时发出高音调的蜂鸣声在151毫秒时发出低音调的蜂鸣声200毫秒时发出低音调的蜂鸣声250毫秒的高音调蜂鸣声有没有办法在Ruby或Python中做到这一点?我真的不在乎输出编码是什么(.wav、.mp3、.ogg等等),但我确实想创建一个输出文件。
我在我的项目目录中完成了compasscreate.和compassinitrails。几个问题:我已将我的.sass文件放在public/stylesheets中。这是放置它们的正确位置吗?当我运行compasswatch时,它不会自动编译这些.sass文件。我必须手动指定文件:compasswatchpublic/stylesheets/myfile.sass等。如何让它自动运行?文件ie.css、print.css和screen.css已放在stylesheets/compiled。如何在编译后不让它们重新出现的情况下删除它们?我自己编译的.sass文件编译成compiled/t
我正在用Ruby编写一个简单的程序来检查域列表是否被占用。基本上它循环遍历列表,并使用以下函数进行检查。require'rubygems'require'whois'defcheck_domain(domain)c=Whois::Client.newc.query("google.com").available?end程序不断出错(即使我在google.com中进行硬编码),并打印以下消息。鉴于该程序非常简单,我已经没有什么想法了-有什么建议吗?/Library/Ruby/Gems/1.8/gems/whois-2.0.2/lib/whois/server/adapters/base.
我想将html转换为纯文本。不过,我不想只删除标签,我想智能地保留尽可能多的格式。为插入换行符标签,检测段落并格式化它们等。输入非常简单,通常是格式良好的html(不是整个文档,只是一堆内容,通常没有anchor或图像)。我可以将几个正则表达式放在一起,让我达到80%,但我认为可能有一些现有的解决方案更智能。 最佳答案 首先,不要尝试为此使用正则表达式。很有可能你会想出一个脆弱/脆弱的解决方案,它会随着HTML的变化而崩溃,或者很难管理和维护。您可以使用Nokogiri快速解析HTML并提取文本:require'nokogiri'h