Skip to content

Commit

Permalink
implemented point to screen resolution translation to scale geometry …
Browse files Browse the repository at this point in the history
…features correctly for screen rendering
  • Loading branch information
ARolek committed Jul 11, 2016
1 parent 82952a9 commit f3b9820
Show file tree
Hide file tree
Showing 9 changed files with 122 additions and 166 deletions.
5 changes: 5 additions & 0 deletions extent.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package tegola

type Extent struct {
Minx, Miny, Maxx, Maxy float64
}
69 changes: 36 additions & 33 deletions mvt/feature.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,14 +61,14 @@ func NewFeatures(geo tegola.Geometry, tags map[string]interface{}) (f []Feature)
}

// VTileFeature will return a vectorTile.Feature that would represent the Feature
func (f *Feature) VTileFeature(keyMap []string, valMap []interface{}, trx, try float64, extent int) (tf *vectorTile.Tile_Feature, err error) {
func (f *Feature) VTileFeature(keyMap []string, valMap []interface{}, extent tegola.Extent, layerExtent int) (tf *vectorTile.Tile_Feature, err error) {
tf = new(vectorTile.Tile_Feature)
tf.Id = f.ID
if tf.Tags, err = keyvalTagsMap(keyMap, valMap, f); err != nil {
return tf, err
}

geo, gtype, err := encodeGeometry(f.Geometry, trx, try, extent)
geo, gtype, err := encodeGeometry(f.Geometry, extent, layerExtent)
if err != nil {
return tf, err
}
Expand Down Expand Up @@ -103,22 +103,20 @@ type cursor struct {
func encodeZigZag(i int64) uint32 {
return uint32((i << 1) ^ (i >> 31))
}
func (c *cursor) NormalizePoint(p tegola.Point) (nx, ny float64) {

/*
nx = int64(p.X())
ny = int64(p.Y())
if nx > int64(c.extent) {
//log.Printf("Point is greater then extent: %v — %v", nx, c.extent)
}
if ny > int64(c.extent) {
//log.Printf("Point is greater then extent: %v — %v", ny, c.extent)
}
*/
return p.X(), p.Y()
// converts a point to a screen resolution point
func (c *cursor) NormalizePoint(extent tegola.Extent, p tegola.Point) (nx, ny float64) {
xspan := extent.Maxx - extent.Minx
yspan := extent.Maxy - extent.Miny

nx = ((p.X() - extent.Minx) * 4096 / xspan)
ny = ((p.Y() - extent.Miny) * 4096 / yspan)

return
}

func (c *cursor) MoveTo(points ...tegola.Point) []uint32 {
func (c *cursor) MoveTo(tile tegola.Extent, points ...tegola.Point) []uint32 {

if len(points) == 0 {
return []uint32{}
}
Expand All @@ -130,7 +128,7 @@ func (c *cursor) MoveTo(points ...tegola.Point) []uint32 {

// range through our points
for _, p := range points {
ix, iy := c.NormalizePoint(p)
ix, iy := c.NormalizePoint(tile, p)
// computer our point delta
dx := int64(ix - c.X)
dy := int64(iy - c.Y)
Expand All @@ -144,18 +142,21 @@ func (c *cursor) MoveTo(points ...tegola.Point) []uint32 {
}
return g
}
func (c *cursor) LineTo(points ...tegola.Point) []uint32 {
func (c *cursor) LineTo(tile tegola.Extent, points ...tegola.Point) []uint32 {
if len(points) == 0 {
return []uint32{}
}
g := make([]uint32, 0, (2*len(points))+1)
g = append(g, (cmdLineTo&0x7)|(uint32(len(points))<<3))
for _, p := range points {
ix, iy := c.NormalizePoint(p)
ix, iy := c.NormalizePoint(tile, p)
dx := int64(ix - c.X)
dy := int64(iy - c.Y)

// update our cursor
c.X = ix
c.Y = iy

g = append(g, encodeZigZag(dx), encodeZigZag(dy))
}
return g
Expand All @@ -167,46 +168,48 @@ func (c *cursor) ClosePath() uint32 {

// encodeGeometry will take a tegola.Geometry type and encode it according to the
// mapbox vector_tile spec.
func encodeGeometry(geo tegola.Geometry, tlx, tly float64, extent int) (g []uint32, vtyp vectorTile.Tile_GeomType, err error) {
func encodeGeometry(geo tegola.Geometry, extent tegola.Extent, layerExtent int) (g []uint32, vtyp vectorTile.Tile_GeomType, err error) {
// new cursor
c := cursor{
X: tlx,
Y: tly,
extent: extent,
X: 0,
Y: 0,
extent: layerExtent,
}

switch t := geo.(type) {
case tegola.Point:
g = append(g, c.MoveTo(t)...)
g = append(g, c.MoveTo(extent, t)...)
return g, vectorTile.Tile_POINT, nil

case tegola.Point3:
g = append(g, c.MoveTo(t)...)
g = append(g, c.MoveTo(extent, t)...)
return g, vectorTile.Tile_POINT, nil

case tegola.MultiPoint:
g = append(g, c.MoveTo(t.Points()...)...)
g = append(g, c.MoveTo(extent, t.Points()...)...)
return g, vectorTile.Tile_POINT, nil

case tegola.LineString:
points := t.Subpoints()
g = append(g, c.MoveTo(points[0])...)
g = append(g, c.LineTo(points[1:]...)...)
g = append(g, c.MoveTo(extent, points[0])...)
g = append(g, c.LineTo(extent, points[1:]...)...)
return g, vectorTile.Tile_LINESTRING, nil

case tegola.MultiLine:
lines := t.Lines()
for _, l := range lines {
points := l.Subpoints()
g = append(g, c.MoveTo(points[0])...)
g = append(g, c.LineTo(points[1:]...)...)
g = append(g, c.MoveTo(extent, points[0])...)
g = append(g, c.LineTo(extent, points[1:]...)...)
}
return g, vectorTile.Tile_LINESTRING, nil

case tegola.Polygon:
lines := t.Sublines()
for _, l := range lines {
points := l.Subpoints()
g = append(g, c.MoveTo(points[0])...)
g = append(g, c.LineTo(points[1:]...)...)
g = append(g, c.MoveTo(extent, points[0])...)
g = append(g, c.LineTo(extent, points[1:]...)...)
g = append(g, c.ClosePath())
}
return g, vectorTile.Tile_POLYGON, nil
Expand All @@ -217,8 +220,8 @@ func encodeGeometry(geo tegola.Geometry, tlx, tly float64, extent int) (g []uint
lines := p.Sublines()
for _, l := range lines {
points := l.Subpoints()
g = append(g, c.MoveTo(points[0])...)
g = append(g, c.LineTo(points[1:]...)...)
g = append(g, c.MoveTo(extent, points[0])...)
g = append(g, c.LineTo(extent, points[1:]...)...)
g = append(g, c.ClosePath())
}
}
Expand Down
52 changes: 51 additions & 1 deletion mvt/feature_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ import (

"github.com/terranodo/tegola"
"github.com/terranodo/tegola/basic"
"github.com/terranodo/tegola/mvt/vector_tile"
)

/*
func TestEncodeGeometry(t *testing.T) {
testcases := []struct {
geo tegola.Geometry
Expand Down Expand Up @@ -135,3 +135,53 @@ func TestNewFeature(t *testing.T) {
}
}
*/

func TestNormalizePoint(t *testing.T) {
/*
fn test_point_to_screen_coords() {
//let zh_mercator = geom::Point::new(949398.0, 6002729.0);
let zh_mercator = geom::Point::new(960000.0, 6002729.0);
//let zh_wgs84 = postgis::Point::<WGS84>::new(47.3703149, 8.5285874);
let tile_extent = Extent {minx: 958826.08, miny: 5987771.04, maxx: 978393.96, maxy: 6007338.92};
let screen_pt = screen::Point::from_geom(&tile_extent, false, 4096, &zh_mercator);
assert_eq!(screen_pt, screen::Point { x: 245, y: 3131 });
assert_eq!(screen_pt.encode().vec(), &[9,490,6262]);
}
*/

testcases := []struct {
point basic.Point
extent tegola.Extent
nx, ny float64
}{
{
point: basic.Point{960000, 6002729},
extent: tegola.Extent{
Minx: 958826.08,
Miny: 5987771.04,
Maxx: 978393.96,
Maxy: 6007338.92,
},
nx: 245,
ny: 3131,
},
}

for i, tcase := range testcases {
c := cursor{
X: 0,
Y: 0,
extent: 4096,
}
nx, ny := c.NormalizePoint(tcase.extent, &tcase.point)

if nx != tcase.nx {
t.Errorf("Test %v: Expected nx value of %v got %v.", i, tcase.nx, nx)
}
if ny != tcase.ny {
t.Errorf("Test %v: Expected ny value of %v got %v.", i, tcase.ny, ny)
}
continue
}
}
5 changes: 3 additions & 2 deletions mvt/layer.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"encoding/binary"
"fmt"

"github.com/terranodo/tegola"
"github.com/terranodo/tegola/mvt/vector_tile"
)

Expand All @@ -26,15 +27,15 @@ func valMapToVTileValue(valMap []interface{}) (vt []*vectorTile.Tile_Value) {
}

// VTileLayer returns a vectorTile Tile_Layer object that represents this Layer.
func (l *Layer) VTileLayer(tlx, tly float64) (*vectorTile.Tile_Layer, error) {
func (l *Layer) VTileLayer(extent tegola.Extent) (*vectorTile.Tile_Layer, error) {
kmap, vmap, err := keyvalMapsFromFeatures(l.features)
if err != nil {
return nil, err
}
valmap := valMapToVTileValue(vmap)
var features = make([]*vectorTile.Tile_Feature, 0, len(l.features))
for _, f := range l.features {
vtf, err := f.VTileFeature(kmap, vmap, tlx, tly, l.Extent())
vtf, err := f.VTileFeature(kmap, vmap, extent, l.Extent())
if err != nil {
return nil, fmt.Errorf("Error getting VTileFeature: %v", err)
}
Expand Down
9 changes: 2 additions & 7 deletions mvt/layer_test.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,6 @@
package mvt

import (
"testing"

"github.com/terranodo/tegola/basic"
"github.com/terranodo/tegola/mvt/vector_tile"
)

/*
func newTileLayer(name string, keys []string, values []*vectorTile.Tile_Value, features []*vectorTile.Tile_Feature) *vectorTile.Tile_Layer {
version := uint32(2)
extent := uint32(4096)
Expand Down Expand Up @@ -115,3 +109,4 @@ func TestLayer(t *testing.T) {
}
}
*/
7 changes: 4 additions & 3 deletions mvt/tile.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package mvt
import (
"fmt"

"github.com/terranodo/tegola"
"github.com/terranodo/tegola/mvt/vector_tile"
)

Expand Down Expand Up @@ -32,12 +33,12 @@ func (t *Tile) Layers() (l []Layer) {
return l
}

// VTile returns a tile object according to the Google Protobuff def. This function
//VTile returns a tile object according to the Google Protobuff def. This function
// does the hard work of converting everthing to the standard.
func (t *Tile) VTile(ulx, uly float64) (vt *vectorTile.Tile, err error) {
func (t *Tile) VTile(extent tegola.Extent) (vt *vectorTile.Tile, err error) {
vt = new(vectorTile.Tile)
for _, l := range t.layers {
vtl, err := l.VTileLayer(ulx, uly)
vtl, err := l.VTileLayer(extent)
if err != nil {
return nil, fmt.Errorf("Error Getting VTileLayer: %v", err)
}
Expand Down
9 changes: 1 addition & 8 deletions provider/postgis/postgis.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package postgis
import (
"bytes"
"fmt"
"log"
"strings"
"text/template"

Expand Down Expand Up @@ -84,8 +83,7 @@ func NewProvider(config Config) (*Provider, error) {
FROM
%[1]v
WHERE
geom && {{.BBox}}
LIMIT 1`, tplStr)
geom && {{.BBox}}`, tplStr)
}

_, err := tpl.Parse(tplStr)
Expand Down Expand Up @@ -125,8 +123,6 @@ func (p *Provider) MVTLayer(layerName string, tile tegola.Tile) (layer *mvt.Laye

sql := sr.String()

log.Printf("Running sql:\n%v\n", sql)

// execute query
rows, err := p.pool.Query(sql)
if err != nil {
Expand Down Expand Up @@ -188,8 +184,5 @@ func (p *Provider) MVTLayer(layerName string, tile tegola.Tile) (layer *mvt.Laye
})
}

log.Printf("# of rows for tile /%v/%v/%v: %v\n", tile.Z, tile.X, tile.Y, rowsCount)

// log.Printf("Layer looks like %+v\n", layer)
return layer, nil
}
Loading

0 comments on commit f3b9820

Please sign in to comment.