where

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README | LICENSE

commit 7e59e56cb5133686b96e6b3ffaa86ae72cd95521
parent 024bf6c5e1dec1c12ac56f328d50e49bde5fa742
Author: aabacchus <bvnfuller@gmail.com>
Date:   Sat, 20 Mar 2021 14:27:28 +0000

make static map of markers

Diffstat:
M.gitignore | 1+
MTODO | 2+-
Amapbox.go | 68++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Mwhere.go | 16+++++++++++++++-
4 files changed, 85 insertions(+), 2 deletions(-)

diff --git a/.gitignore b/.gitignore @@ -2,3 +2,4 @@ creds.json ips.json where whoips +map.png diff --git a/TODO b/TODO @@ -1,4 +1,4 @@ * Only get locations of opted-in users * Anonymize users who wish -* Create webpage (static img & interactive map?) +* Create webpage (interactive map?) * Read input from json file and append new user information diff --git a/mapbox.go b/mapbox.go @@ -0,0 +1,68 @@ +/* Copyright 2021 Ben Fuller + * Apache License, Version 2.0 + * See LICENSE file for copyright and licence details. + */ + +package main + +import ( + "fmt" + "io/ioutil" + "net/http" + "os" +) + +// MapboxDetails holds the basic credentials to generate mapbox content. +type MapboxDetails struct { + Uname string + Style string + Apikey string + Padding string +} + +// MapboxStatic creates a static image of a map +// with markers represented by m +func MapboxStatic(m []Marker, fname string, mbox MapboxDetails) error { + baseURL := "https://api.mapbox.com/" + query := fmt.Sprintf("styles/v1/%s/%s/static/", mbox.Uname, mbox.Style) + // it is possible to define a bbox (which defines the field of view) + // in the format [minlng,minlat,maxlng,maxlat] + // rather than "auto" which fits the field of view to the markers on the map + // the field after auto is the dimensions of the png image + // the parameters at the end (after the ?) are the access_token (required) + // and padding (optional) + suffix := fmt.Sprintf("/auto/800x720?padding=%s&access_token=%s", mbox.Padding, mbox.Apikey) + + var markersMapbox string + for _, mark := range m { + markersMapbox += MarkerToMapbox(mark, "", "aa0500") + "," + } + // remove the final comma + markersMapbox = markersMapbox[:len(markersMapbox)-1] + + // make the request + imgP, err := http.Get(baseURL + query + markersMapbox + suffix) + if err != nil { + return err + } + defer imgP.Body.Close() + bytes, _ := ioutil.ReadAll(imgP.Body) + // save the image + f, err := os.Create(fname) + f.Write(bytes) + return err +} + +// MarkerToMapbox takes a Marker which has a position, +// and optionally a label (a-z, 0-99, or a Makl icon) and color +// (if you don't want these, provide empty strings) +// and returns the correctly formatted marker for Mapbox +func MarkerToMapbox(m Marker, label string, color string) string { + if label != "" { + label = "-" + label + } + if color != "" { + color = "+" + color + } + return fmt.Sprintf("pin-s%s%s(%f,%f)", label, color, m.Lng, m.Lat) +} diff --git a/where.go b/where.go @@ -26,13 +26,18 @@ import ( ) func usage() { - fmt.Fprintf(os.Stderr, "usage: %s\t[-h] [-k]\n", os.Args[0]) + fmt.Fprintf(os.Stderr, "usage: %s\t[-h] [-k]\n\t\t[-mboxu -mboxa -mboxs] [-mboxp]\n", os.Args[0]) flag.PrintDefaults() fmt.Fprintf(os.Stderr, "\nwhere finds users who have opted in by creating a \".here\" file in their home directory,\nfinds their approximate location from their IP address, and creates a map of the locations of those users.\n") } func main() { apiKey := flag.String("k", "", "API key for ipstack") + var mboxDetails MapboxDetails + flag.StringVar(&mboxDetails.Uname, "mboxu", "", "mapbox.com username") + flag.StringVar(&mboxDetails.Apikey, "mboxa", "", "mapbox.com API key") + flag.StringVar(&mboxDetails.Style, "mboxs", "", "mapbox map style") + flag.StringVar(&mboxDetails.Padding, "mboxp", "5", "mapbox map padding (a percentage without the %)") flag.Usage = usage flag.Parse() @@ -67,6 +72,15 @@ func main() { fmt.Printf("error saving as json: %s\n", err) os.Exit(1) } + + // make a map of the markers and save it as a png + imageFile := "map.png" + err = MapboxStatic(results, imageFile, mboxDetails) + if err != nil { + fmt.Printf("error creating static map: %s\n", err) + os.Exit(1) + } + fmt.Printf("saved static map to %s\n", imageFile) } func MarkersSaveJson(m []Marker, fname string) error {