0 Star 37 Fork 12

exlimit / tiler

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
utils.go 4.18 KB
一键复制 编辑 原始数据 按行查看 历史
atlasdatatech 提交于 2024-01-24 00:00 . 添加天地图示例
package main
import (
"database/sql"
"encoding/json"
"fmt"
"os"
"path/filepath"
"sync"
"github.com/paulmach/orb"
"github.com/paulmach/orb/geojson"
"github.com/paulmach/orb/maptile"
"github.com/paulmach/orb/maptile/tilecover"
log "github.com/sirupsen/logrus"
)
func saveToMBTile(tile Tile, db *sql.DB) error {
_, err := db.Exec("insert into tiles (zoom_level, tile_column, tile_row, tile_data) values (?, ?, ?, ?);", tile.T.Z, tile.T.X, tile.flipY(), tile.C)
// _, err := db.Exec("insert or ignore into tiles (zoom_level, tile_column, tile_row, tile_data) values (?, ?, ?, ?);", tile.T.Z, tile.T.X, tile.flipY(), tile.C)
if err != nil {
return err
}
return nil
}
func saveToFiles(tile Tile, task *Task) error {
dir := filepath.Join(task.File, fmt.Sprintf(`%d`, tile.T.Z), fmt.Sprintf(`%d`, tile.T.X))
os.MkdirAll(dir, os.ModePerm)
fileName := filepath.Join(dir, fmt.Sprintf(`%d.%s`, tile.T.Y, task.TileMap.Format))
err := os.WriteFile(fileName, tile.C, os.ModePerm)
if err != nil {
return err
}
log.Println(fileName)
return nil
}
func optimizeConnection(db *sql.DB) error {
// _, err := db.Exec("PRAGMA synchronous=0")
// if err != nil {
// return err
// }
_, err := db.Exec("PRAGMA locking_mode=EXCLUSIVE")
if err != nil {
return err
}
_, err = db.Exec("PRAGMA journal_mode=DELETE")
if err != nil {
return err
}
return nil
}
func optimizeDatabase(db *sql.DB) error {
_, err := db.Exec("ANALYZE;")
if err != nil {
return err
}
_, err = db.Exec("VACUUM;")
if err != nil {
return err
}
return nil
}
func loadFeature(path string) *geojson.Feature {
data, err := os.ReadFile(path)
if err != nil {
log.Fatalf("unable to read file: %v", err)
}
f, err := geojson.UnmarshalFeature(data)
if err == nil {
return f
}
fc, err := geojson.UnmarshalFeatureCollection(data)
if err == nil {
if len(fc.Features) != 1 {
log.Fatalf("must have 1 feature: %v", len(fc.Features))
}
return fc.Features[0]
}
g, err := geojson.UnmarshalGeometry(data)
if err != nil {
log.Fatalf("unable to unmarshal feature: %v", err)
}
return geojson.NewFeature(g.Geometry())
}
func loadFeatureCollection(path string) *geojson.FeatureCollection {
data, err := os.ReadFile(path)
if err != nil {
log.Fatalf("unable to read file: %v", err)
}
fc, err := geojson.UnmarshalFeatureCollection(data)
if err != nil {
log.Fatalf("unable to unmarshal feature: %v", err)
}
count := 0
for i := range fc.Features {
if fc.Features[i].Properties["name"] != "original" {
fc.Features[count] = fc.Features[i]
count++
}
}
fc.Features = fc.Features[:count]
return fc
}
func loadCollection(path string) orb.Collection {
data, err := os.ReadFile(path)
if err != nil {
log.Fatalf("unable to read file: %v", err)
}
fc, err := geojson.UnmarshalFeatureCollection(data)
if err != nil {
log.Fatalf("unable to unmarshal feature: %v", err)
}
var collection orb.Collection
for _, f := range fc.Features {
collection = append(collection, f.Geometry)
}
return collection
}
// output gets called if there is a test failure for debugging.
func output(name string, r *geojson.FeatureCollection) {
f := loadFeature("./data/" + name + ".geojson")
if f.Properties == nil {
f.Properties = make(geojson.Properties)
}
f.Properties["fill"] = "#FF0000"
f.Properties["fill-opacity"] = "0.5"
f.Properties["stroke"] = "#FF0000"
f.Properties["name"] = "original"
r.Append(f)
data, err := json.MarshalIndent(r, "", " ")
if err != nil {
log.Fatalf("error marshalling json: %v", err)
}
err = os.WriteFile("failure_"+name+".geojson", data, 0644)
if err != nil {
log.Fatalf("write file failure: %v", err)
}
}
// output gets called if there is a test failure for debugging.
func output2(name string, r *geojson.FeatureCollection, wg *sync.WaitGroup) {
defer wg.Done()
data, err := json.MarshalIndent(r, "", " ")
if err != nil {
log.Fatalf("error marshalling json: %v", err)
}
err = os.WriteFile(name+".geojson", data, 0644)
if err != nil {
log.Fatalf("write file failure: %v", err)
}
}
func getZoomCount(g orb.Geometry, minz int, maxz int) map[int]int64 {
info := make(map[int]int64)
for z := minz; z <= maxz; z++ {
info[z] = tilecover.GeometryCount(g, maptile.Zoom(z))
}
return info
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/exlimit/tiler.git
git@gitee.com:exlimit/tiler.git
exlimit
tiler
tiler
master

搜索帮助

344bd9b3 5694891 D2dac590 5694891