Skip to content

Commit

Permalink
DBC22-1935: added advisory layer on map
Browse files Browse the repository at this point in the history
  • Loading branch information
ray-oxd authored and fatbird committed Apr 3, 2024
1 parent d0ce93c commit 4251cf9
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 8 deletions.
34 changes: 32 additions & 2 deletions src/frontend/src/Components/Map.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ import {

// Components and functions
import CamPopup from './map/camPopup.js';
import { getCamerasLayer } from './map/layers/camerasLayer.js';
import {
getEventPopup,
getFerryPopup,
Expand All @@ -44,6 +43,8 @@ import {
import { getEvents } from './data/events.js';
import { getWeather, getRegional } from './data/weather.js';
import { getRestStops } from './data/restStops.js';
import { getAdvisoriesLayer } from './map/layers/advisoriesLayer.js';
import { getCamerasLayer } from './map/layers/camerasLayer.js';
import { getRestStopsLayer } from './map/layers/restStopsLayer.js';
import { loadEventsLayers } from './map/layers/eventsLayer.js';
import { loadWeatherLayers } from './map/layers/weatherLayer.js';
Expand Down Expand Up @@ -103,8 +104,9 @@ export default function MapWrapper({
camTimeStamp, // Cameras
events,
eventTimeStamp, // Events
advisories, // CMS
ferries,
ferriesTimeStamp, // CMS
ferriesTimeStamp, // Ferries
weather,
weatherTimeStamp, // Current Weather
regional,
Expand All @@ -125,6 +127,8 @@ export default function MapWrapper({
events: state.feeds.events.list,
eventTimeStamp: state.feeds.events.routeTimeStamp,
// CMS
advisories: state.cms.advisories.list,
// Ferries
ferries: state.feeds.ferries.list,
ferriesTimeStamp: state.feeds.ferries.routeTimeStamp,
// Current Weather
Expand Down Expand Up @@ -631,6 +635,7 @@ export default function MapWrapper({
}
}, [searchLocationFrom]);

// Route layer
useEffect(() => {
if (isInitialMountRoute.current) {
// Do nothing on first load
Expand All @@ -646,6 +651,7 @@ export default function MapWrapper({
loadData(false);
}, [selectedRoute]);

// Camera layer
useEffect(() => {
// Remove layer if it already exists
if (mapLayers.current['highwayCams']) {
Expand Down Expand Up @@ -687,6 +693,7 @@ export default function MapWrapper({
}
};

// Event layers
useEffect(() => {
loadEventsLayers(events, mapContext, mapLayers, mapRef);
}, [events]);
Expand All @@ -706,6 +713,7 @@ export default function MapWrapper({
}
};

// Ferries and rest stops layers
useEffect(() => {
// Remove layer if it already exists
if (mapLayers.current['inlandFerries']) {
Expand Down Expand Up @@ -767,6 +775,7 @@ export default function MapWrapper({
}
};

// Weather layers
useEffect(() => {
if (mapLayers.current['weather']) {
mapRef.current.removeLayer(mapLayers.current['weather']);
Expand Down Expand Up @@ -797,6 +806,27 @@ export default function MapWrapper({
}
};

// Advisories layer
useEffect(() => {
// Remove layer if it already exists
if (mapLayers.current['advisoriesLayer']) {
mapRef.current.removeLayer(mapLayers.current['advisoriesLayer']);
}

// Add layer if array exists
if (advisories) {
// Generate and add layer
mapLayers.current['advisoriesLayer'] = getAdvisoriesLayer(
advisories,
mapRef.current.getView().getProjection().getCode(),
mapContext,
);

mapRef.current.addLayer(mapLayers.current['advisoriesLayer']);
mapLayers.current['advisoriesLayer'].setZIndex(55);
}
}, [advisories]);

useEffect(() => {
if (mapLayers.current['regional']) {
mapRef.current.removeLayer(mapLayers.current['regional']);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

.advisories-on-map {
position: relative;
top: 1rem;
top: 6rem;
left: 1rem;
width: fit-content;

Expand Down
15 changes: 14 additions & 1 deletion src/frontend/src/Components/data/featureStyleDefinitions.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Icon, Stroke, Style } from 'ol/style.js';
import { Fill, Icon, Stroke, Style } from 'ol/style.js';

// Static assets
// Cameras
Expand Down Expand Up @@ -75,6 +75,19 @@ import genericDelaysActiveIcon from '../../images/mapIcons/incident-minor-active
import genericDelaysHoverIcon from '../../images/mapIcons/incident-minor-hover.png';
import genericDelaysStaticIcon from '../../images/mapIcons/incident-minor-static.png';

// Map advisory styles
export const advisoryStyles = {
polygon: new Style({
stroke: new Stroke({
color: 'rgb(255, 90, 0)',
width: 2,
}),
fill: new Fill({
color: 'rgba(255, 217, 105, 0.4)',
}),
})
};

// Camera icon styles
export const cameraStyles = {
static: new Style({
Expand Down
50 changes: 50 additions & 0 deletions src/frontend/src/Components/map/layers/advisoriesLayer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// Components and functions
import { transformFeature } from '../helper.js';

// OpenLayers
import { Polygon } from 'ol/geom';
import * as ol from 'ol';
import GeoJSON from 'ol/format/GeoJSON.js';
import VectorLayer from 'ol/layer/Vector';
import VectorSource from 'ol/source/Vector';

// Styling
import { advisoryStyles } from '../../data/featureStyleDefinitions.js';

export function getAdvisoriesLayer(
advisories,
projectionCode,
mapContext,
) {
return new VectorLayer({
classname: 'advisories',
visible: true,
source: new VectorSource({
format: new GeoJSON(),
loader: function (extent, resolution, projection) {
const vectorSource = this;
vectorSource.clear();

advisories.forEach(advisory => {
// Build a new OpenLayers feature
const olGeometry = new Polygon(advisory.geometry.coordinates);
const olFeature = new ol.Feature({ geometry: olGeometry });

// Transform the projection
const olFeatureForMap = transformFeature(
olFeature,
'EPSG:4326',
projectionCode,
);

// feature ID to advisory ID for retrieval
olFeatureForMap.setId(advisory.id);

vectorSource.addFeature(olFeatureForMap);
});
},
}),

style: () => advisoryStyles.polygon,
});
}
7 changes: 3 additions & 4 deletions src/frontend/src/pages/AdvisoryDetailsPage.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,11 @@ function getMap(advisoryData) {
}),
'Polygon': new Style({
stroke: new Stroke({
color: 'blue',
lineDash: [4],
width: 3,
color: 'rgb(255, 90, 0)',
width: 2,
}),
fill: new Fill({
color: 'rgba(0, 0, 255, 0.1)',
color: 'rgba(255, 217, 105, 0.4)',
}),
})
};
Expand Down

0 comments on commit 4251cf9

Please sign in to comment.