Refactoring osm tags (wip)

This commit is contained in:
Eric van der Vlist 2023-06-09 15:23:08 +02:00
parent 92fb5f7bac
commit 29f92dca10
5 changed files with 34 additions and 125 deletions

View File

@ -22,7 +22,7 @@ import osmIcons from './osm-icons';
import { getZoomInteger } from '../map/Map'; import { getZoomInteger } from '../map/Map';
import { import {
getHighlightedTagValue, getHighlightedTagValue,
getTagValue, getTagType,
} from '../map-tile-provider/MapTileProvider'; } from '../map-tile-provider/MapTileProvider';
import { getCenter } from 'ol/extent'; import { getCenter } from 'ol/extent';
import { getVectorTileFeatureType } from '../overlays/overlay-definitions'; import { getVectorTileFeatureType } from '../overlays/overlay-definitions';
@ -579,7 +579,7 @@ const styles = {
name: feature.get('name'), name: feature.get('name'),
poiType: isHighlightedFeature poiType: isHighlightedFeature
? highlightedTagValue ? highlightedTagValue
: getTagValue(feature), : getTagType(feature),
isHighlighted: isHighlightedFeature, isHighlighted: isHighlightedFeature,
isTextHidden: getZoomInteger() < 19, isTextHidden: getZoomInteger() < 19,
}; };

View File

@ -16,7 +16,7 @@ import { Browser } from '@capacitor/browser';
import { isHighlighted } from '../map-tile-provider'; import { isHighlighted } from '../map-tile-provider';
import { import {
getHighlightedTagValue, getHighlightedTagValue,
getTagValue, getTagType,
} from '../map-tile-provider/MapTileProvider'; } from '../map-tile-provider/MapTileProvider';
import style from '../gpx/styles'; import style from '../gpx/styles';
import OsmFindAll from './OsmFindAll'; import OsmFindAll from './OsmFindAll';
@ -219,7 +219,7 @@ const Infos: Component<{}> = (props) => {
highlightedTagValue: highlightedTagValue:
getHighlightedTagValue(feature), getHighlightedTagValue(feature),
isHighlighted: isHighlighted(feature), isHighlighted: isHighlighted(feature),
tagValue: getTagValue(feature), tagValue: getTagType(feature),
properties: feature.getProperties(), properties: feature.getProperties(),
style: style(feature), style: style(feature),
})} })}

View File

