From c15eda00ac2a7af3259c5ac6ae4d626697e8f086 Mon Sep 17 00:00:00 2001 From: Patrick Browne Date: Fri, 17 Feb 2023 13:12:52 +0100 Subject: [PATCH] refactor: Extract generate --- website/src/pages/api/_generate.ts | 53 +++++++++++++++++++++++++++ website/src/pages/api/generate.ts | 59 +++--------------------------- website/src/pages/api/v0.ts | 50 +------------------------ 3 files changed, 60 insertions(+), 102 deletions(-) create mode 100644 website/src/pages/api/_generate.ts diff --git a/website/src/pages/api/_generate.ts b/website/src/pages/api/_generate.ts new file mode 100644 index 00000000..9a9edda0 --- /dev/null +++ b/website/src/pages/api/_generate.ts @@ -0,0 +1,53 @@ +import { promises as fs } from "fs"; +import * as mapshaper from "mapshaper"; +import * as path from "path"; + +export const generate = async ({ + format, + shpFilenames, + simplify, + mapshaperCommands, +}: { + format: "topojson" | "svg"; + shpFilenames: string[]; + simplify: string; + mapshaperCommands?: string[]; +}) => { + const input = await (async () => { + const props = shpFilenames.flatMap((shpFilename) => { + const shape = path.basename(shpFilename, ".shp"); + const fullPathWithoutExt = shpFilename.substring( + 0, + shpFilename.length - ".shp".length + ); + return ["shp", "dbf", "prj"].map(async (ext) => { + return [ + `${shape}.${ext}`, + await fs.readFile(`${fullPathWithoutExt}.${ext}`), + ] as const; + }); + }); + return Object.fromEntries(await Promise.all(props)); + })(); + + const inputFiles = shpFilenames + .map((shpFilename) => `${path.basename(shpFilename)}`) + .join(" "); + + const commands = [ + `-i ${inputFiles} combine-files string-fields=*`, + simplify ? `-simplify ${simplify} keep-shapes` : "", + "-clean", + `-proj ${format === "topojson" ? "wgs84" : "somerc"}`, + ...(mapshaperCommands || []), + ].join("\n"); + + console.log("### Mapshaper commands ###"); + console.log(commands); + + const output = await mapshaper.applyCommands(commands, input); + + return format === "topojson" + ? output["output.topojson"] + : output["output.svg"]; +}; diff --git a/website/src/pages/api/generate.ts b/website/src/pages/api/generate.ts index 266d64d4..f99d5ec0 100644 --- a/website/src/pages/api/generate.ts +++ b/website/src/pages/api/generate.ts @@ -1,10 +1,8 @@ import Cors from "cors"; -import { promises as fs } from "fs"; import { enableMapSet } from "immer"; -import * as t from "io-ts"; -import * as mapshaper from "mapshaper"; import { NextApiRequest, NextApiResponse } from "next"; import * as path from "path"; +import { generate } from "./_generate"; import { formatContentTypes, formatExtensions, @@ -20,56 +18,6 @@ const cors = initMiddleware( }) ); -const generateFromShapefiles = async ({ - format, - shpFilenames, - simplify, -}: { - format: "topojson" | "svg"; - year: string; - shpFilenames: string[]; - simplify?: string; -}) => { - const input = await (async () => { - const props = shpFilenames.flatMap((shpFilename) => { - const shape = path.basename(shpFilename, ".shp"); - const fullPathWithoutExt = shpFilename.substring( - 0, - shpFilename.length - ".shp".length - ); - return ["shp", "dbf", "prj"].map(async (ext) => { - return [ - `${shape}.${ext}`, - await fs.readFile(`${fullPathWithoutExt}.${ext}`), - ] as const; - }); - }); - return Object.fromEntries(await Promise.all(props)); - })(); - - const inputFiles = shpFilenames - .map((shpFilename) => `${path.basename(shpFilename)}`) - .join(" "); - - const commands = [ - `-i ${inputFiles} combine-files string-fields=*`, - // `-rename-layers ${shp.join(",")}`, - simplify ? `-simplify ${simplify} keep-shapes` : "", - "-clean", - `-proj ${format === "topojson" ? "wgs84" : "somerc"}`, - `-o output.${format} format=${format} drop-table id-field=id`, - ].join("\n"); - - console.log("### Mapshaper commands ###"); - console.log(commands); - - const output = await mapshaper.applyCommands(commands, input); - - return format === "topojson" - ? output["output.topojson"] - : output["output.svg"]; -}; - export default async function handler( req: NextApiRequest, res: NextApiResponse @@ -83,11 +31,14 @@ export default async function handler( const { format, year } = options; const cwd = process.cwd(); - const output = await generateFromShapefiles({ + const output = await generate({ ...options, shpFilenames: [...options.shapes].map((shapeName) => { return path.join(cwd, "public", "swiss-maps", year, `${shapeName}.shp`); }), + mapshaperCommands: [ + `-o output.${format} format=${format} drop-table id-field=id target=*`, + ], }); if (query.download !== undefined) { diff --git a/website/src/pages/api/v0.ts b/website/src/pages/api/v0.ts index e70c19cd..9da461c6 100644 --- a/website/src/pages/api/v0.ts +++ b/website/src/pages/api/v0.ts @@ -1,8 +1,5 @@ import Cors from "cors"; -import { either } from "fp-ts"; -import { promises as fs } from "fs"; import { enableMapSet } from "immer"; -import * as mapshaper from "mapshaper"; import { NextApiRequest, NextApiResponse } from "next"; import * as path from "path"; import { @@ -11,6 +8,7 @@ import { initMiddleware, parseOptions, } from "./_utils"; +import { generate } from "./_generate"; /** * Difference from `generate` api @@ -25,54 +23,10 @@ const cors = initMiddleware( }) ); -const generate = async ({ - format, - shpFilenames, - simplify, - mapshaperCommands, -}: { - format: "topojson" | "svg"; - shpFilenames: string[]; - simplify: string; - mapshaperCommands?: string[]; -}) => { - const input = await (async () => { - const props = shpFilenames.flatMap((shpFilename) => { - const shape = path.basename(shpFilename, ".shp"); - const fullPathWithoutExt = shpFilename.substring( - 0, - shpFilename.length - ".shp".length - ); - return ["shp", "dbf", "prj"].map(async (ext) => { - return [ - `${shape}.${ext}`, - await fs.readFile(`${fullPathWithoutExt}.${ext}`), - ] as const; - }); - }); - return Object.fromEntries(await Promise.all(props)); - })(); - - const inputFiles = shpFilenames - .map((shpFilename) => `${path.basename(shpFilename)}`) - .join(" "); - - const commands = [ - `-i ${inputFiles} combine-files string-fields=*`, - simplify ? `-simplify ${simplify} keep-shapes` : "", - "-clean", - `-proj ${format === "topojson" ? "wgs84" : "somerc"}`, - ...(mapshaperCommands || []), - ].join("\n"); - console.log("### Mapshaper commands ###"); - console.log(commands); +const makeMapshaperStyleCommands = ( - const output = await mapshaper.applyCommands(commands, input); - return format === "topojson" - ? output["output.topojson"] - : output["output.svg"]; }; export default async function handler(