Skip to content

Commit 58b6690

Browse files
author
atlasdatatech
committed
修复sqlite数据库初始化错误
1 parent f703f3f commit 58b6690

File tree

6 files changed

+60
-33
lines changed

6 files changed

+60
-33
lines changed

go.mod

+3-2
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,12 @@ go 1.15
55
require (
66
github.com/antonfisher/nested-logrus-formatter v1.3.0
77
github.com/mattn/go-runewidth v0.0.9 // indirect
8+
github.com/mattn/go-sqlite3 v1.14.19
89
github.com/paulmach/orb v0.1.6
9-
github.com/shiena/ansicolor v0.0.0-20200830101100-9405ca8e49f3
10+
github.com/shiena/ansicolor v0.0.0-20230509054315-a9deabde6e02
1011
github.com/sirupsen/logrus v1.6.0
1112
github.com/spf13/viper v1.7.1
12-
github.com/teris-io/shortid v0.0.0-20171029131806-771a37caa5cf
13+
github.com/teris-io/shortid v0.0.0-20220617161101-71ec9f2aa569
1314
golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd // indirect
1415
gopkg.in/cheggaaa/pb.v1 v1.0.28
1516
)

go.sum

+6-4
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,8 @@ github.com/mattn/go-isatty v0.0.3 h1:ns/ykhmWi7G9O+8a448SecJU3nSMBXJfqQkl0upE1jI
124124
github.com/mattn/go-isatty v0.0.3/go.mod h1:M+lRXTBqGeGNdLjl/ufCoiOlB5xdOkqRJdNxMWT7Zi4=
125125
github.com/mattn/go-runewidth v0.0.9 h1:Lm995f3rfxdpd6TSmuVCHVb/QhupuXlYr8sCI/QdE+0=
126126
github.com/mattn/go-runewidth v0.0.9/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m2gUSrubnMI=
127+
github.com/mattn/go-sqlite3 v1.14.19 h1:fhGleo2h1p8tVChob4I9HpmVFIAkKGpiukdrgQbWfGI=
128+
github.com/mattn/go-sqlite3 v1.14.19/go.mod h1:2eHXhiwb8IkHr+BDWZGa96P6+rkvnG63S2DGjv9HUNg=
127129
github.com/matttproud/golang_protobuf_extensions v1.0.1/go.mod h1:D8He9yQNgCq6Z5Ld7szi9bcBfOoFv/3dc6xSMkL2PC0=
128130
github.com/miekg/dns v1.0.14/go.mod h1:W1PPwlIAgtquWBMBEV9nkV9Cazfe8ScdGz/Lj7v3Nrg=
129131
github.com/mitchellh/cli v1.0.0/go.mod h1:hNIlj7HEI86fIcpObd7a0FcrxTWetlwJDGcceTlRvqc=
@@ -159,8 +161,8 @@ github.com/rogpeppe/fastuuid v0.0.0-20150106093220-6724a57986af/go.mod h1:XWv6So
159161
github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4=
160162
github.com/ryanuber/columnize v0.0.0-20160712163229-9b3edd62028f/go.mod h1:sm1tb6uqfes/u+d4ooFouqFdy9/2g9QGwK3SQygK0Ts=
161163
github.com/sean-/seed v0.0.0-20170313163322-e2103e2c3529/go.mod h1:DxrIzT+xaE7yg65j358z/aeFdxmN0P9QXhEzd20vsDc=
162-
github.com/shiena/ansicolor v0.0.0-20200830101100-9405ca8e49f3 h1:AutMBemBfeplnEpI2sXdLuvQKKyHixlKKSBFCcPLaE4=
163-
github.com/shiena/ansicolor v0.0.0-20200830101100-9405ca8e49f3/go.mod h1:nkxAfR/5quYxwPZhyDxgasBMnRtBZd0FCEpawpjMUFg=
164+
github.com/shiena/ansicolor v0.0.0-20230509054315-a9deabde6e02 h1:v9ezJDHA1XGxViAUSIoO/Id7Fl63u6d0YmsAm+/p2hs=
165+
github.com/shiena/ansicolor v0.0.0-20230509054315-a9deabde6e02/go.mod h1:RF16/A3L0xSa0oSERcnhd8Pu3IXSDZSK2gmGIMsttFE=
164166
github.com/sirupsen/logrus v1.2.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPxbbu5VWo=
165167
github.com/sirupsen/logrus v1.6.0 h1:UBcNElsrwanuuMsnGSlYmtmgbb23qDR5dG+6X6Oo89I=
166168
github.com/sirupsen/logrus v1.6.0/go.mod h1:7uNnSEd1DgxDLC74fIahvMZmmYsHGZGEOFrfsX/uA88=
@@ -187,8 +189,8 @@ github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0
187189
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
188190
github.com/subosito/gotenv v1.2.0 h1:Slr1R9HxAlEKefgq5jn9U+DnETlIUa6HfgEzj0g5d7s=
189191
github.com/subosito/gotenv v1.2.0/go.mod h1:N0PQaV/YGNqwC0u51sEeR/aUtSLEXKX9iv69rRypqCw=
190-
github.com/teris-io/shortid v0.0.0-20171029131806-771a37caa5cf h1:Z2X3Os7oRzpdJ75iPqWZc0HeJWFYNCvKsfpQwFpRNTA=
191-
github.com/teris-io/shortid v0.0.0-20171029131806-771a37caa5cf/go.mod h1:M8agBzgqHIhgj7wEn9/0hJUZcrvt9VY+Ln+S1I5Mha0=
192+
github.com/teris-io/shortid v0.0.0-20220617161101-71ec9f2aa569 h1:xzABM9let0HLLqFypcxvLmlvEciCHL7+Lv+4vwZqecI=
193+
github.com/teris-io/shortid v0.0.0-20220617161101-71ec9f2aa569/go.mod h1:2Ly+NIftZN4de9zRmENdYbvPQeaVIYKWpLFStLFEBgI=
192194
github.com/tmc/grpc-websocket-proxy v0.0.0-20190109142713-0ad062ec5ee5/go.mod h1:ncp9v5uamzpCO7NfCPTXjqaC+bZgJeR0sMTm6dMHP7U=
193195
github.com/xiang90/probing v0.0.0-20190116061207-43a291ad63a2/go.mod h1:UETIi67q53MR2AWcXfiuqkDkRtnGDLqkBTpCHuJHxtU=
194196
go.etcd.io/bbolt v1.3.2/go.mod h1:IbVyRI1SCnLcuJnV2u8VeU0CEYM7e686BmAb1XKL+uU=