@ -25,8 +25,10 @@ import getUri from '../../lib/ids';
import { Feature } from 'ol'; import { Feature } from 'ol';
import { import {
overlayCategories, overlayDefinitions,
legacyOverlayDefinitions, legacyOverlayDefinitions,
osmTags,
OsmTag,
} from '../overlays/overlay-definitions'; } from '../overlays/overlay-definitions';
import { cloneDeep } from 'lodash'; import { cloneDeep } from 'lodash';
@ -126,65 +128,6 @@ const defaultOverlays: Overlays = {
none: { selected: true, highlighted: { none: [] }, hidden: {} }, none: { selected: true, highlighted: { none: [] }, hidden: {} },
}; };
// type OverlayDefinition = Record<string, FeatureTypes>;
// type OverlayDefinitions = Record<string, OverlayDefinition>;
// export const legacyOverlayDefinitions: OverlayDefinitions = {
// none: {},
// hiking: {
// none: {},
// sleeping: {
// tourism: [
// 'hotel',
// 'alpine_hut',
// 'apartment',
// 'camp_site',
// 'chalet',
// 'guest_house',
// 'hostel',
// 'motel',
// 'wilderness_hut',
// ],
// },
// drinking: {
// amenity: ['bar', 'cafe', 'pub', 'drinking_water', 'water_point'],
// natural: ['spring'],
// },
// eating: {
// amenity: ['fast_food', 'pub', 'restaurant'],
// shop: [
// 'bakery',
// 'butcher',
// 'cheese',
// 'chocolate',
// 'convenience',
// 'dairy',
// 'farm',
// 'greengrocer',
// 'health_food',
// 'pastry',
// 'seafood',
// 'department_store',
// 'supermarket',
// ],
// },
// health: {
// amenity: ['doctors', 'hospital', 'pharmacy'],
// },
// security: {
// amenity: ['police', 'fire_station'],
// },
// dayToday: {
// amenity: ['waste_basket', 'waste_disposal'],
// shop: ['laundry'],
// },
// naturalSites: {
// tourism: ['viewpoint'],
// natural: '*',
// },
// },
// };
const getOverlays = createCachedSignal({ const getOverlays = createCachedSignal({
id, id,
method: 'getOverlays', method: 'getOverlays',
@ -199,12 +142,14 @@ const currentOverlayKey = () =>
const currentOverlay = () => const currentOverlay = () =>
getOverlays() ? getOverlays()[currentOverlayKey()] : {}; getOverlays() ? getOverlays()[currentOverlayKey()] : {};
const overlayCategoriesPlusNone = { none: [], ...overlayCategories }; const overlayDefinitionsPlusNone = { none: {}, ...overlayDefinitions };
export const currentCategory = () => export const currentCategory = () =>
Object.keys(
currentOverlayKey() === 'none' currentOverlayKey() === 'none'
? [] ? {}
: ['none', ...overlayCategoriesPlusNone[currentOverlayKey()]]; : { none: [], ...overlayDefinitionsPlusNone[currentOverlayKey()] }
);
const currentOverlayHighlightedKey = () => const currentOverlayHighlightedKey = () =>
currentOverlay() && currentOverlay().highlighted currentOverlay() && currentOverlay().highlighted
@ -214,7 +159,6 @@ const currentOverlayHighlightedKey = () =>
export const currentOverlayHighlightedDefinition = () => export const currentOverlayHighlightedDefinition = () =>
currentCategory()[currentOverlayHighlightedKey()]; currentCategory()[currentOverlayHighlightedKey()];
export const highlightedTags = () => { export const highlightedTags = () => {
let result = {}; let result = {};
Object.keys(legacyOverlayDefinitions).forEach((tagName) => { Object.keys(legacyOverlayDefinitions).forEach((tagName) => {
@ -310,34 +254,15 @@ export const isHighlighted = (feature: Feature) => {
return !!getHighlightedTagValue(feature); return !!getHighlightedTagValue(feature);
}; };
export const getTagValue = (feature: Feature) => { export const getTagType = (feature: Feature) => {
let result = false; for (const tagType of Object.keys(osmTags)) {
Object.keys(legacyOverlayDefinitions).every((tagName) => { const tag: OsmTag = osmTags[tagType];
const value = feature.get(tagName); const tagValue = feature.get(tag.name);
const tagValues = legacyOverlayDefinitions[tagName]; if (tagValue != undefined) {
return tagType;
if (value !== undefined && Object.keys(tagValues).includes(value)) {
// console.log({
// caller: 'MapTileProviders / tagValue / found',
// feature,
// feature_values: feature.values_,
// highlightedTags,
// tagName,
// tag_value: value,
// highlightedValues,
// });
result = value;
return false;
} }
return true; }
}); return false;
// console.log({
// caller: 'MapTileProviders / isHighlighted / false',
// feature,
// feature_values: feature.values_,
// highlightedTags,
// });
return result;
}; };
const MapTilesProvider: Component<{}> = (props) => { const MapTilesProvider: Component<{}> = (props) => {
@ -434,7 +359,7 @@ const MapTilesProvider: Component<{}> = (props) => {
defaultValue={currentOverlayKey()} defaultValue={currentOverlayKey()}
onChange={handleOverlayChange} onChange={handleOverlayChange}
> >
<For each={Object.keys(overlayCategoriesPlusNone)}> <For each={Object.keys(overlayDefinitionsPlusNone)}>
{(p: string) => ( {(p: string) => (
<FormControlLabel value={p} control={<Radio />} label={p} /> <FormControlLabel value={p} control={<Radio />} label={p} />
)} )}

View File

@ -1 +1,6 @@
import { legacyOverlayDefinitions, overlayCategories } from './overlay-definitions'; import {
legacyOverlayDefinitions,
overlayDefinitions,
OsmTag,
osmTags,
} from './overlay-definitions';

View File

@ -1,6 +1,11 @@
import { indexOf } from 'lodash'; import { indexOf } from 'lodash';
import { Feature } from 'ol'; import { Feature } from 'ol';
export interface OsmTag {
name: string;
value: string;
}
export const osmTags = { export const osmTags = {
bar: { name: 'amenity', value: 'bar' }, bar: { name: 'amenity', value: 'bar' },
fast_food: { name: 'amenity', value: 'fast_food' }, fast_food: { name: 'amenity', value: 'fast_food' },
@ -563,31 +568,6 @@ export const legacyOverlayDefinitions = {
}, },
}; };
let _flat: any = [];
Object.values(legacyOverlayDefinitions).forEach((category) => {
Object.values(category).forEach((subCategory) => {
_flat = [..._flat, subCategory];
});
});
type Categories = { string: Set<string> };
export let overlayCategories: Categories = {};
_flat.forEach((obj: any) => {
Object.keys(obj).forEach((catName: string) => {
const catValue = obj[catName];
const previous =
overlayCategories[catName] === undefined
? []
: overlayCategories[catName];
overlayCategories[catName] = new Set([
...previous,
...Object.keys(catValue),
]);
});
});
export const getVectorTileFeatureType = (feature: Feature) => { export const getVectorTileFeatureType = (feature: Feature) => {
if ( if (
['hiking'].includes(feature.get('route')) || ['hiking'].includes(feature.get('route')) ||
@ -608,6 +588,5 @@ export const getVectorTileFeatureType = (feature: Feature) => {
console.log({ console.log({
caller: 'overlay-definitions', caller: 'overlay-definitions',
legacyOverlayDefinitions, legacyOverlayDefinitions,
_flat, overlayDefinitions,
overlayCategories,
}); });