Skip to content

Commit

Permalink
first working poc for georaster
Browse files Browse the repository at this point in the history
  • Loading branch information
tim-salabim committed Jul 8, 2020
1 parent 2cb35f7 commit 7ae2b86
Show file tree
Hide file tree
Showing 4 changed files with 3,322 additions and 0 deletions.
70 changes: 70 additions & 0 deletions R/addGeoRaster.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
addGeoRaster = function(map,
file = NULL,
url = NULL,
group = NULL,
layerId = NULL) {

if (inherits(map, "mapview")) map = mapview2leaflet(map)

if (is.null(file) & is.null(url))
stop("need either file or url!\n", call. = FALSE)

if (is.null(group))
group = basename(tools::file_path_sans_ext(file))

if (is.null(layerId)) layerId = group
layerId = gsub("\\.", "_", layerId)

if (!is.null(file)) {
path_layer = tempfile()
dir.create(path_layer)
path_layer = paste0(path_layer, "/", layerId, "_layer.tif")

file.copy(file, path_layer, overwrite = TRUE)

map$dependencies <- c(
map$dependencies
, fileAttachment(path_layer, layerId)
, leafletGeoRasterDependencies()
)

leaflet::invokeMethod(
map
, data = leaflet::getMapData(map)
, method = "addGeoRaster"
, url
, group
, layerId
)
} else {
map$dependencies <- c(
map$dependencies
, leafletGeoRasterDependencies()
)

leaflet::invokeMethod(
map
, data = leaflet::getMapData(map)
, method = "addGeoRaster"
, url
, group
, layerId
)
}

}

leafletGeoRasterDependencies = function() {
list(
htmltools::htmlDependency(
"GeoRaster",
'0.0.1',
system.file("htmlwidgets/lib/georaster-for-leaflet", package = "leafem"),
script = c(
"georaster.min.js"
, "georaster-layer-for-leaflet.browserify.min.js"
, "georaster-binding.js"
)
)
)
}
29 changes: 29 additions & 0 deletions inst/htmlwidgets/lib/georaster-for-leaflet/georaster-binding.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
LeafletWidget.methods.addGeoRaster = function (url,
group,
layerId) {

var map = this;

var data_fl = document.getElementById(layerId + '-1-attachment');

if (data_fl === null) {
data_fl = url;
} else {
data_fl = data_fl.href;
}

fetch(data_fl)
.then(response => response.arrayBuffer())
.then(arrayBuffer => {
parseGeoraster(arrayBuffer).then(georaster => {
console.log("georaster:", georaster);
var layer = new GeoRasterLayer({
georaster: georaster,
resolution: 96
});
layer.addTo(map);
map.fitBounds(layer.getBounds());
});
});

};
Loading

0 comments on commit 7ae2b86

Please sign in to comment.