44 "fmt"
55
66 "github.com/paulmach/orb"
7+ "go.mongodb.org/mongo-driver/bson"
78)
89
910// A Feature corresponds to GeoJSON feature object
@@ -37,19 +38,30 @@ var _ orb.Pointer = &Feature{}
3738// It will handle the encoding of all the child geometries.
3839// Alternately one can call json.Marshal(f) directly for the same result.
3940func (f Feature ) MarshalJSON () ([]byte , error ) {
40- jf := & jsonFeature {
41+ return marshalJSON (newFeatureDoc (& f ))
42+ }
43+
44+ // MarshalBSON converts the feature object into the proper JSON.
45+ // It will handle the encoding of all the child geometries.
46+ // Alternately one can call json.Marshal(f) directly for the same result.
47+ func (f Feature ) MarshalBSON () ([]byte , error ) {
48+ return bson .Marshal (newFeatureDoc (& f ))
49+ }
50+
51+ func newFeatureDoc (f * Feature ) * featureDoc {
52+ doc := & featureDoc {
4153 ID : f .ID ,
4254 Type : "Feature" ,
4355 Properties : f .Properties ,
4456 BBox : f .BBox ,
4557 Geometry : NewGeometry (f .Geometry ),
4658 }
4759
48- if len (jf .Properties ) == 0 {
49- jf .Properties = nil
60+ if len (doc .Properties ) == 0 {
61+ doc .Properties = nil
5062 }
5163
52- return marshalJSON ( jf )
64+ return doc
5365}
5466
5567// UnmarshalFeature decodes the data into a GeoJSON feature.
@@ -67,39 +79,54 @@ func UnmarshalFeature(data []byte) (*Feature, error) {
6779// UnmarshalJSON handles the correct unmarshalling of the data
6880// into the orb.Geometry types.
6981func (f * Feature ) UnmarshalJSON (data []byte ) error {
70- jf := & jsonFeature {}
71- err := unmarshalJSON (data , & jf )
82+ doc := & featureDoc {}
83+ err := unmarshalJSON (data , & doc )
84+ if err != nil {
85+ return err
86+ }
87+
88+ return featureUnmarshalFinish (doc , f )
89+ }
90+
91+ // UnmarshalBSON will unmarshal a BSON document created with bson.Marshal.
92+ func (f * Feature ) UnmarshalBSON (data []byte ) error {
93+ doc := & featureDoc {}
94+ err := bson .Unmarshal (data , & doc )
7295 if err != nil {
7396 return err
7497 }
7598
76- if jf .Type != "Feature" {
77- return fmt .Errorf ("geojson: not a feature: type=%s" , jf .Type )
99+ return featureUnmarshalFinish (doc , f )
100+ }
101+
102+ func featureUnmarshalFinish (doc * featureDoc , f * Feature ) error {
103+ if doc .Type != "Feature" {
104+ return fmt .Errorf ("geojson: not a feature: type=%s" , doc .Type )
78105 }
79106
80107 var g orb.Geometry
81- if jf .Geometry != nil {
82- if jf .Geometry .Coordinates == nil && jf .Geometry .Geometries == nil {
108+ if doc .Geometry != nil {
109+ if doc .Geometry .Coordinates == nil && doc .Geometry .Geometries == nil {
83110 return ErrInvalidGeometry
84111 }
85- g = jf .Geometry .Geometry ()
112+ g = doc .Geometry .Geometry ()
86113 }
87114
88115 * f = Feature {
89- ID : jf .ID ,
90- Type : jf .Type ,
91- Properties : jf .Properties ,
92- BBox : jf .BBox ,
116+ ID : doc .ID ,
117+ Type : doc .Type ,
118+ Properties : doc .Properties ,
119+ BBox : doc .BBox ,
93120 Geometry : g ,
94121 }
95122
96123 return nil
97124}
98125
99- type jsonFeature struct {
100- ID interface {} `json:"id,omitempty"`
101- Type string `json:"type"`
102- BBox BBox `json:"bbox,omitempty"`
103- Geometry * Geometry `json:"geometry"`
104- Properties Properties `json:"properties"`
126+ type featureDoc struct {
127+ ID interface {} `json:"id,omitempty" bson:"id" `
128+ Type string `json:"type" bson:"type" `
129+ BBox BBox `json:"bbox,omitempty" bson:"bbox,omitempty" `
130+ Geometry * Geometry `json:"geometry" bson:"geometry" `
131+ Properties Properties `json:"properties" bson:"properties" `
105132}
0 commit comments