-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1dde496
commit 80ae1e8
Showing
8 changed files
with
285 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
wmsheaderDependency <- function() { | ||
list( | ||
htmltools::htmlDependency( | ||
"lfx-wmsheader", | ||
version = "1.0.0", | ||
src = system.file("htmlwidgets/lfx-wmsheader", package = "leaflet.extras2"), | ||
script = c( | ||
"leaflet.wmsheader.js", | ||
"leaflet.wmsheader-bindings.js" | ||
) | ||
) | ||
) | ||
} | ||
|
||
#' Add WMS Layerwith custom Header | ||
#' | ||
#' @description | ||
#' Custom headers on Leaflet TileLayer WMS. It's a simple plugin that allow to | ||
#' set custom header for WMS interface. | ||
#' | ||
#' @inheritParams leaflet::addWMSTiles | ||
#' @inherit leaflet::addWMSTiles return | ||
#' @references \url{https://github.com/ticinum-aerospace/leaflet-wms-header} | ||
#' @family WMS Functions | ||
#' @export | ||
addWMSHeader <- function(map, baseUrl, layerId = NULL, group = NULL, | ||
options = WMSTileOptions(), | ||
attribution = NULL, | ||
layers = NULL, | ||
header = NULL, | ||
data = getMapData(map)) { | ||
if (is.null(layers)) { | ||
stop("layers is a required argument with comma-separated list of WMS layers to show") | ||
} | ||
options$attribution <- attribution | ||
options$layers <- layers | ||
|
||
map$dependencies <- c(map$dependencies, wmsheaderDependency()) | ||
|
||
invokeMethod( | ||
map, data, "addWMSHeader", baseUrl, layerId, | ||
group, options, header | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
library(shiny) | ||
library(leaflet) | ||
library(leaflet.extras2) | ||
|
||
|
||
ui <- fluidPage( | ||
leafletOutput("map", height = "500px") | ||
) | ||
|
||
base64pwd <- paste0("user:strongpwd") | ||
|
||
server <- function(input, output, session) { | ||
output$map <- renderLeaflet({ | ||
leaflet() %>% | ||
addTiles(group = "base") %>% | ||
addWMSHeader(baseUrl = "http://localhost:8080/geoserver/wms", | ||
layers = c("tiger:poi"), | ||
group = "wmsgroup", | ||
header = list( | ||
list("header" = "Authorization", | ||
"value" = paste0("Basic ", base64enc::base64encode(charToRaw(base64pwd))) ), | ||
list("header" = "content-type", | ||
"value" = "text/plain") | ||
), | ||
options = leaflet::WMSTileOptions( | ||
transparent = TRUE, format = "image/png")) %>% | ||
addLayersControl(baseGroups = "base", | ||
overlayGroups = c("tiger:poi")) | ||
|
||
}) | ||
} | ||
shinyApp(ui, server) | ||
|
||
|
12 changes: 12 additions & 0 deletions
12
inst/htmlwidgets/lfx-wmsheader/leaflet.wmsheader-bindings.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
LeafletWidget.methods.addWMSHeader = function(baseUrl, layerId, group, options, header) { | ||
|
||
console.log("addWMSHeader header:"); console.log(header) | ||
|
||
if(options && options.crs) { | ||
options.crs = LeafletWidget.utils.getCRS(options.crs); | ||
} | ||
|
||
// Add WMS source | ||
var source = L.TileLayer.wmsHeader(baseUrl, options, [header], null); | ||
this.layerManager.addLayer(source, "tile", layerId, group); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
'use strict'; | ||
|
||
async function fetchImage(url, callback, headers, abort) { | ||
let _headers = {}; | ||
if (headers) { | ||
headers.forEach(h => { | ||
_headers[h.header] = h.value; | ||
}); | ||
} | ||
const controller = new AbortController(); | ||
const signal = controller.signal; | ||
if (abort) { | ||
abort.subscribe(() => { | ||
controller.abort(); | ||
}); | ||
} | ||
const f = await fetch(url, { | ||
method: "GET", | ||
headers: _headers, | ||
mode: "cors", | ||
signal: signal | ||
}); | ||
const blob = await f.blob(); | ||
callback(blob); | ||
} | ||
|
||
L.TileLayer.WMSHeader = L.TileLayer.WMS.extend({ | ||
initialize: function (url, options, headers, abort, results) { | ||
L.TileLayer.WMS.prototype.initialize.call(this, url, options); | ||
this.headers = headers; | ||
this.abort = abort; | ||
this.results = results; | ||
}, | ||
createTile(coords, done) { | ||
const url = this.getTileUrl(coords); | ||
const img = document.createElement("img"); | ||
img.setAttribute("role", "presentation"); | ||
|
||
self = this; | ||
|
||
fetchImage( | ||
url, | ||
resp => { | ||
const reader = new FileReader(); | ||
reader.onload = () => { | ||
img.src = reader.result; | ||
if (self.results) { | ||
self.results.next(reader.result); | ||
}; | ||
}; | ||
reader.readAsDataURL(resp); | ||
done(null, img); | ||
}, | ||
this.headers, | ||
this.abort | ||
); | ||
return img; | ||
} | ||
}); | ||
|
||
L.TileLayer.wmsHeader = function (url, options, headers, abort, results) { | ||
return new L.TileLayer.WMSHeader(url, options, headers, abort, results); | ||
}; |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters