Skip to content

Commit

Permalink
switched to fogleman's gg
Browse files Browse the repository at this point in the history
  • Loading branch information
flopp committed Feb 20, 2016
1 parent cc94b91 commit 9b19840
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 744 deletions.
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,15 @@ $ create-static-map --width 600 --height 400 -o map3.png -m "red|52.514536,13.35

![Static map of Australia](https://raw.githubusercontent.com/flopp/flopp.github.io/master/go-staticmaps/australia.png)

## Acknowledgements
Besides the go standard library, go-staticmaps uses

- MapQuest (https://developer.mapquest.com/) and Thunderforest (http://www.thunderforest.com/) as map tile providers
- Go Graphics (https://github.com/fogleman/gg) for 2D drawing
- S2 geometry library (https://github.com/golang/geo) for spherical geometry calculations
- appdirs (https://github.com/Wessie/appdirs) for platform specific system directories
- go-coordsparser (https://github.com/flopp/go-coordsparser) for parsing geo coordinates

## License
Copyright 2016 Florian Pigorsch. All rights reserved.

Expand Down
8 changes: 5 additions & 3 deletions cmd/create-static-map/create-static-map.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,11 @@ func main() {

m := staticmaps.NewMapCreator()

tp := getTileProviderOrExit(opts.Type)
if tp != nil {
m.SetTileProvider(tp)
if parser.FindOptionByLongName("type").IsSet() {
tp := getTileProviderOrExit(opts.Type)
if tp != nil {
m.SetTileProvider(tp)
}
}

m.SetSize(opts.Width, opts.Height)
Expand Down
676 changes: 0 additions & 676 deletions staticmaps/assets.go

This file was deleted.

54 changes: 12 additions & 42 deletions staticmaps/map_creator.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,33 +8,13 @@ package staticmaps
import (
"errors"
"image"
"image/color"
"image/draw"
"log"
"math"

"github.com/golang/freetype/truetype"
"github.com/fogleman/gg"
"github.com/golang/geo/s2"
"github.com/llgcode/draw2d"
"github.com/llgcode/draw2d/draw2dimg"
)

var (
isFontLoaded = false
)

func loadFont() {
if !isFontLoaded {
isFontLoaded = true

font, err := truetype.Parse(ttfFontData)
if err != nil {
log.Panic(err)
}
draw2d.RegisterFont(draw2d.FontData{Name: "font", Family: draw2d.FontFamilySans, Style: draw2d.FontStyleNormal}, font)
}
}

// MapCreator class
type MapCreator struct {
width int
Expand Down Expand Up @@ -236,39 +216,29 @@ func (m *MapCreator) Create() (image.Image, error) {
}
}

gc := draw2dimg.NewGraphicContext(img)
dc := gg.NewContextForRGBA(img)

for _, path := range m.paths {
path.draw(gc, trans)
path.draw(dc, trans)
}

for _, marker := range m.markers {
marker.draw(gc, trans)
marker.draw(dc, trans)
}

croppedImg := image.NewRGBA(image.Rect(0, 0, int(m.width), int(m.height)))
draw.Draw(croppedImg, image.Rect(0, 0, int(m.width), int(m.height)),
img, image.Point{trans.pCenterX - int(m.width)/2, trans.pCenterY - int(m.height)/2},
draw.Src)

// draw attribution box
gc = draw2dimg.NewGraphicContext(croppedImg)

gc.SetFillColor(color.RGBA{0, 0, 0, 0x7f})
gc.MoveTo(0, float64(m.height)-14.0)
gc.LineTo(float64(m.width), float64(m.height)-14.0)
gc.LineTo(float64(m.width), float64(m.height))
gc.LineTo(0, float64(m.height))
gc.Close()
gc.Fill()

// draw attribution
loadFont()
gc.SetFontData(draw2d.FontData{Name: "font", Family: draw2d.FontFamilySans, Style: draw2d.FontStyleNormal})
gc.SetStrokeColor(color.RGBA{0xff, 0xff, 0xff, 0xff})
gc.SetFillColor(color.RGBA{0xff, 0xff, 0xff, 0xff})
gc.SetFontSize(8)
gc.FillStringAt(m.tileProvider.Attribution, 4.0, float64(m.height)-4.0)
_, textHeight := dc.MeasureString(m.tileProvider.Attribution)
boxHeight := textHeight + 4.0
dc = gg.NewContextForRGBA(croppedImg)
dc.SetRGBA(0.0, 0.0, 0.0, 0.5)
dc.DrawRectangle(0.0, float64(m.height)-boxHeight, float64(m.width), boxHeight)
dc.Fill()
dc.SetRGBA(1.0, 1.0, 1.0, 0.75)
dc.DrawString(m.tileProvider.Attribution, 4.0, float64(m.height)-4.0)

return croppedImg, nil
}
23 changes: 14 additions & 9 deletions staticmaps/marker.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ import (
"strings"

"github.com/flopp/go-coordsparser"
"github.com/fogleman/gg"
"github.com/golang/geo/s2"
"github.com/llgcode/draw2d/draw2dimg"
)

// Marker represents a marker on the map
Expand Down Expand Up @@ -68,14 +68,19 @@ func ParseMarkerString(s string) ([]Marker, error) {
return markers, nil
}

func (m *Marker) draw(gc *draw2dimg.GraphicContext, trans *transformer) {
gc.SetStrokeColor(color.RGBA{0, 0, 0, 0xff})
gc.SetFillColor(m.Color)
gc.SetLineWidth(1.0)
func (m *Marker) draw(dc *gg.Context, trans *transformer) {
dc.ClearPath()

dc.SetLineJoin(gg.LineJoinRound)
dc.SetLineWidth(1.0)

radius := 0.5 * m.Size
x, y := trans.ll2p(m.Position)
gc.ArcTo(x, y-m.Size, radius, radius, 150.0*math.Pi/180.0, 240.0*math.Pi/180.0)
gc.LineTo(x, y)
gc.Close()
gc.FillStroke()
dc.DrawArc(x, y-m.Size, radius, (90.0+60.0)*math.Pi/180.0, (360.0+90.0-60.0)*math.Pi/180.0)
dc.LineTo(x, y)
dc.ClosePath()
dc.SetColor(m.Color)
dc.FillPreserve()
dc.SetRGB(0, 0, 0)
dc.Stroke()
}
27 changes: 16 additions & 11 deletions staticmaps/path.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ import (
"strings"

"github.com/flopp/go-coordsparser"
"github.com/fogleman/gg"
"github.com/golang/geo/s2"
"github.com/llgcode/draw2d/draw2dimg"
)

// Path represents a path or area on the map
Expand Down Expand Up @@ -59,24 +59,29 @@ func ParsePathString(s string) (Path, error) {
return path, nil
}

func (p *Path) draw(gc *draw2dimg.GraphicContext, trans *transformer) {
func (p *Path) draw(dc *gg.Context, trans *transformer) {
if len(p.Positions) <= 1 {
return
}

gc.SetStrokeColor(p.Color)
gc.SetFillColor(p.FillColor)
gc.SetLineWidth(p.Weight)
dc.ClearPath()

gc.MoveTo(trans.ll2p(p.Positions[0]))
for _, ll := range p.Positions[1:] {
gc.LineTo(trans.ll2p(ll))
dc.SetLineWidth(p.Weight)
dc.SetLineCap(gg.LineCapRound)
dc.SetLineJoin(gg.LineJoinRound)

for _, ll := range p.Positions {
dc.LineTo(trans.ll2p(ll))
}

if p.IsFilled {
gc.Close()
gc.FillStroke()
dc.ClosePath()
dc.SetColor(p.FillColor)
dc.FillPreserve()
dc.SetColor(p.Color)
dc.Stroke()
} else {
gc.Stroke()
dc.SetColor(p.Color)
dc.Stroke()
}
}
6 changes: 3 additions & 3 deletions staticmaps/tile_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func (t *TileProvider) GetURL(shard string, zoom, x, y int) string {
func NewTileProviderMapQuest() *TileProvider {
t := new(TileProvider)
t.Name = "mapquest"
t.Attribution = "Maps © MapQuest; Data © OSM and contributors, ODbL"
t.Attribution = "Maps (c) MapQuest; Data (c) OSM and contributors, ODbL"
t.TileSize = 256
t.URLPattern = "http://otile%[1]s.mqcdn.com/tiles/1.0.0/osm/%[2]d/%[3]d/%[4]d.png"
t.Shards = []string{"1", "2", "3", "4"}
Expand All @@ -32,7 +32,7 @@ func NewTileProviderMapQuest() *TileProvider {
func newTileProviderThunderforest(name string) *TileProvider {
t := new(TileProvider)
t.Name = fmt.Sprintf("thunderforest-%s", name)
t.Attribution = "Maps © Thundeforest; Data © OSM and contributors, ODbL"
t.Attribution = "Maps (c) Thundeforest; Data (c) OSM and contributors, ODbL"
t.TileSize = 256
t.URLPattern = "https://%[1]s.tile.thunderforest.com/" + name + "/%[2]d/%[3]d/%[4]d.png"
t.Shards = []string{"a", "b", "c"}
Expand All @@ -54,7 +54,7 @@ func NewTileProviderThunderforestTransport() *TileProvider {
func NewTileProviderStamenToner() *TileProvider {
t := new(TileProvider)
t.Name = "stamen-toner"
t.Attribution = "Maps © Stamen; Data © OSM and contributors, ODbL"
t.Attribution = "Maps (c) Stamen; Data (c) OSM and contributors, ODbL"
t.TileSize = 256
t.URLPattern = "http://%[1]s.tile.stamen.com/toner/%[2]d/%[3]d/%[4]d.png"
t.Shards = []string{"a", "b", "c", "d"}
Expand Down

0 comments on commit 9b19840

Please sign in to comment.