Skip to content

Commit c15eda0

Browse files
committed
refactor: Extract generate
1 parent 11ad9ac commit c15eda0

File tree

3 files changed

+60
-102
lines changed

3 files changed

+60
-102
lines changed

website/src/pages/api/_generate.ts

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import { promises as fs } from "fs";
2+
import * as mapshaper from "mapshaper";
3+
import * as path from "path";
4+
5+
export const generate = async ({
6+
format,
7+
shpFilenames,
8+
simplify,
9+
mapshaperCommands,
10+
}: {
11+
format: "topojson" | "svg";
12+
shpFilenames: string[];
13+
simplify: string;
14+
mapshaperCommands?: string[];
15+
}) => {
16+
const input = await (async () => {
17+
const props = shpFilenames.flatMap((shpFilename) => {
18+
const shape = path.basename(shpFilename, ".shp");
19+
const fullPathWithoutExt = shpFilename.substring(
20+
0,
21+
shpFilename.length - ".shp".length
22+
);
23+
return ["shp", "dbf", "prj"].map(async (ext) => {
24+
return [
25+
`${shape}.${ext}`,
26+
await fs.readFile(`${fullPathWithoutExt}.${ext}`),
27+
] as const;
28+
});
29+
});
30+
return Object.fromEntries(await Promise.all(props));
31+
})();
32+
33+
const inputFiles = shpFilenames
34+
.map((shpFilename) => `${path.basename(shpFilename)}`)
35+
.join(" ");
36+
37+
const commands = [
38+
`-i ${inputFiles} combine-files string-fields=*`,
39+
simplify ? `-simplify ${simplify} keep-shapes` : "",
40+
"-clean",
41+
`-proj ${format === "topojson" ? "wgs84" : "somerc"}`,
42+
...(mapshaperCommands || []),
43+
].join("\n");
44+
45+
console.log("### Mapshaper commands ###");
46+
console.log(commands);
47+
48+
const output = await mapshaper.applyCommands(commands, input);
49+
50+
return format === "topojson"
51+
? output["output.topojson"]
52+
: output["output.svg"];
53+
};

website/src/pages/api/generate.ts

Lines changed: 5 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
import Cors from "cors";
2-
import { promises as fs } from "fs";
32
import { enableMapSet } from "immer";
4-
import * as t from "io-ts";
5-
import * as mapshaper from "mapshaper";
63
import { NextApiRequest, NextApiResponse } from "next";
74
import * as path from "path";
5+
import { generate } from "./_generate";
86
import {
97
formatContentTypes,
108
formatExtensions,
@@ -20,56 +18,6 @@ const cors = initMiddleware(
2018
})
2119
);
2220

23-
const generateFromShapefiles = async ({
24-
format,
25-
shpFilenames,
26-
simplify,
27-
}: {
28-
format: "topojson" | "svg";
29-
year: string;
30-
shpFilenames: string[];
31-
simplify?: string;
32-
}) => {
33-
const input = await (async () => {
34-
const props = shpFilenames.flatMap((shpFilename) => {
35-
const shape = path.basename(shpFilename, ".shp");
36-
const fullPathWithoutExt = shpFilename.substring(
37-
0,
38-
shpFilename.length - ".shp".length
39-
);
40-
return ["shp", "dbf", "prj"].map(async (ext) => {
41-
return [
42-
`${shape}.${ext}`,
43-
await fs.readFile(`${fullPathWithoutExt}.${ext}`),
44-
] as const;
45-
});
46-
});
47-
return Object.fromEntries(await Promise.all(props));
48-
})();
49-
50-
const inputFiles = shpFilenames
51-
.map((shpFilename) => `${path.basename(shpFilename)}`)
52-
.join(" ");
53-
54-
const commands = [
55-
`-i ${inputFiles} combine-files string-fields=*`,
56-
// `-rename-layers ${shp.join(",")}`,
57-
simplify ? `-simplify ${simplify} keep-shapes` : "",
58-
"-clean",
59-
`-proj ${format === "topojson" ? "wgs84" : "somerc"}`,
60-
`-o output.${format} format=${format} drop-table id-field=id`,
61-
].join("\n");
62-
63-
console.log("### Mapshaper commands ###");
64-
console.log(commands);
65-
66-
const output = await mapshaper.applyCommands(commands, input);
67-
68-
return format === "topojson"
69-
? output["output.topojson"]
70-
: output["output.svg"];
71-
};
72-
7321
export default async function handler(
7422
req: NextApiRequest,
7523
res: NextApiResponse
@@ -83,11 +31,14 @@ export default async function handler(
8331
const { format, year } = options;
8432
const cwd = process.cwd();
8533

86-
const output = await generateFromShapefiles({
34+
const output = await generate({
8735
...options,
8836
shpFilenames: [...options.shapes].map((shapeName) => {
8937
return path.join(cwd, "public", "swiss-maps", year, `${shapeName}.shp`);
9038
}),
39+
mapshaperCommands: [
40+
`-o output.${format} format=${format} drop-table id-field=id target=*`,
41+
],
9142
});
9243

9344
if (query.download !== undefined) {

website/src/pages/api/v0.ts

Lines changed: 2 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
import Cors from "cors";
2-
import { either } from "fp-ts";
3-
import { promises as fs } from "fs";
42
import { enableMapSet } from "immer";
5-
import * as mapshaper from "mapshaper";
63
import { NextApiRequest, NextApiResponse } from "next";
74
import * as path from "path";
85
import {
@@ -11,6 +8,7 @@ import {
118
initMiddleware,
129
parseOptions,
1310
} from "./_utils";
11+
import { generate } from "./_generate";
1412

1513
/**
1614
* Difference from `generate` api
@@ -25,54 +23,10 @@ const cors = initMiddleware(
2523
})
2624
);
2725

28-
const generate = async ({
29-
format,
30-
shpFilenames,
31-
simplify,
32-
mapshaperCommands,
33-
}: {
34-
format: "topojson" | "svg";
35-
shpFilenames: string[];
36-
simplify: string;
37-
mapshaperCommands?: string[];
38-
}) => {
39-
const input = await (async () => {
40-
const props = shpFilenames.flatMap((shpFilename) => {
41-
const shape = path.basename(shpFilename, ".shp");
42-
const fullPathWithoutExt = shpFilename.substring(
43-
0,
44-
shpFilename.length - ".shp".length
45-
);
46-
return ["shp", "dbf", "prj"].map(async (ext) => {
47-
return [
48-
`${shape}.${ext}`,
49-
await fs.readFile(`${fullPathWithoutExt}.${ext}`),
50-
] as const;
51-
});
52-
});
53-
return Object.fromEntries(await Promise.all(props));
54-
})();
55-
56-
const inputFiles = shpFilenames
57-
.map((shpFilename) => `${path.basename(shpFilename)}`)
58-
.join(" ");
59-
60-
const commands = [
61-
`-i ${inputFiles} combine-files string-fields=*`,
62-
simplify ? `-simplify ${simplify} keep-shapes` : "",
63-
"-clean",
64-
`-proj ${format === "topojson" ? "wgs84" : "somerc"}`,
65-
...(mapshaperCommands || []),
66-
].join("\n");
6726

68-
console.log("### Mapshaper commands ###");
69-
console.log(commands);
27+
const makeMapshaperStyleCommands = (
7028

71-
const output = await mapshaper.applyCommands(commands, input);
7229

73-
return format === "topojson"
74-
? output["output.topojson"]
75-
: output["output.svg"];
7630
};
7731

7832
export default async function handler(

0 commit comments

Comments
 (0)