import L from 'leaflet'; import 'leaflet.markercluster'; import uniqBy from 'lodash/fp/uniqBy'; import isString from 'lodash/fp/isString'; import isEmpty from 'lodash/fp/isEmpty'; import { toGeoJson, isValidCoordinate, geoJsonOptions } from './GeoJson'; import { GeoJson } from 'leaflet'; import { Feature, GeometryObject } from 'geojson'; import { EVENT_COLOR, EVENT_RADIUS } from '../constants/layer.constant'; import { getOrgUnitsFromRows, getFiltersFromColumns, getFiltersAsText, getPeriodFromFilters, getPeriodNameFromId } from '../utils/analytics'; export const event = options => { const { geofeature, layerOptions, displaySettings, opacity, id, dataSelections, legendProperties, analyticsData } = options; const { startDate, endDate } = dataSelections; const { eventPointColor, eventPointRadius, radiusLow, eventClustering } = layerOptions; const { labelFontSize, labelFontStyle } = displaySettings; const orgUnits = getOrgUnitsFromRows(dataSelections.rows); const period = getPeriodFromFilters(dataSelections.filters); const dataFilters = getFiltersFromColumns(dataSelections.columns); let legend = { period: period ? getPeriodNameFromId(period.id) : `${startDate} - ${endDate}`, filters: dataFilters && getFiltersAsText(dataFilters), items: null }; const features = toGeoJson(geofeature); let geoJsonLayer = L.geoJSON(features); if (analyticsData) { const names = { true: 'Yes', false: 'No' }; const { rows, headers, height, metaData, width } = analyticsData; headers.forEach(header => (names[header.name] = header.column)); const data = rows .map(row => createEventFeature(headers, names, row)) .filter(feature => isValidCoordinate(feature.geometry.coordinates)); if (Array.isArray(data) && data.length) { const items = [ { name: 'Event', color: eventPointColor || EVENT_COLOR, radius: eventPointRadius || EVENT_RADIUS } ]; legend = { ...legend, items }; const clusterOptions = { spiderfyOnMaxZoom: false, showCoverageOnHover: false, iconCreateFunction: cluster => { return _iconCreateFunction( cluster, eventPointColor, opacity, labelFontStyle, labelFontSize ); } }; const geoJSonOptions = geoJsonOptions(id, eventPointRadius, opacity, eventPointColor); geoJsonLayer = L.geoJSON(data, geoJSonOptions); // All Clustering is done here; if (eventClustering) { const markers = new L.markerClusterGroup(clusterOptions); geoJsonLayer = markers.addLayers(geoJsonLayer); } geoJsonLayer.on({ click: eventLayerEvents().onClick, mouseover: eventLayerEvents().mouseover, rightClick: eventLayerEvents().onRightClick, mouseout: eventLayerEvents().mouseout }); } } const bounds = geoJsonLayer.getBounds(); const optionsToReturn = { ...options, features, geoJsonLayer }; if (bounds.isValid()) { return { ...optionsToReturn, bounds }; } return optionsToReturn; }; const createEventFeature = (headers, names, layerEvent, eventCoordinateField?) => { const properties = layerEvent.reduce( (props, value, i) => ({ ...props, [headers[i].name]: names[value] || value }), {} ); // properties style properties.style = { color: '#fff', weight: 1, stroke: true, fill: true }; let coordinates; if (eventCoordinateField) { // If coordinate field other than event location const eventCoord = properties[eventCoordinateField]; if (Array.isArray(eventCoord)) { coordinates = eventCoord; } else if (isString(eventCoord) && !isEmpty(eventCoord)) { coordinates = JSON.parse(eventCoord); } else { coordinates = []; } } else { // Use event location coordinates = [properties.longitude, properties.latitude]; // Event location } return { type: 'Feature', id: properties.psi, properties, geometry: { type: 'Point', coordinates: coordinates.map(parseFloat) } }; }; const eventLayerEvents = () => { const onClick = evt => { const attr = evt.layer.feature.properties; const content = `
| Organisation unit: | ${attr.ouname} |
|---|---|
| Event time: | ${attr.eventdate} |
| Event location: | ${attr.latitude}, ${attr.longitude} |