Refactoring: move download functionality for OsmObjects into a new object

This commit is contained in:
Pieter Vander Vennet 2023-04-20 03:58:31 +02:00
parent 8eb2c68f79
commit 1f9aacfb29
23 changed files with 633 additions and 901 deletions

View file

@ -3,13 +3,13 @@
*/
import { UIEventSource } from "../UIEventSource"
import { Changes } from "../Osm/Changes"
import { OsmObject } from "../Osm/OsmObject"
import { OsmConnection } from "../Osm/OsmConnection"
import LayoutConfig from "../../Models/ThemeConfig/LayoutConfig"
import SimpleMetaTagger from "../SimpleMetaTagger"
import FeaturePropertiesStore from "../FeatureSource/Actors/FeaturePropertiesStore"
import { Feature } from "geojson"
import { OsmTags } from "../../Models/OsmFeature"
import OsmObjectDownloader from "../Osm/OsmObjectDownloader"
export default class SelectedElementTagsUpdater {
private static readonly metatags = new Set([
@ -27,6 +27,7 @@ export default class SelectedElementTagsUpdater {
changes: Changes
osmConnection: OsmConnection
layout: LayoutConfig
osmObjectDownloader: OsmObjectDownloader
}
constructor(state: {
@ -35,6 +36,7 @@ export default class SelectedElementTagsUpdater {
changes: Changes
osmConnection: OsmConnection
layout: LayoutConfig
osmObjectDownloader: OsmObjectDownloader
}) {
this.state = state
state.osmConnection.isLoggedIn.addCallbackAndRun((isLoggedIn) => {
@ -70,8 +72,8 @@ export default class SelectedElementTagsUpdater {
return
}
try {
const latestTags = await OsmObject.DownloadPropertiesOf(id)
if (latestTags === "deleted") {
const osmObject = await state.osmObjectDownloader.DownloadObjectAsync(id)
if (osmObject === "deleted") {
console.warn("The current selected element has been deleted upstream!")
const currentTagsSource = state.featureProperties.getStore(id)
if (currentTagsSource.data["_deleted"] === "yes") {
@ -81,6 +83,7 @@ export default class SelectedElementTagsUpdater {
currentTagsSource.ping()
return
}
const latestTags = osmObject.tags
this.applyUpdate(latestTags, id)
console.log("Updated", id)
} catch (e) {

View file

@ -1,10 +1,10 @@
import { UIEventSource } from "../UIEventSource"
import { OsmObject } from "../Osm/OsmObject"
import Loc from "../../Models/Loc"
import { ElementStorage } from "../ElementStorage"
import FeaturePipeline from "../FeatureSource/FeaturePipeline"
import { GeoOperations } from "../GeoOperations"
import LayoutConfig from "../../Models/ThemeConfig/LayoutConfig"
import OsmObjectDownloader from "../Osm/OsmObjectDownloader"
/**
* Makes sure the hash shows the selected element and vice-versa.
@ -26,6 +26,7 @@ export default class SelectedFeatureHandler {
allElements: ElementStorage
locationControl: UIEventSource<Loc>
layoutToUse: LayoutConfig
objectDownloader: OsmObjectDownloader
}
constructor(
@ -36,6 +37,7 @@ export default class SelectedFeatureHandler {
featurePipeline: FeaturePipeline
locationControl: UIEventSource<Loc>
layoutToUse: LayoutConfig
objectDownloader: OsmObjectDownloader
}
) {
this.hash = hash
@ -65,8 +67,11 @@ export default class SelectedFeatureHandler {
return
}
OsmObject.DownloadObjectAsync(hash).then((obj) => {
this.state.objectDownloader.DownloadObjectAsync(hash).then((obj) => {
try {
if (obj === "deleted") {
return
}
console.log("Downloaded selected object from OSM-API for initial load: ", hash)
const geojson = obj.asGeoJson()
this.state.allElements.addOrGetElement(geojson)

View file

@ -1,10 +1,11 @@
import { Changes } from "../../Osm/Changes"
import { OsmNode, OsmObject, OsmRelation, OsmWay } from "../../Osm/OsmObject"
import { OsmNode, OsmRelation, OsmWay } from "../../Osm/OsmObject"
import { IndexedFeatureSource, WritableFeatureSource } from "../FeatureSource"
import { UIEventSource } from "../../UIEventSource"
import { ChangeDescription } from "../../Osm/Actions/ChangeDescription"
import { OsmId, OsmTags } from "../../../Models/OsmFeature"
import { Feature } from "geojson"
import OsmObjectDownloader from "../../Osm/OsmObjectDownloader"
export class NewGeometryFromChangesFeatureSource implements WritableFeatureSource {
// This class name truly puts the 'Java' into 'Javascript'
@ -21,7 +22,7 @@ export class NewGeometryFromChangesFeatureSource implements WritableFeatureSourc
const seenChanges = new Set<ChangeDescription>()
const features = this.features.data
const self = this
const backend = changes.backend
changes.pendingChanges.stabilized(100).addCallbackAndRunD((changes) => {
if (changes.length === 0) {
return
@ -58,15 +59,20 @@ export class NewGeometryFromChangesFeatureSource implements WritableFeatureSourc
}
console.debug("Detected a reused point")
// The 'allElementsStore' does _not_ have this point yet, so we have to create it
OsmObject.DownloadObjectAsync(change.type + "/" + change.id).then((feat) => {
console.log("Got the reused point:", feat)
for (const kv of change.tags) {
feat.tags[kv.k] = kv.v
}
const geojson = feat.asGeoJson()
self.features.data.push(geojson)
self.features.ping()
})
new OsmObjectDownloader(backend)
.DownloadObjectAsync(change.type + "/" + change.id)
.then((feat) => {
console.log("Got the reused point:", feat)
if (feat === "deleted") {
throw "Panic: snapping to a point, but this point has been deleted in the meantime"
}
for (const kv of change.tags) {
feat.tags[kv.k] = kv.v
}
const geojson = feat.asGeoJson()
self.features.data.push(geojson)
self.features.ping()
})
continue
} else if (change.id < 0 && change.changes === undefined) {
// The geometry is not described - not a new point

View file

@ -4,9 +4,9 @@ import { ImmutableStore, Store, UIEventSource } from "../../UIEventSource"
import { Tiles } from "../../../Models/TileRange"
import { BBox } from "../../BBox"
import { TagsFilter } from "../../Tags/TagsFilter"
import { OsmObject } from "../../Osm/OsmObject"
import { Feature } from "geojson"
import FeatureSourceMerger from "../Sources/FeatureSourceMerger"
import OsmObjectDownloader from "../../Osm/OsmObjectDownloader"
/**
* If a tile is needed (requested via the UIEventSource in the constructor), will download the appropriate tile and pass it via 'handleTile'
@ -101,7 +101,17 @@ export default class OsmFeatureSource extends FeatureSourceMerger {
// This member is missing. We redownload the entire relation instead
console.debug("Fetching incomplete relation " + feature.properties.id)
return (await OsmObject.DownloadObjectAsync(feature.properties.id)).asGeoJson()
const dfeature = await new OsmObjectDownloader(this._backend).DownloadObjectAsync(
feature.properties.id
)
if (dfeature === "deleted") {
console.warn(
"This relation has been deleted in the meantime: ",
feature.properties.id
)
return
}
return dfeature.asGeoJson()
}
return feature
}
@ -149,6 +159,7 @@ export default class OsmFeatureSource extends FeatureSourceMerger {
for (let i = 0; i < features.length; i++) {
features[i] = await this.patchIncompleteRelations(features[i], osmJson)
}
features = Utils.NoNull(features)
features.forEach((f) => {
f.properties["_backend"] = this._backend
})

View file

@ -8,6 +8,7 @@ import { And } from "../../Tags/And"
import { Tag } from "../../Tags/Tag"
import { OsmId } from "../../../Models/OsmFeature"
import { Utils } from "../../../Utils"
import OsmObjectDownloader from "../OsmObjectDownloader";
export default class DeleteAction extends OsmChangeAction {
private readonly _softDeletionTags: TagsFilter
@ -71,8 +72,12 @@ export default class DeleteAction extends OsmChangeAction {
changes: Changes,
object?: OsmObject
): Promise<ChangeDescription[]> {
const osmObject = object ?? (await OsmObject.DownloadObjectAsync(this._id))
const osmObject = object ?? (await new OsmObjectDownloader(changes.backend, changes).DownloadObjectAsync(this._id))
if(osmObject === "deleted"){
// already deleted in the meantime - no more changes necessary
return []
}
if (this._hardDelete) {
return [
{

View file

@ -1,7 +1,8 @@
import OsmChangeAction from "./OsmChangeAction"
import { Changes } from "../Changes"
import { ChangeDescription } from "./ChangeDescription"
import { OsmObject, OsmRelation, OsmWay } from "../OsmObject"
import { OsmRelation, OsmWay } from "../OsmObject"
import OsmObjectDownloader from "../OsmObjectDownloader"
export interface RelationSplitInput {
relation: OsmRelation
@ -14,11 +15,13 @@ export interface RelationSplitInput {
abstract class AbstractRelationSplitHandler extends OsmChangeAction {
protected readonly _input: RelationSplitInput
protected readonly _theme: string
protected readonly _objectDownloader: OsmObjectDownloader
constructor(input: RelationSplitInput, theme: string) {
constructor(input: RelationSplitInput, theme: string, objectDownloader: OsmObjectDownloader) {
super("relation/" + input.relation.id, false)
this._input = input
this._theme = theme
this._objectDownloader = objectDownloader
}
/**
@ -33,7 +36,9 @@ abstract class AbstractRelationSplitHandler extends OsmChangeAction {
return member.ref
}
if (member.type === "way") {
const osmWay = <OsmWay>await OsmObject.DownloadObjectAsync("way/" + member.ref)
const osmWay = <OsmWay>(
await this._objectDownloader.DownloadObjectAsync("way/" + member.ref)
)
const nodes = osmWay.nodes
if (first) {
return nodes[0]
@ -52,26 +57,30 @@ abstract class AbstractRelationSplitHandler extends OsmChangeAction {
* When a way is split and this way is part of a relation, the relation should be updated too to have the new segment if relevant.
*/
export default class RelationSplitHandler extends AbstractRelationSplitHandler {
constructor(input: RelationSplitInput, theme: string) {
super(input, theme)
constructor(input: RelationSplitInput, theme: string, objectDownloader: OsmObjectDownloader) {
super(input, theme, objectDownloader)
}
async CreateChangeDescriptions(changes: Changes): Promise<ChangeDescription[]> {
if (this._input.relation.tags["type"] === "restriction") {
// This is a turn restriction
return new TurnRestrictionRSH(this._input, this._theme).CreateChangeDescriptions(
changes
)
return new TurnRestrictionRSH(
this._input,
this._theme,
this._objectDownloader
).CreateChangeDescriptions(changes)
}
return new InPlaceReplacedmentRTSH(this._input, this._theme).CreateChangeDescriptions(
changes
)
return new InPlaceReplacedmentRTSH(
this._input,
this._theme,
this._objectDownloader
).CreateChangeDescriptions(changes)
}
}
export class TurnRestrictionRSH extends AbstractRelationSplitHandler {
constructor(input: RelationSplitInput, theme: string) {
super(input, theme)
constructor(input: RelationSplitInput, theme: string, objectDownloader: OsmObjectDownloader) {
super(input, theme, objectDownloader)
}
public async CreateChangeDescriptions(changes: Changes): Promise<ChangeDescription[]> {
@ -91,9 +100,11 @@ export class TurnRestrictionRSH extends AbstractRelationSplitHandler {
if (selfMember.role === "via") {
// A via way can be replaced in place
return new InPlaceReplacedmentRTSH(this._input, this._theme).CreateChangeDescriptions(
changes
)
return new InPlaceReplacedmentRTSH(
this._input,
this._theme,
this._objectDownloader
).CreateChangeDescriptions(changes)
}
// We have to keep only the way with a common point with the rest of the relation
@ -166,8 +177,8 @@ export class TurnRestrictionRSH extends AbstractRelationSplitHandler {
* Note that the feature might appear multiple times.
*/
export class InPlaceReplacedmentRTSH extends AbstractRelationSplitHandler {
constructor(input: RelationSplitInput, theme: string) {
super(input, theme)
constructor(input: RelationSplitInput, theme: string, objectDownloader: OsmObjectDownloader) {
super(input, theme, objectDownloader)
}
async CreateChangeDescriptions(changes: Changes): Promise<ChangeDescription[]> {

View file

@ -5,6 +5,7 @@ import OsmChangeAction from "./OsmChangeAction"
import { ChangeDescription } from "./ChangeDescription"
import RelationSplitHandler from "./RelationSplitHandler"
import { Feature, LineString } from "geojson"
import OsmObjectDownloader from "../OsmObjectDownloader"
interface SplitInfo {
originalIndex?: number // or negative for new elements
@ -61,7 +62,9 @@ export default class SplitAction extends OsmChangeAction {
}
async CreateChangeDescriptions(changes: Changes): Promise<ChangeDescription[]> {
const originalElement = <OsmWay>await OsmObject.DownloadObjectAsync(this.wayId)
const originalElement = <OsmWay>(
await new OsmObjectDownloader(changes.backend, changes).DownloadObjectAsync(this.wayId)
)
const originalNodes = originalElement.nodes
// First, calculate the splitpoints and remove points close to one another
@ -172,7 +175,8 @@ export default class SplitAction extends OsmChangeAction {
// At last, we still have to check that we aren't part of a relation...
// At least, the order of the ways is identical, so we can keep the same roles
const relations = await OsmObject.DownloadReferencingRelations(this.wayId)
const downloader = new OsmObjectDownloader(changes.backend, changes)
const relations = await downloader.DownloadReferencingRelations(this.wayId)
for (const relation of relations) {
const changDescrs = await new RelationSplitHandler(
{
@ -182,7 +186,8 @@ export default class SplitAction extends OsmChangeAction {
allWaysNodesInOrder: allWaysNodesInOrder,
originalWayId: originalElement.id,
},
this._meta.theme
this._meta.theme,
downloader
).CreateChangeDescriptions(changes)
changeDescription.push(...changDescrs)
}

View file

@ -12,6 +12,7 @@ import { GeoOperations } from "../GeoOperations"
import { ChangesetHandler, ChangesetTag } from "./ChangesetHandler"
import { OsmConnection } from "./OsmConnection"
import FeaturePropertiesStore from "../FeatureSource/Actors/FeaturePropertiesStore"
import OsmObjectDownloader from "./OsmObjectDownloader"
/**
* Handles all changes made to OSM.
@ -23,10 +24,10 @@ export class Changes {
public readonly allChanges = new UIEventSource<ChangeDescription[]>(undefined)
public readonly state: { allElements?: IndexedFeatureSource; osmConnection: OsmConnection }
public readonly extraComment: UIEventSource<string> = new UIEventSource(undefined)
public readonly backend: string
private readonly historicalUserLocations?: FeatureSource
private _nextId: number = -1 // Newly assigned ID's are negative
private readonly isUploading = new UIEventSource(false)
public readonly isUploading = new UIEventSource(false)
private readonly previouslyCreated: OsmObject[] = []
private readonly _leftRightSensitive: boolean
private readonly _changesetHandler: ChangesetHandler
@ -47,6 +48,7 @@ export class Changes {
// If a pending change contains a negative ID, we save that
this._nextId = Math.min(-1, ...(this.pendingChanges.data?.map((pch) => pch.id) ?? []))
this.state = state
this.backend = state.osmConnection.Backend()
this._changesetHandler = new ChangesetHandler(
state.dryRun,
state.osmConnection,
@ -149,274 +151,6 @@ export class Changes {
this.allChanges.ping()
}
private calculateDistanceToChanges(
change: OsmChangeAction,
changeDescriptions: ChangeDescription[]
) {
const locations = this.historicalUserLocations?.features?.data
if (locations === undefined) {
// No state loaded or no locations -> we can't calculate...
return
}
if (!change.trackStatistics) {
// Probably irrelevant, such as a new helper node
return
}
const now = new Date()
const recentLocationPoints = locations
.filter((feat) => feat.geometry.type === "Point")
.filter((feat) => {
const visitTime = new Date(
(<GeoLocationPointProperties>(<any>feat.properties)).date
)
// In seconds
const diff = (now.getTime() - visitTime.getTime()) / 1000
return diff < Constants.nearbyVisitTime
})
if (recentLocationPoints.length === 0) {
// Probably no GPS enabled/no fix
return
}
// The applicable points, contain information in their properties about location, time and GPS accuracy
// They are all GeoLocationPointProperties
// We walk every change and determine the closest distance possible
// Only if the change itself does _not_ contain any coordinates, we fall back and search the original feature in the state
const changedObjectCoordinates: [number, number][] = []
{
const feature = this.state.allElements?.featuresById?.data.get(change.mainObjectId)
if (feature !== undefined) {
changedObjectCoordinates.push(GeoOperations.centerpointCoordinates(feature))
}
}
for (const changeDescription of changeDescriptions) {
const chng:
| { lat: number; lon: number }
| { coordinates: [number, number][] }
| { members } = changeDescription.changes
if (chng === undefined) {
continue
}
if (chng["lat"] !== undefined) {
changedObjectCoordinates.push([chng["lat"], chng["lon"]])
}
if (chng["coordinates"] !== undefined) {
changedObjectCoordinates.push(...chng["coordinates"])
}
}
return Math.min(
...changedObjectCoordinates.map((coor) =>
Math.min(
...recentLocationPoints.map((gpsPoint) => {
const otherCoor = GeoOperations.centerpointCoordinates(gpsPoint)
return GeoOperations.distanceBetween(coor, otherCoor)
})
)
)
)
}
/**
* UPload the selected changes to OSM.
* Returns 'true' if successfull and if they can be removed
*/
private async flushSelectChanges(
pending: ChangeDescription[],
openChangeset: UIEventSource<number>
): Promise<boolean> {
const self = this
const neededIds = Changes.GetNeededIds(pending)
const osmObjects = Utils.NoNull(
await Promise.all(
neededIds.map(async (id) =>
OsmObject.DownloadObjectAsync(id).catch((e) => {
console.error(
"Could not download OSM-object",
id,
" dropping it from the changes (" + e + ")"
)
pending = pending.filter((ch) => ch.type + "/" + ch.id !== id)
return undefined
})
)
)
)
if (this._leftRightSensitive) {
osmObjects.forEach((obj) => SimpleMetaTagger.removeBothTagging(obj.tags))
}
console.log("Got the fresh objects!", osmObjects, "pending: ", pending)
if (pending.length == 0) {
console.log("No pending changes...")
return true
}
const perType = Array.from(
Utils.Hist(
pending
.filter(
(descr) =>
descr.meta.changeType !== undefined && descr.meta.changeType !== null
)
.map((descr) => descr.meta.changeType)
),
([key, count]) => ({
key: key,
value: count,
aggregate: true,
})
)
const motivations = pending
.filter((descr) => descr.meta.specialMotivation !== undefined)
.map((descr) => ({
key: descr.meta.changeType + ":" + descr.type + "/" + descr.id,
value: descr.meta.specialMotivation,
}))
const distances = Utils.NoNull(pending.map((descr) => descr.meta.distanceToObject))
distances.sort((a, b) => a - b)
const perBinCount = Constants.distanceToChangeObjectBins.map((_) => 0)
let j = 0
const maxDistances = Constants.distanceToChangeObjectBins
for (let i = 0; i < maxDistances.length; i++) {
const maxDistance = maxDistances[i]
// distances is sorted in ascending order, so as soon as one is to big, all the resting elements will be bigger too
while (j < distances.length && distances[j] < maxDistance) {
perBinCount[i]++
j++
}
}
const perBinMessage = Utils.NoNull(
perBinCount.map((count, i) => {
if (count === 0) {
return undefined
}
const maxD = maxDistances[i]
let key = `change_within_${maxD}m`
if (maxD === Number.MAX_VALUE) {
key = `change_over_${maxDistances[i - 1]}m`
}
return {
key,
value: count,
aggregate: true,
}
})
)
// This method is only called with changedescriptions for this theme
const theme = pending[0].meta.theme
let comment = "Adding data with #MapComplete for theme #" + theme
if (this.extraComment.data !== undefined) {
comment += "\n\n" + this.extraComment.data
}
const metatags: ChangesetTag[] = [
{
key: "comment",
value: comment,
},
{
key: "theme",
value: theme,
},
...perType,
...motivations,
...perBinMessage,
]
await this._changesetHandler.UploadChangeset(
(csId, remappings) => {
if (remappings.size > 0) {
console.log("Rewriting pending changes from", pending, "with", remappings)
pending = pending.map((ch) => ChangeDescriptionTools.rewriteIds(ch, remappings))
console.log("Result is", pending)
}
const changes: {
newObjects: OsmObject[]
modifiedObjects: OsmObject[]
deletedObjects: OsmObject[]
} = self.CreateChangesetObjects(pending, osmObjects)
return Changes.createChangesetFor("" + csId, changes)
},
metatags,
openChangeset
)
console.log("Upload successfull!")
return true
}
private async flushChangesAsync(): Promise<void> {
const self = this
try {
// At last, we build the changeset and upload
const pending = self.pendingChanges.data
const pendingPerTheme = new Map<string, ChangeDescription[]>()
for (const changeDescription of pending) {
const theme = changeDescription.meta.theme
if (!pendingPerTheme.has(theme)) {
pendingPerTheme.set(theme, [])
}
pendingPerTheme.get(theme).push(changeDescription)
}
const successes = await Promise.all(
Array.from(pendingPerTheme, async ([theme, pendingChanges]) => {
try {
const openChangeset = this.state.osmConnection
.GetPreference("current-open-changeset-" + theme)
.sync(
(str) => {
const n = Number(str)
if (isNaN(n)) {
return undefined
}
return n
},
[],
(n) => "" + n
)
console.log(
"Using current-open-changeset-" +
theme +
" from the preferences, got " +
openChangeset.data
)
return await self.flushSelectChanges(pendingChanges, openChangeset)
} catch (e) {
console.error("Could not upload some changes:", e)
return false
}
})
)
if (!successes.some((s) => s == false)) {
// All changes successfull, we clear the data!
this.pendingChanges.setData([])
}
} catch (e) {
console.error(
"Could not handle changes - probably an old, pending changeset in localstorage with an invalid format; erasing those",
e
)
self.pendingChanges.setData([])
} finally {
self.isUploading.setData(false)
}
}
public CreateChangesetObjects(
changes: ChangeDescription[],
downloadedOsmObjects: OsmObject[]
@ -584,4 +318,285 @@ export class Changes {
)
return result
}
private calculateDistanceToChanges(
change: OsmChangeAction,
changeDescriptions: ChangeDescription[]
) {
const locations = this.historicalUserLocations?.features?.data
if (locations === undefined) {
// No state loaded or no locations -> we can't calculate...
return
}
if (!change.trackStatistics) {
// Probably irrelevant, such as a new helper node
return
}
const now = new Date()
const recentLocationPoints = locations
.filter((feat) => feat.geometry.type === "Point")
.filter((feat) => {
const visitTime = new Date(
(<GeoLocationPointProperties>(<any>feat.properties)).date
)
// In seconds
const diff = (now.getTime() - visitTime.getTime()) / 1000
return diff < Constants.nearbyVisitTime
})
if (recentLocationPoints.length === 0) {
// Probably no GPS enabled/no fix
return
}
// The applicable points, contain information in their properties about location, time and GPS accuracy
// They are all GeoLocationPointProperties
// We walk every change and determine the closest distance possible
// Only if the change itself does _not_ contain any coordinates, we fall back and search the original feature in the state
const changedObjectCoordinates: [number, number][] = []
{
const feature = this.state.allElements?.featuresById?.data.get(change.mainObjectId)
if (feature !== undefined) {
changedObjectCoordinates.push(GeoOperations.centerpointCoordinates(feature))
}
}
for (const changeDescription of changeDescriptions) {
const chng:
| { lat: number; lon: number }
| { coordinates: [number, number][] }
| { members } = changeDescription.changes
if (chng === undefined) {
continue
}
if (chng["lat"] !== undefined) {
changedObjectCoordinates.push([chng["lat"], chng["lon"]])
}
if (chng["coordinates"] !== undefined) {
changedObjectCoordinates.push(...chng["coordinates"])
}
}
return Math.min(
...changedObjectCoordinates.map((coor) =>
Math.min(
...recentLocationPoints.map((gpsPoint) => {
const otherCoor = GeoOperations.centerpointCoordinates(gpsPoint)
return GeoOperations.distanceBetween(coor, otherCoor)
})
)
)
)
}
/**
* Upload the selected changes to OSM.
* Returns 'true' if successful and if they can be removed
*/
private async flushSelectChanges(
pending: ChangeDescription[],
openChangeset: UIEventSource<number>
): Promise<boolean> {
const self = this
const neededIds = Changes.GetNeededIds(pending)
// We _do not_ pass in the Changes object itself - we want the data from OSM directly in order to apply the changes
const downloader = new OsmObjectDownloader(this.backend, undefined)
let osmObjects = await Promise.all<{ id: string; osmObj: OsmObject | "deleted" }>(
neededIds.map(async (id) => {
try {
const osmObj = await downloader.DownloadObjectAsync(id)
return { id, osmObj }
} catch (e) {
console.error(
"Could not download OSM-object",
id,
" dropping it from the changes (" + e + ")"
)
return undefined
}
})
)
osmObjects = Utils.NoNull(osmObjects)
for (const { osmObj, id } of osmObjects) {
if (osmObj === "deleted") {
pending = pending.filter((ch) => ch.type + "/" + ch.id !== id)
}
}
const objects = osmObjects
.filter((obj) => obj.osmObj !== "deleted")
.map((obj) => <OsmObject>obj.osmObj)
if (this._leftRightSensitive) {
objects.forEach((obj) => SimpleMetaTagger.removeBothTagging(obj.tags))
}
console.log("Got the fresh objects!", objects, "pending: ", pending)
if (pending.length == 0) {
console.log("No pending changes...")
return true
}
const perType = Array.from(
Utils.Hist(
pending
.filter(
(descr) =>
descr.meta.changeType !== undefined && descr.meta.changeType !== null
)
.map((descr) => descr.meta.changeType)
),
([key, count]) => ({
key: key,
value: count,
aggregate: true,
})
)
const motivations = pending
.filter((descr) => descr.meta.specialMotivation !== undefined)
.map((descr) => ({
key: descr.meta.changeType + ":" + descr.type + "/" + descr.id,
value: descr.meta.specialMotivation,
}))
const distances = Utils.NoNull(pending.map((descr) => descr.meta.distanceToObject))
distances.sort((a, b) => a - b)
const perBinCount = Constants.distanceToChangeObjectBins.map((_) => 0)
let j = 0
const maxDistances = Constants.distanceToChangeObjectBins
for (let i = 0; i < maxDistances.length; i++) {
const maxDistance = maxDistances[i]
// distances is sorted in ascending order, so as soon as one is to big, all the resting elements will be bigger too
while (j < distances.length && distances[j] < maxDistance) {
perBinCount[i]++
j++
}
}
const perBinMessage = Utils.NoNull(
perBinCount.map((count, i) => {
if (count === 0) {
return undefined
}
const maxD = maxDistances[i]
let key = `change_within_${maxD}m`
if (maxD === Number.MAX_VALUE) {
key = `change_over_${maxDistances[i - 1]}m`
}
return {
key,
value: count,
aggregate: true,
}
})
)
// This method is only called with changedescriptions for this theme
const theme = pending[0].meta.theme
let comment = "Adding data with #MapComplete for theme #" + theme
if (this.extraComment.data !== undefined) {
comment += "\n\n" + this.extraComment.data
}
const metatags: ChangesetTag[] = [
{
key: "comment",
value: comment,
},
{
key: "theme",
value: theme,
},
...perType,
...motivations,
...perBinMessage,
]
await this._changesetHandler.UploadChangeset(
(csId, remappings) => {
if (remappings.size > 0) {
console.log("Rewriting pending changes from", pending, "with", remappings)
pending = pending.map((ch) => ChangeDescriptionTools.rewriteIds(ch, remappings))
console.log("Result is", pending)
}
const changes: {
newObjects: OsmObject[]
modifiedObjects: OsmObject[]
deletedObjects: OsmObject[]
} = self.CreateChangesetObjects(pending, objects)
return Changes.createChangesetFor("" + csId, changes)
},
metatags,
openChangeset
)
console.log("Upload successfull!")
return true
}
private async flushChangesAsync(): Promise<void> {
const self = this
try {
// At last, we build the changeset and upload
const pending = self.pendingChanges.data
const pendingPerTheme = new Map<string, ChangeDescription[]>()
for (const changeDescription of pending) {
const theme = changeDescription.meta.theme
if (!pendingPerTheme.has(theme)) {
pendingPerTheme.set(theme, [])
}
pendingPerTheme.get(theme).push(changeDescription)
}
const successes = await Promise.all(
Array.from(pendingPerTheme, async ([theme, pendingChanges]) => {
try {
const openChangeset = this.state.osmConnection
.GetPreference("current-open-changeset-" + theme)
.sync(
(str) => {
const n = Number(str)
if (isNaN(n)) {
return undefined
}
return n
},
[],
(n) => "" + n
)
console.log(
"Using current-open-changeset-" +
theme +
" from the preferences, got " +
openChangeset.data
)
return await self.flushSelectChanges(pendingChanges, openChangeset)
} catch (e) {
console.error("Could not upload some changes:", e)
return false
}
})
)
if (!successes.some((s) => s == false)) {
// All changes successfull, we clear the data!
this.pendingChanges.setData([])
}
} catch (e) {
console.error(
"Could not handle changes - probably an old, pending changeset in localstorage with an invalid format; erasing those",
e
)
self.pendingChanges.setData([])
} finally {
self.isUploading.setData(false)
}
}
}

View file

@ -82,7 +82,6 @@ export class OsmConnection {
OsmConnection.oauth_configs[options.osmConfiguration ?? "osm"] ??
OsmConnection.oauth_configs.osm
console.debug("Using backend", this._oauth_config.url)
OsmObject.SetBackendUrl(this._oauth_config.url + "/")
this._iframeMode = Utils.runningFromConsole ? false : window !== window.top
this.userDetails = new UIEventSource<UserDetails>(

View file

@ -1,17 +1,13 @@
import { Utils } from "../../Utils"
import polygon_features from "../../assets/polygon-features.json"
import { Store, UIEventSource } from "../UIEventSource"
import { BBox } from "../BBox"
import OsmToGeoJson from "osmtogeojson"
import { NodeId, OsmFeature, OsmId, OsmTags, RelationId, WayId } from "../../Models/OsmFeature"
import { OsmFeature, OsmId, OsmTags, WayId } from "../../Models/OsmFeature"
import { Feature, LineString, Polygon } from "geojson"
export abstract class OsmObject {
private static defaultBackend = "https://www.openstreetmap.org/"
protected static backendURL = OsmObject.defaultBackend
private static polygonFeatures = OsmObject.constructPolygonFeatures()
private static objectCache = new Map<string, UIEventSource<OsmObject>>()
private static historyCache = new Map<string, UIEventSource<OsmObject[]>>()
type: "node" | "way" | "relation"
id: number
/**
@ -31,190 +27,6 @@ export abstract class OsmObject {
}
}
public static SetBackendUrl(url: string) {
if (!url.endsWith("/")) {
throw "Backend URL must end with a '/'"
}
if (!url.startsWith("http")) {
throw "Backend URL must begin with http"
}
this.backendURL = url
}
public static DownloadObject(id: NodeId, forceRefresh?: boolean): Store<OsmNode>
public static DownloadObject(id: RelationId, forceRefresh?: boolean): Store<OsmRelation>
public static DownloadObject(id: WayId, forceRefresh?: boolean): Store<OsmWay>
public static DownloadObject(id: string, forceRefresh: boolean = false): Store<OsmObject> {
let src: UIEventSource<OsmObject>
if (OsmObject.objectCache.has(id)) {
src = OsmObject.objectCache.get(id)
if (forceRefresh) {
src.setData(undefined)
} else {
return src
}
} else {
src = UIEventSource.FromPromise(OsmObject.DownloadObjectAsync(id))
}
OsmObject.objectCache.set(id, src)
return src
}
static async DownloadPropertiesOf(id: string): Promise<OsmTags | "deleted"> {
const splitted = id.split("/")
const idN = Number(splitted[1])
if (idN < 0) {
return undefined
}
const url = `${OsmObject.backendURL}api/0.6/${id}`
const rawData = await Utils.downloadJsonCachedAdvanced(url, 1000)
if (rawData["error"] !== undefined && rawData["statuscode"] === 410) {
return "deleted"
}
// Tags is undefined if the element does not have any tags
return rawData["content"].elements[0].tags ?? {}
}
static async DownloadObjectAsync(
id: NodeId,
maxCacheAgeInSecs?: number
): Promise<OsmNode | undefined>
static async DownloadObjectAsync(
id: WayId,
maxCacheAgeInSecs?: number
): Promise<OsmWay | undefined>
static async DownloadObjectAsync(
id: RelationId,
maxCacheAgeInSecs?: number
): Promise<OsmRelation | undefined>
static async DownloadObjectAsync(
id: OsmId,
maxCacheAgeInSecs?: number
): Promise<OsmObject | undefined>
static async DownloadObjectAsync(
id: string,
maxCacheAgeInSecs?: number
): Promise<OsmObject | undefined>
static async DownloadObjectAsync(
id: string,
maxCacheAgeInSecs?: number
): Promise<OsmObject | undefined> {
const splitted = id.split("/")
const type = splitted[0]
const idN = Number(splitted[1])
if (idN < 0) {
return undefined
}
const full = !id.startsWith("node") ? "/full" : ""
const url = `${OsmObject.backendURL}api/0.6/${id}${full}`
const rawData = await Utils.downloadJsonCached(url, (maxCacheAgeInSecs ?? 10) * 1000)
if (rawData === undefined) {
return undefined
}
// A full query might contain more then just the requested object (e.g. nodes that are part of a way, where we only want the way)
const parsed = OsmObject.ParseObjects(rawData.elements)
// Lets fetch the object we need
for (const osmObject of parsed) {
if (osmObject.type !== type) {
continue
}
if (osmObject.id !== idN) {
continue
}
// Found the one!
return osmObject
}
throw "PANIC: requested object is not part of the response"
}
/**
* Downloads the ways that are using this node.
* Beware: their geometry will be incomplete!
*/
public static DownloadReferencingWays(id: string): Promise<OsmWay[]> {
return Utils.downloadJsonCached(
`${OsmObject.backendURL}api/0.6/${id}/ways`,
60 * 1000
).then((data) => {
return data.elements.map((wayInfo) => {
const way = new OsmWay(wayInfo.id)
way.LoadData(wayInfo)
return way
})
})
}
/**
* Downloads the relations that are using this feature.
* Beware: their geometry will be incomplete!
*/
public static async DownloadReferencingRelations(id: string): Promise<OsmRelation[]> {
const data = await Utils.downloadJsonCached(
`${OsmObject.backendURL}api/0.6/${id}/relations`,
60 * 1000
)
return data.elements.map((wayInfo) => {
const rel = new OsmRelation(wayInfo.id)
rel.LoadData(wayInfo)
rel.SaveExtraData(wayInfo, undefined)
return rel
})
}
public static DownloadHistory(id: NodeId): UIEventSource<OsmNode[]>
public static DownloadHistory(id: WayId): UIEventSource<OsmWay[]>
public static DownloadHistory(id: RelationId): UIEventSource<OsmRelation[]>
public static DownloadHistory(id: OsmId): UIEventSource<OsmObject[]>
public static DownloadHistory(id: string): UIEventSource<OsmObject[]> {
if (OsmObject.historyCache.has(id)) {
return OsmObject.historyCache.get(id)
}
const splitted = id.split("/")
const type = splitted[0]
const idN = Number(splitted[1])
const src = new UIEventSource<OsmObject[]>([])
OsmObject.historyCache.set(id, src)
Utils.downloadJsonCached(
`${OsmObject.backendURL}api/0.6/${type}/${idN}/history`,
10 * 60 * 1000
).then((data) => {
const elements: any[] = data.elements
const osmObjects: OsmObject[] = []
for (const element of elements) {
let osmObject: OsmObject = null
element.nodes = []
switch (type) {
case "node":
osmObject = new OsmNode(idN)
break
case "way":
osmObject = new OsmWay(idN)
break
case "relation":
osmObject = new OsmRelation(idN)
break
}
osmObject?.LoadData(element)
osmObject?.SaveExtraData(element, [])
osmObjects.push(osmObject)
}
src.setData(osmObjects)
})
return src
}
// bounds should be: [[maxlat, minlon], [minlat, maxlon]] (same as Utils.tile_bounds)
public static async LoadArea(bbox: BBox): Promise<OsmObject[]> {
const url = `${OsmObject.backendURL}api/0.6/map.json?bbox=${bbox.minLon},${bbox.minLat},${bbox.maxLon},${bbox.maxLat}`
const data = await Utils.downloadJson(url)
const elements: any[] = data.elements
return OsmObject.ParseObjects(elements)
}
public static ParseObjects(elements: any[]): OsmObject[] {
const objects: OsmObject[] = []
const allNodes: Map<number, OsmNode> = new Map<number, OsmNode>()
@ -357,12 +169,16 @@ export abstract class OsmObject {
return 'version="' + this.version + '"'
}
private LoadData(element: any): void {
this.tags = element.tags ?? this.tags
this.version = element.version
this.timestamp = element.timestamp
protected LoadData(element: any): void {
if (element === undefined) {
return
}
this.tags = element?.tags ?? this.tags
const tgs = this.tags
if (element.tags === undefined) {
tgs["id"] = <OsmId>(this.type + "/" + this.id)
this.version = element?.version
this.timestamp = element?.timestamp
if (element?.tags === undefined) {
// Simple node which is part of a way - not important
return
}
@ -371,7 +187,6 @@ export abstract class OsmObject {
tgs["_last_edit:changeset"] = element.changeset
tgs["_last_edit:timestamp"] = element.timestamp
tgs["_version_number"] = element.version
tgs["id"] = <OsmId>(this.type + "/" + this.id)
}
}
@ -379,8 +194,9 @@ export class OsmNode extends OsmObject {
lat: number
lon: number
constructor(id: number) {
constructor(id: number, extraData?) {
super("node", id)
this.LoadData(extraData)
}
ChangesetXML(changesetId: string): string {
@ -431,8 +247,9 @@ export class OsmWay extends OsmObject {
lat: number
lon: number
constructor(id: number) {
constructor(id: number, wayInfo?) {
super("way", id)
this.LoadData(wayInfo)
}
centerpoint(): [number, number] {
@ -535,8 +352,9 @@ export class OsmRelation extends OsmObject {
private geojson = undefined
constructor(id: number) {
constructor(id: number, extraInfo?: any) {
super("relation", id)
this.LoadData(extraInfo)
}
centerpoint(): [number, number] {

View file

@ -0,0 +1,152 @@
import { Utils } from "../../Utils"
import { OsmNode, OsmObject, OsmRelation, OsmWay } from "./OsmObject"
import { NodeId, OsmId, RelationId, WayId } from "../../Models/OsmFeature"
import { Store, UIEventSource } from "../UIEventSource"
import { ChangeDescription } from "./Actions/ChangeDescription"
/**
* The OSM-Object downloader downloads the latest version of the object, but applies 'pendingchanges' to them,
* so that we always have a consistent view
*/
export default class OsmObjectDownloader {
private readonly _changes?: {
readonly pendingChanges: UIEventSource<ChangeDescription[]>
readonly isUploading: Store<boolean>
}
private readonly backend: string
private historyCache = new Map<string, UIEventSource<OsmObject[]>>()
constructor(
backend: string = "https://openstreetmap.org",
changes?: {
readonly pendingChanges: UIEventSource<ChangeDescription[]>
readonly isUploading: Store<boolean>
}
) {
this._changes = changes
if (!backend.endsWith("/")) {
backend += "/"
}
if (!backend.startsWith("http")) {
throw "Backend URL must begin with http"
}
this.backend = backend
}
async DownloadObjectAsync(id: NodeId, maxCacheAgeInSecs?: number): Promise<OsmNode | "deleted">
async DownloadObjectAsync(id: WayId, maxCacheAgeInSecs?: number): Promise<OsmWay | "deleted">
async DownloadObjectAsync(
id: RelationId,
maxCacheAgeInSecs?: number
): Promise<OsmRelation | undefined>
async DownloadObjectAsync(id: OsmId, maxCacheAgeInSecs?: number): Promise<OsmObject | "deleted">
async DownloadObjectAsync(
id: string,
maxCacheAgeInSecs?: number
): Promise<OsmObject | "deleted">
async DownloadObjectAsync(
id: string,
maxCacheAgeInSecs?: number
): Promise<OsmObject | "deleted"> {
const splitted = id.split("/")
const type = splitted[0]
const idN = Number(splitted[1])
if (idN < 0) {
throw "Invalid request: cannot download OsmObject " + id + ", it has a negative id"
}
const full = !id.startsWith("node") ? "/full" : ""
const url = `${this.backend}api/0.6/${id}${full}`
const rawData = await Utils.downloadJsonCachedAdvanced(
url,
(maxCacheAgeInSecs ?? 10) * 1000
)
if (rawData["error"] !== undefined && rawData["statuscode"] === 410) {
return "deleted"
}
// A full query might contain more then just the requested object (e.g. nodes that are part of a way, where we only want the way)
const parsed = OsmObject.ParseObjects(rawData["content"].elements)
// Lets fetch the object we need
for (const osmObject of parsed) {
if (osmObject.type !== type) {
continue
}
if (osmObject.id !== idN) {
continue
}
// Found the one!
return osmObject
}
throw "PANIC: requested object is not part of the response"
}
public DownloadHistory(id: NodeId): UIEventSource<OsmNode[]>
public DownloadHistory(id: WayId): UIEventSource<OsmWay[]>
public DownloadHistory(id: RelationId): UIEventSource<OsmRelation[]>
public DownloadHistory(id: OsmId): UIEventSource<OsmObject[]>
public DownloadHistory(id: string): UIEventSource<OsmObject[]> {
if (this.historyCache.has(id)) {
return this.historyCache.get(id)
}
const splitted = id.split("/")
const type = splitted[0]
const idN = Number(splitted[1])
const src = new UIEventSource<OsmObject[]>([])
this.historyCache.set(id, src)
Utils.downloadJsonCached(
`${this.backend}api/0.6/${type}/${idN}/history`,
10 * 60 * 1000
).then((data) => {
const elements: any[] = data.elements
const osmObjects: OsmObject[] = []
for (const element of elements) {
let osmObject: OsmObject = null
element.nodes = []
switch (type) {
case "node":
osmObject = new OsmNode(idN, element)
break
case "way":
osmObject = new OsmWay(idN, element)
break
case "relation":
osmObject = new OsmRelation(idN, element)
break
}
osmObject?.SaveExtraData(element, [])
osmObjects.push(osmObject)
}
src.setData(osmObjects)
})
return src
}
/**
* Downloads the ways that are using this node.
* Beware: their geometry will be incomplete!
*/
public async DownloadReferencingWays(id: string): Promise<OsmWay[]> {
const data = await Utils.downloadJsonCached(`${this.backend}api/0.6/${id}/ways`, 60 * 1000)
return data.elements.map((wayInfo) => new OsmWay(wayInfo.id, wayInfo))
}
/**
* Downloads the relations that are using this feature.
* Beware: their geometry will be incomplete!
*/
public async DownloadReferencingRelations(id: string): Promise<OsmRelation[]> {
const data = await Utils.downloadJsonCached(
`${this.backend}api/0.6/${id}/relations`,
60 * 1000
)
return data.elements.map((wayInfo) => {
const rel = new OsmRelation(wayInfo.id, wayInfo)
rel.SaveExtraData(wayInfo, undefined)
return rel
})
}
}

View file

@ -14,12 +14,14 @@ import { OsmObject } from "./Osm/OsmObject"
import { OsmTags } from "../Models/OsmFeature"
import { UIEventSource } from "./UIEventSource"
import LayoutConfig from "../Models/ThemeConfig/LayoutConfig"
import OsmObjectDownloader from "./Osm/OsmObjectDownloader"
/**
* All elements that are needed to perform metatagging
*/
export interface MetataggingState {
layout: LayoutConfig
osmObjectDownloader: OsmObjectDownloader
}
export abstract class SimpleMetaTagger {
@ -97,7 +99,7 @@ export class ReferencingWaysMetaTagger extends SimpleMetaTagger {
}
Utils.AddLazyPropertyAsync(feature.properties, "_referencing_ways", async () => {
const referencingWays = await OsmObject.DownloadReferencingWays(id)
const referencingWays = await state.osmObjectDownloader.DownloadReferencingWays(id)
const wayIds = referencingWays.map((w) => "way/" + w.id)
wayIds.sort()
return wayIds.join(";")