This repository was archived by the owner on Sep 24, 2024. It is now read-only.
forked from ezwelty/opentrees-harvester
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathprocess.js
More file actions
executable file
·118 lines (112 loc) · 3.33 KB
/
process.js
File metadata and controls
executable file
·118 lines (112 loc) · 3.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#!/usr/bin/env node
const colors = require('colors')
const commandLineUsage = require('command-line-usage')
const commandLineArgs = require('command-line-args')
const { DEFAULT_OPTIONS } = require('./common')
const { loadSources } = require('../lib/load')
const { deleteFeature } = require('../lib/clean')
const { interpolateString } = require('../lib/helpers')
const OPTIONS = [
...DEFAULT_OPTIONS,
{
name: 'out', alias: 'o', type: String, defaultValue: 'data/${id}/output/output.csv',
// Escape special characters for chalk. See https://github.com/Polymer/tools/pull/612
description: "Template for output file, with source properties referred to by name (default: 'data/${id}/output/output.csv').".
replace(/[{}\\]/g, '\\$&')
},
{
name: 'centroids', type: Boolean, defaultValue: false,
description: 'Whether to reduce non-point geometries to centroids.'
},
{
name: 'keepInvalid', type: Boolean, defaultValue: false,
description: 'Whether to keep features with empty or invalid geometries.'
},
{
name: 'keepFields', type: Boolean, defaultValue: false,
description: 'Whether to keep the input feature fields alongside the result of the schema crosswalk.'
},
{
name: 'prefix', type: String, defaultValue: '',
description: 'String to append to input field names to prevent collisions with output field names. Applies only with `keepFields`.'
},
{
name: 'bounds', type: Number, multiple: true,
description: 'Bounding box in the format [xmin, ymin, xmax, ymax] (EPSG:4326). If provided, features outside the bounds are skipped.'
},
{
name: 'force', alias: 'f', type: Boolean, defaultValue: false,
description: 'Overwrite output file even if it already exists.'
}
]
const USAGE = [
{
header: 'example/process.js',
content: 'Process sources and save as new vector files.',
},
{
header: 'Options',
optionList: OPTIONS
}
]
// Parse command line arguments
let options
try {
options = commandLineArgs(OPTIONS)
if (options.help) {
console.log(commandLineUsage(USAGE))
process.exit(0)
}
} catch (error) {
console.error(`${'[ERROR]'.red} ${error.message}`)
console.log(commandLineUsage(USAGE))
process.exit(1)
}
// Load sources
const sources = loadSources(
`${__dirname}/../sources`,
{ ids: options.ids, countries: options.countries },
options.dir
)
// Process sources
const success = []
const failure = []
const skip = []
const processOptions = {
overwrite: options.force,
centroids: options.centroids,
keepInvalid: options.keepInvalid,
keepFields: options.keepFields,
prefix: options.prefix,
bounds: options.bounds,
delFunc: deleteFeature
}
sources.forEach(source => {
const file = interpolateString(options.out, source.props)
try {
const result = source.process(file, processOptions)
if (result) success.push(source.props.id)
else skip.push(source.props.id)
} catch (error) {
console.error(error.message)
failure.push(source.props.id)
}
})
if (success.length) {
console.log(
`${'[SUCCESS]'.green} Processed ${success.length} sources:`,
success.join(', ')
)
}
if (failure.length) {
console.error(
`${'[ERROR]'.red} Failed to process ${failure.length} sources:`,
failure.join(', ')
)
}
if (skip.length) {
console.log(
`${'[SKIPPED]'.dim} Skipped ${skip.length} sources:`,
skip.join(', ')
)
}