简体   繁体   中英

Converting table with coordinates raster plot

I'm a total beginner and have what I believe is a rather simple problem, but I am kind of stuck...

I have a data table with three columns: x , y and height values. I've imported the csv file and extracted the values:

data <- read.csv("C:/Data/heights.csv")
mydata <- data[, c("x", "y", "height")]

I then need to put the height values into a grid with these coordinates, which I created as a raster:

grid <- raster(ncol=2001, nrow=2001, xmn=479975, xmx=500025, ymn=119975, ymx=140025)

I think you are very close. I'd suggest you reading the raster package material, the vignette and description .

You'll have to pay attention to your data and how it is organized. By row? Column? raster cells values will be populated by row.

Using something simmilar to the example provided in the help ?raster

library(raster)
vals <- 1:100
r1 <- raster(nrows=10, ncols=10, xmn=479975, xmx=500025, ymn=119975, ymx=140025)
r1[] <- vals
plot(r1)

在此处输入图片说明

But the procedure may be different approaching with a spatial object

library(sp)
x <- 1:10
y <- 1:10
df <- data.frame(expand.grid(x=x,y=y), v=1:100)
coordinates(df)=~x+y
gridded(df) = TRUE
r2 <- raster(df)
plot(r2)

来自空间物体的栅格

I think this may be the best approach but it would be better to have an idea about your data and how it is organized.

You could try this:

library(raster)
x <- rasterFromXYZ(mydata)
extent(x) <- c(479975, 500025, 119975, 140025)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM