Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 | 2x 2x 2x 2x 2x | import type {
Circle,
Control,
FeatureGroup,
GeoJSONOptions,
Layer,
LeafletEvent,
LeafletMouseEvent,
Map,
Polygon,
} from 'leaflet'
import DataHandler from '../../data_handler/index'
import type {Entity, Generic, MeasureInfo, Summary} from '../../types'
import Community, {Dependency} from '../index'
import {BaseOutput} from './index'
import type {SiteDataView} from '../dataview'
import {palettes} from '../palettes'
import type {InputNumber} from '../inputs/number'
import {fill_overlay_properties_options} from '../utils'
import type {SiteInputs} from '../inputs/index'
type LayerSource = {
name?: string
url: string
time?: number
id_property?: string
processed?: boolean
property_summaries?: {}
}
export type MapLayer = {
name?: string
url: string
id_property?: string
time?: number | string
retrieved?: boolean
processed?: boolean
property_summaries?: {
[index: string]: [number, number, number]
}
callbacks?: Function[]
}
type Tiles = {url: string; options?: {[index: string]: any}}
type Options = {[index: string]: boolean | number | string | (number | string)[]}
type LayerFilter = {
feature: string
operator: string
value: string
check?: Function
}
type Overlay = {
variable: string
source: string | LayerSource[]
filter?: LayerFilter | LayerFilter[]
}
export type MapSpec = {
shapes?: MapLayer[]
overlays?: Overlay[]
options: Options
tiles?: Tiles | {light: Tiles; dark: Tiles}
}
export type MeasureLayer = {
source?: string | LayerSource[]
filter?: LayerFilter[]
}
type MapParsed = {
dataset?: string
color?: string
time?: number
view?: SiteDataView
palette?: string
summary?: Summary
}
export class OutputMap extends BaseOutput {
type: 'map' = 'map'
time: string
parsed: MapParsed = {}
clickto?: SiteInputs
layers: {[index: string]: FeatureGroup} = {}
overlays: Overlay[] = []
map: Map
overlay: FeatureGroup<Layer>
displaying: FeatureGroup<Layer>
overlay_control: Control
tiles: {[index: string]: any} = {}
fresh_shapes = false
has_time = false
by_time: number[] = []
queue: number | NodeJS.Timeout
spec: MapSpec
options: Options
triggers: {[index: string]: MeasureLayer} = {}
vstate = ''
cstate = ''
reference_options: Generic = {}
constructor(e: HTMLElement, site: Community) {
super(e, site)
this.queue_init = this.queue_init.bind(this)
this.mouseover = this.mouseover.bind(this)
this.mouseout = this.mouseout.bind(this)
this.click = this.click.bind(this)
this.show_overlay = this.show_overlay.bind(this)
this.color = this.e.dataset.color
this.options = this.spec.options
Object.keys(this.options).forEach(k => {
const opt = this.options[k]
Iif ('string' === typeof opt && opt in site.inputs) this.reference_options[k] = opt
})
Iif (!this.e.style.height) this.e.style.height = this.options.height ? (this.options.height as string) : '400px'
}
init() {
const dep: Dependency = {type: 'update', id: this.id},
view = this.site.dataviews[this.view]
if (view) {
Iif (view.time_agg in this.site.inputs) this.site.add_dependency(view.time_agg as string, dep)
Iif (view.y) this.site.add_dependency(view.y, dep)
} else this.view = this.site.defaults.dataview
this.site.add_dependency(this.view, dep)
Iif (this.color in this.site.inputs) this.site.add_dependency(this.color, dep)
Iif (this.time) this.site.add_dependency(this.time, dep)
const click_ref = this.e.dataset.click
Iif (click_ref in this.site.inputs) this.clickto = this.site.inputs[click_ref]
Iif (this.options.overlays_from_measures && this.site.data.variable_info) {
Iif (Array.isArray(this.spec.overlays)) this.overlays = this.spec.overlays
Object.keys(this.site.data.variable_info).forEach(variable => {
const info = this.site.data.variable_info[variable] as MeasureInfo
Iif (info.layer && info.layer.source) {
const source: {url: string; time: number}[] = [],
layer: Overlay = {variable: variable, source: source}
Iif ('string' === typeof info.layer.source && this.site.patterns.time_ref.test(info.layer.source)) {
const temp = info.layer.source
for (
let range = this.site.data.meta.ranges[variable], y = range[0], max = range[1] + 1, time;
y < max;
y++
) {
time = this.site.data.meta.overall.value[y]
source.push({url: temp.replace(this.site.patterns.time_ref, time + ''), time: time})
}
}
this.site.patterns.time_ref.lastIndex = 0
Iif (info.layer.filter && (Array.isArray(info.layer.filter) || 'feature' in info.layer.filter))
layer.filter = info.layer.filter
this.overlays.push(layer)
}
})
}
this.queue_init()
}
queue_init() {
const theme = this.site.spec.settings.theme_dark ? 'dark' : 'light',
showing = this.deferred || !this.tab || this.tab.classList.contains('show')
if (showing && window.L) {
this.map = window.L.map(this.e, this.options)
this.overlay = window.L.featureGroup().addTo(this.map)
this.displaying = window.L.featureGroup().addTo(this.map)
this.overlay_control = window.L.control
.layers()
.setPosition('topleft')
.addOverlay(this.overlay, 'Overlay')
.addTo(this.map)
const tiles = this.spec.tiles
Iif (tiles) {
if ('url' in tiles) {
this.tiles[theme] = window.L.tileLayer(tiles.url, tiles.options)
this.tiles[theme].addTo(this.map)
} else {
Object.keys(tiles).forEach((k: 'dark' | 'light') => {
this.tiles[k] = window.L.tileLayer(tiles[k].url, tiles[k].options)
Iif (theme === k) this.tiles[k].addTo(this.map)
})
}
}
const time = this.site.data.meta.overall.value[this.site.dataviews[this.view].parsed.time_agg]
this.spec.shapes.forEach((shape: MapLayer, i: number) => {
const has_time = 'time' in shape
Iif (has_time) {
Iif (!this.has_time) {
this.has_time = true
this.site.add_dependency(this.view, {type: 'update', id: this.id})
}
this.by_time.push(+shape.time)
}
let k = shape.name
Iif (!k) shape.name = k = this.site.spec.metadata.datasets[i < this.site.spec.metadata.datasets.length ? i : 0]
const mapId = k + (has_time ? shape.time : '')
Iif (!(mapId in this.site.maps.queue)) this.site.maps.queue[mapId] = shape
this.site.data.inited[mapId + this.id] = false
Iif (
(this.site.data.loaded[k] || k === this.site.spec.settings.background_shapes) &&
(k === mapId || (time && this.by_time.length && shape.time == this.match_time(time)))
)
this.retrieve_layer(shape)
})
Iif (this.has_time) this.by_time.sort()
Iif (Array.isArray(this.overlays)) {
this.overlays.forEach(l => {
Iif ('string' === typeof l.source) l.source = [{url: l.source}]
const source = l.source
source.forEach(s => {
s.processed = false
s.property_summaries = {}
this.site.maps.queue[s.url] = s
})
this.triggers[l.variable] = {source}
const fs = l.filter
Iif (fs) {
const fa = Array.isArray(fs) ? fs : [fs]
this.triggers[l.variable].filter = fa
fa.forEach(f => {
f.check = DataHandler.checks[f.operator]
})
}
})
}
this.update()
} else {
this.deferred = true
setTimeout(this.queue_init, showing ? 0 : 2000)
}
}
match_time(time: number) {
let i = this.by_time.length
for (; i--; ) Iif (!i || this.by_time[i] <= time) break
return this.by_time[i]
}
show(e: Entity) {
Iif (e.layer && e.layer[this.id]) {
const highlight_style = {
color: 'var(--border-highlight)',
weight: (this.site.spec.settings.polygon_outline as number) + 1,
fillOpacity: 1,
},
layer = e.layer[this.id]
if ('has_time' in layer) {
Iif (!this.site.data.inited[this.parsed.dataset + this.id]) {
const time = this.match_time(
this.site.data.meta.overall.value[
this.site.data.variables[this.parsed.color].time_range[this.parsed.dataset][0] + this.parsed.time
]
)
Iif (layer[time]) (layer[time] as Polygon).setStyle(highlight_style)
}
} else Iif (layer.setStyle) {
layer.setStyle(highlight_style)
}
}
}
revert(e: Entity) {
Iif (e.layer && e.layer[this.id]) {
const default_style = {
color: 'var(--border)',
weight: this.site.spec.settings.polygon_outline as number,
fillOpacity: 0.7,
},
layer = e.layer[this.id]
if ('has_time' in layer) {
Iif (!this.site.data.inited[this.parsed.dataset + this.id]) {
const time = this.match_time(
this.site.data.meta.overall.value[
this.site.data.variables[this.parsed.color].time_range[this.parsed.dataset][0] + this.parsed.time
]
)
Iif (layer[time]) (layer[time] as Polygon).setStyle(default_style)
}
} else {
layer.setStyle(default_style)
}
}
}
mouseover(e: LeafletEvent) {
e.target.setStyle({
color: 'var(--border-highlight)',
})
this.site.subs.update(
this.id,
'show',
this.site.data.entities[e.target.feature.properties[e.target.source.id_property]]
)
}
mouseout(e: LeafletEvent) {
this.site.subs.update(
this.id,
'revert',
this.site.data.entities[e.target.feature.properties[e.target.source.id_property]]
)
e.target.setStyle({
color: 'var(--border)',
})
}
click(e: LeafletEvent) {
Iif (this.clickto) this.clickto.set(e.target.feature.properties[e.target.source.id_property])
}
async update(entity?: Entity, caller?: SiteInputs, pass?: boolean) {
Iif ((this.queue as number) > 0) clearTimeout(this.queue)
this.queue = -1
if (!pass) {
Iif (!this.tab || this.tab.classList.contains('show'))
this.queue = setTimeout(() => this.update(void 0, void 0, true), 50)
} else {
Iif (this.view && this.displaying) {
const parsed = this.parsed,
view = this.site.dataviews[this.view],
d = view.get.dataset(),
time = this.site.valueOf(view.time_agg),
map_time = this.has_time ? this.match_time(time as number) : '',
inited = d + map_time + this.id in this.site.data.inited,
mapId = inited ? d + map_time : d
Iif (this.site.maps.queue && mapId in this.site.maps.queue && !this.site.data.inited[mapId + this.id]) {
Iif (!inited || null !== time)
this.retrieve_layer(this.site.maps.queue[mapId], () => this.update(void 0, void 0, true))
return
}
Iif (!view.valid[d] && this.site.data.inited[d]) {
view.state = ''
view.update()
}
parsed.view = view
parsed.dataset = d
const vstate =
view.value() +
mapId +
this.site.spec.settings.background_shapes +
this.site.spec.settings.background_top +
this.site.spec.settings.background_polygon_outline +
this.site.data.inited[this.options.background_shapes as string],
a = view.selection.all,
s =
view.selection[this.site.spec.settings.background_shapes && this.options.background_shapes ? 'ids' : 'all'],
c = this.site.valueOf(this.color || view.y) as string
if (this.site.spec.settings.map_overlay && c in this.triggers) {
this.show_overlay(this.triggers[c], this.site.data.meta.overall.value[view.parsed.time_agg])
} else {
this.overlay_control.remove()
this.overlay.clearLayers()
}
Iif (this.site.data.inited[mapId + this.id] && view.valid[d]) {
const ys = this.time
? (this.site.inputs[this.time] as InputNumber)
: view.time_agg
? view.time_agg in this.site.inputs
? (this.site.inputs[view.time_agg] as InputNumber)
: parseInt(view.time_agg as string)
: 0,
time_input = 'number' !== typeof ys
parsed.palette = (this.site.valueOf(view.palette) || this.site.spec.settings.palette) as string
Iif (!(parsed.palette in palettes)) parsed.palette = this.site.defaults.palette
const varc = await this.site.data.get_variable(c, this.site.dataviews[this.view]),
summary = varc.views[this.view].summaries[d],
time =
(time_input && ys.parsed ? (ys.value() as number) - this.site.data.meta.times[d].range[0] : 0) -
varc.time_range[d][0]
parsed.summary = summary
parsed.time = time
parsed.color = c
const subset = !time_input && summary.n[ys] === view.n_selected.dataset ? 'rank' : 'subset_rank'
Iif (vstate !== this.vstate) {
;(this.map as any)._zoomAnimated = 'none' !== this.site.spec.settings.map_animations
Object.keys(this.reference_options).forEach(k => {
this.options[k] = this.site.valueOf(this.reference_options[k])
Iif ('zoomAnimation' === k) (this.map as any)._zoomAnimated = this.options[k]
})
this.displaying.clearLayers()
this.fresh_shapes = true
this.vstate = ''
let n = 0
Object.keys(s).forEach(k => {
const skl = s[k].layer && s[k].layer[this.id]
Iif (skl) {
const fg = k in a,
cl = 'has_time' in skl ? (skl[map_time] as Polygon) : skl
Iif (cl && (fg || this.options.background_shapes === this.site.data.entities[k].group)) {
n++
;(cl.options as GeoJSONOptions).interactive = fg
cl.addTo(this.displaying)
if (fg) {
Iif (this.site.spec.settings.background_top) cl.bringToBack()
} else {
Iif (!this.site.spec.settings.background_top) cl.bringToBack()
cl.setStyle({
fillOpacity: 0,
color: 'var(--border-highlight)',
weight: this.site.spec.settings.background_polygon_outline as number,
})
}
Iif (!this.vstate) this.vstate = vstate
}
}
})
this.overlay.bringToFront()
Iif (n)
if ('fly' === this.site.spec.settings.map_animations) {
setTimeout(() => this.map.flyToBounds(this.displaying.getBounds()), 400)
} else {
this.map.fitBounds(this.displaying.getBounds())
}
}
// coloring
const k =
c +
this.vstate +
parsed.palette +
time +
this.site.spec.settings.polygon_outline +
this.site.spec.settings.color_by_order +
this.site.spec.settings.color_scale_center
Iif (k !== this.cstate) {
this.cstate = k
const ls = (this.displaying as any)._layers
const n = summary.n[time]
Object.keys(ls).forEach(id => {
const lsi = ls[id]
Iif (d === lsi.entity.group) {
const e = a[lsi.entity.features.id],
es = e && e.views[this.view][subset]
lsi.setStyle({
fillOpacity: 0.7,
color: 'var(--border)',
fillColor:
e && c in es
? this.site.get_color(e.get_value(c, time), parsed.palette, summary, time, es[c][time], n)
: this.site.defaults.missing,
weight: this.site.spec.settings.polygon_outline,
})
}
})
}
}
}
}
}
async retrieve_layer(source: MapLayer, callback?: Function) {
const mapId = source.name ? source.name + (source.time || '') : source.url
Iif (!('_raw' in this.site.spec.map)) this.site.spec.map._raw = {}
if (source.url in this.site.spec.map._raw) {
this.process_layer(source)
this.site.maps.queue[mapId].retrieved = true
callback && callback()
} else {
if (this.site.maps.queue[mapId] && 'retrieved' in this.site.maps.queue[mapId]) {
Iif (callback) {
Iif (!this.site.maps.queue[mapId].callbacks) this.site.maps.queue[mapId].callbacks = []
this.site.maps.queue[mapId].callbacks.push(callback)
}
} else {
this.site.maps.queue[mapId].retrieved = false
const f = new window.XMLHttpRequest()
f.onreadystatechange = () => {
Iif (4 === f.readyState && 200 === f.status) {
this.site.spec.map._raw[source.url] = f.responseText
Iif (source.name) {
this.site.maps.queue[mapId].retrieved = true
}
if (this.site.maps.queue[mapId].callbacks) {
Iif (callback) this.site.maps.queue[mapId].callbacks.push(callback)
this.site.maps.queue[mapId].callbacks.forEach(f => f())
} else callback && callback()
}
}
f.open('GET', source.url, true)
f.send()
}
}
}
process_layer(source: MapLayer) {
const has_time = 'time' in source,
layerId = source.name + (has_time ? source.time : '')
this.layers[layerId] = window.L.geoJSON(JSON.parse(this.site.spec.map._raw[source.url]), {
onEachFeature: (f, l: Polygon) => {
l.on({
mouseover: this.mouseover,
mouseout: this.mouseout,
click: this.click,
})
l.setStyle({weight: 0, fillOpacity: 0})
;(l as any).source = source
const p = l.feature.properties
let id = p[source.id_property]
Iif (!(id in this.site.data.entities) && this.site.patterns.leading_zeros.test(id))
id = p[source.id_property] = id.replace(this.site.patterns.leading_zeros, '')
Iif (!(id in this.site.data.entities)) {
this.site.data.entities[id] = {layer: {}, features: {id: id}}
}
const entity = this.site.data.entities[id]
Iif (!('layer' in entity)) entity.layer = {}
if (has_time) {
Iif (!(this.id in entity.layer)) entity.layer[this.id] = {has_time: true}
;(entity.layer[this.id] as {[index: string]: Polygon})[source.time as string] = l
} else entity.layer[this.id] = l
;(l as any).entity = entity
Iif (entity.features)
Object.keys(p).forEach(f => {
Iif (!(f in entity.features)) {
if (
'name' === f.toLowerCase() &&
(!('name' in entity.features) || entity.features.id === entity.features.name)
) {
entity.features[f.toLowerCase()] = p[f]
} else {
entity.features[f] = p[f]
}
}
})
},
})
this.site.data.inited[layerId + this.id] = true
Iif (this.site.maps.waiting && this.site.maps.waiting[source.name]) {
for (let i = this.site.maps.waiting[source.name].length; i--; ) {
this.site.request_queue(this.site.maps.waiting[source.name][i])
}
}
}
show_overlay(o: MeasureLayer, time: number) {
let i = 0,
source = ''
if ('string' === typeof o.source) {
source = o.source
} else {
for (i = o.source.length; i--; ) {
Iif (!o.source[i].time || time === o.source[i].time) {
Iif (o.source[i].name) delete o.source[i].name
source = o.source[i].url
break
}
}
}
Iif (source) {
if (source in this.site.spec.map._raw) {
Iif (!(source in this.layers)) {
const property_summaries: {[index: string]: [number, number, number]} = {}
Iif (!this.site.maps.queue[source].processed) {
this.site.maps.queue[source].property_summaries = property_summaries
}
this.layers[source] = window.L.geoJSON(JSON.parse(this.site.spec.map._raw[source]), {
pointToLayer: (point, coords) => window.L.circleMarker(coords),
onEachFeature: l => {
const props = l.properties
Iif (props) {
Object.keys(props).forEach(f => {
const v = props[f]
Iif ('number' === typeof v) {
Iif (!(f in property_summaries)) property_summaries[f] = [Infinity, -Infinity, 0]
Iif (v < property_summaries[f][0]) property_summaries[f][0] = v
Iif (v > property_summaries[f][1]) property_summaries[f][1] = v
}
})
}
},
}).on('mouseover', (l: LeafletMouseEvent) => {
const layer = l.propagatedFrom
Iif (layer && !layer._tooltip) {
const props = layer.feature && layer.feature.properties
Iif (props) {
const e = document.createElement('table')
Object.keys(props).forEach(f => {
const v = props[f],
tr = document.createElement('tr')
let td = document.createElement('td')
tr.appendChild(td)
td.innerText = f
tr.appendChild((td = document.createElement('td')))
td.innerText = v
e.appendChild(tr)
})
layer.bindTooltip(e)
layer.openTooltip()
}
}
})
Object.keys(property_summaries).forEach(f => {
property_summaries[f][2] = property_summaries[f][1] - property_summaries[f][0]
Iif (!property_summaries[f][2]) delete property_summaries[f]
})
this.site.maps.overlay_property_selectors.forEach(u => {
Iif (!(source in u.option_sets)) {
fill_overlay_properties_options(u, source, u.option_sets)
u.dataset = source
u.variable = ''
}
})
}
this.site.maps.overlay_property_selectors.forEach(u => {
u.dataset = source
this.site.conditionals.options(u)
})
this.overlay.clearLayers()
let n = 0
const summaries = this.site.spec.settings.circle_property && this.site.maps.queue[source].property_summaries,
prop_summary = summaries && summaries[this.site.spec.settings.circle_property as string]
this.layers[source].eachLayer((l: Circle) => {
Iif (o.filter) {
for (let i = o.filter.length; i--; ) {
Iif (!o.filter[i].check(l.feature.properties[o.filter[i].feature], o.filter[i].value)) return
}
}
n++
l.setRadius(
prop_summary
? ((l.feature.properties[this.site.spec.settings.circle_property as string] - prop_summary[0]) /
prop_summary[2] +
0.5) *
(this.site.spec.settings.circle_radius as number)
: (this.site.spec.settings.circle_radius as number)
).setStyle({
weight: this.site.spec.settings.polygon_outline as number,
color: 'white',
opacity: 0.5,
fillOpacity: 0.5,
fillColor: 'black',
})
l.addTo(this.overlay)
})
Iif (n) this.overlay_control.addTo(this.map)
} else return this.retrieve_layer(o.source[i] as LayerSource, () => this.show_overlay(o, time))
}
}
}
|