gpx

package module
v1.3.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 24, 2024 License: MIT Imports: 8 Imported by: 18

README

go-gpx

GoDoc

Package gpx provides convenince methods for reading and writing GPX documents.

Read example

r := bytes.NewBufferString(`
    <?xml version="1.0" encoding="UTF-8"?>
    <gpx version="1.0" creator="ExpertGPS 1.1 - http://www.topografix.com" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.topografix.com/GPX/1/0" xsi:schemaLocation="http://www.topografix.com/GPX/1/0 http://www.topografix.com/GPX/1/0/gpx.xsd">
        <wpt lat="42.438878" lon="-71.119277">
        <ele>44.586548</ele>
        <time>2001-11-28T21:05:28Z</time>
        <name>5066</name>
        <desc>5066</desc>
        <sym>Crossing</sym>
        <type>Crossing</type>
        </wpt>
    </gpx>
`)
t, err := gpx.Read(r)
if err != nil {
    fmt.Printf("err == %v", err)
    return
}
fmt.Printf("t.Wpt[0] == %+v", t.Wpt[0])
// Output:
// t.Wpt[0] == &{Lat:42.438878 Lon:-71.119277 Ele:44.586548 Time:2001-11-28 21:05:28 +0000 UTC MagVar:0 GeoidHeight:0 Name:5066 Cmt: Desc:5066 Src: Link:[] Sym:Crossing Type:Crossing Fix: Sat:0 HDOP:0 VDOP:0 PDOP:0 AgeOfDGPSData:0 DGPSID:[] Extensions:<nil>}

Write example

g := &gpx.GPX{
    Version: "1.0",
    Creator: "ExpertGPS 1.1 - http://www.topografix.com",
    Wpt: []*gpx.WptType{
        {
            Lat:  42.438878,
            Lon:  -71.119277,
            Ele:  44.586548,
            Time: time.Date(2001, 11, 28, 21, 5, 28, 0, time.UTC),
            Name: "5066",
            Desc: "5066",
            Sym:  "Crossing",
            Type: "Crossing",
        },
    },
}
if _, err := os.Stdout.WriteString(xml.Header); err != nil {
    fmt.Printf("err == %v", err)
}
if err := g.WriteIndent(os.Stdout, "", "  "); err != nil {
    fmt.Printf("err == %v", err)
}
// Output:
// <?xml version="1.0" encoding="UTF-8"?>
// <gpx version="1.0" creator="ExpertGPS 1.1 - http://www.topografix.com" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.topografix.com/GPX/1/0" xsi:schemaLocation="http://www.topografix.com/GPX/1/0 http://www.topografix.com/GPX/1/0/gpx.xsd">
//   <wpt lat="42.438878" lon="-71.119277">
//     <ele>44.586548</ele>
//     <time>2001-11-28T21:05:28Z</time>
//     <name>5066</name>
//     <desc>5066</desc>
//     <sym>Crossing</sym>
//     <type>Crossing</type>
//   </wpt>
// </gpx>

License

MIT

Documentation

Overview

Package gpx provides convenience types for reading and writing GPX documents. See http://www.topografix.com/gpx.asp.

Index

Examples

Constants

This section is empty.

Variables

View Source
var StartElement = xml.StartElement{
	Name: xml.Name{Local: "gpx"},
}

StartElement is the XML start element for GPX files.

Functions

func MToTime added in v1.3.0

func MToTime(m float64) time.Time

func TimeToM added in v1.3.0

func TimeToM(t time.Time) float64

Types

type BoundsType

type BoundsType struct {
	MinLat float64 `xml:"minlat,attr"`
	MinLon float64 `xml:"minlon,attr"`
	MaxLat float64 `xml:"maxlat,attr"`
	MaxLon float64 `xml:"maxlon,attr"`
}

A BoundsType is a boundsType.

type CopyrightType

type CopyrightType struct {
	Author  string `xml:"author,attr"`
	Year    int    `xml:"year,omitempty"`
	License string `xml:"license,omitempty"`
}

A CopyrightType is a copyrightType.

func (*CopyrightType) UnmarshalXML added in v1.1.1

func (c *CopyrightType) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error

UnmarshalXML implements xml.Unmarshaler.UnmarshalXML.

type EmailType added in v1.3.1

type EmailType struct {
	Name   string `xml:"id,attr"`
	Domain string `xml:"domain,attr"`
}

An EmailType is an emailType.

type ExtensionsType

type ExtensionsType struct {
	XML []byte `xml:",innerxml"`
}

An ExtensionsType contains elements from another schema.

type GPX

