Skip to content

Instantly share code, notes, and snippets.

@peterhellberg
Last active February 15, 2019 14:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save peterhellberg/1af2c0a82524752152226bb1f1d9bc9e to your computer and use it in GitHub Desktop.
Save peterhellberg/1af2c0a82524752152226bb1f1d9bc9e to your computer and use it in GitHub Desktop.
GeoPoint rendering using gfx.
package main
import (
"image/draw"
"io"
"strconv"
"text/scanner"
"github.com/peterhellberg/gfx"
)
const tileSize, zoom float64 = 170, 5
var move = gfx.V(-2600, 1450)
func main() {
dst := gfx.NewImage(768, 768, gfx.BlockColorBlack.Dark)
drawCountry(dst, "sweden", gfx.GP(59.3293, 18.0686), gfx.BlockColorBlue)
drawCountry(dst, "finland", gfx.GP(60.1699, 24.9384), gfx.BlockColorWhite)
drawCountry(dst, "denmark", gfx.GP(55.6761, 12.5683), gfx.BlockColorRed)
drawCountry(dst, "germany", gfx.GP(52.5200, 13.4050), gfx.BlockColorOrange)
gfx.SavePNG("gfx-geo-countries.png", dst)
}
func drawCountry(dst draw.Image, country string, city gfx.GeoPoint, bc gfx.BlockColor) {
downloadPoly(country).Fill(dst, bc.Medium)
gfx.DrawCircleFilled(dst, city.Vec(tileSize, zoom).Add(move), 6, bc.Dark)
}
func downloadPoly(country string) gfx.Polygon {
rawurl := gfx.Sprintf("https://download.geofabrik.de/europe/%s.poly", country)
resp, err := gfx.Get(rawurl)
if err != nil || resp.StatusCode != 200 {
return gfx.Polygon{}
}
defer resp.Body.Close()
return scanPoly(resp.Body).Project(gfx.IM.Moved(move))
}
func scanPoly(r io.Reader) gfx.Polygon {
var s scanner.Scanner
s.Init(r)
var floats []float64
for tok := s.Scan(); tok != scanner.EOF; tok = s.Scan() {
switch tok {
case scanner.Float:
if f, err := strconv.ParseFloat(s.TokenText(), 64); err == nil {
floats = append(floats, f)
}
}
}
var p gfx.Polygon
for i := len(floats) - 2; i >= 0; i -= 2 {
p = append(p, gfx.GP(floats[i+1], floats[i]).Vec(tileSize, zoom))
}
return p
}
@peterhellberg
Copy link
Author

gfx-geo

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment