HK80 (Hong Kong 1980, EPSG: 2326) is one of the most widely used geographical coordinate systems in Hong Kong. Compared to latitude and longitude in the WGS84 (EPSG: 4326) system, HK80 uses X and Y for denoting easting and northing from the origin. This tutorial describes how to convert HK80 coordinates to WGS84 coordinate system using the HK80 package.

The HK80 R package was develped based on the equations provided by the Survey & Mapping Office, Lands Department, Hong Kong SAR Governemt, which is available at (https://www.geodetic.gov.hk/common/data/pdf/explanatorynotes_c.pdf).

Over the past few years, a number of new tools have appreared. For example, the HK80 online conversion tool of the Survey and Mapping Office, Lands Department (https://www.geodetic.gov.hk/en/services/tform/tform.aspx) provides an API that can process user’s query. The sf R package provides the function st_transform based on the proj package, making the conversion between different coordinate systems very convenient. There is also a Python package called hk80 (https://pypi.org/project/hk80/) for converting the coordinates, basing upon the more widely used package pyproj (https://pyproj4.github.io/pyproj/stable/#, https://proj.org/).

The online conversion tool supplied by the Hong Kong Lands Department is the first choice among different conversion tools. However, because the server may have restrictions when querying a large amount of data, the conversion tool may not be very convinient (although I have not tried to get my IP blocked).

sf and HK80, on the other hand, do not have these restrictions. Both the results from sf and HK80 are reliable, but sf is easier to use. The results of the HK80 package can be used as a reference.

The conversion between DMS to decimal format and vice versa are also provided as an appendix.

HK80GRID to WGS84

HK80 R package

library(HK80)
HK1980GRID_TO_WGS84GEO(N = 820359.389, E = 832591.320)
##   latitude longitude
## 1 22.32225  114.1412

The official online conversion tool

the Geodetic Survey Section, Lands Department, Hong Kong SAR Gov.

API example: http://www.geodetic.gov.hk/transform/v2/?inSys=hkgrid&e=832591.320&n=820359.389

library(jsonlite)
data1 <- fromJSON("http://www.geodetic.gov.hk/transform/v2/?inSys=hkgrid&e=832591.320&n=820359.389")
names(data1)
##  [1] "wgsLat"      "wgsLong"     "hkLat"       "hkLong"      "utmGridZone"
##  [6] "utmGridE"    "utmGridN"    "utmRefZone"  "utmRefE"     "utmRefN"
data1$wgsLat
## [1] 22.32224
data1$wgsLong
## [1] 114.1412

the sf package

## Linking to GEOS 3.8.1, GDAL 3.1.4, PROJ 6.3.1
p1 = st_point(c(832591.320, 820359.389))
sfc = st_sfc(p1, crs = 2326)
(st_transform(sfc, 4326))
## Geometry set for 1 feature 
## Geometry type: POINT
## Dimension:     XY
## Bounding box:  xmin: 114.1412 ymin: 22.32224 xmax: 114.1412 ymax: 22.32224
## Geodetic CRS:  WGS 84
## POINT (114.1412 22.32224)

WGS84 to HK80GRID

the HK80 R package

library(HK80)
WGS84GEO_TO_HK1980GRID(latitude = 22.32224, longitude = 114.14118)
##          N        E
## 1 820358.7 832591.4

The official online conversion tool

from the Geodetic Survey Section, Lands Department, Hong Kong SAR Gov.

# Copy the following URL to browser
# http://www.geodetic.gov.hk/transform/v2/?inSys=wgsgeog&outSys=hkgrid&lat=22.32224&long=114.14118&h=23.128
# {"hkN": 820358.910,"hkE": 832590.508,"hkpd": 26.009}

library(jsonlite)
data1 <- fromJSON("http://www.geodetic.gov.hk/transform/v2/?inSys=wgsgeog&outSys=hkgrid&lat=22.32224&long=114.14118&h=23.128")

names(data1)
## [1] "hkN"  "hkE"  "hkpd"
data1$hkN
## [1] 820358.9
data1$hkE
## [1] 832590.5

the sf package

library(sf)
p1 = st_point(c(114.14118, 22.32224))
sfc = st_sfc(p1, crs = 4326)
(ccc <- st_transform(sfc, 2326))
## Geometry set for 1 feature 
## Geometry type: POINT
## Dimension:     XY
## Bounding box:  xmin: 832590.5 ymin: 820358.9 xmax: 832590.5 ymax: 820358.9
## Projected CRS: Hong Kong 1980 Grid System
## POINT (832590.5 820358.9)

Appendix: DMS to demical and vice versa

Using the sp package

library(sp)
dd2dms(114.14118) # decimal to Degree, Minute, Second format
## [1] 114d8'28.248"E
as.numeric(dd2dms(114.14118)) #
## [1] 114.1412
char2dms("47d15'6.12\"E")
## [1] 47d15'6.12"E
as.numeric(char2dms("47d15'6.12\"E"))
## [1] 47.2517

Using the biogeo package

library(biogeo)
res <- dms2dd(47,15,6.12,"E") # ns letters (N,S,E,W)
print(res)
## [1] 47.2517
dd2dmslong(114.14118)
##   xdeg xmin xsec EW
## 1  114    8 28.2  E
dd2dmslat(22.32224)
##   ydeg ymin ysec NS
## 1   22   19 20.1  N

SessionInfo

## R version 4.0.3 (2020-10-10)
## Platform: x86_64-apple-darwin17.0 (64-bit)
## Running under: macOS Big Sur 10.16
## 
## Matrix products: default
## BLAS:   /Library/Frameworks/R.framework/Versions/4.0/Resources/lib/libRblas.dylib
## LAPACK: /Library/Frameworks/R.framework/Versions/4.0/Resources/lib/libRlapack.dylib
## 
## locale:
## [1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8
## 
## attached base packages:
## [1] stats     graphics  grDevices utils     datasets  methods   base     
## 
## other attached packages:
## [1] biogeo_1.0     sp_1.4-5       sf_0.9-8       jsonlite_1.7.2 HK80_0.0.3    
## 
## loaded via a namespace (and not attached):
##  [1] Rcpp_1.0.6         lattice_0.20-41    class_7.3-18       assertthat_0.2.1  
##  [5] rprojroot_2.0.2    digest_0.6.27      utf8_1.2.1         R6_2.5.0          
##  [9] evaluate_0.14      e1071_1.7-6        pillar_1.5.1       rlang_0.4.10      
## [13] curl_4.3           raster_3.4-5       vegan_2.5-7        jquerylib_0.1.3   
## [17] Matrix_1.3-2       rmarkdown_2.7      pkgdown_1.6.1      textshaping_0.3.3 
## [21] desc_1.3.0         splines_4.0.3      foreign_0.8-81     stringr_1.4.0     
## [25] proxy_0.4-25       compiler_4.0.3     xfun_0.22          pkgconfig_2.0.3   
## [29] systemfonts_1.0.1  mgcv_1.8-34        htmltools_0.5.1.1  tidyselect_1.1.0  
## [33] tibble_3.1.0       codetools_0.2-18   fansi_0.4.2        permute_0.9-5     
## [37] crayon_1.4.1       dplyr_1.0.5        MASS_7.3-53.1      grid_4.0.3        
## [41] nlme_3.1-152       lifecycle_1.0.0    DBI_1.1.1          magrittr_2.0.1    
## [45] units_0.7-1        KernSmooth_2.23-18 stringi_1.5.3      cachem_1.0.4      
## [49] fs_1.5.0           bslib_0.2.4        ellipsis_0.3.1     ragg_1.1.2        
## [53] generics_0.1.0     vctrs_0.3.6        tools_4.0.3        glue_1.4.2        
## [57] purrr_0.3.4        parallel_4.0.3     fastmap_1.1.0      yaml_2.2.1        
## [61] cluster_2.1.1      maptools_1.1-1     classInt_0.4-3     memoise_2.0.0     
## [65] knitr_1.31         sass_0.3.1

Further Reading