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

杀马特。学长 韩版系。学妹 提交于 2019-12-06 06:18:41

问题


I am planning to create an interactive map with markers of hospital OHS incidents using Leaflet, Shiny and Shinydashboard along the lines of the following awesome template for interactive map and histogram

My problem is that I do not have a coordinate reference system as this is not a geographic object (no lat and long). Also it is in raster form.

How can I make the below floorplan into something with a CRS (coordinate reference system) that can be treated like a map.

That is, I want to be able to pan, zoom, add Markers etc.

There appears to be a way to do this using Java however I was hoping to do this in R as I am unfamiliar with Java. See Coordinates to space map


回答1:


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.




回答2:


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)


来源:https://stackoverflow.com/questions/38478139/add-coordinates-to-image-for-use-as-map-in-leaflet-shiny-and-shinydashboard-pac

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!