Skip to content

Commit fc3e062

Browse files
committed
feat: Improve styling of SVG map
1 parent c15eda0 commit fc3e062

File tree

1 file changed

+76
-8
lines changed
  • website/src/pages/api

1 file changed

+76
-8
lines changed

website/src/pages/api/v0.ts

Lines changed: 76 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,48 @@ const cors = initMiddleware(
2323
})
2424
);
2525

26+
const truthy = <T>(x: T): x is Exclude<T, undefined | null> => {
27+
return Boolean(x);
28+
};
2629

2730
const makeMapshaperStyleCommands = (
31+
shapeStyles: Record<
32+
string,
33+
null | {
34+
fill?: string;
35+
stroke?: string;
36+
}
37+
>
38+
) => {
39+
return Object.entries(shapeStyles)
40+
.map(([shapeName, style]) => {
41+
if (style === null) {
42+
return style;
43+
}
44+
return `-style target='${shapeName}' ${Object.entries(style)
45+
.map(([propName, propValue]) => {
46+
return `${propName}='${propValue}'`;
47+
})
48+
.join(" ")}`;
49+
})
50+
.filter(truthy);
51+
};
2852

53+
const getShapeZIndex = (shape: string) => {
54+
if (shape.includes("country")) {
55+
return 3;
56+
} else if (shape.includes("cantons")) {
57+
return 2;
58+
} else if (shape.includes("lakes")) {
59+
return 1;
60+
}
61+
return 0;
62+
};
2963

64+
const shapeIndexComparator = (a: string, b: string) => {
65+
const za = getShapeZIndex(a);
66+
const zb = getShapeZIndex(b);
67+
return za === zb ? 0 : za < zb ? -1 : 1;
3068
};
3169

3270
export default async function handler(
@@ -45,17 +83,47 @@ export default async function handler(
4583
}
4684

4785
const cwd = process.cwd();
86+
const shpFilenames = [...options.shapes]
87+
.map((shapeName) => {
88+
return path.join(cwd, "public", "swiss-maps", year, `${shapeName}.shp`);
89+
})
90+
.sort(shapeIndexComparator);
91+
92+
const hasCantons = shapes.has("cantons");
93+
const hasMunicipalities = shapes.has("municipalities");
94+
const hasLakes = shapes.has("lakes");
95+
96+
const shapeStyles = {
97+
country: {
98+
fill: hasCantons || hasMunicipalities ? "transparent" : "#eee",
99+
stroke: "#111",
100+
},
101+
lakes: hasLakes
102+
? {
103+
fill: "#a1d0f7",
104+
}
105+
: null,
106+
cantons: hasCantons
107+
? {
108+
fill: hasMunicipalities ? "transparent" : "#eee",
109+
stroke: "#666",
110+
}
111+
: null,
112+
municipalities: hasMunicipalities
113+
? {
114+
fill: "#eee",
115+
stroke: hasCantons ? "#bbb" : "#666",
116+
}
117+
: null,
118+
};
119+
120+
const styleCommands = makeMapshaperStyleCommands(shapeStyles);
121+
48122
const output = await generate({
49123
...options,
50-
shpFilenames: [...options.shapes].map((shapeName) => {
51-
return path.join(cwd, "public", "swiss-maps", year, `${shapeName}.shp`);
52-
}),
124+
shpFilenames: shpFilenames,
53125
mapshaperCommands: [
54-
// svg coloring, otherwise is all bblack
55-
shapes.has("cantons")
56-
? `-style fill='#e6e6e6' stroke='#999' target='cantons'`
57-
: "",
58-
shapes.has("lakes") ? `-style fill='#a1d0f7' target='lakes'` : "",
126+
...styleCommands,
59127
`-o output.${format} format=${format} target=*`,
60128
],
61129
});

0 commit comments

Comments
 (0)