GoLang Mongo GeoJSON(地理JSON)

huangapple go评论140阅读模式
英文:

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

你可以尝试在结构体中将polygonpoint的字段都定义为interface类型。我为你的情况创建了一个简单的程序,如下所示:

  1. package main
  2. import (
  3. "fmt"
  4. )
  5. type figure struct {
  6. name string
  7. coordinates interface{}
  8. }
  9. func main() {
  10. Point := figure{"Point", [2]float64{2.0, 7.88}}
  11. Polygon := figure{"Polygon", [2][2]float64{{2.0, 7.88}, {3.0, 7.88}}}
  12. fmt.Println(Point)
  13. fmt.Println(Polygon)
  14. }

输出结果为:

  1. {Point [2 7.88]}
  2. {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:

  1. package main
  2. import (
  3. "fmt"
  4. )
  5. type figure struct {
  6. name string
  7. coordinates interface{}
  8. }
  9. func main() {
  10. Point := figure{"Point", [2]float64{2.0, 7.88}}
  11. Polygon := figure{"Polygon", [2][2]float64{{2.0, 7.88}, {3.0, 7.88}}}
  12. fmt.Println(Point)
  13. fmt.Println(Polygon)
  14. }

Output:

  1. {Point [2 7.88]}
  2. {Polygon [[2 7.88] [3 7.88]]}

答案2

得分: 0

首先,Ghoper的回答是**100%**正确的。包含Coordinates interface{}对我帮助很大。对于将来访问这里的任何人,这是我用于参考的GeoJson Feature Collection结构布局:

  1. // Feature Collection
  2. type FeatureCollection struct {
  3. ID string `json:"_id,omitempty" bson:"_id,omitempty"`
  4. Features []Feature `json:"features" bson:"features"`
  5. Type string `json:"type" bson:"type"`
  6. }
  7. // Individual Feature
  8. type Feature struct {
  9. Type string `json:"type" bson:"type"`
  10. Properties Properties `json:"properties" bson:"properties"`
  11. Geometry Geometry `json:"geometry" bson:"geometry"`
  12. }
  13. // Feature Properties
  14. type Properties struct {
  15. Name string `json:"name" bson:"name"`
  16. Height uint64 `json:"height" bson:"height"`
  17. Purchased bool `json:"purchased" bson:"purchased"`
  18. LastUpdated string `json:"last_updated" bson:"last_updated"`
  19. }
  20. // Feature Geometry
  21. type Geometry struct {
  22. Type string `json:"type" bson:"type"`
  23. Coordinates interface{} `json:"coordinates" bson:"coordinates"`
  24. }

适用于所有的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:

  1. // Feature Collection
  2. type FeatureCollection struct {
  3. ID string `json:"_id,omitempty" bson:"_id,omitempty"`
  4. Features []Feature `json:"features" bson:"features"`
  5. Type string `json:"type" bson:"type"`
  6. }
  7. // Individual Feature
  8. type Feature struct {
  9. Type string `json:"type" bson:"type"`
  10. Properties Properties `json:"properties" bson:"properties"`
  11. Geometry Geometry `json:"geometry" bson:"geometry"`
  12. }
  13. // Feature Properties
  14. type Properties struct {
  15. Name string `json:"name" bson:"name"`
  16. Height uint64 `json:"height" bson:"height"`
  17. Purchased bool `json:"purchased" bson:"purchased"`
  18. LastUpdated string `json:"last_updated" bson:"last_updated"`
  19. }
  20. // Feature Geometry
  21. type Geometry struct {
  22. Type string `json:"type" bson:"type"`
  23. Coordinates interface{} `json:"coordinates" bson:"coordinates"`
  24. }

Works for all GeoJson Types ( LineString, Point, Polygon, MultiPolygon )

huangapple
  • 本文由 发表于 2021年6月2日 22:31:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/67807153.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定