Skip to content

Commit

Permalink
Merge pull request #101 from trafficonese/fgb_highlight
Browse files Browse the repository at this point in the history
Fgb highlight fix
  • Loading branch information
tim-salabim authored Dec 14, 2024
2 parents c9c1183 + b1041c7 commit 2c3c947
Show file tree
Hide file tree
Showing 15 changed files with 149 additions and 88 deletions.
1 change: 1 addition & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -91,4 +91,5 @@ Suggests:
terra,
tools
Encoding: UTF-8
Roxygen: list(markdown = TRUE)
RoxygenNote: 7.3.2
37 changes: 28 additions & 9 deletions R/file.R
Original file line number Diff line number Diff line change
Expand Up @@ -152,17 +152,13 @@ addLocalFile = function(map,

map$dependencies <- c(
map$dependencies,
leafletFileDependencies(),
fileDependency(
fn = path_outfile,
layerId = layerId
)
)

map$dependencies <- c(
map$dependencies,
leafletFileDependencies()
)

leaflet::invokeMethod(
map,
leaflet::getMapData(map),
Expand Down Expand Up @@ -308,6 +304,33 @@ addTileFolder = function(map,
#' @inheritParams leaflet::addPolylines
#' @param ... currently not used.
#'
#' @details
#' Styling options in `addFgb` offer flexibility by allowing
#' users to either specify styles directly as function arguments or define them
#' as attributes in the data object:
#'
#' - **Direct Styling:** You can pass style arguments (e.g., `color`, `weight`,
#' `opacity`) directly to the function. These will apply uniformly to all features
#' in the layer.
#' - **Attribute-based Styling:** Alternatively, you can include styling properties
#' (e.g., `color`, `fillColor`, `weight`) as columns in your data object before
#' writing it to an FGB file. Set the corresponding arguments in `addFgb` to
#' `NULL`, and the function will use these attributes for styling during map
#' rendering.
#'
#' For example:
#' ```R
#' ## using custom `color`
#' data$color <- colorNumeric(palette = "viridis", domain = data$var)(data$var)
#' sf::st_write(obj = data, dsn = "myfile.fgb", driver = "FlatGeobuf")
#' leafem::addFgb(file = "myfile.fgb", color = NULL)
#'
#' ## using custom `fillColor`
#' data$fillColor <- colorNumeric(palette = "viridis", domain = data$var)(data$var)
#' sf::st_write(obj = data, dsn = "myfile.fgb", driver = "FlatGeobuf")
#' leafem::addFgb(file = "myfile.fgb", fill = TRUE, fillColor = NULL)
#' ```
#'
#' @examples
#' if (interactive()) {
#' library(leaflet)
Expand Down Expand Up @@ -414,10 +437,6 @@ addFgb = function(map,
map$dependencies
, fgbDependencies()
, chromaJsDependencies()
)

map$dependencies = c(
map$dependencies
, fileAttachment(path_layer, group)
)

Expand Down
22 changes: 16 additions & 6 deletions inst/htmlwidgets/lib/FlatGeoBuf/fgb.js
Original file line number Diff line number Diff line change
Expand Up @@ -146,11 +146,9 @@ LeafletWidget.methods.addFlatGeoBuf = function (layerId,
},
// remove highlight when hover stops
'mouseout': function(e) {
const layer = e.target;
if (e.layer.feature.properties.color) {
style.color = e.layer.feature.properties.color
}
layer.setStyle(style);
const layer = e.layer;
let oldstyle = updateStyleFromProperties(structuredClone(style), layer.feature.properties);
layer.setStyle(oldstyle);
if (highlightOptions.sendToBack) {
layer.bringToBack();
}
Expand Down Expand Up @@ -221,7 +219,6 @@ function makePopup(popup, className) {
return pop;
}


function json2table(json, cls) {
let cols = Object.keys(json);
let vals = Object.values(json);
Expand Down Expand Up @@ -286,6 +283,18 @@ function updateStyle(style_obj, feature, scale, scaleValues) {

return out;
}
function updateStyleFromProperties(style, props) {
const keysToUpdate = ['stroke', 'color', 'weight', 'opacity',
'fill', 'fillColor', 'fillOpacity', 'dashArray'];
// Create a shallow copy of style to avoid mutating the original object
const updatedStyle = { ...style };
keysToUpdate.forEach(key => {
if (updatedStyle[key] === null && props[key]) {
updatedStyle[key] = props[key];
}
});
return updatedStyle
}


function rescale(value, to_min, to_max, from_min, from_max) {
Expand All @@ -297,6 +306,7 @@ function rescale(value, to_min, to_max, from_min, from_max) {




LeafletWidget.methods.addFlatGeoBufFiltered = function (layerId,
group,
url,
Expand Down
3 changes: 2 additions & 1 deletion leafem.Rproj
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
Version: 1.0
ProjectId: a2cce78f-b118-47fb-83fb-d62c6c6506d5

RestoreWorkspace: Default
SaveWorkspace: Default
Expand All @@ -16,6 +15,8 @@ LaTeX: pdfLaTeX
StripTrailingWhitespace: Yes

BuildType: Package
PackageUseDevtools: Yes
PackageCleanBeforeInstall: No
PackageInstallArgs: --no-multiarch --with-keep.source
PackageBuildArgs: --no-manual
PackageCheckArgs: --as-cran --no-manual
Expand Down
4 changes: 2 additions & 2 deletions man/addCOG.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

52 changes: 40 additions & 12 deletions man/addFgb.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions man/addLogo.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 7 additions & 7 deletions man/addMouseCoordinates.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 6 additions & 6 deletions man/addPMPolygons.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions man/addRasterRGB.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 8 additions & 8 deletions man/addReactiveFeatures.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion man/addTileFolder.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 2c3c947

Please sign in to comment.