How to convert point data collected at grid interval to a georeferenced dataset in r?

眉间皱痕 提交于 2019-12-13 17:06:08

问题


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"

回答1:


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')



来源:https://stackoverflow.com/questions/30470895/how-to-convert-point-data-collected-at-grid-interval-to-a-georeferenced-dataset

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!