在 R 中读取二进制文件

2023-12-29

在以下代码中,可以导入具有这些属性的部分 LAS 文件版本 1.1:

列表。项目格式 尺寸要求

  1. X长4字节*
  2. Y长4字节*
  3. Z长4字节*
  4. 强度无符号短 2 字节
  5. 返回编号 3 位(位 0、1、2) 3 位 *
  6. 返回数(给定脉冲) 3 位(位 3、4、5) 3 位 *
  7. 扫描方向标志 1 位 (位 6) 1 位 *
  8. 飞行线边缘 1 位(位 7) 1 位 *
  9. 分类 unsigned char 1 字节 *
  10. 扫描角度等级(-90 至 +90) – 左侧无符号字符 1 字节 *
  11. 用户数据 unsigned char 1 字节
  12. 点源 ID 无符号短 2 字节 *
  13. GPS 时间双 8 字节 *

并返回 x、y、z 和强度(上面列表的第 1 行到第 4 行):

allbytes <- matrix(readBin(con, "raw", n = pointDataRecordLength * numberPointRecords, size = 1, endian = "little"),
                   ncol= pointDataRecordLength, nrow = numberPointRecords, byrow = TRUE)    
close(con)
mm <- matrix(readBin(t(allbytes[,1:(3*4)]), "integer", size = 4, n = 3 * numberPointRecords, endian = "little"), ncol = 3, byrow = TRUE)

mm[,1] <- mm[ ,1] * xyzScaleOffset[1,1] + xyzScaleOffset[1, 2]
mm[,2] <- mm[ ,2] * xyzScaleOffset[2,1] + xyzScaleOffset[2, 2]
mm[,3] <- mm[ ,3] * xyzScaleOffset[3,1] + xyzScaleOffset[3, 2]
colnames(mm) <- c("x", "y", "z")

intensity <- readBin(t(allbytes[, 13:14]), "double", size = 2, n = numberPointRecords, signed = FALSE, endian = "little")

我想扩展代码以读取“返回号码”和“返回号码”(表的第 5 行和第 6 行)。我做错了什么?

returnNumber <- readBin(t(allbytes[, 15]), "integer", size = 1, n = numberPointRecords, signed = FALSE, endian = "little")

预期回报应为 1 到 5 之间的整数。提前致谢!

示例文件:

link http://www.filefactory.com/file/68sdgepk12pd/n/lidar_las

完整代码:

    publicHeaderDescription <- function() {
  hd <- structure(list(Item = c("File Signature (\"LASF\")",
                                "(1.1) File Source ID", "(1.1) Global Encoding",
                                "(1.1) Project ID - GUID data 1", "(1.1) Project ID - GUID data 2",
                                "(1.1) Project ID - GUID data 3", "(1.1) Project ID - GUID data 4",
                                "Version Major", "Version Minor", "(1.1) System Identifier",
                                "Generating Software", "(1.1) File Creation Day of Year",
                                "(1.1) File Creation Year", "Header Size", "Offset to point data",
                                "Number of variable length records",
                                "Point Data Format ID (0-99 for spec)", "Point Data Record Length",
                                "Number of point records", "Number of points by return",
                                "X scale factor", "Y scale factor", "Z scale factor", "X offset",
                                "Y offset", "Z offset", "Max X", "Min X", "Max Y", "Min Y", "Max Z",
                                "Min Z"), Format = c("char[4]", "unsigned short", "unsigned short",
                                                     "unsigned long", "unsigned short", "unsigned short",
                                                     "unsigned char[8]", "unsigned char", "unsigned char", "char[32]",
                                                     "char[32]", "unsigned short", "unsigned short", "unsigned short",
                                                     "unsigned long", "unsigned long", "unsigned char", "unsigned short",
                                                     "unsigned long", "unsigned long[5]", "double", "double", "double",
                                                     "double", "double", "double", "double", "double", "double", "double",
                                                     "double", "double"), Size = c("4 bytes", "2 bytes", "2 bytes",
                                                                                   "4 bytes", "2 byte", "2 byte", "8 bytes", "1 byte", "1 byte",
                                                                                   "32 bytes", "32 bytes", "2 bytes", "2 bytes", "2 bytes", "4 bytes",
                                                                                   "4 bytes", "1 byte", "2 bytes", "4 bytes", "20 bytes", "8 bytes",
                                                                                   "8 bytes", "8 bytes", "8 bytes", "8 bytes", "8 bytes", "8 bytes",
                                                                                   "8 bytes", "8 bytes", "8 bytes", "8 bytes", "8 bytes"), Required =
                                                                                     c("*", "*", "*", "*", "*", "*", "*", "*", "*", "*", "*", "*", "*",
                                                                                       "*", "*", "*", "*", "*", "*", "*", "*", "*", "*", "*", "*", "*", "*",
                                                                                       "*", "*", "*", "*", "*")), .Names = c("Item", "Format", "Size",
                                                                                                                             "Required"), row.names = 2:33, class = "data.frame")
  hd$what <- ""
  hd$what[grep("unsigned", hd$Format)] <- "integer"
  hd$what[grep("char", hd$Format)] <- "raw"
  hd$what[grep("short", hd$Format)] <- "integer"
  hd$what[grep("long", hd$Format)] <- "integer"
  hd$what[grep("double", hd$Format)] <- "numeric"
  hd$signed <- TRUE
  hd$signed[grep("unsigned", hd$Format)] <- FALSE
  ## number of values in record
  hd$n <- as.numeric(gsub("[[:alpha:][:punct:]]", "", hd$Format))
  hd$n[hd$what == "character"] <- 1
  hd$n[is.na(hd$n)] <- 1
  ## size of record
  hd$Hsize <- as.numeric(gsub("[[:alpha:]]", "", hd$Size))
  ## size of each value in record
  hd$Rsize <- hd$Hsize / hd$n
  hd$Rsize[hd$what == "raw"] <- 1
  hd$n[hd$what == "raw"] <- hd$Hsize[hd$what == "raw"]
  hd
}

