Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions backend/migrations/080_backfill_trail_activities.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
-- Migration 080: Backfill and correct trail activities so the activity filter
-- (e.g. selecting "Biking") only surfaces trails that actually allow that activity.
--
-- Three parts, all idempotent (guarded so re-runs are no-ops and so later admin
-- edits are not clobbered once a row no longer matches the known-bad state):
-- 1. Untagged trails -> 'Hiking' (most are CVNP/Metro foot trails).
-- 2. Drop 'Biking' from 11 footpaths that were mis-tagged as bikeable.
-- 3. Bikeable multi-use trails on the allowlist (Towpath, Bike & Hike, Gateway,
-- etc.) keep their existing 'Hiking, Biking' tags — no change needed.

BEGIN;

-- 1. Untagged trails -> 'Hiking'
UPDATE pois
SET primary_activities = 'Hiking'
WHERE 'trail' = ANY(poi_roles)
AND coalesce(deleted, false) = false
AND coalesce(nullif(trim(primary_activities), ''), '') = '';

-- 2. Remove 'Biking' from hiking-only footpaths that were incorrectly tagged.
-- Element-wise: split on commas, drop the 'Biking' item, rejoin. Guarded by id list
-- + "still contains Biking" (Postgres word boundaries are \m \M, NOT \b), so it
-- fires once and re-runs as UPDATE 0. Gateway Trail (West Creek paved connector,
-- id 1018) and the paved/limestone multi-use trails are intentionally excluded.
UPDATE pois
SET primary_activities = (
SELECT string_agg(trim(part), ', ')
FROM unnest(string_to_array(primary_activities, ',')) AS part
WHERE lower(trim(part)) <> 'biking'
)
Comment on lines +26 to +30
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

If a trail only has 'Biking' as its primary activity, removing 'Biking' will cause the subquery to return NULL. This would set primary_activities to NULL (or empty), which in the frontend is treated as an untagged trail. Since untagged trails always pass the activity filter, this trail would incorrectly continue to show up when the "Biking" filter is active.

To prevent this, we should fallback to 'Hiking' if removing 'Biking' leaves the activities list empty.

SET primary_activities = COALESCE(
      NULLIF(
        (
          SELECT string_agg(trim(part), ', ')
          FROM unnest(string_to_array(primary_activities, ',')) AS part
          WHERE lower(trim(part)) <> 'biking'
        ),
        ''
      ),
      'Hiking'
    )

WHERE id IN (974, 1021, 1036, 1045, 1050, 1057, 1068, 1074, 1089, 1099, 1095)
AND primary_activities ~* '\mBiking\M';

