How to display longitude and latitude lines on a m

2019-08-08 15:18发布

问题:

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:

回答1:

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.

> require(raster)
> conne <- file("landcover.bin","rb")
> sd<- readBin(conne, integer(), size=1,  n=360*720, signed=F)
> y<-t(matrix((data=sd), ncol=360, nrow=720))
> r = raster(y)

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:

> r[r==255]=NA
> plot(r)

Looks a lot better. Now let's fix the range:

> extent(r) = extent(c(xmn=-180,xmx=180,ymn=-90,ymx=90))
> plot(r)

Finally we should tell R that this is in the lat-long coordinate system - most likely epsg:4326:

> projection(r)=CRS("+init=epsg:4326")
> plot(r)

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...



回答2:

You have to mirror the columns on the matrix to get the right result. Use abline to draw your lats and longs.

conne <- file("Landcover.bin", "rb")
sd<- readBin(conne, integer(), size=1,  n=360*720, signed=F)
y<-matrix(sd,ncol=360,nrow=720)
image(y[,360:1])

lats=seq(-90,90,by=30)
longs=seq(-180,180,by=30) 

trans.lats=(lats+90) / 180
trans.longs=(longs+180) / 360

abline(h=trans.lats,v=trans.longs)

But, as @January mentioned, there are many packages that handle maps. You should use one of them.



回答3:

To get a ggplot2 based solution, you can use geom_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 the breaks argument for scale_{x,y}_*.

ggplot2 requires a data.frame, not a matrix or array, use melt to do this transformation (example here). To get the correct lat lon values, please take care that the array has the correct dimnames, please see ?dimnames for more information.



回答4:

Maybe the maps in the R map package would suit you better?

library( maps )
map( "world" )
points(  -0.11832, 51.50939, pch= 19, col= "red" )
text(  -0.11832, 51.50939, "London", pos=3, col= "red" )
abline( h= 0 )
text( -150, 0, "Equator", pos= 3 )
abline( v= 0 )

Result:



标签: r plot ggplot2