Skip to content

Instantly share code, notes, and snippets.

@achun
Created March 14, 2014 03:44
Show Gist options
  • Save achun/9541792 to your computer and use it in GitHub Desktop.
Save achun/9541792 to your computer and use it in GitHub Desktop.
Mobile Atlas Creator sqlitdb 图片导出
package main
import (
"flag"
"fmt"
"os"
"path/filepath"
"github.com/cheggaaa/pb"
"github.com/typepress/db"
_ "github.com/typepress/db/sqlite"
)
func main() {
flag.Parse()
if dir == "" {
dir, _ = os.Getwd()
dir = filepath.Join(dir, fmt.Sprint(z))
}
settings := db.Settings{
Database: sqlitdb,
}
dbh, err := db.Open("sqlite", settings)
if err != nil {
fmt.Println(err)
flag.Usage()
return
}
defer dbh.Close()
tiles, err := dbh.Collection("tiles")
if err != nil {
fmt.Println(err)
return
}
t := Tiles{}
err = tiles.Find().Sort(`-x`, `-y`).One(&t)
gX, gY := t.X, t.Y
if err == nil {
err = tiles.Find().Sort(`x`, `y`).One(&t)
lX, lY = t.X, t.Y
}
res := tiles.Find()
if err == nil {
cnt, err = res.Count()
}
if err != nil {
fmt.Println(err)
return
}
pX := gX - lX + 1
pY := gY - lY + 1
if pX*pY != cnt {
fmt.Println("error:", pX, pY, "not equal", cnt)
return
}
defer res.Close()
pX = uint64(len(fmt.Sprint(pX)))
pY = uint64(len(fmt.Sprint(pY)))
format = fmt.Sprintf("%dz%%.%ddx%%.%dd", z, pX, pY)
bar := pb.StartNew(int(cnt))
for {
err = res.Next(&t)
if err != nil {
if err != db.ErrNoMoreRows {
fmt.Println(err)
return
} else {
bar.FinishPrint("The End!")
break
}
}
storeImg(&t)
bar.Increment()
}
}
var z int = 17
var dir string
var sqlitdb string
var cnt, lX, lY uint64
var format string
func init() {
flag.IntVar(&z, "z", 0, "zoom level")
flag.StringVar(&dir, "o", "", "directory for output")
flag.StringVar(&sqlitdb, "db", "", "Mobile Atlas Creator sqlitdb full filename")
}
type Tiles struct {
X uint64 `field:"x"`
Y uint64 `field:"y"`
Image string `field:"image"`
}
const JPG = "\xff\xd8" // FF E0|E1|E2|E3|E8
const PNG = "\x89\x50\x4E\x47\x0D\x0A\x1A\x0A"
func storeImg(t *Tiles) {
name := fmt.Sprintf(format, t.Y-lY, t.X-lX)
if len(t.Image) < 8 {
fmt.Printf("Unknow image format: %x : %s \n", name, t.Image[:])
return
}
var ext string
if JPG == string(t.Image[:2]) {
ext = ".jpg"
} else if PNG == string(t.Image[:8]) {
ext = ".png"
} else {
fmt.Printf("Unknow image format: %x : %s \n", name, t.Image[:8])
return
}
f, err := os.Create(filepath.Join(dir, name+ext))
if err != nil {
fmt.Println(err)
return
}
defer f.Close()
f.WriteString(t.Image)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment