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

时间:2015-07-23 18:42:59

标签: r spatial rgdal

我正在尝试创建一个类似伦敦的世界形状文件。我正在使用这个形状文件。 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"

1 个答案:

答案 0 :(得分:0)

您误解了语法... 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字段以便正确排序(大概是英文)。