Public
Edited
Feb 14
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
viewof map = {
const container = html`<div id="map">`;
yield container; // Give the container dimensions.

const map = (container.value = new mapboxgl.Map({
container,
center: [-80.0721, 50.711],
zoom: 1.5,
style: "mapbox://styles/mapbox/light-v10",
scrollZoom: true
}));
invalidation.then(() => map.remove());


// Coverage area of the Hazard Mapping System (HMS) data, defined as:
// Upper left corner: 170°W 72°N
// Lower right corner: 50°W 14.6°N
const southWest = new mapboxgl.LngLat(-170, 14.6);
const northEast = new mapboxgl.LngLat(-50, 72);
const boundingBox = new mapboxgl.LngLatBounds(southWest, northEast);

// map.fitBounds(boundingBox);

// Setup our svg layer that we can manipulate with d3
const bb = container.getBoundingClientRect();
var svg = d3
.select(container)
.append("svg")
.style("position", "absolute")
.style("top", 0)
.style("left", 0)
.attr("width", bb.width)
.attr("height", bb.height)
.style("pointer-events", "none"); // the svg shouldn't capture mouse events, so we can have pan and zoom from mapbox

//Project any point to map's current state
function projectPoint(lon, lat) {
var point = map.project(new mapboxgl.LngLat(lon, lat));
this.stream.point(point.x, point.y);
}

function project(d) {
return map.project(new mapboxgl.LngLat(d[0], d[1]));
}
//Projection function
var transform = d3.geoTransform({ point: projectPoint });
var path = d3.geoPath().projection(transform);

//Draw geojson data with d3
var lines;
var points;

var tooltip = d3.select('body')
.append('div')
.attr('class', 'hidden tooltip');

function drawData(err) {
lines = svg.append("g")
.selectAll("path")
.data(smokeGeojson.features)
.enter()
.append("path")
.attr("class", function(d) {return d.properties["styleUrl"];})
.attr("fill", function(d) {return d.properties["fill"];})
// .attr("fill-opacity", function(d) {return d.properties["fill-opacity"];})
.attr("fill-opacity", 0.15)
.attr("stroke", function(d) {return d.properties["stroke"];})
.attr("stroke-opacity", function(d) {return d.properties["stroke-opacity"];})
.attr("d", path)
.on('mousemove', function(d) {
var mouse = d3.mouse(svg.node()).map(function(d) {
return parseInt(d);
});
tooltip.classed('hidden', false)
.attr('style', 'left:' + (mouse[0] + 15) +
'px; top:' + (mouse[1] - 35) + 'px')
.html(d.properties.routes_r_1);
})
.on('mouseout', function() {
tooltip.classed('hidden', true);;
});
points = svg.append("g")
.selectAll("circles")
.data(fireGeojson.features)
.enter()
.append("circle")
.attr("class", "fire")
.attr("r", 2)
.style("fill", "#ff0000")
.attr("fill-opacity", 0.05)
.attr("cx", function (d) {
return project(d.geometry.coordinates).x;
})
.attr("cy", function (d) {
return project(d.geometry.coordinates).y;
})
.on('mousemove', function(d) {
var mouse = d3.mouse(svg.node()).map(function(d) {
return parseInt(d);
});
tooltip.classed('hidden', false)
.attr('style', 'left:' + (mouse[0] + 15) +
'px; top:' + (mouse[1] - 35) + 'px')
.html(d.properties.routes_r_1);
})
.on('mouseout', function() {
tooltip.classed('hidden', true);;
});
;

map.on("viewreset", update);
map.on("move", update);
map.on("moveend", update);
}

function render() {
points
.attr("cx", function (d) {
return project(d.geometry.coordinates).x;
})
.attr("cy", function (d) {
return project(d.geometry.coordinates).y;
});
}

//update D3 shapes' positions to the map's current state
function update() {
console.log("update");
lines.attr("d", path);
render();
}

drawData();

update(500);

}
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
// https://satepsanone.nesdis.noaa.gov/pub/FIRE/web/HMS/Fire_Points/KML/2023/09/hms_fire20230920.kml
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
fireGeojson.features[0].geometry.coordinates[0]
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell

One platform to build and deploy the best data apps

Experiment and prototype by building visualizations in live JavaScript notebooks. Collaborate with your team and decide which concepts to build out.
Use Observable Framework to build data apps locally. Use data loaders to build in any language or library, including Python, SQL, and R.
Seamlessly deploy to Observable. Test before you ship, use automatic deploy-on-commit, and ensure your projects are always up-to-date.
Learn more