将"map"对象转换为"SpatialPolygon"对象

Mik*_*han 15 maps r r-maptools r-sp

我猜我有一个简单的解决方案,但我遇到了一些麻烦.

我试图转换以下map对象:

require(maps)
usa <- map("state")
Run Code Online (Sandbox Code Playgroud)

SpatialPolygon使用map2SpatialPolygons函数进入对象:

require(maptools)
usa.sp <- map2SpatialPolygons(usa, IDs=usa$names,proj4string=CRS("+proj=longlat"))
Run Code Online (Sandbox Code Playgroud)

我一直收到以下错误:

Error in map2SpatialPolygons(usa, IDs = usa$names, proj4string = CRS("+proj=longlat")) : 
  map and IDs differ in length
Run Code Online (Sandbox Code Playgroud)

经过一些研究,看起来ID的长度为63,并且map在应用函数后对象的长度为169 .NAmat2xyList(cbind(map$x, map$y))(我找不到源代码).

有人有主意吗?这是usa地图对象的结构:

> str(usa)
List of 4
 $ x    : num [1:1705] -88.4 -88.1 -88 -87.9 -87.8 ...
 $ y    : num [1:1705] 30.4 30.4 30.8 30.6 30.3 ...
 $ range: num [1:4] -124.7 -67 25.1 49.4
 $ names: chr [1:63] "alabama" "arizona" "arkansas" "california" ...
 - attr(*, "class")= chr "map"
Run Code Online (Sandbox Code Playgroud)

Mik*_*han 16

刚刚在"使用R应用空间数据分析"一文中找到了一些代码.它很棒!

require(maps)
usa <- map("state", fill = TRUE)

require(sp)
require(maptools)
IDs <- sapply(strsplit(usa$names, ":"), function(x) x[1])
usa <- map2SpatialPolygons(usa, IDs=IDs, proj4string=CRS("+proj=longlat +datum=WGS84"))
Run Code Online (Sandbox Code Playgroud)


小智 6

多边形具有表面(面积),因此,关键参数是fill = TRUE

usa <- map('state', fill = TRUE)
Run Code Online (Sandbox Code Playgroud)

将参数值更改为TRUE会停止错误消息.