Skip to content

Commit

Permalink
Merge pull request #3665 from stormpython/enhancement/3155
Browse files Browse the repository at this point in the history
Filter Tile Map Data based on Map Bounds
  • Loading branch information
rashidkpc committed May 1, 2015
2 parents b8166ee + 19913d4 commit b1fe261
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 4 deletions.
28 changes: 25 additions & 3 deletions src/kibana/components/vislib/visualizations/tile_map.js
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,8 @@ define(function (require) {
map.on('moveend', function setZoomCenter() {
mapZoom = self._attr.mapZoom = map.getZoom();
mapCenter = self._attr.mapCenter = map.getCenter();
featureLayer.clearLayers();
featureLayer = self.markerType(map, mapData).addTo(map);
});

map.on('draw:created', function (e) {
Expand Down Expand Up @@ -180,6 +182,19 @@ define(function (require) {
};
};

/**
* Return features within the map bounds
*/
TileMap.prototype._filterToMapBounds = function (map) {
return function (feature) {
var coordinates = feature.geometry.coordinates;
var p0 = coordinates[0];
var p1 = coordinates[1];

return map.getBounds().contains([p1, p0]);
};
};

/**
* zoom map to fit all features in featureLayer
*
Expand Down Expand Up @@ -261,7 +276,8 @@ define(function (require) {
},
style: function (feature) {
return self.applyShadingStyle(feature, min, max);
}
},
filter: self._filterToMapBounds(map)
});

// add legend
Expand Down Expand Up @@ -300,7 +316,8 @@ define(function (require) {
},
style: function (feature) {
return self.applyShadingStyle(feature, min, max);
}
},
filter: self._filterToMapBounds(map)
});

// add legend
Expand Down Expand Up @@ -355,7 +372,8 @@ define(function (require) {
},
style: function (feature) {
return self.applyShadingStyle(feature, min, max);
}
},
filter: self._filterToMapBounds(map)
});

// add legend
Expand Down Expand Up @@ -398,6 +416,10 @@ define(function (require) {
*/
TileMap.prototype.addLegend = function (data, map) {
var self = this;
var isLegend = $('div.tilemap-legend').length;

if (isLegend) return; // Don't add Legend if already one

var legend = L.control({position: 'bottomright'});
legend.onAdd = function () {
var div = L.DomUtil.create('div', 'tilemap-legend');
Expand Down
25 changes: 24 additions & 1 deletion test/unit/specs/vislib/visualizations/tile_map.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@ define(function (require) {
});
});


describe('Leaflet controls', function () {
var vis;
var leafletContainer;
Expand Down Expand Up @@ -141,6 +140,30 @@ define(function (require) {
destroyVis(vis);
});

describe('_filterToMapBounds method', function () {
it('should filter out data points that are outside of the map bounds', function () {
vis.handler.charts.forEach(function (chart) {
chart.maps.forEach(function (map) {
var featuresLength = chart.chartData.geoJson.features.length;
var mapFeatureLength;

function getSize(obj) {
var size = 0;
var key;

for (key in obj) { if (obj.hasOwnProperty(key)) size++; }
return size;
}

map.setZoom(13); // Zoom in on the map!
mapFeatureLength = getSize(map._layers);

expect(mapFeatureLength).to.be.lessThan(featuresLength);
});
});
});
});

describe('geohashMinDistance method', function () {
it('should return a number', function () {
vis.handler.charts.forEach(function (chart) {
Expand Down

0 comments on commit b1fe261

Please sign in to comment.