From 1373bd106e5937c4669acdac97dde427d024f770 Mon Sep 17 00:00:00 2001 From: Pieter Vander Vennet Date: Wed, 22 Jul 2020 00:50:30 +0200 Subject: [PATCH] Add better way handling --- Customizations/LayerDefinition.ts | 15 +++++++++++++-- Customizations/Layers/BikeParkings.ts | 1 + Customizations/Layers/BikeShops.ts | 1 + Customizations/Layers/BikeStations.ts | 1 + Customizations/Layers/DrinkingWater.ts | 1 + Customizations/Layouts/Cyclofix.ts | 2 -- Logic/FilteredLayer.ts | 18 +++++++++++++++--- Logic/GeoOperations.ts | 9 +++++++++ UI/SimpleAddUI.ts | 7 +++---- index.ts | 4 ++-- 10 files changed, 46 insertions(+), 13 deletions(-) diff --git a/Customizations/LayerDefinition.ts b/Customizations/LayerDefinition.ts index 138102c..43e6169 100644 --- a/Customizations/LayerDefinition.ts +++ b/Customizations/LayerDefinition.ts @@ -15,7 +15,7 @@ export class LayerDefinition { /** * This name is shown in the 'add XXX button' */ - name: string; + name: string | UIElement; /** * These tags are added whenever a new point is added by the user on the map. * This is the ideal place to add extra info, such as "fixme=added by MapComplete, geometry should be checked" @@ -72,7 +72,15 @@ export class LayerDefinition { */ maxAllowedOverlapPercentage: number = undefined; - + /** + * If true, then ways (and polygons) will be converted to a 'point' at the center instead before further processing + */ + wayHandling: number = 0; + + static WAYHANDLING_DEFAULT = 0; + static WAYHANDLING_CENTER_ONLY = 1; + static WAYHANDLING_CENTER_AND_WAY = 2; + constructor(options: { name: string, newElementTags: Tag[], @@ -82,6 +90,7 @@ export class LayerDefinition { title?: TagRenderingOptions, elementsToShow?: TagDependantUIElementConstructor[], maxAllowedOverlapPercentage?: number, + waysToCenterPoints?: boolean, style?: (tags: any) => { color: string, icon: any @@ -99,6 +108,7 @@ export class LayerDefinition { this.title = options.title; this.elementsToShow = options.elementsToShow; this.style = options.style; + this.wayHandling = options.waysToCenterPoints ?? LayerDefinition.WAYHANDLING_DEFAULT; } asLayer(basemap: Basemap, allElements: ElementStorage, changes: Changes, userDetails: UIEventSource, selectedElement: UIEventSource, @@ -109,6 +119,7 @@ export class LayerDefinition { basemap, allElements, changes, this.overpassFilter, this.maxAllowedOverlapPercentage, + this.wayHandling, this.style, selectedElement, showOnPopup); diff --git a/Customizations/Layers/BikeParkings.ts b/Customizations/Layers/BikeParkings.ts index 28d5ebf..3f07869 100644 --- a/Customizations/Layers/BikeParkings.ts +++ b/Customizations/Layers/BikeParkings.ts @@ -29,6 +29,7 @@ export default class BikeParkings extends LayerDefinition { //new ParkingOperator(), new ParkingType() ]; + this.wayHandling = LayerDefinition.WAYHANDLING_CENTER_AND_WAY; } diff --git a/Customizations/Layers/BikeShops.ts b/Customizations/Layers/BikeShops.ts index bbba24d..0cec27f 100644 --- a/Customizations/Layers/BikeShops.ts +++ b/Customizations/Layers/BikeShops.ts @@ -27,6 +27,7 @@ export default class BikeShops extends LayerDefinition { new Tag("shop", "bicycle"), ] this.maxAllowedOverlapPercentage = 10 + this.wayHandling = LayerDefinition.WAYHANDLING_CENTER_AND_WAY this.minzoom = 13; this.style = this.generateStyleFunction(); diff --git a/Customizations/Layers/BikeStations.ts b/Customizations/Layers/BikeStations.ts index 5239833..2fd80c2 100644 --- a/Customizations/Layers/BikeStations.ts +++ b/Customizations/Layers/BikeStations.ts @@ -38,6 +38,7 @@ export default class BikeStations extends LayerDefinition { this.minzoom = 13; this.style = this.generateStyleFunction(); this.title = new FixedText(Translations.t.cyclofix.station.title) + this.wayHandling = LayerDefinition.WAYHANDLING_CENTER_AND_WAY this.elementsToShow = [ new ImageCarouselWithUploadConstructor(), diff --git a/Customizations/Layers/DrinkingWater.ts b/Customizations/Layers/DrinkingWater.ts index 7bdd632..1dd0c89 100644 --- a/Customizations/Layers/DrinkingWater.ts +++ b/Customizations/Layers/DrinkingWater.ts @@ -24,6 +24,7 @@ export class DrinkingWater extends LayerDefinition { new Tag("amenity", "drinking_water"), ]; this.maxAllowedOverlapPercentage = 10; + this.wayHandling = LayerDefinition.WAYHANDLING_CENTER_AND_WAY this.minzoom = 13; this.style = this.generateStyleFunction(); diff --git a/Customizations/Layouts/Cyclofix.ts b/Customizations/Layouts/Cyclofix.ts index 4adbae3..82d56dd 100644 --- a/Customizations/Layouts/Cyclofix.ts +++ b/Customizations/Layouts/Cyclofix.ts @@ -2,10 +2,8 @@ import {Layout} from "../Layout"; import BikeParkings from "../Layers/BikeParkings"; import BikeServices from "../Layers/BikeStations"; import BikeShops from "../Layers/BikeShops"; -import {GhostBike} from "../Layers/GhostBike"; import Translations from "../../UI/i18n/Translations"; import {DrinkingWater} from "../Layers/DrinkingWater"; -import {BikeShop} from "../Layers/BikeShop" import Combine from "../../UI/Base/Combine"; diff --git a/Logic/FilteredLayer.ts b/Logic/FilteredLayer.ts index 76a9d9c..fcb60c3 100644 --- a/Logic/FilteredLayer.ts +++ b/Logic/FilteredLayer.ts @@ -6,6 +6,7 @@ import { Changes } from "./Changes"; import L from "leaflet" import { GeoOperations } from "./GeoOperations"; import { UIElement } from "../UI/UIElement"; +import {LayerDefinition} from "../Customizations/LayerDefinition"; /*** * A filtered layer is a layer which offers a 'set-data' function @@ -18,7 +19,7 @@ import { UIElement } from "../UI/UIElement"; */ export class FilteredLayer { - public readonly name: string; + public readonly name: string | UIElement; public readonly filters: TagsFilter; public readonly isDisplayed: UIEventSource = new UIEventSource(true); @@ -32,6 +33,7 @@ export class FilteredLayer { /** The featurecollection from overpass */ private _dataFromOverpass; + private _wayHandling: number; /** List of new elements, geojson features */ private _newElements = []; @@ -43,15 +45,17 @@ export class FilteredLayer { private _showOnPopup: (tags: UIEventSource) => UIElement; constructor( - name: string, + name: string | UIElement, map: Basemap, storage: ElementStorage, changes: Changes, filters: TagsFilter, maxAllowedOverlap: number, + wayHandling: number, style: ((properties) => any), selectedElement: UIEventSource, showOnPopup: ((tags: UIEventSource) => UIElement) ) { + this._wayHandling = wayHandling; this._selectedElement = selectedElement; this._showOnPopup = showOnPopup; @@ -66,6 +70,7 @@ export class FilteredLayer { this._style = style; this._storage = storage; this._maxAllowedOverlap = maxAllowedOverlap; + const self = this; this.isDisplayed.addCallback(function (isDisplayed) { if (self._geolayer !== undefined && self._geolayer !== null) { @@ -86,10 +91,17 @@ export class FilteredLayer { public SetApplicableData(geojson: any): any { const leftoverFeatures = []; const selfFeatures = []; - for (const feature of geojson.features) { + for (let feature of geojson.features) { // feature.properties contains all the properties var tags = TagUtils.proprtiesToKV(feature.properties); if (this.filters.matches(tags)) { + if(feature.geometry.type !== "Point"){ + if(this._wayHandling === LayerDefinition.WAYHANDLING_CENTER_AND_WAY){ + selfFeatures.push(GeoOperations.centerpoint(feature)); + }else if(this._wayHandling === LayerDefinition.WAYHANDLING_CENTER_ONLY){ + feature = GeoOperations.centerpoint(feature); + } + } selfFeatures.push(feature); } else { leftoverFeatures.push(feature); diff --git a/Logic/GeoOperations.ts b/Logic/GeoOperations.ts index 91687e9..4ed677e 100644 --- a/Logic/GeoOperations.ts +++ b/Logic/GeoOperations.ts @@ -6,6 +6,15 @@ export class GeoOperations { return turf.area(feature); } + static centerpoint(feature: any) + { + const newFeature= turf.center(feature); + newFeature.properties = feature.properties; + newFeature.id = feature.id; + + return newFeature; + } + static featureIsContainedInAny(feature: any, shouldNotContain: any[], maxOverlapPercentage: number): boolean { diff --git a/UI/SimpleAddUI.ts b/UI/SimpleAddUI.ts index 2aa3e0e..80f7743 100644 --- a/UI/SimpleAddUI.ts +++ b/UI/SimpleAddUI.ts @@ -25,7 +25,7 @@ export class SimpleAddUI extends UIElement { selectedElement: UIEventSource, dataIsLoading: UIEventSource, userDetails: UIEventSource, - addButtons: { name: string; icon: string; tags: Tag[]; layerToAddTo: FilteredLayer }[], + addButtons: { name: UIElement; icon: string; tags: Tag[]; layerToAddTo: FilteredLayer }[], ) { super(zoomlevel); this._zoomlevel = zoomlevel; @@ -42,17 +42,16 @@ export class SimpleAddUI extends UIElement { //