Extracting amenity tag info from OSM database in 3 simple steps

Step 1. Download the needed OSM database in PBF format from here: planet.openstreetmap.org (be careful, it has huge size, many GBs)

Step 2. Install golang package for for decoding osm pdf files: github.com/qedus/osmpbf

Step 3. Use simple golang program below to decode whatever you need (let’s say you need to find all petrol stations)


package main
import (
    “fmt”
    “io”
    “log”
    “os”
    “runtime”
    “github.com/qedus/osmpbf”
)
func main() {
    f, err := os.Open(“planet-latest.osm.pbf”)
    if err != nil {
        log.Fatal(err)
    }
    defer f.Close()
    d := osmpbf.NewDecoder(f)
    err = d.Start(runtime.GOMAXPROCS(1))
    if err != nil {
        log.Fatal(err)
    }
    count := 0
    tag2find := “fuel” // let’s find all petrol stations
    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.
                name := “”
                lat := 0.0
                lon := 0.0
                found := false
                for key, value := range v.Tags {
                    lat = v.Lat
                    lon = v.Lon
                    if key == “amenity” && value == tag2find {
                        found = true
                        count++
                    }
                    if key == “name” {
                        name = value
                    }
                }
                if found {
                    fmt.Printf(“\n\nNode lat: %.2f, long: %.2f\n”, lat, lon)
                    fmt.Println(“Name: ” + name)
                }
            }
        }
    }
    fmt.Printf(“Found petrol stations: %d\n”, count)
}

 

, , , , , , ,

  1. Leave a comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: