monkeyshines
monkeyshines

Reputation: 1078

Add coordinates to image for use as map

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 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.Ward 2 East

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

Upvotes: 5

Views: 2053

Answers (2)

TimSalabim
TimSalabim

Reputation: 5834

Here's a solution using mapview:

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

web_img <- "https://i.sstatic.net/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.

Upvotes: 1

Robert Hijmans
Robert Hijmans

Reputation: 47146

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)

Upvotes: 1

Related Questions