-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
154 lines (133 loc) · 3.03 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
package main
import (
"flag"
"fmt"
"os"
"os/exec"
"strings"
)
type element string
const (
api = "https://www.openstreetmap.org/api/0.6"
node element = "node"
way element = "way"
relation element = "relation"
)
func main() {
file := flag.String("f", "", "output file.")
nids := flag.String("n", "", "node id list.")
wids := flag.String("w", "", "way id list.")
rids := flag.String("r", "", "relation id list.")
flag.Parse()
if *file == "" {
fmt.Printf("parameter error, missing -f\n")
usage()
os.Exit(-1)
}
if *nids == "" && *wids == "" && *rids == "" {
fmt.Printf("parameter error, missing -n or -w or -r\n")
usage()
os.Exit(-1)
}
var files []string
if *nids != "" {
f := download(node, nids)
files = append(files, f...)
}
if *wids != "" {
f := download(way, wids)
files = append(files, f...)
}
if *rids != "" {
f := download(relation, rids)
files = append(files, f...)
}
fmt.Printf("---------------------------------------------\n")
if len(files) == 0 {
fmt.Printf("nothing was downloaded\n")
os.Exit(-1)
}
var args []string
for _, f := range files {
args = append(args, "--read-xml")
args = append(args, f)
}
for i := 0; i < len(files)-1; i++ {
args = append(args, "--merge")
}
args = append(args, "--write-xml")
args = append(args, *file)
fmt.Printf("generate %v\n", *file)
cmd := exec.Command("osmosis", args...)
if err := cmd.Run(); err != nil {
fmt.Printf("failed to merge *.osm, err: %v\n", err)
clear(files)
os.Exit(-1)
}
clear(files)
fmt.Printf("done in success\n")
os.Exit(0)
}
func download(e element, ids *string) []string {
if *ids == "" {
return nil
}
var (
succes []string
failed []string
)
defer func() {
clear(failed)
}()
idArray := strings.Split(*ids, ",")
for _, id := range idArray {
url := fmt.Sprintf("%v/%v/%v", api, e, id)
if e == way || e == relation {
url += "/full"
}
fmt.Printf("%-15s%-15s", e, id)
f := fmt.Sprintf("%v%v.osm", e, id)
cmd := exec.Command("wget", url, "-O", f)
if err := cmd.Run(); err != nil {
fmt.Printf("%-15s\n", "[not found]")
failed = append(failed, f)
continue
}
if isEmptyFile(f) {
fmt.Printf("%-15s\n", "[not found]")
failed = append(failed, f)
continue
}
fmt.Printf("%-15s\n", "[ok]")
succes = append(succes, f)
}
return succes
}
func isEmptyFile(file string) bool {
fi, err := os.Stat(file)
if err != nil {
return false
}
return fi.Size() == 0
}
func clear(files []string) {
for _, file := range files {
if _, err := os.Stat(file); err != nil {
fmt.Printf("failed to remove %v, err: %v\n", files, err)
continue
}
if err := os.Remove(file); err != nil {
fmt.Printf("failed to remove %v, err: %v\n", file, err)
continue
}
}
}
func usage() {
fmt.Printf("-----------------------------\n")
fmt.Printf("Usage\n")
fmt.Printf("example: osmdownloader -f test.osm -n 111,222 -w 333,444 -r 555,666\n")
fmt.Printf("-f\tthe output file, required\n")
fmt.Printf("-n\tnode id list\n")
fmt.Printf("-w\tway id list\n")
fmt.Printf("-r\trelation id list\n")
}