【问题标题】:R: strange map formatR:奇怪的地图格式
【发布时间】:2012-03-29 19:50:51
【问题描述】:

我得到一个包含以下数据的文件:

str(dat)
List of 2
 $ x: Named num [1:28643] 2714769 2728569 NA 2728569 2740425 ...
  ..- attr(*, "names")= chr [1:28643] "h" "h" "" "h" ...
 $ y: Named num [1:28643] 925000 925000 NA 925000 925000 ...
  ..- attr(*, "names")= chr [1:28643] "h" "h" "" "h" ...
 - attr(*, "class")= chr [1:2] "bor" "list"

dat$x[1:10]
      h       h               h       h               h       h               h 
2714769 2728569      NA 2728569 2740425      NA 2740425 2751585      NA 2751585 

dat$y[1:10]
      h      h             h      h             h      h             h 
 925000 925000     NA 925000 925000     NA 925000 925000     NA 925000 

class(dat)
"bor"  "list"

table(names(dat$x))
          h 
  479 28164 

table(names(dat$y))
          h 
  479 28164 

plot(dat, type='l') 得到一张漂亮的地图。

我在第 38 页的“使用 R 的应用空间数据分析”(Bivand,Pebesma,Gomez-Rubio;Springer 2008)中读到了 S 中使用的一种旧/简单形式的线-'对象',它们似乎有相似之处到我的档案。这种格式将线定义为“起点;终点;NA”三元组。

你知道这种格式吗? 如何将其转换为 sp 对象?

提前致谢

【问题讨论】:

  • 对象的类是什么?
  • 我不知道那种格式,但你似乎有一个线段列表。 $x$y中所有条目的名称总是h吗?它们可能是识别哪些段属于不同的线/多边形的候选者。另一个问题是结构是否在 dat 对象的整个长度上始终相同,即h - h - NA?如果没有,那里可能还有其他结构......
  • 该类在“类”属性中——它是一个“bor”——不管它是什么。现在,是线、点还是多边形?

标签: r format type-conversion gis geo


【解决方案1】:

根据您的信息,这是一种可行的方法:

假设您的数据表示线并且NA 值表示每条线的结尾,您可以执行以下操作将数据转换为空间线:

# Creating artificial data for the example
dat <- list()
dat$x <- rnorm(1000) + rep(c(rep(0, 99), NA), 10)
dat$y <- dat$x + rnorm(1000)

# For simplicity, convert to data frame
# (this would be the first step for you to do with your data)
mydat <- data.frame(x = dat$x, y = dat$y)

# Convert each part to a line, using the NA values as breaks
mylines <- list()
last <- 1
for(i in 1:nrow(mydat)){
    if(is.na(mydat$x[i])){
        print(i)
        mylines[[as.character(i)]] <- Lines(Line(mydat[last:(i-1),]), ID = as.character(i))
        last <- i+1
    }
}

# Convert to spatial lines object
mylines <- SpatialLines(mylines)

# Plot to see if it worked
plot(mylines)

【讨论】:

    猜你喜欢
    • 2018-08-14
    • 2020-01-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-10-05
    相关资源
    最近更新 更多