forked from MapComplete/MapComplete
Slice script now clips to tile box if flag is set
This commit is contained in:
parent
b88bb5b6d0
commit
8cf3b88172
2 changed files with 163 additions and 133 deletions
|
@ -783,4 +783,14 @@ export class GeoOperations {
|
||||||
): boolean {
|
): boolean {
|
||||||
return booleanWithin(feature, possiblyEncloingFeature)
|
return booleanWithin(feature, possiblyEncloingFeature)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create a union between two features
|
||||||
|
*/
|
||||||
|
static union = turf.union
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create an intersection between two features
|
||||||
|
*/
|
||||||
|
static intersect = turf.intersect
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,13 +4,21 @@ import StaticFeatureSource from "../Logic/FeatureSource/Sources/StaticFeatureSou
|
||||||
import * as readline from "readline"
|
import * as readline from "readline"
|
||||||
import ScriptUtils from "./ScriptUtils"
|
import ScriptUtils from "./ScriptUtils"
|
||||||
import { Utils } from "../Utils"
|
import { Utils } from "../Utils"
|
||||||
|
import Script from "./Script"
|
||||||
|
import { BBox } from "../Logic/BBox"
|
||||||
|
import { GeoOperations } from "../Logic/GeoOperations"
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This script slices a big newline-delimeted geojson file into tiled geojson
|
* This script slices a big newline-delimeted geojson file into tiled geojson
|
||||||
* It was used to convert the CRAB-data into geojson tiles
|
* It was used to convert the CRAB-data into geojson tiles
|
||||||
*/
|
*/
|
||||||
|
|
||||||
async function readFeaturesFromLineDelimitedJsonFile(inputFile: string): Promise<any[]> {
|
class Slice extends Script {
|
||||||
|
constructor() {
|
||||||
|
super("Break data into tiles")
|
||||||
|
}
|
||||||
|
|
||||||
|
async readFeaturesFromLineDelimitedJsonFile(inputFile: string): Promise<any[]> {
|
||||||
const fileStream = fs.createReadStream(inputFile)
|
const fileStream = fs.createReadStream(inputFile)
|
||||||
|
|
||||||
const rl = readline.createInterface({
|
const rl = readline.createInterface({
|
||||||
|
@ -34,9 +42,9 @@ async function readFeaturesFromLineDelimitedJsonFile(inputFile: string): Promise
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return allFeatures
|
return allFeatures
|
||||||
}
|
}
|
||||||
|
|
||||||
async function readGeojsonLineByLine(inputFile: string): Promise<any[]> {
|
async readGeojsonLineByLine(inputFile: string): Promise<any[]> {
|
||||||
const fileStream = fs.createReadStream(inputFile)
|
const fileStream = fs.createReadStream(inputFile)
|
||||||
|
|
||||||
const rl = readline.createInterface({
|
const rl = readline.createInterface({
|
||||||
|
@ -72,27 +80,30 @@ async function readGeojsonLineByLine(inputFile: string): Promise<any[]> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return allFeatures
|
return allFeatures
|
||||||
}
|
}
|
||||||
|
|
||||||
async function readFeaturesFromGeoJson(inputFile: string): Promise<any[]> {
|
async readFeaturesFromGeoJson(inputFile: string): Promise<any[]> {
|
||||||
try {
|
try {
|
||||||
return JSON.parse(fs.readFileSync(inputFile, { encoding: "utf-8" })).features
|
return JSON.parse(fs.readFileSync(inputFile, { encoding: "utf-8" })).features
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
// We retry, but with a line-by-line approach
|
// We retry, but with a line-by-line approach
|
||||||
return await readGeojsonLineByLine(inputFile)
|
return await this.readGeojsonLineByLine(inputFile)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
async function main(args: string[]) {
|
async main(args: string[]) {
|
||||||
console.log("GeoJSON slicer")
|
console.log("GeoJSON slicer")
|
||||||
if (args.length < 3) {
|
if (args.length < 3) {
|
||||||
console.log("USAGE: <input-file.geojson> <target-zoom-level> <output-directory>")
|
console.log(
|
||||||
|
"USAGE: <input-file.geojson> <target-zoom-level> <output-directory> [--clip]"
|
||||||
|
)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
const inputFile = args[0]
|
const inputFile = args[0]
|
||||||
const zoomlevel = Number(args[1])
|
const zoomlevel = Number(args[1])
|
||||||
const outputDirectory = args[2]
|
const outputDirectory = args[2]
|
||||||
|
const doSlice = args[3] === "--clip"
|
||||||
|
|
||||||
if (!fs.existsSync(outputDirectory)) {
|
if (!fs.existsSync(outputDirectory)) {
|
||||||
fs.mkdirSync(outputDirectory)
|
fs.mkdirSync(outputDirectory)
|
||||||
|
@ -103,10 +114,10 @@ async function main(args: string[]) {
|
||||||
let allFeatures: any[]
|
let allFeatures: any[]
|
||||||
if (inputFile.endsWith(".geojson")) {
|
if (inputFile.endsWith(".geojson")) {
|
||||||
console.log("Detected geojson")
|
console.log("Detected geojson")
|
||||||
allFeatures = await readFeaturesFromGeoJson(inputFile)
|
allFeatures = await this.readFeaturesFromGeoJson(inputFile)
|
||||||
} else {
|
} else {
|
||||||
console.log("Loading as newline-delimited features")
|
console.log("Loading as newline-delimited features")
|
||||||
allFeatures = await readFeaturesFromLineDelimitedJsonFile(inputFile)
|
allFeatures = await this.readFeaturesFromLineDelimitedJsonFile(inputFile)
|
||||||
}
|
}
|
||||||
allFeatures = Utils.NoNull(allFeatures)
|
allFeatures = Utils.NoNull(allFeatures)
|
||||||
|
|
||||||
|
@ -129,7 +140,19 @@ async function main(args: string[]) {
|
||||||
maxFeatureCount: Number.MAX_VALUE,
|
maxFeatureCount: Number.MAX_VALUE,
|
||||||
registerTile: (tile) => {
|
registerTile: (tile) => {
|
||||||
const path = `${outputDirectory}/tile_${tile.z}_${tile.x}_${tile.y}.geojson`
|
const path = `${outputDirectory}/tile_${tile.z}_${tile.x}_${tile.y}.geojson`
|
||||||
const features = tile.features.data.map((ff) => ff.feature)
|
const box = BBox.fromTile(tile.z, tile.x, tile.y)
|
||||||
|
let features = tile.features.data.map((ff) => ff.feature)
|
||||||
|
if (doSlice) {
|
||||||
|
features = Utils.NoNull(
|
||||||
|
features.map((f) => {
|
||||||
|
const intersection = GeoOperations.intersect(f, box.asGeoJson({}))
|
||||||
|
if (intersection) {
|
||||||
|
intersection.properties = f.properties
|
||||||
|
}
|
||||||
|
return intersection
|
||||||
|
})
|
||||||
|
)
|
||||||
|
}
|
||||||
features.forEach((f) => {
|
features.forEach((f) => {
|
||||||
delete f.bbox
|
delete f.bbox
|
||||||
})
|
})
|
||||||
|
@ -153,10 +176,7 @@ async function main(args: string[]) {
|
||||||
)
|
)
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
let args = [...process.argv]
|
new Slice().run()
|
||||||
args.splice(0, 2)
|
|
||||||
main(args).then((_) => {
|
|
||||||
console.log("All done!")
|
|
||||||
})
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue