user1885116
user1885116

Reputation: 1797

Extracting colour values from map object

I have created a OpenStreetMap object, onto which i have plotted some points. Right now, i am trying to obtain the colour-values of the grid values of these points. (for example - if one of the points is in the ocean, i would expect a RGB colour value which is blue-ish). However - i am struggling to find the right way to access the colourData in the Map object (copy of structure per below), and then extract the values for the respective points.

Any high-level tips would be very welcome to help me on my way; Many thanks in advance, W

Code to create map object and plot points:

library(rJava)
library(OpenStreetMap)
library(ggplot2)
map <- openmap(c(70,-179),
               c(-70,179),zoom=1, type="mapquest-aerial")
map <- openproj(map)

reclat <- c(50,20,30,40)
reclong <- c(30,40,30,50)         
autoplot(map) + geom_point(aes(x=reclong,y=reclat))

Structure of map object

str(map)
List of 2
 $ tiles:List of 1
  ..$ :List of 5
  .. ..$ colorData : chr [1:106590] NA NA NA NA ...

Upvotes: 1

Views: 260

Answers (1)

Oscar Perpi&#241;&#225;n
Oscar Perpi&#241;&#225;n

Reputation: 4511

Convert the result of openmap to a Raster* object with raster, and then extract the points you need. The result is a matrix of three columns with the values of RGB. Use rgb to obtain the colors.

library(OpenStreetMap)
library(raster)

myMap <- openmap(c(70,-179),
               c(-70,179),zoom=1, type="mapquest-aerial")
myMap <- openproj(myMap)

reclat <- c(50,20,30,40)
reclong <- c(30,40,30,50)

rMap <- raster(myMap)

myPoints <- cbind(reclong, reclat)

myRGB <- extract(rMap, myPoints)
myColors <- rgb(myRGB[,1], myRGB[,2], myRGB[,3], maxColorValue=255)

Upvotes: 1

Related Questions