Skip to content

Instantly share code, notes, and snippets.

@s-l-teichmann
Created August 28, 2018 08:46
Show Gist options
  • Save s-l-teichmann/76913be951a00df1408005eccbd98dce to your computer and use it in GitHub Desktop.
Save s-l-teichmann/76913be951a00df1408005eccbd98dce to your computer and use it in GitHub Desktop.
Using json.Marshaler in GeoJSON
package main
import (
"encoding/json"
"fmt"
"log"
"os"
)
//type Point2D [2]float64
type Point2D struct {
X float64
Y float64
}
type Properties struct {
// ...
}
type Feature struct {
Type string `json:"type"`
Geometry *map[string]interface{} `json:"geometry"`
Properties *Properties `json:"properties,omitempty"`
}
type Geometry struct {
// the geometry type
Type string `json:"type"`
}
type Point struct {
// the geometry type
Type string `json:"type"`
Coordinates *Point2D `json:"coordinates,omitempty"`
}
func (p *Point2D) MarshalJSON() ([]byte, error) {
return []byte(fmt.Sprintf("[%.6f,%.6f]", p.X, p.Y)), nil
}
func main() {
feature := Feature{
Type: "Feature",
Geometry: &map[string]interface{}{
"type": "Point",
"geometry": &Point2D{12.3567, 89.0123},
},
}
enc := json.NewEncoder(os.Stdout)
enc.SetIndent("", " ")
if err := enc.Encode(&feature); err != nil {
log.Fatalf("error: %v\n", err)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment