Skip to content

Instantly share code, notes, and snippets.

@andybega
Last active November 24, 2020 18:27
Show Gist options
  • Save andybega/42f2e7b7137c9d1f22659b1761a9edbf to your computer and use it in GitHub Desktop.
Save andybega/42f2e7b7137c9d1f22659b1761a9edbf to your computer and use it in GitHub Desktop.
Example of how to add countries (polygons) to cshapes
# Example of how to add missing countries/territories to cshapes using another source for
# the map data/polygon
#
library(cshapes)
library(rgdal) # need this for projection transform (spTransform)
# this returns all polygons, with start and end dates for indepdence
# we'll have to manually subset for dates below
all <- cshp()
head(all@data$CNTRY_NAME)
# Just using this as an example (data could come from somewhere else too)
library(rnaturalearth)
pick <- ne_countries(scale = "small")
# All current countries in the world
head(pick@data)
# This data doesn't match the projection in cshapes; need to reproject
proj4string(all)
proj4string(pick)
pick <- spTransform(pick, proj4string(all))
# Let's pick one polygon we want to merge into cshapes
# to find, just figure out the correct corresponding row in pick@data
pick@data$admin
# ok, "Greenland"'s in there
greenland <- pick[pick@data$admin=="Greenland", ]
# this is just greenland now
plot(greenland)
# to merge this in "all", will need to match the data columsn in all@data
# make an empty template for this (with NA values but correct column data
# types)
template <- all@data[1, ]
template[1, 1:ncol(template)] <- NA
# could fill in more values here as needed
greenland@data <- template
greenland@data$CNTRY_NAME <- "Greenland"
greenland@data$ISO1AL3 <- "GRL"
greenland@data$COWEYEAR <- 2016
greenland@data$COWSYEAR <- 1946
# add this to the cshapes map data
all <- rbind(all, greenland)
# now we can do the subsetting by date, otherwise we will have some overlapping
# nonsensical polygons (e.g. East, West, and unified Germany all there)
map <- all[all@data$COWEYEAR==2016, ]
plot(map)
# Example output is in the file below
library(cshapes)
#> Loading required package: sp
#> Loading required package: maptools
#> Checking rgeos availability: TRUE
#> Loading required package: plyr
library(rgdal)  # need this for projection transform (spTransform)
#> rgdal: version: 1.5-18, (SVN revision 1082)
#> Geospatial Data Abstraction Library extensions to R successfully loaded
#> Loaded GDAL runtime: GDAL 3.1.1, released 2020/06/22
#> Path to GDAL shared files: /Library/Frameworks/R.framework/Versions/4.0/Resources/library/rgdal/gdal
#> GDAL binary built with GEOS: TRUE 
#> Loaded PROJ runtime: Rel. 6.3.1, February 10th, 2020, [PJ_VERSION: 631]
#> Path to PROJ shared files: /Library/Frameworks/R.framework/Versions/4.0/Resources/library/rgdal/proj
#> Linking to sp version:1.4-4
#> To mute warnings of possible GDAL/OSR exportToProj4() degradation,
#> use options("rgdal_show_exportToProj4_warnings"="none") before loading rgdal.

# this returns all polygons, with start and end dates for indepdence
# we'll have to manually subset for dates below
all <- cshp()
#> Warning: readShapePoly is deprecated; use rgdal::readOGR or sf::st_read
#> Warning in showSRID(uprojargs, format = "PROJ", multiline = "NO", prefer_proj =
#> prefer_proj): Discarded datum Unknown based on WGS84 ellipsoid in CRS definition
head(all@data$CNTRY_NAME)
#> [1] Guyana              Suriname            Trinidad and Tobago
#> [4] Venezuela           Samoa               Tonga              
#> 207 Levels: Afghanistan Albania Algeria Andorra Angola ... Zimbabwe

# Just using this as an example (data could come from somewhere else too)
library(rnaturalearth)
pick <- ne_countries(scale = "small")

# All current countries in the world
head(pick@data)
#>   scalerank      featurecla labelrank           sovereignt sov_a3 adm0_dif
#> 0         1 Admin-0 country         3          Afghanistan    AFG        0
#> 1         1 Admin-0 country         3               Angola    AGO        0
#> 2         1 Admin-0 country         6              Albania    ALB        0
#> 3         1 Admin-0 country         4 United Arab Emirates    ARE        0
#> 4         1 Admin-0 country         2            Argentina    ARG        0
#> 5         1 Admin-0 country         6              Armenia    ARM        0
#>   level              type                admin adm0_a3 geou_dif
#> 0     2 Sovereign country          Afghanistan     AFG        0
#> 1     2 Sovereign country               Angola     AGO        0
#> 2     2 Sovereign country              Albania     ALB        0
#> 3     2 Sovereign country United Arab Emirates     ARE        0
#> 4     2 Sovereign country            Argentina     ARG        0
#> 5     2 Sovereign country              Armenia     ARM        0
#>                geounit gu_a3 su_dif              subunit su_a3 brk_diff
#> 0          Afghanistan   AFG      0          Afghanistan   AFG        0
#> 1               Angola   AGO      0               Angola   AGO        0
#> 2              Albania   ALB      0              Albania   ALB        0
#> 3 United Arab Emirates   ARE      0 United Arab Emirates   ARE        0
#> 4            Argentina   ARG      0            Argentina   ARG        0
#> 5              Armenia   ARM      0              Armenia   ARM        0
#>                   name            name_long brk_a3             brk_name
#> 0          Afghanistan          Afghanistan    AFG          Afghanistan
#> 1               Angola               Angola    AGO               Angola
#> 2              Albania              Albania    ALB              Albania
#> 3 United Arab Emirates United Arab Emirates    ARE United Arab Emirates
#> 4            Argentina            Argentina    ARG            Argentina
#> 5              Armenia              Armenia    ARM              Armenia
#>   brk_group abbrev postal                    formal_en formal_fr note_adm0
#> 0      <NA>   Afg.     AF Islamic State of Afghanistan      <NA>      <NA>
#> 1      <NA>   Ang.     AO  People's Republic of Angola      <NA>      <NA>
#> 2      <NA>   Alb.     AL          Republic of Albania      <NA>      <NA>
#> 3      <NA> U.A.E.     AE         United Arab Emirates      <NA>      <NA>
#> 4      <NA>   Arg.     AR           Argentine Republic      <NA>      <NA>
#> 5      <NA>   Arm.    ARM          Republic of Armenia      <NA>      <NA>
#>   note_brk            name_sort name_alt mapcolor7 mapcolor8 mapcolor9
#> 0     <NA>          Afghanistan     <NA>         5         6         8
#> 1     <NA>               Angola     <NA>         3         2         6
#> 2     <NA>              Albania     <NA>         1         4         1
#> 3     <NA> United Arab Emirates     <NA>         2         1         3
#> 4     <NA>            Argentina     <NA>         3         1         3
#> 5     <NA>              Armenia     <NA>         3         1         2
#>   mapcolor13  pop_est gdp_md_est pop_year lastcensus gdp_year
#> 0          7 28400000      22270       NA       1979       NA
#> 1          1 12799293     110300       NA       1970       NA
#> 2          6  3639453      21810       NA       2001       NA
#> 3          3  4798491     184300       NA       2010       NA
#> 4         13 40913584     573900       NA       2010       NA
#> 5         10  2967004      18770       NA       2001       NA
#>                     economy              income_grp wikipedia fips_10 iso_a2
#> 0 7. Least developed region           5. Low income        NA    <NA>     AF
#> 1 7. Least developed region  3. Upper middle income        NA    <NA>     AO
#> 2      6. Developing region  4. Lower middle income        NA    <NA>     AL
#> 3      6. Developing region 2. High income: nonOECD        NA    <NA>     AE
#> 4   5. Emerging region: G20  3. Upper middle income        NA    <NA>     AR
#> 5      6. Developing region  4. Lower middle income        NA    <NA>     AM
#>   iso_a3 iso_n3 un_a3 wb_a2 wb_a3 woe_id adm0_a3_is adm0_a3_us adm0_a3_un
#> 0    AFG    004   004    AF   AFG     NA        AFG        AFG         NA
#> 1    AGO    024   024    AO   AGO     NA        AGO        AGO         NA
#> 2    ALB    008   008    AL   ALB     NA        ALB        ALB         NA
#> 3    ARE    784   784    AE   ARE     NA        ARE        ARE         NA
#> 4    ARG    032   032    AR   ARG     NA        ARG        ARG         NA
#> 5    ARM    051   051    AM   ARM     NA        ARM        ARM         NA
#>   adm0_a3_wb     continent region_un       subregion                  region_wb
#> 0         NA          Asia      Asia   Southern Asia                 South Asia
#> 1         NA        Africa    Africa   Middle Africa         Sub-Saharan Africa
#> 2         NA        Europe    Europe Southern Europe      Europe & Central Asia
#> 3         NA          Asia      Asia    Western Asia Middle East & North Africa
#> 4         NA South America  Americas   South America  Latin America & Caribbean
#> 5         NA          Asia      Asia    Western Asia      Europe & Central Asia
#>   name_len long_len abbrev_len tiny homepart
#> 0       11       11          4   NA        1
#> 1        6        6          4   NA        1
#> 2        7        7          4   NA        1
#> 3       20       20          6   NA        1
#> 4        9        9          4   NA        1
#> 5        7        7          4   NA        1

