19

i have to turn it into a UTM in order to make the buffer functional.

wets<-readOGR(dsn=".",layer="shapefile")
r.raster <- raster()
extent(r.raster) <- extent(wets)
res(r.raster) <- 100 

wets.r <- rasterize(wet,r.raster)
plot(wets.r)
wetsbuf<-buffer(wets.r,width=500)

During the buffer creation which is the last line of code, it gives this warning:

Warning message:  
In couldBeLonLat(x) :
  raster has a longitude/latitude CRS, but coordinates do not match that

here's the info

  summary(wets.r)
          layer
 Min.        1
 1st Qu.     1
 Median      2
 3rd Qu.     9
 Max.       11
 NA's    52629

summary(wets)

  Object of class SpatialPolygonsDataFrame
Coordinates:
      min       max
 x  683705  714088.8
 y 4326266 4343768.0
 Is projected: TRUE 
 proj4string :
 [+proj=tmerc +lat_0=0 +lon_0=24 +k=0.9996 +x_0=500000 +y_0=0 +datum=GGRS87
 +units=m +no_defs +ellps=GRS80 +towgs84=-199.87,74.79,246.62]
 Data attributes:
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
    0.0     2.5     5.0     5.0     7.5    10.0 






 wets.r

class       : RasterLayer 
dimensions  : 175, 304, 53200  (nrow, ncol, ncell)
resolution  : 100, 100  (x, y)
extent      : 683705, 714105, 4326268, 4343768  (xmin, xmax, ymin, ymax)
coord. ref. : +proj=longlat +datum=WGS84 +ellps=WGS84 +towgs84=0,0,0 
data source : in memory
names       : layer 
values      : 1, 11  (min, max)
attributes  :
   ID FID
 from:  1   0
 to  : 11  10

I have to change the prjection in order to be possible to do the buffer.

PolyGeo
  • 65,136
  • 29
  • 109
  • 338
gsa
  • 1,246
  • 6
  • 19
  • 29

1 Answers1

26

This is how you can reproject a raster in R using the raster package. In this example, the input geotiff was in a NAD83 geographic coordinate system and I reproject to a NAD 83 UTM 15 projected coordinate system. A good reference for Proj4 format projections, which are used by RGDAL, can be found at spatialreference.org.

library(raster)

# Create RasterLayer object
r <- raster('C:/temp/binary_nad83.tif')

# Define the Proj.4 spatial reference 
# http://spatialreference.org/ref/epsg/26915/proj4/
sr <- "+proj=utm +zone=15 +ellps=GRS80 +datum=NAD83 +units=m +no_defs" 

# Project Raster
projected_raster <- projectRaster(r, crs = sr)

# Write the RasterLayer to disk (See datatype documentation for other formats)
writeRaster(projected_raster, filename="C:/temp/binary_utm15.tif", datatype='INT1U', overwrite=TRUE)
Aaron
  • 51,658
  • 28
  • 154
  • 317
  • Thanks for the answer, it returns this though : Error in projectExtent(from, projto) : cannot do this transformation In addition: Warning message: In rgdal::rawTransform(projfrom, projto, nrow(xy), xy[, 1], xy[, : 218 projected point(s) not finite – gsa Jul 14 '15 at 14:12
  • Are you using a single band raster or a multiband? This example is is for a single band raster. – Aaron Jul 14 '15 at 14:19
  • It comes from a rasterize process of a vector (wetlands) not a satellite image so i will go with the single band. – gsa Jul 14 '15 at 14:22
  • Try updating your raster/rgal packages: http://r-sig-geo.2731867.n2.nabble.com/Error-with-projectRaster-td7197193.html – Aaron Jul 14 '15 at 15:29
  • @gsa, if this works it's a good idea to upvote and accept the answer! Otherwise, edit and clarify your original question. – Simbamangu Oct 20 '15 at 04:41