How to re-project a map such as "wrld_simpl"? - r

To plot the global map, I simply use:
library(maptools)
data(wrld_simpl)
plot(wrld_simpl)
It seems that the projection of the plotted map is WGS84. How can I change the projection to another projection.
Thanks for any hint

Using spTransform from package rgdal:
library(rgdal)
library(maptools)
data(wrld_simpl)
wrld_transf <- spTransform(wrld_simpl, CRS(" +proj=cea +lon_0=0 +lat_ts=30 +x_0=0 +y_0=0 +a=6371228 +b=6371228 +units=m +no_defs "))
plot(wrld_transf)

Related

How to change the projection of a map (created from ETOPO1 bathymetry data) in R

I have downloaded a TIF file from the ETOPO1 website, which contains bathymetry information. I have plotted the bathymetry map using the following code:
bathy <- raster("ice_data.tif")
plot(bathy)
bathy[bathy>0] <-0
dev.new()
plot(bathy)
intervals<-c(-0.000001, -500, -1000, -5000)
colourscale<-colorRampPalette(c("blue","lightblue1"))
plot(bathy, breaks=intervals, col = colourscale(4))
I am wanting to change the project of the map (to this: +proj=laea +lon_0=-21.09375 +lat_0=-90 +datum=WGS84 +units=m +no_defs). I have tried using the code below (with st_transform() function), but am getting an error message
Error in UseMethod("st_transform") : no applicable method for 'st_transform' applied to an object of class "c('RasterLayer', 'Raster', 'BasicRaster')
new_map = st_transform(bathy, crs = "+proj=laea +lon_0=-21.09375 +lat_0=-90 +datum=WGS84 +units=m +no_defs")
Has anyone got any suggestions? It would be very much appreciated!
Thanks
For rasters you need to use the raster::crs() function to change the crs.
The sf::st_transform() function needs an sf object as input.
Try:
new_crs <- "+proj=laea +lon_0=-21.09375 +lat_0=-90 +datum=WGS84 +units=m +no_defs"
crs(bathy) <- new_crs

How to plot rectangle/bounding box in R?

How do I generate a box rather than the hourglass shape this code is returning? I've tried changing the order of the coordinates but always have that "x" forming rather than a box, sometimes as a sideways hourglass.
library(sp)
mybb <- cbind(x=c(363498.5,480497.5,363498.5, 480497.5,363498.5), y=c(5894630,5894630,5806524,5806524,5894630))
crs <-CRS("+proj=utm +zone=12 +datum=WGS84 +units=m +no_defs +ellps=WGS84 +towgs84=0,0,0")
mybb <- SpatialPolygons(list(Polygons(list(Polygon(mybb)),"1")), proj4string=crs)
plot(mybb)
Returns:
Not sure why rearranging the coordinates hasn't worked so far. This worked for me.
library(rgdal)
library(sp)
mybb <- cbind(x=c(363498.5, 480497.5, 480497.5, 363498.5), y=c(5894630, 5894630, 5806524, 5806524))
crs <-CRS("+proj=utm +zone=12 +datum=WGS84 +units=m +no_defs +ellps=WGS84 +towgs84=0,0,0")
mybb <- SpatialPolygons(list(Polygons(list(Polygon(mybb)),"1")), proj4string=crs)
plot(mybb)

R Levelplot overlaying two SpatialPolygonsDataFrame on a RasterLayer projection

