我正在使用MongoDB处理地理空间Web应用程序。我在有不同类别的集合(COUNTRY
,STATE
等)上有很多多边形,我想知道哪个是某个STATE
的COUNTRY
,但在某些情况下,邻居COUNTRY
的边框正在触摸STATE
所以当我查询交叉口时,我得到2个国家。多边形重叠百分比
我想计算州和这两个国家之间的重叠百分比,以知道哪一个是父级。我一直在寻找,但是我没有找到任何这类操作的库,我做这种算法并不是很好。
编辑:添加更多的上下文
这是我与
type GeoEntity struct {
ID bson.ObjectId `json:"id" bson:"_id"`
Type string `json:"type" bson:"type"` // COUNTRY, STATE, etc.
Geometry Geometry `json:"geometry" bson:"geometry"`
}
// GeoJSON entity
type Geometry struct {
Type string `json:"type" bson:"type"`
Coordinates [][][][]float64 `json:"coordinates" bson:"coordinates"`
}
工作模式这是代码块我现在所拥有的:
func findParent(state *GeoEntity) GeoEntity{
session, err := mgo.Dial("localhost")
check(err)
defer session.Close()
entities := session.DB("geo").C("entity")
query := bson.M{
"geometry": bson.M{
"$geoIntersects": bson.M{
"$geometry": state.Geometry,
},
},
"type": "COUNTRY",
}
var countries []GeoEntity
err = entities.Find(query).All(&countries)
check(err)
var parent GeoEntity
if len(countries) > 1 {
//TODO: parent = findTheTrueParent(countries, state)
} else {
parent = countries[0]
}
return parent
}
And here is an image example of the problem I'm having.当我作出查询时,我得到两个国家,红色和绿色的,但真正的父母是绿色的没有。
有相当的golang几个地理空间库,但不知道你是如何存储数据,或者是什么工作,你已经完成的工作,这个问题太笼统。 – Andrew
在使用Go之前,您是否尝试过使用简单的MongoDB查询? –
是的,这正是我所展示的,mongo查询返回2个父母,因为邻居国家的边界与州的边界重叠 –