Local vs. remote images. editMap can't display local image files in RStudio.
I want to use Leaflet and mapEdit to display local images and draw polygons on them. I've almost got everything working, but I can't display local images in a leaflet with mapEdit. I think this might be a problem with sandboxing because Leaflet uses javascript via htmlwidgets. The local images display fine in leaflet view though (see comments below).
`library(leaflet)
library(mapview)
library(mapedit)
library(rgdal) #just in case - related to github Issue #130
# Works fine here with url access
rlogo_url <- "https://www.r-project.org/logo/Rlogo.svg"
lf_remote <- leaflet() %>% addLogo(img=rlogo_url) #looks good
editMap(lf_remote) #looks good
# Doesn't work with local access to downloaded file!
download.file(url=rlogo_url, destfile="./Rlogo.svg")
rlogo_file <- "./Rlogo.svg"
lf_local <- leaflet() %>% addLogo(img=rlogo_file, src = "local", alpha=1) #looks ok at this stage
editMap(lf_local) # but here I just get the blue question mark icon
## Also doesn't work with example code from documentation (uses local system/R logo)
library(png)
img_sys <- system.file("img", "Rlogo.png", package="png")
lf_local2 <- leaflet() %>% addLogo(img=img_sys, src = "local", alpha = 1) #looks ok at this stage
editMap(lf_local2) # but here I just get the blue question mark icon again
#the path for the local image files looks a little weird here
cat(lf_remote$jsHooks$render[[1]]$code)
cat(lf_local$jsHooks$render[[1]]$code)
cat(lf_local$jsHooks$render[[1]]$code)
#but "fixing this path doesn't solve the problem
lf_local$jsHooks$render[[1]]$code <- sub("../graphs/", "file:///C:/Users/Eli/Desktop/", lf_local$jsHooks$render[[1]]$code)
editMap(lf_local) # still only get blue question mark icon
`
When I provide the image URL, I can see the Rlogo image in the Viewer panel (RStudio). If I download the image and provide the file path to addLogo, then I see a blue question mark icon in place of the image. Same thing using the local R logo. A couple things that are puzzling to me:
- The local images display fine when I call the leaflet object. They only look bad (Blue ? mark) when I use the editMap() function on a leaflet object from a local image.
- Digging through the contents of the leafet objects, I found that the local and remote leaflets were identical except in their "img src=" locations. The remote version has html source as expected, but the local source was surprising because I have no idea where this directory is:
<img src="../graphs/Rlogo.svg" <img src="../graphs/Rlogo.png"
Changing this line does not fix the editMap display however
lf_local$jsHooks$render[[1]]$code <- sub("../graphs/", "file:///C:/Users/Eli/Desktop/", lf_local$jsHooks$render[[1]]$code)
editMap(lf_local) # still only get blue question mark icon
I'd love to hear if there's a fix or workaround for this problem. Thanks! Eli
Hi, the issue is that the html needs relative paths to work everywhere. As we cannot know the location of the index.html file created by leaflet, we have to save the logo locally in the directory where we know the folder containing index.html resides - tempfolder(). This is where we save/copy the local image to and tell the map where to find it - ../graphs/.
The easiest would be to embed the image in the html file - as we can already do with popupImage. I'll investigate this option.
Thanks for looking into this. I wasn't aware of the R temp directory location so this was good for me to learn!
Just for fun I tried editing the leaflet path explicitly to this temp directory, but it still didn't work with editMap().
#try fixing the path with tmp folder location
tmp_folder <- tempdir()
tmp_folder <- gsub("\\\\","/",tmp_folder) #portability
svg_file_loc <- paste("file:///", tmp_folder, "/Rlogo.svg", sep="")
lf_local$jsHooks$render[[1]]$code <- sub("../graphs/Rlogo.svg", svg_file_loc, lf_local$jsHooks$render[[1]]$code)
editMap(lf_local) # still doesn't work, get an empty rectangle instead of the blue question mark icon
I'm interested to use leaflet and mapedit to display and subset datasets by xy scatterplot in R. Not geographical at all. From reading the documentation, it seemed like addLogo was the best way to do this. I'd be very interested to hear if there are better ways. Maybe with rasterImage or something along these lines? https://leafletjs.com/examples/crs-simple/crs-simple.html
Thanks a lot for the quick reply and looking in to this. Hope we can get it pointing at local files soon, and I'm happy to help if there's any way I can (I know R but not much java). Eli
I am not quite sure I understand your intention correctly. Why do you wanto to use a raster display when dealing with xy scatterplots?
mapview has scatterplot facilities (either from a data.frame or two numeric vectors), so you could just set-up your "map" using e.g.
library(mapview)
library(mapedit)
df = data.frame(x = 1:10, y = 1:10)
m = mapview(df, xcol = "x", ycol = "y")
editMap(m, targetLayerId = "data")
Thanks for taking the time to look into my questions and think about solutions. I want to use raster display (or really just image files - maybe this is the same thing?) because my xy datasets have millions of points and this tends to bog down the performance of normal scatterplot visualization. My "workaround" solution to this performance problem is to just use an image of the scatterplot instead. If there are other strategies that work better for this I'd be very interested to hear about them. Perhaps a bit outside the scope of mapedit though - not your job to solve this for me! Although I think the potential of the leaflet/mapedit draw tools for non-spatial/geographic data analysis is really powerful.
Ok I think I get it now.
Unfortunately, I don't think this is easily achievable. The draw tools for leaflet need projected map coordinates and will return unprojected longitude/latitude values. Hence, this will only work if your image coordinates are between -180/180 (x) and -90/90 (y). Or if you can somehow rescale everything to be between 0 and 1.
There is a leaflet plugin to deal with non-geographic image data that provides ways to project polygons etc to image coordinates https://github.com/commenthol/leaflet-rastercoords, however I am not sure whether leaflet.draw (what mapedit uses) understands those coordinates (it may as it simply collects what has been drawn if I am not mistaken). However, leaflet.rastercoords is currently not available through R. There are others too, https://leafletjs.com/plugins.html#non-map-base-layers but I don't think any of these has been made available from R so far.
The easiest would be to create a leaflet add-on package that provides raster.coords functionlity (along the lines of leaflet.extras). However, at this stage, I don't think I can help much, as non of these are high on my priority list.