如何使用R显示地图上的经度和纬度线? [英] How to display longitude and latitude lines on a map using R?

查看:399
本文介绍了如何使用R显示地图上的经度和纬度线?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图将这个二进制文件(无符号字符,像素= 720和行= 360)视为一张地图,我尝试了下面给出的一段代码。首先,我下载并保存为 landcover.bin

 连接<  - 文件(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)

我收到了一张看起来很奇怪的地图,然后我换了ncol和nrow作为

  y <-matrix((data = sd),ncol = 720,nrow = 360)
image y)

我得到了一个合理的地图,但倒过来了

我的问题有谁能告诉我如何显示我的文件(它不应该是相同的),以及如何显示此地图上显示的经度和纬度:


使用栅格打包地理数据。
$ b
$ b

你可以直接从一个带有rgdal技巧的二进制文件中读取它,但可以通过一个矩阵来完成它。

 >要求(栅格)
>连接< - 文件(landcover.bin,rb)
> sd< - readBin(conne,integer(),size = 1,n = 360 * 720,signed = F)
> y(-t(矩阵((数据= sd),ncol = 360,nrow = 720))
> r =栅格(y)

现在您有一个栅格对象。但是,如果你将它绘制出来,你会注意到三件事情 - 大量的绿色,规模达到250,轴线从0到1.

地图似乎使用255代表海洋。如果我们重新编码为NA,我们会得到更好的地图:

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

看起来好多了。现在让我们来修复范围:

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

最后,我们应该告诉R这是在lat长的坐标系中 - 大多数可能是epsg:4326:

 >投影(r)= CRS(+ init = epsg:4326)
> plot(r)



请注意,即使您有离散数据(我认为这是一个分类方案)。你可以使用光栅将数字映射为颜色,但这是另一个问题......


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.

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

这篇关于如何使用R显示地图上的经度和纬度线?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