Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make TileProvider an interface (or: maybe this is just an FYI rather than a PR...) #33

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 14 additions & 14 deletions context.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,10 @@ type Context struct {
paths []*Path
areas []*Area
circles []*Circle
overlays []*TileProvider
overlays []TileProvider

userAgent string
tileProvider *TileProvider
tileProvider TileProvider

overrideAttribution *string
}
Expand All @@ -61,7 +61,7 @@ func NewContext() *Context {
}

// SetTileProvider sets the TileProvider to be used
func (m *Context) SetTileProvider(t *TileProvider) {
func (m *Context) SetTileProvider(t TileProvider) {
m.tileProvider = t
}

Expand Down Expand Up @@ -140,7 +140,7 @@ func (m *Context) ClearCircles() {
}

// AddOverlay adds an overlay to the Context
func (m *Context) AddOverlay(overlay *TileProvider) {
func (m *Context) AddOverlay(overlay TileProvider) {
m.overlays = append(m.overlays, overlay)
}

Expand Down Expand Up @@ -205,7 +205,7 @@ func (m *Context) determineZoom(bounds s2.Rect, center s2.LatLng) int {
return 15
}

tileSize := m.tileProvider.TileSize
tileSize := m.tileProvider.TileSize()
margin := 4.0 + m.determineExtraMarginPixels()
w := (float64(m.width) - 2.0*margin) / float64(tileSize)
h := (float64(m.height) - 2.0*margin) / float64(tileSize)
Expand Down Expand Up @@ -350,7 +350,7 @@ func (m *Context) Render() (image.Image, error) {
return nil, err
}

tileSize := m.tileProvider.TileSize
tileSize := m.tileProvider.TileSize()
trans := newTransformer(m.width, m.height, zoom, center, tileSize)
img := image.NewRGBA(image.Rect(0, 0, trans.pWidth, trans.pHeight))
gc := gg.NewContextForRGBA(img)
Expand All @@ -359,7 +359,7 @@ func (m *Context) Render() (image.Image, error) {
}

// fetch and draw tiles to img
layers := []*TileProvider{m.tileProvider}
layers := []TileProvider{m.tileProvider}
if m.overlays != nil {
layers = append(layers, m.overlays...)
}
Expand Down Expand Up @@ -390,7 +390,7 @@ func (m *Context) Render() (image.Image, error) {
img, image.Point{trans.pCenterX - int(m.width)/2, trans.pCenterY - int(m.height)/2},
draw.Src)

attribution := m.tileProvider.Attribution
attribution := m.tileProvider.Attribution()
if m.overrideAttribution != nil {
attribution = *m.overrideAttribution
}
Expand Down Expand Up @@ -422,7 +422,7 @@ func (m *Context) RenderWithBounds() (image.Image, s2.Rect, error) {
return nil, s2.Rect{}, err
}

tileSize := m.tileProvider.TileSize
tileSize := m.tileProvider.TileSize()
trans := newTransformer(m.width, m.height, zoom, center, tileSize)
img := image.NewRGBA(image.Rect(0, 0, trans.pWidth, trans.pHeight))
gc := gg.NewContextForRGBA(img)
Expand All @@ -431,7 +431,7 @@ func (m *Context) RenderWithBounds() (image.Image, s2.Rect, error) {
}

// fetch and draw tiles to img
layers := []*TileProvider{m.tileProvider}
layers := []TileProvider{m.tileProvider}
if m.overlays != nil {
layers = append(layers, m.overlays...)
}
Expand All @@ -457,21 +457,21 @@ func (m *Context) RenderWithBounds() (image.Image, s2.Rect, error) {
}

// draw attribution
if m.tileProvider.Attribution == "" {
if m.tileProvider.Attribution() == "" {
return img, trans.Rect(), nil
}
_, textHeight := gc.MeasureString(m.tileProvider.Attribution)
_, textHeight := gc.MeasureString(m.tileProvider.Attribution())
boxHeight := textHeight + 4.0
gc.SetRGBA(0.0, 0.0, 0.0, 0.5)
gc.DrawRectangle(0.0, float64(trans.pHeight)-boxHeight, float64(trans.pWidth), boxHeight)
gc.Fill()
gc.SetRGBA(1.0, 1.0, 1.0, 0.75)
gc.DrawString(m.tileProvider.Attribution, 4.0, float64(m.height)-4.0)
gc.DrawString(m.tileProvider.Attribution(), 4.0, float64(m.height)-4.0)

return img, trans.Rect(), nil
}

func (m *Context) renderLayer(gc *gg.Context, zoom int, trans *transformer, tileSize int, provider *TileProvider) error {
func (m *Context) renderLayer(gc *gg.Context, zoom int, trans *transformer, tileSize int, provider TileProvider) error {
t := NewTileFetcher(provider)
if m.userAgent != "" {
t.SetUserAgent(m.userAgent)
Expand Down
44 changes: 26 additions & 18 deletions tile_fetcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ import (
_ "image/jpeg" // to be able to decode jpegs
_ "image/png" // to be able to decode pngs
"io"
"io/ioutil"
//"io/ioutil"
"log"
"net/http"
//"net/http"
"os"
"path/filepath"

Expand All @@ -23,14 +23,14 @@ import (

// TileFetcher downloads map tile images from a TileProvider
type TileFetcher struct {
tileProvider *TileProvider
tileProvider TileProvider
cacheDir string
useCaching bool
userAgent string
}

// NewTileFetcher creates a new Tilefetcher struct
func NewTileFetcher(tileProvider *TileProvider) *TileFetcher {
func NewTileFetcher(tileProvider TileProvider) *TileFetcher {
t := new(TileFetcher)
t.tileProvider = tileProvider
app := appdirs.New("go-staticmaps", "flopp.net", "0.1")
Expand All @@ -45,15 +45,6 @@ func (t *TileFetcher) SetUserAgent(a string) {
t.userAgent = a
}

func (t *TileFetcher) url(zoom, x, y int) string {
shard := ""
ss := len(t.tileProvider.Shards)
if len(t.tileProvider.Shards) > 0 {
shard = t.tileProvider.Shards[(x+y)%ss]
}
return t.tileProvider.getURL(shard, zoom, x, y)
}

func (t *TileFetcher) cacheFileName(zoom int, x, y int) string {
return fmt.Sprintf("%s/%d/%d/%d", t.cacheDir, zoom, x, y)
}
Expand All @@ -65,6 +56,7 @@ func (t *TileFetcher) ToggleCaching(enabled bool) {

// Fetch download (or retrieves from the cache) a tile image for the specified zoom level and tile coordinates
func (t *TileFetcher) Fetch(zoom, x, y int) (image.Image, error) {

if t.useCaching {
fileName := t.cacheFileName(zoom, x, y)
cachedImg, err := t.loadCache(fileName)
Expand All @@ -73,11 +65,15 @@ func (t *TileFetcher) Fetch(zoom, x, y int) (image.Image, error) {
}
}

url := t.url(zoom, x, y)
data, err := t.download(url)
if err != nil {
return nil, err
}
data, err := t.tileProvider.FetchTile(zoom, x, y)

/*
url := t.url(zoom, x, y)
data, err := t.download(url)
if err != nil {
return nil, err
}
*/

img, _, err := image.Decode(bytes.NewBuffer(data))
if err != nil {
Expand All @@ -94,6 +90,17 @@ func (t *TileFetcher) Fetch(zoom, x, y int) (image.Image, error) {
return img, nil
}

/*
func (t *TileFetcher) url(zoom, x, y int) string {
possible_shards := t.tileProvider.Shards()
shard := ""
ss := len(possible_shards)
if len(possible_shards) > 0 {
shard = possible_shards[(x+y)%ss]
}
return t.tileProvider.URL(shard, zoom, x, y)
}

func (t *TileFetcher) download(url string) ([]byte, error) {
req, _ := http.NewRequest("GET", url, nil)
req.Header.Set("User-Agent", t.userAgent)
Expand All @@ -116,6 +123,7 @@ func (t *TileFetcher) download(url string) ([]byte, error) {

return contents, nil
}
*/

func (t *TileFetcher) loadCache(fileName string) (image.Image, error) {
file, err := os.Open(fileName)
Expand Down
Loading