Scripts: fix import into smaller parts

This commit is contained in:
Pieter Vander Vennet 2025-10-11 01:00:38 +02:00
parent 6ca410d26f
commit 5b4743be57

View file

@ -1,6 +1,6 @@
import Script from "../Script"
import { existsSync, promises as fs, readFileSync, writeFile, writeFileSync } from "fs"
import { Feature, FeatureCollection, Point } from "geojson"
import { Feature, FeatureCollection, Point, Polygon } from "geojson"
import { join } from "path"
import sqlite3, { Database } from "sqlite3"
import { open } from "sqlite"
@ -15,6 +15,7 @@ import ChangeTagAction from "../../src/Logic/Osm/Actions/ChangeTagAction"
import { Tag as OsmTag } from "../../src/Logic/Tags/Tag"
import { Changes } from "../../src/Logic/Osm/Changes"
import ScriptUtils from "../ScriptUtils"
import { GeoOperations } from "../../src/Logic/GeoOperations"
/**
* Note:
@ -56,6 +57,125 @@ function mediaUrl(sha: string | { "sha1": string }): string {
return `https://openbenches.org/image/${sha}.jpg`
}
const uk: Feature<Polygon> = {
"type": "Feature",
"properties": {},
"geometry": {
"coordinates": [
[
[
3.139397666817615,
53.112746745001914
],
[
0.12546232547020963,
61.34289409315957
],
[
-5.193638926198332,
60.3858935023425
],
[
-12.316831332595541,
56.76308878364702
],
[
-12.586640816376246,
51.076733390490034
],
[
-3.6443836396576046,
49.4256703574342
],
[
1.0194660085441853,
50.442813369706585
],
[
3.139397666817615,
53.112746745001914
]
]
],
"type": "Polygon"
}
}
const us : Feature<Polygon> = {
"type": "Feature",
"properties": {},
"geometry": {
"coordinates": [
[
[
-171.55472370762342,
71.44263911390138
],
[
-171.31347027402668,
33.24735774004321
],
[
-105.9804086342826,
-3.5292610992716362
],
[
-57.00596161415962,
15.805666337324794
],
[
-32.880618254493015,
49.584578264365916
],
[
-47.35582427029317,
72.85409976292118
],
[
-101.60890406091582,
79.0557752859543
],
[
-171.55472370762342,
71.44263911390138
]
]
],
"type": "Polygon"
}
}
const australia: Feature<Polygon> = {
"type": "Feature",
"properties": {},
"geometry": {
"coordinates": [
[
[
177.6309142850211,
-48.72845301037672
],
[
177.6309142850211,
-8.050870320392335
],
[
107.59695622498174,
-8.050870320392335
],
[
107.59695622498174,
-48.72845301037672
],
[
177.6309142850211,
-48.72845301037672
]
]
],
"type": "Polygon"
}
}
const areas = {uk, us, australia}
class Openbenches extends Script {
private db: Database
@ -200,14 +320,14 @@ class Openbenches extends Script {
return JSON.parse(readFileSync(alreadyImportedPath, "utf-8"))
}
async conflate(osmData: FeatureCollection, openBenchesData: FeatureCollection) {
async conflate(osmData: Feature[], openBenchesData: FeatureCollection, area: string = "") {
const dict: Map<string, Feature> = new Map()
for (const bench of openBenchesData.features) {
const obid = bench.properties["openbenches:id"]
dict.set("" + obid, bench)
}
const changes: OsmChangeAction[] = []
for (const bench of osmData.features) {
for (const bench of osmData) {
const obid = bench.properties["openbenches:id"]
const ob = dict.get(obid)
if (!ob) {
@ -256,8 +376,11 @@ class Openbenches extends Script {
}
}
}
if(changes.length === 0){
return
}
const xml = await Changes.createChangesetXMLForJosm(changes)
writeFileSync("attributes_import.osc",xml, "utf-8")
writeFileSync(`attributes_import${area}.osc`,xml, "utf-8")
}
async main(args: string[]): Promise<void> {
@ -289,6 +412,7 @@ class Openbenches extends Script {
tagsOnBenches.get(bench).push(tags.get(tg.tagID))
}
const alreadyLinked = new Set(osmData.features.map(f => f.properties["openbenches:id"]))
const r = await this.all<Bench & User>("SELECT * FROM benches INNER JOIN users ON benches.userID = users.userID")
const features: Feature<Point>[] = []
for (let i = 0; i < r.length; i++) {
@ -310,7 +434,12 @@ class Openbenches extends Script {
type: "FeatureCollection", features,
}, null, " "), "utf-8")
const maproulette = features.map(f => {
const maproulette = features
.filter(f => {
const openbenchesId = f.properties["openbenches:id"]
return !alreadyLinked.has(openbenchesId)
})
.map(f => {
const properties = {tags: JSON.stringify(f.properties)}
properties["id"] = "openbenches/"+f.properties["openbenches:id"]
return {...f, properties}
@ -319,9 +448,21 @@ class Openbenches extends Script {
type: "FeatureCollection", features: maproulette,
}, null, " "), "utf-8")
if(!createTest){
writeFileSync(`openbenches_export_maproulette_first_100.geojson`, JSON.stringify({
type: "FeatureCollection", features: maproulette.slice(0, 100),
}, null, " "), "utf-8")
}
const openBenches = JSON.parse(readFileSync("openbenches_export_josm_.geojson", "utf-8"))
// await this.conflate(osmData, openBenches)
for (const area in areas) {
const areaGeo = areas[area]
await this.conflate(osmData.features.filter(
f => GeoOperations.inside(GeoOperations.centerpointCoordinates(f),
areaGeo)
), openBenches, "_"+area)
}
await this.conflate(osmData.features, openBenches, "_all")
}
}