map = {
let map = this;
if (!map) {
map = new mapboxgl.Map({
container,
style: r,
center: [-127, 51.8],
scrollZoom: false,
zoom: 5.3,
maxZoom: 12,
minZoom: 4
});
}
const popup = new mapboxgl.Popup({
closeButton: false,
closeOnClick: false
});
map.on("load", function () {
map.addControl(new mapboxgl.NavigationControl());
const addedImages = new Set();
map.on("styleimagemissing", function (e) {
console.log("Handling missing image:", e.id);
});
const loadImages = async () => {
for (const item of groupsData) {
if (!item.file || item.group === undefined) continue;
// Convert group to string and check if we've already added it
const groupId = String(item.group);
if (addedImages.has(groupId)) {
console.log(`Image for group ${groupId} already added, skipping`);
continue;
}
try {
const iconImage = await item.file.image();
console.log(`Adding image for group: ${groupId}`);
map.addImage(groupId, iconImage);
addedImages.add(groupId);
} catch (error) {
console.error(`Error adding image for group ${groupId}:`, error);
}
}
console.log("All images processed");
// Now you can add your layers that use these images
// addLayers();
};
loadImages();
});
map.on("mouseenter", "hexbins", function (e) {
map.getCanvas().style.cursor = "default";
const coordinates = e.features[0].geometry.coordinates.slice();
const properties = e.features[0].properties;
// mutable debug = properties;
const catText =
properties.Location === undefined ? "none" : properties.Organization;
while (Math.abs(e.lngLat.lng - coordinates[0]) > 180) {
coordinates[0] += e.lngLat.lng > coordinates[0] ? 360 : -360;
}
// make the date more readable
let firstCatch = new Date(properties.firstCatch).toLocaleString(undefined, {
timeZone: "utc",
year: "numeric",
month: "numeric",
day: "numeric"
});
const dataLine = dataSentinels
.filter((d) => d.Location === properties.Location)
.sort((a, b) => a.Date - b.Date);
// mutable debug = properties;
// if (d3.sum(dataLine, (d) => d.total) > 0) {
dataLine.map((d) => {
console.log(new Date(d.Date));
});
popup
.setLngLat(coordinates)
.setHTML(
`<div>${catText} <br> Location: ${properties.Location} <br> Date of first dungeness: ${firstCatch} <br> Total over last 3 days: ${properties.total} <br> Total dungeness caught to date: ${properties.totalTODate} </div><svg class="popup-d3-svg" style="margin: 0 auto; display: block;"></svg>`
)
.addTo(map);
const width1 = 125,
height1 = 25;
const svgElement = popup
.getElement()
.getElementsByClassName("popup-d3-svg")[0];
const svg = d3
.select(svgElement)
.attr("width", width1)
.attr("height", height1);
// .style("background-color", "black");
const margin = { top: 2, right: 3, bottom: 1, left: 4 };
const x1 = d3
.scaleTime()
.domain(d3.extent(dataLine, (d) => d.Date))
.range([margin.left, width1 - margin.right]);
const y1 = d3
.scaleLinear()
.domain(d3.extent(dataLine, (d) => d.total))
.nice()
.range([height1 - margin.bottom, margin.top]);
svg
.append("path")
.datum(dataLine)
.attr("fill", "none")
.attr("stroke", "steelblue")
.attr("stroke-width", 1.5)
.attr("stroke-linejoin", "round")
.attr("stroke-linecap", "round")
.attr(
"d",
d3
.line()
.x((d) => x1(d.Date))
.y((d) => y1(d.total))
);
map.on("mouseleave", "hexbins", function () {
map.getCanvas().style.cursor = "";
popup.remove();
});
map.on("click", "hexbins", function (e) {
// console.log(e);
mutable siteClicked = e.features[0].properties.Location;
container.value.selected = {
lng: e.lngLat.lng,
lat: e.lngLat.lat,
data: e.features[0].properties
};
container.dispatchEvent(new CustomEvent("input", { bubbles: true }));
});
});
// invalidation.then(() => map.remove());
// Wait until the map loads, then yield the container again.
yield new Promise((resolve) => {
if (map.loaded()) resolve(map);
else map.on("load", () => resolve(map));
});
let fc = {
type: "FeatureCollection",
features: hexgeo
};
if (map._loaded) {
if (!map.getSource("hexbins")) {
map.addSource("hexbins", {
type: "geojson",
data: fc
});
} else {
map.getSource("hexbins").setData(fc);
}
if (map.getLayer(hexbinLayer.id)) {
map.removeLayer(hexbinLayer.id);
}
// console.log("adding layer")
map.addLayer(hexbinLayer);
// map.flyTo(projection.invert([100,1000]))
// console.log("flying")
// }
return true;
}
}