-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsat.go
69 lines (59 loc) · 1.73 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
60
61
62
63
64
65
66
67
68
69
package main
import (
//"../common"
"github.com/mitroadmaps/gomapinfer/common"
"bytes"
"fmt"
"image"
"image/png"
"io/ioutil"
"net/http"
"time"
)
const URL = "https://maps.googleapis.com/maps/api/staticmap?center=%f,%f&zoom=%d&size=576x576&maptype=satellite&key=%s"
/*
To use Bing Static Map Maker instead of Google services,
Comment off the above URL (line 17) and add the following line to the code.
const URL = "https://dev.virtualearth.net/REST/V1/Imagery/Map/Aerial/%f,%f/%d?mapSize=576,576&format=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)
//fmt.Println(url)
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 ssssssssstatus 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
}