1
0
forked from MTSR/mapserver
This commit is contained in:
NatureFreshMilk 2019-01-18 09:13:37 +01:00
parent c483bb699c
commit f8f524013e
17 changed files with 247 additions and 255 deletions

View File

@ -1,14 +1,14 @@
package app
import (
"mapserver/params"
"mapserver/worldconfig"
"mapserver/db"
"mapserver/mapblockaccessor"
"mapserver/colormapping"
"mapserver/mapblockrenderer"
"mapserver/tiledb"
"mapserver/tilerenderer"
"mapserver/colormapping"
"mapserver/db"
"mapserver/mapblockaccessor"
"mapserver/mapblockrenderer"
"mapserver/params"
"mapserver/tiledb"
"mapserver/tilerenderer"
"mapserver/worldconfig"
)
const (
@ -16,15 +16,15 @@ const (
)
type App struct {
Params params.ParamsType
Config *Config
Worldconfig worldconfig.WorldConfig
Params params.ParamsType
Config *Config
Worldconfig worldconfig.WorldConfig
Blockdb db.DBAccessor
Tiledb tiledb.DBAccessor
Blockdb db.DBAccessor
Tiledb tiledb.DBAccessor
BlockAccessor *mapblockaccessor.MapBlockAccessor
Colormapping *colormapping.ColorMapping
Mapblockrenderer *mapblockrenderer.MapBlockRenderer
Tilerenderer *tilerenderer.TileRenderer
BlockAccessor *mapblockaccessor.MapBlockAccessor
Colormapping *colormapping.ColorMapping
Mapblockrenderer *mapblockrenderer.MapBlockRenderer
Tilerenderer *tilerenderer.TileRenderer
}

View File

@ -1,34 +1,34 @@
package app
import (
"encoding/json"
"io/ioutil"
"os"
"encoding/json"
"io/ioutil"
"os"
)
type Config struct {
Port int `json:"port"`
EnableInitialRendering bool `json:"enableinitialrendering"`
Port int `json:"port"`
EnableInitialRendering bool `json:"enableinitialrendering"`
}
func ParseConfig(filename string) (*Config, error) {
cfg := Config{
Port: 80,
EnableInitialRendering: true,
}
cfg := Config{
Port: 80,
EnableInitialRendering: true,
}
info, err := os.Stat(filename)
if info != nil && err == nil {
data, err := ioutil.ReadFile(filename)
if err != nil {
return nil, err
}
info, err := os.Stat(filename)
if info != nil && err == nil {
data, err := ioutil.ReadFile(filename)
if err != nil {
return nil, err
}
err = json.Unmarshal(data, &cfg)
if err != nil {
return nil, err
}
}
err = json.Unmarshal(data, &cfg)
if err != nil {
return nil, err
}
}
return &cfg, nil
return &cfg, nil
}

View File

@ -1,53 +1,53 @@
package app
import (
"mapserver/params"
"mapserver/worldconfig"
"mapserver/db"
"mapserver/mapblockaccessor"
"mapserver/colormapping"
"mapserver/mapblockrenderer"
"mapserver/tiledb"
"mapserver/tilerenderer"
"mapserver/layerconfig"
"mapserver/colormapping"
"mapserver/db"
"mapserver/layerconfig"
"mapserver/mapblockaccessor"
"mapserver/mapblockrenderer"
"mapserver/params"
"mapserver/tiledb"
"mapserver/tilerenderer"
"mapserver/worldconfig"
"github.com/sirupsen/logrus"
"github.com/sirupsen/logrus"
"errors"
"errors"
)
func Setup(p params.ParamsType, cfg *Config) (*App, error) {
a := App{}
a.Params = p
a.Config = cfg
a := App{}
a.Params = p
a.Config = cfg
//Parse world config
//Parse world config
a.Worldconfig = worldconfig.Parse("world.mt")
logrus.WithFields(logrus.Fields{"version": Version}).Info("Starting mapserver")
a.Worldconfig = worldconfig.Parse("world.mt")
logrus.WithFields(logrus.Fields{"version": Version}).Info("Starting mapserver")
if a.Worldconfig.Backend != worldconfig.BACKEND_SQLITE3 {
return nil, errors.New("no supported backend found!")
}
if a.Worldconfig.Backend != worldconfig.BACKEND_SQLITE3 {
return nil, errors.New("no supported backend found!")
}
//create db accessor
var err error
a.Blockdb, err = db.NewSqliteAccessor("map.sqlite")
//create db accessor
var err error
a.Blockdb, err = db.NewSqliteAccessor("map.sqlite")
if err != nil {
return nil, err
}
//migrate block db
//migrate block db
err = a.Blockdb.Migrate()
if err != nil {
return nil, err
}
//mapblock accessor
//mapblock accessor
a.BlockAccessor = mapblockaccessor.NewMapBlockAccessor(a.Blockdb)
//color mapping
//color mapping
a.Colormapping = colormapping.NewColorMapping()
err = a.Colormapping.LoadVFSColors(false, "/colors.txt")
@ -55,10 +55,10 @@ func Setup(p params.ParamsType, cfg *Config) (*App, error) {
return nil, err
}
//mapblock renderer
a.Mapblockrenderer = mapblockrenderer.NewMapBlockRenderer(a.BlockAccessor, a.Colormapping)
//mapblock renderer
a.Mapblockrenderer = mapblockrenderer.NewMapBlockRenderer(a.BlockAccessor, a.Colormapping)
//tile database
//tile database
a.Tiledb, err = tiledb.NewSqliteAccessor("tiles.sqlite")
@ -66,7 +66,7 @@ func Setup(p params.ParamsType, cfg *Config) (*App, error) {
return nil, err
}
//migrate tile database
//migrate tile database
err = a.Tiledb.Migrate()
@ -74,13 +74,13 @@ func Setup(p params.ParamsType, cfg *Config) (*App, error) {
return nil, err
}
//setup tile renderer
a.Tilerenderer = tilerenderer.NewTileRenderer(
a.Mapblockrenderer,
a.Tiledb,
a.Blockdb,
layerconfig.DefaultLayers,
)
//setup tile renderer
a.Tilerenderer = tilerenderer.NewTileRenderer(
a.Mapblockrenderer,
a.Tiledb,
a.Blockdb,
layerconfig.DefaultLayers,
)
return &a, nil
return &a, nil
}

View File

@ -19,8 +19,8 @@ func GetMapBlockRangeFromTile(tc TileCoords, y int) MapBlockRange {
mapBlockX1 := tc.X * scale
mapBlockZ1 := (tc.Y * scale * -1) - 1
mapBlockX2 := mapBlockX1 + scale-1
mapBlockZ2 := (mapBlockZ1 + ((scale-1) * -1))
mapBlockX2 := mapBlockX1 + scale - 1
mapBlockZ2 := (mapBlockZ1 + ((scale - 1) * -1))
return MapBlockRange{
Pos1: NewMapBlockCoords(mapBlockX1, y, mapBlockZ1),

View File

@ -5,8 +5,8 @@ import (
)
type TileCoords struct {
X, Y int
Zoom int
X, Y int
Zoom int
LayerId int
}
@ -20,9 +20,9 @@ func NewTileCoords(x, y, zoom int, layerId int) TileCoords {
func (tc TileCoords) GetZoomedOutTile() TileCoords {
return TileCoords{
X: int(math.Floor(float64(tc.X) / 2.0)),
Y: int(math.Floor(float64(tc.Y) / 2.0)),
Zoom: tc.Zoom - 1,
X: int(math.Floor(float64(tc.X) / 2.0)),
Y: int(math.Floor(float64(tc.Y) / 2.0)),
Zoom: tc.Zoom - 1,
LayerId: tc.LayerId}
}

View File

@ -2,12 +2,12 @@ package db
import (
"database/sql"
"errors"
_ "github.com/mattn/go-sqlite3"
"github.com/sirupsen/logrus"
"mapserver/coords"
"time"
"strings"
"errors"
"time"
)
const migrateScript = `
@ -104,7 +104,7 @@ func (db *Sqlite3Accessor) GetBlock(pos coords.MapBlockCoords) (*Block, error) {
return nil, nil
}
func sortAsc(a, b int) (int, int){
func sortAsc(a, b int) (int, int) {
if a > b {
return b, a
} else {
@ -112,7 +112,7 @@ func sortAsc(a, b int) (int, int){
}
}
func (db *Sqlite3Accessor) CountBlocks(pos1 coords.MapBlockCoords, pos2 coords.MapBlockCoords) (int, error){
func (db *Sqlite3Accessor) CountBlocks(pos1 coords.MapBlockCoords, pos2 coords.MapBlockCoords) (int, error) {
poslist := make([]interface{}, 0)
@ -123,7 +123,7 @@ func (db *Sqlite3Accessor) CountBlocks(pos1 coords.MapBlockCoords, pos2 coords.M
for x := minX; x <= maxX; x++ {
for y := minY; y <= maxY; y++ {
for z := minZ; z <= maxZ; z++ {
poslist = append(poslist, coords.CoordToPlain(coords.NewMapBlockCoords(x,y,z)))
poslist = append(poslist, coords.CoordToPlain(coords.NewMapBlockCoords(x, y, z)))
}
}
}

View File

@ -75,7 +75,6 @@ func TestMigrateAndQuery(t *testing.T) {
}
func TestMigrateAndQueryStride(t *testing.T) {
tmpfile, err := ioutil.TempFile("", "TestMigrateAndQueryStride.*.sqlite")
if err != nil {

View File

@ -1,71 +1,68 @@
package initialrenderer
import (
"mapserver/tilerenderer"
"mapserver/layerconfig"
"mapserver/coords"
"github.com/sirupsen/logrus"
"time"
"github.com/sirupsen/logrus"
"mapserver/coords"
"mapserver/layerconfig"
"mapserver/tilerenderer"
"time"
)
func worker(tr *tilerenderer.TileRenderer, jobs <-chan coords.TileCoords){
for coord := range(jobs){
tr.Render(coord)
}
func worker(tr *tilerenderer.TileRenderer, jobs <-chan coords.TileCoords) {
for coord := range jobs {
tr.Render(coord)
}
}
func Render(tr *tilerenderer.TileRenderer,
layers []layerconfig.Layer){
layers []layerconfig.Layer) {
start := time.Now()
complete_count := 256*256
current_count := 0
perf_count := 0
start := time.Now()
complete_count := 256 * 256
current_count := 0
perf_count := 0
jobs := make(chan coords.TileCoords, 100)
jobs := make(chan coords.TileCoords, 100)
go worker(tr, jobs)
go worker(tr, jobs)
go worker(tr, jobs)
go worker(tr, jobs)
go worker(tr, jobs)
go worker(tr, jobs)
go worker(tr, jobs)
go worker(tr, jobs)
for _, layer := range(layers) {
for _, layer := range layers {
//zoom 10 iterator
for x := -127; x<128; x++ {
for y := -127; y<128; y++ {
tc := coords.NewTileCoords(x,y,9,layer.Id)
jobs <- tc
current_count++
perf_count++
//zoom 10 iterator
for x := -127; x < 128; x++ {
for y := -127; y < 128; y++ {
tc := coords.NewTileCoords(x, y, 9, layer.Id)
jobs <- tc
current_count++
perf_count++
if time.Now().Sub(start).Seconds() > 2 {
start = time.Now()
progress := float64(current_count) / float64(complete_count) * 100
if time.Now().Sub(start).Seconds() > 2 {
start = time.Now()
progress := float64(current_count) / float64(complete_count) * 100
fields := logrus.Fields{
"x": x,
"y": y,
"progress(%)": progress,
"layer": layer.Name,
"perf": perf_count,
}
fields := logrus.Fields{
"x": x,
"y": y,
"progress(%)": progress,
"layer": layer.Name,
"perf": perf_count,
}
perf_count = 0
logrus.WithFields(fields).Info("Initial render progress")
}
}
}
perf_count = 0
logrus.WithFields(fields).Info("Initial render progress")
}
}
}
}
}
close(jobs)
close(jobs)
}
// zoom:1 == length=1
// zoom:2 == length=2
// zoom:3 == length=4

View File

@ -1,47 +1,47 @@
package layerconfig
import (
"encoding/json"
"io/ioutil"
"encoding/json"
"io/ioutil"
)
type LayerContainer struct {
Layers []Layer `json:"layers"`
Layers []Layer `json:"layers"`
}
type Layer struct {
Id int `json:"id"`
Name string `json:"name"`
To int `json:"to"`
From int `json:"from"`
Id int `json:"id"`
Name string `json:"name"`
To int `json:"to"`
From int `json:"from"`
}
var DefaultLayers []Layer
func init(){
DefaultLayers = []Layer{
Layer{
Id: 0,
Name: "Base",
From: -16,
To: 160,
},
}
func init() {
DefaultLayers = []Layer{
Layer{
Id: 0,
Name: "Base",
From: -16,
To: 160,
},
}
}
func ParseFile(filename string) ([]Layer, error) {
data, err := ioutil.ReadFile(filename)
if err != nil {
return nil, err
}
data, err := ioutil.ReadFile(filename)
if err != nil {
return nil, err
}
lc := LayerContainer{}
lc := LayerContainer{}
err = json.Unmarshal(data, &lc)
if err != nil {
return nil, err
}
err = json.Unmarshal(data, &lc)
if err != nil {
return nil, err
}
return lc.Layers, nil
return lc.Layers, nil
}

View File

@ -1,26 +1,26 @@
package layerconfig
import (
"testing"
"testing"
)
func TestReadJson(t *testing.T){
layers, err := ParseFile("./testdata/layers.json")
func TestReadJson(t *testing.T) {
layers, err := ParseFile("./testdata/layers.json")
if err != nil {
t.Fatal(err)
}
if err != nil {
t.Fatal(err)
}
if layers == nil {
t.Fatal("no data")
}
if layers == nil {
t.Fatal("no data")
}
if len(layers) != 1 {
t.Fatal("length mismatch")
}
if len(layers) != 1 {
t.Fatal("length mismatch")
}
if layers[0].Name != "Base" {
t.Fatal("name mismatch")
}
if layers[0].Name != "Base" {
t.Fatal("name mismatch")
}
}

32
main.go
View File

@ -1,35 +1,33 @@
package main
import (
"mapserver/initialrenderer"
"github.com/sirupsen/logrus"
"mapserver/layerconfig"
"mapserver/app"
"mapserver/params"
"encoding/json"
"github.com/sirupsen/logrus"
"mapserver/app"
"mapserver/initialrenderer"
"mapserver/layerconfig"
"mapserver/params"
"fmt"
)
func main() {
logrus.SetLevel(logrus.InfoLevel)
//Parse command line
p := params.Parse()
p := params.Parse()
if p.Help {
params.PrintHelp()
return
}
if p.Help {
params.PrintHelp()
return
}
if p.Version {
fmt.Print("Mapserver version: ")
fmt.Println(app.Version)
return
}
if p.Version {
fmt.Print("Mapserver version: ")
fmt.Println(app.Version)
return
}
//parse Config
cfg, err := app.ParseConfig("mapserver.json")

View File

@ -4,8 +4,8 @@ import (
"errors"
"github.com/sirupsen/logrus"
"image"
"image/draw"
"image/color"
"image/draw"
"mapserver/colormapping"
"mapserver/coords"
"mapserver/mapblockaccessor"
@ -13,8 +13,8 @@ import (
)
type MapBlockRenderer struct {
accessor *mapblockaccessor.MapBlockAccessor
colors *colormapping.ColorMapping
accessor *mapblockaccessor.MapBlockAccessor
colors *colormapping.ColorMapping
enableShadow bool
}
@ -33,23 +33,23 @@ func IsViewBlocking(nodeName string) bool {
}
func clamp(num int) uint8 {
if num < 0 {
return 0
}
if num < 0 {
return 0
}
if num > 255 {
return 255
}
if num > 255 {
return 255
}
return uint8(num)
return uint8(num)
}
func addColorComponent(c *color.RGBA, value int) *color.RGBA {
return &color.RGBA{
R: clamp( int(c.R) + value ),
G: clamp( int(c.G) + value ),
B: clamp( int(c.B) + value ),
A: clamp( int(c.A) + value ),
R: clamp(int(c.R) + value),
G: clamp(int(c.G) + value),
B: clamp(int(c.B) + value),
A: clamp(int(c.A) + value),
}
}

View File

@ -5,9 +5,9 @@ import (
)
type ParamsType struct {
Help bool
Version bool
Dumpconfig bool
Help bool
Version bool
Dumpconfig bool
}
func Parse() ParamsType {
@ -21,6 +21,6 @@ func Parse() ParamsType {
return params
}
func PrintHelp(){
func PrintHelp() {
flag.PrintDefaults()
}

View File

@ -5,9 +5,9 @@ import (
)
type Tile struct {
Pos coords.TileCoords
Data []byte
Mtime int64
Pos coords.TileCoords
Data []byte
Mtime int64
}
type DBAccessor interface {

View File

@ -69,9 +69,9 @@ func (db *Sqlite3Accessor) GetTile(pos coords.TileCoords) (*Tile, error) {
}
mb := Tile{
Pos: pos,
Data: data,
Mtime: mtime,
Pos: pos,
Data: data,
Mtime: mtime,
}
return &mb, nil
@ -100,7 +100,6 @@ func (db *Sqlite3Accessor) RemoveTile(pos coords.TileCoords) error {
return err
}
func NewSqliteAccessor(filename string) (*Sqlite3Accessor, error) {
db, err := sql.Open("sqlite3", filename)
if err != nil {

View File

@ -1,25 +1,25 @@
package tilerenderer
import (
"errors"
"image"
"time"
"bytes"
"image/png"
"image/draw"
"mapserver/db"
"mapserver/coords"
"mapserver/mapblockrenderer"
"mapserver/layerconfig"
"mapserver/tiledb"
"errors"
"github.com/sirupsen/logrus"
"image"
"image/draw"
"image/png"
"mapserver/coords"
"mapserver/db"
"mapserver/layerconfig"
"mapserver/mapblockrenderer"
"mapserver/tiledb"
"time"
)
type TileRenderer struct {
mapblockrenderer *mapblockrenderer.MapBlockRenderer
layers []layerconfig.Layer
tdb tiledb.DBAccessor
dba db.DBAccessor
layers []layerconfig.Layer
tdb tiledb.DBAccessor
dba db.DBAccessor
}
func NewTileRenderer(mapblockrenderer *mapblockrenderer.MapBlockRenderer,
@ -29,14 +29,14 @@ func NewTileRenderer(mapblockrenderer *mapblockrenderer.MapBlockRenderer,
return &TileRenderer{
mapblockrenderer: mapblockrenderer,
layers: layers,
tdb: tdb,
dba: dba,
layers: layers,
tdb: tdb,
dba: dba,
}
}
const (
IMG_SIZE = 256
IMG_SIZE = 256
)
func (tr *TileRenderer) Render(tc coords.TileCoords) ([]byte, error) {
@ -90,7 +90,7 @@ func (tr *TileRenderer) RenderImage(tc coords.TileCoords) (*image.NRGBA, error)
var layer *layerconfig.Layer
for _, l := range(tr.layers) {
for _, l := range tr.layers {
if l.Id == tc.LayerId {
layer = &l
}
@ -203,7 +203,6 @@ func (tr *TileRenderer) RenderImage(tc coords.TileCoords) (*image.NRGBA, error)
png.Encode(buf, img)
}
tile := tiledb.Tile{Pos: tc, Data: buf.Bytes(), Mtime: time.Now().Unix()}
tr.tdb.SetTile(&tile)

View File

@ -1,19 +1,19 @@
package tilerenderer
import (
"bytes"
"github.com/sirupsen/logrus"
"io/ioutil"
"mapserver/colormapping"
"mapserver/coords"
"mapserver/db"
"mapserver/mapblockaccessor"
"mapserver/testutils"
"mapserver/mapblockrenderer"
"mapserver/coords"
"mapserver/layerconfig"
"mapserver/mapblockaccessor"
"mapserver/mapblockrenderer"
"mapserver/testutils"
"mapserver/tiledb"
"os"
"testing"
"bytes"
)
func TestTileRender(t *testing.T) {
@ -52,23 +52,23 @@ func TestTileRender(t *testing.T) {
tdb, _ := tiledb.NewSqliteAccessor(tiletmpfile.Name())
tdb.Migrate()
tr := NewTileRenderer(r, tdb, a, layerconfig.DefaultLayers)
tr := NewTileRenderer(r, tdb, a, layerconfig.DefaultLayers)
if tr == nil {
panic("no renderer")
}
if tr == nil {
panic("no renderer")
}
coord := coords.NewTileCoords(0,0,12,0)
coord := coords.NewTileCoords(0, 0, 12, 0)
data, err := tr.Render(coord)
data, err := tr.Render(coord)
if err != nil {
panic(err)
}
if err != nil {
panic(err)
}
if data == nil {
panic("no data")
}
if data == nil {
panic("no data")
}
f, _ := os.Create("../output/0_0_12.png")
bytes.NewReader(data).WriteTo(f)