readLAS <-
  function(lasfile, skip = 0, nrows = NULL, returnSP = FALSE, returnHeaderOnly = FALSE) {

    hd <- publicHeaderDescription()
    pheader <- vector("list", nrow(hd))
    names(pheader) <- hd$Item
    con <- file(lasfile, open = "rb")
    isLASFbytes <- readBin(con, "raw", size = 1, n = 4, endian = "little")
    pheader[[hd$Item[1]]] <- readBin(isLASFbytes, "character", size = 4, endian = "little")
    if (! pheader[[hd$Item[1]]] == "LASF") {
      stop("Not a valid LAS file")
    }
    for (i in 2:nrow(hd)) {
      pheader[[hd$Item[i]]] <- readBin(con, what = hd$what[i], signed = hd$signed[i], size = hd$Rsize[i], endian = "little", n = hd$n[i])
      #print(names(pheader)[i])
      #print(pheader[[hd$Item[i]]])
    }
    close(con)
    ## read the data
    numberPointRecords <- pheader[["Number of point records"]]
    offsetToPointData <- pheader[["Offset to point data"]]
    pointDataRecordLength <-pheader[["Point Data Record Length"]]
    xyzScaleOffset <- cbind(unlist(pheader[c("X scale factor", "Y scale factor", "Z scale factor")]),
                            unlist(pheader[c("X offset", "Y offset", "Z offset")]))


    if (returnHeaderOnly) return(pheader)

    con <- file(lasfile, open = "rb")
    junk <- readBin(con, "raw", size = 1, n = offsetToPointData)

    ## deal with rows to skip and max rows to be read
    if (skip > 0) {
      ## seek is unreliable on windows, or I'm using it incorrectly
      ## so we junk the bytes to skip
      junk <- readBin(con, "raw", size = 1, n = pointDataRecordLength * skip)
      numberPointRecords <- numberPointRecords - skip
      #pos <- seek(con, where = pointDataRecordLength * skip)
      # print(c(pos = seek(con), skip = skip, where = pointDataRecordLength * skip))
    }
    if (!is.null(nrows)) {
      if (numberPointRecords > nrows) numberPointRecords <- nrows
    }

    if (numberPointRecords < 1) stop("no records left to read")


    # include a loop to read just points inside the x and y coordinates

    allbytes <- matrix(readBin(con, "raw", n = pointDataRecordLength * numberPointRecords, size = 1, endian = "little"),
                       ncol= pointDataRecordLength, nrow = numberPointRecords, byrow = TRUE)


    close(con)
    mm <- matrix(readBin(t(allbytes[,1:(3*4)]), "integer", size = 4, n = 3 * numberPointRecords, endian = "little"), ncol = 3, byrow = TRUE)
    gpstime <- NULL
    if (ncol(allbytes) == 28) gpstime <- readBin(t(allbytes[ , 21:28]), "numeric", size = 8, n = numberPointRecords, endian = "little")

    intensity <- readBin(t(allbytes[, 13:14]), "double", size = 2, n = numberPointRecords, signed = FALSE, endian = "little")
    mm[,1] <- mm[ ,1] * xyzScaleOffset[1,1] + xyzScaleOffset[1, 2]
    mm[,2] <- mm[ ,2] * xyzScaleOffset[2,1] + xyzScaleOffset[2, 2]
    mm[,3] <- mm[ ,3] * xyzScaleOffset[3,1] + xyzScaleOffset[3, 2]
    colnames(mm) <- c("x", "y", "z")

    returnNumber <- readBin(t(allbytes[,15]), "integer", size = 1, n = numberPointRecords, signed = FALSE, endian = "little")
    require(bitops)


    if (returnSP) {
      require(sp)
      SpatialPoints(cbind(mm, gpstime, intensity))
    } else {
      cbind(mm, gpstime, intensity)
    }
  }

