Question

I use getBinaryURL of curl packet to receive raster data (png, tiff, ...) from the web and save data to disk. This raster data is later used for spatial analysis where I currently use raster (and rgdal) packet to load raster data and create a raster object.

library(RCurl)
url<-"http://demo.mapserver.org/cgi-bin/wms?version=1.1.1&service=WMS&request=GetMap&VERSION=1.1.1&LAYERS=continents&WIDTH=500&HEIGHT=500&BBOX=0,45,15,60&SRS=EPSG:4326&FORMAT=image/png"
map_png = getBinaryURL(url)
#saving the png to disk
map_file <- file("C:/Users/.../map.png", "wb")
writeBin(map_png, map_file)
close(map_file)

#loading png as raster
library(raster)
map <- raster("C:/Users/.../map.png")
#projection and extend
projection(map)<-"+proj=longlat +datum=WGS84 +ellps=WGS84 towgs84=0,0,0"
map@extent@xmin<-0
map@extent@xmax<-15
map@extent@ymin<-45
map@extent@ymax<-60

It works, however the saving and loading part of the raw data is not really nice. So what I like is to pass the "raw" (map_png) object directly to a "RasterLayer" (map) object. Like this:

map <- raster(map_png)

Anyone aware how to archiv this?

-

I know I can decode png or use packet png to decode, but with a lot of different input formats this is not a desire way. To be more specific I ad example for time gain working direct with the binary object already available after fetching with getBinaryURL()

system.time(
for(i in 1:200){
map_file <- file("C:/.../map.png", "wb")
writeBin(map_png, map_file)
close(map_file)
map <- raster("C:/.../map.png")
}
)

system.time(
for(i in 1:200){
xx <- readPNG(map_png)
map <- raster(xx[,,2])
}
)
Was it helpful?

Solution

User, There are a few things you can do. The best is to install rgdal and change the mapserver format parameter on your call to a geographic format, like GeoTIFF ('Gtiff). The parameter options are here. This will preserve the georeferencing. Once rgdal is installed, you can read the file very easily:

url <- "http://demo.mapserver.org/cgi-bin/wms?version=1.1.1&service=WMS&request=GetMap&VERSION=1.1.1&LAYERS=continents&WIDTH=500&HEIGHT=500&BBOX=0,45,15,60&SRS=EPSG:4326&FORMAT=Gtiff"
download.file(url, destfile="my.tiff")
map <- brick("my.tiff")
map2 <- raster(map, layer=2)

map
class       : RasterBrick 
dimensions  : 500, 500, 250000, 3  (nrow, ncol, ncell, nlayers)
resolution  : 0.03, 0.03  (x, y)
extent      : 0, 15, 45, 60  (xmin, xmax, ymin, ymax)
coord. ref. : +proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0 
data source : ~/my.tiff 
names       : my.1, my.2, my.3 
min values  :    0,    0,    0 
max values  :  255,  255,  255 

map2
class       : RasterLayer 
band        : 2 
dimensions  : 500, 500, 250000  (nrow, ncol, ncell)
resolution  : 0.03, 0.03  (x, y)
extent      : 0, 15, 45, 60  (xmin, xmax, ymin, ymax)
coord. ref. : +proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0 
data source : ~/my.tiff 
names       : my.2 
values      : 0, 255  (min, max)

enter image description here

If you want to use the png format, it will be easier if you use the png package:

library(png)
library(RCurl)
url <-"http://demo.mapserver.org/cgi-bin/wms?version=1.1.1&service=WMS&request=GetMap&VERSION=1.1.1&LAYERS=continents&WIDTH=500&HEIGHT=500&BBOX=0,45,15,60&SRS=EPSG:4326&FORMAT=image/png"
map_png <- getBinaryURL(url)
xx <- readPNG(map_png)
map <- raster(xx[,,2]) # The png is read as an array, and your data is in the second layer.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top