Last active
February 15, 2019 14:03
-
-
Save peterhellberg/1af2c0a82524752152226bb1f1d9bc9e to your computer and use it in GitHub Desktop.
GeoPoint rendering using gfx.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} |
Author
peterhellberg
commented
Feb 15, 2019
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment