Refactor isShown into a tagsfilter
This commit is contained in:
		
							parent
							
								
									dab0565a8b
								
							
						
					
					
						commit
						b8bca0287d
					
				
					 13 changed files with 64 additions and 123 deletions
				
			
		|  | @ -4,6 +4,8 @@ import {FeatureSourceForLayer, Tiled} from "../FeatureSource"; | |||
| import {BBox} from "../../BBox"; | ||||
| import {ElementStorage} from "../../ElementStorage"; | ||||
| import {TagsFilter} from "../../Tags/TagsFilter"; | ||||
| import {tag} from "@turf/turf"; | ||||
| import {OsmFeature} from "../../../Models/OsmFeature"; | ||||
| 
 | ||||
| export default class FilteringFeatureSource implements FeatureSourceForLayer, Tiled { | ||||
|     public features: UIEventSource<{ feature: any; freshness: Date }[]> = | ||||
|  | @ -65,22 +67,17 @@ export default class FilteringFeatureSource implements FeatureSourceForLayer, Ti | |||
|     private update() { | ||||
|         const self = this; | ||||
|         const layer = this.upstream.layer; | ||||
|         const features: { feature: any; freshness: Date }[] = (this.upstream.features.data ?? []); | ||||
|         const features: { feature: OsmFeature; freshness: Date }[] = (this.upstream.features.data ?? []); | ||||
|         const includedFeatureIds = new Set<string>(); | ||||
|         const newFeatures = (features ?? []).filter((f) => { | ||||
| 
 | ||||
|             self.registerCallback(f.feature) | ||||
| 
 | ||||
|             const isShown = layer.layerDef.isShown; | ||||
|             const isShown: TagsFilter = layer.layerDef.isShown; | ||||
|             const tags = f.feature.properties; | ||||
|             if (isShown.IsKnown(tags)) { | ||||
|                 const result = layer.layerDef.isShown.GetRenderValue( | ||||
|                     f.feature.properties | ||||
|                 ).txt; | ||||
|                 if (result !== "yes") { | ||||
|             if (isShown !== undefined && !isShown.matchesProperties(tags) ) { | ||||
|                 return false; | ||||
|             } | ||||
|             } | ||||
| 
 | ||||
|             const tagsFilter = Array.from(layer.appliedFilters?.data?.values() ?? []) | ||||
|             for (const filter of tagsFilter) { | ||||
|  |  | |||
|  | @ -257,6 +257,20 @@ export class TagUtils { | |||
|         } | ||||
|     } | ||||
| 
 | ||||
|     /** | ||||
|      * Same as `.Tag`, except that this will return undefined if the json is undefined | ||||
|      * @param json | ||||
|      * @param context | ||||
|      * @constructor | ||||
|      */ | ||||
|     public static TagD(json?: TagConfigJson, context: string = ""): TagsFilter | undefined { | ||||
|         if(json === undefined){ | ||||
|             return undefined | ||||
|         } | ||||
|         return TagUtils.Tag(json, context) | ||||
|     } | ||||
|      | ||||
|      | ||||
|     /** | ||||
|      * INLINE sort of the given list | ||||
|      */ | ||||
|  |  | |||
|  | @ -103,23 +103,11 @@ export default class CreateNoteImportLayer extends Conversion<LayerConfigJson, L | |||
|                 "_tags=(() => {let lines = feat.get('comments')[0].text.split('\\n').map(l => l.trim()); lines.splice(0, feat.get('_trigger_index') + 1); lines = lines.filter(l => l != ''); return lines.join(';');})()" | ||||
|             ], | ||||
|             "isShown": { | ||||
|                 "render": "no", | ||||
|                 "mappings": [ | ||||
|                     { | ||||
|                         "if": "comments!~.*https://mapcomplete.osm.be.*", | ||||
|                         "then": "no" | ||||
|                     }, | ||||
|                     { | ||||
|                         "if": { | ||||
|                 and: | ||||
|                     ["_trigger_index~*", | ||||
|                         {or: isShownIfAny} | ||||
|                     ] | ||||
|             }, | ||||
|                         "then": "yes" | ||||
|                     } | ||||
|                 ] | ||||
|             }, | ||||
|             "titleIcons": [ | ||||
|                 { | ||||
|                     "render": "<a href='https://openstreetmap.org/note/{id}' target='_blank'><img src='./assets/svg/osm-logo-us.svg'></a>" | ||||
|  |  | |||
|  | @ -357,7 +357,7 @@ class AddDependencyLayersToTheme extends DesugaringStep<LayoutConfigJson> { | |||
| 
 | ||||
|             for (const layerConfig of alreadyLoaded) { | ||||
|                 try { | ||||
|                     const layerDeps = DependencyCalculator.getLayerDependencies(new LayerConfig(layerConfig)) | ||||
|                     const layerDeps = DependencyCalculator.getLayerDependencies(new LayerConfig(layerConfig, themeId+"(dependencies)")) | ||||
|                     dependencies.push(...layerDeps) | ||||
|                 } catch (e) { | ||||
|                     console.error(e) | ||||
|  |  | |||
|  | @ -549,6 +549,10 @@ export class ValidateLayer extends DesugaringStep<LayerConfigJson> { | |||
|                 if (json["hideUnderlayingFeaturesMinPercentage"] !== undefined) { | ||||
|                     errors.push(context + ": layer " + json.id + " contains an old 'hideUnderlayingFeaturesMinPercentage'") | ||||
|                 } | ||||
|                  | ||||
|                 if(json.isShown !== undefined && (json.isShown["render"] !== undefined || json.isShown["mappings"] !== undefined)){ | ||||
|                     warnings.push(context + " has a tagRendering as `isShown`") | ||||
|                 } | ||||
|             } | ||||
|             { | ||||
|                 // Check location of layer file
 | ||||
|  |  | |||
|  | @ -135,7 +135,7 @@ export interface LayerConfigJson { | |||
|     doNotDownload?: boolean; | ||||
| 
 | ||||
|     /** | ||||
|      * This tag rendering should either be 'yes' or 'no'. If 'no' is returned, then the feature will be hidden from view. | ||||
|      * 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. | ||||
|  | @ -143,7 +143,7 @@ export interface LayerConfigJson { | |||
|      * | ||||
|      * The default value is 'yes' | ||||
|      */ | ||||
|     isShown?: TagRenderingConfigJson; | ||||
|     isShown?: TagConfigJson; | ||||
| 
 | ||||
|     /** | ||||
|      * Advanced option - might be set by the theme compiler | ||||
|  |  | |||
|  | @ -39,7 +39,7 @@ export default class LayerConfig extends WithContextLoader { | |||
|     public readonly calculatedTags: [string, string, boolean][]; | ||||
|     public readonly doNotDownload: boolean; | ||||
|     public readonly passAllFeatures: boolean; | ||||
|     public readonly isShown: TagRenderingConfig; | ||||
|     public readonly isShown: TagsFilter; | ||||
|     public minzoom: number; | ||||
|     public minzoomVisible: number; | ||||
|     public readonly maxzoom: number; | ||||
|  | @ -302,7 +302,7 @@ export default class LayerConfig extends WithContextLoader { | |||
|         }); | ||||
| 
 | ||||
|         this.title = this.tr("title", undefined); | ||||
|         this.isShown = this.tr("isShown", "yes"); | ||||
|         this.isShown = TagUtils.TagD(json.isShown, context+".isShown") | ||||
| 
 | ||||
|         this.deletion = null; | ||||
|         if (json.deletion === true) { | ||||
|  | @ -478,7 +478,7 @@ export default class LayerConfig extends WithContextLoader { | |||
|     } | ||||
| 
 | ||||
|     AllTagRenderings(): TagRenderingConfig[] { | ||||
|         return Utils.NoNull([...this.tagRenderings, ...this.titleIcons, this.title, this.isShown]) | ||||
|         return Utils.NoNull([...this.tagRenderings, ...this.titleIcons, this.title]) | ||||
|     } | ||||
| 
 | ||||
|     public isLeftRightSensitive(): boolean { | ||||
|  |  | |||
|  | @ -21,17 +21,9 @@ | |||
|     "_is_enclosed=feat.properties._enclosing != '[]'" | ||||
|   ], | ||||
|   "isShown": { | ||||
|     "render": "yes", | ||||
|     "mappings": [ | ||||
|       { | ||||
|         "if": { | ||||
|           "and": [ | ||||
|             "building~*", | ||||
|             "_is_enclosed=true" | ||||
|           ] | ||||
|         }, | ||||
|         "then": "no" | ||||
|       } | ||||
|     "or": [ | ||||
|       "building=", | ||||
|       "_is_enclosed!=true" | ||||
|     ] | ||||
|   }, | ||||
|   "tagRenderings": [ | ||||
|  |  | |||
|  | @ -34,7 +34,9 @@ | |||
|         "osmTags": { | ||||
|           "or": [ | ||||
|             "leisure=nature_reserve", | ||||
|             "boundary=protected_area" | ||||
|             { | ||||
|               "and": ["boundary=protected_area","protect_class!=22"] | ||||
|             } | ||||
|           ] | ||||
|         } | ||||
|       }, | ||||
|  | @ -149,15 +151,7 @@ | |||
|         "_overlapWithUpperLayers=Math.max(...feat.overlapWith('nature_reserve_buurtnatuur').map(o => o.overlap))/feat.area", | ||||
|         "_tooMuchOverlap=Number(feat.properties._overlapWithUpperLayers) > 0.1 ? 'yes' :'no'" | ||||
|       ], | ||||
|       "isShown": { | ||||
|         "render": "yes", | ||||
|         "mappings": [ | ||||
|           { | ||||
|             "if": "_tooMuchOverlap=yes", | ||||
|             "then": "no" | ||||
|           } | ||||
|         ] | ||||
|       }, | ||||
|       "isShown": "_tooMuchOverlap!=yes", | ||||
|       "title": { | ||||
|         "render": { | ||||
|           "nl": "Park" | ||||
|  | @ -257,15 +251,7 @@ | |||
|         "_overlapWithUpperLayers=Math.max(...feat.overlapWith('parks','nature_reserve_buurtnatuur').map(o => o.overlap))/feat.area", | ||||
|         "_tooMuchOverlap=Number(feat.properties._overlapWithUpperLayers) > 0.1 ? 'yes' : 'no'" | ||||
|       ], | ||||
|       "isShown": { | ||||
|         "render": "yes", | ||||
|         "mappings": [ | ||||
|           { | ||||
|             "if": "_tooMuchOverlap=yes", | ||||
|             "then": "no" | ||||
|           } | ||||
|         ] | ||||
|       }, | ||||
|       "isShown": "_tooMuchOverlap!=yes", | ||||
|       "title": { | ||||
|         "render": { | ||||
|           "nl": "Bos" | ||||
|  |  | |||
|  | @ -73,15 +73,7 @@ | |||
|           ] | ||||
|         } | ||||
|       }, | ||||
|       "isShown": { | ||||
|         "render": "yes", | ||||
|         "mappings": [ | ||||
|           { | ||||
|             "if": "id!~way/.*", | ||||
|             "then": "no" | ||||
|           } | ||||
|         ] | ||||
|       }, | ||||
|       "isShown": "id~way/.*", | ||||
|       "description": { | ||||
|         "nl": "Een fietsstraat is een straat waar gemotoriseerd verkeer een fietser niet mag inhalen", | ||||
|         "en": "A cyclestreet is a street where motorized traffic is not allowed to overtake a cyclist", | ||||
|  |  | |||
|  | @ -631,17 +631,9 @@ | |||
|         } | ||||
|       ], | ||||
|       "isShown":{ | ||||
|         "render": "yes", | ||||
|         "mappings": [ | ||||
|           { | ||||
|             "if": { | ||||
|               "and": [ | ||||
|                 "_imported_osm_object_found=true", | ||||
|                 "_imported_osm_still_fresh=true" | ||||
|               ] | ||||
|             }, | ||||
|             "then": "no" | ||||
|           } | ||||
|         "or": [ | ||||
|           "_imported_osm_object_found!=true", | ||||
|           "_imported_osm_still_fresh!=true" | ||||
|         ] | ||||
|       }, | ||||
|       "mapRendering": [ | ||||
|  |  | |||
|  | @ -117,15 +117,7 @@ | |||
|           "dashArray": "8 8" | ||||
|         } | ||||
|       ], | ||||
|       "isShown": { | ||||
|         "render": "yes", | ||||
|         "mappings": [ | ||||
|           { | ||||
|             "if": "_country!=be", | ||||
|             "then": "no" | ||||
|           } | ||||
|         ] | ||||
|       } | ||||
|       "isShown": "_country=be" | ||||
|     }, | ||||
|     { | ||||
|       "id": "wrong_postal_code", | ||||
|  | @ -196,15 +188,7 @@ | |||
|           ] | ||||
|         } | ||||
|       ], | ||||
|       "isShown": { | ||||
|         "render": "yes", | ||||
|         "mappings": [ | ||||
|           { | ||||
|             "if": "_country!=be", | ||||
|             "then": "no" | ||||
|           } | ||||
|         ] | ||||
|       } | ||||
|       "isShown": "_country=be" | ||||
|     } | ||||
|   ] | ||||
| } | ||||
|  | @ -288,14 +288,6 @@ | |||
|         "then": "./assets/themes/speelplekken/youtube.svg" | ||||
|       } | ||||
|     ], | ||||
|     "isShown": { | ||||
|       "render": "yes", | ||||
|       "mappings": [ | ||||
|         { | ||||
|           "if": "_is_shadowed=yes", | ||||
|           "then": "no" | ||||
|         } | ||||
|       ] | ||||
|     } | ||||
|     "isShown":"_is_shadowed!=yes" | ||||
|   } | ||||
| } | ||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue