Skip to content

Commit

Permalink
Add some testing
Browse files Browse the repository at this point in the history
  • Loading branch information
flopp committed Apr 21, 2024
1 parent e7c58d0 commit 71a7dd7
Show file tree
Hide file tree
Showing 6 changed files with 112 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,5 @@ jobs:
run: $HOME/go/bin/gocyclo -over 19 $(find . -iname '*.go' -type f)
- name: fmt
run: test -z $(gofmt -s -l $(find . -iname '*.go' -type f))
- name: test
run: go test ./...
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
*.png
.DS_Store
4 changes: 4 additions & 0 deletions context.go
Original file line number Diff line number Diff line change
Expand Up @@ -634,6 +634,10 @@ func (m *Context) RenderWithBounds() (image.Image, s2.Rect, error) {
}

func (m *Context) renderLayer(gc *gg.Context, zoom int, trans *Transformer, tileSize int, provider *TileProvider) error {
if provider.IsNone() {
return nil
}

var wg sync.WaitGroup
tiles := (1 << uint(zoom))
fetchedTiles := make(chan *Tile)
Expand Down
52 changes: 52 additions & 0 deletions context_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package sm

import (
"image/color"
"testing"

"github.com/golang/geo/s2"
)

func TestRenderEverything(t *testing.T) {
width := 640
height := 480

ctx := NewContext()
ctx.SetSize(width, height)
ctx.SetTileProvider(NewTileProviderNone())
ctx.SetBackground(color.RGBA{255, 255, 255, 255})

coords1 := s2.LatLngFromDegrees(48.123, 7.0)
coords2 := s2.LatLngFromDegrees(48.987, 8.0)
coords3 := s2.LatLngFromDegrees(47.987, 7.5)
coords4 := s2.LatLngFromDegrees(48.123, 9.0)

p := make([]s2.LatLng, 0, 3)
p = append(p, coords1)
p = append(p, coords2)
p = append(p, coords3)
path := NewPath(p, color.RGBA{0, 0, 255, 255}, 4.0)

a := make([]s2.LatLng, 0, 3)
a = append(a, coords1)
a = append(a, coords3)
a = append(a, coords4)
area := NewArea(a, color.RGBA{255, 0, 0, 255}, color.RGBA{255, 255, 0, 50}, 3.0)

marker := NewMarker(coords1, color.RGBA{255, 0, 0, 255}, 16.0)
circle := NewCircle(coords2, color.RGBA{0, 255, 0, 255}, color.RGBA{0, 255, 0, 100}, 10000.0, 2.0)

ctx.AddObject(area)
ctx.AddObject(path)
ctx.AddObject(marker)
ctx.AddObject(circle)

img, err := ctx.Render()
if err != nil {
t.Errorf("failed to render: %v", err)
}

if img.Bounds().Dx() != width || img.Bounds().Dy() != height {
t.Errorf("unexpected image size: %d x %d; expected %d x %d", img.Bounds().Dx(), img.Bounds().Dy(), width, height)
}
}
34 changes: 34 additions & 0 deletions examples/idl/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// This is an example on how to use a multiline attribution string.

package main

import (
"image/color"

sm "github.com/flopp/go-staticmaps"
"github.com/fogleman/gg"
"github.com/golang/geo/s2"
)

func main() {
ctx := sm.NewContext()
ctx.SetSize(1920, 1080)

newyork := sm.NewMarker(s2.LatLngFromDegrees(40.641766, -73.780968), color.RGBA{255, 0, 0, 255}, 16.0)
hongkong := sm.NewMarker(s2.LatLngFromDegrees(22.308046, 113.918480), color.RGBA{0, 0, 255, 255}, 16.0)
ctx.AddObject(newyork)
ctx.AddObject(hongkong)
path := make([]s2.LatLng, 0, 2)
path = append(path, newyork.Position)
path = append(path, hongkong.Position)
ctx.AddObject(sm.NewPath(path, color.RGBA{0, 255, 0, 255}, 4.0))

img, err := ctx.Render()
if err != nil {
panic(err)
}

if err := gg.SavePNG("idl.png", img); err != nil {
panic(err)
}
}
19 changes: 19 additions & 0 deletions tile_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,14 @@ type TileProvider struct {
APIKey string
}

func (t TileProvider) IsNone() bool {
return len(t.URLPattern) == 0
}

func (t *TileProvider) getURL(shard string, zoom, x, y int, apikey string) string {
if t.IsNone() {
return ""
}
return fmt.Sprintf(t.URLPattern, shard, zoom, x, y, apikey)
}

Expand Down Expand Up @@ -145,6 +152,17 @@ func NewTileProviderArcgisWorldImagery() *TileProvider {
return t
}

// NewTileNone creates a TileProvider struct that does not provide any tiles
func NewTileProviderNone() *TileProvider {
t := new(TileProvider)
t.Name = "none"
t.Attribution = ""
t.TileSize = 256
t.URLPattern = ""
t.Shards = []string{}
return t
}

// GetTileProviders returns a map of all available TileProviders
func GetTileProviders(thunderforestApiKey string) map[string]*TileProvider {
m := make(map[string]*TileProvider)
Expand All @@ -162,6 +180,7 @@ func GetTileProviders(thunderforestApiKey string) map[string]*TileProvider {
NewTileProviderCartoDark(),
NewTileProviderArcgisWorldImagery(),
NewTileProviderWikimedia(),
NewTileProviderNone(),
}

for _, tp := range list {
Expand Down

0 comments on commit 71a7dd7

Please sign in to comment.