2023-06-14 20:39:36 +02:00
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"
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
}
export default class ConflateImportButtonViz implements SpecialVisualization , AutoAction {
2023-06-14 20:39:36 +02:00
supportsAutoAction : boolean = true
public readonly funcName : string = "conflate_button"
public readonly args : {
name : string
defaultValue? : string
doc : string
required? : boolean
} [ ] = [
2023-06-01 02:52:21 +02:00
. . . 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" ,
} ,
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
layout : LayoutConfig
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 )
}
2023-06-14 20:39:36 +02:00
constr (
state : SpecialVisualizationState ,
tagSource : UIEventSource < Record < string , string > > ,
argument : string [ ] ,
feature : Feature ,
layer : LayerConfig
) : BaseUIElement {
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 ) {
return Translations . t . general . add . import . wrongTypeToConflate . SetClass ( "alert" )
}
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 importFlow = new ConflateImportFlowState (
state ,
< Feature < LineString | Polygon > > feature ,
args ,
tagsToApply ,
tagSource ,
idOfWayToReplaceGeometry
)
2023-06-01 02:52:21 +02:00
return new SvelteUIElement ( WayImportFlow , {
2023-06-14 20:39:36 +02:00
importFlow ,
2023-06-01 02:52:21 +02:00
} )
}
getLayerDependencies ( args : string [ ] ) {
return ImportFlowUtils . getLayerDependenciesWithSnapOnto ( this . args , args )
}
}