Skip to content

Instantly share code, notes, and snippets.

@rmarianski
Created July 20, 2015 21:43
Show Gist options
  • Save rmarianski/6c45723f3d90c9209f4f to your computer and use it in GitHub Desktop.
Save rmarianski/6c45723f3d90c9209f4f to your computer and use it in GitHub Desktop.
package main
import (
"fmt"
"github.com/qedus/osmpbf"
"io"
"log"
"os"
"runtime"
"time"
)
type CountRecord struct {
Uid int32
Timestamp time.Time
}
func main() {
nCPU := runtime.NumCPU()
runtime.GOMAXPROCS(nCPU)
if len(os.Args) != 2 {
fmt.Println("Pass in path to metro extract")
os.Exit(1)
}
f, err := os.Open(os.Args[1])
if err != nil {
log.Fatal(err)
}
defer f.Close()
d := osmpbf.NewDecoder(f)
err = d.Start(nCPU)
if err != nil {
log.Fatal(err)
}
countChan := make(chan *CountRecord, 255)
doneCounting := make(chan bool)
now := time.Now()
yearsAgo := 0
monthsAgo := 0
daysAgo := 90
cutoff := now.AddDate(-yearsAgo, -monthsAgo, -daysAgo)
users := make(map[int32]int)
nFeatures := 0
go func() {
for cr := range countChan {
if cutoff.After(cr.Timestamp) {
continue
}
users[cr.Uid]++
nFeatures++
}
doneCounting <- true
}()
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:
countChan <- &CountRecord{
Uid: v.Info.Uid,
Timestamp: v.Info.Timestamp,
}
case *osmpbf.Way:
countChan <- &CountRecord{
Uid: v.Info.Uid,
Timestamp: v.Info.Timestamp,
}
case *osmpbf.Relation:
countChan <- &CountRecord{
Uid: v.Info.Uid,
Timestamp: v.Info.Timestamp,
}
default:
log.Fatalf("unknown type %T\n", v)
}
}
}
close(countChan)
<-doneCounting
nUsers := len(users)
fmt.Printf("Active users: %d\n", nUsers)
fmt.Printf("Features updated: %d\n", nFeatures)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment