Created
August 28, 2018 08:46
-
-
Save s-l-teichmann/76913be951a00df1408005eccbd98dce to your computer and use it in GitHub Desktop.
Using json.Marshaler in GeoJSON
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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