posted also on gis.stackexchange
I'm having some trouble plotting a RasterLayer and a SpatialPolygonsDataFrame in a level plot in R. Something is wrong with the projection, but I don't understand what.
Here you can find my
reproducible example data and code:
library(sp)
library(rgdal)
library(rgeos)
library(raster)
library(rasterVis)
library(maptools)
setwd("C:/...path_to/test2")
data<-read.csv("test.csv", header=TRUE)
#creating the raster from a data.frame and giving projection
raster<-rasterFromXYZ(data, crs="+proj=longlat")
raster_proj<-projectRaster(raster, crs="+proj=longlat +datum=WGS84
+ellps=WGS84 +towgs84=0,0,0,0,0,0,0 +no_defs")
#loading shapes
nuts<-readShapePoly("NUTS_RG_WGS84.shp", proj4string=CRS("+proj=longlat"))
countries<-readShapePoly("countries.shp", proj4string=CRS("+proj=longlat"))
#subsetting the raster
raster_clip<- crop(raster_proj, countries, snap="near")
#plot
p.strip <- list(cex=1.5, lines=1, fontface='bold')
x.scale <- list(cex=1.5)
y.scale <- list(cex=1.5)
label <- list(labels=list(width=1, cex=1.5), height=0.95)
levelplot(raster_clip, par.settings = RdBuTheme, margin=FALSE,
at=seq(min(na.omit(values(raster_clip))), max(na.omit(values(raster_clip))), length.out=15),scales=list(x=x.scale, y=y.scale),
par.strip.text=p.strip, colorkey=label, xlab=list(label="Longitude", cex=2), ylab=list(label="Latitude", cex=2))+
layer(sp.polygons(nuts))+layer(sp.polygons(countries))
The error message is:
Error: Attempted to create layer with no geom.
But the raster and the polygons are in the same projection. I also tried to re-project the polygons using:
nuts <- spTransform(nuts, CRS("+proj=longlat +datum=WGS84 +ellps=WGS84 +towgs84=0,0,0,0,0,0,0 +no_defs"))
countries <- spTransform(countries, CRS("+proj=longlat +datum=WGS84 +ellps=WGS84 +towgs84=0,0,0,0,0,0,0 +no_defs"))
Any Idea?
If you don't want to or can't detach ggplot2 because you need it for something else, you can also call the layer function specifically from the latticeExtra package using:
levelplot(myRaster) +
latticeExtra::layer(sp.polygon(myPolygon))
detach("package:ggplot2", unload=TRUE)
I solved this problem by detaching ggplot2 from my library.

plot LAT/LON coordinates on geotiff in R

I have a sea ice map as a "geotiff". The eventual goal is to extract sea-ice concentrations at specific lat/lon coordinates.
The geotiff can be found at: http://www.iup.uni-bremen.de:8084/amsr2data/asi_daygrid_swath/n6250/2015/jan/asi-AMSR2-n6250-20150101-v5.tif
What i am trying to do is load the geotiff using raster() and then overlay it by my locations and then using the extract() function to acquire values from the raster file at the specific location.
However my lat/lon points accumulate in the centre of the map. Where do I go wrong? Any help or input is greatly appreciated!
library(raster)
library(sp)
r1 = raster("test.tif")
##check plot
plot(r1)
## check projection
projection(r1)
mydf <- structure(list(longitude = rep(22,7), latitude = seq(60,90,5)),.Names = c("longitude","latitude"), class = "data.frame", row.names = c(NA, -7L))
### Get long and lat from data.frame.
xy <- mydf[,c(1,2)]
spdf <- SpatialPointsDataFrame(coords = xy, data = mydf,
proj4string = CRS("+proj=stere +lat_0=90 +lat_ts=70 +lon_0=-45 +k=1 +x_0=0 +y_0=0 +datum=WGS84 +units=m +no_defs +ellps=WGS84 +towgs84=0,0,0"))
points(spdf, col="red", lwd=2)
##extract RGB values as reference for sea-ice concentration
seaice_conc = extract(r1, spdf)
Geo-sp's solution would work, but is sub-optimal (slow and imprecise). You should always (re-)project your vector (points in this case) data and not your raster data. Projecting raster data changes values, while this is not the case with vector data. Projecting raster data is also much more computationally intensive.
Thus, you should do something like this:
library(raster)
r <- raster("asi-AMSR2-n6250-20150101-v5.tif")
crs(r)
# CRS arguments:
# +proj=stere +lat_0=90 +lat_ts=70 +lon_0=-45 +k=1 +x_0=0 +y_0=0 +datum=WGS84 +units=m +no_defs +ellps=WGS84 +towgs84=0,0,0
df <- data.frame(longitude = rep(22,7), latitude = seq(60,90,5), ID=1:7)
spdf <- SpatialPointsDataFrame(coords = df[,1:2], data = df,
proj4string = CRS("+proj=longlat +datum=WGS84"))
library(rgdal)
p <- spTransform(spdf, crs(r))
extract(r, p)
It is important to note that you made a very common mistake:
spdf <- SpatialPointsDataFrame(coords = xy, data = mydf, proj4string =
CRS("+proj=stere +lat_0=90 +lat_ts=70 +lon_0=-45 +k=1 +x_0=0 +y_0=0 +datum=WGS84 +units=m"))
You create a SpatialPointsDataFrame and assign the wrong coordinate reference system (crs). You must assign the crs that actually matches your data, which is "+proj=longlat +datum=WGS84". After that, you can transform the data to the crs that you would like to have (using spTransform).
You can use projectRaster to reproject your raster file. Then you can overlay the points and extract the values.
newproj <- CRS("+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs +towgs84=0,0,0")
p <- projectRaster(r1, newproj)

Gauss-Krüger coordinates to WGS84 in R

I need to convert from Gauss-Krüger coordinates to WGS84 coordinates (system uses by Google). I am using R to do it, but I don´t find a simple example where this problem is solved.
Could you please help me?
My data for testing the conversion:
Address: Hauptstraße 62
70736 Fellbach, Germany
Input(Gauss-Krüger): 3519358.50 5411371.00
Output (WGS84): 48.839580, 9.262591
Thanks!!!
I cannot comment on this so I had to post it as an answer here. But before I get to the solution, should the output coordinates be reversed?
Solution: (adapted from here)
library(rgdal)
# Creating data
GK <- data.frame(cbind("X_GK"=3519358.50,"Y_GK"=5411371.00))
#Spatial Information, Coordinates Transform
coordinates(GK) <- c("X_GK", "Y_GK")
proj4string(GK) <- CRS("+proj=tmerc +lat_0=0 +lon_0=9 +k=1 +x_0=3500000 +y_0=0 +ellps=bessel +datum=potsdam +units=m +no_defs") # Defining Gauss Krüger
GK.WGS84 <- spTransform(GK, CRS("+proj=longlat +datum=WGS84 +ellps=WGS84 +towgs84=0,0,0")) # tranforming to WGS84 longlat
GK.WGS84
Output:
> GK.WGS84
SpatialPoints:
X_GK Y_GK
[1,] 9.262686 48.83949
Coordinate Reference System (CRS) arguments: +proj=longlat +datum=WGS84 +ellps=WGS84 +towgs84=0,0,0

Resources