Public
Edited
Nov 16, 2023
Insert cell
Insert cell
L = require('leaflet@1.2.0')
Insert cell
h = html`<link href='${resolve('leaflet@1.2.0/dist/leaflet.css')}' rel='stylesheet' />`
Insert cell
Insert cell
//We are writing the event listener function for each feature in our geojson layer
function onEachFeature(feature, layer) {
layer.on({
click:whenClicked //callback function
});
}
Insert cell
//We update our data in the function using mutable. This overrides observables defaults so we can push data from within //function scope globally
whenClicked = (event) => {
mutable nHood = event.target.feature.properties.col_id
}
Insert cell
//We define an null variable to be replaced by our callback function
mutable nHood = null
Insert cell
//mapSelection = {
//if(nHood == null ){
// return Array.from(new Set(macrostratMap.map(d => d.NEIGHBOURHOOD))) //Set is a new datastructure in ES6 -> only unique values
//} else {
// return [nHood]
//}
//}
Insert cell
macrostratColumns = {
let response = await fetch('https://macrostrat.org/api/v2/columns?all&format=geojson_bare');
let json = await response.json();
return json
}
Insert cell
viewof macrostratMap = {
// You'll often see Leaflet examples initializing a map like L.map('map'),
// which tells the library to look for a div with the id 'map' on the page.
// In Observable, we instead create a div from scratch in this cell, so it's
// completely self-contained.
let container = DOM.element('div', { style: `width:${width/1.7}px;height:${width/2.5}px` });
// Note that I'm yielding the container pretty early here: this allows the
// div to be placed on the page. This is important, because Leaflet uses
// the div's .offsetWidth and .offsetHeight to size the map. If I were
// to only return the container at the end of this method, Leaflet might
// get the wrong idea about the map's size.
yield container;
// Now we create a map object and add a layer to it.
let map = L.map(container).setView([38.896, -77.0459], 11);
let osmLayer = L.tileLayer('https://{s}.basemaps.cartocdn.com/light_all/{z}/{x}/{y}@2x.png', {
attribution: '&copy; <a href="http://osm.org/copyright">OpenStreetMap</a> contributors'
}).addTo(map);
let macrostratColumnLayer = L.geoJson(macrostratColumns, {
weight: 1,
color: '#432',
onEachFeature: onEachFeature
}).bindPopup(function (Layer) { //binds a popup when clicking on each polygon to access underlying data
return Layer.feature.properties.col_id;
}).addTo(map); //Adds the layer to the map.
map.on('click', () =>
mutable nHood = null
);
map.fitBounds(macrostratColumnLayer.getBounds());
}
Insert cell
Insert cell
heatLayer = L, require('leaflet.heat').catch(() => L.heatLayer)
Insert cell
Insert cell
crimes = (await fetch('https://opendata.arcgis.com/datasets/6af5cb8dc38e4bcbac8168b27ee104aa_38.geojson')).json()
Insert cell
crimeMap = {
// You'll often see Leaflet examples initializing a map like L.map('map'),
// which tells the library to look for a div with the id 'map' on the page.
// In Observable, we instead create a div from scratch in this cell, so it's
// completely self-contained.
let container = DOM.element('div', { style: `width:${width}px;height:${width/1.6}px` });
// Note that I'm yielding the container pretty early here: this allows the
// div to be placed on the page. This is important, because Leaflet uses
// the div's .offsetWidth and .offsetHeight to size the map. If I were
// to only return the container at the end of this method, Leaflet might
// get the wrong idea about the map's size.
yield container;
// Now we create a map object and add a layer to it.
let map = L.map(container).setView([38.896, -77.0459], 5);
let osmLayer = L.tileLayer('https://{s}.basemaps.cartocdn.com/light_all/{z}/{x}/{y}@2x.png', {
attribution: '&copy; <a href="http://osm.org/copyright">OpenStreetMap</a> contributors'
}).addTo(map);
// Process DC's crime data into the [latitude, longitude, intensity] form that
// Leaflet.heat desires.
let crimePoints = crimes.features.map(feature =>
feature.geometry.coordinates.slice().reverse().concat([0.1]));
let crimeLayer = heatLayer(crimePoints).addTo(map);
}
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more