refactoring: fix basic flow to add a new point

This commit is contained in:
Pieter Vander Vennet 2023-04-06 01:33:08 +02:00
parent 52a0810ea9
commit 0241f89d3d
109 changed files with 1931 additions and 1446 deletions

View file

@ -1,4 +1,4 @@
import FeatureSource, { IndexedFeatureSource } from "../FeatureSource"
import { FeatureSource, IndexedFeatureSource } from "../FeatureSource"
import { UIEventSource } from "../../UIEventSource"
/**

View file

@ -1,4 +1,4 @@
import FeatureSource, { FeatureSourceForLayer } from "../FeatureSource"
import { FeatureSource , FeatureSourceForLayer } from "../FeatureSource"
import { Feature } from "geojson"
import { BBox } from "../../BBox"
import { GeoOperations } from "../../GeoOperations"

View file

@ -1,4 +1,4 @@
import FeatureSource from "../FeatureSource"
import { FeatureSource } from "../FeatureSource"
import { Feature } from "geojson"
import TileLocalStorage from "./TileLocalStorage"
import { GeoOperations } from "../../GeoOperations"

View file

@ -3,7 +3,7 @@ import FilteredLayer from "../../Models/FilteredLayer"
import { BBox } from "../BBox"
import { Feature } from "geojson"
export default interface FeatureSource {
export interface FeatureSource {
features: Store<Feature[]>
}
export interface WritableFeatureSource extends FeatureSource {

View file

@ -1,10 +1,9 @@
import FeatureSource, { FeatureSourceForLayer } from "./FeatureSource"
import { FeatureSource, FeatureSourceForLayer } from "./FeatureSource"
import FilteredLayer from "../../Models/FilteredLayer"
import SimpleFeatureSource from "./Sources/SimpleFeatureSource"
import { Feature } from "geojson"
import { Utils } from "../../Utils"
import { UIEventSource } from "../UIEventSource"
import { feature } from "@turf/turf"
/**
* In some rare cases, some elements are shown on multiple layers (when 'passthrough' is enabled)
@ -26,7 +25,7 @@ export default class PerLayerFeatureSourceSplitter<
const knownLayers = new Map<string, T>()
this.perLayer = knownLayers
const layerSources = new Map<string, UIEventSource<Feature[]>>()
console.log("PerLayerFeatureSourceSplitter got layers", layers)
const constructStore =
options?.constructStore ?? ((store, layer) => new SimpleFeatureSource(layer, store))
for (const layer of layers) {

View file

@ -1,4 +1,4 @@
import FeatureSource from "../FeatureSource"
import { FeatureSource } from "../FeatureSource"
import { Feature, Polygon } from "geojson"
import StaticFeatureSource from "./StaticFeatureSource"
import { GeoOperations } from "../../GeoOperations"

View file

@ -1,5 +1,5 @@
import { Store, UIEventSource } from "../../UIEventSource"
import FeatureSource, { IndexedFeatureSource } from "../FeatureSource"
import { FeatureSource , IndexedFeatureSource } from "../FeatureSource"
import { Feature } from "geojson"
import { Utils } from "../../../Utils"

View file

@ -1,6 +1,6 @@
import { Store, UIEventSource } from "../../UIEventSource"
import FilteredLayer from "../../../Models/FilteredLayer"
import FeatureSource from "../FeatureSource"
import { FeatureSource } from "../FeatureSource"
import { TagsFilter } from "../../Tags/TagsFilter"
import { Feature } from "geojson"
import { GlobalFilter } from "../../../Models/GlobalFilter"
@ -73,21 +73,9 @@ export default class FilteringFeatureSource implements FeatureSource {
return false
}
for (const filter of layer.layerDef.filters) {
const state = layer.appliedFilters.get(filter.id).data
if (state === undefined) {
continue
}
let neededTags: TagsFilter
if (typeof state === "string") {
// This filter uses fields
} else {
neededTags = filter.options[state].osmTags
}
if (neededTags !== undefined && !neededTags.matchesProperties(f.properties)) {
// Hidden by the filter on the layer itself - we want to hide it no matter what
return false
}
let neededTags: TagsFilter = layer.currentFilter.data
if (neededTags !== undefined && !neededTags.matchesProperties(f.properties)) {
return false
}
for (const globalFilter of globalFilters ?? []) {

View file

@ -3,7 +3,7 @@
*/
import { Store, UIEventSource } from "../../UIEventSource"
import { Utils } from "../../../Utils"
import FeatureSource from "../FeatureSource"
import { FeatureSource } from "../FeatureSource"
import { BBox } from "../../BBox"
import { GeoOperations } from "../../GeoOperations"
import { Feature } from "geojson"

View file

