英文:
GoLang Mongo GeoJSON
问题
在Golang中,使用MongoDB,我正在尝试存储GeoJSON对象,并同时保持2dsphere索引。
我无法声明一个通用的结构体来处理"Point"和"Polygon",因为"Point"有一个[]float64
类型的坐标字段,而"Polygon"有一个[][]float64
类型的坐标字段。
你有任何关于如何声明这样的结构体的想法吗?
英文:
In Golang, with MongoDB, I am trying to store GeoJSON objects, while keeping a 2dsphere index.
I am unable to declare a generic struct that can handle both "Point" and "Polygon", since a "Point" have a []float64
coordinates field, while a "Polygon" have a [][]float64
coordinates field.
Do you have any idea on how to declare such a struct?
答案1
得分: 5
你可以尝试在结构体中将polygon
和point
的字段都定义为interface
类型。我为你的情况创建了一个简单的程序,如下所示:
package main
import (
"fmt"
)
type figure struct {
name string
coordinates interface{}
}
func main() {
Point := figure{"Point", [2]float64{2.0, 7.88}}
Polygon := figure{"Polygon", [2][2]float64{{2.0, 7.88}, {3.0, 7.88}}}
fmt.Println(Point)
fmt.Println(Polygon)
}
输出结果为:
{Point [2 7.88]}
{Polygon [[2 7.88] [3 7.88]]}
英文:
You can try using interface
as field for both polygon
and point
in your struct. I created a simple program for your scenario as follows:
package main
import (
"fmt"
)
type figure struct {
name string
coordinates interface{}
}
func main() {
Point := figure{"Point", [2]float64{2.0, 7.88}}
Polygon := figure{"Polygon", [2][2]float64{{2.0, 7.88}, {3.0, 7.88}}}
fmt.Println(Point)
fmt.Println(Polygon)
}
Output:
{Point [2 7.88]}
{Polygon [[2 7.88] [3 7.88]]}
答案2
得分: 0
首先,Ghoper的回答是**100%**正确的。包含Coordinates interface{}
对我帮助很大。对于将来访问这里的任何人,这是我用于参考的GeoJson Feature Collection结构布局:
// Feature Collection
type FeatureCollection struct {
ID string `json:"_id,omitempty" bson:"_id,omitempty"`
Features []Feature `json:"features" bson:"features"`
Type string `json:"type" bson:"type"`
}
// Individual Feature
type Feature struct {
Type string `json:"type" bson:"type"`
Properties Properties `json:"properties" bson:"properties"`
Geometry Geometry `json:"geometry" bson:"geometry"`
}
// Feature Properties
type Properties struct {
Name string `json:"name" bson:"name"`
Height uint64 `json:"height" bson:"height"`
Purchased bool `json:"purchased" bson:"purchased"`
LastUpdated string `json:"last_updated" bson:"last_updated"`
}
// Feature Geometry
type Geometry struct {
Type string `json:"type" bson:"type"`
Coordinates interface{} `json:"coordinates" bson:"coordinates"`
}
适用于所有的GeoJson类型(LineString、Point、Polygon、MultiPolygon)
英文:
Firstly, Ghoper's answer is 100% correct. The inclusion of Coordinates interface{}
helped me massively. For anyone who visits here in the future this is my GeoJson Feature Collection struct layout for refernce:
// Feature Collection
type FeatureCollection struct {
ID string `json:"_id,omitempty" bson:"_id,omitempty"`
Features []Feature `json:"features" bson:"features"`
Type string `json:"type" bson:"type"`
}
// Individual Feature
type Feature struct {
Type string `json:"type" bson:"type"`
Properties Properties `json:"properties" bson:"properties"`
Geometry Geometry `json:"geometry" bson:"geometry"`
}
// Feature Properties
type Properties struct {
Name string `json:"name" bson:"name"`
Height uint64 `json:"height" bson:"height"`
Purchased bool `json:"purchased" bson:"purchased"`
LastUpdated string `json:"last_updated" bson:"last_updated"`
}
// Feature Geometry
type Geometry struct {
Type string `json:"type" bson:"type"`
Coordinates interface{} `json:"coordinates" bson:"coordinates"`
}
Works for all GeoJson Types ( LineString, Point, Polygon, MultiPolygon )
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论