ail
ail

Reputation: 149

Changing WGS84 to EPSG:5330 in R

I would like to change my coordinate form WGS84 to EPSG:5330. Hope, anyone can help me thanks

ID,X,Y   
1,106.6874498,-6.2107887   
2,106.6883199,-6.2069667

Upvotes: 2

Views: 199

Answers (2)

Anthony Basooma
Anthony Basooma

Reputation: 21

Another option is to use sf package, which is a highly versatile package that provides a dataframe as an output. Therefore, other data processing steps are easy to implement, such as filtering, merging, and subsetting.

#Load libraries

> library(sf)
> library(dplyr) #optional

Original data

> pts <-  data.frame(id = c(1, 2),
+                    x = c(106.6874498, 106.6883199), 
+                    y = c(-6.2107887, -6.2069667)) 

1.Tranformed to simple features

> pts_sf <- st_as_sf(pts, coords = c("x",'y'), crs = 4326) 
> print(pts_sf)
Simple feature collection with 2 features and 1 field
Geometry type: POINT
Dimension:     XY
Bounding box:  xmin: 106.6874 ymin: -6.210789 xmax: 106.6883 ymax: -6.206967
Geodetic CRS:  WGS 84
  id                   geometry
1  1 POINT (106.6874 -6.210789)
2  2 POINT (106.6883 -6.206967)

2. Tranformed into Batavia (Jakarta) / NEIEZ cordinate reference system

> ptstranfoemed <-  st_transform(pts_sf, crs = 5330)
> print(ptstranfoemed)
Simple feature collection with 2 features and 1 field
Geometry type: POINT
Dimension:     XY
Bounding box:  xmin: 3532231 ymin: 213991.4 xmax: 3532328 ymax: 214415.3
Projected CRS: Batavia (Jakarta) / NEIEZ
  id                 geometry
1  1 POINT (3532231 213991.4)
2  2 POINT (3532328 214415.3)

More information on cordinate reference system can be obtained at https://epsg.io/5330

3. Using piping to get the transformed out at once

> pts_transf_piped <-  data.frame(id = c(1, 2),
+                                 x = c(106.6874498, 106.6883199), 
+                                 y = c(-6.2107887, -6.2069667)) %>% 
+   st_as_sf(coords = c("x",'y'), crs = 4326) %>% 
+   st_transform(pts_sf, crs = 5330)
 
> print(pts_transf_piped)
Simple feature collection with 2 features and 1 field
Geometry type: POINT
Dimension:     XY
Bounding box:  xmin: 3532231 ymin: 213991.4 xmax: 3532328 ymax: 214415.3
Projected CRS: Batavia (Jakarta) / NEIEZ
  id                 geometry
1  1 POINT (3532231 213991.4)
2  2 POINT (3532328 214415.3)

4. Additonal to get x and y in a dataframe

> ptsextracted <-  pts_transf_piped %>% 
+   mutate(X = sf::st_coordinates(.)[,1],
+          Y = sf::st_coordinates(.)[,2]) %>% 
+   st_drop_geometry()
> print(ptsextracted)
  id       X        Y
1  1 3532231 213991.4
2  2 3532328 214415.3

Upvotes: 1

Roman
Roman

Reputation: 4989

Easy-cheesy with the sp library.

library(sp)
# Create SpatialPoints out of coordinates. 
# Assign WGS84 (EPSG 4326) coordinate reference system.
pts <- SpatialPoints(coords = data.frame(x = c(106.6874498, 106.6883199), 
                                         y = c(-6.2107887, -6.2069667)),
                     proj4string = CRS("+init=epsg:4326")) 

# Transform SpatialPoints to EPSG 5330.    
pts_epsg5330 <- spTransform(x = pts, CRSobj = CRS("+init=epsg:5330"))

Result:

# Get coordinates of new SpatialPoints.
> coordinates(pts_epsg5330)

           x        y
[1,] 3532231 213991.4
[2,] 3532328 214415.3

Upvotes: 0

Related Questions