Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closes #2010, Adds simple popup to assist with color recognition #2027

Open
wants to merge 6 commits into
base: Development
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
116 changes: 115 additions & 1 deletion js/symb/leafletMap.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,21 @@ function getObservationSvg(opts = {color: "#7A8BE7", size: 24, className:""}) {
});
}

async function getMacroStratData(lat, lng, zoom) {
return fetch(`https://macrostrat.org/api/v2/mobile/map_query_v2?lng=${lng}&lat=${lat}&z=${zoom}`)
.then(async response => {
if(!response.ok) {
return {};
}
const json_data = await response.json()

if(!json_data.success) {
return {};
}
return json_data.success.data;
})
}

function getSpecimenSvg(opts = {color: "#7A8BE7", size: 24, className:""}) {
const default_ops = {color: "#7A8BE7", size: 24};
opts = {...default_ops, ...opts};
Expand Down Expand Up @@ -142,6 +157,105 @@ class LeafletMap {
attribution: 'Map data: &copy; <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors, <a href="http://viewfinderpanoramas.org">SRTM</a> | Map style: &copy; <a href="https://opentopomap.org">OpenTopoMap</a> (<a href="https://creativecommons.org/licenses/by-sa/3.0/">CC-BY-SA</a>)'
});

const macro_strat_info = async e => {
const zoom = e.target._zoom;
const lat = e.latlng.lat;
const lon = e.latlng.lng;
const macro_strat_data = await getMacroStratData(lat, lon, zoom);

const loop_strat_names = (data) => {
let html_str = "";
for(let strat_name of data.macrostrat.strat_names) {
html_str += `<a target="_blank" href="https://macrostrat.org/sift/#/strat_name/${strat_name.strat_name_id}">${strat_name.rank_name}</a> `
}

return html_str;
}

if(macro_strat_data.mapData && macro_strat_data.mapData.length) {
let content = ""

if(macro_strat_data.mapData[0].name) {
content += `<div>
<span style="font-weight:bold">Unit: </span>
<span>${macro_strat_data.mapData[0].name}</span>
</div>`;
}

if(macro_strat_data.mapData[0].age) {
content+= `<div>
<span style="font-weight:bold">Age: </span>
<span>${macro_strat_data.mapData[0].age}</span>
</div>`;

}

if(macro_strat_data.mapData[0].ref) {
content += `<div style="font-size:0.8rem">
<span style="font-weight:bold">Source:</span>
${macro_strat_data.mapData[0].ref.authors}, ${macro_strat_data.mapData[0].ref.ref_title}: ${macro_strat_data.mapData[0].ref.ref_source}, ${macro_strat_data.mapData[0].ref.isbn_doi} ${macro_strat_data.mapData[0].ref.source_id} / ${macro_strat_data.mapData[0].map_id}
</div>`;
}

if(macro_strat_data.mapData[0].macrostrat && macro_strat_data.mapData[0].macrostrat.strat_names && macro_strat_data.mapData[0].macrostrat.strat_names.length) {
content += `<div style="margin-top:1rem">
<span style="font-weight:bold">Macrostrat matched units: </span>
${loop_strat_names(macro_strat_data.mapData[0])}
</div>`;
}

L.popup()
.setLatLng([lat, lon])
.setContent(`
<div style="font-size:1rem">
${content}
</div>`)
.openOn(this.mapLayer);
}
}


/* Alternative to using the api. Uses color inference. Back if we don't want to use macrostrat api*/
const macro_strat_color = (e) => {
const zoom = e.target._zoom;

let coords = this.mapLayer.project(e.latlng, zoom).floor();

let pX = coords.x / 256;
let pY = coords.y / 256;

coords.x = Math.floor(pX);
coords.y = Math.floor(pY);
coords.z = zoom

const tile = new Image();
tile.crossOrigin = "anonymous";
tile.src = `https://macrostrat.org/api/v2/maps/burwell/emphasized/${coords.z}/${coords.x}/${coords.y}/tile.png`;

const canvas = document.createElement("canvas");
const ctx = canvas.getContext("2d");

tile.addEventListener('load', function() {
ctx.drawImage(tile, 0, 0);
const dX = Math.floor((pX - coords.x) * 512);
const dY = Math.floor((pY - coords.y) * 512);
const pixel = ctx.getImageData(dX, dY, 1, 1);

ctx.fillRect(dX, dY, 10, 10);

const data = pixel.data;
console.log(`rgb(${data[0]} ${data[1]} ${data[2]} / ${data[3] / 255})`);
});
}

macro_strat.on('add', (e) => {
this.mapLayer.on('click', macro_strat_info)
})

macro_strat.on('remove', (e) => {
this.mapLayer.off('click', macro_strat_info)
})

if(map_options.layer_control !== false) {
const layers = {
"Terrain": terrainLayer,
Expand All @@ -150,7 +264,7 @@ class LeafletMap {
"Satellite": Esri_WorldImagery,
};
const overlays = {
"Macro Strat": L.layerGroup([macro_strat])
"Macrostrat": L.layerGroup([macro_strat])
}
L.control.layers(layers, overlays).addTo(this.mapLayer);
}
Expand Down