Add coordinates to image for use as map in Leaflet, Shiny and Shinydashboard packages in R

微笑、不失礼 提交于 2019-12-04 09:22:47

Here's a solution using mapview:

library(raster)
library(png)
library(mapview)

web_img <- "http://i.stack.imgur.com/8aSe9.png"

png <- readPNG(readBin(web_img, "raw", 1e6))

rst_blue <- raster(png[, , 1])
rst_green <- raster(png[, , 2])
rst_red <- raster(png[, , 3])

img <- brick(rst_red, rst_green, rst_blue)

m <- viewRGB(img)

m@map %>% addMarkers(lng = 0.5, lat = 0.5)

Note, that coordinates have their origin in the lower left corner of the image (0, 0) and, in this case scale to (0, 1) in the lower right corner and (0.859, 1) in the upper right corner to keep the aspect ration correct. Adding markers within this local coordinate reference system should be easy.

You can do:

library(raster)
b <- brick("8aSe9.png")

That gives you a four layer georeferenced RasterBrick object (RGB+alpha) you can look at with

plotRGB(b)

Of course the georeference is not relation to any other spatial object, but it seems that this may not matter to you.

If you want a single layer object (a RasterLayer) you can take any of the three layers (they are all the same)

r <- b[[1]]

or directly from the file:

r <- raster("8aSe9.png")

and then

image(r, col=gray(seq(0,1,.1)))
# or  plot(r, col=gray(seq(0,1,.1)), legend=F)
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!