-
Notifications
You must be signed in to change notification settings - Fork 0
/
libmagnacarto.go
151 lines (132 loc) · 3.67 KB
/
libmagnacarto.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
package main
/*
typedef struct {
char* builderType;
char* baseDir;
char* sqliteDir;
char* fontDir;
char* shapeDir;
char* imageDir;
_Bool relPaths;
_Bool msNoMapBlock;
} Opts;
*/
import (
"C"
)
import (
"bytes"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"
"github.com/omniscale/magnacarto/builder"
"github.com/omniscale/magnacarto/builder/mapnik"
"github.com/omniscale/magnacarto/builder/mapserver"
"github.com/omniscale/magnacarto/config"
"github.com/omniscale/magnacarto/mml"
)
//export buildFromString
func buildFromString(mmlStr *C.char, options C.Opts) (output, error *C.char) {
mml := C.GoString(mmlStr)
if mml == "" {
return nil, C.CString("Please specify a MML string.")
}
baseDir := C.GoString(options.baseDir)
if baseDir == "" {
return nil, C.CString("Please specify a basedir path.")
}
return build(mml, baseDir, options)
}
//export buildFromFile
func buildFromFile(file *C.char, options C.Opts) (output, error *C.char) {
filePath := C.GoString(file)
if filePath == "" {
return nil, C.CString("Please specify a file path.")
}
mml, err := ioutil.ReadFile(filePath)
if err != nil {
return nil, C.CString(fmt.Sprint("Error when reading from file ", filePath, ": ", err))
}
baseDir := C.GoString(options.baseDir)
if baseDir == "" {
baseDir = filepath.Dir(filePath)
}
return build(string(mml), baseDir, options)
}
func build(mmlStr string, baseDir string, options C.Opts) (output, error *C.char) {
conf := config.Magnacarto{}
locator := conf.Locator()
builderType := C.GoString(options.builderType)
if builderType == "" {
builderType = "mapnik2"
}
sqliteDir := C.GoString(options.sqliteDir)
if sqliteDir != "" {
conf.Datasources.SQLiteDirs = []string{sqliteDir}
}
fontDir := C.GoString(options.fontDir)
if fontDir != "" {
conf.Mapnik.FontDirs = []string{fontDir}
}
shapeDir := C.GoString(options.shapeDir)
if shapeDir != "" {
conf.Datasources.ShapefileDirs = []string{shapeDir}
}
imageDir := C.GoString(options.imageDir)
if imageDir != "" {
conf.Datasources.ImageDirs = []string{imageDir}
}
relPaths := bool(options.relPaths)
if relPaths {
locator.UseRelPaths(relPaths)
}
msNoMapBlock := bool(options.msNoMapBlock)
locator.SetBaseDir(baseDir)
var m builder.MapWriter
switch builderType {
case "mapserver":
mm := mapserver.New(locator)
mm.SetNoMapBlock(msNoMapBlock)
m = mm
case "mapnik2":
mm := mapnik.New(locator)
mm.SetMapnik2(true)
m = mm
case "mapnik3":
m = mapnik.New(locator)
default:
return nil, C.CString(fmt.Sprint("unknown builder ", builderType))
}
r := strings.NewReader(mmlStr)
mmlData, err := mml.Parse(r)
if err != nil {
return nil, C.CString(fmt.Sprint(err))
}
var style bytes.Buffer
for _, s := range mmlData.Stylesheets {
if strings.HasSuffix(s, ".mss") {
r, err := os.Open(filepath.Join(baseDir, s))
if err != nil {
return nil, C.CString(fmt.Sprint(err))
}
content, err := ioutil.ReadAll(r)
if err != nil {
return nil, C.CString(fmt.Sprint(err))
}
r.Close()
style.Write(content)
} else {
style.WriteString(s)
}
}
builder.BuildMapFromString(m, mmlData, style.String())
var buf bytes.Buffer
if err := m.Write(&buf); err != nil {
return nil, C.CString(fmt.Sprint(err))
}
return C.CString(buf.String()), nil
}
func main() {
}