如何将网格间隔采集的点数据转换为r中的地理参考数据集? [英] How to convert point data collected at grid interval to a georeferenced dataset in r?

查看:209
本文介绍了如何将网格间隔采集的点数据转换为r中的地理参考数据集?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个数据集: https://www.dropbox。

(样本)

,row,col,flagrv
1,2361,530,2
2,2378,531,2
3,2360,531,2
4,2355,531,2
5,2363,532,2
6,2359,532, 2
7,2368,533,2
8,2367,533,2
10,2359,533,2



 gs.pal < -  colorRampPalette(c(blue,green,yellow,orange,red),bias = 1,space =rgb)
ggplot(data = ndata,aes(x = col,y = row,color = flagrv))+
geom_point(size = 0.01)+
scale_colour_gradientn(name =Scale,colors = gs.pal(5)) +
xlab('Longitude')+
ylab('Latitude')+
theme_bw()+
theme(line = element_blank())+
theme legend.position = c(.93,.20),panel.grid.major = element_line(color =#854440))+
ggsave(test.png,width = 10,height = 8,dpi = 300)

我们得到这个数字:



现在,问题是我没有Lat-Long值。我想重叠状态边界,但不能使用地图包。有人建议我使用gdal,但我不知道如何。请告诉我如何将其映射到Lat-Long域,以便我可以轻松操纵它。



编辑:



我从其他人处了解到,我可以使用它:

  gdal_translate -a_srs EPSG:4269 FILE.asc FILE.tif 





答案错误1

 错误:在spdf = SpatialPointsDataFrame(coords,all_data [,c(flagrv]

然后我将代码更改为:

  spdf = SpatialPointsDataFrame(coords,all_data [,c(flagrv)])

但是现在我有这个错误:

  validObject(.Object)中的错误:无效的类SpatialPointsDataFrame对象:数据在类SpatialPointsDataFrame中:获得类整数,应该是或扩展类data.frame


解决方案

不知道l向东是数据集的投影和数据(但希望有更多的信息,如分辨率和范围),但没有简单的方法来做到这一点。如果这是派生地图,请尝试查找用于生成它的内容。
使用这些信息,您可以使用光栅包中的投影函数来定义数据集的投影。



编辑(根据提供的其他信息,是一个工作解决方案):
这是一个工作解决方案,假设数据集的左下角具有24.55,-130坐标,行间距为0.01度,投影为nad83。请注意,提供的元数据信息是错误的,因为最低纬度值不是20度,而是可以从最南端(关键西部)估计为24.55。

  #load dataset 
all_data =(read.csv('new_data.csv',header = T,stringsAsFactors = F))
res = 0.01#行间距和坐标系间隔预先指定
#origin_col_row = c(0,0)
origin_lat_lon = c(24.55,-130)
all_data $ row =(all_data $ row)* res + origin_lat_lon [1]
all_data $ col =(all_data $ col)* res + origin_lat_lon [2]

#现在我们有了真正的经纬度,我们可以创建一个空间数据框
library rgdal)
库(sp)
coords = cbind(all_data $ col,all_data $ row)
spdf = SpatialPointsDataFrame(coords,data = all_data)#sp = SpatialPoints(coords)
proj4string(spdf)< - CRS(+ init = epsg:4269)

r似乎扼杀试图绘制许多点,所以要检查答案是否有意义,我将数据集保存为shapefile并将其绘制在arcgis上:

  writeOGR(spdf,D:/tmp_shapefile4.shp,flagrv,driver =ESRI Shapefile)


我用下面的代码设法使用ggplot2来绘制它,只需要耐心,因为它需要一段时间才能绘制出来:

  df = as.data.frame(spdf)
library(ggplot2)
ggplot(data = df,aes( ('经度')+
ylab('纬度')
(大小= 0.01)+


I have this dataset: https://www.dropbox.com/s/k06n9l05t25r6x2/newdata.csv?dl=0

(Sample)

"","row","col","flagrv"
"1",2361,530,2
"2",2378,531,2
"3",2360,531,2
"4",2355,531,2
"5",2363,532,2
"6",2359,532,2
"7",2368,533,2
"8",2367,533,2
"10",2359,533,2

And if I plot using this code:

gs.pal <- colorRampPalette(c("blue", "green","yellow","orange","red"),bias=1,space="rgb")
ggplot(data=ndata,aes(x=col,y=row,color=flagrv)) + 
  geom_point(size = 0.01)+
  scale_colour_gradientn(name = "Scale",colours = gs.pal(5))+
  xlab('Longitude')+
  ylab('Latitude')+
  theme_bw()+
  theme(line = element_blank())+
  theme(legend.position = c(.93,.20),panel.grid.major = element_line(colour = "#854440"))+
  ggsave("test.png",width=10, height=8,dpi=300)

We get this figure:

Now, the problem is I don't have Lat-Long values. I want to overlay the state boundaries but can't use the Maps package. Someone suggested I used gdal but I don't know how. Could you please tell me how I can map this into the Lat-Long domain so that I can easily manipulate it.

Edit:

I learnt from someone else that I can use this:

gdal_translate -a_srs EPSG:4269 FILE.asc FILE.tif

#

Errors for answers 1

Error: unexpected ']' in "spdf = SpatialPointsDataFrame(coords, all_data[, c("flagrv"]"

Then I changed the code to:

spdf = SpatialPointsDataFrame(coords, all_data[, c("flagrv")]) 

But now I have this error:

Error in validObject(.Object) : invalid class "SpatialPointsDataFrame" object: invalid object for slot "data" in class "SpatialPointsDataFrame": got class "integer", should be or extend class "data.frame"

解决方案

Without knowing at least the projection and datum of the dataset (but hopefully more info such as resolution and extent), there is no easy way to do this. If this is a derived map, try to find what was used to generate it. With this information you can then use the projection function in the raster package to define the projection of the dataset.

EDIT (based on additional info provided, there is a working solution): Here is a working solution given that the lower left corner of the dataset has a 24.55, -130 coordinate, spacing among row/col is 0.01 degrees and projection is nad83. Note that the metadata info provided was wrong, as the min lat value was not 20 degrees but could be estimated from the southernmost point (key west) as 24.55.

#load dataset 
all_data=(read.csv('new_data.csv',header=T, stringsAsFactors=F))
res=0.01 #spacing of row and col coords pre-specified
#origin_col_row=c(0, 0) 
origin_lat_lon=c(24.55, -130) 
all_data$row=(all_data$row)*res+origin_lat_lon[1] 
all_data$col=(all_data$col)*res+origin_lat_lon[2]

#now that we have real lat/lon, we can just create a spatial dataframe
library(rgdal)
library(sp)
coords = cbind(all_data$col, all_data$row)
spdf = SpatialPointsDataFrame(coords, data=all_data) #sp = SpatialPoints(coords)
proj4string(spdf) <- CRS("+init=epsg:4269") 

r seems to choke trying to plot that many points, so to check if the answer made sense, I saved the dataset as a shapefile and plotted it on arcgis:

writeOGR(spdf,"D:/tmp_shapefile4.shp", "flagrv", driver="ESRI Shapefile")

I managed to plot it using ggplot2 with the code below, just be patient as it takes a while to plot it:

df=as.data.frame(spdf)
library(ggplot2)
ggplot(data=df,aes(x=col,y=row,color=flagrv))+ 
   geom_point(size = 0.01)+
  xlab('Longitude')+
  ylab('Latitude')

这篇关于如何将网格间隔采集的点数据转换为r中的地理参考数据集?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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