2

All my shapefiles, rasters and Spatial objects were projected to UTM NAD83. However, I need to convert them from meters to km for calculation reason. For rasters I can easily convert the "values" by dividing them with 1000. However when I plot them the coordinates were still in meters. It didn't look right if I convert the extent though (by dividing the extent with 1000). (the resolution was 25 meters).

Are methods for converting meters to km different for shapefiles, spatial objects and rasters?

Eventually I would like to create a discrete distance-to-road surface in which the unit and the coordinate system were both in km.

##Read road shp.file
roads<-readOGR(dsn="E:/Doccuments/GIS", layer="Roads_2005_INDOT")

#coerce SpatialLinesDataFrame object to SpatialLines object    
rd.spl<-as(roads, "SpatialLines") 

#create an empty raster with the same extent and grid size=25   
r<-raster(ext=extent(rd.spl), res=25)  
rd.rs<-rasterize(rd.spl, r)          #rasterize SpatialLine
#assign 0 to all roads values 
rd.rs<-reclassify(rd.rs, c(0,108,0)) 
rd.rs

class       : RasterLayer 
dimensions  : 125, 104, 13000  (nrow, ncol, ncell)
resolution  : 25, 25  (x, y)
extent      : 590847.7, 593447.7, 4235915, 4239040  (xmin, xmax, ymin, ymax)
coord. ref. : +proj=utm +zone=16 +datum=NAD83 +units=m +no_defs +ellps=GRS80 +towgs84=0,0,0 
data source : in memory
names       : layer 
values      : 0, 0  (min, max)
plot(rd.rs)

enter image description here

#create discrete "distance-to-road" surface 
d<-distance(rd.rs)/1000   #distance in km    
plot(d)
plot(locations, add=TRUE)
plot(movements, add=TRUE)

enter image description here

1

You can modify the extent and projection directly.

extent(r) <- extent(c(xmin(r), xmax(r), ymin(r), ymax(r))/1000)
projection(r) <- gsub("units=m", "units=km", projection(r))

Obviously that's a bit "fast and loose", but it's going to work - as long as you have "units=m" in there, and it's not just assumed (which it often is).

You won't want to use projectRaster because it will completely rebuild your data with resampling, unnecessarily. Beware of what you do next because plotting and most things in sp/rgdal are not metadata/projection/unit aware, though most of them will tell you there's a problem and/or error so you can't see what's wrong. Plotting will never complain so it's very hard to figure out what is wrong.

Many of the raster functions are metadata-aware, at least to the level where it can spTransform them to the coordinate system in use, though some key ones are not.

  • Thank you so much. I figured it out that day but there were more steps involved. I knew there must be an "easy click". Thanks a gain. – lamushidi Feb 1 '17 at 2:00

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.