Beatriz R. Gonzalez Dominguez
2014-Apr-16 06:22 UTC
[R] netCDF to raster and spatial projection
I've recently started using R for spatial data. I'd be really grateful if you could could help me with this. Thanks! Sorry I don't provide a reproducible example. Please ask me if you have any questions about the data. I've extracted data from a multidimensional netCDF file. This file had longitude, latitude and temperature data (for 12 months of a specific year). From this netCDF I've got a data frame for January with these variables: longitude, latitude, temperature. With this data frame I've created a raster. # Packages library("sp") library("raster") library("rgdal") library("ncdf") library("maptools") library("rgeos") library("sm") library("chron") # Dataframe to raster # Create spatial points data frame coordinates(tmp.df01) <- ~ lon + lat # Coerce to SpatialPixelsDataFrame gridded(tmp.df01) <- T # Coerce to raster rasterDF1 <- raster(tmp.df01) > print(tmp.df01) class : SpatialPixelsDataFrame dimensions : 103, 241, 24823, 1 (nrow, ncol, npixels, nlayers) resolution : 0.02083333, 0.02083333 (x, y) extent : 5.739583, 10.76042, 45.73958, 47.88542 (xmin, xmax, ymin, ymax) coord. ref. : NA names : TabsM_1 min values : -18.1389980316162 max values : 2.26920962333679 There is no value for 'coord. ref.' The projection of the original netCDF was WGS84. So I gave this projection to the raster. proj4string(rasterDF1) <- "+proj=longlat +datum=WGS84 +ellps=WGS84 +towgs84=0,0,0" Then, I wanted to reproject my raster to another projection: # Reprojecting into CH1903_LV03 # First, change the coordinate reference system (crs) proj4string(rasterDF1) <- "+init=epsg:21781" # Second, reproject the raster rasterDF1.CH <- spTransform(rasterDF1, crs("+init=epsg:21781")) At this point I get the following error: Error in spTransform(rasterDF1, crs("+init=epsg:21781")) : load package rgdal for spTransform methods But the package rgdal is already uploaded! It must be something wrong in the code!
Hi Beatriz Try to skip this step> # Reprojecting into CH1903_LV03 > # First, change the coordinate reference system (crs) > proj4string(rasterDF1) <- "+init=epsg:21781"And just do this> # Second, reproject the raster > rasterDF1.CH <- spTransform(rasterDF1, crs("+init=epsg:21781")) >Also there is a spTransform both in the rgdal and sp packages. So are they masking each other? rgdal should be before sp in the search() list. I cannot be of more help since you provided no data. Yours sincerely / Med venlig hilsen Frede Aakmann T?gersen Specialist, M.Sc., Ph.D. Plant Performance & Modeling Technology & Service Solutions T +45 9730 5135 M +45 2547 6050 frtog at vestas.com http://www.vestas.com Company reg. name: Vestas Wind Systems A/S This e-mail is subject to our e-mail disclaimer statement. Please refer to www.vestas.com/legal/notice If you have received this e-mail in error please contact the sender.> -----Original Message----- > From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] > On Behalf Of Beatriz R. Gonzalez Dominguez > Sent: 16. april 2014 08:22 > To: r-help at r-project.org > Subject: [R] netCDF to raster and spatial projection > > I've recently started using R for spatial data. I'd be really grateful > if you could could help me with this. Thanks! > > Sorry I don't provide a reproducible example. Please ask me if you have > any questions about the data. > > I've extracted data from a multidimensional netCDF file. This file had > longitude, latitude and temperature data (for 12 months of a specific year). > > From this netCDF I've got a data frame for January with these > variables: longitude, latitude, temperature. > > With this data frame I've created a raster. > > # Packages > library("sp") > library("raster") > library("rgdal") > library("ncdf") > library("maptools") > library("rgeos") > library("sm") > library("chron") > > # Dataframe to raster > # Create spatial points data frame > coordinates(tmp.df01) <- ~ lon + lat > # Coerce to SpatialPixelsDataFrame > gridded(tmp.df01) <- T > # Coerce to raster > rasterDF1 <- raster(tmp.df01) > > print(tmp.df01) > class : SpatialPixelsDataFrame > dimensions : 103, 241, 24823, 1 (nrow, ncol, npixels, nlayers) > resolution : 0.02083333, 0.02083333 (x, y) > extent : 5.739583, 10.76042, 45.73958, 47.88542 (xmin, xmax, > ymin, ymax) > coord. ref. : NA > names : TabsM_1 > min values : -18.1389980316162 > max values : 2.26920962333679 > > There is no value for 'coord. ref.' > > The projection of the original netCDF was WGS84. So I gave this > projection to the raster. > > proj4string(rasterDF1) <- "+proj=longlat +datum=WGS84 +ellps=WGS84 > +towgs84=0,0,0" > > Then, I wanted to reproject my raster to another projection: > > # Reprojecting into CH1903_LV03 > # First, change the coordinate reference system (crs) > proj4string(rasterDF1) <- "+init=epsg:21781" > # Second, reproject the raster > rasterDF1.CH <- spTransform(rasterDF1, crs("+init=epsg:21781")) > > At this point I get the following error: > > Error in spTransform(rasterDF1, crs("+init=epsg:21781")) : > load package rgdal for spTransform methods > > But the package rgdal is already uploaded! It must be something wrong in > the code! > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting- > guide.html > and provide commented, minimal, self-contained, reproducible code.