From ace7caada13fdc7699337fedff3da83207e99dd9 Mon Sep 17 00:00:00 2001 From: Pieter Vander Vennet Date: Thu, 1 Jun 2023 02:52:21 +0200 Subject: [PATCH] Refactoring: port import flow --- Docs/SpecialRenderings.md | 1 + Logic/ExtraFunctions.ts | 3 + .../FeatureSource/Actors/TileLocalStorage.ts | 38 ++-- Logic/FeatureSource/FeatureSource.ts | 5 - Logic/FeatureSource/Sources/LayoutSource.ts | 15 +- .../FeatureSource/Sources/OsmFeatureSource.ts | 25 ++- .../FullNodeDatabaseSource.ts | 59 ++++--- .../LocalStorageFeatureSource.ts | 23 ++- Logic/MetaTagging.ts | 93 +++++----- .../CreateMultiPolygonWithPointReuseAction.ts | 11 +- Logic/Osm/Actions/CreateNewWayAction.ts | 2 +- .../Actions/CreateWayWithPointReuseAction.ts | 4 +- Logic/Osm/Actions/OsmChangeAction.ts | 5 + Logic/Osm/Actions/ReplaceGeometryAction.ts | 36 ++-- Logic/Osm/Changes.ts | 37 ++-- Logic/Osm/ChangesetHandler.ts | 4 +- Logic/SimpleMetaTagger.ts | 27 +-- Logic/Tags/RegexTag.ts | 56 +++++- Logic/Tags/Tag.ts | 16 +- Logic/Web/IdbLocalStorage.ts | 2 +- Models/ThemeConfig/Conversion/PrepareLayer.ts | 104 +++++++---- Models/ThemeConfig/Json/LayerConfigJson.ts | 8 +- Models/ThemeConfig/LayerConfig.ts | 3 +- Models/ThemeConfig/WithContextLoader.ts | 2 +- Models/ThemeViewState.ts | 50 ++++-- Models/TileRange.ts | 10 ++ UI/Base/Table.ts | 3 + UI/Map/ShowDataLayer.ts | 3 + UI/Popup/AutoApplyButton.ts | 70 +++++--- UI/Popup/ImportButton.ts | 162 ------------------ .../ImportButtons/ConflateImportButtonViz.ts | 89 ++++++++++ .../ImportButtons/ConflateImportFlowState.ts | 83 +++++++++ UI/Popup/ImportButtons/ImportFlow.svelte | 76 ++++++-- UI/Popup/ImportButtons/ImportFlow.ts | 11 +- ...ntButtonViz.ts => PointImportButtonViz.ts} | 8 +- .../ImportButtons/PointImportFlowState.ts | 6 +- UI/Popup/ImportButtons/WayImportButtonViz.ts | 75 ++++---- UI/Popup/ImportButtons/WayImportFlow.svelte | 7 +- UI/Popup/ImportButtons/WayImportFlowState.ts | 15 +- UI/Popup/TagApplyButton.ts | 16 +- UI/SpecialVisualizations.ts | 7 +- UI/i18n/Translations.ts | 26 ++- Utils.ts | 25 +-- assets/themes/grb/grb.json | 59 ++++--- package-lock.json | 12 +- test.ts | 23 --- test/CodeQuality.spec.ts | 2 +- .../OSM/Actions/ReplaceGeometryAction.spec.ts | 9 +- 48 files changed, 852 insertions(+), 574 deletions(-) delete mode 100644 UI/Popup/ImportButton.ts create mode 100644 UI/Popup/ImportButtons/ConflateImportButtonViz.ts create mode 100644 UI/Popup/ImportButtons/ConflateImportFlowState.ts rename UI/Popup/ImportButtons/{ImportPointButtonViz.ts => PointImportButtonViz.ts} (92%) diff --git a/Docs/SpecialRenderings.md b/Docs/SpecialRenderings.md index 571a82529..f90e81f5d 100644 --- a/Docs/SpecialRenderings.md +++ b/Docs/SpecialRenderings.md @@ -1,3 +1,4 @@ + [//]: # (WARNING: this file is automatically generated. Please find the sources at the bottom and edit those sources) Special tag renderings diff --git a/Logic/ExtraFunctions.ts b/Logic/ExtraFunctions.ts index f032d430a..c88978762 100644 --- a/Logic/ExtraFunctions.ts +++ b/Logic/ExtraFunctions.ts @@ -414,6 +414,9 @@ class GetParsed implements ExtraFunction { if (value === undefined) { return undefined } + if(typeof value !== "string"){ + return value + } try { const parsed = JSON.parse(value) if (parsed === null) { diff --git a/Logic/FeatureSource/Actors/TileLocalStorage.ts b/Logic/FeatureSource/Actors/TileLocalStorage.ts index 897c2e852..b1f7cc6f6 100644 --- a/Logic/FeatureSource/Actors/TileLocalStorage.ts +++ b/Logic/FeatureSource/Actors/TileLocalStorage.ts @@ -1,5 +1,5 @@ -import { IdbLocalStorage } from "../../Web/IdbLocalStorage" -import { UIEventSource } from "../../UIEventSource" +import {IdbLocalStorage} from "../../Web/IdbLocalStorage" +import {UIEventSource} from "../../UIEventSource" /** * A class which allows to read/write a tile to local storage. @@ -10,23 +10,25 @@ import { UIEventSource } from "../../UIEventSource" */ export default class TileLocalStorage { private static perLayer: Record> = {} + private static readonly useIndexedDb = typeof indexedDB !== "undefined" private readonly _layername: string private readonly inUse = new UIEventSource(false) private readonly cachedSources: Record & { flush: () => void }> = {} + private readonly _maxAgeSeconds: number; - private static readonly useIndexedDb = typeof indexedDB !== "undefined" - private constructor(layername: string) { + private constructor(layername: string, maxAgeSeconds: number) { this._layername = layername + this._maxAgeSeconds = maxAgeSeconds; } - public static construct(backend: string, layername: string): TileLocalStorage { + public static construct(backend: string, layername: string, maxAgeS: number): TileLocalStorage { const key = backend + "_" + layername const cached = TileLocalStorage.perLayer[key] if (cached) { return cached } - const tls = new TileLocalStorage(key) + const tls = new TileLocalStorage(key, maxAgeS) TileLocalStorage.perLayer[key] = tls return tls } @@ -50,13 +52,15 @@ export default class TileLocalStorage { } private async SetIdb(tileIndex: number, data: any): Promise { - if(!TileLocalStorage.useIndexedDb){ + if (!TileLocalStorage.useIndexedDb) { return } try { await this.inUse.AsPromise((inUse) => !inUse) this.inUse.setData(true) await IdbLocalStorage.SetDirectly(this._layername + "_" + tileIndex, data) + await IdbLocalStorage.SetDirectly(this._layername + "_" + tileIndex + "_date", Date.now()) + this.inUse.setData(false) } catch (e) { console.error( @@ -72,10 +76,24 @@ export default class TileLocalStorage { } } - private GetIdb(tileIndex: number): Promise { - if(!TileLocalStorage.useIndexedDb){ + private async GetIdb(tileIndex: number): Promise { + if (!TileLocalStorage.useIndexedDb) { return undefined } - return IdbLocalStorage.GetDirectly(this._layername + "_" + tileIndex) + const date = await IdbLocalStorage.GetDirectly(this._layername + "_" + tileIndex + "_date") + const maxAge = this._maxAgeSeconds + const timeDiff = Date.now() - date + if (timeDiff >= maxAge) { + console.log("Dropping cache for", this._layername, tileIndex, "out of date") + await IdbLocalStorage.SetDirectly(this._layername + "_" + tileIndex, undefined) + + return undefined + } + const data = await IdbLocalStorage.GetDirectly(this._layername + "_" + tileIndex) + return data + } + + invalidate(zoomlevel: number, tileIndex) { + this.getTileSource(tileIndex).setData(undefined) } } diff --git a/Logic/FeatureSource/FeatureSource.ts b/Logic/FeatureSource/FeatureSource.ts index 62600deaa..2788a4b62 100644 --- a/Logic/FeatureSource/FeatureSource.ts +++ b/Logic/FeatureSource/FeatureSource.ts @@ -10,11 +10,6 @@ export interface WritableFeatureSource extends Feat features: UIEventSource } -export interface Tiled { - tileIndex: number - bbox: BBox -} - /** * A feature source which only contains features for the defined layer */ diff --git a/Logic/FeatureSource/Sources/LayoutSource.ts b/Logic/FeatureSource/Sources/LayoutSource.ts index 44f702556..de1c25c51 100644 --- a/Logic/FeatureSource/Sources/LayoutSource.ts +++ b/Logic/FeatureSource/Sources/LayoutSource.ts @@ -10,6 +10,7 @@ import FeatureSourceMerger from "./FeatureSourceMerger" import DynamicGeoJsonTileSource from "../TiledFeatureSource/DynamicGeoJsonTileSource" import {BBox} from "../../BBox" import LocalStorageFeatureSource from "../TiledFeatureSource/LocalStorageFeatureSource" +import FullNodeDatabaseSource from "../TiledFeatureSource/FullNodeDatabaseSource"; /** * This source will fetch the needed data from various sources for the given layout. @@ -27,7 +28,8 @@ export default class LayoutSource extends FeatureSourceMerger { featureSwitches: FeatureSwitchState, mapProperties: { bounds: Store; zoom: Store }, backend: string, - isDisplayed: (id: string) => Store + isDisplayed: (id: string) => Store, + fullNodeDatabaseSource?: FullNodeDatabaseSource ) { const { bounds, zoom } = mapProperties // remove all 'special' layers @@ -39,6 +41,7 @@ export default class LayoutSource extends FeatureSourceMerger { (l) => new LocalStorageFeatureSource(backend, l.id, 15, mapProperties, { isActive: isDisplayed(l.id), + maxAge: l.maxAgeOfCache }) ) @@ -55,7 +58,8 @@ export default class LayoutSource extends FeatureSourceMerger { bounds, zoom, backend, - featureSwitches + featureSwitches, + fullNodeDatabaseSource ) const geojsonSources: FeatureSource[] = geojsonlayers.map((l) => LayoutSource.setupGeojsonSource(l, mapProperties, isDisplayed(l.id)) @@ -96,7 +100,8 @@ export default class LayoutSource extends FeatureSourceMerger { bounds: Store, zoom: Store, backend: string, - featureSwitches: FeatureSwitchState + featureSwitches: FeatureSwitchState, + fullNodeDatabase: FullNodeDatabaseSource ): OsmFeatureSource | undefined { if (osmLayers.length == 0) { return undefined @@ -121,8 +126,8 @@ export default class LayoutSource extends FeatureSourceMerger { bounds, backend, isActive, - patchRelations: true - + patchRelations: true, + fullNodeDatabase }) } diff --git a/Logic/FeatureSource/Sources/OsmFeatureSource.ts b/Logic/FeatureSource/Sources/OsmFeatureSource.ts index b46b4a33b..8ae25dc80 100644 --- a/Logic/FeatureSource/Sources/OsmFeatureSource.ts +++ b/Logic/FeatureSource/Sources/OsmFeatureSource.ts @@ -7,6 +7,7 @@ import { TagsFilter } from "../../Tags/TagsFilter" import { Feature } from "geojson" import FeatureSourceMerger from "../Sources/FeatureSourceMerger" import OsmObjectDownloader from "../../Osm/OsmObjectDownloader" +import FullNodeDatabaseSource from "../TiledFeatureSource/FullNodeDatabaseSource"; /** * If a tile is needed (requested via the UIEventSource in the constructor), will download the appropriate tile and pass it via 'handleTile' @@ -16,9 +17,19 @@ export default class OsmFeatureSource extends FeatureSourceMerger { private readonly isActive: Store private readonly _backend: string private readonly allowedTags: TagsFilter + private options: { + bounds: Store + readonly allowedFeatures: TagsFilter + backend?: "https://openstreetmap.org/" | string + /** + * If given: this featureSwitch will not update if the store contains 'false' + */ + isActive?: Store, + patchRelations?: true | boolean, + fullNodeDatabase?: FullNodeDatabaseSource + }; public readonly isRunning: UIEventSource = new UIEventSource(false) - public rawDataHandlers: ((osmJson: any, tileIndex: number) => void)[] = [] private readonly _downloadedTiles: Set = new Set() private readonly _downloadedData: Feature[][] = [] @@ -35,9 +46,11 @@ export default class OsmFeatureSource extends FeatureSourceMerger { * If given: this featureSwitch will not update if the store contains 'false' */ isActive?: Store, - patchRelations?: true | boolean + patchRelations?: true | boolean, + fullNodeDatabase?: FullNodeDatabaseSource }) { super() + this.options = options; this._bounds = options.bounds this.allowedTags = options.allowedFeatures this.isActive = options.isActive ?? new ImmutableStore(true) @@ -119,7 +132,7 @@ export default class OsmFeatureSource extends FeatureSourceMerger { return feature } - private async LoadTile(z, x, y): Promise { + private async LoadTile(z: number, x: number, y: number): Promise { console.log("OsmFeatureSource: loading ", z, x, y, "from", this._backend) if (z >= 22) { throw "This is an absurd high zoom level" @@ -141,9 +154,7 @@ export default class OsmFeatureSource extends FeatureSourceMerger { try { const osmJson = await Utils.downloadJsonCached(url, 2000) try { - this.rawDataHandlers.forEach((handler) => - handler(osmJson, Tiles.tile_index(z, x, y)) - ) + this.options?.fullNodeDatabase?.handleOsmJson(osmJson, z, x, y) let features = []>OsmToGeoJson( osmJson, // @ts-ignore @@ -181,7 +192,7 @@ export default class OsmFeatureSource extends FeatureSourceMerger { y, "due to", e, - "; retrying with smaller bounds" + e === "rate limited" ? "; stopping now" : "; retrying with smaller bounds" ) if (e === "rate limited") { return diff --git a/Logic/FeatureSource/TiledFeatureSource/FullNodeDatabaseSource.ts b/Logic/FeatureSource/TiledFeatureSource/FullNodeDatabaseSource.ts index 883beb14c..4127a33d1 100644 --- a/Logic/FeatureSource/TiledFeatureSource/FullNodeDatabaseSource.ts +++ b/Logic/FeatureSource/TiledFeatureSource/FullNodeDatabaseSource.ts @@ -1,31 +1,25 @@ -import {FeatureSource, FeatureSourceForLayer, Tiled } from "../FeatureSource" -import { OsmNode, OsmObject, OsmWay } from "../../Osm/OsmObject" -import SimpleFeatureSource from "../Sources/SimpleFeatureSource" -import FilteredLayer from "../../../Models/FilteredLayer" -import { UIEventSource } from "../../UIEventSource" -import { OsmTags } from "../../../Models/OsmFeature"; -import { BBox } from "../../BBox"; -import { Feature, Point } from "geojson"; +import {OsmNode, OsmObject, OsmWay} from "../../Osm/OsmObject" +import {UIEventSource} from "../../UIEventSource" +import {BBox} from "../../BBox"; +import StaticFeatureSource from "../Sources/StaticFeatureSource"; +import {Tiles} from "../../../Models/TileRange"; export default class FullNodeDatabaseSource { - public readonly loadedTiles = new Map() - private readonly onTileLoaded: (tile: Tiled & FeatureSourceForLayer) => void - private readonly layer: FilteredLayer + + private readonly loadedTiles = new Map>() private readonly nodeByIds = new Map() private readonly parentWays = new Map>() - constructor(layer: FilteredLayer, onTileLoaded: (tile: Tiled & FeatureSourceForLayer) => void) { - this.onTileLoaded = onTileLoaded - this.layer = layer - if (this.layer === undefined) { - throw "Layer is undefined" - } - } + private smallestZoom = 99 + private largestZoom = 0 - public handleOsmJson(osmJson: any, tileId: number) { + public handleOsmJson(osmJson: any, z: number, x: number, y: number) : void { const allObjects = OsmObject.ParseObjects(osmJson.elements) const nodesById = new Map() + this.smallestZoom = Math.min(this.smallestZoom, z) + this.largestZoom = Math.max(this.largestZoom, z) + for (const osmObj of allObjects) { if (osmObj.type !== "node") { continue @@ -59,10 +53,9 @@ export default class FullNodeDatabaseSource { osmNode.asGeoJson() ) - const featureSource = new SimpleFeatureSource(this.layer, tileId) - featureSource.features.setData(asGeojsonFeatures) - this.loadedTiles.set(tileId, featureSource) - this.onTileLoaded(featureSource) + const featureSource = new StaticFeatureSource(asGeojsonFeatures) + const tileId = Tiles.tile_index(z, x, y) + this.loadedTiles.set(tileId, nodesById) } /** @@ -76,7 +69,7 @@ export default class FullNodeDatabaseSource { } /** - * Gets the parent way list + * Gets all the ways that the given node is a part of * @param nodeId * @constructor */ @@ -84,8 +77,20 @@ export default class FullNodeDatabaseSource { return this.parentWays.get(nodeId) } - getNodesWithin(bBox: BBox) : Feature[]{ - // TODO - throw "TODO" + /** + * Gets (at least) all nodes which are part of this BBOX; might also return some nodes that fall outside of the bbox but are closeby + * @param bbox + */ + getNodesWithin(bbox: BBox) : Map{ + const allById = new Map() + for (let z = this.smallestZoom; z < this.largestZoom; z++) { + const range = Tiles.tileRangeFrom(bbox, z) + Tiles.MapRange(range, (x, y ) => { + const tileId = Tiles.tile_index(z, x, y) + const nodesById = this.loadedTiles.get(tileId) + nodesById?.forEach((v,k) => allById.set(k,v)) + }) + } + return allById } } diff --git a/Logic/FeatureSource/TiledFeatureSource/LocalStorageFeatureSource.ts b/Logic/FeatureSource/TiledFeatureSource/LocalStorageFeatureSource.ts index d83db825e..d8b1dc575 100644 --- a/Logic/FeatureSource/TiledFeatureSource/LocalStorageFeatureSource.ts +++ b/Logic/FeatureSource/TiledFeatureSource/LocalStorageFeatureSource.ts @@ -1,9 +1,11 @@ import DynamicTileSource from "./DynamicTileSource" -import { Store } from "../../UIEventSource" -import { BBox } from "../../BBox" +import {Store} from "../../UIEventSource" +import {BBox} from "../../BBox" import TileLocalStorage from "../Actors/TileLocalStorage" -import { Feature } from "geojson" +import {Feature} from "geojson" import StaticFeatureSource from "../Sources/StaticFeatureSource" +import {constants} from "http2"; +import HTTP_STATUS_CONTINUE = module export default class LocalStorageFeatureSource extends DynamicTileSource { constructor( @@ -15,18 +17,25 @@ export default class LocalStorageFeatureSource extends DynamicTileSource { zoom: Store }, options?: { - isActive?: Store + isActive?: Store, + maxAge?: number // In seconds } ) { - const storage = TileLocalStorage.construct(backend, layername) + const storage = TileLocalStorage.construct(backend, layername, options?.maxAge ?? 24 * 60 * 60) super( zoomlevel, (tileIndex) => new StaticFeatureSource( storage .getTileSource(tileIndex) - .map((features) => - features?.filter((f) => !f.properties.id.match(/(node|way)\/-[0-9]+/)) + .mapD((features) => { + if (features.length === undefined) { + console.trace("These are not features:", features) + storage.invalidate(zoomlevel, tileIndex) + return [] + } + return features.filter((f) => !f.properties.id.match(/(node|way)\/-[0-9]+/)); + } ) ), mapProperties, diff --git a/Logic/MetaTagging.ts b/Logic/MetaTagging.ts index efa60e53d..1e6351f44 100644 --- a/Logic/MetaTagging.ts +++ b/Logic/MetaTagging.ts @@ -9,6 +9,7 @@ import {IndexedFeatureSource} from "./FeatureSource/FeatureSource" import OsmObjectDownloader from "./Osm/OsmObjectDownloader" import {Utils} from "../Utils"; import {GeoJSONFeature} from "maplibre-gl"; +import {UIEventSource} from "./UIEventSource"; /** * Metatagging adds various tags to the elements, e.g. lat, lon, surface area, ... @@ -18,7 +19,7 @@ import {GeoJSONFeature} from "maplibre-gl"; export default class MetaTagging { private static errorPrintCount = 0 private static readonly stopErrorOutputAt = 10 - private static retaggingFuncCache = new Map void)[]>() + private static retaggingFuncCache = new Map) => void)[]>() constructor(state: { layout: LayoutConfig @@ -27,19 +28,7 @@ export default class MetaTagging { indexedFeatures: IndexedFeatureSource featureProperties: FeaturePropertiesStore }) { - const params: ExtraFuncParams = { - getFeatureById: (id) => state.indexedFeatures.featuresById.data.get(id), - getFeaturesWithin: (layerId, bbox) => { - if(layerId === '*' || layerId === null || layerId === undefined){ - const feats: Feature[][] = [] - state.perLayer.forEach((layer) => { - feats.push(layer.GetFeaturesWithin(bbox)) - }) - return feats - } - return [state.perLayer.get(layerId).GetFeaturesWithin(bbox)]; - }, - } + const params = MetaTagging.createExtraFuncParams(state) for (const layer of state.layout.layers) { if (layer.source === null) { continue @@ -108,7 +97,7 @@ export default class MetaTagging { // The calculated functions - per layer - which add the new keys // Calculated functions are defined by the layer const layerFuncs = this.createRetaggingFunc(layer, ExtraFunctions.constructHelpers(params)) - const state: MetataggingState = { layout, osmObjectDownloader } + const state: MetataggingState = {layout, osmObjectDownloader} let atLeastOneFeatureChanged = false let strictlyEvaluated = 0 @@ -117,15 +106,7 @@ export default class MetaTagging { const tags = featurePropertiesStores?.getStore(feature.properties.id) let somethingChanged = false let definedTags = new Set(Object.getOwnPropertyNames(feature.properties)) - if (layerFuncs !== undefined) { - let retaggingChanged = false - try { - retaggingChanged = layerFuncs(feature) - } catch (e) { - console.error(e) - } - somethingChanged = somethingChanged || retaggingChanged - } + for (const metatag of metatagsToApply) { try { if (!metatag.keys.some((key) => !(key in feature.properties))) { @@ -144,7 +125,7 @@ export default class MetaTagging { if (options?.evaluateStrict) { for (const key of metatag.keys) { const evaluated = feature.properties[key] - if(evaluated !== undefined){ + if (evaluated !== undefined) { strictlyEvaluated++ } } @@ -175,12 +156,18 @@ export default class MetaTagging { ) } } - - + if (layerFuncs !== undefined) { + try { + // We cannot do `somethingChanged || layerFuncs(feature)', due to the shortcutting behaviour it would not calculate the lazy functions + somethingChanged = layerFuncs(feature, tags) || somethingChanged + } catch (e) { + console.error(e) + } + } if (somethingChanged) { try { - featurePropertiesStores?.getStore(feature.properties.id)?.ping() + tags?.ping() } catch (e) { console.error("Could not ping a store for a changed property due to", e) } @@ -190,6 +177,25 @@ export default class MetaTagging { return atLeastOneFeatureChanged } + public static createExtraFuncParams(state: { + indexedFeatures: IndexedFeatureSource, + perLayer: ReadonlyMap + }) { + return { + getFeatureById: (id) => state.indexedFeatures.featuresById.data.get(id), + getFeaturesWithin: (layerId, bbox) => { + if (layerId === '*' || layerId === null || layerId === undefined) { + const feats: Feature[][] = [] + state.perLayer.forEach((layer) => { + feats.push(layer.GetFeaturesWithin(bbox)) + }) + return feats + } + return [state.perLayer.get(layerId).GetFeaturesWithin(bbox)]; + }, + } + } + /** * Creates a function that implements that calculates a property and adds this property onto the feature properties * @param specification @@ -197,28 +203,28 @@ export default class MetaTagging { * @param layerId * @private */ - private static createFunctionForFeature( [key, code, isStrict]: [string, string, boolean], + private static createFunctionForFeature([key, code, isStrict]: [string, string, boolean], helperFunctions: Record Function>, layerId: string = "unkown layer" - ): ((feature: GeoJSONFeature) => void) | undefined { + ): ((feature: GeoJSONFeature, propertiesStore?: UIEventSource) => void) | undefined { if (code === undefined) { return undefined } - - const calculateAndAssign: ((feat: GeoJSONFeature) => (string | undefined)) = (feat) => { + const calculateAndAssign: ((feat: GeoJSONFeature, store?: UIEventSource) => string | any) = (feat, store) => { try { - let result = new Function("feat", "{"+ExtraFunctions.types.join(", ")+"}", "return " + code + ";")(feat, helperFunctions) + let result = new Function("feat", "{" + ExtraFunctions.types.join(", ") + "}", "return " + code + ";")(feat, helperFunctions) if (result === "") { result = undefined } - if (result !== undefined && typeof result !== "string") { - // Make sure it is a string! - result = JSON.stringify(result) + const oldValue= feat.properties[key] + if(oldValue == result){ + return oldValue } delete feat.properties[key] feat.properties[key] = result + store?.ping() return result } catch (e) { if (MetaTagging.errorPrintCount < MetaTagging.stopErrorOutputAt) { @@ -250,13 +256,12 @@ export default class MetaTagging { } } - if(isStrict){ + if (isStrict) { return calculateAndAssign } - return (feature: any) => { + return (feature: any, store?: UIEventSource) => { delete feature.properties[key] - Utils.AddLazyProperty(feature.properties, key, () => calculateAndAssign(feature)) - return undefined + Utils.AddLazyProperty(feature.properties, key, () => calculateAndAssign(feature, store)) } } @@ -266,19 +271,19 @@ export default class MetaTagging { private static createRetaggingFunc( layer: LayerConfig, helpers: Record Function> - ): (feature: any) => boolean { + ): (feature: Feature, tags: UIEventSource>) => boolean { const calculatedTags: [string, string, boolean][] = layer.calculatedTags if (calculatedTags === undefined || calculatedTags.length === 0) { return undefined } - let functions: ((feature: Feature) => void)[] = MetaTagging.retaggingFuncCache.get(layer.id) + let functions: ((feature: Feature, propertiesStore?: UIEventSource) => void)[] = MetaTagging.retaggingFuncCache.get(layer.id) if (functions === undefined) { functions = calculatedTags.map(spec => this.createFunctionForFeature(spec, helpers, layer.id)) MetaTagging.retaggingFuncCache.set(layer.id, functions) } - return (feature: Feature) => { + return (feature: Feature, store: UIEventSource>) => { const tags = feature.properties if (tags === undefined) { return @@ -286,7 +291,7 @@ export default class MetaTagging { try { for (const f of functions) { - f(feature) + f(feature, store) } } catch (e) { console.error("Invalid syntax in calculated tags or some other error: ", e) diff --git a/Logic/Osm/Actions/CreateMultiPolygonWithPointReuseAction.ts b/Logic/Osm/Actions/CreateMultiPolygonWithPointReuseAction.ts index 86d5cd0e7..1cf3dd369 100644 --- a/Logic/Osm/Actions/CreateMultiPolygonWithPointReuseAction.ts +++ b/Logic/Osm/Actions/CreateMultiPolygonWithPointReuseAction.ts @@ -1,4 +1,4 @@ -import {OsmCreateAction} from "./OsmChangeAction" +import {OsmCreateAction, PreviewableAction} from "./OsmChangeAction" import {Tag} from "../../Tags/Tag" import {Changes} from "../Changes" import {ChangeDescription} from "./ChangeDescription" @@ -6,7 +6,7 @@ import CreateNewWayAction from "./CreateNewWayAction" import CreateWayWithPointReuseAction, {MergePointConfig} from "./CreateWayWithPointReuseAction" import {And} from "../../Tags/And" import {TagUtils} from "../../Tags/TagUtils" -import {IndexedFeatureSource} from "../../FeatureSource/FeatureSource" +import {FeatureSource, IndexedFeatureSource} from "../../FeatureSource/FeatureSource" import LayoutConfig from "../../../Models/ThemeConfig/LayoutConfig"; import {Position} from "geojson"; import FullNodeDatabaseSource from "../../FeatureSource/TiledFeatureSource/FullNodeDatabaseSource"; @@ -14,7 +14,7 @@ import FullNodeDatabaseSource from "../../FeatureSource/TiledFeatureSource/FullN /** * More or less the same as 'CreateNewWay', except that it'll try to reuse already existing points */ -export default class CreateMultiPolygonWithPointReuseAction extends OsmCreateAction { +export default class CreateMultiPolygonWithPointReuseAction extends OsmCreateAction implements PreviewableAction { public newElementId: string = undefined public newElementIdNumber: number = undefined private readonly _tags: Tag[] @@ -67,7 +67,6 @@ export default class CreateMultiPolygonWithPointReuseAction extends OsmCreateAct } protected async CreateChangeDescriptions(changes: Changes): Promise { - console.log("Running CMPWPRA") const descriptions: ChangeDescription[] = [] descriptions.push(...(await this.createOuterWay.CreateChangeDescriptions(changes))) for (const innerWay of this.createInnerWays) { @@ -103,4 +102,8 @@ export default class CreateMultiPolygonWithPointReuseAction extends OsmCreateAct return descriptions } + + getPreview(): Promise { + return undefined + } } diff --git a/Logic/Osm/Actions/CreateNewWayAction.ts b/Logic/Osm/Actions/CreateNewWayAction.ts index 8b44a0c2c..2cf638bc6 100644 --- a/Logic/Osm/Actions/CreateNewWayAction.ts +++ b/Logic/Osm/Actions/CreateNewWayAction.ts @@ -1,5 +1,5 @@ import { ChangeDescription } from "./ChangeDescription" -import { OsmCreateAction } from "./OsmChangeAction" +import {OsmCreateAction, PreviewableAction} from "./OsmChangeAction" import { Changes } from "../Changes" import { Tag } from "../../Tags/Tag" import CreateNewNodeAction from "./CreateNewNodeAction" diff --git a/Logic/Osm/Actions/CreateWayWithPointReuseAction.ts b/Logic/Osm/Actions/CreateWayWithPointReuseAction.ts index 15a483697..a4a60039d 100644 --- a/Logic/Osm/Actions/CreateWayWithPointReuseAction.ts +++ b/Logic/Osm/Actions/CreateWayWithPointReuseAction.ts @@ -1,4 +1,4 @@ -import {OsmCreateAction} from "./OsmChangeAction" +import {OsmCreateAction, PreviewableAction} from "./OsmChangeAction" import {Tag} from "../../Tags/Tag" import {Changes} from "../Changes" import {ChangeDescription} from "./ChangeDescription" @@ -56,7 +56,7 @@ interface CoordinateInfo { /** * More or less the same as 'CreateNewWay', except that it'll try to reuse already existing points */ -export default class CreateWayWithPointReuseAction extends OsmCreateAction { +export default class CreateWayWithPointReuseAction extends OsmCreateAction implements PreviewableAction { public newElementId: string = undefined public newElementIdNumber: number = undefined private readonly _tags: Tag[] diff --git a/Logic/Osm/Actions/OsmChangeAction.ts b/Logic/Osm/Actions/OsmChangeAction.ts index 335d9a14c..b6043a886 100644 --- a/Logic/Osm/Actions/OsmChangeAction.ts +++ b/Logic/Osm/Actions/OsmChangeAction.ts @@ -4,6 +4,7 @@ */ import { Changes } from "../Changes" import { ChangeDescription } from "./ChangeDescription" +import {FeatureSource} from "../../FeatureSource/FeatureSource"; export default abstract class OsmChangeAction { public readonly trackStatistics: boolean @@ -35,3 +36,7 @@ export abstract class OsmCreateAction extends OsmChangeAction { public newElementId: string public newElementIdNumber: number } + +export interface PreviewableAction { + getPreview(): Promise +} diff --git a/Logic/Osm/Actions/ReplaceGeometryAction.ts b/Logic/Osm/Actions/ReplaceGeometryAction.ts index 9efe482b3..70afff79e 100644 --- a/Logic/Osm/Actions/ReplaceGeometryAction.ts +++ b/Logic/Osm/Actions/ReplaceGeometryAction.ts @@ -1,21 +1,21 @@ -import OsmChangeAction from "./OsmChangeAction" -import { Changes } from "../Changes" -import { ChangeDescription } from "./ChangeDescription" -import { Tag } from "../../Tags/Tag" -import { FeatureSource } from "../../FeatureSource/FeatureSource" -import { OsmNode, OsmObject, OsmWay } from "../OsmObject" -import { GeoOperations } from "../../GeoOperations" +import OsmChangeAction, {PreviewableAction} from "./OsmChangeAction" +import {Changes} from "../Changes" +import {ChangeDescription} from "./ChangeDescription" +import {Tag} from "../../Tags/Tag" +import {FeatureSource} from "../../FeatureSource/FeatureSource" +import {OsmNode, OsmObject, OsmWay} from "../OsmObject" +import {GeoOperations} from "../../GeoOperations" import StaticFeatureSource from "../../FeatureSource/Sources/StaticFeatureSource" import CreateNewNodeAction from "./CreateNewNodeAction" import ChangeTagAction from "./ChangeTagAction" -import { And } from "../../Tags/And" -import { Utils } from "../../../Utils" -import { OsmConnection } from "../OsmConnection" -import { Feature } from "@turf/turf" -import { Geometry, LineString, Point } from "geojson" +import {And} from "../../Tags/And" +import {Utils} from "../../../Utils" +import {OsmConnection} from "../OsmConnection" +import {Feature} from "@turf/turf" +import {Geometry, LineString, Point} from "geojson" import FullNodeDatabaseSource from "../../FeatureSource/TiledFeatureSource/FullNodeDatabaseSource" -export default class ReplaceGeometryAction extends OsmChangeAction { +export default class ReplaceGeometryAction extends OsmChangeAction implements PreviewableAction{ /** * The target feature - mostly used for the metadata */ @@ -38,9 +38,14 @@ export default class ReplaceGeometryAction extends OsmChangeAction { private readonly identicalTo: number[] private readonly newTags: Tag[] | undefined + /** + * Not really the 'new' element, but the target that has been applied. + * Added for compatibility with other systems + */ + public readonly newElementId: string constructor( state: { - osmConnection: OsmConnection + osmConnection: OsmConnection, fullNodeDatabase?: FullNodeDatabaseSource }, feature: any, @@ -55,6 +60,7 @@ export default class ReplaceGeometryAction extends OsmChangeAction { this.feature = feature this.wayToReplaceId = wayToReplaceId this.theme = options.theme + this.newElementId = wayToReplaceId const geom = this.feature.geometry let coordinates: [number, number][] @@ -81,7 +87,6 @@ export default class ReplaceGeometryAction extends OsmChangeAction { this.newTags = options.newTags } - // noinspection JSUnusedGlobalSymbols public async getPreview(): Promise { const { closestIds, allNodesById, detachedNodes, reprojectedNodes } = await this.GetClosestIds() @@ -455,6 +460,7 @@ export default class ReplaceGeometryAction extends OsmChangeAction { } } + console.log("Adding tags", this.newTags,"to conflated way nr", this.wayToReplaceId) if (this.newTags !== undefined && this.newTags.length > 0) { const addExtraTags = new ChangeTagAction( this.wayToReplaceId, diff --git a/Logic/Osm/Changes.ts b/Logic/Osm/Changes.ts index b6dc34d9a..8ab80f151 100644 --- a/Logic/Osm/Changes.ts +++ b/Logic/Osm/Changes.ts @@ -1,16 +1,16 @@ -import { OsmNode, OsmObject, OsmRelation, OsmWay } from "./OsmObject" -import { Store, UIEventSource } from "../UIEventSource" +import {OsmNode, OsmObject, OsmRelation, OsmWay} from "./OsmObject" +import {Store, UIEventSource} from "../UIEventSource" import Constants from "../../Models/Constants" import OsmChangeAction from "./Actions/OsmChangeAction" -import { ChangeDescription, ChangeDescriptionTools } from "./Actions/ChangeDescription" -import { Utils } from "../../Utils" -import { LocalStorageSource } from "../Web/LocalStorageSource" +import {ChangeDescription, ChangeDescriptionTools} from "./Actions/ChangeDescription" +import {Utils} from "../../Utils" +import {LocalStorageSource} from "../Web/LocalStorageSource" import SimpleMetaTagger from "../SimpleMetaTagger" -import { FeatureSource, IndexedFeatureSource } from "../FeatureSource/FeatureSource" -import { GeoLocationPointProperties } from "../State/GeoLocationState" -import { GeoOperations } from "../GeoOperations" -import { ChangesetHandler, ChangesetTag } from "./ChangesetHandler" -import { OsmConnection } from "./OsmConnection" +import {FeatureSource, IndexedFeatureSource} from "../FeatureSource/FeatureSource" +import {GeoLocationPointProperties} from "../State/GeoLocationState" +import {GeoOperations} from "../GeoOperations" +import {ChangesetHandler, ChangesetTag} from "./ChangesetHandler" +import {OsmConnection} from "./OsmConnection" import FeaturePropertiesStore from "../FeatureSource/Actors/FeaturePropertiesStore" import OsmObjectDownloader from "./OsmObjectDownloader" @@ -25,9 +25,9 @@ export class Changes { public readonly state: { allElements?: IndexedFeatureSource; osmConnection: OsmConnection } public readonly extraComment: UIEventSource = new UIEventSource(undefined) public readonly backend: string + public readonly isUploading = new UIEventSource(false) private readonly historicalUserLocations?: FeatureSource private _nextId: number = -1 // Newly assigned ID's are negative - public readonly isUploading = new UIEventSource(false) private readonly previouslyCreated: OsmObject[] = [] private readonly _leftRightSensitive: boolean private readonly _changesetHandler: ChangesetHandler @@ -246,11 +246,12 @@ export class Changes { switch (change.type) { case "node": // @ts-ignore - const nlat = change.changes.lat + const nlat = Utils.Round7(change.changes.lat) // @ts-ignore - const nlon = change.changes.lon + const nlon = Utils.Round7(change.changes.lon) const n = obj if (n.lat !== nlat || n.lon !== nlon) { + console.log("Node moved:", n.lat, nlat, n.lon, nlon) n.lat = nlat n.lon = nlon changed = true @@ -407,7 +408,7 @@ export class Changes { neededIds.map(async (id) => { try { const osmObj = await downloader.DownloadObjectAsync(id) - return { id, osmObj } + return {id, osmObj} } catch (e) { console.error( "Could not download OSM-object", @@ -421,7 +422,7 @@ export class Changes { osmObjects = Utils.NoNull(osmObjects) - for (const { osmObj, id } of osmObjects) { + for (const {osmObj, id} of osmObjects) { if (osmObj === "deleted") { pending = pending.filter((ch) => ch.type + "/" + ch.id !== id) } @@ -572,9 +573,9 @@ export class Changes { ) console.log( "Using current-open-changeset-" + - theme + - " from the preferences, got " + - openChangeset.data + theme + + " from the preferences, got " + + openChangeset.data ) return await self.flushSelectChanges(pendingChanges, openChangeset) diff --git a/Logic/Osm/ChangesetHandler.ts b/Logic/Osm/ChangesetHandler.ts index 4641567f4..01c2da21c 100644 --- a/Logic/Osm/ChangesetHandler.ts +++ b/Logic/Osm/ChangesetHandler.ts @@ -129,9 +129,9 @@ export class ChangesetHandler { const csId = await this.OpenChangeset(extraMetaTags) openChangeset.setData(csId) const changeset = generateChangeXML(csId, this._remappings) - console.trace( + console.log( "Opened a new changeset (openChangeset.data is undefined):", - changeset + changeset, extraMetaTags ) const changes = await this.UploadChange(csId, changeset) const hasSpecialMotivationChanges = ChangesetHandler.rewriteMetaTags( diff --git a/Logic/SimpleMetaTagger.ts b/Logic/SimpleMetaTagger.ts index 7c177416d..cd3e9b397 100644 --- a/Logic/SimpleMetaTagger.ts +++ b/Logic/SimpleMetaTagger.ts @@ -306,22 +306,26 @@ export default class SimpleMetaTaggers { ) private static surfaceArea = new InlineMetaTagger( { - keys: ["_surface", "_surface:ha"], - doc: "The surface area of the feature, in square meters and in hectare. Not set on points and ways", + keys: ["_surface"], + doc: "The surface area of the feature in square meters. Not set on points and ways", isLazy: true, }, (feature) => { - Object.defineProperty(feature.properties, "_surface", { - enumerable: false, - configurable: true, - get: () => { - const sqMeters = "" + GeoOperations.surfaceAreaInSqMeters(feature) - delete feature.properties["_surface"] - feature.properties["_surface"] = sqMeters - return sqMeters - }, + Utils.AddLazyProperty(feature.properties, "_surface", () => { + return "" + GeoOperations.surfaceAreaInSqMeters(feature) + }) + return true + } + ) + private static surfaceAreaHa = new InlineMetaTagger( + { + keys: ["_surface:ha"], + doc: "The surface area of the feature in hectare. Not set on points and ways", + isLazy: true, + }, + (feature) => { Utils.AddLazyProperty(feature.properties, "_surface:ha", () => { const sqMeters = GeoOperations.surfaceAreaInSqMeters(feature) return "" + Math.floor(sqMeters / 1000) / 10 @@ -581,6 +585,7 @@ export default class SimpleMetaTaggers { SimpleMetaTaggers.latlon, SimpleMetaTaggers.layerInfo, SimpleMetaTaggers.surfaceArea, + SimpleMetaTaggers.surfaceAreaHa, SimpleMetaTaggers.lngth, SimpleMetaTaggers.canonicalize, SimpleMetaTaggers.country, diff --git a/Logic/Tags/RegexTag.ts b/Logic/Tags/RegexTag.ts index 0e1f4c658..805438c81 100644 --- a/Logic/Tags/RegexTag.ts +++ b/Logic/Tags/RegexTag.ts @@ -1,5 +1,5 @@ -import { Tag } from "./Tag" -import { TagsFilter } from "./TagsFilter" +import {Tag} from "./Tag" +import {TagsFilter} from "./TagsFilter" export class RegexTag extends TagsFilter { public readonly key: RegExp | string @@ -15,7 +15,20 @@ export class RegexTag extends TagsFilter { this.matchesEmpty = RegexTag.doesMatch("", this.value) } - private static doesMatch(fromTag: string, possibleRegex: string | RegExp): boolean { + /** + * + * Checks that the value provided by the object properties (`fromTag`) match the specified regex `possibleRegex + * + * RegexTag.doesMatch("abc", /abc/) // => true + * RegexTag.doesMatch("ab", /abc/) // => false + * RegexTag.doesMatch("", /.+/) // => false + * RegexTag.doesMatch("", new RegExp(".*")) // => true + * + * @param fromTag + * @param possibleRegex + * @private + */ + private static doesMatch(fromTag: string | number, possibleRegex: string | RegExp): boolean { if (fromTag === undefined) { return } @@ -25,11 +38,8 @@ export class RegexTag extends TagsFilter { if (typeof possibleRegex === "string") { return fromTag === possibleRegex } - if (typeof fromTag.match !== "function") { - console.error("Error: fromTag is not a regex: ", fromTag, possibleRegex) - throw "Error: fromTag is not a regex: " + fromTag + possibleRegex - } - return fromTag.match(possibleRegex) !== null + return possibleRegex.test(fromTag) + } private static source(r: string | RegExp) { @@ -125,10 +135,38 @@ export class RegexTag extends TagsFilter { * * new RegexTag("key","value").matchesProperties({"otherkey":"value"}) // => false * new RegexTag("key","value",true).matchesProperties({"otherkey":"something"}) // => true + * + * const v: string = {someJson: ""} + * new RegexTag("key", new RegExp(".*")).matchesProperties({"key": v}) // => true + * new RegexTag("key", new RegExp(".*")).matchesProperties({"key": ""}) // => true + * new RegexTag("key", new RegExp(".*")).matchesProperties({"key": null}) // => true + * new RegexTag("key", new RegExp(".*")).matchesProperties({"key": undefined}) // => true + * + * const v: string = {someJson: ""} + * new RegexTag("key", new RegExp(".+")).matchesProperties({"key": null}) // => false + * new RegexTag("key", new RegExp(".+")).matchesProperties({"key": undefined}) // => false + * new RegexTag("key", new RegExp(".+")).matchesProperties({"key": v}) // => false + * new RegexTag("key", new RegExp(".+")).matchesProperties({"key": ""}) // => false */ matchesProperties(tags: Record): boolean { if (typeof this.key === "string") { - const value = tags[this.key] ?? "" + const value = tags[this.key] + if(!value || value === ""){ + // No tag is known, so we assume the empty string + // If this regexTag matches the empty string, we return true, otherwise false + // (Note: if inverted, we must reverse this) + return this.invert !== this.matchesEmpty + } + if(typeof value !== "string"){ + if(typeof this.value !== "string"){ + const regExp = this.value + if(regExp.source === ".*"){ + // We match anything, and we do have a value + return !this.invert + } + return RegexTag.doesMatch(value, JSON.stringify(this.value)) != this.invert + } + } return RegexTag.doesMatch(value, this.value) != this.invert } diff --git a/Logic/Tags/Tag.ts b/Logic/Tags/Tag.ts index 4337e8b45..00d0fdb84 100644 --- a/Logic/Tags/Tag.ts +++ b/Logic/Tags/Tag.ts @@ -35,15 +35,27 @@ export class Tag extends TagsFilter { * isEmpty.matchesProperties({"other_key": "value"}) // => true * isEmpty.matchesProperties({"key": undefined}) // => true * + * const isTrue = new Tag("key", "true") + * isTrue.matchesProperties({"key","true"}) // => true + * isTrue.matchesProperteis({"key", true}) // => true */ matchesProperties(properties: Record): boolean { - const foundValue = properties[this.key] + let foundValue = properties[this.key] + if (foundValue === undefined && (this.value === "" || this.value === undefined)) { // The tag was not found // and it shouldn't be found! return true } - + if(typeof foundValue !== "string"){ + if(foundValue === true && (this.value === "true" || this.value === "yes" )){ + return true + } + if(foundValue === false && (this.value === "false" || this.value === "no" )){ + return true + } + foundValue = ""+foundValue + } return foundValue === this.value } diff --git a/Logic/Web/IdbLocalStorage.ts b/Logic/Web/IdbLocalStorage.ts index 81ce2b6eb..ec417a1f8 100644 --- a/Logic/Web/IdbLocalStorage.ts +++ b/Logic/Web/IdbLocalStorage.ts @@ -43,7 +43,7 @@ export class IdbLocalStorage { return idb.set(key, copy) } - static GetDirectly(key: string): Promise { + static GetDirectly(key: string): Promise { return idb.get(key) } } diff --git a/Models/ThemeConfig/Conversion/PrepareLayer.ts b/Models/ThemeConfig/Conversion/PrepareLayer.ts index b5883e891..611dabce6 100644 --- a/Models/ThemeConfig/Conversion/PrepareLayer.ts +++ b/Models/ThemeConfig/Conversion/PrepareLayer.ts @@ -14,8 +14,8 @@ import {TagConfigJson} from "../Json/TagConfigJson" import PointRenderingConfigJson from "../Json/PointRenderingConfigJson" import LineRenderingConfigJson from "../Json/LineRenderingConfigJson" import ValidationUtils from "./ValidationUtils" -import { RenderingSpecification } from "../../../UI/SpecialVisualization" -import { QuestionableTagRenderingConfigJson } from "../Json/QuestionableTagRenderingConfigJson" +import {RenderingSpecification} from "../../../UI/SpecialVisualization" +import {QuestionableTagRenderingConfigJson} from "../Json/QuestionableTagRenderingConfigJson" class ExpandFilter extends DesugaringStep { private static readonly predefinedFilters = ExpandFilter.load_filters() @@ -201,7 +201,7 @@ class ExpandTagRendering extends Conversion< if (state.tagRenderings.has(name)) { return [state.tagRenderings.get(name)] } - if(this._tagRenderingsByLabel.has(name)){ + if (this._tagRenderingsByLabel.has(name)) { return this._tagRenderingsByLabel.get(name) } @@ -437,11 +437,11 @@ class DetectInline extends DesugaringStep { information?: string[] } { if (json.freeform === undefined) { - return { result: json } + return {result: json} } let spec: Record if (typeof json.render === "string") { - spec = { "*": json.render } + spec = {"*": json.render} } else { spec = >json.render } @@ -450,7 +450,7 @@ class DetectInline extends DesugaringStep { if (spec[key].indexOf("= 0) { // We have a link element, it probably contains something that needs to be substituted... // Let's play this safe and not inline it - return { result: json } + return {result: json} } const fullSpecification = SpecialVisualizations.constructSpecification(spec[key]) if (fullSpecification.length > 1) { @@ -458,19 +458,19 @@ class DetectInline extends DesugaringStep { if (json.freeform.inline === true) { errors.push( "At " + - context + - ": 'inline' is set, but the rendering contains a special visualisation...\n " + - spec[key] + context + + ": 'inline' is set, but the rendering contains a special visualisation...\n " + + spec[key] ) } json = JSON.parse(JSON.stringify(json)) json.freeform.inline = false - return { result: json, errors } + return {result: json, errors} } } json = JSON.parse(JSON.stringify(json)) json.freeform.inline ??= true - return { result: json, errors } + return {result: json, errors} } } @@ -491,7 +491,7 @@ export class AddQuestionBox extends DesugaringStep { json.tagRenderings === undefined || json.tagRenderings.some((tr) => tr["id"] === "leftover-questions") ) { - return { result: json } + return {result: json} } json = JSON.parse(JSON.stringify(json)) const allSpecials: Exclude[] = [] @@ -512,8 +512,8 @@ export class AddQuestionBox extends DesugaringStep { if (noLabels.length > 1) { errors.push( "At " + - context + - ": multiple 'questions'-visualisations found which would show _all_ questions. Don't do this" + context + + ": multiple 'questions'-visualisations found which would show _all_ questions. Don't do this" ) } @@ -537,24 +537,24 @@ export class AddQuestionBox extends DesugaringStep { if (blacklisted?.length > 0 && used?.length > 0) { errors.push( "At " + - context + - ": the {questions()}-special rendering only supports either a blacklist OR a whitelist, but not both." + - "\n Whitelisted: " + - used.join(", ") + - "\n Blacklisted: " + - blacklisted.join(", ") + context + + ": the {questions()}-special rendering only supports either a blacklist OR a whitelist, but not both." + + "\n Whitelisted: " + + used.join(", ") + + "\n Blacklisted: " + + blacklisted.join(", ") ) } for (const usedLabel of used) { if (!allLabels.has(usedLabel)) { errors.push( "At " + - context + - ": this layers specifies a special question element for label `" + - usedLabel + - "`, but this label doesn't exist.\n" + - " Available labels are " + - Array.from(allLabels).join(", ") + context + + ": this layers specifies a special question element for label `" + + usedLabel + + "`, but this label doesn't exist.\n" + + " Available labels are " + + Array.from(allLabels).join(", ") ) } seen.add(usedLabel) @@ -583,6 +583,7 @@ export class AddQuestionBox extends DesugaringStep { export class AddEditingElements extends DesugaringStep { private readonly _desugaring: DesugaringContext + constructor(desugaring: DesugaringContext) { super( "Add some editing elements, such as the delete button or the move button if they are configured. These used to be handled by the feature info box, but this has been replaced by special visualisation elements", @@ -609,7 +610,7 @@ export class AddEditingElements extends DesugaringStep { if (json.allowSplit && !ValidationUtils.hasSpecialVisualisation(json, "split_button")) { json.tagRenderings.push({ id: "split-button", - render: { "*": "{split_button()}" }, + render: {"*": "{split_button()}"}, }) delete json.allowSplit } @@ -617,13 +618,13 @@ export class AddEditingElements extends DesugaringStep { if (json.allowMove && !ValidationUtils.hasSpecialVisualisation(json, "move_button")) { json.tagRenderings.push({ id: "move-button", - render: { "*": "{move_button()}" }, + render: {"*": "{move_button()}"}, }) } if (json.deletion && !ValidationUtils.hasSpecialVisualisation(json, "delete_button")) { json.tagRenderings.push({ id: "delete-button", - render: { "*": "{delete_button()}" }, + render: {"*": "{delete_button()}"}, }) } @@ -640,7 +641,7 @@ export class AddEditingElements extends DesugaringStep { if (!ValidationUtils.hasSpecialVisualisation(json, "all_tags")) { const trc: TagRenderingConfigJson = { id: "all-tags", - render: { "*": "{all_tags()}" }, + render: {"*": "{all_tags()}"}, metacondition: { or: [ @@ -653,7 +654,7 @@ export class AddEditingElements extends DesugaringStep { json.tagRenderings?.push(trc) } - return { result: json } + return {result: json} } } @@ -1149,6 +1150,36 @@ class PreparePointRendering extends Fuse { + constructor() { + super("sets the fullNodeDatabase-bit if needed", + ["fullNodeDatabase"], + "SetFullNodeDatabase") + } + + convert(json: LayerConfigJson, context: string): { + result: LayerConfigJson; + errors?: string[]; + warnings?: string[]; + information?: string[] + } { + const needsSpecial = json.tagRenderings?.some(tr => { + if (typeof tr === "string") { + return false + } + const specs = ValidationUtils.getSpecialVisualisations(tr) + return specs?.some(sp => sp.needsNodeDatabase) + }) ?? false + if (!needsSpecial) { + return {result: json} + } + return { + result: {...json, fullNodeDatabase: true}, + information: ["Layer " + json.id + " needs the fullNodeDatabase"] + }; + } +} + export class AddMiniMap extends DesugaringStep { private readonly _state: DesugaringContext @@ -1163,19 +1194,19 @@ export class AddMiniMap extends DesugaringStep { convert(layerConfig: LayerConfigJson, context: string): { result: LayerConfigJson } { if (!layerConfig.tagRenderings || layerConfig.source === "special") { - return { result: layerConfig } + return {result: layerConfig} } const state = this._state const hasMinimap = ValidationUtils.hasSpecialVisualisation(layerConfig, "minimap") if (!hasMinimap) { - layerConfig = { ...layerConfig } + layerConfig = {...layerConfig} layerConfig.tagRenderings = [...layerConfig.tagRenderings] const minimap = state.tagRenderings.get("minimap") - if(minimap === undefined){ - if(state.tagRenderings.size > 0){ + if (minimap === undefined) { + if (state.tagRenderings.size > 0) { throw "The 'minimap'-builtin tagrendering is not defined. As such, it cannot be added automatically" } - }else{ + } else { layerConfig.tagRenderings.push(minimap) } } @@ -1197,6 +1228,7 @@ export class PrepareLayer extends Fuse { new AddQuestionBox(), new AddMiniMap(state), new AddEditingElements(state), + new SetFullNodeDatabase(), new On("mapRendering", new Concat(new ExpandRewrite()).andThenF(Utils.Flatten)), new On<(PointRenderingConfigJson | LineRenderingConfigJson)[], LayerConfigJson>( "mapRendering", diff --git a/Models/ThemeConfig/Json/LayerConfigJson.ts b/Models/ThemeConfig/Json/LayerConfigJson.ts index f5550ff81..2bbee874f 100644 --- a/Models/ThemeConfig/Json/LayerConfigJson.ts +++ b/Models/ThemeConfig/Json/LayerConfigJson.ts @@ -124,9 +124,6 @@ export interface LayerConfigJson { * If set, only features matching this extra tag will be shown. * This is useful to hide certain features from view. * - * Important: hiding features does not work dynamically, but is only calculated when the data is first renders. - * This implies that it is not possible to hide a feature after a tagging change - * * The default value is 'yes' */ isShown?: TagConfigJson @@ -404,4 +401,9 @@ export interface LayerConfigJson { * If set, open the selectedElementView in a floatOver instead of on the right */ popupInFloatover?: boolean + + /** + * _Set automatically by MapComplete, please ignore_ + */ + fullNodeDatabase?: boolean } diff --git a/Models/ThemeConfig/LayerConfig.ts b/Models/ThemeConfig/LayerConfig.ts index 94d7be601..cd4433fb9 100644 --- a/Models/ThemeConfig/LayerConfig.ts +++ b/Models/ThemeConfig/LayerConfig.ts @@ -68,7 +68,7 @@ export default class LayerConfig extends WithContextLoader { public readonly forceLoad: boolean public readonly syncSelection: typeof LayerConfig.syncSelectionAllowed[number] // this is a trick to conver a constant array of strings into a type union of these values - public readonly _needsFullNodeDatabase = false + public readonly _needsFullNodeDatabase: boolean public readonly popupInFloatover constructor(json: LayerConfigJson, context?: string, official: boolean = true) { @@ -217,6 +217,7 @@ export default class LayerConfig extends WithContextLoader { this.doNotDownload = json.doNotDownload ?? false this.passAllFeatures = json.passAllFeatures ?? false this.minzoom = json.minzoom ?? 0 + this._needsFullNodeDatabase = json.fullNodeDatabase ?? false if (json["minZoom"] !== undefined) { throw "At " + context + ": minzoom is written all lowercase" } diff --git a/Models/ThemeConfig/WithContextLoader.ts b/Models/ThemeConfig/WithContextLoader.ts index 489531460..8d480fb80 100644 --- a/Models/ThemeConfig/WithContextLoader.ts +++ b/Models/ThemeConfig/WithContextLoader.ts @@ -15,7 +15,7 @@ export default class WithContextLoader { * * The found value is interpreted as a tagrendering and fetched/parsed * */ - public tr(key: string, deflt: undefined, translationContext?: string) { + public tr(key: string, deflt?: string, translationContext?: string) { const v = this._json[key] if (v === undefined || v === null) { if (deflt === undefined) { diff --git a/Models/ThemeViewState.ts b/Models/ThemeViewState.ts index 7cf50c4e9..0f64bde4d 100644 --- a/Models/ThemeViewState.ts +++ b/Models/ThemeViewState.ts @@ -146,7 +146,7 @@ export default class ThemeViewState implements SpecialVisualizationState { rasterInfo.defaultState ?? true, "Wether or not overlayer layer " + rasterInfo.id + " is shown" ) - const state = { isDisplayed } + const state = {isDisplayed} overlayLayerStates.set(rasterInfo.id, state) new ShowOverlayRasterLayer(rasterInfo, this.map, this.mapProperties, state) } @@ -158,18 +158,34 @@ export default class ThemeViewState implements SpecialVisualizationState { * A bit tricky, as this is heavily intertwined with the 'changes'-element, which generate a stream of new and changed features too */ + + if(this.layout.layers.some(l => l._needsFullNodeDatabase)){ + this.fullNodeDatabase = new FullNodeDatabaseSource() + } + const layoutSource = new LayoutSource( layout.layers, this.featureSwitches, this.mapProperties, this.osmConnection.Backend(), - (id) => self.layerState.filteredLayers.get(id).isDisplayed + (id) => self.layerState.filteredLayers.get(id).isDisplayed, + this.fullNodeDatabase ) this.indexedFeatures = layoutSource const empty = [] + let currentViewIndex = 0 this.currentView = new StaticFeatureSource( - this.mapProperties.bounds.map((bbox) => - bbox === undefined ? empty : [bbox.asGeoJson({ id: "current_view" })] + this.mapProperties.bounds.map((bbox) => { + if (!bbox) { + return empty + } + currentViewIndex++ + return [bbox.asGeoJson({ + zoom: this.mapProperties.zoom.data, + ...this.mapProperties.location.data, + id: "current_view" } + )]; + } ) ) this.featuresInView = new BBoxFeatureSource(layoutSource, this.mapProperties.bounds) @@ -270,7 +286,7 @@ export default class ThemeViewState implements SpecialVisualizationState { for (const l of levels) { floors.add(l) } - }else{ + } else { floors.add("0") // '0' is the default and is thus _always_ present } } @@ -305,7 +321,7 @@ export default class ThemeViewState implements SpecialVisualizationState { this.drawSpecialLayers() this.initHotkeys() this.miscSetup() - if(!Utils.runningFromConsole){ + if (!Utils.runningFromConsole) { console.log("State setup completed", this) } } @@ -333,7 +349,7 @@ export default class ThemeViewState implements SpecialVisualizationState { private initHotkeys() { Hotkeys.RegisterHotkey( - { nomod: "Escape", onUp: true }, + {nomod: "Escape", onUp: true}, Translations.t.hotkeyDocumentation.closeSidebar, () => { this.selectedElement.setData(undefined) @@ -354,7 +370,7 @@ export default class ThemeViewState implements SpecialVisualizationState { ) Hotkeys.RegisterHotkey( - { shift: "O" }, + {shift: "O"}, Translations.t.hotkeyDocumentation.selectMapnik, () => { this.mapProperties.rasterLayer.setData(AvailableRasterLayers.osmCarto) @@ -373,17 +389,17 @@ export default class ThemeViewState implements SpecialVisualizationState { } Hotkeys.RegisterHotkey( - { nomod: "O" }, + {nomod: "O"}, Translations.t.hotkeyDocumentation.selectOsmbasedmap, () => setLayerCategory("osmbasedmap") ) - Hotkeys.RegisterHotkey({ nomod: "M" }, Translations.t.hotkeyDocumentation.selectMap, () => + Hotkeys.RegisterHotkey({nomod: "M"}, Translations.t.hotkeyDocumentation.selectMap, () => setLayerCategory("map") ) Hotkeys.RegisterHotkey( - { nomod: "P" }, + {nomod: "P"}, Translations.t.hotkeyDocumentation.selectAerial, () => setLayerCategory("photo") ) @@ -451,7 +467,7 @@ export default class ThemeViewState implements SpecialVisualizationState { ), range: new StaticFeatureSource( this.mapProperties.maxbounds.map((bbox) => - bbox === undefined ? empty : [bbox.asGeoJson({ id: "range" })] + bbox === undefined ? empty : [bbox.asGeoJson({id: "range"})] ) ), current_view: this.currentView @@ -465,6 +481,14 @@ export default class ThemeViewState implements SpecialVisualizationState { this.featureSwitches.featureSwitchIsTesting ) } + const currentViewLayer = this.layout.layers.find(l => l.id === "current_view") + if (currentViewLayer?.tagRenderings?.length > 0) { + const params = MetaTagging.createExtraFuncParams(this) + this.featureProperties.trackFeatureSource(specialLayers.current_view) + specialLayers.current_view.features.addCallbackAndRunD(features => { + MetaTagging.addMetatags(features, params, currentViewLayer, this.layout, this.osmObjectDownloader, this.featureProperties) + }) + } this.layerState.filteredLayers .get("range") @@ -545,7 +569,7 @@ export default class ThemeViewState implements SpecialVisualizationState { } { this.selectedElement.addCallback(selected => { - if(selected === undefined){ + if (selected === undefined) { // We did _unselect_ an item - we always remove the lastclick-object this.lastClickObject.features.setData([]) this.selectedLayer.setData(undefined) diff --git a/Models/TileRange.ts b/Models/TileRange.ts index a711a99a3..6b70c438e 100644 --- a/Models/TileRange.ts +++ b/Models/TileRange.ts @@ -97,6 +97,16 @@ export class Tiles { ) } + /** + * Construct a tilerange which (at least) contains the given coordinates. + * This means that the actual iterated area might be a bit bigger then the the passed in coordinates + * @param zoomlevel + * @param lat0 + * @param lon0 + * @param lat1 + * @param lon1 + * @constructor + */ static TileRangeBetween( zoomlevel: number, lat0: number, diff --git a/UI/Base/Table.ts b/UI/Base/Table.ts index 080566c42..b5ee42531 100644 --- a/UI/Base/Table.ts +++ b/UI/Base/Table.ts @@ -78,6 +78,9 @@ export default class Table extends BaseUIElement { for (let j = 0; j < row.length; j++) { try { let elem = row[j] + if(elem?.ConstructElement === undefined){ + continue + } const htmlElem = elem?.ConstructElement() if (htmlElem === undefined) { continue diff --git a/UI/Map/ShowDataLayer.ts b/UI/Map/ShowDataLayer.ts index c35ed676e..ebd7b35f3 100644 --- a/UI/Map/ShowDataLayer.ts +++ b/UI/Map/ShowDataLayer.ts @@ -330,6 +330,7 @@ class LineRenderingLayer { }) if (this._onClick) { map.on("click", polylayer, (e) => { + console.log("Got polylayer click:", e) // polygon-layer-listener if(e.originalEvent["consumed"]){ // This is a polygon beneath a marker, we can ignore it @@ -469,8 +470,10 @@ export default class ShowDataLayer { if (this._options.zoomToFeatures) { const features = this._options.features.features.data const bbox = BBox.bboxAroundAll(features.map(BBox.get)) + map.resize() map.fitBounds(bbox.toLngLat(), { padding: {top: 10, bottom: 10, left: 10, right: 10}, + animate: false }) } } diff --git a/UI/Popup/AutoApplyButton.ts b/UI/Popup/AutoApplyButton.ts index 086629c16..d415176af 100644 --- a/UI/Popup/AutoApplyButton.ts +++ b/UI/Popup/AutoApplyButton.ts @@ -1,26 +1,26 @@ import BaseUIElement from "../BaseUIElement" -import { Stores, UIEventSource } from "../../Logic/UIEventSource" -import { SubtleButton } from "../Base/SubtleButton" +import {Stores, UIEventSource} from "../../Logic/UIEventSource" +import {SubtleButton} from "../Base/SubtleButton" import Img from "../Base/Img" -import { FixedUiElement } from "../Base/FixedUiElement" +import {FixedUiElement} from "../Base/FixedUiElement" import Combine from "../Base/Combine" import Link from "../Base/Link" -import { Utils } from "../../Utils" +import {Utils} from "../../Utils" import StaticFeatureSource from "../../Logic/FeatureSource/Sources/StaticFeatureSource" -import { VariableUiElement } from "../Base/VariableUIElement" +import {VariableUiElement} from "../Base/VariableUIElement" import Loading from "../Base/Loading" -import { OsmConnection } from "../../Logic/Osm/OsmConnection" +import {OsmConnection} from "../../Logic/Osm/OsmConnection" import Translations from "../i18n/Translations" import LayoutConfig from "../../Models/ThemeConfig/LayoutConfig" -import { Changes } from "../../Logic/Osm/Changes" -import { UIElement } from "../UIElement" +import {Changes} from "../../Logic/Osm/Changes" +import {UIElement} from "../UIElement" import FilteredLayer from "../../Models/FilteredLayer" import TagRenderingConfig from "../../Models/ThemeConfig/TagRenderingConfig" import Lazy from "../Base/Lazy" import List from "../Base/List" -import { SpecialVisualization, SpecialVisualizationState } from "../SpecialVisualization" -import { IndexedFeatureSource } from "../../Logic/FeatureSource/FeatureSource" -import { MapLibreAdaptor } from "../Map/MapLibreAdaptor" +import {SpecialVisualization, SpecialVisualizationState} from "../SpecialVisualization" +import {IndexedFeatureSource} from "../../Logic/FeatureSource/FeatureSource" +import {MapLibreAdaptor} from "../Map/MapLibreAdaptor" import ShowDataLayer from "../Map/ShowDataLayer" import SvelteUIElement from "../Base/SvelteUIElement" import MaplibreMap from "../Map/MaplibreMap.svelte" @@ -54,6 +54,7 @@ class ApplyButton extends UIElement { >("idle") private readonly layer: FilteredLayer private readonly tagRenderingConfig: TagRenderingConfig + private readonly appliedNumberOfFeatures = new UIEventSource(0) constructor( state: SpecialVisualizationState, @@ -110,7 +111,7 @@ class ApplyButton extends UIElement { mla.allowZooming.setData(false) mla.allowMoving.setData(false) - const previewMap = new SvelteUIElement(MaplibreMap, { map: mlmap }).SetClass("h-48") + const previewMap = new SvelteUIElement(MaplibreMap, {map: mlmap}).SetClass("h-48") const features = this.target_feature_ids.map((id) => this.state.indexedFeatures.featuresById.data.get(id) @@ -131,7 +132,9 @@ class ApplyButton extends UIElement { return new FixedUiElement("All done!").SetClass("thanks") } if (st === "running") { - return new Loading("Applying changes...") + return new Loading(new VariableUiElement(this.appliedNumberOfFeatures.map(appliedTo => { + return "Applying changes, currently at " + appliedTo + "/" + this.target_feature_ids.length + }))) } const error = st.error return new Combine([ @@ -142,11 +145,16 @@ class ApplyButton extends UIElement { ) } + /** + * Actually applies all the changes... + */ private async Run() { try { console.log("Applying auto-action on " + this.target_feature_ids.length + " features") - for (const targetFeatureId of this.target_feature_ids) { + for (let i = 0; i < this.target_feature_ids.length; i++) { + const targetFeatureId = this.target_feature_ids[i]; + const feature = this.state.indexedFeatures.featuresById.data.get(targetFeatureId) const featureTags = this.state.featureProperties.getStore(targetFeatureId) const rendering = this.tagRenderingConfig.GetRenderValue(featureTags.data).txt const specialRenderings = Utils.NoNull( @@ -156,8 +164,8 @@ class ApplyButton extends UIElement { if (specialRenderings.length == 0) { console.warn( "AutoApply: feature " + - targetFeatureId + - " got a rendering without supported auto actions:", + targetFeatureId + + " got a rendering without supported auto actions:", rendering ) } @@ -167,15 +175,19 @@ class ApplyButton extends UIElement { continue } const action = specialRendering.func - await action.applyActionOn(this.state, featureTags, specialRendering.args) + await action.applyActionOn(feature, this.state, featureTags, specialRendering.args) } + if( i % 50 === 0){ + await this.state.changes.flushChanges("Auto button: intermediate save") + } + this.appliedNumberOfFeatures.setData(i + 1) } console.log("Flushing changes...") - await this.state.changes.flushChanges("Auto button") + await this.state.changes.flushChanges("Auto button: done") this.buttonState.setData("done") } catch (e) { console.error("Error while running autoApply: ", e) - this.buttonState.setData({ error: e }) + this.buttonState.setData({error: e}) } } } @@ -230,7 +242,7 @@ export default class AutoApplyButton implements SpecialVisualization { "To effectively use this button, you'll need some ingredients:", new List([ "A target layer with features for which an action is defined in a tag rendering. The following special visualisations support an autoAction: " + - supportedActions.join(", "), + supportedActions.join(", "), "A host feature to place the auto-action on. This can be a big outline (such as a city). Another good option for this is the layer ", new Link("current_view", "./BuiltinLayers.md#current_view"), "Then, use a calculated tag on the host feature to determine the overlapping object ids", @@ -250,7 +262,7 @@ export default class AutoApplyButton implements SpecialVisualization { !( state.featureSwitchIsTesting.data || state.osmConnection._oauth_config.url === - OsmConnection.oauth_configs["osm-test"].url + OsmConnection.oauth_configs["osm-test"].url ) ) { const t = Translations.t.general.add.import @@ -274,21 +286,27 @@ export default class AutoApplyButton implements SpecialVisualization { } return new Lazy(() => { - const to_parse = new UIEventSource(undefined) + const to_parse = new UIEventSource(undefined) // Very ugly hack: read the value every 500ms Stores.Chronic(500, () => to_parse.data === undefined).addCallback(() => { - const applicable = tagSource.data[argument[1]] - to_parse.setData(applicable) + let applicable = tagSource.data[argument[1]] + if(typeof applicable === "string"){ + applicable = JSON.parse(applicable) + } + to_parse.setData( applicable) }) const loading = new Loading("Gathering which elements support auto-apply... ") return new VariableUiElement( - to_parse.map((ids) => { + Stores.ListStabilized(to_parse).map((ids) => { if (ids === undefined) { return loading } - return new ApplyButton(state, JSON.parse(ids), options) + if (typeof ids === "string") { + ids = JSON.parse(ids) + } + return new ApplyButton(state, ids, options) }) ) }) diff --git a/UI/Popup/ImportButton.ts b/UI/Popup/ImportButton.ts deleted file mode 100644 index 1dd8cbf97..000000000 --- a/UI/Popup/ImportButton.ts +++ /dev/null @@ -1,162 +0,0 @@ -import BaseUIElement from "../BaseUIElement" -import {Store, UIEventSource} from "../../Logic/UIEventSource" -import Translations from "../i18n/Translations" -import {FixedUiElement} from "../Base/FixedUiElement" -import OsmChangeAction from "../../Logic/Osm/Actions/OsmChangeAction" -import {And} from "../../Logic/Tags/And" -import {Tag} from "../../Logic/Tags/Tag" -import {SpecialVisualization, SpecialVisualizationState} from "../SpecialVisualization" -import {Feature} from "geojson" -import {ImportFlowArguments, ImportFlowUtils} from "./ImportButtons/ImportFlow"; -import {MergePointConfig} from "../../Logic/Osm/Actions/CreateWayWithPointReuseAction"; -import {GeoOperations} from "../../Logic/GeoOperations"; -import ReplaceGeometryAction from "../../Logic/Osm/Actions/ReplaceGeometryAction"; -import {TagUtils} from "../../Logic/Tags/TagUtils"; - - -/** - * @deprecated - * A helper class for the various import-flows. - * An import-flow always starts with a 'Import this'-button. Upon click, a custom confirmation panel is provided - */ -export abstract class AbstractImportButton implements SpecialVisualization { - - public readonly funcName: string - public readonly docs: string - public readonly args: { name: string; defaultValue?: string; doc: string }[] - private readonly showRemovedTags: boolean - private readonly cannotBeImportedMessage: BaseUIElement | undefined - - constructor( - funcName: string, - docsIntro: string, - extraArgs: { name: string; doc: string; defaultValue?: string; required?: boolean }[], - options?: { showRemovedTags?: true | boolean; cannotBeImportedMessage?: BaseUIElement } - ) { - this.funcName = funcName - this.showRemovedTags = options?.showRemovedTags ?? true - this.cannotBeImportedMessage = options?.cannotBeImportedMessage - this.docs = `${docsIntro}${ImportFlowUtils.documentationGeneral}` - - this.args = [ - { - name: "targetLayer", - doc: "The id of the layer where this point should end up. This is not very strict, it will simply result in checking that this layer is shown preventing possible duplicate elements", - required: true, - }, - { - name: "tags", - doc: "The tags to add onto the new object - see specification above. If this is a key (a single word occuring in the properties of the object), the corresponding value is taken and expanded instead", - required: true, - }, - { - name: "text", - doc: "The text to show on the button", - defaultValue: "Import this data into OpenStreetMap", - }, - { - name: "icon", - doc: "A nice icon to show in the button", - defaultValue: "./assets/svg/addSmall.svg", - }, - ...extraArgs, - ] - } - - abstract constructElement( - state: SpecialVisualizationState, - args: ImportFlowArguments, - newTags: Store, - tagSource: UIEventSource>, - feature: Feature, - onCancelClicked: () => void - ): BaseUIElement - - constr( - state: SpecialVisualizationState, - tagSource: UIEventSource>, - argsRaw: string[] - ) { - return new FixedUiElement("Deprecated") - } - -} - -export class ConflateButton extends AbstractImportButton { - needsNodeDatabase = true - - constructor() { - super( - "conflate_button", - "This button will modify the geometry of an existing OSM way to match the specified geometry. This can conflate OSM-ways with LineStrings and Polygons (only simple polygons with one single ring). An attempt is made to move points with special values to a decent new location (e.g. entrances)", - [ - { - name: "way_to_conflate", - doc: "The key, of which the corresponding value is the id of the OSM-way that must be conflated; typically a calculatedTag", - }, - ], - { - cannotBeImportedMessage: Translations.t.general.add.import.wrongTypeToConflate, - } - ) - } - - constructElement( - state: SpecialVisualizationState, - args: { - max_snap_distance: string - snap_onto_layers: string - icon: string - text: string - tags: string - newTags: UIEventSource - targetLayer: string - }, - tagSource: UIEventSource, - feature: Feature, - onCancelClicked: () => void - ): BaseUIElement { - const nodesMustMatch = args.snap_onto_layers - ?.split(";") - ?.map((tag, i) => TagUtils.Tag(tag, "TagsSpec for import button " + i)) - - const mergeConfigs = [] - if (nodesMustMatch !== undefined && nodesMustMatch.length > 0) { - const mergeConfig: MergePointConfig = { - mode: args["point_move_mode"] == "move_osm" ? "move_osm_point" : "reuse_osm_point", - ifMatches: new And(nodesMustMatch), - withinRangeOfM: Number(args.max_snap_distance), - } - mergeConfigs.push(mergeConfig) - } - - const key = args["way_to_conflate"] - const wayToConflate = tagSource.data[key] - feature = GeoOperations.removeOvernoding(feature) - const action: OsmChangeAction & { getPreview(): Promise } = new ReplaceGeometryAction( - state, - feature, - wayToConflate, - { - theme: state.layout.id, - newTags: args.newTags.data, - } - ) - - return this.createConfirmPanelForWay( - state, - args, - feature, - tagSource, - action, - onCancelClicked - ) - } - - protected canBeImported(feature: Feature) { - return ( - feature.geometry.type === "LineString" || - (feature.geometry.type === "Polygon" && feature.geometry.coordinates.length === 1) - ) - } -} diff --git a/UI/Popup/ImportButtons/ConflateImportButtonViz.ts b/UI/Popup/ImportButtons/ConflateImportButtonViz.ts new file mode 100644 index 000000000..e13b4b06c --- /dev/null +++ b/UI/Popup/ImportButtons/ConflateImportButtonViz.ts @@ -0,0 +1,89 @@ +import {SpecialVisualization, SpecialVisualizationState} from "../../SpecialVisualization"; +import {UIEventSource} from "../../../Logic/UIEventSource"; +import {Feature, Geometry, LineString, Polygon} from "geojson"; +import LayerConfig from "../../../Models/ThemeConfig/LayerConfig"; +import BaseUIElement from "../../BaseUIElement"; +import {ImportFlowArguments, ImportFlowUtils} from "./ImportFlow"; +import Translations from "../../i18n/Translations"; +import {Utils} from "../../../Utils"; +import SvelteUIElement from "../../Base/SvelteUIElement"; +import WayImportFlow from "./WayImportFlow.svelte"; +import ConflateImportFlowState from "./ConflateImportFlowState"; +import {AutoAction} from "../AutoApplyButton"; +import {IndexedFeatureSource} from "../../../Logic/FeatureSource/FeatureSource"; +import {Changes} from "../../../Logic/Osm/Changes"; +import LayoutConfig from "../../../Models/ThemeConfig/LayoutConfig"; +import {OsmConnection} from "../../../Logic/Osm/OsmConnection"; + +export interface ConflateFlowArguments extends ImportFlowArguments { + way_to_conflate: string + point_move_mode?: "move_osm" | undefined; + max_snap_distance?: string + snap_onto_layers?: string, +} + +export default class ConflateImportButtonViz implements SpecialVisualization, AutoAction { + supportsAutoAction: boolean = true; + public readonly funcName: string = "conflate_button"; + public readonly args: { name: string; defaultValue?: string; doc: string; required?: boolean }[] = [ + ...ImportFlowUtils.generalArguments, + { + name: "way_to_conflate", + doc: "The key, of which the corresponding value is the id of the OSM-way that must be conflated; typically a calculatedTag", + }, + + + ]; + readonly docs: string = "This button will modify the geometry of an existing OSM way to match the specified geometry. This can conflate OSM-ways with LineStrings and Polygons (only simple polygons with one single ring). An attempt is made to move points with special values to a decent new location (e.g. entrances)" + ImportFlowUtils.documentationGeneral + public readonly needsNodeDatabase = true + + + async applyActionOn(feature: Feature, state: { + osmConnection: OsmConnection, + layout: LayoutConfig; + changes: Changes; + indexedFeatures: IndexedFeatureSource; + }, tagSource: UIEventSource, argument: string[]): Promise { + + { + // Small safety check to prevent duplicate imports + const id = tagSource.data.id + if (ImportFlowUtils.importedIds.has(id)) { + return + } + ImportFlowUtils.importedIds.add(id) + } + + if (feature.geometry.type !== "LineString" && feature.geometry.type !== "Polygon") { + return + } + + const args: ConflateFlowArguments = Utils.ParseVisArgs(this.args, argument) + const tagsToApply = ImportFlowUtils.getTagsToApply(tagSource, args) + const idOfWayToReplaceGeometry = tagSource.data[args.way_to_conflate] + const action = ConflateImportFlowState.createAction(>feature, args, state, idOfWayToReplaceGeometry, tagsToApply) + tagSource.data["_imported"] = "yes" + tagSource.ping() + await state.changes.applyAction(action) + } + + constr(state: SpecialVisualizationState, tagSource: UIEventSource>, argument: string[], feature: Feature, layer: LayerConfig): BaseUIElement { + + const canBeImported = feature.geometry.type === "LineString" || + (feature.geometry.type === "Polygon" && feature.geometry.coordinates.length === 1) + if (!canBeImported) { + return Translations.t.general.add.import.wrongTypeToConflate.SetClass("alert") + } + const args: ConflateFlowArguments = Utils.ParseVisArgs(this.args, argument) + const tagsToApply = ImportFlowUtils.getTagsToApply(tagSource, args) + const idOfWayToReplaceGeometry = tagSource.data[args.way_to_conflate] + const importFlow = new ConflateImportFlowState(state, >feature, args, tagsToApply, tagSource, idOfWayToReplaceGeometry) + return new SvelteUIElement(WayImportFlow, { + importFlow + }) + } + + getLayerDependencies(args: string[]) { + return ImportFlowUtils.getLayerDependenciesWithSnapOnto(this.args, args) + } +} diff --git a/UI/Popup/ImportButtons/ConflateImportFlowState.ts b/UI/Popup/ImportButtons/ConflateImportFlowState.ts new file mode 100644 index 000000000..967396956 --- /dev/null +++ b/UI/Popup/ImportButtons/ConflateImportFlowState.ts @@ -0,0 +1,83 @@ +import ImportFlow from "./ImportFlow"; +import {ConflateFlowArguments} from "./ConflateImportButtonViz"; +import {SpecialVisualizationState} from "../../SpecialVisualization"; +import {Feature, LineString, Polygon} from "geojson"; +import {Store, UIEventSource} from "../../../Logic/UIEventSource"; +import {Tag} from "../../../Logic/Tags/Tag"; +import OsmChangeAction from "../../../Logic/Osm/Actions/OsmChangeAction"; +import ReplaceGeometryAction from "../../../Logic/Osm/Actions/ReplaceGeometryAction"; +import {GeoOperations} from "../../../Logic/GeoOperations"; +import {TagUtils} from "../../../Logic/Tags/TagUtils"; +import {MergePointConfig} from "../../../Logic/Osm/Actions/CreateWayWithPointReuseAction"; +import {And} from "../../../Logic/Tags/And"; +import LayoutConfig from "../../../Models/ThemeConfig/LayoutConfig"; +import {Changes} from "../../../Logic/Osm/Changes"; +import {FeatureSource, IndexedFeatureSource} from "../../../Logic/FeatureSource/FeatureSource"; +import FullNodeDatabaseSource from "../../../Logic/FeatureSource/TiledFeatureSource/FullNodeDatabaseSource"; +import {OsmConnection} from "../../../Logic/Osm/OsmConnection"; + +export default class ConflateImportFlowState extends ImportFlow { + + public readonly originalFeature: Feature + private readonly action: OsmChangeAction & { getPreview?(): Promise; newElementId?: string }; + constructor(state: SpecialVisualizationState, originalFeature: Feature, args: ConflateFlowArguments, tagsToApply: Store, originalFeatureTags: UIEventSource>, idOfFeatureToReplaceGeometry: string) { + super(state, args, tagsToApply, originalFeatureTags) + this.originalFeature = originalFeature + this.action = ConflateImportFlowState.createAction(originalFeature, args, state, idOfFeatureToReplaceGeometry, tagsToApply) + + } + + // noinspection JSUnusedGlobalSymbols + public GetPreview(): Promise{ + return this.action.getPreview() + } + + public async onConfirm() { + const originalFeatureTags = this._originalFeatureTags + originalFeatureTags.data["_imported"] = "yes" + originalFeatureTags.ping() // will set isImported as per its definition + const action = this.action + await this.state.changes.applyAction(action) + const newId = action.newElementId ?? action.mainObjectId + this.state.selectedLayer.setData(this.targetLayer.layerDef) + this.state.selectedElement.setData(this.state.indexedFeatures.featuresById.data.get(newId)) + } + + public static createAction(feature: Feature, + args: ConflateFlowArguments, + state: { + osmConnection: OsmConnection, + layout: LayoutConfig; + changes: Changes; + indexedFeatures: IndexedFeatureSource, + fullNodeDatabase?: FullNodeDatabaseSource + }, + idOfFeatureToReplaceGeometry, + tagsToApply: Store + ): OsmChangeAction & { getPreview?(): Promise; newElementId?: string } { + const nodesMustMatch = args.snap_onto_layers + ?.split(";") + ?.map((tag, i) => TagUtils.Tag(tag, "TagsSpec for import button " + i)) + + const mergeConfigs = [] + if (nodesMustMatch !== undefined && nodesMustMatch.length > 0) { + const mergeConfig: MergePointConfig = { + mode: args.point_move_mode == "move_osm" ? "move_osm_point" : "reuse_osm_point", + ifMatches: new And(nodesMustMatch), + withinRangeOfM: Number(args.max_snap_distance ?? 0), + } + mergeConfigs.push(mergeConfig) + } + + + return new ReplaceGeometryAction( + state, + GeoOperations.removeOvernoding(feature), + idOfFeatureToReplaceGeometry, + { + theme: state.layout.id, + newTags: tagsToApply.data, + } + ) + } +} diff --git a/UI/Popup/ImportButtons/ImportFlow.svelte b/UI/Popup/ImportButtons/ImportFlow.svelte index 98cbdc089..436c7f82d 100644 --- a/UI/Popup/ImportButtons/ImportFlow.svelte +++ b/UI/Popup/ImportButtons/ImportFlow.svelte @@ -15,6 +15,9 @@ import TagHint from "../TagHint.svelte"; import {TagsFilter} from "../../../Logic/Tags/TagsFilter"; import {Store} from "../../../Logic/UIEventSource"; + import Svg from "../../../Svg"; + import ToSvelte from "../../Base/ToSvelte.svelte"; + import {EyeIcon, EyeOffIcon} from "@rgossiaux/svelte-heroicons/solid"; export let importFlow: ImportFlow let state = importFlow.state @@ -24,16 +27,74 @@ const isLoading = state.dataIsLoading const dispatch = createEventDispatcher<{ confirm }>() const canBeImported = importFlow.canBeImported() - const tags : Store = importFlow.tagsToApply.map(tags => new And(tags)) + const tags: Store = importFlow.tagsToApply.map(tags => new And(tags)) const isDisplayed = importFlow.targetLayer.isDisplayed - const hasFilter = importFlow.targetLayer.appliedFilters + const hasFilter = importFlow.targetLayer.hasFilter + + function abort() { + state.selectedElement.setData(undefined) + state.selectedLayer.setData(undefined) + } + - {#if currentFlowStep === "start"} + + {#if $canBeImported !== true && $canBeImported !== undefined} + + {#if $canBeImported.extraHelp} + + {/if} + {:else if !$isDisplayed} + +
+ + +
+ +
+ + + + +
+ {:else if $hasFilter} + +
+ + +
+
+ + +
+ + {:else if $isLoading} + + + + + {:else if currentFlowStep === "start"} currentFlowStep = "confirm"}> {#if importFlow?.args?.icon} @@ -42,15 +103,6 @@ {importFlow.args.text} - {:else if $canBeImported !== true && $canBeImported !== undefined} - - {#if $canBeImported.extraHelp} - - {/if} - {:else if $isLoading} - - - {:else if currentFlowStep === "confirm"}
diff --git a/UI/Popup/ImportButtons/ImportFlow.ts b/UI/Popup/ImportButtons/ImportFlow.ts index d16085ee2..1644da27a 100644 --- a/UI/Popup/ImportButtons/ImportFlow.ts +++ b/UI/Popup/ImportButtons/ImportFlow.ts @@ -149,11 +149,13 @@ export default abstract class ImportFlow { public readonly args: ArgT; public readonly targetLayer: FilteredLayer; public readonly tagsToApply: Store + protected readonly _originalFeatureTags: UIEventSource>; - constructor(state: SpecialVisualizationState, args: ArgT, tagsToApply: Store) { + constructor(state: SpecialVisualizationState, args: ArgT, tagsToApply: Store, originalTags: UIEventSource>) { this.state = state; this.args = args; this.tagsToApply = tagsToApply; + this._originalFeatureTags = originalTags; this.targetLayer = state.layerState.filteredLayers.get(args.targetLayer) @@ -166,6 +168,11 @@ export default abstract class ImportFlow { const state = this.state return state.featureSwitchIsTesting.map(isTesting => { const t = Translations.t.general.add.import + + if(this._originalFeatureTags.data["_imported"] === "yes"){ + return {error: t.hasBeenImported} + } + const usesTestUrl = this.state.osmConnection._oauth_config.url === OsmConnection.oauth_configs["osm-test"].url if (!state.layout.official && !(isTesting || usesTestUrl)) { // Unofficial theme - imports not allowed @@ -191,7 +198,7 @@ export default abstract class ImportFlow { } return undefined - }, [state.mapProperties.zoom, state.dataIsLoading]) + }, [state.mapProperties.zoom, state.dataIsLoading, this._originalFeatureTags]) } diff --git a/UI/Popup/ImportButtons/ImportPointButtonViz.ts b/UI/Popup/ImportButtons/PointImportButtonViz.ts similarity index 92% rename from UI/Popup/ImportButtons/ImportPointButtonViz.ts rename to UI/Popup/ImportButtons/PointImportButtonViz.ts index 68f84c068..bf17aacd4 100644 --- a/UI/Popup/ImportButtons/ImportPointButtonViz.ts +++ b/UI/Popup/ImportButtons/PointImportButtonViz.ts @@ -13,7 +13,7 @@ import Translations from "../../i18n/Translations"; /** * The wrapper to make the special visualisation for the PointImportFlow */ -export class ImportPointButtonViz implements SpecialVisualization { +export class PointImportButtonViz implements SpecialVisualization { public readonly funcName: string public readonly docs: string | BaseUIElement @@ -51,7 +51,7 @@ export class ImportPointButtonViz implements SpecialVisualization { } const baseArgs: PointImportFlowArguments = Utils.ParseVisArgs(this.args, argument) const tagsToApply = ImportFlowUtils.getTagsToApply(tagSource , baseArgs) - const importFlow = new PointImportFlowState(state, > feature, baseArgs, tagsToApply) + const importFlow = new PointImportFlowState(state, > feature, baseArgs, tagsToApply, tagSource) return new SvelteUIElement( PointImportFlow, { @@ -60,7 +60,5 @@ export class ImportPointButtonViz implements SpecialVisualization { ) } - getLayerDependencies(argsRaw: string[]): string[] { - return ImportFlowUtils.getLayerDependenciesWithSnapOnto(this.args, argsRaw) - } + } diff --git a/UI/Popup/ImportButtons/PointImportFlowState.ts b/UI/Popup/ImportButtons/PointImportFlowState.ts index f42dc33ff..b20c925a8 100644 --- a/UI/Popup/ImportButtons/PointImportFlowState.ts +++ b/UI/Popup/ImportButtons/PointImportFlowState.ts @@ -20,12 +20,10 @@ export interface PointImportFlowArguments extends ImportFlowArguments { export class PointImportFlowState extends ImportFlow { public readonly startCoordinate: [number, number] private readonly _originalFeature: Feature; - private readonly _originalFeatureTags: UIEventSource> - constructor(state: SpecialVisualizationState, originalFeature: Feature, args: PointImportFlowArguments, tagsToApply: Store) { - super(state, args, tagsToApply); + constructor(state: SpecialVisualizationState, originalFeature: Feature, args: PointImportFlowArguments, tagsToApply: Store, originalFeatureTags: UIEventSource>) { + super(state, args, tagsToApply, originalFeatureTags); this._originalFeature = originalFeature; - this._originalFeatureTags = state.featureProperties.getStore(originalFeature.properties.id) this.startCoordinate = GeoOperations.centerpointCoordinates(originalFeature) } diff --git a/UI/Popup/ImportButtons/WayImportButtonViz.ts b/UI/Popup/ImportButtons/WayImportButtonViz.ts index 0f4fc73c6..b674551ea 100644 --- a/UI/Popup/ImportButtons/WayImportButtonViz.ts +++ b/UI/Popup/ImportButtons/WayImportButtonViz.ts @@ -22,49 +22,42 @@ import FullNodeDatabaseSource from "../../../Logic/FeatureSource/TiledFeatureSou export default class WayImportButtonViz implements AutoAction, SpecialVisualization { - public readonly funcName: string - public readonly docs: string | BaseUIElement - public readonly example?: string - public readonly args: { name: string; defaultValue?: string; doc: string }[] + public readonly funcName: string= "import_way_button" + public readonly docs: string = "This button will copy the data from an external dataset into OpenStreetMap, copying the geometry and adding it as a 'line'" + ImportFlowUtils.documentationGeneral + public readonly args: { name: string; defaultValue?: string; doc: string }[]= [ + ...ImportFlowUtils.generalArguments, + { + name: "snap_to_point_if", + doc: "Points with the given tags will be snapped to or moved", + }, + { + name: "max_snap_distance", + doc: "If the imported object is a LineString or (Multi)Polygon, already existing OSM-points will be reused to construct the geometry of the newly imported way", + defaultValue: "0.05", + }, + { + name: "move_osm_point_if", + doc: "Moves the OSM-point to the newly imported point if these conditions are met", + }, + { + name: "max_move_distance", + doc: "If an OSM-point is moved, the maximum amount of meters it is moved. Capped on 20m", + defaultValue: "0.05", + }, + { + name: "snap_onto_layers", + doc: "If no existing nearby point exists, but a line of a specified layer is closeby, snap to this layer instead", + }, + { + name: "snap_to_layer_max_distance", + doc: "Distance to distort the geometry to snap to this layer", + defaultValue: "0.1", + }, + ] public readonly supportsAutoAction = true public readonly needsNodeDatabase = true - constructor() { - this.funcName = "import_way_button" - this.docs = "This button will copy the data from an external dataset into OpenStreetMap, copying the geometry and adding it as a 'line'" + ImportFlowUtils.documentationGeneral - this.args = [ - ...ImportFlowUtils.generalArguments, - { - name: "snap_to_point_if", - doc: "Points with the given tags will be snapped to or moved", - }, - { - name: "max_snap_distance", - doc: "If the imported object is a LineString or (Multi)Polygon, already existing OSM-points will be reused to construct the geometry of the newly imported way", - defaultValue: "0.05", - }, - { - name: "move_osm_point_if", - doc: "Moves the OSM-point to the newly imported point if these conditions are met", - }, - { - name: "max_move_distance", - doc: "If an OSM-point is moved, the maximum amount of meters it is moved. Capped on 20m", - defaultValue: "0.05", - }, - { - name: "snap_onto_layers", - doc: "If no existing nearby point exists, but a line of a specified layer is closeby, snap to this layer instead", - }, - { - name: "snap_to_layer_max_distance", - doc: "Distance to distort the geometry to snap to this layer", - defaultValue: "0.1", - }, - ] - } - constr(state: SpecialVisualizationState, tagSource: UIEventSource>, argument: string[], feature: Feature, layer: LayerConfig): BaseUIElement { const geometry = feature.geometry if (!(geometry.type == "LineString" || geometry.type === "Polygon")) { @@ -100,8 +93,10 @@ export default class WayImportButtonViz implements AutoAction, SpecialVisualizat const args: WayImportFlowArguments = Utils.ParseVisArgs(this.args, argument) const tagsToApply = ImportFlowUtils.getTagsToApply(tagSource, args) - const mergeConfigs = WayImportFlowState.GetMergeConfig(args, tagsToApply) + const mergeConfigs = WayImportFlowState.GetMergeConfig(args) const action = WayImportFlowState.CreateAction(>feature, args, state, tagsToApply, mergeConfigs) + tagSource.data["_imported"] = "yes" + tagSource.ping() await state.changes.applyAction(action) } diff --git a/UI/Popup/ImportButtons/WayImportFlow.svelte b/UI/Popup/ImportButtons/WayImportFlow.svelte index 807d7be72..0451f1228 100644 --- a/UI/Popup/ImportButtons/WayImportFlow.svelte +++ b/UI/Popup/ImportButtons/WayImportFlow.svelte @@ -1,5 +1,7 @@