Tao Liang
Tao Liang

Reputation: 21

R: Create RasterLayer object from a dataframe with long & lat & altitude values

I have a data.frame list with 3 columns (Col 1: Long, col 2: Lat, col 3: Altitude) and 1095 rows. I want to change this data.frame to the RasterLayer formula. What should I do next? Anything which help will be greatly appreciate!!

It is a grid cell (1 * 1 degree,Long & Lat) of China with elevation values, Thank you for your time and help!

Tao Liang

Upvotes: 2

Views: 623

Answers (1)

Robert Hijmans
Robert Hijmans

Reputation: 47686

You can probably use rasterFromXYZ

Example data

library(raster)
r <- raster(nrow=5, ncol=5, xmn=0, xmx=10, ymn=0, ymx=10, crs="")
set.seed(1)
values(r) <- sample(1:25)
r[r < 15] <- NA
xyz <- rasterToPoints(r)

Solution

rst <- rasterFromXYZ(xyz)

Or with terra

library(terra)
x <- rast(xyz, type="xyz")

Upvotes: 1

Related Questions