Interactive interface for browsing global, full-resolution satellite imagery
more progress, tracks working
Jason Kente662a76c95d5
diff --git a/web/js/map/natural-events/event-markers.js b/web/js/map/natural-events/event-markers.jsindex 594f5c771..87cfbf93b 100644--- a/web/js/map/natural-events/event-markers.js+++ b/web/js/map/natural-events/event-markers.js@@ -34,14 +34,15 @@ class EventMarkers extends React.Component {componentDidUpdate(prevProps, prevState) {const {- proj, eventsDataIsLoading, eventsActive, selectedEvent,+ proj, eventsDataIsLoading, eventsActive, selectedEvent, selectedDate,} = this.props;const activeChange = eventsActive !== prevProps.eventsActive;const finishedLoading = !eventsDataIsLoading && (eventsDataIsLoading !== prevProps.eventsDataIsLoading);const projChange = proj !== prevProps.proj;const selectedEventChange = selectedEvent !== prevProps.selectedEvent;+ const selectedDateChange = selectedDate !== prevProps.selectedDate;- if (activeChange || finishedLoading || projChange || selectedEventChange) {+ if (activeChange || finishedLoading || projChange || selectedEventChange || selectedDateChange) {this.remove();this.draw();}@@ -53,7 +54,7 @@ class EventMarkers extends React.Component {draw() {const {- eventsData, selectEvent, selectedEvent, proj, map,+ eventsData, selectEvent, selectedEvent, proj, map, mapUi,} = this.props;if (!eventsData || eventsData.length < 1) return null;@@ -141,8 +142,7 @@ class EventMarkers extends React.Component {willSelect = true;moveCount = 0;},- // TODO get from Redux state?- // ui.supportsPassive ? { passive: true } : false,+ mapUi.supportsPassive ? { passive: true } : false,);});['pointermove', 'mousemove'].forEach((type) => {@@ -154,8 +154,7 @@ class EventMarkers extends React.Component {willSelect = false;}},- // TODO get from Redux state?- // ui.supportsPassive ? { passive: true } : false,+ mapUi.supportsPassive ? { passive: true } : false,);});['touchend', 'click'].forEach((type) => {@@ -173,8 +172,7 @@ class EventMarkers extends React.Component {});}},- // TODO get from Redux state?- // ui.supportsPassive ? { passive: true } : false,+ mapUi.supportsPassive ? { passive: true } : false,);});}@@ -266,6 +264,7 @@ const mapStateToProps = (state) => {return {activeTab: sidebar.activeTab,map: map.ui.selected,+ mapUi: map.ui,proj,selectedEvent: events.selected,eventsData: requestedEvents.response,@@ -285,9 +284,11 @@ EventMarkers.propTypes = {eventsData: PropTypes.array,eventsDataIsLoading: PropTypes.bool,map: PropTypes.object,+ mapUi: PropTypes.object,proj: PropTypes.object,selectEvent: PropTypes.func,selectedEvent: PropTypes.object,+ selectedDate: PropTypes.object,};export default connect(diff --git a/web/js/map/natural-events/event-track.js b/web/js/map/natural-events/event-track.jsindex 1658f38cf..e4f1fb137 100644--- a/web/js/map/natural-events/event-track.js+++ b/web/js/map/natural-events/event-track.js@@ -37,44 +37,62 @@ class EventTrack extends React.Component {active: false,trackDetails: {},};++ this.onMoveEnd = this.onMoveEnd.bind(this);+ this.onPropertyChange = this.onPropertyChange.bind(this);}- componentDidUpdate(prevProps, prevState) {- const { trackDetails } = this.state;- const { map, eventsActive } = this.props;+ componentDidMount() {+ const { map, selectedEvent } = this.props;+ if (map && selectedEvent.id && selectedEvent.date) {+ this.updateCurrentTrack();+ }+ }- // Previously onSidebarChange- if (eventsActive !== prevProps.eventsActive) {- this.debounceTrackUpdate();- } else if (trackDetails.id) this.update(null);+ componentDidUpdate(prevProps, prevState) {+ const {+ map, selectedEvent, selectedDate,+ } = this.props;+ const selectedEventChange = selectedEvent !== prevProps.selectedEvent;+ const selectedDateChange = selectedDate !== prevProps.selectedDate;if (map !== prevProps.map) {map.on('moveend', this.onMoveEnd);-- // reset track on change to resolution or rotationmap.getView().on('propertychange', this.onPropertyChange);}++ if (selectedEvent && (selectedEventChange || selectedDateChange)) {+ this.updateCurrentTrack();+ }}componentWillUnmount() {const { map } = this.props;+ this.update(null);map.un('moveend', this.onMoveEnd);map.getView().un('propertychange', this.onPropertyChange);}- debounceTrackUpdate = lodashDebounce(function () {- const { selectEvent, eventsData, selectedEvent } = this.props;+ updateCurrentTrack() {+ const { selectedEvent, eventsData } = this.props;+ const { id, date } = selectedEvent;+ const event = eventsData.find((e) => e.id === id);+ this.update(event, date);+ }- if (!selectedEvent.id || !selectedEvent.date) {- return;- }- const event = eventsData.find((e) => e.id === selectedEvent.id);- this.update(event, selectedEvent.date, (id, date) => {- selectEvent(id, date);- });- }, 250);+ // debounceTrackUpdate = lodashDebounce(function () {+ // const { selectEvent, eventsData, selectedEvent } = this.props;++ // if (!selectedEvent.id || !selectedEvent.date) {+ // return;+ // }+ // const event = eventsData.find((e) => e.id === selectedEvent.id);+ // this.update(event, selectedEvent.date, (id, date) => {+ // selectEvent(id, date);+ // });+ // }, 250);- onMoveEnd = (e) => {+ onMoveEnd = function(e) {const { map } = this.props;const { trackDetails, active } = this.state;@@ -82,7 +100,7 @@ class EventTrack extends React.Component {if (trackDetails.id) {addPointOverlays(map, trackDetails.pointArray);} else {- this.debounceTrackUpdate();+ // this.debounceTrackUpdate();}}@@ -139,20 +157,22 @@ class EventTrack extends React.Component {* @param {Object} event EONET event object* @param {Object} map Ol map object* @param {String} selectedDate- * @param {Function} callback event change callback* @return {[type]}*/- update = function(event, selectedDate, callback) {- const { proj, map } = this.props;+ update = function(event, date, callbackParam) {+ const {+ proj, map, selectEventCallback,+ } = this.props;const { trackDetails } = this.state;let newTrackDetails;+ const callback = callbackParam || selectEventCallback;const createAndAddTrack = () => {newTrackDetails = createTrack(proj,event,map,- selectedDate,+ date,callback,);map.addLayer(newTrackDetails.track);@@ -167,9 +187,9 @@ class EventTrack extends React.Component {if (trackDetails.id === event.id) {// If same Track but different selection// Just update classNames- if (trackDetails.selectedDate !== selectedDate) {+ if (trackDetails.selectedDate !== date) {const isClusteredSelection = !document.getElementById(- `track-marker-${selectedDate}`,+ `track-marker-${date}`,);// If New Date is in cluster build new trackif (isClusteredSelection) {@@ -177,8 +197,8 @@ class EventTrack extends React.Component {createAndAddTrack();} else {newTrackDetails = trackDetails;- updateSelection(selectedDate);- newTrackDetails.selectedDate = selectedDate;+ updateSelection(date);+ newTrackDetails.selectedDate = date;}} else {// If the date and event are the same@@ -643,11 +663,12 @@ function addOverlayIfIsVisible(map, overlay) {const mapStateToProps = (state) => {const {- map, proj, events, requestedEvents,+ map, proj, events, requestedEvents, date,} = state;return {map: map.ui.selected,proj,+ selectedDate: date.selected,selectedEvent: events.selected,eventsData: requestedEvents.response,eventsActive: events.active,@@ -661,12 +682,13 @@ const mapDispatchToProps = (dispatch) => ({});EventTrack.propTypes = {- eventsActive: PropTypes.bool,eventsData: PropTypes.array,map: PropTypes.object,proj: PropTypes.object,selectEvent: PropTypes.func,+ selectEventCallback: PropTypes.func,selectedEvent: PropTypes.object,+ selectedDate: PropTypes.object,};export default connect(diff --git a/web/js/map/natural-events/natural-events.js b/web/js/map/natural-events/natural-events.jsindex 160ead397..2285b7a74 100644--- a/web/js/map/natural-events/natural-events.js+++ b/web/js/map/natural-events/natural-events.js@@ -1,72 +1,239 @@import React from 'react';import { connect } from 'react-redux';import PropTypes from 'prop-types';+import * as olExtent from 'ol/extent';+import * as olProj from 'ol/proj';+import { getDefaultEventDate, getEventsWithinExtent } from './util';+import util from '../../util/util';+import { selectDate as selectDateAction } from '../../modules/date/actions';+import { selected as selectedAction } from '../../modules/natural-events/actions';+import {+ activateLayersForEventCategory as activateLayersForEventCategoryAction,+} from '../../modules/layers/actions';+import EventTrack from './event-track';import EventMarkers from './event-markers';-// const zoomLevelReference = {-// Wildfires: 8,-// Volcanoes: 6,-// };+const zoomLevelReference = {+ Wildfires: 8,+ Volcanoes: 6,+};class NaturalEvents extends React.Component {constructor(props) {super(props);this.state = {-+ filteredEvents: [],+ prevSelectedEvent: {},};++ this.selectEvent = this.selectEvent.bind(this);+ this.filterEventList = this.filterEventList.bind(this);}componentDidMount() {-+ const { map, eventsDataIsLoading, eventsData } = this.props;+ if (map && !eventsDataIsLoading && eventsData) {+ this.filterEventList();+ }}componentDidUpdate(prevProps, prevState) {const {- active,+ map,proj,+ eventsDataIsLoading,+ selectedEvent,+ } = this.props;+ const loadingChange = eventsDataIsLoading !== prevProps.eventsDataIsLoading;+ const projChange = proj !== prevProps.proj;+ const mapChange = map !== prevProps.map;+ const selectedEventChange = selectedEvent !== prevProps.selectedEvent;++ if (!map || eventsDataIsLoading) return;++ if (loadingChange || projChange || mapChange) {+ map.on('moveend', this.filterEventList);+ // TODO unbind when map changes?+ this.filterEventList();+ }++ if (selectedEventChange) {+ if (selectedEvent) {+ const { id, date } = selectedEvent;+ this.selectEvent(id, date, loadingChange);+ } else {+ this.deselectEvent();+ }+ }+ }++ componentWillUnmount() {+ const { map } = this.props;+ map.un('moveend', this.filterEventList);+ }++ filterEventList() {+ const {+ map, proj, eventsData, selectedEvent, isZoomed,+ } = this.props;+ // const extent = map.getView().calculateExtent();+ // const shouldFilter = isZoomed || proj.selected.id !== 'geographic';++ // const filteredEvents = shouldFilter+ // ? getEventsWithinExtent(eventsData, selectedEvent, extent, proj.selected)+ // : eventsData;+ this.setState({ filteredEvents: eventsData });+ }++ getZoomPromise = function(+ event,+ date,+ isIdChange,+ isInitialLoad,+ ) {+ return isInitialLoad+ ? new Promise((resolve, reject) => { resolve(); })+ : this.zoomToEvent(event, date, isIdChange);+ };++ selectEvent(id, date, isInitialLoad) {+ const { filteredEvents, prevSelectedEvent } = this.state;+ const {+ mapUi, selectDate, selectEventFinished,} = this.props;- if (active !== prevProps.active) {- // onsidebarchange+ const isIdChange = !prevSelectedEvent || prevSelectedEvent.id !== id;+ const prevId = prevSelectedEvent.id ? prevSelectedEvent.id : false;+ const prevEvent = prevId && filteredEvents.find((e) => e.id === prevId);+ const prevCategory = prevEvent ? prevEvent.categories[0].title : false;+ const event = filteredEvents.find((e) => e.id === id);+ const category = event && event.categories[0].title;+ const isSameCategory = category === prevCategory;+ if (!event) {+ return;}- if (proj !== prevProps.proj) {- // onProjChange+ const eventDate = date || getDefaultEventDate(event);+ const dateFormat = (d) => d.toISOString().split('T')[0];++ this.setState({ prevSelectedEvent: { id, date } });++ this.getZoomPromise(event, eventDate, !isIdChange, isInitialLoad).then(() => {+ /* For Wildfires that didn't happen today, move the timeline forward a day+ * to improve the chance that the fire is visible.+ * NOTE: If the fire happened yesterday and the imagery isn't yet available+ * for today, this may not help.+ */+ if (event.categories[0].title === 'Wildfires' && !isInitialLoad) {+ const today = dateFormat(new Date());+ const yesterday = dateFormat(util.yesterday());+ if (date !== today || date !== yesterday) {+ selectDate(util.dateAdd(util.parseDateUTC(date), 'day', 1));+ }+ } else if (!isInitialLoad) {+ selectDate(util.parseDateUTC(date));+ }+ if (isIdChange && !isSameCategory && !isInitialLoad) {+ this.activateLayersForCategory(event.categories[0].title);+ }+ // hack to update layers+ if (isIdChange) {+ mapUi.reloadLayers();+ } else {+ mapUi.updateDate();+ }++ selectEventFinished();+ });+ }++ zoomToEvent = function(event, date, isSameEventID) {+ const { proj, map, mapUi } = this.props;+ const { crs, id } = proj.selected;+ const category = event.categories[0].title;+ const zoom = isSameEventID ? map.getView().getZoom() : zoomLevelReference[category];+ const geometry = event.geometry.find((geom) => geom.date.split('T')[0] === date);++ // check for polygon geometries and/or perform projection coordinate transform+ let coordinates = geometry.type === 'Polygon'+ ? olExtent.boundingExtent(+ olProj.transform(geometry.coordinates[0], 'EPSG:4326', crs),+ )+ : olProj.transform(geometry.coordinates, 'EPSG:4326', crs);++ // handle extent transform for polar+ if (geometry.type === 'Polygon' && id !== 'geographic') {+ coordinates = olProj.transformExtent(coordinates, 'EPSG:4326', crs);}+ return mapUi.animate.fly(coordinates, zoom, null);+ };++ /**+ * Add the relevant layers for event based on projection and category+ * @param {*} category+ */+ activateLayersForCategory(category = 'Default') {+ const { config, proj, activateLayersForEventCategory } = this.props;+ const { layers } = config.naturalEvents;+ activateLayersForEventCategory(layers[proj.id][category]);}render() {return (<>- <EventTrack />+ <EventTrack selectEventCallback={this.selectEvent} /><EventMarkers /></>);}}-function mapStateToProps(state) {+const mapStateToProps = (state) => {const {events, config, map, proj, requestedEvents,} = state;+ const selectedMap = map.ui.selected;return {- map: map.ui.selected,+ map: selectedMap,+ mapUi: map.ui,config,proj,active: events.active,- eventsRequestLoading: requestedEvents.isLoading,+ isZoomed: selectedMap && Math.floor(selectedMap.getView().getZoom()) >= 3,+ eventsDataIsLoading: requestedEvents.isLoading,eventsData: requestedEvents.response,+ selectedEvent: events.selected,};-}+};++const mapDispatchToProps = (dispatch) => ({+ activateLayersForEventCategory: (layers) => {+ dispatch(activateLayersForEventCategoryAction(layers));+ },+ selectDate: (date) => {+ dispatch(selectDateAction(date));+ },+ selectEventFinished: () => {+ dispatch(selectedAction());+ },+});NaturalEvents.propTypes = {- active: PropTypes.bool,+ activateLayersForEventCategory: PropTypes.func,+ isZoomed: PropTypes.bool,config: PropTypes.object,+ eventsData: PropTypes.array,+ eventsDataIsLoading: PropTypes.bool,+ selectedEvent: PropTypes.object,+ selectEventFinished: PropTypes.func,+ selectDate: PropTypes.func,map: PropTypes.object,+ mapUi: PropTypes.object,proj: PropTypes.object,};export default connect(mapStateToProps,+ mapDispatchToProps,)(NaturalEvents);diff --git a/web/js/map/natural-events/util.js b/web/js/map/natural-events/util.jsindex 17ce3ded4..a34180921 100644--- a/web/js/map/natural-events/util.js+++ b/web/js/map/natural-events/util.js@@ -1,6 +1,6 @@import lodashFind from 'lodash/find';import * as olProj from 'ol/proj';-import * as olExtent from 'ol/extent';+import { getCenter, boundingExtent, containsCoordinate } from 'ol/extent';/***@@ -32,9 +32,8 @@ export function getEventsWithinExtent(selected,extent,selectedProj,- showAll,) {- const { maxExtent } = selectedProj;+ const { maxExtent, crs } = selectedProj;const visibleListEvents = {};loadedEvents.forEach((naturalEvent) => {@@ -43,42 +42,36 @@ export function getEventsWithinExtent(if (selected && selected.date) {date = selected.date;}- const geometry = lodashFind(naturalEvent.geometry, (geometry) => geometry.date.split('T')[0] === date) || naturalEvent.geometry[0];+ const geometry = lodashFind(+ naturalEvent.geometry,+ (geom) => geom.date.split('T')[0] === date,+ ) || naturalEvent.geometry[0];let { coordinates } = geometry;if (selectedProj.id !== 'geographic') {- // check for polygon geometries for targeted projection coordinate transformif (geometry.type === 'Polygon') {- const coordinatesTransform = coordinates[0].map((coordinate) => olProj.transform(coordinate, 'EPSG:4326', selectedProj.crs));- const geomExtent = olExtent.boundingExtent(coordinatesTransform);- coordinates = olExtent.getCenter(geomExtent);- } else {- // if normal geometries, transform given lon/lat array- coordinates = olProj.transform(- coordinates,- 'EPSG:4326',- selectedProj.crs,+ const coordinatesTransform = coordinates[0].map(+ (coordinate) => olProj.transform(coordinate, 'EPSG:4326', crs),);+ const geomExtent = boundingExtent(coordinatesTransform);+ coordinates = getCenter(geomExtent);+ } else {+ coordinates = olProj.transform(coordinates, 'EPSG:4326', crs);}} else if (geometry.type === 'Polygon') {- const geomExtent = olExtent.boundingExtent(geometry.coordinates[0]);- coordinates = olExtent.getCenter(geomExtent);+ const geomExtent = boundingExtent(geometry.coordinates[0]);+ coordinates = getCenter(geomExtent);}// limit to maxExtent while allowing zoom and filter 'out of extent' events- const isVisible = olExtent.containsCoordinate(extent, coordinates)- && olExtent.containsCoordinate(maxExtent, coordinates);+ const coordsInProjExtent = containsCoordinate(maxExtent, coordinates);+ const isVisible = containsCoordinate(extent, coordinates) && coordsInProjExtent;- if (isVisible) {- visibleListEvents[naturalEvent.id] = true;- } else if (- // Keep selected in event list if within proj limits- isSelectedEvent- && olExtent.containsCoordinate(maxExtent, coordinates)- ) {+ if (isVisible || (isSelectedEvent && coordsInProjExtent)) {visibleListEvents[naturalEvent.id] = true;}});+return visibleListEvents;}diff --git a/web/js/util/util.js b/web/js/util/util.jsindex d60219bef..748d8d6aa 100644--- a/web/js/util/util.js+++ b/web/js/util/util.js@@ -691,6 +691,11 @@ export default (function(self) {return self.now();};+ self.yesterday = function() {+ const now = new Date();+ return new Date(now.setDate(now.getDate() - 1));+ };+/*** General warning handler.*