map2SpatialLines如何在maptools库中工作?

时间:2014-05-20 01:47:21

标签: r coordinates geospatial

maptools库中,有一个函数可以将地图从map库转换为SpatialLine对象。例如:

library(maptools)
library(maps)
usa <- map('usa')
llCRS <- CRS("+proj=longlat +ellps=WGS84")
wrld_sp <- map2SpatialLines(usa, proj4string = llCRS)

然而,地图usa在某种意义上有点奇怪,因为它没有分别描述每组线。相反,它似乎在坐标xy

中聚集在一起
> str(usa)
List of 4
 $ x    : num [1:381] -101.4 -100.6 -99.6 -99 -97.3 ...
 $ y    : num [1:381] 29.7 28.8 27.6 26.4 25.9 ...
 $ range: num [1:4] -124.7 -67 25.1 49.4
 $ names: chr [1:10] "main" "martha's vineyard" "nantucket island" "manhattan" ...
 - attr(*, "class")= chr "map"

然而,函数map2SpatialLines能够获得每个区域(在这种情况下为10个Line对象):

> str(wrld_sp, max.level=3)
Formal class 'SpatialLines' [package "sp"] with 3 slots
  ..@ lines      :List of 10
  .. ..$ :Formal class 'Lines' [package "sp"] with 2 slots
  .. ..$ :Formal class 'Lines' [package "sp"] with 2 slots
  .. ..$ :Formal class 'Lines' [package "sp"] with 2 slots
  .. ..$ :Formal class 'Lines' [package "sp"] with 2 slots
  .. ..$ :Formal class 'Lines' [package "sp"] with 2 slots
  .. ..$ :Formal class 'Lines' [package "sp"] with 2 slots
  .. ..$ :Formal class 'Lines' [package "sp"] with 2 slots
  .. ..$ :Formal class 'Lines' [package "sp"] with 2 slots
  .. ..$ :Formal class 'Lines' [package "sp"] with 2 slots
  .. ..$ :Formal class 'Lines' [package "sp"] with 2 slots
  ..@ bbox       : num [1:2, 1:2] -124.7 25.2 -67.1 49.4
  .. ..- attr(*, "dimnames")=List of 2
  ..@ proj4string:Formal class 'CRS' [package "sp"] with 1 slots

它是如何做到的?我问这个的原因是因为我想在Python中获取给定区域的坐标作为多边形或线对象。

1 个答案:

答案 0 :(得分:1)

尝试使用相同的数据结构

usadf <- data.frame(usa$x, usa$y)
usa_l <- split(usadf, cumsum(is.na(usa$x)))
with(usa_l$'0', plot(usa.x, usa.y, type = 'l'))

simple plot