pbf

package module
v0.0.0-...-c4e4932 Latest Latest
Warning

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

Go to latest
Published: Oct 14, 2018 License: Apache-2.0 Imports: 16 Imported by: 0

README

pbf

OpenStreetMap PBF golang encoder/decoder

Build Status Go Report Card Documentation codecov.io License

A golang based OpenStreetMap PBF encoder/decoder with a handy command line utility, pbf.

pbf Command Line Utility

The pbf CLI can be installed using the go install command:

$ go install m4o.io/pbf/cmd/pbf
pbf info

The pbf CLI can be used to obtain summary and extended information about an OpenStreetMap PBF file:

$ pbf info -i testdata/greater-london.osm.pbf
BoundingBox: [-0.511482, 51.28554, 0.335437, 51.69344]
RequiredFeatures: OsmSchema-V0.6, DenseNodes
OptionalFeatures: 
WritingProgram: Osmium (http://wiki.openstreetmap.org/wiki/Osmium)
Source: 
OsmosisReplicationTimestamp: 2014-03-24T21:55:02Z
OsmosisReplicationSequenceNumber: 0
OsmosisReplicationBaseURL: 

JSON output can be obtained by adding the -j option:

$ pbf info -j -i testdata/greater-london.osm.pbf | jq
{
  "BoundingBox": {
    "Left": -0.511482,
    "Right": 0.33543700000000004,
    "Top": 51.69344,
    "Bottom": 51.285540000000005
  },
  "RequiredFeatures": [
    "OsmSchema-V0.6",
    "DenseNodes"
  ],
  "OptionalFeatures": null,
  "WritingProgram": "Osmium (http://wiki.openstreetmap.org/wiki/Osmium)",
  "Source": "",
  "OsmosisReplicationTimestamp": "2014-03-24T14:55:02-07:00",
  "OsmosisReplicationSequenceNumber": 0,
  "OsmosisReplicationBaseURL": ""
}

Here, jq is used to pretty print the compact JSON.

Extended information about the OpenStreetMap PBF file can be obtained by using the -e option. This causes the entire file to be scanned, which can take a very long time; a progress bar is displayed on stderr.

$ pbf info -e -i testdata/greater-london.osm.pbf
BoundingBox: [-0.511482, 51.28554, 0.335437, 51.69344]
RequiredFeatures: OsmSchema-V0.6, DenseNodes
OptionalFeatures: 
WritingProgram: Osmium (http://wiki.openstreetmap.org/wiki/Osmium)
Source: 
OsmosisReplicationTimestamp: 2014-03-24T21:55:02Z
OsmosisReplicationSequenceNumber: 0
OsmosisReplicationBaseURL: 
NodeCount: 2,729,006
WayCount: 459,055
RelationCount: 12,833

Finally, pbf can read an OpenStreetMap PBF file from stdin:

$ cat testdata/greater-london.osm.pbf | pbf info -e

In this case, a progress bar is not displayed since there is no way to know, a priori, what the size of the PBF file is.

Documentation

Overview

Example
package main

import (
	"context"
	"fmt"
	"io"
	"log"
	"os"

	parser "m4o.io/pbf"
)

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

	const size = 3 * 1024 * 1024
	d, err := parser.NewDecoder(context.Background(), in,
		parser.WithProtoBufferSize(size),
		parser.WithNCpus(2))
	if err != nil {
		log.Fatal(err)
	}
	defer d.Close()

	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 *parser.Node:
				// Process Node v.
				nc++
			case *parser.Way:
				// Process Way v.
				wc++
			case *parser.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 (
	// DefaultBufferSize is the default buffer size for protobuf un-marshaling
	DefaultBufferSize = 1024 * 1024

	// DefaultInputChannelLength is the default channel length of raw blobs
	DefaultInputChannelLength = 16

	// DefaultOutputChannelLength is the default channel length of decoded arrays of element
	DefaultOutputChannelLength = 8

	// DefaultDecodedChannelLength is the default channel length of decoded elements coalesced from output channels
	DefaultDecodedChannelLength = 8000
)
View Source
const (
	Degree Degrees = 1
	Radian         = (180 / math.Pi) * Degree

	E5 Epsilon = 1e-5
	E6 Epsilon = 1e-6
	E7 Epsilon = 1e-7
	E8 Epsilon = 1e-8
	E9 Epsilon = 1e-9
)

Degrees units.

Variables

This section is empty.

Functions

func DefaultNCpu

func DefaultNCpu() uint16

DefaultNCpu provides the default number of CPUs.

Types

type Angle

type Angle s1.Angle

Angle represents a 1D angle in radians.

func (Angle) EqualWithin

func (d Angle) EqualWithin(o Angle, eps Epsilon) bool

EqualWithin checks if two angles are within a specific epsilon.

type BoundingBox

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

BoundingBox is simply a bounding box.

func (BoundingBox) Contains

func (b BoundingBox) Contains(lon Degrees, lat Degrees) bool

Contains checks if the bounding box contains the lon lat point.

func (BoundingBox) EqualWithin

func (b BoundingBox) EqualWithin(o BoundingBox, eps Epsilon) bool

EqualWithin checks if two bounding boxes are within a specific epsilon.

func (BoundingBox) String

func (b BoundingBox) String() string

type Decoder

type Decoder struct {
	Header Header
	// contains filtered or unexported fields
}

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

func NewDecoder

func NewDecoder(ctx context.Context, reader io.Reader, opts ...DecoderOption) (*Decoder, error)

NewDecoder returns a new decoder, configured with cfg, that reads from reader. The decoder is initialized with the OSM header.

func (*Decoder) Close

func (d *Decoder) Close()

Close will cancel the background decoding pipeline.

func (*Decoder) Decode

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

Decode reads the next OSM object 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.

type DecoderOption

type DecoderOption func(*decoderOptions)

DecoderOption configures how we set up the decoder.

func WithDecodedChannelLength

func WithDecodedChannelLength(l int) DecoderOption

WithDecodedChannelLength lets you set the channel length of decoded elements coalesced from output channels.

func WithInputChannelLength

func WithInputChannelLength(l int) DecoderOption

WithInputChannelLength lets you set the channel length of raw blobs.

func WithNCpus

func WithNCpus(n uint16) DecoderOption

WithNCpus lets you set the number of CPUs to use for background processing.

func WithOutputChannelLength

func WithOutputChannelLength(l int) DecoderOption

WithOutputChannelLength lets you set the channel length of decoded arrays of element.

func WithProtoBufferSize

func WithProtoBufferSize(s int) DecoderOption

WithProtoBufferSize lets you set the buffer size for protobuf un-marshaling.

type Degrees

type Degrees float64

Degrees is the decimal degree representation of a longitude or latitude.

func ParseDegrees

func ParseDegrees(s string) (Degrees, error)

ParseDegrees converts a string to a Degrees instance.

func (Degrees) Angle

func (d Degrees) Angle() Angle

Angle returns the equivalent s1.Angle.

func (Degrees) E5

func (d Degrees) E5() int32

E5 returns the angle in hundred thousandths of degrees.

func (Degrees) E6

func (d Degrees) E6() int32

E6 returns the angle in millionths of degrees.

func (Degrees) E7

func (d Degrees) E7() int32

E7 returns the angle in ten millionths of degrees.

func (Degrees) EqualWithin

func (d Degrees) EqualWithin(o Degrees, eps Epsilon) bool

EqualWithin checks if two degrees are within a specific epsilon.

func (Degrees) String

func (d Degrees) String() string

type ElementType

type ElementType int

ElementType is an enumeration of relation types.

const (
	// NODE denotes that the member is a node
	NODE ElementType = iota

	// WAY denotes that the member is a way
	WAY

	// RELATION denotes that the member is a relation
	RELATION
)

func (ElementType) String

func (i ElementType) String() string

type Epsilon

type Epsilon float64

Epsilon is an enumeration of precisions that can be used when comparing Degrees.

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

Header is the contents of the OpenStreetMap PBF data file.

type Info

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

Info represents information common to Node, Way, and Relation elements.

type Member

type Member struct {
	ID   uint64
	Type ElementType
	Role string
}

Member represents an element that

type Node

type Node struct {
	ID   uint64
	Tags map[string]string
	Info *Info
	Lat  Degrees
	Lon  Degrees
}

Node represents a specific point on the earth's surface defined by its latitude and longitude. Each node comprises at least an id number and a pair of coordinates.

type Relation

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

Relation is a multi-purpose data structure that documents a relationship between two or more data elements (nodes, ways, and/or other relations).

type Way

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

Way is an ordered list of between 2 and 2,000 nodes that define a polyline.

Directories

Path Synopsis
cmd
pbf
Package protobuf is a generated protocol buffer package.
Package protobuf is a generated protocol buffer package.

Jump to

Keyboard shortcuts

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