How to plot rectangle/bounding box in R? - 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)

Related

Conversion of Coordinate reference system in Raster objects in 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)

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 re-project a map such as "wrld_simpl"?

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)

Resources