转换“地图";反对"SpatialPolygon"目的 [英] Converting a "map" object to a "SpatialPolygon" object
问题描述
我猜有一个简单的解决方案可以解决我遇到的问题,但是我遇到了麻烦.
I am guessing there is a simple solution to the problem I have been having, but I am having some trouble.
我正在尝试转换以下map
对象:
I am trying to convert the following map
object:
require(maps)
usa <- map("state")
使用map2SpatialPolygons
函数将
转换为SpatialPolygon
对象:
require(maptools)
usa.sp <- map2SpatialPolygons(usa, IDs=usa$names,proj4string=CRS("+proj=longlat"))
我一直收到以下错误:
Error in map2SpatialPolygons(usa, IDs = usa$names, proj4string = CRS("+proj=longlat")) :
map and IDs differ in length
经过研究,在应用函数.NAmat2xyList(cbind(map$x, map$y))
之后,ID似乎长63,map
对象长169(我找不到源).
After some research, it looks like the IDs have length 63 and the map
object has length 169 after applying the function .NAmat2xyList(cbind(map$x, map$y))
(for which I cannot find the source to).
有人有什么想法吗?这是usa
地图对象的结构:
Anyone have any ideas? Here is the structure of the usa
map object:
> 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"
推荐答案
仅在文本使用R进行应用空间数据分析"中找到了一些代码.效果很好!
Just found some code in the text "Applied Spatial Data Analysis with R". It works great!
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"))
这篇关于转换“地图";反对"SpatialPolygon"目的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!