osmpbf

package module
v0.0.0-...-1ed960e Latest Latest
Warning

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

Go to latest
Published: Jun 23, 2017 License: MIT Imports: 10 Imported by: 0

README

osmpbf

Go Report Card GoDoc

Package osmpbf is used to decode OpenStreetMap pbf files.

Installation

$ go get github.com/HellButcher/osmpbf

Usage

Usage is similar to json.Decoder.

	f, err := os.Open("greater-london-140324.osm.pbf")
	if err != nil {
		log.Fatal(err)
	}
	defer f.Close()

	d := osmpbf.NewDecoder(f)

	// use more memory from the start, it is faster
	d.SetBufferSize(osmpbf.MaxBlobSize)

	// start decoding with several goroutines, it is faster
	err = d.Start(runtime.GOMAXPROCS(-1))
	if err != nil {
		log.Fatal(err)
	}

	var nc, wc, rc uint64
	for {
		if v, err := d.Decode(); err == io.EOF {
			break
		} else if err != nil {
			log.Fatal(err)
		} else {
			switch v := v.(type) {
			case *osmpbf.Node:
				// Process Node v.
				nc++
			case *osmpbf.Way:
				// Process Way v.
				wc++
			case *osmpbf.Relation:
				// Process Relation v.
				rc++
			default:
				log.Fatalf("unknown type %T\n", v)
			}
		}
	}

	fmt.Printf("Nodes: %d, Ways: %d, Relations: %d\n", nc, wc, rc)

Documentation

http://godoc.org/github.com/HellButcher/osmpbf

To Do

The parseNodes code has not been tested as I can only find PBF files with DenseNode format.

An Encoder still needs to be created to reverse the process.

Documentation

Overview

Package osmpbf decodes OpenStreetMap (OSM) PBF files. Use this package by creating a NewDecoder and passing it a PBF file. Use Start to start decoding process. Use Decode to return Node, Way and Relation structs.

Example
package main

import (
	"fmt"
	"io"
	"log"
	"os"
	"runtime"

	"github.com/HellButcher/osmpbf"
)

func main() {
	f, err := os.Open("greater-london-140324.osm.pbf")
	if err != nil {
		log.Fatal(err)
	}
	defer f.Close()

	d := osmpbf.NewDecoder(f)

	// use more memory from the start, it is faster
	d.SetBufferSize(osmpbf.MaxBlobSize)

	// start decoding with several goroutines, it is faster
	err = d.Start(runtime.GOMAXPROCS(-1))
	if err != nil {
		log.Fatal(err)
	}

	var nc, wc, rc uint64
	for {
		if v, err := d.Decode(); err == io.EOF {
			break
		} else if err != nil {
			log.Fatal(err)
		} else {
			switch v := v.(type) {
			case *osmpbf.Node:
				// Process Node v.
				nc++
			case *osmpbf.Way:
				// Process Way v.
				wc++
			case *osmpbf.Relation:
				// Process Relation v.
				rc++
			default:
				log.Fatalf("unknown type %T\n", v)
			}
		}
	}

	fmt.Printf("Nodes: %d, Ways: %d, Relations: %d\n", nc, wc, rc)
}
Output:

Nodes: 2729006, Ways: 459055, Relations: 12833

Index

Examples

Constants

View Source
const (

	// MaxBlobSize is maximum supported blob size.
	MaxBlobSize = 32 * 1024 * 1024
)

Variables

This section is empty.

Functions

This section is empty.

Types

type BoundingBox

type BoundingBox struct {
	Left   float64
	Right  float64
	Top    float64
	Bottom float64
}

type Decoder

type Decoder struct {
	Options DecoderOptions
	// contains filtered or unexported fields
}

A Decoder reads and decodes OpenStreetMap PBF data from an input stream.

func NewDecoder

func NewDecoder(r io.Reader) *Decoder

NewDecoder returns a new decoder that reads from r.

func (*Decoder) Decode

func (dec *Decoder) Decode() (interface{}, error)

Decode reads the next object from the input stream and returns either a pointer to Node, Way or Relation struct representing the underlying OpenStreetMap PBF data, or error encountered. The end of the input stream is reported by an io.EOF error.

Decode is safe for parallel execution. Only first error encountered will be returned, subsequent invocations will return io.EOF.

func (*Decoder) Header

func (dec *Decoder) Header() (*Header, error)

Get the file header

func (*Decoder) SetBufferSize

func (dec *Decoder) SetBufferSize(n int)

SetBufferSize sets initial size of decoding buffer. Default value is 1MB, you can set higher value (for example, MaxBlobSize) for (probably) faster decoding, or lower value for reduced memory consumption. Any value will produce valid results; buffer will grow automatically if required.

func (*Decoder) Start

func (dec *Decoder) Start(n int) error

Start decoding process using n goroutines.

type DecoderOptions

type DecoderOptions struct {
	SkipNodes     bool
	SkipWays      bool
	SkipRelations bool
}

Options for controlling the decoder

type Header struct {
	BoundingBox                      *BoundingBox
	RequiredFeatures                 []string
	OptionalFeatures                 []string
	WritingProgram                   string
	Source                           string
	OsmosisReplicationTimestamp      time.Time
	OsmosisReplicationSequenceNumber int64
	OsmosisReplicationBaseUrl        string
}

type Info

type Info struct {
	Version   int32
	Timestamp time.Time
	Changeset int64
	Uid       int32
	User      string
	Visible   bool
}

type Member

type Member struct {
	ID   int64
	Type MemberType
	Role string
}

type MemberType

type MemberType int
const (
	NodeType MemberType = iota
	WayType
	RelationType
)

type Node

type Node struct {
	ID   int64
	Lat  float64
	Lon  float64
	Tags map[string]string
	Info Info
}

type Relation

type Relation struct {
	ID      int64
	Tags    map[string]string
	Members []Member
	Info    Info
}

type Way

type Way struct {
	ID      int64
	Tags    map[string]string
	NodeIDs []int64
	Info    Info
}

Directories

Path Synopsis
Package OSMPBF is a generated protocol buffer package.
Package OSMPBF is a generated protocol buffer package.

Jump to

Keyboard shortcuts

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