1.将地球表面从西经180 度到东经180 度、从南纬90 度到北纬90 度,划分成18*18 个field,用AA ~RR 表示,第一个字母代表经度编码,第二个字母为纬度编码。
2.每个field 进一步划分为10*10 个square,用00~99 表示。
3.每个square 划分为24*24 个subsquare,用AA~XX 表示。
4.按顺序将field、square、subsquare 的编码组合,则具体位置所在的网格可以用六个字符表示,如本人所在的网格编码为PN44TO,该编码的第1、3、5 位由经度确定,第2、4、6 位由纬度确定。
这张图,为本人用ggplot2实现,不涉及图片版权,代码如下:
- library(ggplot2)
- library(maptools)
- library(sf)
- orig <-
- st_read("worldmap.shp", stringsAsFactors = F)#导入shp数据
- text <- data.frame(
- x = rep(seq(-170, 170, 20), 18),
- y = rep(seq(-85, 85, 10), each = 18),
- label = paste(rep(LETTERS[1:18], 18),
- rep(LETTERS[1:18], each =18)))
- ggplot(orig) +
- geom_sf(aes(fill = `MAPCOLOR7`, geometry = `geometry`),
- size = 0.2, color = "black") +
- geom_hline(yintercept = seq(-90, 90, 10)) +
- geom_vline(xintercept = seq(-180, 180, 20)) +
- geom_text(data = text, aes(x, y, label = label),
- color = "red", size = 3.5) +
- coord_sf(xlim = c(-180, 180), ylim = c(-90, 90), expand = FALSE) +
- theme(
- panel.background = element_rect(fill = "lightblue",
- colour = "black"),
- legend.position = "none",
- panel.grid = element_blank(),
- axis.title = element_blank())
下面,提供两个自定义函数,实现经纬度和网格定位的相互转换。
- #经纬度转网格定位,参数为经度的度、分、秒和纬度的度、分、秒
- #东经为正,西经为负,北纬为正,南纬为负
- toMLS <-
- function (lonD = 129, lonM = 37, lonS = 27.17,
- latD = 44, latM = 35, latS = 48.07) {
- x <- round(lonD + lonM / 60 + lonS / 3600, 6)
- if (x < -180 | x >= 180) print("经度错了吧?")
- y <- round(latD + latM / 60 + latS / 3600, 6)
- if (y < -90 | y >= 90) print("纬度错了吧?")
- MLS <- paste(LETTERS[floor(x / 20 + 9) + 1],
- LETTERS[floor(y / 10 + 9) + 1],
- floor((x / 20 + 9 - floor(x / 20 + 9)) * 10),
- floor((y / 10 + 9 - floor(y / 10 + 9)) * 10),
- LETTERS[floor(((x / 20 + 9 - floor(x / 20 + 9)) * 10 - floor((
- x / 20 + 9 - floor(x / 20 + 9)
- ) * 10)) * 24) + 1],
- LETTERS[floor(((y / 10 + 9 - floor(y / 10 + 9)) * 10 - floor((
- y / 10 + 9 - floor(y / 10 + 9)
- ) * 10)) * 24) + 1],
- sep = "")
- return(sprintf("Longitude:%06f; Latitude:%06f; MLS:%s",
- x,y,MLS))
- }
- #网格定位转经纬度范围,参数为网格定位字符
- fromMLS <- function (MLS = "PN44TO") {
- temp <- unlist(strsplit(MLS, split = ""))
- x <-
- -200 + which(LETTERS == temp[1]) * 20 +
- as.integer(temp[3]) * 2 + which(LETTERS == temp[5]) / 12
- y <-
- -100 + which(LETTERS == temp[2]) * 10 +
- as.integer(temp[4]) + which(LETTERS == temp[6]) / 24
- return(
- sprintf(
- "MLS:%s; Longitude:from %06f to %06f; Latitude:from %06f to %06f",
- MLS, x - 1 / 12, x, y - 1 / 24, y))
- }
- #以本人位置为例,城市MDJ 的某个角落,网格定位编码为 PN44TO
- toMLS() #默认参数
- #[1] "Longitude:129.624214; Latitude:44.596686; MLS:PN44TO"
- fromMLS()#默认参数
- #[1] "MLS:PN44TO; Longitude:from 129.583333 to 129.666667; Latitude:from 44.583333 to 44.625000"
上述代码经过专业验证无误,分享出来供无线电爱好者利用。