Skip to content

Commit

Permalink
refactor: Extract generate
Browse files Browse the repository at this point in the history
  • Loading branch information
ptbrowne committed Feb 17, 2023
1 parent 11ad9ac commit c15eda0
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 102 deletions.
53 changes: 53 additions & 0 deletions website/src/pages/api/_generate.ts
Original file line number Diff line number Diff line change
@@ -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"];
};
59 changes: 5 additions & 54 deletions website/src/pages/api/generate.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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
Expand All @@ -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) {
Expand Down
50 changes: 2 additions & 48 deletions website/src/pages/api/v0.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -11,6 +8,7 @@ import {
initMiddleware,
parseOptions,
} from "./_utils";
import { generate } from "./_generate";

/**
* Difference from `generate` api
Expand All @@ -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(
Expand Down

0 comments on commit c15eda0

Please sign in to comment.