-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathdemo.go
76 lines (66 loc) · 2.19 KB
/
demo.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
70
71
72
73
74
75
76
package main
// This file contains various demo applications of the go-mapnik package
import (
"fmt"
"io/ioutil"
"net/http"
"os"
"github.com/fawick/go-mapnik/mapnik"
"github.com/fawick/go-mapnik/maptiles"
)
// Render a simple map of europe to a PNG file
func SimpleExample() {
m := mapnik.NewMap(1600, 1200)
defer m.Free()
m.Load("sampledata/stylesheet.xml")
fmt.Println(m.SRS())
// Perform a projection that is only neccessary because stylesheet.xml
// is using EPSG:3857 rather than WGS84
p := m.Projection()
ll := p.Forward(mapnik.Coord{0, 35}) // 0 degrees longitude, 35 degrees north
ur := p.Forward(mapnik.Coord{16, 70}) // 16 degrees east, 70 degrees north
m.ZoomToMinMax(ll.X, ll.Y, ur.X, ur.Y)
blob, err := m.RenderToMemoryPng()
if err != nil {
fmt.Println(err)
return
}
ioutil.WriteFile("mapnik.png", blob, 0644)
}
// This function resembles the OSM python script 'generate_tiles.py'
// The original script is found here:
// http://svn.openstreetmap.org/applications/rendering/mapnik/generate_tiles.py
func GenerateOSMTiles() {
g := maptiles.Generator{}
// Modify this number according to your machine!
g.Threads = 4
home := os.Getenv("HOME")
g.MapFile = os.Getenv("MAPNIK_MAP_FILE")
if g.MapFile == "" {
g.MapFile = home + "/svn.openstreetmap.org/applications/rendering/mapnik/osm-local.xml"
}
g.TileDir = os.Getenv("MAPNIK_TILE_DIR")
if g.TileDir == "" {
g.TileDir = home + "/osm/tiles"
}
g.Run(mapnik.Coord{-180, -90}, mapnik.Coord{180, 90}, 0, 6, "World")
g.Run(mapnik.Coord{0, 35.0}, mapnik.Coord{16, 70}, 1, 11, "Europe")
}
// Serve a single stylesheet via HTTP. Open view_tileserver.html in your browser
// to see the results.
// The created tiles are cached in an sqlite database (MBTiles 1.2 conform) so
// successive access a tile is much faster.
func TileserverWithCaching() {
cache := "gomapnikcache.sqlite"
os.Remove(cache)
t := maptiles.NewTileServer(cache)
t.AddMapnikLayer("default", "sampledata/stylesheet.xml")
http.ListenAndServe(":8080", t)
}
// Before uncommenting the GenerateOSMTiles call make sure you have
// the neccessary OSM sources. Consult OSM wiki for details.
func main() {
SimpleExample()
//GenerateOSMTiles()
TileserverWithCaching()
}