main.go

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
log "github.com/sirupsen/logrus"
1111

1212
nested "github.com/antonfisher/nested-logrus-formatter"
13+
_ "github.com/mattn/go-sqlite3"
1314
"github.com/spf13/viper"
1415
)
1516

map.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import (
88
"github.com/paulmach/orb/maptile"
99
)
1010

11-
//TileMap 瓦片地图类型
11+
// TileMap 瓦片地图类型
1212
type TileMap struct {
1313
ID int
1414
Name string
@@ -23,14 +23,14 @@ type TileMap struct {
2323
//such as porxy...
2424
}
2525

26-
//CreateTileMap 添加地图
26+
// CreateTileMap 添加地图
2727
func CreateTileMap(url string) {
2828
// tileMap := TileMap{}
2929
//tileMap.Save()
3030
//成功默认保存到数据库
3131
}
3232

33-
//GetTileMapList 获取初始化默认地图列表
33+
// GetTileMapList 获取初始化默认地图列表
3434
func GetTileMapList() map[int]TileMap {
3535
tml := make(map[int]TileMap)
3636
var list = []string{"http://mt0.google.com/vt/lyrs=y&x={x}&y={y}&z={z}",
@@ -49,7 +49,7 @@ func GetTileMapList() map[int]TileMap {
4949
return tml
5050
}
5151

52-
//TileURL 获取瓦片URL
52+
// TileURL 获取瓦片URL
5353
func (m TileMap) getTileURL(t maptile.Tile) string {
5454
url := strings.Replace(m.URL, "{x}", strconv.Itoa(int(t.X)), -1)
5555
url = strings.Replace(url, "{y}", strconv.Itoa(int(t.Y)), -1)

task.go

+40-16
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ import (
55
"compress/gzip"
66
"database/sql"
77
"fmt"
8-
"io/ioutil"
8+
"io"
9+
"math"
910
"net/http"
1011
"os"
1112
"path/filepath"
@@ -23,10 +24,10 @@ import (
2324
pb "gopkg.in/cheggaaa/pb.v1"
2425
)
2526

26-
//MBTileVersion mbtiles版本号
27+
// MBTileVersion mbtiles版本号
2728
const MBTileVersion = "1.2"
2829

29-
//Task 下载任务
30+
// Task 下载任务
3031
type Task struct {
3132
ID string
3233
Name string
@@ -52,7 +53,7 @@ type Task struct {
5253
outformat string
5354
}
5455

55-
//NewTask 创建下载任务
56+
// NewTask 创建下载任务
5657
func NewTask(layers []Layer, m TileMap) *Task {
5758
if len(layers) == 0 {
5859
return nil
@@ -92,7 +93,7 @@ func NewTask(layers []Layer, m TileMap) *Task {
9293
return &task
9394
}
9495

95-
//Bound 范围
96+
// Bound 范围
9697
func (task *Task) Bound() orb.Bound {
9798
bound := orb.Bound{Min: orb.Point{1, 1}, Max: orb.Point{-1, -1}}
9899
for _, layer := range task.Layers {
@@ -103,7 +104,7 @@ func (task *Task) Bound() orb.Bound {
103104
return bound
104105
}
105106

106-
//Center 中心点
107+
// Center 中心点
107108
func (task *Task) Center() orb.Point {
108109
layer := task.Layers[len(task.Layers)-1]
109110
bound := orb.Bound{Min: orb.Point{1, 1}, Max: orb.Point{-1, -1}}
@@ -113,7 +114,7 @@ func (task *Task) Center() orb.Point {
113114
return bound.Center()
114115
}
115116

116-
//MetaItems 输出
117+
// MetaItems 输出
117118
func (task *Task) MetaItems() map[string]string {
118119
b := task.Bound()
119120
c := task.Center()
@@ -136,7 +137,7 @@ func (task *Task) MetaItems() map[string]string {
136137
return data
137138
}
138139

139-
//SetupMBTileTables 初始化配置MBTile库
140+
// SetupMBTileTables 初始化配置MBTile库
140141
func (task *Task) SetupMBTileTables() error {
141142

142143
if task.File == "" {
@@ -205,7 +206,7 @@ func (task *Task) playFun() {
205206
task.play <- struct{}{}
206207
}
207208

208-
//SavePipe 保存瓦片管道
209+
// SavePipe 保存瓦片管道
209210
func (task *Task) savePipe() {
210211
for tile := range task.savingpipe {
211212
err := saveToMBTile(tile, task.db)
@@ -219,7 +220,7 @@ func (task *Task) savePipe() {
219220
}
220221
}
221222

222-
//SaveTile 保存瓦片
223+
// SaveTile 保存瓦片
223224
func (task *Task) saveTile(tile Tile) error {
224225
// defer task.wg.Done()
225226
err := saveToFiles(tile, task)
@@ -229,7 +230,7 @@ func (task *Task) saveTile(tile Tile) error {
229230
return nil
230231
}
231232

232-
//tileFetcher 瓦片加载器
233+
// tileFetcher 瓦片加载器
233234
func (task *Task) tileFetcher(mt maptile.Tile, url string) {
234235
start := time.Now()
235236
defer task.tileWG.Done() //结束该瓦片请求
@@ -240,21 +241,44 @@ func (task *Task) tileFetcher(mt maptile.Tile, url string) {
240241
prep := func(t maptile.Tile, url string) string {
241242
url = strings.Replace(url, "{x}", strconv.Itoa(int(t.X)), -1)
242243
url = strings.Replace(url, "{y}", strconv.Itoa(int(t.Y)), -1)
244+
maxY := int(math.Pow(2, float64(t.Z))) - 1
245+
url = strings.Replace(url, "{-y}", strconv.Itoa(maxY-int(t.Y)), -1)
243246
url = strings.Replace(url, "{z}", strconv.Itoa(int(t.Z)), -1)
244247
return url
245248
}
246249
tile := prep(mt, url)
247-
resp, err := http.Get(tile)
250+
client := &http.Client{
251+
CheckRedirect: func(req *http.Request, via []*http.Request) error {
252+
// 自定义重定向的行为
253+
return http.ErrUseLastResponse // 使用最后一个响应
254+
},
255+
}
256+
req, err := http.NewRequest("GET", tile, nil)
257+
if err != nil {
258+
fmt.Println("创建请求失败:", err)
259+
return
260+
}
261+
262+
req.Header.Set("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36")
263+
// req.Header.Set("Origin", "https://map.tianditu.gov.cn")
264+
req.Header.Set("Referer", "https://jiangsu.tianditu.gov.cn")
265+
resp, err := client.Do(req)
248266
if err != nil {
249-
log.Errorf("fetch :%s error, details: %s ~", tile, err)
267+
fmt.Println("发送请求失败:", err)
250268
return
251269
}
252270
defer resp.Body.Close()
271+
// resp, err := http.Get(tile)
272+
// if err != nil {
273+
// log.Errorf("fetch :%s error, details: %s ~", tile, err)
274+
// return
275+
// }
276+
// defer resp.Body.Close()
253277
if resp.StatusCode != 200 {
254278
log.Errorf("fetch %v tile error, status code: %d ~", tile, resp.StatusCode)
255279
return
256280
}
257-
body, err := ioutil.ReadAll(resp.Body)
281+
body, err := io.ReadAll(resp.Body)
258282
if err != nil {
259283
log.Errorf("read %v tile error ~ %s", mt, err)
260284
return
@@ -294,7 +318,7 @@ func (task *Task) tileFetcher(mt maptile.Tile, url string) {
294318
log.Infof("tile(z:%d, x:%d, y:%d), %dms , %.2f kb, %s ...\n", mt.Z, mt.X, mt.Y, cost, float32(len(body))/1024.0, tile)
295319
}
296320

297-
//DownloadZoom 下载指定层级
321+
// DownloadZoom 下载指定层级
298322
func (task *Task) downloadLayer(layer Layer) {
299323
bar := pb.New64(layer.Count).Prefix(fmt.Sprintf("Zoom %d : ", layer.Zoom)).Postfix("\n")
300324
// bar.SetRefreshRate(time.Second)
@@ -334,7 +358,7 @@ func (task *Task) downloadLayer(layer Layer) {
334358
bar.FinishPrint(fmt.Sprintf("Task %s Zoom %d finished ~", task.ID, layer.Zoom))
335359
}
336360

337-
//Download 开启下载任务
361+
// Download 开启下载任务
338362
func (task *Task) Download() {
339363
//g orb.Geometry, minz int, maxz int
340364
task.Bar = pb.New64(task.Total).Prefix("Task : ").Postfix("\n")

utils.go

+6-7
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"database/sql"
55
"encoding/json"
66
"fmt"
7-
"io/ioutil"
87
"os"
98
"path/filepath"
109
"sync"
@@ -30,7 +29,7 @@ func saveToFiles(tile Tile, task *Task) error {
3029
dir := filepath.Join(rootdir, fmt.Sprintf(`%d`, tile.T.Z), fmt.Sprintf(`%d`, tile.T.X))
3130
os.MkdirAll(dir, os.ModePerm)
3231
fileName := filepath.Join(dir, fmt.Sprintf(`%d.%s`, tile.T.Y, task.TileMap.Format))
33-
err := ioutil.WriteFile(fileName, tile.C, os.ModePerm)
32+
err := os.WriteFile(fileName, tile.C, os.ModePerm)
3433
if err != nil {
3534
return err
3635
}
@@ -69,7 +68,7 @@ func optimizeDatabase(db *sql.DB) error {
6968
}
7069

7170
func loadFeature(path string) *geojson.Feature {
72-
data, err := ioutil.ReadFile(path)
71+
data, err := os.ReadFile(path)
7372
if err != nil {
7473
log.Fatalf("unable to read file: %v", err)
7574
}
@@ -96,7 +95,7 @@ func loadFeature(path string) *geojson.Feature {
9695
}
9796

9897
func loadFeatureCollection(path string) *geojson.FeatureCollection {
99-
data, err := ioutil.ReadFile(path)
98+
data, err := os.ReadFile(path)
10099
if err != nil {
101100
log.Fatalf("unable to read file: %v", err)
102101
}
@@ -119,7 +118,7 @@ func loadFeatureCollection(path string) *geojson.FeatureCollection {
119118
}
120119

121120
func loadCollection(path string) orb.Collection {
122-
data, err := ioutil.ReadFile(path)
121+
data, err := os.ReadFile(path)
123122
if err != nil {
124123
log.Fatalf("unable to read file: %v", err)
125124
}
@@ -155,7 +154,7 @@ func output(name string, r *geojson.FeatureCollection) {
155154
log.Fatalf("error marshalling json: %v", err)
156155
}
157156

158-
err = ioutil.WriteFile("failure_"+name+".geojson", data, 0644)
157+
err = os.WriteFile("failure_"+name+".geojson", data, 0644)
159158
if err != nil {
160159
log.Fatalf("write file failure: %v", err)
161160
}
@@ -169,7 +168,7 @@ func output2(name string, r *geojson.FeatureCollection, wg *sync.WaitGroup) {
169168
log.Fatalf("error marshalling json: %v", err)
170169
}
171170

172-
err = ioutil.WriteFile(name+".geojson", data, 0644)
171+
err = os.WriteFile(name+".geojson", data, 0644)
173172
if err != nil {
174173
log.Fatalf("write file failure: %v", err)
175174
}

0 commit comments

Comments
 (0)