forked from mitroadmaps/gomapinfer
/
sat.go
59 lines (53 loc) · 1.47 KB
/
sat.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package bingmaps
import (
//"../common"
"github.com/IronSublimate/gomapinfer/common"
"bytes"
"fmt"
"image"
"image/png"
"io/ioutil"
"net/http"
"time"
)
const URL = "https://dev.virtualearth.net/REST/V1/Imagery/Map/Aerial/%f,%f/%d?mapSize=576,576&fmt=png&key=%s"
// Returns a 512x512 image centered at the point using the specified zoom level and API key
func GetSatelliteImage(point common.Point, zoom int, key string) image.Image {
url := fmt.Sprintf(URL, point.Y, point.X, zoom, key)
resp, err := http.Get(url)
if err != nil {
panic(err)
}
if resp.StatusCode != 200 || resp.Header.Get("Content-Type") != "image/png" {
var errdesc string
if resp.Header.Get("Content-Type") != "image/png" {
if bytes, err := ioutil.ReadAll(resp.Body); err == nil {
errdesc = string(bytes)
}
}
if resp.StatusCode == 500 {
fmt.Printf("warning: got 500 (errdesc=%s) on %s (retrying later)\n", errdesc, url)
time.Sleep(time.Minute)
return GetSatelliteImage(point, zoom, key)
} else {
panic(fmt.Errorf("got status code %d (errdesc=%s)", resp.StatusCode, errdesc))
}
}
imBytes, err := ioutil.ReadAll(resp.Body)
resp.Body.Close()
if err != nil {
panic(err)
}
im, err := png.Decode(bytes.NewReader(imBytes))
if err != nil {
panic(err)
}
// crop out logo
cropped := image.NewRGBA(image.Rect(0, 0, 512, 512))
for i := 0; i < 512; i++ {
for j := 0; j < 512; j++ {
cropped.Set(i, j, im.At(i+32, j+32))
}
}
return cropped
}