从您的问题中不清楚您是否了解字段 5-8 全部编码在单个字节中,因此您需要提取这些位。假设你的其余代码可以工作,

require(bitops)

fields.5.to.8 <- readBin(t(allbytes[, 15]), "integer", size = 1, n = numberPointRecords, signed = FALSE, endian = "little")

# bits 0..2: byte & 00000111
field.5 <- bitAnd(7, fields.5.to.8)
# bits 3..5: byte & 00111000 >> 3
field.6 <- bitShiftR(bitAnd(56, fields.5.to.8), 3)
# bit 6: & 0100000 >> 6
field.7 <- bitShiftR(bitAnd(fields.5.to.8, 64), 6)
# bit 7: & 1000000 >> 7 
field.8 <- bitShiftR(bitAnd(fields.5.to.8, 128), 7)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

在 R 中读取二进制文件 的相关文章

  • 如何在 Shiny 中提取动态生成的输入值?

    我正在创建一个闪亮的应用程序 它将根据客户的不同功能为客户生成分数 在我闪亮的应用程序中 我提供了 checkboxGroupInput 来选择所需的功能 根据所选功能 应用程序将动态地将 numericInput 添加到 Web ui 以
  • 绘制 Cox 回归的 Kaplan-Meier 图

    我使用 R 中的以下代码设置了一个 Cox 比例风险模型来预测死亡率 添加协变量 A B 和 C 只是为了避免混淆 即年龄 性别 种族 但我们真正对预测变量 X 感兴趣 X 是一个连续变量 cox model lt coxph Surv t
  • 如何纠正 data.frame 上的字符编码

    我有一个像这样的数据框 data names lt data frame DATA c 1 5 rownames data names lt c IV xc1N JOS xc9 LUC xcdA RAM xd3N TO xd1O data
  • 如何在 R 中执行近似(模糊)名称匹配

    我有一个专门用于生物学期刊的大型数据集 该数据集是由不同的人长时间编写的 因此 数据不采用单一格式 例如 在 作者 栏中我可以找到John Smith Smith John Smith J等 但它们是同一个人 我连最简单的动作都做不了 例如
  • 如何在 R 中的 for 循环内将值存储在向量中

    我正在开始使用 R 但我对以下问题感到非常沮丧 我试图将 for 循环内完成的某些计算的值存储到我之前定义的向量中 问题是如何进行索引 因为for循环迭代代码的次数取决于用户的输入 所以变量i不一定要从1开始 它可以从80开始 for举个例
  • `as.matrix` 和 `as.data.frame` S3 方法与 S4 方法

    我注意到定义as matrix or as data frame作为 S4 类的 S3 方法 使例如lm formula objS4 and prcomp object 开箱即用 如果它们被定义为 S4 方法 则这不起作用 为什么将方法定义
  • R Shinydashboard 自定义 CSS 到 valueBox

    我一直在尝试将 valueBox 的颜色更改为自定义颜色 超出 validColors 中可用的颜色 但一直无法这样做 我知道有一种方法可以使用标签来包含自定义 CSS 但是我无法将它们放在正确的位置 ui lt dashboardPage
  • R在Windows平台Rstudio上打印data.frames中的UTF-8代码

    当数据框中存在UTF 8字符时 将无法正常显示 例如 以下内容是正确的 gt U6731 1 朱 但是当我将其放入数据框中并打印出来时 它是 gt data frame x U6731 x 1
  • data.table 抛出“找不到对象”错误[重复]

    这个问题在这里已经有答案了 我有一个数据表 library data table mydt lt data table index 1 10 当我在全局环境中尝试它时 我可以让它工作 但当我在调试器中或在包测试中使用它时却无法工作 问题是我
  • 使用选定因子水平的值向 ggplot-barchart 添加水平线

    在这个情节中 df lt data frame factor as factor c rep A 3 rep B 3 Treatment c rep c A B C 2 values runif 6 0 1 ggplot df aes Tr
  • ggplot:如何限制条形图中的输出,以便仅显示最频繁出现的情况?

    我几个小时以来一直在寻找这个简单的东西 但没有结果 我有一个数据框 其中一列为变量 国家 地区 我想要两件事以下 绘制最常见的国家 地区 最常见的位于顶部 找到部分解决方案EDIT找到完整的解决方案 gt gt 重点问题是根据频率限制条形图
  • R:如何将字符/数字转为1,NA转为0?

    有没有一种简单的方法可以将列的字符 数字变为 1 将 NA 变为 0 这里有一些示例数据 我想将其应用于 3 4 structure list Item Code c 176L 187L 191L 201L 217L 220L Item x
  • 如何将旋转的 NetCDF 转换回正常的纬度/经度网格?

    我有一个带有旋转坐标的 NetCDF 文件 我需要将其转换为正常的纬度 经度坐标 经度为 180到180 纬度为 90到90 library ncdf4 nc open dat nf 对于尺寸 它显示 1 5 variables exclu
  • rvest 函数 html_nodes 返回 {xml_nodeset (0)}

    我正在尝试抓取以下网站的数据框 http stats nba com game 0041700404 playbyplay http stats nba com game 0041700404 playbyplay 我想创建一个表格 其中包
  • HTTR GET 新错误:SSL 证书问题:证书已过期

    我已经运行这段代码几个月了 没有出现任何问题 今天我突然开始在我的两台 AWS 服务器上收到以下错误消息 错误 curl curl fetch memory url handle handle SSL证书问题 证书已过期 当尝试运行以下代码
  • 条件和分组 mutate dplyr

    假设我有以下每个抽屉库存增加的数据 gt socks year drawer nbr sock total 1990 1 2 1991 1 2 1990 2 3 1991 2 4 1990 3 2 1991 3 1 我想要一个二进制变量来标
  • R:改变堆积条形图的颜色

    library ggplot2 df2 lt data frame supp rep c VC OJ each 3 dose rep c D0 5 D1 D2 2 len c 6 8 15 33 4 2 10 29 5 head df2 g
  • 安装 2.15 后 ggplot2 中的 alpha 通道不起作用

    更新到 R 2 15 后 ggplot 中的 alpha 通道似乎不再起作用 plot rnorm 100 rnorm 100 bg cc000055 pch 21 工作得很好但是 qplot rnorm 100 rnorm 100 col
  • 将 Excel 文件读入 R 并锁定单元格

    我有一个 Excel 电子表格要读入 R 它受密码保护并锁定了单元格 我可以使用 excel link 导入受密码保护的文件 但我不知道如何解锁 取消保护单元格 excel link 给了我这个错误 gt
  • 斯皮尔曼相关性和联系

    我正在一小组配对排名上计算斯皮尔曼的 rho 斯皮尔曼因处理领带不当而闻名 例如 取2组8个排名 即使两组中有6个是平局 相关性仍然很高 gt cor test c 1 2 3 4 5 6 7 8 c 0 0 0 0 0 0 7 8 met

