-
Notifications
You must be signed in to change notification settings - Fork 30
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
2cb35f7
commit 7ae2b86
Showing
4 changed files
with
3,322 additions
and
0 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
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
29
inst/htmlwidgets/lib/georaster-for-leaflet/georaster-binding.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,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()); | ||
}); | ||
}); | ||
|
||
}; |
Oops, something went wrong.