使用 rgdal 包创建世界形状文件

Creating a world shape file with rdal package

我正在尝试创建一个类似于伦敦的世界形状文件。我正在使用这个形状文件。 Link to the shape file。我已下载此文件并发出命令 lnd@countries。我收到以下错误。有什么建议吗?

library(rgdal)
lnd <- readOGR(dsn = "countries", layer = "ne_10m_admin_0_countries")
head(lnd@countries, n = 4)

 Error in head(lnd@countries) : 
   no slot of name "countries" for this object of class "SpatialPolygonsDataFrame"

您弄错了语法...OGR 数据对象会将数据存储在 myobject@data 中,并将坐标存储在 myobject@coords 中。

data = readOGR("/Users/Benjamin/Desktop/ne_10m_admin_0_countries/ne_10m_admin_0_countries.shp", "ne_10m_admin_0_countries")
colnames(data@data)

请注意,没有名为 "countries" 的字段,这是您在问题中要求的:

 [1] "scalerank"  "featurecla" "LABELRANK"  "SOVEREIGNT" "SOV_A3"     "ADM0_DIF"   "LEVEL"      "TYPE"       "ADMIN"      "ADM0_A3"    "GEOU_DIF"  
[12] "GEOUNIT"    "GU_A3"      "SU_DIF"     "SUBUNIT"    "SU_A3"      "BRK_DIFF"   "NAME"       "NAME_LONG"  "BRK_A3"     "BRK_NAME"   "BRK_GROUP" 
[23] "ABBREV"     "POSTAL"     "FORMAL_EN"  "FORMAL_FR"  "NOTE_ADM0"  "NOTE_BRK"   "NAME_SORT"  "NAME_ALT"   "MAPCOLOR7"  "MAPCOLOR8"  "MAPCOLOR9" 
[34] "MAPCOLOR13" "POP_EST"    "GDP_MD_EST" "POP_YEAR"   "LASTCENSUS" "GDP_YEAR"   "ECONOMY"    "INCOME_GRP" "WIKIPEDIA"  "FIPS_10_"   "ISO_A2"    
[45] "ISO_A3"     "ISO_N3"     "UN_A3"      "WB_A2"      "WB_A3"      "WOE_ID"     "WOE_ID_EH"  "WOE_NOTE"   "ADM0_A3_IS" "ADM0_A3_US" "ADM0_A3_UN"
[56] "ADM0_A3_WB" "CONTINENT"  "REGION_UN"  "SUBREGION"  "REGION_WB"  "NAME_LEN"   "LONG_LEN"   "ABBREV_LEN" "TINY"       "HOMEPART"  

按名称获取前四个国家,注意这取决于特征的顺序而不是字母顺序(恰好与此处匹配):

head(data@data$NAME, 4)

请注意,他们足够聪明,包括一个 NAME_SORT 字段用于正确排序(大概是英文)。