我正在使用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個父母,因爲鄰居國家的邊界與州的邊界重疊 –