Error in projecting SpatialPolygonsDataFrame from geographical coordinate to UTM - r

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)

Related

Since update of sp package i get a warning by calling a sp::CRS definition

Since update of sp package i get a warning i did not get before:
1: In showSRID(uprojargs, format = "PROJ", multiline = "NO") :
Discarded ellps unknown in CRS definition: +proj=stere +lat_0=90 +lon_0=10 +k=0.93301270189 +x_0=0 +y_0=0 +R=6370040 +units=km +no_defs
2: In showSRID(uprojargs, format = "PROJ", multiline = "NO") :
Discarded datum unknown in CRS definition
This is a code i use to define CRS
sp::CRS(
"+proj=stere +lat_0=90 +lat_ts=90 +lon_0=10 +k=0.93301270189 +x_0=0 +y_0=0 +a=6370040 +b=6370040 +to_meter=1000 +no_defs"
)
I try to create a projection and it was working before :)
But since update i get this warning.
What am i doing wrong?
Thank you in advance,
Best regards,
Andreas
In your definition, you have parameters a and b --- these refer to the radii of the ellipsoid representation of the earth. As they are the same, you are using a sphere. The new version of the PROJ library require that, if you use the proj4 notation, you must used the WGS84 (or closely related) datum. Otherwise, you need to provide the EPSG code, or WKT notation. So something like this:
p4 <- "+proj=stere +lat_0=90 +lat_ts=90 +lon_0=10 +k=0.93301270189 +units=km +datum=WGS84"
sp::CRS(p4)
#CRS arguments:
# +proj=stere +lat_0=90 +lon_0=10 +k=0.93301270189 +x_0=0 +y_0=0 +units=km +datum=WGS84 +units=m +no_defs
Since there does not seem to be a EPSG code for this CRS, you would need to provide WKT description. I found this one
wkt <- 'PROJCS["unknown",
GEOGCS["unknown",
DATUM["unknown",
SPHEROID["unknown",6370040,0]],
PRIMEM["Greenwich",0,
AUTHORITY["EPSG","8901"]],
UNIT["degree",0.0174532925199433,
AUTHORITY["EPSG","9122"]]],
PROJECTION["Polar_Stereographic"],
PARAMETER["latitude_of_origin",90],
PARAMETER["central_meridian",10],
PARAMETER["scale_factor",0.93301270189],
PARAMETER["false_easting",0],
PARAMETER["false_northing",0],
UNIT["kilometre",1000,
AUTHORITY["EPSG","9036"]],
AXIS["Easting",SOUTH],
AXIS["Northing",SOUTH]]'
But that still does not work because of the datum being unknown
CRS(SRS_string=wkt)
#CRS arguments:
# +proj=stere +lat_0=90 +lon_0=10 +k=0.93301270189 +x_0=0 +y_0=0 +R=6370040 +units=km +no_defs
#Warning messages:
#1: In showSRID(SRS_string, format = "PROJ", multiline = "NO") :
# Discarded ellps unknown in CRS definition: +proj=stere +lat_0=90 +lon_0=10 +k=0.93301270189 +x_0=0 +y_0=0 +R=6370040 +units=km +no_defs
#2: In showSRID(SRS_string, format = "PROJ", multiline = "NO") :
# Discarded datum unknown in CRS definition
There were some big changes in sp and other spatial packages in the recent releases. See, for example:
https://www.r-spatial.org/r/2020/03/17/wkt.html
and
https://www.r-spatial.org/r/2020/06/17/s2.html
Relevant to your issues is there is a difference between crs and CRS. Perhaps things are getting stuck there.

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)

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.

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