2025-08-15 02:32:04 +02:00
import {
SpecialVisualisationArg ,
SpecialVisualisationParams ,
SpecialVisualizationSvelte ,
SpecialVisualizationUtils ,
} from "../../SpecialVisualization"
2023-06-14 20:39:36 +02:00
import { UIEventSource } from "../../../Logic/UIEventSource"
import { Feature , Geometry , LineString , Polygon } from "geojson"
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"
2025-06-02 17:03:07 +02:00
import { AutoAction } from "../AutoApplyButtonVis"
2023-06-14 20:39:36 +02:00
import { IndexedFeatureSource } from "../../../Logic/FeatureSource/FeatureSource"
import { Changes } from "../../../Logic/Osm/Changes"
2024-10-17 04:06:03 +02:00
import ThemeConfig from "../../../Models/ThemeConfig/ThemeConfig"
2023-06-14 20:39:36 +02:00
import { OsmConnection } from "../../../Logic/Osm/OsmConnection"
2025-06-26 05:20:12 +02:00
import { OsmTags } from "../../../Models/OsmFeature"
2025-08-15 02:32:04 +02:00
import Tr from "../../Base/Tr.svelte"
2023-06-01 02:52:21 +02:00
export interface ConflateFlowArguments extends ImportFlowArguments {
way_to_conflate : string
2023-06-14 20:39:36 +02:00
point_move_mode ? : "move_osm" | undefined
2023-06-01 02:52:21 +02:00
max_snap_distance? : string
2023-06-14 20:39:36 +02:00
snap_onto_layers? : string
2023-06-01 02:52:21 +02:00
}
2025-08-15 02:32:04 +02:00
export default class ConflateImportButtonViz extends SpecialVisualizationSvelte implements AutoAction {
2023-06-14 20:39:36 +02:00
supportsAutoAction : boolean = true
2023-09-27 22:21:35 +02:00
needsUrls = [ ]
2025-01-29 20:37:04 +01:00
group = "data_import"
2023-06-14 20:39:36 +02:00
public readonly funcName : string = "conflate_button"
2025-08-14 15:54:33 +02:00
public readonly args : SpecialVisualisationArg [ ] = [
2023-06-01 02:52:21 +02:00
. . . ImportFlowUtils . generalArguments ,
{
name : "way_to_conflate" ,
2025-08-14 15:54:33 +02:00
type : "key" ,
2023-06-01 02:52:21 +02:00
doc : "The key, of which the corresponding value is the id of the OSM-way that must be conflated; typically a calculatedTag" ,
} ,
2023-06-14 20:39:36 +02:00
]
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
2023-06-01 02:52:21 +02:00
public readonly needsNodeDatabase = true
2023-06-14 20:39:36 +02:00
async applyActionOn (
feature : Feature < Geometry , { [ name : string ] : any } > ,
state : {
osmConnection : OsmConnection
2024-10-17 04:06:03 +02:00
theme : ThemeConfig
2023-06-14 20:39:36 +02:00
changes : Changes
indexedFeatures : IndexedFeatureSource
} ,
tagSource : UIEventSource < any > ,
argument : string [ ]
) : Promise < void > {
2023-06-01 02:52:21 +02:00
{
// 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 = < any > Utils . ParseVisArgs ( this . args , argument )
const tagsToApply = ImportFlowUtils . getTagsToApply ( tagSource , args )
const idOfWayToReplaceGeometry = tagSource . data [ args . way_to_conflate ]
2023-06-14 20:39:36 +02:00
const action = ConflateImportFlowState . createAction (
< Feature < LineString | Polygon > > feature ,
args ,
state ,
idOfWayToReplaceGeometry ,
tagsToApply
)
2023-06-01 02:52:21 +02:00
tagSource . data [ "_imported" ] = "yes"
tagSource . ping ( )
await state . changes . applyAction ( action )
}
2025-08-15 02:32:04 +02:00
constr ( { state , tags , args , feature } : SpecialVisualisationParams ) : SvelteUIElement {
2023-06-14 20:39:36 +02:00
const canBeImported =
feature . geometry . type === "LineString" ||
2023-06-01 02:52:21 +02:00
( feature . geometry . type === "Polygon" && feature . geometry . coordinates . length === 1 )
if ( ! canBeImported ) {
2025-08-15 02:32:04 +02:00
return new SvelteUIElement ( Tr , { t : Translations.t.general.add.import.wrongTypeToConflate , cls : "alert" } )
2023-06-01 02:52:21 +02:00
}
2025-08-15 02:32:04 +02:00
const argsParsed : ConflateFlowArguments = < any > SpecialVisualizationUtils . parseArgs ( this . args , args )
const tagsToApply = ImportFlowUtils . getTagsToApply ( < UIEventSource < OsmTags > > tags , argsParsed )
const idOfWayToReplaceGeometry = tags . data [ argsParsed . way_to_conflate ]
2023-06-14 20:39:36 +02:00
const importFlow = new ConflateImportFlowState (
state ,
< Feature < LineString | Polygon > > feature ,
2025-08-15 02:32:04 +02:00
argsParsed ,
2023-06-14 20:39:36 +02:00
tagsToApply ,
2025-08-15 02:32:04 +02:00
tags ,
2023-06-14 20:39:36 +02:00
idOfWayToReplaceGeometry
)
2025-08-15 02:32:04 +02:00
return new SvelteUIElement ( WayImportFlow , { importFlow } )
2023-06-01 02:52:21 +02:00
}
2025-07-10 18:26:31 +02:00
getLayerDependencies = ( args : string [ ] ) = >
ImportFlowUtils . getLayerDependenciesWithSnapOnto ( this . args , args )
2023-06-01 02:52:21 +02:00
}