Skip to content

Commit

Permalink
Squashed commit of the following:
Browse files Browse the repository at this point in the history
commit 6fb47bc
Merge: 3da01fc 818fee4
Author: nreese <[email protected]>
Date:   Tue Dec 13 15:30:20 2016 -0700

    display geo_distance filters in Applied Filters layer

commit 3da01fc
Author: nreese <[email protected]>
Date:   Tue Dec 13 14:01:04 2016 -0700

    remove toolbar tool when poi layers are cleared

commit db0a1fd
Author: nreese <[email protected]>
Date:   Tue Dec 13 13:23:29 2016 -0700

    add control to L.draw.toolbar to create POI filters

commit e3062b2
Author: nreese <[email protected]>
Date:   Fri Dec 9 14:34:41 2016 -0700

    make popup marker with button to trigger filter creation

commit 8b796ac
Author: nreese <[email protected]>
Date:   Wed Dec 7 16:56:47 2016 -0700

    experement with geo_distance filter

commit 38888f5
Author: nreese <[email protected]>
Date:   Fri Dec 9 14:34:41 2016 -0700

    make popup marker with button to trigger filter creation

commit 527fa1d
Author: nreese <[email protected]>
Date:   Wed Dec 7 16:56:47 2016 -0700

    experement with geo_distance filter

commit 818fee4
Author: nreese <[email protected]>
Date:   Tue Dec 13 14:01:04 2016 -0700

    remove toolbar tool when poi layers are cleared

commit b6d442e
Author: nreese <[email protected]>
Date:   Tue Dec 13 13:23:29 2016 -0700

    add control to L.draw.toolbar to create POI filters

commit e784c2d
Merge: b2d0cc9 380a4b7
Author: nreese <[email protected]>
Date:   Mon Dec 12 16:21:21 2016 -0700

    rebase conflicts

commit b2d0cc9
Author: nreese <[email protected]>
Date:   Fri Dec 9 14:34:41 2016 -0700

    make popup marker with button to trigger filter creation

commit 6a9412a
Author: nreese <[email protected]>
Date:   Wed Dec 7 16:56:47 2016 -0700

    experement with geo_distance filter

commit 380a4b7
Author: nreese <[email protected]>
Date:   Fri Dec 9 14:34:41 2016 -0700

    make popup marker with button to trigger filter creation

commit 31b6124
Author: nreese <[email protected]>
Date:   Wed Dec 7 16:56:47 2016 -0700

    experement with geo_distance filter
  • Loading branch information
