我正在使用AIS(自动识别系统)数据来定位船只。我能够遵循this guide来成功解码几乎所有信息(与在线解码here相比)。
但是,我在经度部分遇到了问题。我认为与十进制值为负有关,但是我无法弄清楚要正确修改代码中的内容。
TLDR版本:如何从二进制字符串1101001000001001001110010000转换为十进制值-48196720(或48196720)?
完整版本:
玩具数据:
library(dplyr)
library(tidyr)
library(stringr)
# choose an example - two strings are provided.
# The first string shows the issue with the longitude,
# whereas the second string (where longitude is positive) has no issue
s <- "15E3tB001;J@BLPaK5j7qFA406;d"
# s <- "133m@ogP00PD;88MD5MTDww@2D7k"
### for input into the online decoder - use these:
# !AIVDM,1,1,,A,14eG;o@034o8sd<L9i:a;WF>062D,0*7D
# !AIVDM,1,1,,A,133m@ogP00PD;88MD5MTDww@2D7k,0*46
temp <- data.frame(V6 = s) %>%
# splitting the AIS info into separate characters
mutate(char_split = str_split(V6,pattern=""))
temp$Text <- apply(temp, 1, function(x) paste(unlist(x$char_split), collapse = ","))
temp <- temp %>%
select(-char_split) %>%
# and then into separate columns
separate(Text, into = paste0("v", 1:43, sep = ""), sep = ",", fill = "right")
ASCII <- temp %>%
select(v1:v43)
# translating to ASCII
ASCII <- apply(ASCII, c(1, 2), function(x) utf8ToInt(x))
# translating to 6-bit
ASCII <- apply(ASCII, c(1, 2), function(x) ifelse(x <= 88, x - 48, x - 48 - 8))
一旦数据以ASCII格式,则需要转换为二进制
# making binary
Binary <- apply(ASCII, c(1, 2), function(x){ paste(rev(as.integer(intToBits(x))[1:6]), collapse = "")})
# pasting all the binary info into a single string
temp$Binary <- apply(Binary, 1, function(x) paste(x, collapse = "" ))
temp <- temp %>%
select(V6, Binary) %>%
# selecting bits of the single binary string,
#and translating back to decimal, as per the guide
mutate(MMSI = strtoi(substr(Binary, 9, 38), base = 2),
SOG = strtoi(substr(Binary, 50, 60), base = 2)/10,
Accuracy = strtoi(substr(Binary, 61, 61), base = 2),
Lon = strtoi(substr(Binary, 62, 89), base = 2)/600000,
Lat = strtoi(substr(Binary, 90, 116), base = 2)/600000,
COG = strtoi(substr(Binary, 117, 128), base = 2)/10,
Heading = strtoi(substr(Binary, 129, 137), base = 2))
输出:
select(temp, -Binary, -V6)
当我与在线解码器进行比较时,除了经度之外,其他所有东西都匹配。在解码器中,结果是80.3278667(尽管实际上是-80.3278667),而我的是367.0646。为了对此进行反向工程,我看了temp$Binary
的相关子字符串:
mine <- substr(temp$Binary, 62, 89)
RevEng <- -80.3278667 * 600000
binaryLogic:::as.binary(as.integer(RevEng), signed = FALSE)
mine
所以看起来RevEng值匹配我的二进制字符串的右尾,但是我不知道为什么它不匹配完整的二进制字符串,或者从这里开始怎么做... >
答案 0 :(得分:1)
与博客文章告诉您的相反,经度是一个有符号的整数。但是,它仅使用28位,而R内部使用32位。因此,您必须自己处理two's compliment conversion。对于设置了最高位的任何数字,您都必须减去2^28
,例如:
mine <- "1101001000001001001110010000"
strtoi(mine, base = 2) - 2^28
#> [1] -48196720
您可以使用二进制字符串上的substr
或通过查找数字>= 2^27
来标识这些数字。
顺便说一句,对纬度的修改也是如此,因为它仅使用27位。