-
Notifications
You must be signed in to change notification settings - Fork 0
/
tile_creator.go
145 lines (122 loc) · 3.28 KB
/
tile_creator.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
package main
import (
"flag"
"fmt"
"io"
"log"
"math"
"net/http"
"os"
"path/filepath"
"strconv"
"sync"
"sync/atomic"
"time"
"github.com/schollz/progressbar/v3"
)
const (
TileURLTemplate = "https://services.arcgisonline.com/ArcGIS/rest/services/World_Imagery/MapServer/tile/%d/%d/%d.png"
MaxRetries = 3
WorkerPoolSize = 64
AvgTileSizeKB = 50 // Average tile size in KB for estimation
)
type Task struct {
Zoom int
X int
Y int
}
func degToNum(lat, lon float64, zoom int) (int, int) {
n := math.Pow(2, float64(zoom))
x := int((lon + 180.0) / 360.0 * n)
y := int((1.0 - math.Log(math.Tan(lat*math.Pi/180.0)+1.0/math.Cos(lat*math.Pi/180.0))/math.Pi) / 2.0 * n)
return x, y
}
func downloadTile(task Task, outputDir string, progress *progressbar.ProgressBar, totalSize *uint64) error {
url := fmt.Sprintf(TileURLTemplate, task.Zoom, task.Y, task.X)
savePath := filepath.Join(outputDir, strconv.Itoa(task.Zoom), strconv.Itoa(task.X), fmt.Sprintf("%d.png", task.Y))
if _, err := os.Stat(savePath); err == nil {
progress.Add(1)
return nil
}
if err := os.MkdirAll(filepath.Dir(savePath), os.ModePerm); err != nil {
return err
}
for attempt := 1; attempt <= MaxRetries; attempt++ {
resp, err := http.Get(url)
if err == nil && resp.StatusCode == http.StatusOK {
file, err := os.Create(savePath)
if err != nil {
resp.Body.Close()
return err
}
defer file.Close()
n, err := io.Copy(file, resp.Body)
resp.Body.Close()
if err == nil {
atomic.AddUint64(totalSize, uint64(n))
progress.Add(1)
return nil
}
}
time.Sleep(2 * time.Second)
}
return fmt.Errorf("failed to download tile: Zoom %d, X %d, Y %d", task.Zoom, task.X, task.Y)
}
func main() {
maxZoom := flag.Int("zoom", 5, "Highest zoom level (e.g., 5 means zoom levels 0 to 5)")
minLat := flag.Float64("minlat", 16.5, "Minimum latitude")
maxLat := flag.Float64("maxlat", 26.5, "Maximum latitude")
minLon := flag.Float64("minlon", 51.8, "Minimum longitude")
maxLon := flag.Float64("maxlon", 60.0, "Maximum longitude")
outputDir := flag.String("output", "./tiles", "Output directory for downloaded tiles")
flag.Parse()
zoomLevels := make([]int, *maxZoom+1)
for i := 0; i <= *maxZoom; i++ {
zoomLevels[i] = i
}
if err := os.MkdirAll(*outputDir, os.ModePerm); err != nil {
log.Fatalf("Failed to create output directory: %v", err)
}
var tasks []Task
for _, zoom := range zoomLevels {
xMin, yMin := degToNum(*maxLat, *minLon, zoom)
xMax, yMax := degToNum(*minLat, *maxLon, zoom)
maxTile := 1 << zoom
if xMin < 0 {
xMin = 0
}
if xMax >= maxTile {
xMax = maxTile - 1
}
if yMin < 0 {
yMin = 0
}
if yMax >= maxTile {
yMax = maxTile - 1
}
for x := xMin; x <= xMax; x++ {
for y := yMin; y <= yMax; y++ {
tasks = append(tasks, Task{Zoom: zoom, X: x, Y: y})
}
}
}
progress := progressbar.NewOptions(len(tasks))
var totalSize uint64
jobs := make(chan Task, len(tasks))
var wg sync.WaitGroup
for i := 0; i < WorkerPoolSize; i++ {
wg.Add(1)
go func() {
defer wg.Done()
for task := range jobs {
_ = downloadTile(task, *outputDir, progress, &totalSize)
}
}()
}
for _, task := range tasks {
jobs <- task
}
close(jobs)
wg.Wait()
log.Printf("Download complete. Total size: %.2f MB", float64(totalSize)/1024/1024)
}