vignettes/HK80-vignette.Rmd
HK80-vignette.Rmd
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.
library(HK80)
HK1980GRID_TO_WGS84GEO(N = 820359.389, E = 832591.320)
## latitude longitude
## 1 22.32225 114.1412
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
## 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)
library(HK80)
WGS84GEO_TO_HK1980GRID(latitude = 22.32224, longitude = 114.14118)
## N E
## 1 820358.7 832591.4
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
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)
## [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
## 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
Jeroen Ooms (2014). The jsonlite Package: A Practical and Consistent Mapping Between JSON Data and R Objects. arXiv:1403.2805 URL https://arxiv.org/abs/1403.2805.
Jinlong Zhang (2016). HK80: Conversion Tools for HK80 Geographical Coordinate System. R package version 0.0.2. https://CRAN.R-project.org/package=HK80
Mark Robertson (2016). biogeo: Point Data Quality Assessment and Coordinate Conversion. R package version 1.0. https://CRAN.R-project.org/package=biogeo
Pebesma, E., 2018. Simple Features for R: Standardized Support for Spatial Vector Data. The R Journal 10 (1), 439-446, https://doi.org/10.32614/RJ-2018-009
Roger S. Bivand, Edzer Pebesma, Virgilio Gomez-Rubio,