@ -1,21 +1,23 @@
import LayoutConfig from "../../../Models/ThemeConfig/LayoutConfig"
import FeatureSource from "../FeatureSource"
import { ImmutableStore, Store } from "../../UIEventSource"
import { WritableFeatureSource } from "../FeatureSource"
import { ImmutableStore, Store, UIEventSource } from "../../UIEventSource"
import { Feature, Point } from "geojson"
import { TagUtils } from "../../Tags/TagUtils"
import BaseUIElement from "../../../UI/BaseUIElement"
import { Utils } from "../../../Utils"
import { regex_not_newline_characters } from "svelte/types/compiler/utils/patterns"
import { render } from "sass"
/**
* Highly specialized feature source.
* Based on a lon/lat UIEVentSource, will generate the corresponding feature with the correct properties
*/
export class LastClickFeatureSource implements FeatureSource {
features: Store<Feature[]>
export class LastClickFeatureSource implements WritableFeatureSource {
public readonly features: UIEventSource<Feature[]> = new UIEventSource<Feature[]>([])
/**
* Must be public: passed as tags into the selected view
*/
public properties: Record<string, string>
constructor(location: Store<{ lon: number; lat: number }>, layout: LayoutConfig) {
const allPresets: BaseUIElement[] = []
for (const layer of layout.layers)
@ -43,15 +45,16 @@ export class LastClickFeatureSource implements FeatureSource {
first_preset: renderings[0],
}
this.properties = properties
this.features = location.mapD(({ lon, lat }) => [
<Feature<Point>>{
location.addCallbackAndRunD(({ lon, lat }) => {
const point = <Feature<Point>>{
type: "Feature",
properties,
geometry: {
type: "Point",
coordinates: [lon, lat],
},
},
])
}
this.features.setData([point])
})
}
}

View file

@ -1,15 +1,16 @@
import GeoJsonSource from "./GeoJsonSource"
import LayerConfig from "../../../Models/ThemeConfig/LayerConfig"
import FeatureSource from "../FeatureSource"
import { FeatureSource } from "../FeatureSource"
import { Or } from "../../Tags/Or"
import FeatureSwitchState from "../../State/FeatureSwitchState"
import OverpassFeatureSource from "./OverpassFeatureSource"
import { Store } from "../../UIEventSource"
import { ImmutableStore, Store } from "../../UIEventSource"
import OsmFeatureSource from "./OsmFeatureSource"
import FeatureSourceMerger from "./FeatureSourceMerger"
import DynamicGeoJsonTileSource from "../TiledFeatureSource/DynamicGeoJsonTileSource"
import { BBox } from "../../BBox"
import LocalStorageFeatureSource from "../TiledFeatureSource/LocalStorageFeatureSource"
import StaticFeatureSource from "./StaticFeatureSource"
/**
* This source will fetch the needed data from various sources for the given layout.
@ -78,6 +79,9 @@ export default class LayoutSource extends FeatureSourceMerger {
backend: string,
featureSwitches: FeatureSwitchState
): FeatureSource {
if (osmLayers.length == 0) {
return new StaticFeatureSource(new ImmutableStore([]))
}
const minzoom = Math.min(...osmLayers.map((layer) => layer.minzoom))
const isActive = zoom.mapD((z) => {
if (z < minzoom) {
@ -107,6 +111,9 @@ export default class LayoutSource extends FeatureSourceMerger {
zoom: Store<number>,
featureSwitches: FeatureSwitchState
): FeatureSource {
if (osmLayers.length == 0) {
return new StaticFeatureSource(new ImmutableStore([]))
}
const minzoom = Math.min(...osmLayers.map((layer) => layer.minzoom))
const isActive = zoom.mapD((z) => {
if (z < minzoom) {

View file

@ -1,6 +1,6 @@
import { Changes } from "../../Osm/Changes"
import { OsmNode, OsmObject, OsmRelation, OsmWay } from "../../Osm/OsmObject"
import FeatureSource from "../FeatureSource"
import { FeatureSource } from "../FeatureSource"
import { UIEventSource } from "../../UIEventSource"
import { ChangeDescription } from "../../Osm/Actions/ChangeDescription"
import { ElementStorage } from "../../ElementStorage"

View file

@ -1,5 +1,5 @@
import { Feature } from "geojson"
import FeatureSource from "../FeatureSource"
import { FeatureSource } from "../FeatureSource"
import { ImmutableStore, Store, UIEventSource } from "../../UIEventSource"
import LayerConfig from "../../../Models/ThemeConfig/LayerConfig"
import { Or } from "../../Tags/Or"

View file

@ -1,37 +1,69 @@
import FeatureSource from "../FeatureSource"
import { Store } from "../../UIEventSource"
import { FeatureSource } from "../FeatureSource"
import { Store, UIEventSource } from "../../UIEventSource"
import { Feature, Point } from "geojson"
import { GeoOperations } from "../../GeoOperations"
import { BBox } from "../../BBox"
export interface SnappingOptions {
/**
* If the distance is bigger then this amount, don't snap.
* In meter
*/
maxDistance?: number
maxDistance: number
allowUnsnapped?: false | boolean
/**
* The snapped-to way will be written into this
*/
snappedTo?: UIEventSource<string>
/**
* The resulting snap coordinates will be written into this UIEventSource
*/
snapLocation?: UIEventSource<{ lon: number; lat: number }>
}
export default class SnappingFeatureSource implements FeatureSource {
public readonly features: Store<Feature<Point>[]>
private readonly _snappedTo: UIEventSource<string>
public readonly snappedTo: Store<string>
constructor(
snapTo: FeatureSource,
location: Store<{ lon: number; lat: number }>,
options?: SnappingOptions
options: SnappingOptions
) {
const simplifiedFeatures = snapTo.features.mapD((features) =>
features
.filter((feature) => feature.geometry.type !== "Point")
.map((f) => GeoOperations.forceLineString(<any>f))
)
const maxDistance = options?.maxDistance
this._snappedTo = options.snappedTo ?? new UIEventSource<string>(undefined)
this.snappedTo = this._snappedTo
const simplifiedFeatures = snapTo.features
.mapD((features) =>
features
.filter((feature) => feature.geometry.type !== "Point")
.map((f) => GeoOperations.forceLineString(<any>f))
)
.map(
(features) => {
const { lon, lat } = location.data
const loc: [number, number] = [lon, lat]
return features.filter((f) => BBox.get(f).isNearby(loc, maxDistance))
},
[location]
)
location.mapD(
this.features = location.mapD(
({ lon, lat }) => {
const features = snapTo.features.data
const features = simplifiedFeatures.data
const loc: [number, number] = [lon, lat]
const maxDistance = (options?.maxDistance ?? 1000) * 1000
const maxDistance = (options?.maxDistance ?? 1000) / 1000
let bestSnap: Feature<Point, { "snapped-to": string; dist: number }> = undefined
for (const feature of features) {
if (feature.geometry.type !== "LineString") {
// TODO handle Polygons with holes
continue
}
const snapped = GeoOperations.nearestPoint(<any>feature, loc)
if (snapped.properties.dist > maxDistance) {
continue
@ -44,7 +76,23 @@ export default class SnappingFeatureSource implements FeatureSource {
bestSnap = <any>snapped
}
}
return bestSnap
this._snappedTo.setData(bestSnap?.properties?.["snapped-to"])
if (bestSnap === undefined && options?.allowUnsnapped) {
bestSnap = {
type: "Feature",
geometry: {
type: "Point",
coordinates: [lon, lat],
},
properties: {
"snapped-to": undefined,
dist: -1,
},
}
}
const c = bestSnap.geometry.coordinates
options?.snapLocation?.setData({ lon: c[0], lat: c[1] })
return [bestSnap]
},
[snapTo.features]
)

View file

@ -1,4 +1,4 @@
import FeatureSource, { FeatureSourceForLayer, Tiled } from "../FeatureSource"
import { FeatureSource , FeatureSourceForLayer, Tiled } from "../FeatureSource"
import { ImmutableStore, Store } from "../../UIEventSource"
import FilteredLayer from "../../../Models/FilteredLayer"
import { BBox } from "../../BBox"

View file

@ -1,4 +1,4 @@
import FeatureSource, { FeatureSourceForLayer } from "../FeatureSource"
import {FeatureSource, FeatureSourceForLayer } from "../FeatureSource"
import StaticFeatureSource from "./StaticFeatureSource"
import { GeoOperations } from "../../GeoOperations"
import { BBox } from "../../BBox"

View file

@ -81,6 +81,7 @@ export default class DynamicGeoJsonTileSource extends DynamicTileSource {
return new GeoJsonSource(layer, {
zxy,
featureIdBlacklist: blackList,
isActive: options?.isActive,
})
},
mapProperties,

View file

@ -1,7 +1,7 @@
import { Store, Stores } from "../../UIEventSource"
import { Tiles } from "../../../Models/TileRange"
import { BBox } from "../../BBox"
import FeatureSource from "../FeatureSource"
import { FeatureSource } from "../FeatureSource"
import FeatureSourceMerger from "../Sources/FeatureSourceMerger"
/***
@ -26,10 +26,6 @@ export default class DynamicTileSource extends FeatureSourceMerger {
mapProperties.bounds
.mapD(
(bounds) => {
if (options?.isActive?.data === false) {
// No need to download! - the layer is disabled
return undefined
}
const tileRange = Tiles.TileRangeBetween(
zoomlevel,
bounds.getNorth(),

View file

@ -1,4 +1,4 @@
import FeatureSource, { FeatureSourceForLayer, Tiled } from "../FeatureSource"
import {FeatureSource, FeatureSourceForLayer, Tiled } from "../FeatureSource"
import { OsmNode, OsmObject, OsmWay } from "../../Osm/OsmObject"
import SimpleFeatureSource from "../Sources/SimpleFeatureSource"
import FilteredLayer from "../../../Models/FilteredLayer"