2022-01-07 04:14:53 +01:00
|
|
|
import * as fs from "fs"
|
|
|
|
|
2022-01-26 21:40:38 +01:00
|
|
|
function main(args) {
|
|
|
|
if (args.length < 2) {
|
2022-04-19 23:43:51 +02:00
|
|
|
console.log(
|
|
|
|
"Given a single geojson file and an attribute-key, will generate a new file for every value of the partition."
|
|
|
|
)
|
2022-01-07 04:14:53 +01:00
|
|
|
console.log("USAGE: perProperty `file.geojson` `property-key`")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
const path = args[0]
|
|
|
|
const key = args[1]
|
2022-01-26 21:40:38 +01:00
|
|
|
|
|
|
|
const data = JSON.parse(fs.readFileSync(path, "UTF8"))
|
2022-01-07 04:14:53 +01:00
|
|
|
const perProperty = new Map<string, any[]>()
|
|
|
|
|
2022-01-26 21:40:38 +01:00
|
|
|
console.log("Partitioning", data.features.length, "features")
|
2022-01-07 04:14:53 +01:00
|
|
|
for (const feature of data.features) {
|
|
|
|
const v = feature.properties[key]
|
2022-01-26 21:40:38 +01:00
|
|
|
if (!perProperty.has(v)) {
|
2022-01-07 04:14:53 +01:00
|
|
|
console.log("Found a new category:", v)
|
|
|
|
perProperty.set(v, [])
|
|
|
|
}
|
|
|
|
perProperty.get(v).push(feature)
|
|
|
|
}
|
2022-01-26 21:40:38 +01:00
|
|
|
|
|
|
|
const stripped = path.substr(0, path.length - ".geojson".length)
|
2022-01-07 04:14:53 +01:00
|
|
|
perProperty.forEach((features, v) => {
|
2022-01-26 21:40:38 +01:00
|
|
|
fs.writeFileSync(
|
|
|
|
stripped + "." + v.replace(/[^a-zA-Z0-9_]/g, "_") + ".geojson",
|
2022-01-07 04:14:53 +01:00
|
|
|
JSON.stringify({
|
2022-01-26 21:40:38 +01:00
|
|
|
type: "FeatureCollection",
|
2022-01-07 04:14:53 +01:00
|
|
|
features,
|
|
|
|
})
|
2022-09-08 21:40:48 +02:00
|
|
|
)
|
2022-01-07 04:14:53 +01:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-04-19 23:43:51 +02:00
|
|
|
main(process.argv.slice(2))
|