Conversion of Coordinate reference system in Raster objects in R - r

I am working on NDVI monitoring using Sentinel 2 imagery data.
I have village level boundaries for a region imported as SpatialPolygonsDataFrame in R. After getting to an NDVI rasterlayer when I try to crop out the area of interest, it says extents do not overlap.
I have found the CRS of my region variable to be different(longlat) from my NDVI layer(utm).
> crs(ndvi)
CRS arguments:
+proj=utm +zone=42 +datum=WGS84 +units=m +no_defs +ellps=WGS84 +towgs84=0,0,0
> crs(region)
CRS arguments:
+proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0
> r <- crop(ndvi,extent(region))
Error in .local(x, y, ...) : extents do not overlap`
Any help would be appreciated.

You can use rgdal::spTranform on "region" to create an object that has the same crs as "ndvi"
library(rgdal)
reg <- spTransform(region, crs(ndvi))
and now
r <- crop(ndvi, reg)
Perhaps followed by
m <- mask(r, reg)

Related

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)

How to change coordinate reference system (CRS) for a .shp file in R?

I have a .shp file and I want to change its crs, I have tried to use spTransform but it does not work in my case. The .shp file can be found at https://www.dropbox.com/s/8wfgf8207dbh79r/gpr_000b11a_e.zip?dl=0.
library(rgdal)
shpfile <- readOGR(dsn="D:/Map",layer = "gpr_000b11a_e")
crs(shpfile)
CRS arguments:
+proj=longlat +datum=NAD83 +no_defs +ellps=GRS80 +towgs84=0,0,0
spTransform(shpfile, CRS("+proj=longlat +datum=WGS84 +ellps=WGS84 +towgs84=0,0,0"))
crs(shpfile)
CRS arguments:
+proj=longlat +datum=NAD83 +no_defs +ellps=GRS80 +towgs84=0,0,0
The problem is: after spTransform, the crs for the shapefile does not change. Thanks for any help.
The problem is that you didn't attribute the transformed shape to an object.
Try this:
shpfile <- spTransform(shpfile,
CRS("+proj=longlat +datum=WGS84 +ellps=WGS84 +towgs84=0,0,0"))
I recommend you the package sf, for reading and handling .shp files, its easy to use and efficient.
Hope it helps.

Error in projecting SpatialPolygonsDataFrame from geographical coordinate to UTM

I would like to project the coordinates of the 2012 Peruvian census to work with another raster layer that has the following coordinates reference system :
"+proj=utm +zone=18 +south +ellps=WGS84 +towgs84=0,0,0,0,0,0,0 +units=m +no_defs ".
The answer of the post https://gis.stackexchange.com/questions/31743/projecting-sp-objects-in-r did not work in my case.
crs(census)
CRS arguments:
+proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0
newCRScensus=CRS("+proj=utm +zone=18 +south +ellps=WGS84 +towgs84=0,0,0,0,0,0,0 +units=m +no_defs ")
projcensus=spTransform(census,crs=newCRScensus)
Error in spTransform(census, crs = CRS("+proj=utm +zone=18 +south +ellps=WGS84 +towgs84=0,0,0,0,0,0,0 +units=m +no_defs ")) :
second argument needs to be of class CRS
I don't understand the error message since the second argument is indeed of class CRS.
Any suggestions for another way of projecting the geographical coordinates is welcomed.
The second argument of spTransform is called CRSobj, not crs, so the right call should be either
projcensus=spTransform(census, newCRScensus)
or
projcensus=spTransform(census, CRSobj = newCRScensus)

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

How to calculate polygon means and map them?

I have the dataset (pts) like this:
x <- seq(-124.25,length=115,by=0.5)
y <- seq(26.25,length=46,by=0.5)
z = 1:5290
longlat <- expand.grid(x = x, y = y) # Create an X,Y grid
pts=data.frame(longlat,z)
names(pts) <- c( "x","y","data")
I knew that I can map the dataframe (pts) into a map by doing:
library(sp)
library(rgdal)
library(raster)
library(maps)
coordinates(pts)=~x+y
proj4string(pts)=CRS("+init=epsg:4326") # set it to long, lat
pts = spTransform(pts,CRS(" +init=epsg:4326 +proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs +towgs84=0,0,0"))
pts <- as(pts, "SpatialPixelsDataFrame")
r = raster(pts)
projection(r) = CRS(" +init=epsg:4326 +proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs +towgs84=0,0,0")
plot(r)
map("usa",add=T)
my question is that how I can calculate the means of the 10 EPA Regions and map the means?
the EPA regions can be found at the bottom of the webpage at
http://www.epa.gov/wed/pages/ecoregions/level_iii_iv.htm
Thanks
First read the shape file
er <- readOGR("Eco_Level_III_US.shp", "Eco_Level_III_US")
Then make sure ther raster r and the ecoregions er have the same projection
er.4326 <- spTransform(er, CRS("+init=epsg:4326 +proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs +towgs84=0,0,0"))
Extract the r raster data from the shapefile (that may take a few minutes), calculate the mean and add it to your polyongs.
er.v <- extract(r, er.4326)
means <- sapply(er.v, mean)
er.4326$means <- means
And finally plot it
spplot(er.4326, "means")

Resources