TransWikia.com

CSV to raster grid conversion problem

Geographic Information Systems Asked on February 16, 2021

I have a sample .csv file (sample download) that I am trying to convert to a gridded raster. So far, I have used two methods, one of the methods is explained in the answer here. But, I am running into problems, how can I fix this problem in R? The code for the first method is as follows:

# CSV to raster
library(raster)
# sample data
sample <- read.csv("Sample_Data.csv", header=TRUE)
x <- raster(xmn=-110.906, xmx=-110.031, ymn=43.03938, ymx=44.3438, res=0.0625, crs="+proj=longlat +datum=WGS84")
rstr <- rasterize(sample[, c('X', 'Y')], x, sample[, 'VIC_SC_04'])
plot(rstr)

This code results the plot nelow and as you can see it’s not gridded.
enter image description here

So I tried another method, and the code for this method is:

# CSV to raster
library(raster)
library(sp)
library(rgdal)
library(rgeos)

# sample data
sample <- read.csv("Sample_Data.csv", header=TRUE)
data<-data.frame(sample$X,sample$Y,sample$VIC_SC_04)

# points from scratch 
coords = cbind(sample$X,sample$Y)
sp = SpatialPoints(coords)

# make spatial data frame
spdf = SpatialPointsDataFrame(sp, data)
spdf_pi = SpatialPixelsDataFrame(sp,tolerance = 0.016129, data) # Storing incorrect values in Z basically storing X coordinates in z as well**

    raster = raster(spdf_pi[data$VIC_SC_04]) # gives error Error in which(sapply(from@data, is.factor)) : 
  argument to 'which' is not logical

# back to data
as.data.frame(data)
plot(data)

# Another try
dfr <- rasterFromXYZ(data)  #Convert first two columns as lon-lat and third as value (throws an error)              
plot(dfr)
   
# coerce to SpatialPixelsDataFrame
gridded(spdf_pi) <- TRUE

# coerce to raster
rasterDF <- raster(spdf_pi)
plot(rasterDF)

When I plot ‘data’, I get:

plot(data)

enter image description here
But in the following line, I get the following respective error:

dfr <- rasterFromXYZ(data)
Error in rasterFromXYZ(data) : x cell sizes are not regular

The resultant raster should look like this:
enter image description here

2 Answers

From your code, check number of rows and columns:

library(raster)
sample <- read.csv("Sample_Data.csv", header=TRUE)
x <- raster(xmn=-110.906, xmx=-110.031, ymn=43.03938, ymx=44.3438, res=0.0625, crs="+proj=longlat +datum=WGS84")
x@ncols
# 14
x@nrows
# 21

So, the issue is the number of columns, since you have 15 points horizontally. A solution is:

x <- raster(xmn=-110.906, xmx=-110.031, ymn=43.03938, ymx=44.3438, res=0.0625,ncols=15,nrows=21, crs="+proj=longlat +datum=WGS84")
rstr <- rasterize(points,x,points$VIC_SC_04)
plot(rstr)

enter image description here

This resolves the issue, but keep a weird resolution (0.05833333, 0.06211524 (x, y))

Correct answer by aldo_tapia on February 16, 2021

You were sort of on the right track but, were just overcomplicating your code. I should note that, with your example data, there is no variation in the VIC_SC_04 column.

library(sp)
library(raster)

d <- read.csv("sample_data.csv")
  coordinates(d) <- ~X+Y
  
spdf <- SpatialPixelsDataFrame(d, tolerance = 0.016129, d@data)
 r <- raster(spdf, layer=3) 

Answered by Jeffrey Evans on February 16, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP