-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
196 lines (152 loc) · 3.94 KB
/
main.go
File metadata and controls
196 lines (152 loc) · 3.94 KB
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
package main
import (
"fmt"
"io"
"math"
"os"
"gopkg.in/ini.v1"
)
type TileInfo struct {
Lv int
X1 int
X2 int
Y1 int
Y2 int
}
// var Bound1 = [2]float64{37.4966612, 126.8259632}
// var Bound2 = [2]float64{37.2095794, 127.1892489}
var Bound1 [2]float64
var Bound2 [2]float64
var rootpath string = ""
var destroot string = ""
var stz int = 6
var endz int = 15
// var stz int = 19
// var endz int = 19
// var stz int
// var endz int
// ------------------------------------------------------------------------------
// initEnvVaiable
// ------------------------------------------------------------------------------
func initEnvVaiable() bool {
fmt.Printf("initEnvVaiable...")
cfg, err := ini.Load("./conf.ini")
if err != nil {
fmt.Printf("fail to read sysenvini.ini %v", err)
return false
}
rootpath = cfg.Section("MAPINFO").Key("srcpath").String()
destroot = cfg.Section("MAPINFO").Key("destpath").String()
Bound1[0], _ = cfg.Section("MAPINFO").Key("lat1").Float64()
Bound1[1], _ = cfg.Section("MAPINFO").Key("lon1").Float64()
Bound2[0], _ = cfg.Section("MAPINFO").Key("lat2").Float64()
Bound2[1], _ = cfg.Section("MAPINFO").Key("lon2").Float64()
stz, _ = cfg.Section("MAPINFO").Key("startlv").Int()
endz, _ = cfg.Section("MAPINFO").Key("endlv").Int()
// fmt.Printf("Bound1 %v", Bound1)
// fmt.Printf("Bound2 %v", Bound2)
return true
}
func tiletoLong(x, z float64) float64 {
val := (x/math.Pow(2, z)*360 - 180)
return val
}
func tileToLat(y, z float64) float64 {
n := math.Pi - 2*math.Pi*y/math.Pow(2, z)
val := (180 / math.Pi * math.Atan(0.5*(math.Exp(n)-math.Exp(-n))))
return val
}
func initTileInfo() []TileInfo {
var t []TileInfo = make([]TileInfo, 20)
var x1 int = 52
var x2 int = 57
var y1 int = 22
var y2 int = 26
for z := 6; z <= endz; z++ {
t[z] = TileInfo{Lv: z, X1: x1, X2: x2, Y1: y1, Y2: y2}
x1 = x1 * 2
x2 = x2 * 2
y1 = y1*2 + 1
y2 = y2*2 + 1
}
return t
}
func copyfile(src, dstpath, filenm string) error {
if _, err := os.Stat(dstpath); os.IsNotExist(err) {
fmt.Printf("make dir..%v, %v, %v\n", src, dstpath, filenm)
err = os.MkdirAll(dstpath, 0775)
if err != nil {
return err
}
}
dst := fmt.Sprintf("%s/%s", dstpath, filenm)
in, err := os.Open(src)
if err != nil {
return err
}
defer in.Close()
out, err := os.Create(dst)
if err != nil {
return err
}
defer func() {
cerr := out.Close()
if err == nil {
err = cerr
}
}()
if _, err = io.Copy(out, in); err != nil {
return err
}
err = out.Sync()
if err != nil {
return err
}
return nil
}
func checkRange(lat, lon float64) bool {
fmt.Printf("lat [%f], [%f], [%f]\n", lat, Bound1[0], Bound2[0])
fmt.Printf("lon [%f], [%f], [%f]\n", lon, Bound1[1], Bound2[1])
if lat <= Bound1[0] && lat >= Bound2[0] &&
lon >= Bound1[1] && lon <= Bound2[1] {
return true
}
return false
}
func main() {
initok := initEnvVaiable()
if !initok {
fmt.Printf("Map info initEnvVaiable fail..")
return
}
t := initTileInfo()
for z := stz; z <= endz; z++ {
x := t[z].X1
x2 := t[z].X2
for x <= x2 {
y := t[z].Y1
y2 := t[z].Y2
for y <= y2 {
filepath := fmt.Sprintf("%d/%d/%d.png", z, x, y)
path := fmt.Sprintf("%s/%s", rootpath, filepath)
fmt.Printf("path : [%s]\n", path)
lon := tiletoLong(float64(x), float64(z))
lat := tileToLat(float64(y), float64(z))
fmt.Printf("lat, lon :%v, %v\n", lat, lon)
if _, err := os.Stat(path); os.IsNotExist(err) {
fmt.Printf("Unexist z/x/y file ..%d, %d, %d\n", z, x, y)
} else {
destpath := fmt.Sprintf("%s/%d/%d", destroot, z, x)
filenm := fmt.Sprintf("%d.png", y)
// check range
if checkRange(lat, lon) {
fmt.Printf("copy..file %d, %d, %d \n", z, x, y)
copyfile(path, destpath, filenm)
}
}
y++
}
x++
}
}
}