osmfile

package module
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Dec 18, 2021 License: MIT Imports: 17 Imported by: 0

README

osmfile

A downloader and reader for the OSM planet files.

Features

  • Download the lastest planet files.
  • Get a list of mirrors serving specific planet files.
  • Stop and resume downloads.
  • Includes an OSM PBF parser.
  • Read and process PBF data while download is in process.

Using

Download the Go package.

go get -u github.com/tidwall/osmfile
Examples

Get the latest known osm planet files in order of most recently created.

names, err := osmfile.Latest()
if err != nil {
	panic(err)
}
for _, name := range names {
	fmt.Printf("%s\n", name)
}

// Output something like:
// planet-210329.osm.pbf
// planet-210322.osm.pbf
// planet-210315.osm.pbf
// planet-210301.osm.pbf
// planet-210201.osm.pbf
// planet-210104.osm.pbf

Get a list of the mirror urls.

// using a valid planet file name.
name := "planet-210329.osm.pbf"

mirrors, err := osmfile.Mirrors(name)
if err != nil {
	panic(err)
}
for _, mirror := range mirrors {
	fmt.Printf("%s\n", mirror)
}

// Output something like:
// https://free.nchc.org.tw/osm.planet/pbf/planet-210329.osm.pbf
// https://ftp.fau.de/osm-planet/pbf/planet-210329.osm.pbf
// https://ftp5.gwdg.de/pub/misc/openstreetmap/planet.openstreetmap.org/pbf/planet-210329.osm.pbf
// https://planet.osm-hr.org/pbf/planet-210329.osm.pbf

Download the planet file to disk.


// Using a valid mirror url. 
mirrorURL := "https://ftp.fau.de/osm-planet/pbf/planet-210329.osm.pbf"

// Start downloading. The downloading happens in a background, and will
// continue until an error occurs or the downloader is explicitly stopped
// with the Stop() function.

dl := osmfile.Download(mirrorURL, "planet.pbf")

// Here we will download the file and print a status every second.
for {
	status := dl.Status()
	if status.Size > 0 {
		fmt.Printf("%.2f%% %d / %d MB Downloaded\n",
			float64(status.Downloaded)/float64(status.Size)*100,
			status.Downloaded/1024/1024,
			status.Size/1024/1024)
		if status.Done {
			// The downloader is done due to success or failure.
			break
		}
	}
	if status.Done {
		// The downloader is done due to success or failure.
		break
	}
	time.Sleep(time.Second)
}
// Check if the download failed
if err := dl.Error(); err != nil {
	panic(err)
}

Here's a complete example that downloads the latest planet file from a random mirror and parses PBF data at the same time.

package main

import (
	"fmt"
	"io"
	"math/rand"

	"github.com/tidwall/osmfile"
)

func main() {
	names, err := osmfile.Latest()
	if err != nil {
		panic(err)
	}
	mirrors, err := osmfile.Mirrors(names[0])
	if err != nil {
		panic(err)
	}

	rand.Seed(time.Now().UnixNano())
	url := mirrors[rand.Int()%len(mirrors)]
	fmt.Printf("downloading %s\n", url)

	dl := osmfile.Download(url, names[0])
	defer dl.Stop()

	rd := dl.Reader()
	defer rd.Close()

	brd := osmfile.NewBlockReader(rd)
	var nodes, ways, relations int
	for num := 0; ; num++ {
		// Read and parse the next OSM PBF block. 
		// You can choose to skip the parsing by SkipBlock instead of ReadBlock.
		n, block, err := brd.ReadBlock()
		if err != nil {
			if err == io.EOF {
				// No more blocks
				break
			}
			panic(err)
		}

		// The block variable now contains all of the OSM data belonging to
		// the current PBF block.
		
		// Let's just print some basic info and the download status
		nodes += block.NumNodes()
		ways += block.NumWays()
		relations += block.NumRelations()

		status := dl.Status()
		fmt.Printf("BLOCK #%d (%s, %d bytes) ",
			num, block.DataKind(), n)
		fmt.Printf("(nodes: %d, ways: %d, rels: %d) ",
			nodes, ways, relations)
		fmt.Printf("(%d/%d MB, %.1f%%)\n",
			status.Downloaded/1024/1024,
			status.Size/1024/1024,
			float64(status.Downloaded)/float64(status.Size)*100)
	}
	// Check if the download failed
	if err := dl.Error(); err != nil {
		panic(err)
	}

	// Yay! the entire OSM file was download and every block parsed.
}

Documentation

Index

Constants

This section is empty.

Variables

