Skip to content

Problem Setting Projection of Rasters upon Import

14 messages · Hodgess, Erin, Michael Sumner, Roger Bivand +3 more

#
Hi Michael:

Did you try this;

test<-raster("us_ppt_1981_2010.02.asc", crs="+ellps=GRS80")

with the plus sign, please?

That might do it.

Thanks,
Erin
#
Please report on the actual code you ran, you've just sent two
messages, one with

test<-raster("us_ppt_1981_2010.02.asc", crs="+ellps=GRS80")

this correctly cannot set the CRS and so it is NA

and the other with

t<-raster("us_ppt_1981_2010.02.asc", crs="+proj=longlat +ellps=GRS80")
We have no idea what you see in "t" now. In short you have to have a
"+proj", not just a "+ellps" for this incantation. You can check with

library(rgdal)
CRS("+ellps=GRS80")
Error in CRS("+ellps=GRS80") : projection not named
On Fri, Jul 12, 2013 at 11:10 AM, Michael Treglia <mtreglia at gmail.com> wrote:

  
    
#
On Fri, 12 Jul 2013, Michael Treglia wrote:

            
Are your raster package versions different? In current raster, 
R/rasterFromGDAL.R in line 129 sets crs="", and because in line 152 the 
assigned value in:

projection(r) <- attr(gdalinfo, 'projection')

is NA, you get what you see. My guess is that if your crs= argument is 
neither missing nor NA or the empty string, it should poverride 
attr(gdalinfo, 'projection').

Providing the version of your older installation of raster will help 
isolate when and which change has led to this outcome, but you do have a 
workaround. I can't see the offending revision in SCM in R-forge.

Roger

  
    
#
Michael,

I did not try it, but I think the below accomplishes the same. If at
all possible, you should avoid using ascii format files (that leads to
slow processing).

library(raster)
reference <- raster("D:/GIS/California/Hab Suitability
Files/10m_DEM/10m DEMasc/DEM_10m.asc")
projection(reference) <- "+proj=longlat +ellps=GRS80"

setwd("D:/GIS/PRISM/1981-2010/TMin")
s <- stack(list.files(pattern="*.asc", full.names=TRUE))
projection(s) <- "+proj=longlat +ellps=GRS80"
x <- crop(s, reference)
y <- projectRaster(x, crs="+proj=utm +zone=11 +datum=WGS84", res=800,
filename="TMinCrop.tif")

#if you need separate files you could use
writeRaster(y, .... , bylayer=TRUE)


As for setting the crs when creating a Raster object from file with
the raster function. I removed that because it is was some
inconsistent and misinterpreted. I will put it back in, allowing to
set the crs if it is unknown (as with esri ascii files), but not to
change it if the file supplies one.

Robert
On Fri, Jul 12, 2013 at 2:55 PM, Michael Treglia <mtreglia at gmail.com> wrote:
2 days later
#
"Robert J. Hijmans" <r.hijmans at gmail.com> writes:
I would suggest to add a warning when trying to set a crs when it is
already supplied by the file. This would make the behavior clearer.

Rainer
<#secure method=pgpmime mode=sign>