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
Related
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)
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)
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.
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)
G'day,
I have a large number of lon/lat coordinates that are in the CRS Australian Geodetic Datum 66/84 (AGD66 for brevity). I want to change these coordinates from AGD66 to WGS84 because there is about a 200m difference between them and I have other coordinates and layers in WGS84. I have tried to do this by:
lon lat
147.1428 -43.49083
library(rgdal)
pts<-read.table(file.choose(),header=TRUE,sep=',')
# I first project the pts in their original CRS
pts66<-project(cbind(pts$lon,pts$lat), "+init=epsg:4202")
# Then to transform it to WGS84
pts84 = spTransform(pts66,CRS("+init=epsg:3033"))
Error in function (classes, fdef, mtable) :
unable to find an inherited method for function "spTransform", for signature "matrix", "CRS"
Does anyone know why I get this error or have any suggestions for how I can change these coordinates from AGD66 to WGS84? Thanks for your help in advance.
Cheers,
Adam
I have removed part of an incorrect answer.
The function project() cannot do datum conversions so you may have a problem there and I think what you have is wrong.
The problem is that you can only project() from/to longlat on WGS84, so your first use of project is incorrect. If I guess this right, you have coordinates that are in AGD66 so you must first assign that projection and then you can transform. You cannot do datum transformations with project(), but spTransform() can.
I think you need this:
pts = read.table(text = "lon lat
147.1428 -43.49083", header = TRUE)
## assign original coordinate system
pts66 = SpatialPoints(cbind(pts$lon,pts$lat), CRS("+init=epsg:4202"))
## Then to transform it to WGS84
pts84 = spTransform(pts66, CRS("+init=epsg:3033"))
pts66
SpatialPoints:
coords.x1 coords.x2
[1,] 147.1428 -43.49083
Coordinate Reference System (CRS) arguments: +init=epsg:4202 +proj=longlat +ellps=aust_SA
+no_defs
pts84
SpatialPoints:
coords.x1 coords.x2
[1,] 11126605 2971806
Coordinate Reference System (CRS) arguments: +init=epsg:3033 +proj=lcc +lat_1=-68.5 +lat_2=-74.5
+lat_0=-50 +lon_0=70 +x_0=6000000 +y_0=6000000 +ellps=WGS84 +datum=WGS84 +units=m +no_defs
+towgs84=0,0,0
This means that pts66 are not changed from their original values, but they have the metadata correct for the next step which transforms them to your target (which is Lambert Conformal Conic btw). You might need a bit more investigation to figure out what's required.
CRS("+init=epsg:4202")
CRS arguments:
+init=epsg:4202 +proj=longlat +ellps=aust_SA +no_defs
CRS("+init=epsg:3033")
CRS arguments:
+init=epsg:3033 +proj=lcc +lat_1=-68.5 +lat_2=-74.5 +lat_0=-50
+lon_0=70 +x_0=6000000 +y_0=6000000 +ellps=WGS84 +datum=WGS84 +units=m
+no_defs +towgs84=0,0,0
Your original project() was incorrectly attempting to transform from longlat on WGS84 to longlat on AGD66, but that function cannot do that so it's just added confusion in the mix. A datum is not a projection, it is a critical part of a projection's definition and in this sense "longlat on AGD66" is a projection just as "Lambert Conformal Conic on WGS84" is.