-
Notifications
You must be signed in to change notification settings - Fork 0
/
createMapfile.js
127 lines (116 loc) · 2.98 KB
/
createMapfile.js
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
const fs = require("fs");
const tinycolor = require("tinycolor2");
const defs = readColors();
const file = mapFile(defs);
console.log(file);
function mapFile(defs) {
const layers = mapFileLayers(defs);
const includeMapTag = false;
if (!includeMapTag) return layers;
return `
MAP
IMAGETYPE PNG
SIZE 800 600
SHAPEPATH "./data/LA/"
IMAGECOLOR 255 0 255
PROJECTION
"init=epsg:4326"
END
WEB
METADATA
WMS_ENABLE_REQUEST "*"
END
END
${layers}
END
`;
}
function mapFileLayers(defs) {
return Object.keys(defs)
.map(layer => {
return mapFileLayer(defs, layer);
})
.join("\n");
}
function mapFileLayer(defs, layer1) {
if (!layer1.startsWith("NN-LA-TI-")) return;
const layer = hackKodeFordiUtdaterteKartdata(layer1);
const node = defs[layer1];
const tittel = node.navn;
const parts = layer.split("-");
// if (layer1 === "NN-LA-TI-I") debugger;
if (parts.length > 1) parts.pop();
const prefix = parts.join("-");
return `
LAYER NAME "${layer}"
CONNECTIONTYPE OGR
CONNECTION "/data/${node.url}/polygon.spatialite.4326.sqlite"
DATA "${layer.toLowerCase().replace(/-/g, "_")}"
CLASSITEM "code"
TYPE POLYGON
TEMPLATE "../templates/la.html"
METADATA
"title" "${tittel}"
"gml_include_items" "all"
END
PROJECTION
"init=epsg:4326"
END
CLASSITEM "code"
${writeClasses(defs, layer1).join("\n")}
END`;
}
function writeClasses(defs, kode) {
const layer = defs[kode].barn;
layer.sort((a, b) => a.sortkey - b.sortkey);
return layer.map(def => {
if (!(def.kode in defs)) return mapFileClass(def);
});
}
function mapFileClass(def) {
const { kode, r, g, b } = def;
let hackKode = hackKodeFordiUtdaterteKartdata(kode);
return `
CLASS NAME "${hackKode}"
EXPRESSION ('[code]'='${hackKode}')
STYLE
OUTLINECOLOR ${r} ${g} ${b}
#WIDTH 2
COLOR ${r} ${g} ${b}
END
END`;
}
function hackKodeFordiUtdaterteKartdata(kode) {
return kode.replace("-TI", "").replace("NN-", "");
}
function readColors() {
const data = fs.readFileSync("metadata_med_undertyper.json");
const typer = JSON.parse(data).data;
const layers = {};
const foreldrenoder = {};
Object.keys(typer).forEach(kode => {
const foreldre = typer[kode].foreldre || [];
foreldre.forEach(forelder => {
foreldrenoder[forelder] = true;
});
});
typer.forEach(type => {
const kode = type.kode;
if (kode.startsWith("meta")) return;
if (kode.startsWith("NN-LA-KLG")) return;
if (kode in foreldrenoder) return;
const forelder = type.overordnet[0] || {};
const parts = kode.split("-");
const def = {
kode: kode,
sortkey: parts.pop(),
layer: parts.join("-"),
url: forelder.url,
...tinycolor(type.farge).toRgb()
};
if (!(def.layer in layers))
layers[def.layer] = { navn: forelder.tittel.nb, barn: [], url: def.url };
layers[def.layer].barn.push(def);
});
return layers;
}