Implement fetaure request issue #17

mtseeder and mtwebmapper got an option to set the background color where no nodes are generated, yet.
This commit is contained in:
Sascha L. Teichmann
2016-04-23 16:45:33 +02:00
parent 0030f7bc02
commit 0db9b519a6
10 changed files with 130 additions and 45 deletions

View File

@ -7,6 +7,7 @@ package main
import (
"flag"
"fmt"
"image/color"
"log"
"net"
"net/http"
@ -25,6 +26,7 @@ func main() {
redisPort int
redisHost string
colorsFile string
bgColor string
workers int
transparent bool
transparentDim float64
@ -35,6 +37,9 @@ func main() {
yMin int
yMax int
)
defaultBgColor := common.ColorToHex(common.BackgroundColor)
flag.IntVar(&webPort, "web-port", 8808, "port of the web server")
flag.IntVar(&webPort, "p", 8808, "port of the web server (shorthand)")
flag.StringVar(&webHost, "web-host", "localhost", "address to bind web server")
@ -54,6 +59,8 @@ func main() {
flag.IntVar(&workers, "workers", 1, "number of workers to render tiles")
flag.StringVar(&colorsFile, "colors", "colors.txt", "colors used to render map tiles.")
flag.StringVar(&colorsFile, "c", "colors.txt", "colors used to render map tiles (shorthand).")
flag.StringVar(&bgColor, "background", defaultBgColor, "background color")
flag.StringVar(&bgColor, "bg", defaultBgColor, "background color (shorthand)")
flag.BoolVar(&transparent, "transparent", false, "Render transparent blocks.")
flag.BoolVar(&transparent, "t", false, "Render transparent blocks (shorthand).")
flag.Float64Var(&transparentDim,
@ -76,9 +83,16 @@ func main() {
common.PrintVersionAndExit()
}
var bg color.RGBA
var err error
if bg, err = common.ParseColor(bgColor); err != nil {
log.Printf("WARN: Cannot parse background color '%s': %s\n", bgColor, err)
bg = common.BackgroundColor
}
router := mux.NewRouter()
subBaseLine := newSubBaseLine(mapDir)
subBaseLine := newSubBaseLine(mapDir, bg)
router.Path("/map/{z:[0-9]+}/{x:[0-9]+}/{y:[0-9]+}.png").Handler(subBaseLine)
var btu baseTilesUpdates
@ -116,7 +130,7 @@ func main() {
mapDir,
redisAddress,
allowedUpdateIps,
colors,
colors, bg,
yMin, yMax,
transparent,
workers,

View File

@ -7,6 +7,7 @@ package main
import (
"fmt"
"image"
"image/color"
"image/png"
"log"
"net/http"
@ -21,10 +22,11 @@ import (
type subBaseLine struct {
mapDir string
bg color.RGBA
}
func newSubBaseLine(mapDir string) *subBaseLine {
return &subBaseLine{mapDir: mapDir}
func newSubBaseLine(mapDir string, bg color.RGBA) *subBaseLine {
return &subBaseLine{mapDir: mapDir, bg: bg}
}
func (sb *subBaseLine) ServeHTTP(rw http.ResponseWriter, r *http.Request) {
@ -77,7 +79,7 @@ func (sb *subBaseLine) ServeHTTP(rw http.ResponseWriter, r *http.Request) {
xo := w * rx
yo := w * (parts - 1 - ry)
img := common.LoadPNG(baseTile)
img := common.LoadPNG(baseTile, sb.bg)
type subImage interface {
SubImage(image.Rectangle) image.Image

View File

@ -7,6 +7,7 @@ package main
import (
"encoding/json"
"image"
"image/color"
"image/draw"
"log"
"net"
@ -34,6 +35,7 @@ type tileUpdater struct {
redisAddress string
ips []net.IP
colors *common.Colors
bg color.RGBA
yMin, yMax int16
workers int
transparent bool
@ -71,6 +73,7 @@ func newTileUpdater(
mapDir, redisAddress string,
ips []net.IP,
colors *common.Colors,
bg color.RGBA,
yMin, yMax int,
transparent bool,
workers int,
@ -83,6 +86,7 @@ func newTileUpdater(
ips: ips,
changes: map[xz]bool{},
colors: colors,
bg: bg,
yMin: int16(yMin),
yMax: int16(yMax),
transparent: transparent,
@ -171,11 +175,11 @@ func (tu *tileUpdater) doUpdates() {
continue
}
btc := common.NewBaseTileCreator(
client, tu.colors,
client, tu.colors, tu.bg,
tu.yMin, tu.yMax,
tu.transparent, baseDir, true)
done.Add(1)
go updateBaseTiles(jobs, btc, &done)
go tu.updateBaseTiles(jobs, btc, &done)
}
parentJobs := make(map[xz]uint16)
@ -193,7 +197,7 @@ func (tu *tileUpdater) doUpdates() {
pJobs := make(chan xzm)
for i, n := 0, common.Min(len(parentJobs), tu.workers); i < n; i++ {
done.Add(1)
go updatePyramidTiles(level, tu.mapDir, pJobs, &done)
go tu.updatePyramidTiles(level, pJobs, &done)
}
ppJobs := make(map[xz]uint16)
for c, mask := range parentJobs {
@ -212,13 +216,15 @@ func (tu *tileUpdater) doUpdates() {
}
}
func updatePyramidTiles(level int, baseDir string, jobs chan xzm, done *sync.WaitGroup) {
func (tu *tileUpdater) updatePyramidTiles(
level int, jobs chan xzm, done *sync.WaitGroup) {
defer done.Done()
scratch := image.NewRGBA(image.Rect(0, 0, 256, 256))
resized := image.NewRGBA(image.Rect(0, 0, 128, 128))
for job := range jobs {
if err := updatePyramidTile(scratch, resized, level, baseDir, job); err != nil {
if err := tu.updatePyramidTile(scratch, resized, level, job); err != nil {
log.Printf("Updating pyramid tile failed: %s\n", err)
}
}
@ -244,28 +250,29 @@ var ofs = [4][2]int{
var windowSize = image.Pt(128, 128)
func updatePyramidTile(scratch, resized *image.RGBA, level int, baseDir string, j xzm) error {
func (tu *tileUpdater) updatePyramidTile(scratch, resized *image.RGBA, level int, j xzm) error {
var orig image.Image
origPath := filepath.Join(
baseDir,
tu.mapDir,
strconv.Itoa(level),
strconv.Itoa(int(j.P.X)),
strconv.Itoa(int(j.P.Z))+".png")
sr := resized.Bounds()
levelDir := strconv.Itoa(level + 1)
for i := uint16(0); i < 4; i++ {
if j.Mask&(1<<i) != 0 {
//log.Printf("level %d: modified %d\n", level, i)
o := ofs[i]
bx, bz := int(2*j.P.X), int(2*j.P.Z)
path := filepath.Join(
baseDir,
strconv.Itoa(level+1),
tu.mapDir,
levelDir,
strconv.Itoa(bx+o[0]),
strconv.Itoa(bz+o[1])+".png")
img := common.LoadPNG(path)
img := common.LoadPNG(path, tu.bg)
if err := rez.Convert(resized, img, common.ResizeFilter); err != nil {
return err
}
@ -274,7 +281,7 @@ func updatePyramidTile(scratch, resized *image.RGBA, level int, baseDir string,
} else {
// Load lazy
if orig == nil {
orig = common.LoadPNG(origPath)
orig = common.LoadPNG(origPath, tu.bg)
}
//log.Printf("level %d: copied %d\n", level, i)
min := orig.Bounds().Min.Add(dps[i])
@ -286,7 +293,10 @@ func updatePyramidTile(scratch, resized *image.RGBA, level int, baseDir string,
return common.SaveAsPNGAtomic(origPath, scratch)
}
func updateBaseTiles(jobs chan xz, btc *common.BaseTileCreator, done *sync.WaitGroup) {
func (tu *tileUpdater) updateBaseTiles(
jobs chan xz,
btc *common.BaseTileCreator, done *sync.WaitGroup) {
defer btc.Close()
defer done.Done()
for job := range jobs {