1

I would like to compare vector vs raster data. So i try to fix a problem using both ways.

I made it in vector first :

coastline<-readShapeSpatial("shp")
wets<-readShapeSpatial("shp")
wetb<-gBuffer(wets,width=500,byid=T) #doing a buffer of 500m of the wetlands
wetb2<-gBuffer(wets,width=1000,byid=T) #doing abuffer of 1000
wetb1$vat=0 # assigning a new column of 0 in the buffer i created
wetb2$vat=5 # assigning a new column of 5 in the second buffer
coastline$vat=10 #assigning a new column of 10 in the rest of the land

Now how would i write the same thing in raster?

My thoughts are that i should turn the coastline and the wetlands into raster first off.How would i do that and what after that?

  • It's unclear what you're trying to do here - what do you mean by 'test vector vs raster'? Your code is uncommented and thus it's impossible to figure out why you're assigning various vectors ... if you want to do these steps with rasters, are you looking to do buffer operations? – Simbamangu Jul 12 '15 at 8:33
  • I want to compare the two types (vector vs raster) by doing the same procedure, once with vector and once with raster.i will edit my main question and add comments. – gsa Jul 12 '15 at 9:18
9

Here's a suggestion:

# Use rgdal, better library
library(rgdal)
coastline <- readOGR(dsn=".", layer="coastline")
wets <- readOGR(dsn=".", layer="wets")

Rasterize 'em:

require(raster)
# Create a generic raster, set the extent to the same as wetlands
r.raster <- raster()  

Use extent (from raster package) to read bounds of vector and assign to the raster:

extent(r.raster) <- extent(wets)
res(r.raster) <- 2500 # set cell size to 2500 metres

# Make a raster of the wetlands:
coastline.r <- rasterize(wets, r.raster)

# Do the same buffering operation but in raster:
wetb <- buffer(wets, width=500)

If you want to do it by individual features, you could loop through them and create a raster stack layer for each feature.

|improve this answer|||||
  • Can you also provide an example of how to perform the buffer part and and assign procedure like the question.Because documentation of the package isn;t that easy.Also add comments on how you used the extents previously. – gsa Jul 13 '15 at 19:06
  • Updated code with comments on extent() and corrected missing buffer() command. – Simbamangu Jul 13 '15 at 19:15
  • Nice.why the raster()? what the empty parenthesis supposed to mean? and also i did the buffer part like that and it returned this warning : Warning message: In couldBeLonLat(x) : raster has a longitude/latitude CRS, but coordinates do not match that – gsa Jul 13 '15 at 19:21
  • raster() creates an empty raster object, which can then have arbitrary resolution (res()) and bounds (extent()) assigned. The warning you see lets you know that your data might be in lat/lon - i.e. degrees - instead of a projected system where buffer width actually means something. – Simbamangu Jul 14 '15 at 9:49
  • do u know how to turn it into a projected system in R so it can recieve the buffer normally. Thnx for the help – gsa Jul 14 '15 at 9:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.