11import Cors from "cors" ;
2- import { promises as fs } from "fs" ;
32import { enableMapSet } from "immer" ;
4- import * as t from "io-ts" ;
5- import * as mapshaper from "mapshaper" ;
63import { NextApiRequest , NextApiResponse } from "next" ;
74import * as path from "path" ;
5+ import { generate } from "./_generate" ;
86import {
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-
7321export 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 ) {
0 commit comments