I am trying to view this binary file(unsigned character, pixel =720 and lines=360) as a map ,I tried the piece of code given bellow. First, I downloaded and saved it as landcover.bin
.
conne <- file("C:\\landcover.bin", "rb")
sd<- readBin(conne, integer(), size=1, n=360*720, signed=F)
y<-matrix((data=sd), ncol=360, nrow=720)
image(y)
I got a map that looks weird, and then I swap ncol and nrow as
y<-matrix((data=sd), ncol=720, nrow=360)
image(y)
I got a reasonable map but upside down
My question can anyone tell me how can I display my file as (which is not supposed to be the same)and how to display the longitudes and latitudes as are shown on this map:
Use the
raster
package for geographically based gridded data.You can probably make it read directly from a binary file with some rgdal trickery, but lets do it via a matrix.
Now you have a raster object. But if you plot it you will notice three things - lots of green, the scale going up to 250, and the axes being from 0 to 1.
The map seems to use 255 for the sea. If we recode that as NA we'll get a better map:
Looks a lot better. Now let's fix the range:
Finally we should tell R that this is in the lat-long coordinate system - most likely epsg:4326:
Note this is still using a continuous colour scheme, even though you have discrete data (which I guess is a classification scheme). You can map numbers to colours with raster, but that's a whole other problem...
You have to mirror the columns on the matrix to get the right result. Use
abline
to draw your lats and longs.But, as @January mentioned, there are many packages that handle maps. You should use one of them.
To get a
ggplot2
based solution, you can usegeom_raster
. See my answer to this earlier question of yours for an example.ggplot2
displays lat lon lines, to tweak this see my answer to this other question of yours, specifically thebreaks
argument forscale_{x,y}_*
.ggplot2
requires a data.frame, not amatrix
orarray
, usemelt
to do this transformation (example here). To get the correct lat lon values, please take care that thearray
has the correctdimnames
, please see?dimnames
for more information.Maybe the maps in the R map package would suit you better?
Result: