GoLang Mongo GeoJSON(地理JSON)

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

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类型。我为你的情况创建了一个简单的程序,如下所示:

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 )

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:

确定