1
0
forked from MTSR/mapserver
mapserver/db/postgres/initialblocks.go

194 lines
4.0 KiB
Go
Raw Permalink Normal View History

2019-02-14 11:02:46 +03:00
package postgres
import (
2019-02-14 16:16:32 +03:00
"mapserver/coords"
2019-02-14 11:02:46 +03:00
"mapserver/db"
2019-02-14 11:05:39 +03:00
"mapserver/settings"
2023-12-29 18:00:11 +03:00
"mapserver/types"
2019-02-14 21:59:35 +03:00
"math"
"github.com/sirupsen/logrus"
2019-02-14 11:02:46 +03:00
)
2019-02-14 11:05:39 +03:00
const (
2019-02-14 16:16:32 +03:00
SETTING_LAST_LAYER = "last_layer"
2019-02-14 16:04:01 +03:00
SETTING_LAST_X_BLOCK = "last_x_block"
SETTING_LAST_Y_BLOCK = "last_y_block"
2019-02-14 11:05:39 +03:00
)
2019-02-14 11:02:46 +03:00
func (a *PostgresAccessor) countBlocks(x1, y1, z1, x2, y2, z2 int) (int, error) {
rows, err := a.db.Query(getBlockCountByInitialTileQuery,
2019-02-22 21:26:52 +03:00
x1, y1, z1, x2, y2, z2,
)
if err != nil {
return 0, err
}
defer rows.Close()
if rows.Next() {
2019-02-22 21:26:52 +03:00
var count int
err = rows.Scan(&count)
if err != nil {
return 0, err
}
return count, nil
}
return 0, nil
}
2019-02-14 16:04:01 +03:00
// x -> 0 ... 256
//zoom/mapblock-width
//13 1
//12 2
//11 4
//10 8
//9 16
//Zoom 9:
//10 mapblocks height * 16 * 16 == 2560
2023-12-29 18:00:11 +03:00
func (a *PostgresAccessor) FindNextInitialBlocks(s settings.Settings, layers []*types.Layer, limit int) (*db.InitialBlocksResult, error) {
2019-02-14 11:13:18 +03:00
2019-02-14 16:04:01 +03:00
lastlayer := s.GetInt(SETTING_LAST_LAYER, 0)
2019-02-15 09:36:31 +03:00
lastxblock := s.GetInt(SETTING_LAST_X_BLOCK, -129)
2019-02-14 16:04:01 +03:00
lastyblock := s.GetInt(SETTING_LAST_Y_BLOCK, -128)
if lastxblock >= 128 {
lastxblock = -128
lastyblock++
} else {
lastxblock++
}
if lastyblock > 128 {
//done
2019-02-15 11:04:34 +03:00
var nextlayer = lastlayer
for _, l := range layers {
if l.Id > nextlayer {
nextlayer = l.Id
break
}
}
s.SetInt(SETTING_LAST_LAYER, nextlayer)
s.SetInt(SETTING_LAST_X_BLOCK, -129)
s.SetInt(SETTING_LAST_Y_BLOCK, -128)
2019-02-14 16:04:01 +03:00
result := &db.InitialBlocksResult{}
2019-02-15 11:04:34 +03:00
result.HasMore = nextlayer != lastlayer
2019-02-14 16:04:01 +03:00
return result, nil
}
tc := coords.NewTileCoords(lastxblock, lastyblock, 9, lastlayer)
2023-12-29 18:00:11 +03:00
currentlayer := types.FindLayerById(layers, lastlayer)
2019-02-14 16:04:01 +03:00
2019-02-15 09:36:31 +03:00
tcr := coords.GetMapBlockRangeFromTile(tc, 0)
tcr.Pos1.Y = currentlayer.From
tcr.Pos2.Y = currentlayer.To
2019-02-14 16:04:01 +03:00
fields := logrus.Fields{
2019-02-15 11:08:22 +03:00
"layerId": lastlayer,
"pos1": tcr.Pos1,
"pos2": tcr.Pos2,
"tile": tc,
2019-02-14 16:04:01 +03:00
}
2019-02-19 11:06:12 +03:00
log.WithFields(fields).Debug("Initial-Query")
2019-02-15 09:36:31 +03:00
minX := int(math.Min(float64(tcr.Pos1.X), float64(tcr.Pos2.X)))
maxX := int(math.Max(float64(tcr.Pos1.X), float64(tcr.Pos2.X)))
minY := int(math.Min(float64(tcr.Pos1.Y), float64(tcr.Pos2.Y)))
maxY := int(math.Max(float64(tcr.Pos1.Y), float64(tcr.Pos2.Y)))
minZ := int(math.Min(float64(tcr.Pos1.Z), float64(tcr.Pos2.Z)))
maxZ := int(math.Max(float64(tcr.Pos1.Z), float64(tcr.Pos2.Z)))
if lastxblock <= -128 {
//first x entry, check z stride
stridecount := a.intQuery(`
2019-02-15 09:36:31 +03:00
select count(*) from blocks
where posz >= $1 and posz <= $2
2019-02-15 19:33:49 +03:00
and posy >= $3 and posy <= $4`,
2019-02-15 09:36:31 +03:00
minZ, maxZ,
minY, maxY,
)
if stridecount == 0 {
fields = logrus.Fields{
"minX": minX,
"maxX": maxX,
"minY": minY,
"maxY": maxY,
}
log.WithFields(fields).Debug("Skipping stride")
2019-02-15 00:20:56 +03:00
2019-02-15 09:36:31 +03:00
s.SetInt(SETTING_LAST_LAYER, lastlayer)
s.SetInt(SETTING_LAST_X_BLOCK, -129)
s.SetInt(SETTING_LAST_Y_BLOCK, lastyblock+1)
2019-02-15 00:20:56 +03:00
2019-02-15 09:36:31 +03:00
result := &db.InitialBlocksResult{}
2019-02-15 11:08:22 +03:00
result.Progress = float64(((lastyblock+128)*256)+(lastxblock+128)) / float64(256*256)
2019-02-15 09:36:31 +03:00
result.HasMore = true
return result, nil
}
2019-02-15 00:20:56 +03:00
}
count, err := a.countBlocks(minX, minY, minZ, maxX, maxY, maxZ)
2019-02-14 16:04:01 +03:00
if err != nil {
return nil, err
}
blocks := make([]*db.Block, 0)
2019-02-15 19:33:49 +03:00
var lastmtime int64
2019-02-14 16:04:01 +03:00
2019-02-22 21:26:52 +03:00
if count > 0 {
rows, err := a.db.Query(getBlocksByInitialTileQuery,
2019-02-22 21:26:52 +03:00
minX, minY, minZ, maxX, maxY, maxZ,
)
2019-02-14 16:04:01 +03:00
2019-02-15 23:32:09 +03:00
if err != nil {
return nil, err
2019-02-15 00:20:56 +03:00
}
2019-02-15 23:32:09 +03:00
2019-02-22 21:26:52 +03:00
defer rows.Close()
for rows.Next() {
var posx, posy, posz int
var data []byte
var mtime int64
err = rows.Scan(&posx, &posy, &posz, &data, &mtime)
if err != nil {
return nil, err
}
2019-02-15 23:32:09 +03:00
2019-02-22 21:26:52 +03:00
if mtime > lastmtime {
lastmtime = mtime
}
mb := convertRows(posx, posy, posz, data, mtime)
blocks = append(blocks, mb)
}
2019-02-14 16:04:01 +03:00
}
2019-02-14 21:59:35 +03:00
s.SetInt(SETTING_LAST_LAYER, lastlayer)
s.SetInt(SETTING_LAST_X_BLOCK, lastxblock)
s.SetInt(SETTING_LAST_Y_BLOCK, lastyblock)
2019-02-14 16:04:01 +03:00
result := &db.InitialBlocksResult{}
2019-02-15 19:33:49 +03:00
result.LastMtime = lastmtime
2019-02-15 11:08:22 +03:00
result.Progress = float64(((lastyblock+128)*256)+(lastxblock+128)) / float64(256*256)
2019-02-14 16:04:01 +03:00
result.List = blocks
result.HasMore = true
2019-02-14 11:13:18 +03:00
2019-02-14 16:04:01 +03:00
return result, nil
2019-02-14 11:02:46 +03:00
}