map: set initial view based on barycenter and standard deviation

This commit is contained in:
Nils Schneider 2015-04-06 15:54:26 +02:00
parent 11c689eb29
commit 315484625b
4 changed files with 29 additions and 21 deletions

View file

@ -1,5 +1,6 @@
define({
"dataPath": "https://map.luebeck.freifunk.net/data/",
"siteName": "Freifunk Lübeck",
"mapSigmaScale": 0.5,
"showContact": true
})

View file

@ -1,5 +1,5 @@
define(["d3"], function (d3) {
return function (linkScale, sidebar, router) {
return function (config, linkScale, sidebar, router) {
var self = this
var nodes, links
var svg, vis, link, node

View file

@ -26,7 +26,7 @@ function (chroma, Map, Sidebar, Tabs, Container, Meshstats, Linklist,
function addContent(K) {
removeContent()
content = new K(linkScale, sidebar, router)
content = new K(config, linkScale, sidebar, router)
contentDiv.appendChild(content.div)
if (latestData)

View file

@ -1,4 +1,4 @@
define(["leaflet", "moment", "leaflet.label"], function (L, moment) {
define(["d3", "leaflet", "moment", "leaflet.label"], function (d3, L, moment) {
var options = { worldCopyJump: true,
zoomControl: false
}
@ -54,9 +54,9 @@ define(["leaflet", "moment", "leaflet.label"], function (L, moment) {
var iconAlert = { color: "#D43E2A", radius: 6, fillOpacity: 0.5, weight: 2, className: "stroke-first node-alert" }
var iconNew = { color: "#558020", radius: 6, fillOpacity: 0.5, weight: 2, className: "stroke-first" }
return function (linkScale, sidebar, router) {
return function (config, linkScale, sidebar, router) {
var self = this
var groupOnline, groupOffline, groupNew, groupLost
var barycenter
var el = document.createElement("div")
el.classList.add("map")
@ -74,6 +74,22 @@ define(["leaflet", "moment", "leaflet.label"], function (L, moment) {
var nodeDict = {}
var linkDict = {}
function calcBarycenter(nodes) {
nodes = nodes.map(function (d) { return d.nodeinfo.location })
var lats = nodes.map(function (d) { return d.latitude })
var lngs = nodes.map(function (d) { return d.longitude })
var barycenter = L.latLng(d3.median(lats), d3.median(lngs))
var barycenterDev = [d3.deviation(lats), d3.deviation(lngs)]
var barycenterCircle = L.latLng(barycenter.lat + barycenterDev[0],
barycenter.lng + barycenterDev[1])
var r = barycenter.distanceTo(barycenterCircle)
return L.circle(barycenter, r * config.mapSigmaScale)
}
self.setData = function (data) {
nodeDict = {}
linkDict = {}
@ -81,6 +97,8 @@ define(["leaflet", "moment", "leaflet.label"], function (L, moment) {
var lines = addLinksToMap(linkDict, linkScale, data.graph.links, router)
L.featureGroup(lines).addTo(map)
barycenter = calcBarycenter(data.nodes.all.filter(has_location))
var nodesOnline = subtract(data.nodes.all.filter(online), data.nodes.new)
var nodesOffline = subtract(data.nodes.all.filter(offline), data.nodes.lost)
@ -101,10 +119,10 @@ define(["leaflet", "moment", "leaflet.label"], function (L, moment) {
return iconLost
}, router))
groupOffline = L.featureGroup(markersOffline).addTo(map)
groupOnline = L.featureGroup(markersOnline).addTo(map)
groupNew = L.featureGroup(markersNew).addTo(map)
groupLost = L.featureGroup(markersLost).addTo(map)
L.featureGroup(markersOffline).addTo(map)
L.featureGroup(markersOnline).addTo(map)
L.featureGroup(markersNew).addTo(map)
L.featureGroup(markersLost).addTo(map)
}
function resetMarkerStyles(nodes, links) {
@ -124,18 +142,7 @@ define(["leaflet", "moment", "leaflet.label"], function (L, moment) {
function resetView() {
resetMarkerStyles(nodeDict, linkDict)
var bounds = L.latLngBounds([])
bounds.extend(groupNew.getBounds())
bounds.extend(groupLost.getBounds())
if (!bounds.isValid())
bounds.extend(groupOnline.getBounds())
if (!bounds.isValid())
bounds.extend(groupOffline.getBounds())
if (bounds.isValid())
setView(bounds)
setView(barycenter.getBounds())
}
function goto(dict, id) {