type GPX struct {
	XMLName            string            `xml:"gpx"`
	XMLSchemaLocations []string          `xml:"xsi:schemaLocation,attr"`
	XMLAttrs           map[string]string `xml:"-"`
	Version            string            `xml:"version,attr"`
	Creator            string            `xml:"creator,attr"`
	Metadata           *MetadataType     `xml:"metadata,omitempty"`
	Wpt                []*WptType        `xml:"wpt,omitempty"`
	Rte                []*RteType        `xml:"rte,omitempty"`
	Trk                []*TrkType        `xml:"trk,omitempty"`
	Extensions         *ExtensionsType   `xml:"extensions,omitempty"`
}

A GPX is a gpxType.

func Read

func Read(r io.Reader) (*GPX, error)

Read reads a new GPX from r.

Example
package main

import (
	"bytes"
	"fmt"

	gpx "github.com/twpayne/go-gpx"
)

func main() {
	r := bytes.NewBufferString(`
		<?xml version="1.0" encoding="UTF-8"?>
		<gpx version="1.0" creator="ExpertGPS 1.1 - http://www.topografix.com" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.topografix.com/GPX/1/0" xsi:schemaLocation="http://www.topografix.com/GPX/1/0 http://www.topografix.com/GPX/1/0/gpx.xsd">
		  <wpt lat="42.438878" lon="-71.119277">
			<ele>44.586548</ele>
			<speed>9.16</speed>
			<time>2001-11-28T21:05:28Z</time>
			<name>5066</name>
			<desc>5066</desc>
			<sym>Crossing</sym>
			<type>Crossing</type>
		  </wpt>
		</gpx>
	`)
	t, err := gpx.Read(r)
	if err != nil {
		fmt.Printf("err == %v", err)
		return
	}
	fmt.Printf("t.Wpt[0] == %+v", t.Wpt[0])
}
Output:

t.Wpt[0] == &{Lat:42.438878 Lon:-71.119277 Ele:44.586548 Speed:9.16 Course:0 Time:2001-11-28 21:05:28 +0000 UTC MagVar:0 GeoidHeight:0 Name:5066 Cmt: Desc:5066 Src: Link:[] Sym:Crossing Type:Crossing Fix: Sat:0 HDOP:0 VDOP:0 PDOP:0 AgeOfDGPSData:0 DGPSID:[] Extensions:<nil>}

func (*GPX) MarshalXML

func (g *GPX) MarshalXML(e *xml.Encoder, _ xml.StartElement) error

MarshalXML implements xml.Marshaler.MarshalXML.

func (*GPX) Write

func (g *GPX) Write(w io.Writer) error

Write writes g to w.

func (*GPX) WriteIndent

func (g *GPX) WriteIndent(w io.Writer, prefix, indent string) error

WriteIndent writes g to w.

Example
package main

import (
	"encoding/xml"
	"fmt"
	"os"
	"time"

	gpx "github.com/twpayne/go-gpx"
)

func main() {
	g := &gpx.GPX{
		Version: "1.0",
		Creator: "ExpertGPS 1.1 - http://www.topografix.com",
		Wpt: []*gpx.WptType{
			{
				Lat:  42.438878,
				Lon:  -71.119277,
				Ele:  44.586548,
				Time: time.Date(2001, 11, 28, 21, 5, 28, 0, time.UTC),
				Name: "5066",
				Desc: "5066",
				Sym:  "Crossing",
				Type: "Crossing",
			},
		},
	}
	if _, err := os.Stdout.WriteString(xml.Header); err != nil {
		fmt.Printf("err == %v", err)
	}
	if err := g.WriteIndent(os.Stdout, "", "  "); err != nil {
		fmt.Printf("err == %v", err)
	}
}
Output:

<?xml version="1.0" encoding="UTF-8"?>
<gpx version="1.0" creator="ExpertGPS 1.1 - http://www.topografix.com" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.topografix.com/GPX/1/0" xsi:schemaLocation="http://www.topografix.com/GPX/1/0 http://www.topografix.com/GPX/1/0/gpx.xsd">
  <wpt lat="42.438878" lon="-71.119277">
    <ele>44.586548</ele>
    <time>2001-11-28T21:05:28Z</time>
    <name>5066</name>
    <desc>5066</desc>
    <sym>Crossing</sym>
    <type>Crossing</type>
  </wpt>
</gpx>

type LinkType

type LinkType struct {
	HREF string `xml:"href,attr"`
	Text string `xml:"text,omitempty"`
	Type string `xml:"type,omitempty"`
}

A LinkType is a linkType.

type MetadataType

type MetadataType struct {
	Name       string          `xml:"name,omitempty"`
	Desc       string          `xml:"desc,omitempty"`
	Author     *PersonType     `xml:"author,omitempty"`
	Copyright  *CopyrightType  `xml:"copyright,omitempty"`
	Link       []*LinkType     `xml:"link,omitempty"`
	Time       time.Time       `xml:"time,omitempty"`
	Keywords   string          `xml:"keywords,omitempty"`
	Bounds     *BoundsType     `xml:"bounds,omitempty"`
	Extensions *ExtensionsType `xml:"extensions,omitempty"`
}

