Problem with coordinates transformation

Hello everyone,

I have the coordinates of a weather station "Garmon" which are in epsg:4326 and I would like to convert them in epsg:31370. I first transformed the coordinates to a spatial object, then set the coordinates of the object to epsg:4326 as it was empty, and then changed the coordinates to epsg:31370.
However, the results I got does not match with reality. What could be wrong?

y = GARMON$LAT[1] #50.87991 #latitude
x = GARMON$LON[1] #4.696799 #longitude
coord <- cbind(x , y)
coord <- as.data.frame(coord) #prerequise for coordinate() function
coordinates(coord) <- ~ x + y #Create a spatial object
crs(coord) <- "+init=epsg:4326" #Set CRS because it was empty
coord <- spTransform(coord,CRS("+init=epsg:31370")) #Change CRS to 31370
coord@coords #x = 173176.6, y=174384.9 :wrong coordinates
#Should be x = 173086.5273 y= 174445.2110 in epsg:31370

I suggest using the {sf} package tools & techniques, rather than the {sp} based ones. SF is the newer of the two packages - and much, much easier to use. Since the {sf} objects are modified data frames you can use all the {dplyr} and what not kind of tools. Much recommended!

library(sf)
library(dplyr)

garmon <- data.frame(lat = 50.87991, # create garmon as a data frame...
                     lon = 4.696799) %>% 
  st_as_sf(coords = c('lon', 'lat'), crs = 4326) # ... and set it as a {sf} object, interpreting values in context of 4326

garmon %>% # take the garmon object...
  st_transform(31370) # ... and transform it to EPSG:31370, printing the output to console

# Simple feature collection with 1 feature and 0 fields
# Geometry type: POINT
# Dimension:     XY
# Bounding box:  xmin: 173086.5 ymin: 174445.1 xmax: 173086.5 ymax: 174445.1
# Projected CRS: BD72 / Belgian Lambert 72
#                    geometry
# 1 POINT (173086.5 174445.1)
1 Like

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.