Skip to content

Commit

Permalink
add raster-values in control, check for whitespaces in layerId
Browse files Browse the repository at this point in the history
  • Loading branch information
trafficonese committed Aug 28, 2020
1 parent 653886e commit fc88895
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 7 deletions.
16 changes: 16 additions & 0 deletions R/addGeoRaster.R
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,22 @@ addGeotiff = function(map,
if (is.null(layerId)) layerId = group
layerId = gsub("\\.", "_", layerId)

if (grepl("\\s", layerId)) {
warning("The layerId is invalid. Maybe it contains spaces?")
}

## Add Legend Control if `imagequery` == TRUE
if (TRUE) {
ctrl_nm = paste("rasterValues", layerId, sep = "-")
map = leaflet::addControl(
map,
html = "",
layerId = ctrl_nm,
position = "topright",
className = paste("info legend rastervals", "className")
)
}

if (!is.null(file)) {
path_layer = tempfile()
dir.create(path_layer)
Expand Down
38 changes: 31 additions & 7 deletions inst/htmlwidgets/lib/georaster-for-leaflet/georaster-binding.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
function mouseHandler(mapId, georaster, layerId, group, eventName) {
function mouseHandler(map, georaster, layerId, group, eventName) {
return function(e) {
if (!HTMLWidgets.shinyMode) return;
let outputWidget = getInfoLegend(layerId);
if (!(map.layerManager.getVisibleGroups().includes(group))) {
$(outputWidget).hide();
return;
}

let latLng = this.mouseEventToLatLng(e.originalEvent);
var val = geoblaze.identify(georaster, [latLng.lng, latLng.lat]);
let val = geoblaze.identify(georaster, [latLng.lng, latLng.lat]);

if (val) {
outputWidget.innerHTML = renderInfo(val, layerId, 1, "");
let eventInfo = $.extend({
id: layerId,
".nonce": Math.random(), // force reactivity
Expand All @@ -12,12 +19,29 @@ function mouseHandler(mapId, georaster, layerId, group, eventName) {
},
e.latlng
);
Shiny.onInputChange(mapId + "_" + eventName, eventInfo);
if (HTMLWidgets.shinyMode) {
Shiny.onInputChange(map.id + "_" + eventName, eventInfo);
}
} else {
Shiny.onInputChange(mapId + "_" + eventName, null);
$(outputWidget).hide();
if (HTMLWidgets.shinyMode) {
Shiny.onInputChange(map.id + "_" + eventName, null);
}
}
};
}
function renderInfo(val, layerId, digits, prefix) {
$(document.getElementById("rasterValues-" + layerId)).show();
let text = "<small>"+ "Layer"+ " <strong> "+ layerId + ": </strong>"+ val + "</small>";
return text;
}
function getInfoLegend(layerId) {
let element = window.document.getElementById("rasterValues-" + layerId);
if (element === null) {
console.log("leafem: No control widget found in Leaflet setup. Can't show layer info.");
}
return element;
}


LeafletWidget.methods.addGeotiff = function (url,
Expand Down Expand Up @@ -86,8 +110,8 @@ LeafletWidget.methods.addGeotiff = function (url,
map.layerManager.addLayer(layer, "image", layerId, group);
map.fitBounds(layer.getBounds());

map.on("click", mouseHandler(map.id, georaster, layerId, group, "georaster_click"), this);
map.on("mousemove", mouseHandler(map.id, georaster, layerId, group, "georaster_mousemove"), this);
map.on("click", mouseHandler(map, georaster, layerId, group, "georaster_click"), this);
map.on("mousemove", mouseHandler(map, georaster, layerId, group, "georaster_mousemove"), this);
});
});

Expand Down

0 comments on commit fc88895

Please sign in to comment.