A MetadataType is a metadataType.

type PersonType

type PersonType struct {
	Name  string     `xml:"name,omitempty"`
	Email *EmailType `xml:"email,omitempty"`
	Link  *LinkType  `xml:"link,omitempty"`
}

A PersonType is a personType.

type RteType

type RteType struct {
	Name       string          `xml:"name,omitempty"`
	Cmt        string          `xml:"cmt,omitempty"`
	Desc       string          `xml:"desc,omitempty"`
	Src        string          `xml:"src,omitempty"`
	Link       []*LinkType     `xml:"link,omitempty"`
	Number     int             `xml:"number,omitempty"`
	Type       string          `xml:"type,omitempty"`
	Extensions *ExtensionsType `xml:"extensions,omitempty"`
	RtePt      []*WptType      `xml:"rtept,omitempty"`
}

A RteType is a rteType.

func NewRteType

func NewRteType(g *geom.LineString) *RteType

NewRteType returns a new RteType with geometry g.

func (*RteType) Geom

func (r *RteType) Geom(layout geom.Layout) *geom.LineString

Geom returns r's geometry.

type TrkSegType

type TrkSegType struct {
	TrkPt      []*WptType      `xml:"trkpt,omitempty"`
	Extensions *ExtensionsType `xml:"extensions,omitempty"`
}

A TrkSegType is a trkSegType.

func NewTrkSegType

func NewTrkSegType(g *geom.LineString) *TrkSegType

NewTrkSegType returns a new TrkSegType with geometry g.

func (*TrkSegType) Geom

func (ts *TrkSegType) Geom(layout geom.Layout) *geom.LineString

Geom returns ts's geometry.

type TrkType

type TrkType struct {
	Name       string          `xml:"name,omitempty"`
	Cmt        string          `xml:"cmt,omitempty"`
	Desc       string          `xml:"desc,omitempty"`
	Src        string          `xml:"src,omitempty"`
	Link       []*LinkType     `xml:"link,omitempty"`
	Number     int             `xml:"number,omitempty"`
	Type       string          `xml:"type,omitempty"`
	Extensions *ExtensionsType `xml:"extensions,omitempty"`
	TrkSeg     []*TrkSegType   `xml:"trkseg,omitempty"`
}

A TrkType is a trkType.

func NewTrkType

func NewTrkType(g *geom.MultiLineString) *TrkType

NewTrkType returns a new TrkType with geometry g.

func (*TrkType) Geom

func (t *TrkType) Geom(layout geom.Layout) *geom.MultiLineString

Geom returns t's geometry.

type WptType

type WptType struct {
	Lat           float64         `xml:"lat,omitempty"`
	Lon           float64         `xml:"lon,omitempty"`
	Ele           float64         `xml:"ele,omitempty"`
	Speed         float64         `xml:"speed,omitempty"`
	Course        float64         `xml:"course,omitempty"`
	Time          time.Time       `xml:"time,omitempty"`
	MagVar        float64         `xml:"magvar,omitempty"`
	GeoidHeight   float64         `xml:"geoidheight,omitempty"`
	Name          string          `xml:"name,omitempty"`
	Cmt           string          `xml:"cmt,omitempty"`
	Desc          string          `xml:"desc,omitempty"`
	Src           string          `xml:"src,omitempty"`
	Link          []*LinkType     `xml:"link,omitempty"`
	Sym           string          `xml:"sym,omitempty"`
	Type          string          `xml:"type,omitempty"`
	Fix           string          `xml:"fix,omitempty"`
	Sat           int             `xml:"sat,omitempty"`
	HDOP          float64         `xml:"hdop,omitempty"`
	VDOP          float64         `xml:"vdop,omitempty"`
	PDOP          float64         `xml:"pdop,omitempty"`
	AgeOfDGPSData float64         `xml:"ageofdgpsdata,omitempty"`
	DGPSID        []int           `xml:"dgpsid,omitempty"`
	Extensions    *ExtensionsType `xml:"extensions,omitempty"`
}

A WptType is a wptType.

func NewWptType

func NewWptType(g *geom.Point) *WptType

NewWptType returns a new WptType with geometry g.

func (*WptType) Geom

func (w *WptType) Geom(layout geom.Layout) *geom.Point

Geom returns w's geometry.

func (*WptType) MarshalXML

func (w *WptType) MarshalXML(e *xml.Encoder, start xml.StartElement) error

MarshalXML implements xml.Marshaler.MarshalXML.

func (*WptType) UnmarshalXML

func (w *WptType) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error

UnmarshalXML implements xml.Unmarshaler.UnmarshalXML.

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL