Search code examples
rspatialrgdal

Creating a world shape file with rdal package


I am trying to create a world shape file similar to London one. I am using this shape file. Link to the shape file. I have downloaded this file and I issue the command lnd@countries. I am getting the following error. Any suggestions?

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"

Solution

  • You are mistaking the syntax... An OGR data object will have the data stored in myobject@data, and the coordinates in 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)
    

    Note that there is no field named "countries", which is what you are asking for in your question:

     [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"  
    

    To get the first four countries by name, noting that this depends on the order of features not the alphabetic order (which happens to match here):

    head(data@data$NAME, 4)
    

    Note that they were clever enough to include a NAME_SORT field for proper ordering (in English, presumably).