#!/usr/bin/env node --nouse_idle_notification --expose_gc // // Running node with --nouse_idle-notification can improve performance significantly // with large files. TODO: test that this is still true, since using fewer Arrays // The gc() calls below are intended to collect temp. objects from the previous // processing stage. Timing gc() gives an indication of overhead from memory management. // var mapshaper = require("../"), Node = mapshaper.Node, Utils = mapshaper.Utils, T = mapshaper.T; var opts = mapshaper.getOpts(); // see mapshaper-cli-lib.js mapshaper.C.VERBOSE = opts.verbose; T.start("Start timing"); var importData = mapshaper.importFromFile(opts.input_file, opts), arcData = importData.arcs, bounds = arcData.getBounds(); // Update input/output format with information found during importing // (command line info is insufficient to distinguish topojson and geojson) opts.input_format = opts.input_format || importData.info.input_format || error("Missing input format"); opts.output_format = opts.output_format || opts.input_format; if (opts.use_simplification) { mapshaper.simplifyPaths(arcData, opts.simplify_method); mapshaper.gc(); if (opts.keep_shapes) { mapshaper.protectShapes(arcData, importData.layers); } } if (opts.simplify_pct) { arcData.setRetainedPct(opts.simplify_pct); } else if (opts.simplify_interval) { arcData.setRetainedInterval(opts.simplify_interval); } if (opts.repair) { var xxInfo = mapshaper.findAndRepairIntersections(arcData); console.log("Repaired " + xxInfo.repaired + " intersections; " + xxInfo.post + " remaining"); } var exports = mapshaper.exportContent(importData.layers, arcData, opts); // Copy prj and dbf file, if both importing and exporting as shapefile if (opts.output_format == 'shapefile' && opts.input_format == 'shapefile') { var prjFile = mapshaper.cli.replaceFileExtension(opts.input_file, 'prj'), dbfFile = mapshaper.cli.replaceFileExtension(opts.input_file, 'dbf'), shpFiles = Utils.filter(exports, function(o) {return o.extension == 'shp'}); if (Node.fileExists(dbfFile) && shpFiles.length > 0) { // KLUDGE: assumes that the first layer contains the same shapes as the input exports.push({ content: Node.readFile(dbfFile), extension: 'dbf', filebase: shpFiles[0].filebase }); } if (Node.fileExists(prjFile)) { Utils.forEach(shpFiles, function(o) { exports.push({ content: Node.readFile(prjFile, 'utf-8'), extension: 'prj', filebase: o.filebase }); }); } } var paths = mapshaper.getOutputPaths(exports, opts.output_directory, opts.output_extension); Utils.forEach(exports, function(obj, i) { var path = paths[i]; Node.writeFile(path, obj.content); console.log("Wrote " + path); }); T.stop("Total time");