R 将空间多边形转换为贴图对象

R 将空间多边形转换为贴图对象,r,maps,gis,geospatial,spatial,R,Maps,Gis,Geospatial,Spatial,我正在尝试将R中的空间多边形对象转换为地图对象。我已经通过前面回答的问题做到了这一点,特别是()问题,但是我遇到了与此线程中的人相同的问题:我得到了奇怪的多边形链接,使得地图无法使用 我已经在下面包含了完整的复制代码 谢谢你的帮助 library(sp) library(spdep) library(maps) library(rgdal) ## load a file from GADM (you just have to specify the countries "special par

我正在尝试将R中的空间多边形对象转换为地图对象。我已经通过前面回答的问题做到了这一点,特别是()问题,但是我遇到了与此线程中的人相同的问题:我得到了奇怪的多边形链接,使得地图无法使用

我已经在下面包含了完整的复制代码

谢谢你的帮助

library(sp)
library(spdep)
library(maps)
library(rgdal)

## load a file from GADM (you just have to specify the countries "special part" of the file name, like "ARG" for Argentina. Optionally you can specify which level you want to have
loadGADM <- function (fileName, level = 0, ...) {
load(url(paste("http://gadm.org/data/rda/", fileName, "_adm", level, ".RData", sep     = "")))
gadm
}

## the maps objects get a prefix (like "ARG_" for Argentina)
changeGADMPrefix <- function (GADM, prefix) {
GADM <- spChFIDs(GADM, paste(prefix, row.names(GADM), sep = "_"))
GADM
}

## load file and change prefix
loadChangePrefix <- function (fileName, level = 0, ...) {
theFile <- loadGADM(fileName, level)
theFile <- changeGADMPrefix(theFile, fileName)
theFile
}

## this function creates a SpatialPolygonsDataFrame that contains all maps you specify in "fileNames".
## E.g.: 
## spdf <- getCountries(c("ARG","BOL","CHL"))
## plot(spdf) # should draw a map with Brasil, Argentina and Chile on it.
getCountries <- function (fileNames, level = 0, ...) {
polygon <- sapply(fileNames, loadChangePrefix, level)
polyMap <- do.call("rbind", polygon)
polyMap
}



indiamap <- getCountries("IND",level=1)

xx <- indiamap
require(reshape)
xx@data$id <- rownames(xx@data)

# Convert to dataframe
xx.df <- as.data.frame(xx)

#Fortfy automagic
require(ggplot2)
xx.fort <- fortify(xx, region="id")

# Join operation - one row per coordinate vector
require(plyr)
xx <- join(xx.fort, xx.df,by="id")

# Split by ID because we need to add NA at end of each set of polygon coordinates to 'break' the line
xxSp <- split(xx, xx$id)

# Need to insert NA at end of each polygon shape to cut off that shape
xxL <- do.call( rbind , (lapply( xxSp , function(x) { j <- x[ nrow(x) , ] ; j[1:2] <- c(NA,NA); rbind( x , j ) })) )


# Create list object with same structure as map object
xxMap <- list( x = xxL$long , y = xxL$lat , range = c( range(xxL$long) , range(xxL$lat) ) , names = as.character(unique( xxL$NAME ) ) )

# Define as a map class object
attr(xxMap , "class") <- "map"

# Plot!!
map( xxMap )
库(sp)
图书馆(spdep)
图书馆(地图)
图书馆(rgdal)
##从GADM加载一个文件(您只需指定文件名的国家“特殊部分”,如阿根廷的“ARG”。您还可以选择指定您想要的级别

loadGADM问题在于每个
id
s(省份)由多个多边形组成,例如岛屿。
空间多边形数据框
有信息表明这些单独的多边形是相同的
id
,但不应通过线连接。通过转换为
地图
,该信息将丢失。在您的解决方法中,您只需在
id
之间添加
NA
>s、 不是在所有单独的多边形之间

诀窍是为所有单独的片段生成一个唯一的id,这样就可以在每个小片段之后添加一个NA。由
join
创建的
xx
包含一个名为
piece
的字段。该字段似乎在单个
id
中对每个多边形都有一个单独的编号。例如,
id==4的多边形由3个部分组成的SITS,例如3个岛。然后可通过以下方式生成所有多边形的唯一id:

xx$myid <- xx$ID_1*1000 + as.numeric(xx$piece)

xx$myid当我使用
library(sp)时,我在这一行得到一个错误:
indiamap
上面的错误消失了。很抱歉--我在复制和粘贴时忘记了那一行。我已经添加了它。最后一行出现了另一个错误,但是package
sos
发现太多可能的包,我无法猜出
map
功能需要哪一个包。再次道歉--我已经更正了这一点(忘记库已经在我的编辑器中加载到另一个脚本文件中)太棒了!非常感谢!
indiamap <- getCountries("IND",level=1)

xx <- indiamap
require(reshape)
xx@data$id <- rownames(xx@data)

# Convert to dataframe
xx.df <- as.data.frame(xx)
# xx.df$myid = xx$ID1*100 + xx$piece

#Fortfy automagic
require(ggplot2)
xx.fort <- fortify(xx, region="id")

# Join operation - one row per coordinate vector
require(plyr)
xx <- join(xx.fort, xx.df,by="id")
unique(xx$piece)

# generate unique id for each polygon piece
xx$myid <- xx$ID_1*1000 + as.numeric(xx$piece)

# Split by myid because we need to add NA at end of each set of polygon coordinates to 'break' the line
xxSp <- split(xx, xx$myid)

# Need to insert NA at end of each polygon shape to cut off that shape
# xxL <- do.call( rbind , (lapply( xxSp , function(x) { j <- x[ nrow(x) , ] ; j[1:2] <- c(NA,NA); rbind( x , j ) })) )
xxL <- do.call( rbind , (lapply( xxSp , function(x) { j <- x[nrow(x) , ] ; j[1:2] <- c(NA,NA); rbind( x , j ) })) )


# Create list object with same structure as map object
xxMap <- list( x = xxL$long , y = xxL$lat , range = c( range(xxL$long) , range(xxL$lat) ) , names = as.character(unique( xxL$NAME_1 ) ) )

# Define as a map class object
attr(xxMap , "class") <- "map"

map( xxMap , fill=T, col=2)