View Source
var AllMirrors = []string{
	"https://ftp.spline.de/pub/openstreetmap/pbf/",
	"https://ftp5.gwdg.de/pub/misc/openstreetmap/planet.openstreetmap.org/pbf/",
	"https://ftp.fau.de/osm-planet/pbf/",
	"https://free.nchc.org.tw/osm.planet/pbf/",
	"https://ftpmirror.your.org/pub/openstreetmap/pbf/",
	"https://download.bbbike.org/osm/planet/",
	"https://ftp.nluug.nl/maps/planet.openstreetmap.org/pbf/",
	"https://ftp.osuosl.org/pub/openstreetmap/pbf/",
	"https://planet.passportcontrol.net/pbf/",
	"https://planet.osm-hr.org/pbf/",
}

AllMirrors are a list of all known mirrors

Functions

func Latest

func Latest() (names []string, err error)

Latest returns the latest (most recent) planet names on the primary OSM server.

func Mirrors

func Mirrors(name string) (mirrors []string, err error)

Mirrors returns a list of OSM mirror urls that are hosting the planet file for the provide name.

Types

type Block

type Block struct {
	// contains filtered or unexported fields
}

Block ...

func (Block) DataKind

func (b Block) DataKind() DataKind

DataKind ...

func (Block) NodeAt

func (b Block) NodeAt(index int) Node

NodeAt ...

func (Block) NumNodes

func (b Block) NumNodes() int

NumNodes ...

func (Block) NumRelations

func (b Block) NumRelations() int

NumRelations ...

func (Block) NumStrings

func (b Block) NumStrings() int

NumStrings ...

func (Block) NumWays

func (b Block) NumWays() int

NumWays ...

func (Block) RelationAt

func (b Block) RelationAt(index int) Relation

RelationAt ...

func (Block) StringAt

func (b Block) StringAt(index int) string

StringAt ...

func (Block) WayAt

func (b Block) WayAt(index int) Way

WayAt ...

type BlockReader

type BlockReader struct {
	// contains filtered or unexported fields
}

BlockReader is a reader for reading OSMData blocks from an OSM Planet protobuf file.

func NewBlockReader

func NewBlockReader(r io.Reader) *BlockReader

NewBlockReader returns a reader for reading OSMData blocks from an OSM Planet protobuf file.

func (*BlockReader) ReadBlock

func (r *BlockReader) ReadBlock() (n int, block Block, err error)

ReadBlock reads the next OSMData block. Returns the number of bytes read and the block.

func (*BlockReader) SkipBlock

func (r *BlockReader) SkipBlock() (n int, err error)

SkipBlock skips over the next block. Like ReadBlock but faster.

type DataKind

type DataKind int
const (
	DataKindNodes     DataKind = 0
	DataKindWays      DataKind = 1
	DataKindRelations DataKind = 2
)

func (DataKind) String

func (k DataKind) String() string

type DownloadStatus

type DownloadStatus struct {
	Done       bool
	Path       string
	Downloaded int64
	Size       int64
}

DownloadStatus ...

type Downloader

type Downloader interface {
	Error() error
	Status() DownloadStatus
	Reader() io.ReadCloser
	Stop()
}

Downloader ...

func Download

func Download(planetURL string, path string) Downloader

Download the OSM planet file into the provide file path.

type Node

type Node struct {
	// contains filtered or unexported fields
}

Node ...

func (Node) ID

func (n Node) ID() int64

ID ...

func (Node) Lat

func (n Node) Lat() float64

Lat ...

func (Node) Lon

func (n Node) Lon() float64

Lon ...

func (Node) NumStrings

func (n Node) NumStrings() int

NumStrings ...

func (Node) StringAt

func (n Node) StringAt(index int) string

StringAt ...

type Relation

type Relation struct {
	// contains filtered or unexported fields
}

Relation ..

func (Relation) ID

func (r Relation) ID() int64

ID ...

func (Relation) MemberAt

func (r Relation) MemberAt(index int) (typ byte, ref int64, role string)

MemberAt ...

func (Relation) NumMembers

func (r Relation) NumMembers() int

NumMembers ...

func (Relation) NumStrings

func (r Relation) NumStrings() int

NumStrings ...

func (Relation) StringAt

func (r Relation) StringAt(index int) string

StringAt ...

type Way

type Way struct {
	// contains filtered or unexported fields
}

Way ...

func (Way) ID

func (w Way) ID() int64

ID ...

func (Way) NumRefs

func (w Way) NumRefs() int

NumRefs ...

func (Way) NumStrings

func (w Way) NumStrings() int

NumStrings ...

func (Way) RefAt

func (w Way) RefAt(index int) int64

RefAt ...

func (Way) StringAt

func (w Way) StringAt(index int) string

StringAt ...

type What

type What int

What do you want to parse?

const (
	Everything What = iota
	DataKinds       // for only detecting block data kinds
	Strings         // for processing all strings
	Nodes           // for process all nodes
	Ways            // for processing all ways
	Relations       // for processing all relations
)

What options

Directories

Path Synopsis
internal
pbf

Jump to

Keyboard shortcuts

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