nreese committed Dec 13, 2016
1 parent 86e0192 commit cc07969
Show file tree
Hide file tree
Showing 6 changed files with 272 additions and 61 deletions.
20 changes: 20 additions & 0 deletions public/callbacks.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,26 @@ define(function (require) {
courier.fetch();
}
},
poiFilter: function (event) {
const agg = _.get(event, 'chart.geohashGridAgg');
if (!agg) return;

const field = agg.fieldName();
const indexPatternName = agg.vis.indexPattern.id;

const filters = [];
event.poiLayers.forEach(function (poiLayer) {
poiLayer.getLayers().forEach(function (feature) {
const filter = {geo_distance: {distance: event.radius + "km"}};
filter.geo_distance[field] = {
"lat" : feature.getLatLng().lat,
"lon" : feature.getLatLng().lng
}
filters.push(filter);
});
});
geoFilter.add(filters, field, indexPatternName);
},
polygon: function (event) {
const agg = _.get(event, 'chart.geohashGridAgg');
if (!agg) return;
Expand Down
10 changes: 10 additions & 0 deletions public/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,16 @@ define(function (require) {
//both corners are inside collar so collar contains
return true;
},
getAggConfig: function (aggs, aggName) {
let aggConfig = null;
index = _.findIndex(aggs, function (agg) {
return agg.schema.name === aggName;
});
if (index !== -1) {
aggConfig = aggs[index];
}
return aggConfig;
},
getMapStateFromVis: function(vis) {
const mapState = {
center: [15, 5],
Expand Down
30 changes: 21 additions & 9 deletions public/visController.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,15 +137,7 @@ define(function (require) {
if(!chartData) return;

//add overlay layer to provide visibility of filtered area
let fieldName;
if ($scope.vis.params.filterByShape && $scope.vis.params.shapeField) {
fieldName = $scope.vis.params.shapeField;
} else {
const agg = _.get(chartData, 'geohashGridAgg');
if (agg) {
fieldName = agg.fieldName();
}
}
let fieldName = getGeoField();
if (fieldName) {
map.addFilters(geoFilter.toGeoJson(fieldName));
}
Expand All @@ -160,6 +152,26 @@ define(function (require) {
collar);
}

/**
* Field used for Geospatial filtering can be set in multiple places
* 1) field specified by geohash_grid aggregation
* 2) field specified under options in event no aggregation is used
*
* Use this method to locate the field
*/
function getGeoField() {
let fieldName = null;
if ($scope.vis.params.filterByShape && $scope.vis.params.shapeField) {
fieldName = $scope.vis.params.shapeField;
} else {
const agg = utils.getAggConfig($scope.vis.aggs, 'segment');
if (agg) {
fieldName = agg.fieldName();
}
}
return fieldName;
}

function drawWmsOverlays() {
map.clearWMSOverlays();
if ($scope.vis.params.overlays.wmsOverlays.length === 0) {
Expand Down
128 changes: 128 additions & 0 deletions public/vislib/LDrawToolbench.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
//Adds additional tools to the standard leafelt.draw toolbar
define(function (require) {
function LDrawToolbench(map, drawControl) {
this._map = map;

const container = drawControl.getContainer().firstChild;
this._actionsContainer = container.getElementsByClassName('leaflet-draw-actions')[0];
this._toolbarContainer = container.getElementsByClassName('leaflet-draw-toolbar')[0];
}

LDrawToolbench.prototype.addTool = function () {
const self = this;
_createButton({
title: "Create geo_distance filter around POIs",
className: 'leaflet-draw-draw-circle leaflet-toolbench-tool',
container: this._toolbarContainer,
callback: function() {
self.displayActions();
},
context: {}
});
}

LDrawToolbench.prototype.removeTools = function () {
const tools = this._toolbarContainer.getElementsByClassName('leaflet-toolbench-tool');
for (var i = 0; i < tools.length; i++) {
const elem = tools[i];
elem.parentNode.removeChild(elem);
}
}

LDrawToolbench.prototype.clearActions = function () {
while (this._actionsContainer.firstChild) {
this._actionsContainer.removeChild(this._actionsContainer.firstChild);
}
}

LDrawToolbench.prototype.displayActions = function () {
const self = this;

self.clearActions();

let radius = 10;

_createInput({
container: L.DomUtil.create('li', '', this._actionsContainer),
inputType: 'number',
name: 'radius',
placeholder: 'radius (km)',
callback: function(event) {
radius = getValue(event);
if (radius <= 0) radius = 10;
}
});
_createButton({
title: "Create geo_distance filters POI layer markers.",
text: "Create",
container: L.DomUtil.create('li', '', this._actionsContainer),
callback: function() {
self._map.fire('toolbench:poiFilter', {radius: radius});
self._actionsContainer.style.display = 'none';
self.clearActions();
}
});
_createButton({
title: "Click to cancel.",
text: "Cancel",
container: L.DomUtil.create('li', '', this._actionsContainer),
callback: function() {
self._actionsContainer.style.display = 'none';
self.clearActions();
}
});

//Make actions visible and located next to toolbar button
const numTools = this._toolbarContainer.children.length;
const actionBarLocation = 25 * (numTools - 1);
this._actionsContainer.style.top = actionBarLocation + 'px';
this._actionsContainer.style.display = 'block';
}

function _createButton (options) {
var link = L.DomUtil.create('a', options.className || '', options.container);
link.href = '#';
if (options.text) {
link.innerHTML = options.text;
}
if (options.title) {
link.title = options.title;
}

L.DomEvent
.on(link, 'click', L.DomEvent.stopPropagation)
.on(link, 'mousedown', L.DomEvent.stopPropagation)
.on(link, 'dblclick', L.DomEvent.stopPropagation)
.on(link, 'click', L.DomEvent.preventDefault)
.on(link, 'click', options.callback, options.context);

return link;
}

function _createInput (options) {
var input = L.DomUtil.create('input', options.className || '', options.container);
input.type = options.inputType;
if (options.placeholder) {
input.placeholder = options.placeholder;
input.title = options.placeholder;
}
if (options.value) {
input.value = options.value;
}
L.DomEvent
.on(input, 'mousedown', L.DomEvent.stopPropagation)
.on(input, 'dblclick', L.DomEvent.stopPropagation)
if (options.callback) {
L.DomEvent
.on(input, 'change', options.callback);
}
return input;
}

function getValue (event) {
const el = event.target || event.srcElement;
return el.value;
}

return LDrawToolbench;
});
48 changes: 32 additions & 16 deletions public/vislib/_map.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ define(function (require) {
var _ = require('lodash');
var $ = require('jquery');
var L = require('leaflet');
var LDrawToolbench = require('./LDrawToolbench');
const utils = require('plugins/enhanced_tilemap/utils');
var formatcoords = require('./../lib/formatcoords/index');
require('./../lib/leaflet.mouseposition/L.Control.MousePosition.css');
Expand Down Expand Up @@ -40,6 +41,7 @@ define(function (require) {
iconSize: iconSize,
iconAnchor: [iconSize[0]/2, iconSize[1]],
className: "vector-marker",
popupAnchor: [0, -10]
});
}

Expand Down Expand Up @@ -95,7 +97,7 @@ define(function (require) {
}

TileMapMap.prototype._addDrawControl = function () {
if (this._boundingControl) return;
if (this._drawControl) return;

//create Markers feature group and add saved markers
this._drawnItems = new L.FeatureGroup();
Expand Down Expand Up @@ -140,8 +142,10 @@ define(function (require) {
drawOptions.edit.remove = false;
}

this._boundingControl = new L.Control.Draw(drawOptions);
this.map.addControl(this._boundingControl);
this._drawControl = new L.Control.Draw(drawOptions);
this.map.addControl(this._drawControl);

this._toolbench = new LDrawToolbench(this.map, this._drawControl);
};

TileMapMap.prototype._addSetViewControl = function () {
Expand Down Expand Up @@ -232,7 +236,7 @@ define(function (require) {
TileMapMap.prototype.destroy = function () {
if (this._label) this._label.removeFrom(this.map);
if (this._fitControl) this._fitControl.removeFrom(this.map);
if (this._boundingControl) this._boundingControl.removeFrom(this.map);
if (this._drawControl) this._drawControl.removeFrom(this.map);
if (this._markers) this._markers.destroy();
syncMaps.remove(this.map);
this.map.remove();
Expand All @@ -246,23 +250,29 @@ define(function (require) {
self.map.removeLayer(layer);
});
this._poiLayers = [];

if (this._toolbench) this._toolbench.removeTools();
};

TileMapMap.prototype.addPOILayer = function (layerName, points, options) {
const featureGroup = new L.FeatureGroup();
points.forEach(function(point) {
featureGroup.addLayer(
L.marker(
point.latlng,
{
icon: markerIcon(options.color, options.size),
title: point.label
})
);
const poi = L.marker(
point.latlng,
{
icon: markerIcon(options.color, options.size)
});
featureGroup.addLayer(poi);
});
this.map.addLayer(featureGroup);
this._layerControl.addOverlay(featureGroup, layerName);
this._poiLayers.push(featureGroup);

//Add tool to l.draw.toolbar so users can filter by POIs
if (this._poiLayers.length === 1) {
if (!this._toolbench) this._addDrawControl();
this._toolbench.addTool();
}
};

/**
Expand Down Expand Up @@ -317,10 +327,8 @@ define(function (require) {
opacity: 1,
fillOpacity: 0.75
}
this._filters = L.geoJson(filters, {
clickable: false,
style: style
});
this._filters = L.featureGroup(filters);
this._filters.setStyle(style);
if (isVisible) this.map.addLayer(this._filters);
this._layerControl.addOverlay(this._filters, "Applied Filters");
};
Expand Down Expand Up @@ -414,6 +422,14 @@ define(function (require) {
self._fitBounds();
});

this.map.on('toolbench:poiFilter', function (e) {
self._callbacks.poiFilter({
chart: self._chartData,
poiLayers: self._poiLayers,
radius: _.get(e, 'radius', 10)
});
});

this.map.on('draw:created', function (e) {
switch (e.layerType) {
case "marker":
Expand Down
Loading

0 comments on commit cc07969

Please sign in to comment.