COMMIT;
7 changes: 4 additions & 3 deletions frontend/src/components/Map.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { createPortal } from 'react-dom';
import { MapContainer, TileLayer, Marker, Tooltip, useMap, GeoJSON, useMapEvents, CircleMarker } from 'react-leaflet';
import L from 'leaflet';
import VirtualPoiCreator from './VirtualPoiCreator';
import { getDestinationIconTypeFromConfig, poiMatchesActivityForTypes, matchesWholeWord } from '../utils/iconUtils';
import { getDestinationIconTypeFromConfig, poiMatchesActivityForTypes, trailPassesActivityFilter, matchesWholeWord } from '../utils/iconUtils';
import { useTrip } from '../hooks/useTrip';
import { useNavigate } from 'react-router-dom';
import { generateSlug } from './sidebar/helpers';
Expand Down Expand Up @@ -636,7 +636,8 @@ function MapBoundsTracker({ destinations, visibleTypes, getDestinationIconType,
// Title search matches across all linear types, ignoring layer toggles
isLayerVisible = feature.name?.toLowerCase().includes(search);
} else if (feature.poi_roles?.includes('trail')) {
isLayerVisible = showTrails || poiMatchesActivityForTypes(feature, visibleTypes, iconConfig);
isLayerVisible = (showTrails || poiMatchesActivityForTypes(feature, visibleTypes, iconConfig))
&& trailPassesActivityFilter(feature, visibleTypes, iconConfig);
} else if (feature.poi_roles?.includes('river')) {
isLayerVisible = showRivers;
} else if (feature.poi_roles?.includes('water_taxi')) {
Expand Down Expand Up @@ -1496,7 +1497,7 @@ function Map({ destinations, selectedPoi, selectedIsLinear, onSelectPoi, isAdmin
// regardless of its layer toggle, and hide non-matches.
const isVisible = searchQuery
? feature.name?.toLowerCase().includes(searchQuery.toLowerCase())
: ((feature.poi_roles?.includes('trail') && (showTrails || poiMatchesActivityForTypes(feature, visibleTypes, iconConfig))) ||
: ((feature.poi_roles?.includes('trail') && (showTrails || poiMatchesActivityForTypes(feature, visibleTypes, iconConfig)) && trailPassesActivityFilter(feature, visibleTypes, iconConfig)) ||
(feature.poi_roles?.includes('river') && showRivers) ||
(feature.poi_roles?.includes('water_taxi') && showWaterTaxis) ||
(feature.poi_roles?.includes('boundary') && visibleBoundaries.has(feature.id)));
Expand Down
30 changes: 30 additions & 0 deletions frontend/src/utils/iconUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,36 @@ export function poiMatchesActivityForTypes(poi, visibleTypes, iconConfig) {
return false;
}

/**
* True when the legend is narrowed to a subset of activity-bearing types
* (e.g. just "Biking") rather than showing everything. The Trails layer is only
* refined by activity while this is true; with all (or no) activity types
* selected it stays all-or-nothing, preserving plain "show me the trails" browsing.
*/
export function isActivityFilterActive(visibleTypes, iconConfig) {
if (!iconConfig || iconConfig.length === 0) return false;
let activityTypes = 0, selected = 0;
for (const icon of iconConfig) {
if (icon.enabled === false || !icon.activity_fallbacks) continue;
activityTypes++;
if (visibleTypes.has(icon.name)) selected++;
}
return selected > 0 && selected < activityTypes;
}

/**
* Whether a trail (linear feature) should remain visible under the current
* activity narrowing. Untagged trails always pass, so a missing tag never
* silently hides a trail; a tagged trail passes only if it matches a selected
* activity. Selecting "Biking" thus hides hiking-only trails while the trail
* layer is on.
*/
export function trailPassesActivityFilter(feature, visibleTypes, iconConfig) {
if (!isActivityFilterActive(visibleTypes, iconConfig)) return true;
if (!(feature.primary_activities || '').trim()) return true;
return poiMatchesActivityForTypes(feature, visibleTypes, iconConfig);
}
Comment on lines +64 to +92
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Performance & Safety Improvements

  1. Redundant Computations: isActivityFilterActive is called for every single trail on every map move/zoom and render cycle. Since its result only depends on visibleTypes and iconConfig (which do not change per trail), we can cache the last computed result using simple reference equality checks. This avoids O(N * M) redundant loops over iconConfig during rendering.
  2. Defensive Programming: Added safety guards to ensure visibleTypes is a valid Set (or has a .has method) and feature is not null/undefined before accessing their properties.
let lastVisibleTypes = null;
let lastIconConfig = null;
let lastResult = false;

/**
 * True when the legend is narrowed to a subset of activity-bearing types
 * (e.g. just "Biking") rather than showing everything. The Trails layer is only
 * refined by activity while this is true; with all (or no) activity types
 * selected it stays all-or-nothing, preserving plain "show me the trails" browsing.
 */
export function isActivityFilterActive(visibleTypes, iconConfig) {
  if (!iconConfig || iconConfig.length === 0) return false;
  if (!visibleTypes || typeof visibleTypes.has !== 'function') return false;

  if (visibleTypes === lastVisibleTypes && iconConfig === lastIconConfig) {
    return lastResult;
  }

  let activityTypes = 0, selected = 0;
  for (const icon of iconConfig) {
    if (icon.enabled === false || !icon.activity_fallbacks) continue;
    activityTypes++;
    if (visibleTypes.has(icon.name)) selected++;
  }

  lastVisibleTypes = visibleTypes;
  lastIconConfig = iconConfig;
  lastResult = selected > 0 && selected < activityTypes;
  return lastResult;
}

/**
 * Whether a trail (linear feature) should remain visible under the current
 * activity narrowing. Untagged trails always pass, so a missing tag never
 * silently hides a trail; a tagged trail passes only if it matches a selected
 * activity. Selecting "Biking" thus hides hiking-only trails while the trail
 * layer is on.
 */
export function trailPassesActivityFilter(feature, visibleTypes, iconConfig) {
  if (!feature) return false;
  if (!isActivityFilterActive(visibleTypes, iconConfig)) return true;
  if (!(feature.primary_activities || '').trim()) return true;
  return poiMatchesActivityForTypes(feature, visibleTypes, iconConfig);
}


export function getIconUrlForPOI(poi, iconConfig, poiType) {
if (poiType === 'trail') return '/icons/layers/trails.svg';
if (poiType === 'river') return '/icons/layers/rivers.svg';
Expand Down
Loading