I have a CSV (value,lat,lon) that I am trying to create a raster grid from.
CSV File Sample:
temp,lat,lon
24.1,40.503,-69.248
21.7,38.461,-74.703
19.2,40.694,-72.048
18.5,41.443,-70.187
22.5,40.251,-73.164
14.3,44.287,-67.307
13.9,44.106,-68.109
20,40.369,-73.703
R
Script:
library(raster)
library(rasterVis)
csv_path <- "file.csv"
csv_file <- paste(csv_path, sep="")
temp <- read.csv(csv_file) # takes a while
str(temp)
temp_coords <- cbind(temp$lon, temp$lat)
temp_pts <- SpatialPointsDataFrame(coords=temp_coords,
data=data.frame(temp$temp))
names(temp_pts) <- "temp"
cell_size <- 0.75
lon_min <- -125.0; lon_max <- -65.0; lat_min <- 25.5; lat_max <- 50.5
ncols <- ((lon_max - lon_min)/cell_size)+1; nrows <- ((lat_max - lat_min)/cell_size)+1
gridtemp <- raster(nrows=nrows, ncols=ncols, xmn=lon_min, xmx=lon_max, ymn=lat_min, ymx=lat_max, res=cell_size, crs="+proj=longlat +datum=WGS84")
gridtemp
us_fire <- rasterize(temp_coords, gridtemp, fun=mean)
us_fire
plot(us_fire, col=brewer.pal(9,"PuBuGn"), sub="CSV2RASTER")
Instead of displaying the correct values from the CSV file, it appears that the rasterizing converted the numbers to bogus values:
I need to create a raster with the correct values (column 1) from the CSV.