Reputation: 105
I geo-referenced data from a picture in QGIS and tried exporting it as a R raster for use in a proyect (r markdown) like this:
r.raster<-raster("A:/Path/R-raster.gri",
varname = "r_raster_file")
leaflet(df) %>% addMarkers(popup = ~descr,label = ~Iden) %>%
addRasterImage(r.raster)
the problem I have is that the colors of the image came all wrong. When I open the data in QGIS the colors seem fine, however, the raster layer read by leaflet has totally different colors:
The original colors are at the bottom.
The object in the enviroment has an structure like this:
Formal class 'RasterLayer' [package "raster"] with 12 slots
..@ file :Formal class '.RasterFile' [package "raster"] with 13 slots
.. .. ..@ name : chr "R-raster.grd"
.. .. ..@ datanotation: chr "INT1U"
.. .. ..@ byteorder : Named chr "little"
.. .. .. ..- attr(*, "names")= chr "value"
.. .. ..@ nodatavalue : num -Inf
.. .. ..@ NAchanged : logi FALSE
.. .. ..@ nbands : int 3
.. .. ..@ bandorder : Named chr "BIL"
.. .. .. ..- attr(*, "names")= chr "value"
.. .. ..@ offset : int 0
.. .. ..@ toptobottom : logi TRUE
.. .. ..@ blockrows : int 0
.. .. ..@ blockcols : int 0
.. .. ..@ driver : chr "raster"
.. .. ..@ open : logi FALSE
..@ data :Formal class '.SingleLayerData' [package "raster"] with 13 slots
.. .. ..@ values : logi(0)
.. .. ..@ offset : num 0
.. .. ..@ gain : num 1
.. .. ..@ inmemory : logi FALSE
.. .. ..@ fromdisk : logi TRUE
.. .. ..@ isfactor : logi FALSE
.. .. ..@ attributes: list()
.. .. ..@ haveminmax: logi TRUE
.. .. ..@ min : num 0
.. .. ..@ max : num 255
.. .. ..@ band : int 1
.. .. ..@ unit : chr ""
.. .. ..@ names : chr "Someros.R.raster"
..@ legend :Formal class '.RasterLegend' [package "raster"] with 5 slots
.. .. ..@ type : chr(0)
.. .. ..@ values : logi(0)
.. .. ..@ color : logi(0)
.. .. ..@ names : logi(0)
.. .. ..@ colortable: logi(0)
..@ title : chr(0)
..@ extent :Formal class 'Extent' [package "raster"] with 4 slots
.. .. ..@ xmin: num -9390940
.. .. ..@ xmax: num -9327759
.. .. ..@ ymin: num 1095039
.. .. ..@ ymax: num 1127491
..@ rotated : logi FALSE
..@ rotation:Formal class '.Rotation' [package "raster"] with 2 slots
.. .. ..@ geotrans: num(0)
.. .. ..@ transfun:function ()
..@ ncols : int 1246
..@ nrows : int 640
..@ crs :Formal class 'CRS' [package "sp"] with 1 slot
.. .. ..@ projargs: chr "+proj=merc +a=6378137 +b=6378137 +lat_ts=0 +lon_0=0 +x_0=0 +y_0=0 +k=1 +units=m +nadgrids=@null +wktext +no_defs"
.. .. ..$ comment: chr "PROJCRS[\"WGS 84 / Pseudo-Mercator\",\n BASEGEOGCRS[\"WGS 84\",\n DATUM[\"World Geodetic System 1984\"| __truncated__
..@ history : list()
..@ z : list()
I searched the internet for hours and I am at a stall. The colors are significant since they represent data from geological studies. I've tried stuff like this:
pal <- colorNumeric(c("red","blue","green"), values(someros))
leaflet(df) %>% addMarkers(popup = ~descr,label = ~Iden) %>%
addRasterImage(someros,color=pal)
but I don´t know what I'm doing
Help appreciated.
Edit:here's the content of the grd file
[general]
[georeference]
nrows=640
ncols=1246
xmin=-9390939.65870000049
ymin=1095038.68050000002
xmax=-9327758.77869999968
ymax=1127491.1388999999
projection=+proj=merc +a=6378137 +b=6378137 +lat_ts=0 +lon_0=0 +x_0=0 +y_0=0 +k=1 +units=m +nadgrids=@null +wktext +no_defs
[data]
datatype=INT1U
byteorder=little
nbands=3
bandorder=BIL
minvalue=0:0:0
maxvalue=255:255:255
categorical=FALSE
Upvotes: 0
Views: 249
Reputation: 47601
With this image
You can read it like this:
library(terra)
img <- rast("https://i.sstatic.net/ZyyaD.jpg")
And get the extent and crs from the other file (or set it directly)
#r <- rast("Someros-R-raster.grd")
#crs(img) <- crs(r)
#ext(img) <- ext(r)
crs(img) <- "+proj=merc +a=6378137 +b=6378137 +lat_ts=0 +lon_0=0 +x_0=0 +y_0=0 +k=1 +units=m"
ext(img) <- c(-9390939.6587, -9327758.7787, 1095038.6805, 1127491.1389)
plot(img)
Save to disk
writeRaster(img, "temp.tif")
This is a SpatRaster with three layers (red, green, blue) and you can reduce it to one layer (with a color-table) like this
img2 <- colorize(img, "col")
writeRaster(img2, "temp2.tif")
If you want to use the old raster objects, you can read the file like this
library(raster)
x <- raster("temp.tif")
plot(x)
Upvotes: 1