forked from CrunchyData/pg_tileserv
-
Notifications
You must be signed in to change notification settings - Fork 0
/
layer.go
113 lines (101 loc) · 2.41 KB
/
layer.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package main
import (
"fmt"
"net/http"
"net/url"
)
// LayerType is the table/function type of a layer
type LayerType int
const (
// LayerTypeTable is a table layer
LayerTypeTable = 1
// LayerTypeFunction is a function layer
LayerTypeFunction = 2
)
func (lt LayerType) String() string {
switch lt {
case LayerTypeTable:
return "table"
case LayerTypeFunction:
return "function"
default:
return "unknown"
}
}
// A Layer is a LayerTable or a LayerFunction
// in either case it should be able to generate
// a TileRequest containing SQL to produce tiles
// given an input tile
type Layer interface {
GetType() LayerType
GetID() string
GetDescription() string
GetName() string
GetSchema() string
GetTileRequest(tile Tile, r *http.Request) TileRequest
WriteLayerJSON(w http.ResponseWriter, req *http.Request) error
}
// A TileRequest specifies what to fetch from the database for a single tile
type TileRequest struct {
LayerID string
Tile Tile
SQL string
Args []interface{}
}
func getLayer(lyrID string) (Layer, error) {
lyr, ok := globalLayers[lyrID]
if ok {
return lyr, nil
}
return lyr, fmt.Errorf("Unable to get layer '%s'", lyrID)
}
func loadLayers() error {
_, errBnd := getServerBounds()
if errBnd != nil {
return errBnd
}
tableLayers, errTl := getTableLayers()
if errTl != nil {
return errTl
}
functionLayers, errFl := getFunctionLayers()
if errFl != nil {
return errFl
}
// Empty the global layer map
globalLayersMutex.Lock()
globalLayers = make(map[string]Layer)
for _, lyr := range tableLayers {
globalLayers[lyr.GetID()] = lyr
}
for _, lyr := range functionLayers {
globalLayers[lyr.GetID()] = lyr
}
globalLayersMutex.Unlock()
return nil
}
type layerJSON struct {
Type string `json:"type"`
ID string `json:"id"`
Name string `json:"name"`
Schema string `json:"schema"`
Description string `json:"description"`
DetailURL string `json:"detailurl"`
}
func getJSONLayers(r *http.Request) map[string]layerJSON {
json := make(map[string]layerJSON)
urlBase := serverURLBase(r)
globalLayersMutex.Lock()
for k, v := range globalLayers {
json[k] = layerJSON{
Type: v.GetType().String(),
ID: v.GetID(),
Name: v.GetName(),
Schema: v.GetSchema(),
Description: v.GetDescription(),
DetailURL: fmt.Sprintf("%s/%s.json", urlBase, url.PathEscape(v.GetID())),
}
}
globalLayersMutex.Unlock()
return json
}