# This data doesn't match the projection in cshapes; need to reproject
proj4string(all)
#> Warning in proj4string(all): CRS object has comment, which is lost in output
#> [1] "+proj=longlat +ellps=WGS84 +no_defs"
proj4string(pick)
#> [1] "+proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0"
pick <- spTransform(pick, proj4string(all))
#> Warning in proj4string(all): CRS object has comment, which is lost in output

#> Warning in proj4string(all): Discarded datum Unknown based on WGS84 ellipsoid in
#> CRS definition
#> Warning in spTransform(xSP, CRSobj, ...): NULL source CRS comment, falling back
#> to PROJ string
#> Warning in wkt(obj): CRS object has no comment

# Let's pick one polygon we want to merge into cshapes
# to find, just figure out the correct corresponding row in pick@data
pick@data$admin
#>   [1] "Afghanistan"                        
#>   [2] "Angola"                             
#>   [3] "Albania"                            
#>   [4] "United Arab Emirates"               
#>   [5] "Argentina"                          
#>   [6] "Armenia"                            
#>   [7] "Antarctica"                         
#>   [8] "French Southern and Antarctic Lands"
#>   [9] "Australia"                          
#>  [10] "Austria"                            
#>  [11] "Azerbaijan"                         
#>  [12] "Burundi"                            
#>  [13] "Belgium"                            
#>  [14] "Benin"                              
#>  [15] "Burkina Faso"                       
#>  [16] "Bangladesh"                         
#>  [17] "Bulgaria"                           
#>  [18] "The Bahamas"                        
#>  [19] "Bosnia and Herzegovina"             
#>  [20] "Belarus"                            
#>  [21] "Belize"                             
#>  [22] "Bolivia"                            
#>  [23] "Brazil"                             
#>  [24] "Brunei"                             
#>  [25] "Bhutan"                             
#>  [26] "Botswana"                           
#>  [27] "Central African Republic"           
#>  [28] "Canada"                             
#>  [29] "Switzerland"                        
#>  [30] "Chile"                              
#>  [31] "China"                              
#>  [32] "Ivory Coast"                        
#>  [33] "Cameroon"                           
#>  [34] "Democratic Republic of the Congo"   
#>  [35] "Republic of Congo"                  
#>  [36] "Colombia"                           
#>  [37] "Costa Rica"                         
#>  [38] "Cuba"                               
#>  [39] "Northern Cyprus"                    
#>  [40] "Cyprus"                             
#>  [41] "Czech Republic"                     
#>  [42] "Germany"                            
#>  [43] "Djibouti"                           
#>  [44] "Denmark"                            
#>  [45] "Dominican Republic"                 
#>  [46] "Algeria"                            
#>  [47] "Ecuador"                            
#>  [48] "Egypt"                              
#>  [49] "Eritrea"                            
#>  [50] "Spain"                              
#>  [51] "Estonia"                            
#>  [52] "Ethiopia"                           
#>  [53] "Finland"                            
#>  [54] "Fiji"                               
#>  [55] "Falkland Islands"                   
#>  [56] "France"                             
#>  [57] "Gabon"                              
#>  [58] "United Kingdom"                     
#>  [59] "Georgia"                            
#>  [60] "Ghana"                              
#>  [61] "Guinea"                             
#>  [62] "Gambia"                             
#>  [63] "Guinea Bissau"                      
#>  [64] "Equatorial Guinea"                  
#>  [65] "Greece"                             
#>  [66] "Greenland"                          
#>  [67] "Guatemala"                          
#>  [68] "Guyana"                             
#>  [69] "Honduras"                           
#>  [70] "Croatia"                            
#>  [71] "Haiti"                              
#>  [72] "Hungary"                            
#>  [73] "Indonesia"                          
#>  [74] "India"                              
#>  [75] "Ireland"                            
#>  [76] "Iran"                               
#>  [77] "Iraq"                               
#>  [78] "Iceland"                            
#>  [79] "Israel"                             
#>  [80] "Italy"                              
#>  [81] "Jamaica"                            
#>  [82] "Jordan"                             
#>  [83] "Japan"                              
#>  [84] "Kazakhstan"                         
#>  [85] "Kenya"                              
#>  [86] "Kyrgyzstan"                         
#>  [87] "Cambodia"                           
#>  [88] "South Korea"                        
#>  [89] "Kosovo"                             
#>  [90] "Kuwait"                             
#>  [91] "Laos"                               
#>  [92] "Lebanon"                            
#>  [93] "Liberia"                            
#>  [94] "Libya"                              
#>  [95] "Sri Lanka"                          
#>  [96] "Lesotho"                            
#>  [97] "Lithuania"                          
#>  [98] "Luxembourg"                         
#>  [99] "Latvia"                             
#> [100] "Morocco"                            
#> [101] "Moldova"                            
#> [102] "Madagascar"                         
#> [103] "Mexico"                             
#> [104] "Macedonia"                          
#> [105] "Mali"                               
#> [106] "Myanmar"                            
#> [107] "Montenegro"                         
#> [108] "Mongolia"                           
#> [109] "Mozambique"                         
#> [110] "Mauritania"                         
#> [111] "Malawi"                             
#> [112] "Malaysia"                           
#> [113] "Namibia"                            
#> [114] "New Caledonia"                      
#> [115] "Niger"                              
#> [116] "Nigeria"                            
#> [117] "Nicaragua"                          
#> [118] "Netherlands"                        
#> [119] "Norway"                             
#> [120] "Nepal"                              
#> [121] "New Zealand"                        
#> [122] "Oman"                               
#> [123] "Pakistan"                           
#> [124] "Panama"                             
#> [125] "Peru"                               
#> [126] "Philippines"                        
#> [127] "Papua New Guinea"                   
#> [128] "Poland"                             
#> [129] "Puerto Rico"                        
#> [130] "North Korea"                        
#> [131] "Portugal"                           
#> [132] "Paraguay"                           
#> [133] "Palestine"                          
#> [134] "Qatar"                              
#> [135] "Romania"                            
#> [136] "Russia"                             
#> [137] "Rwanda"                             
#> [138] "Western Sahara"                     
#> [139] "Saudi Arabia"                       
#> [140] "Sudan"                              
#> [141] "South Sudan"                        
#> [142] "Senegal"                            
#> [143] "Solomon Islands"                    
#> [144] "Sierra Leone"                       
#> [145] "El Salvador"                        
#> [146] "Somaliland"                         
#> [147] "Somalia"                            
#> [148] "Republic of Serbia"                 
#> [149] "Suriname"                           
#> [150] "Slovakia"                           
#> [151] "Slovenia"                           
#> [152] "Sweden"                             
#> [153] "Swaziland"                          
#> [154] "Syria"                              
#> [155] "Chad"                               
#> [156] "Togo"                               
#> [157] "Thailand"                           
#> [158] "Tajikistan"                         
#> [159] "Turkmenistan"                       
#> [160] "East Timor"                         
#> [161] "Trinidad and Tobago"                
#> [162] "Tunisia"                            
#> [163] "Turkey"                             
#> [164] "Taiwan"                             
#> [165] "United Republic of Tanzania"        
#> [166] "Uganda"                             
#> [167] "Ukraine"                            
#> [168] "Uruguay"                            
#> [169] "United States of America"           
#> [170] "Uzbekistan"                         
#> [171] "Venezuela"                          
#> [172] "Vietnam"                            
#> [173] "Vanuatu"                            
#> [174] "Yemen"                              
#> [175] "South Africa"                       
#> [176] "Zambia"                             
#> [177] "Zimbabwe"
# ok, "Greenland"'s in there
greenland <- pick[pick@data$admin=="Greenland", ]

# this is just greenland now
plot(greenland)

# to merge this in "all", will need to match the data columsn in all@data
# make an empty template for this (with NA values but correct column data
# types)
template <- all@data[1, ]
template[1, 1:ncol(template)] <- NA

# could fill in more values here as needed
greenland@data <- template
greenland@data$CNTRY_NAME <- "Greenland"
greenland@data$ISO1AL3  <- "GRL"
greenland@data$COWEYEAR <- 2016
greenland@data$COWSYEAR <- 1946

# add this to the cshapes map data
all <- rbind(all, greenland)

# now we can do the subsetting by date, otherwise we will have some overlapping
# nonsensical polygons (e.g. East, West, and unified Germany all there)
map <- all[all@data$COWEYEAR==2016, ]

plot(map)

Created on 2020-11-24 by the reprex package (v0.3.0)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment