From e8930d65bbe4ab4dcccd14e36d1a43dfab95d4a3 Mon Sep 17 00:00:00 2001 From: flopp Date: Sun, 28 Feb 2016 20:46:33 +0100 Subject: [PATCH] simplified command line parsing --- README.md | 2 +- area.go | 15 ++++++--------- marker.go | 25 +++++++++++++++---------- path.go | 11 ++++------- tile_fetcher.go | 6 +++--- util.go | 18 ++++++++++++++++++ 6 files changed, 47 insertions(+), 30 deletions(-) create mode 100644 util.go diff --git a/README.md b/README.md index b8cd993..95c389f 100644 --- a/README.md +++ b/README.md @@ -91,7 +91,7 @@ The `--marker` option defines one or more map markers of the same style. Use mul `MARKER_STYLES` consists of a set of style descriptors separated by the pipe character `|`: - `color:COLOR` - where `COLOR` is either of the form `0xRRGGBB`, `0xRRGGBBAA`, or one of `black`, `blue`, `brown`, `green`, `orange`, `purple`, `red`, `yellow`, `white` (default: `red`) -- `size:SIZE` - where `SIZE` is one of `mid`, `small`, `tiny` (default: `mid`) +- `size:SIZE` - where `SIZE` is one of `mid`, `small`, `tiny`, or some number > 0 (default: `mid`) - `label:LABEL` - where `LABEL` is an alpha numeric character, i.e. `A`-`Z`, `a`-`z`, `0`-`9`; (default: no label) ### Paths diff --git a/area.go b/area.go index 6e0318f..387105a 100644 --- a/area.go +++ b/area.go @@ -32,21 +32,21 @@ func ParseAreaString(s string) (*Area, error) { area.Weight = 5.0 for _, ss := range strings.Split(s, "|") { - if strings.HasPrefix(ss, "color:") { + if ok, suffix := hasPrefix(ss, "color:"); ok { var err error - area.Color, err = ParseColorString(strings.TrimPrefix(ss, "color:")) + area.Color, err = ParseColorString(suffix) if err != nil { return nil, err } - } else if strings.HasPrefix(ss, "fill:") { + } else if ok, suffix := hasPrefix(ss, "fill:"); ok { var err error - area.Fill, err = ParseColorString(strings.TrimPrefix(ss, "fill:")) + area.Fill, err = ParseColorString(suffix) if err != nil { return nil, err } - } else if strings.HasPrefix(ss, "weight:") { + } else if ok, suffix := hasPrefix(ss, "weight:"); ok { var err error - area.Weight, err = strconv.ParseFloat(strings.TrimPrefix(ss, "weight:"), 64) + area.Weight, err = strconv.ParseFloat(suffix, 64) if err != nil { return nil, err } @@ -80,15 +80,12 @@ func (p *Area) draw(gc *gg.Context, trans *transformer) { } gc.ClearPath() - gc.SetLineWidth(p.Weight) gc.SetLineCap(gg.LineCapRound) gc.SetLineJoin(gg.LineJoinRound) - for _, ll := range p.Positions { gc.LineTo(trans.ll2p(ll)) } - gc.ClosePath() gc.SetColor(p.Fill) gc.FillPreserve() diff --git a/marker.go b/marker.go index 5cefbd0..6e88984 100644 --- a/marker.go +++ b/marker.go @@ -9,6 +9,7 @@ import ( "fmt" "image/color" "math" + "strconv" "strings" "github.com/flopp/go-coordsparser" @@ -36,14 +37,19 @@ func NewMarker(pos s2.LatLng, col color.Color, size float64) *Marker { } func parseSizeString(s string) (float64, error) { - if s == "mid" { + switch { + case s == "mid": return 16.0, nil - } else if s == "small" { + case s == "small": return 12.0, nil - } else if s == "tiny" { + case s == "tiny": return 8.0, nil } + if ss, err := strconv.ParseFloat(s, 64); err != nil && ss > 0 { + return ss, nil + } + return 0.0, fmt.Errorf("Cannot parse size string: %s", s) } @@ -56,17 +62,17 @@ func ParseMarkerString(s string) ([]*Marker, error) { label := "" for _, ss := range strings.Split(s, "|") { - if strings.HasPrefix(ss, "color:") { + if ok, suffix := hasPrefix(ss, "color:"); ok { var err error - color, err = ParseColorString(strings.TrimPrefix(ss, "color:")) + color, err = ParseColorString(suffix) if err != nil { return nil, err } - } else if strings.HasPrefix(ss, "label:") { - label = strings.TrimPrefix(ss, "label:") - } else if strings.HasPrefix(ss, "size:") { + } else if ok, suffix := hasPrefix(ss, "label:"); ok { + label = suffix + } else if ok, suffix := hasPrefix(ss, "size:"); ok { var err error - size, err = parseSizeString(strings.TrimPrefix(ss, "size:")) + size, err = parseSizeString(suffix) if err != nil { return nil, err } @@ -95,7 +101,6 @@ func (m *Marker) bounds() s2.Rect { func (m *Marker) draw(gc *gg.Context, trans *transformer) { gc.ClearPath() - gc.SetLineJoin(gg.LineJoinRound) gc.SetLineWidth(1.0) diff --git a/path.go b/path.go index c7bab99..51d9783 100644 --- a/path.go +++ b/path.go @@ -30,15 +30,15 @@ func ParsePathString(s string) (*Path, error) { path.Weight = 5.0 for _, ss := range strings.Split(s, "|") { - if strings.HasPrefix(ss, "color:") { + if ok, suffix := hasPrefix(ss, "color:"); ok { var err error - path.Color, err = ParseColorString(strings.TrimPrefix(ss, "color:")) + path.Color, err = ParseColorString(suffix) if err != nil { return nil, err } - } else if strings.HasPrefix(ss, "weight:") { + } else if ok, suffix := hasPrefix(ss, "weight:"); ok { var err error - path.Weight, err = strconv.ParseFloat(strings.TrimPrefix(ss, "weight:"), 64) + path.Weight, err = strconv.ParseFloat(suffix, 64) if err != nil { return nil, err } @@ -72,15 +72,12 @@ func (p *Path) draw(gc *gg.Context, trans *transformer) { } gc.ClearPath() - gc.SetLineWidth(p.Weight) gc.SetLineCap(gg.LineCapRound) gc.SetLineJoin(gg.LineJoinRound) - for _, ll := range p.Positions { gc.LineTo(trans.ll2p(ll)) } - gc.SetColor(p.Color) gc.Stroke() } diff --git a/tile_fetcher.go b/tile_fetcher.go index 4f36296..e02a71f 100644 --- a/tile_fetcher.go +++ b/tile_fetcher.go @@ -13,6 +13,7 @@ import ( _ "image/png" // to be able to decode pngs "io" "io/ioutil" + "log" "net/http" "os" "path/filepath" @@ -78,9 +79,8 @@ func (t *TileFetcher) Fetch(zoom, x, y int) (image.Image, error) { if t.useCaching { fileName := t.cacheFileName(zoom, x, y) - if err = t.storeCache(fileName, data); err != nil { - fmt.Println("Failed to store image as", fileName) - fmt.Println(err) + if err := t.storeCache(fileName, data); err != nil { + log.Printf("Failed to store map tile as '%s': %s", fileName, err) } } diff --git a/util.go b/util.go new file mode 100644 index 0000000..3f5d4ad --- /dev/null +++ b/util.go @@ -0,0 +1,18 @@ +// Copyright 2016 Florian Pigorsch. All rights reserved. +// +// Use of this source code is governed by a MIT-style +// license that can be found in the LICENSE file. + +package sm + +import ( + "strings" +) + +// hasPrefix checks if 's' has prefix 'prefix'; returns 'true' and the remainder on success, and 'false', 's' otherwise. +func hasPrefix(s string, prefix string) (bool, string) { + if strings.HasPrefix(s, prefix) { + return true, strings.TrimPrefix(s, prefix) + } + return false, s +}