Skip to content

Interactive interface for browsing global, full-resolution satellite imagery

draw track line as SVG

Jason Kent6a191a70325a
diff --git a/web/js/map/natural-events/event-track.js b/web/js/map/natural-events/event-track.js
index 1da04d38f..9bffe2b40 100644
--- a/web/js/map/natural-events/event-track.js
+++ b/web/js/map/natural-events/event-track.js
@@ -1,9 +1,7 @@
import React from 'react';
import { connect } from 'react-redux';
import PropTypes from 'prop-types';
-import OlFeature from 'ol/Feature';
import * as olExtent from 'ol/extent';
-import OlGeomMultiLineString from 'ol/geom/MultiLineString';
import * as olProj from 'ol/proj';
import {
each as lodashEach,
@@ -18,7 +16,7 @@ import { getFilteredEvents } from '../../modules/natural-events/selectors';
import {
- getTrack, getTrackPoint, getArrows, getClusterPointEl,
+ getTrackLines, getTrackPoint, getArrows, getClusterPointEl,
} from './util';
class EventTrack extends React.Component {
@@ -58,7 +56,7 @@ class EventTrack extends React.Component {
if (prevMap) {
this.update(null);
this.removeTrack(prevMap);
- removeOldPoints(prevMap, trackDetails);
+ removeOldPoints(prevMap, trackDetails.pointsAndArrows);
}
this.initialize();
}
@@ -102,7 +100,7 @@ class EventTrack extends React.Component {
const { trackDetails } = this.state;
if (trackDetails.id) {
- addPointOverlays(map, trackDetails.pointArray);
+ addPointOverlays(map, trackDetails.pointsAndArrows);
} else {
this.debouncedTrackUpdate();
}
@@ -132,7 +130,7 @@ class EventTrack extends React.Component {
isNewTarget = oldLon !== targetLon || oldLat !== targetLat;
}
if (isNewTarget) {
- removeOldPoints(map, trackDetails);
+ removeOldPoints(map, trackDetails.pointsAndArrows);
}
}
}
@@ -143,9 +141,9 @@ class EventTrack extends React.Component {
*/
removeTrack = function(map) {
const { trackDetails } = this.state;
- const { track } = trackDetails;
- map.removeLayer(track);
- removeOldPoints(map, trackDetails);
+ const { track, pointsAndArrows } = trackDetails;
+ map.removeOverlay(track);
+ removeOldPoints(map, pointsAndArrows);
return {};
};
@@ -167,8 +165,22 @@ class EventTrack extends React.Component {
const sameDate = trackDetails.selectedDate === date;
const createAndAddTrack = () => {
- newTrackDetails = createTrack(proj, event, map, date, selectEvent);
- map.addLayer(newTrackDetails.track);
+ const {
+ track,
+ pointsAndArrows,
+ } = getTracksAndPoints(event, proj, map, date, selectEvent);
+
+ newTrackDetails = {
+ id: event.id,
+ selectedDate: date,
+ track,
+ pointsAndArrows,
+ hidden: false,
+ };
+
+ map.addOverlay(track);
+ track.changed();
+ console.log('adding track');
};
if (!event || event.geometry.length < 2) {
@@ -204,38 +216,8 @@ class EventTrack extends React.Component {
}
}
-/**
- * Loop through event geometries and create
- * track points and line
- *
- * @param {Object} proj
- * @param {Object} eventObj EONET event Object
- * @param {Object} map Openlayers map Object
- * @param {String} selectedDate
- * @param {Function} callback date-change callback
- * @return {Object} Object with Track elements and info
- */
-const createTrack = function(proj, eventObj, map, selectedDate, callback) {
- const olTrackLineFeatures = [];
-
- const { tracks, overlays } = getTracksAndPoints(eventObj, proj, map, selectedDate, callback);
- olTrackLineFeatures.push(
- new OlFeature({
- geometry: new OlGeomMultiLineString(tracks),
- }),
- );
-
- return {
- id: eventObj.id,
- track: getTrack(proj, olTrackLineFeatures, map),
- pointArray: overlays,
- selectedDate,
- hidden: false,
- };
-};
-
-const removeOldPoints = function(map, { pointArray }) {
- lodashEach(pointArray, (pointOverlay) => {
+const removeOldPoints = function(map, pointsAndArrows) {
+ lodashEach(pointsAndArrows, (pointOverlay) => {
if (map.getOverlayById(pointOverlay.getId())) {
map.removeOverlay(pointOverlay);
}
@@ -275,8 +257,8 @@ const updateSelection = function(newDate) {
* @return {Object} Object Containing track info and elements
*/
const getTracksAndPoints = function(eventObj, proj, map, selectedDate, callback) {
- const overlays = [];
- const tracks = [];
+ const pointsAndArrows = [];
+ const trackSegments = [];
const { clusters, firstClusterObj, secondClusterObj } = getClusters(eventObj, proj, selectedDate, map);
clusters.forEach((clusterPoint, index) => {
@@ -298,21 +280,21 @@ const getTracksAndPoints = function(eventObj, proj, map, selectedDate, callback)
const lineSegmentArray = [prevCoordinates, nextCoordinates];
const arrowOverlay = getArrows(lineSegmentArray, map);
- overlays.push(arrowOverlay);
- tracks.push(lineSegmentArray);
+ pointsAndArrows.push(arrowOverlay);
+ trackSegments.push(lineSegmentArray);
addOverlayIfIsVisible(map, arrowOverlay);
}
const point = clusterPoint.properties.cluster
? getClusterPointEl(proj, clusterPoint, map, pointClusterObj, callback)
- : getTrackPoint(proj, clusterPoint, isSelected, map, callback);
- overlays.push(point);
+ : getTrackPoint(proj, clusterPoint, isSelected, callback);
+ pointsAndArrows.push(point);
addOverlayIfIsVisible(map, point);
});
return {
- tracks,
- overlays,
+ track: getTrackLines(map, trackSegments),
+ pointsAndArrows,
};
};
diff --git a/web/js/map/natural-events/util.js b/web/js/map/natural-events/util.js
index b6d7b7a10..a30dabe4f 100644
--- a/web/js/map/natural-events/util.js
+++ b/web/js/map/natural-events/util.js
@@ -1,8 +1,4 @@
import OlOverlay from 'ol/Overlay';
-import OlLayerVector from 'ol/layer/Vector';
-import OlSourceVector from 'ol/source/Vector';
-import OlStyleStroke from 'ol/style/Stroke';
-import OlStyleStyle from 'ol/style/Style';
import * as olProj from 'ol/proj';
import { mapUtilZoomAction } from '../util';
import { formatDisplayDate } from '../../modules/date/util';
@@ -15,13 +11,7 @@ import { formatDisplayDate } from '../../modules/date/util';
* @param {Function} callback
* @return {Object} Openlayers overlay object
*/
-export const getTrackPoint = function(
- proj,
- clusterPoint,
- isSelected,
- map,
- callback,
-) {
+export const getTrackPoint = function(proj, clusterPoint, isSelected, callback) {
const overlayEl = document.createElement('div');
const circleEl = document.createElement('div');
const textEl = document.createElement('span');
@@ -57,69 +47,44 @@ export const getTrackPoint = function(
});
};
-/**
- * Create vector layer
- *
- * @param {Array} featuresArray Array of linstring points
- * @return {Object} OpenLayers Vector Layer
- */
-export const getTrack = function(proj, features) {
- const extentWithWings = [-250, -90, 250, 90];
- const extent = proj.selected.id === 'geographic'
- ? extentWithWings
- : proj.selected.maxExtent;
-
- const getLineStyle = function(color, width) {
- return new OlStyleStyle({
- stroke: new OlStyleStroke({ color, width }),
- });
- };
- return new OlLayerVector({
- source: new OlSourceVector({ features }),
- extent,
- style: [getLineStyle('black', 2), getLineStyle('white', 1)],
- });
-};
-
-
-
/**
* Create rotated element that displays arrows between points
* as repeated css background image
*
- * @param {Array} lineSegmentCoords Start and end points of
- * line in Lat/long
+ * @param {Array} lineSegmentCoords Start and end points of line in Lat/long
* @param {Object} map OpenLayers map Object
* @return {Object} Openlayers overlay Object
*/
export const getArrows = function(lineSegmentCoords, map) {
const currentRotation = map.getView().getRotation();
const overlayEl = document.createElement('div');
- const innerEl = document.createElement('div');
+ const arrowEl = document.createElement('div');
const clusterPadding = 20; // 10px on each side
const end = lineSegmentCoords[0];
const start = lineSegmentCoords[1];
const dxCoord = end[0] - start[0];
const dyCoord = end[1] - start[1];
- const pixel1 = map.getPixelFromCoordinate(start);
- const pixel2 = map.getPixelFromCoordinate(end);
- const dx = pixel2[0] - pixel1[0];
- const dy = pixel2[1] - pixel1[1];
- const pixelMidPoint = [0.5 * dx + pixel1[0], 0.5 * dy + pixel1[1]];
+ const pixelStart = map.getPixelFromCoordinate(start);
+ const pixelEnd = map.getPixelFromCoordinate(end);
+ const dx = pixelEnd[0] - pixelStart[0];
+ const dy = pixelEnd[1] - pixelStart[1];
+ const pixelMidPoint = [0.5 * dx + pixelStart[0], 0.5 * dy + pixelStart[1]];
const distanceInPixels = Math.sqrt((dx ** 2) + (dy ** 2));
const angleRadians = Math.atan2(dyCoord, dxCoord) - currentRotation;
const angleDegrees = -angleRadians * (180 / Math.PI);
const lengthOfArrowDiv = distanceInPixels - clusterPadding;
- innerEl.className = 'event-track-arrows';
- overlayEl.appendChild(innerEl);
+ arrowEl.className = 'event-track-arrows';
+ arrowEl.style.width = `${lengthOfArrowDiv}px`;
+ arrowEl.style.height = '16px';
+ arrowEl.style.transform = `translate(${-(lengthOfArrowDiv / 2)}px, -8px)`;
+
+ overlayEl.appendChild(arrowEl);
overlayEl.style.width = '1px';
overlayEl.style.height = '1px';
- innerEl.style.width = `${lengthOfArrowDiv}px`;
- innerEl.style.height = '16px';
- innerEl.style.transform = `translate(${-(lengthOfArrowDiv / 2)}px, -8px)`;
overlayEl.style.transform = `rotate(${angleDegrees}deg)`;
+
return new OlOverlay({
position: map.getCoordinateFromPixel(pixelMidPoint),
positioning: 'center-center',
@@ -129,6 +94,63 @@ export const getArrows = function(lineSegmentCoords, map) {
});
};
+export const getTrackLines = function(map, trackCoords) {
+ const svgEl = document.createElementNS('http://www.w3.org/2000/svg', 'svg');
+ const lineEl = document.createElementNS('http://www.w3.org/2000/svg', 'polyline');
+ let lineString = '';
+ const first = trackCoords[0][0];
+ const last = trackCoords[trackCoords.length - 1][0];
+ const bottomLeft = [first[0], first[1]];
+ const topRight = [last[0], last[1]];
+
+ trackCoords.forEach(([[x1, y1], [x2, y2]]) => {
+ bottomLeft[0] = Math.min(x1, x2, bottomLeft[0]);
+ bottomLeft[1] = Math.min(y1, y2, bottomLeft[1]);
+ topRight[0] = Math.max(x1, x2, topRight[0]);
+ topRight[1] = Math.max(y1, y2, topRight[1]);
+ });
+ const pixelBottomLeft = map.getPixelFromCoordinate(bottomLeft);
+ const pixelTopRight = map.getPixelFromCoordinate(topRight);
+
+ const [minX, maxY] = pixelBottomLeft;
+ const [maxX, minY] = pixelTopRight;
+ const height = Math.abs(maxY - minY);
+ const width = Math.abs(maxX - minX);
+ const rotation = Number(map.getView().getRotation() * (180 / Math.PI)).toFixed();
+
+ svgEl.setAttribute('height', height);
+ svgEl.setAttribute('width', width);
+ svgEl.style.height = `${height}px`;
+ svgEl.style.width = `${width}px`;
+ svgEl.style.backgroundColor = 'rgba(255, 0, 0, 0.15)';
+ svgEl.style.display = 'inline-block';
+ svgEl.style.transform = `rotate(${rotation}deg)`;
+ svgEl.appendChild(lineEl);
+
+ trackCoords.forEach(([start, end], index) => {
+ const [x1, y1] = map.getPixelFromCoordinate(start);
+ const [x2, y2] = map.getPixelFromCoordinate(end);
+ const newStart = `${x1 - minX},${y1 - minY}`;
+ const newEnd = `${x2 - minX},${y2 - minY}`;
+ lineString += index === 0 ? `${newStart} ${newEnd} ` : `${newEnd} `;
+ });
+
+ lineEl.style.fill = 'transparent';
+ lineEl.style.stroke = 'black';
+ lineEl.style.strokeWidth = '1px';
+ lineEl.style.display = 'inline-block';
+ lineEl.setAttribute('points', lineString);
+
+ return new OlOverlay({
+ position: bottomLeft,
+ positioning: 'bottom-left',
+ insertFirst: true,
+ stopEvent: false,
+ element: svgEl,
+ id: 'event-track',
+ });
+};
+
/**
* Create cluster point
*