Last active
April 22, 2016 13:48
-
-
Save willnix/9e53be40337c11684247 to your computer and use it in GitHub Desktop.
Converts the JSON format used by "Zombies Run!" to GPX which can be imported in Runtastic.
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 ( | |
"fmt" | |
"os" | |
"time" | |
"encoding/json" | |
"encoding/xml" | |
) | |
type zombiesLog struct { | |
Gps []struct { | |
Altitude float64 `json:"altitude"` | |
HorizontalAccuracy float64 `json:"horizontal_accuracy"` | |
Latitude float64 `json:"latitude"` | |
Longitude float64 `json:"longitude"` | |
Time time.Time `json:"time"` | |
VerticalAccuracy float64 `json:"vertical_accuracy"` | |
} `json:"gps"` | |
StartTime time.Time `json:"start_time"` | |
} | |
type gpx struct { | |
Gpx struct { | |
Creator string `xml:"creator,attr"` | |
Version string `xml:"version,attr"` | |
Xsi string `xml:"xsi,attr"` | |
SchemaLocation string `xml:"schemaLocation,attr"` | |
Trk struct { | |
Name string `xml:"name"` | |
Trkseg struct { | |
Trkpt []trkPoints `xml:"trkpt"` | |
} `xml:"trkseg"` | |
} `xml:"trk"` | |
} `xml:"gpx"` | |
} | |
type trkPoints struct { | |
Lat string `xml:"lat,attr"` | |
Lon string `xml:"lon,attr"` | |
Ele string `xml:"ele"` | |
Time string `xml:"time"` | |
} | |
func main() { | |
// specify the input file as commandline argument | |
dat, err := os.Open(os.Args[1]) | |
check(err) | |
var zombiesJSON zombiesLog | |
err = json.NewDecoder(dat).Decode(&zombiesJSON) | |
check(err) | |
var runtasticGPX gpx | |
// fill header values | |
runtasticGPX.Gpx.Creator = "zombies2gpx" | |
runtasticGPX.Gpx.Version = "1.0" | |
runtasticGPX.Gpx.SchemaLocation = "http://www.topografix.com/GPX/1/1 http://www.topografix.com/GPX/1/1/gpx.xsd" | |
runtasticGPX.Gpx.Xsi = "http://www.w3.org/2001/XMLSchema-instance" | |
// array for the trackpoints | |
runtasticPoints := make([]trkPoints, len(zombiesJSON.Gps)) | |
// copy all trackpoints to the GPX xml stuct | |
for index, trackpoint := range zombiesJSON.Gps { | |
runtasticPoints[index] = trkPoints{ | |
Lat: fmt.Sprintf("%f", trackpoint.Latitude), | |
Lon: fmt.Sprintf("%f", trackpoint.Longitude), | |
Ele: fmt.Sprintf("%f", trackpoint.Altitude), | |
Time: trackpoint.Time.Format(time.RFC3339), | |
} | |
} | |
runtasticGPX.Gpx.Trk.Trkseg.Trkpt = runtasticPoints | |
enc := xml.NewEncoder(os.Stdout) | |
enc.Indent(" ", " ") | |
err = enc.Encode(runtasticGPX) | |
check(err) | |
} | |
func check(e error) { | |
if e != nil { | |
panic(e) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Great little tool! I've been looking for a way to extract the run logs out of the app, this (along with your blog post) was just what I needed - thanks!