随机推荐

  • 在 Heroku 控制台中创建新模型时出现 NoMethodError (nil:NilClass 的未定义方法“名称”)

    我刚刚推送了 Heroku 并尝试通过rails admin 添加模型来进行一些测试 当我这样做时 我得到了一个通用错误页面 我进入日志并注意到这条消息 NoMethodError nil NilClass 的未定义方法 名称 然后我打开
  • 大型下拉菜单悬停时闪烁

    我在网上找到的大型下拉菜单有问题 它非常适合我的目的 但有时表现得很奇怪 并且存在闪烁问题 我找到它的链接在这里 http bootsnipp com snippets featured mega menu slide down on ho
  • 以编程方式重新安装应用程序 apk,无需下载

    Due to 我需要用户重新安装我的应用程序 以便其他应用程序检测到清单权限 这已经让用户感到沮丧 但此外 我找不到从 data app 中存储的 apk 重新安装应用程序的方法 因此我必须在触发之前将相同版本下载到存储卡通常的安装意图 我
  • 实体框架连接字符串utf8

    我想向我的实体框架数据库应用程序添加 utf8 支持 sql server 2008 r2 我想我需要将字符集添加到连接字符串中 这就是我的connectionString的工作原理 当然是匿名的
  • 如何使用log4j创建多个日志文件

    我想创建单独的日志文件 一个用于信息另一个用于调试 我正在使用下面的 log4j property 文件 请建议如何修改不同文件中的二级日志记录 Root logger option log4j rootLogger info file D
  • 我需要做什么才能在我的 xampp (Windows) 上运行 OpenSSL 扩展? :( [复制]

    这个问题在这里已经有答案了 我已经尝试过所有 但不起作用 我确实将 libeay32 dll 和 ssleay32 dll 放在 System32 windows 文件夹中 并在 php ini 上启用了 php openssl dll 扩
  • 在哪里可以找到特定于应用程序的 context.xml 文件?

    我读到了context xml file 在雄猫中 是特定于应用程序的 我已经从我的 netbeans IDE 创建了两个 Web 项目 并使用 Tomcat 作为服务器 但是我无法找到特定于应用程序的项目context xml文件 我只找
  • 在 for 循环中更改 SVG 线的 strokeDashoffset

    我正在尝试制作一条线扩展的动画 我已经在 css 中拥有它 但我需要在 javaScript 中完成它 因为这是我可以获得我需要的路径长度的唯一方法 我想我已经非常接近了 但它不起作用 有任何想法吗 以下是我的代码 正如你所看到的 我得到了
  • Python 中增加版本号

    我正在尝试对 CVS 中的文件进行版本号更新 我最初的逻辑是更新一个浮点数 1 1 gt 1 2 gt 1 3 它工作得很好 直到我到达 1 9 然后它更新到 2 0 我正在尝试使用此逻辑更新到 1 10 但是当我尝试增加 1 x 中的 x
  • 重命名类型后,我无法访问其某些方法

    为了防止项目的不同文件存在多个依赖关系 并且由于我可能会更改数据的呈现方式 我决定为绘制2D包 https github com llgcode draw2d 由于我不需要其他任何东西 我只是重命名了其中一种类型 type CanvasCo
  • 为什么我收到 apple-touch-icon-precomposed.png 错误

    我创建了一个新的 Rails3 项目 但我在服务器日志中多次看到以下日志 为什么我会收到这些请求以及如何避免这些请求 开始获取 192 168 6 2 的 apple touch icon precomposed png 2012 09 1
  • 设置数字格式以分隔千位值(例如 12000000 将变为 12 000 000)

    在lua中 我想格式化一个整数 或浮点数 以用空格 或逗号 如美国 分隔每三个小数位 因此例如数字120000010将显示为 120 000 010 或者 120 000 010 我已经发现this http lua users org w
  • RethinkDB - 更新嵌套数组

    我有一个调查表 如下所示 id Id date Date clients client id Id contacts contact id Id score Number feedback String email String 我需要更新
  • 表达式 std::string {} = "..." 是什么意思?

    在此代码中 include
  • 网格模板区域和网格模板列之间的关系

    我是编码新手 似乎没有正确理解 CSS 网格模型 在下面的代码中 网格分为 3 个网格模板列 每列 300 像素 但网格模板区域中每行有 8 个单元 例如 hd hd hd hd hd hd hd hd 而不是 3 个单元这对我来说没有意义
  • 跨域请求被阻止:同源策略不允许读取远程资源 - React js

    我在 Mac OS 设备上运行我的项目 并且想从另一台笔记本电脑进行访问 第一个设备也从服务器获取所有响应 http 192 168 1 101 3000 http 192 168 1 101 3000 但另一台笔记本电脑我收到此错误消息
  • 如何将llvm IR转换为c代码?

    有什么方法可以将 llvm IR 转换为 c 代码并保留其语义吗 例如 我们可以先将c代码编译到llvm IR 然后再将其编译回另一段c代码吗 我不希望这两个文件是相同的 但它们需要具有相同的功能 谢谢 您可以使用 C 后端 llc mar
  • 使用 MIDP 通过 http 从服务器读取 UTF8 字符串

    我想使用 java MIDP 从我控制的服务器读取 UTF 8 字符串 我的服务器正在发送 UTF 8 数据 下面的代码很接近 c StreamConnection Connector open myServer Connector REA
  • 使用样式或 Javascript 使图像变亮

    我想使用 css 或 javascript 在鼠标悬停时使网页上的图像变亮 我见过一些在样式中使用不透明度和滤镜的示例 但它们似乎对我不起作用 提前致谢 CP UPDATE 一个纯 CSS 解决方案是使用CSS 过滤器 https deve
  • 在 R 中读取二进制文件

    在以下代码中 可以导入具有这些属性的部分 LAS 文件版本 1 1 列表 项目格式 尺寸要求 X长4字节 Y长4字节 Z长4字节 强度无符号短 2 字节 返回编号 3 位 位 0 1 2 3 位 返回数 给定脉冲 3 位 位 3 4 5 3