Converting “sp” objects to “sf” objects

The following makes use of the sf, cshapes, and sp packages. You may need to install them from CRAN using the code install.packages(c("sf","cshapes","sp")) if you want to run this on your computer. (The package is already installed in the notebook container, however.)

library(sf)
Linking to GEOS 3.9.0, GDAL 3.2.2, PROJ 7.2.1; sf_use_s2() is TRUE
library(cshapes)
Loading required package: sp
Loading required package: maptools
Checking rgeos availability: TRUE
Please note that 'maptools' will be retired by the end of 2023,
plan transition at your earliest convenience;
some functionality will be moved to 'sp'.
Loading required package: plyr
cshapes.1990 <- cshp(as.Date("1990-01-01"))
cshapes.1990 <- as(cshapes.1990,"sf")
Warning:
readShapePoly is deprecated; use rgdal::readOGR or sf::st_read
options(width=200)
print(cshapes.1990[c(1:3,10)])
Simple feature collection with 171 features and 4 fieldsGeometry type: MULTIPOLYGON
Dimension:     XY
Bounding box:  xmin: -180 ymin: -55.90223 xmax: 180 ymax: 83.11387CRS:           +proj=longlat +ellps=WGS84
First 10 features:
           CNTRY_NAME         AREA       CAPNAME COWSDAY                       geometry
0              Guyana  211982.0050    Georgetown      26 MULTIPOLYGON (((-58.17262 6...
1            Suriname  145952.2740    Paramaribo      25 MULTIPOLYGON (((-55.12796 5...
2 Trinidad and Tobago    5041.7290 Port-of-Spain      31 MULTIPOLYGON (((-61.02542 1...
3           Venezuela  916782.2172       Caracas       1 MULTIPOLYGON (((-66.11835 1...
4               Samoa    2955.2124          Apia      15 MULTIPOLYGON (((-172.4753 -...
5               Tonga     464.7473    Nuku'alofa      14 MULTIPOLYGON (((-175.3145 -...
6           Argentina 2787442.0977  Buenos Aires       1 MULTIPOLYGON (((-71.8632 -4...
7             Bolivia 1092697.4356        La Paz       1 MULTIPOLYGON (((-62.19884 -...
8              Brazil 8523619.5715      Brasilia      21 MULTIPOLYGON (((-52.16862 -...
9               Chile  745808.4936      Santiago       1 MULTIPOLYGON (((-109.4092 -...
SthAmCntry.names <- c(
    "Argentina",
    "Bolivia",
    "Brazil",
    "Chile",
    "Colombia",
    "Ecuador",
    "Guyana",
    "Paraguay",
    "Peru",
    "Suriname",
    "Uruguay",
    "Venezuela")
SthAmCountries <-
    subset(cshapes.1990,
           CNTRY_NAME %in% SthAmCntry.names)
Brazil <- subset(cshapes.1990,CNTRY_NAME=="Brazil")
Chile <-  subset(cshapes.1990,CNTRY_NAME=="Chile")
Colombia <-  subset(cshapes.1990,CNTRY_NAME=="Colombia")
cap.latlong <- with(cshapes.1990,cbind(CAPLONG,CAPLAT))
cap.latlong <- lapply(1:nrow(cap.latlong),
                      function(i)cap.latlong[i,])
cap.latlong <- lapply(cap.latlong,st_point)
cap.latlong <- st_sfc(cap.latlong)
cshapes.capitals.1990 <- cshapes.1990
st_geometry(cshapes.capitals.1990) <- cap.latlong
st_crs(cshapes.capitals.1990) <- st_crs(cshapes.1990)
print(cshapes.capitals.1990[c(1:3,10)])
Simple feature collection with 171 features and 4 fieldsGeometry type: POINT
Dimension:     XY
Bounding box:  xmin: -175 ymin: -41.3 xmax: 179 ymax: 64.15CRS:           +proj=longlat +ellps=WGS84
First 10 features:
           CNTRY_NAME         AREA       CAPNAME COWSDAY               geometry
0              Guyana  211982.0050    Georgetown      26      POINT (-58.2 6.8)
1            Suriname  145952.2740    Paramaribo      25 POINT (-55.2 5.833333)
2 Trinidad and Tobago    5041.7290 Port-of-Spain      31    POINT (-61.5 10.65)
3           Venezuela  916782.2172       Caracas       1     POINT (-66.9 10.5)
4               Samoa    2955.2124          Apia      15     POINT (-172 -13.8)
5               Tonga     464.7473    Nuku'alofa      14     POINT (-175 -21.1)
6           Argentina 2787442.0977  Buenos Aires       1    POINT (-58.7 -34.6)
7             Bolivia 1092697.4356        La Paz       1    POINT (-68.2 -16.5)
8              Brazil 8523619.5715      Brasilia      21    POINT (-47.9 -15.8)
9               Chile  745808.4936      Santiago       1    POINT (-70.7 -33.5)
Brasilia <- subset(cshapes.capitals.1990,CNTRY_NAME=="Brazil")
Santiago <-  subset(cshapes.capitals.1990,CNTRY_NAME=="Chile")
Bogota <-  subset(cshapes.capitals.1990,CNTRY_NAME=="Colombia")
graypal <- function(n)gray.colors(n,start=.2,end=.9,alpha=.5)
plot(SthAmCountries,pal=graypal)
Warning:
plotting the first 9 out of 24 attributes; use max.plot = 24 to plot all
/book/data-management-r/08-spatial-geographical-data/converting-sp-to-sf/16963f5185642708a96a9915287e4bfe88b3a1331ffb3f0d5a3d78e592caa498.png
plot(st_geometry(SthAmCountries))
plot(st_geometry(Brazil))
save(cshapes.1990,cshapes.capitals.1990,file="cshapes-1990.RData")
save(Brazil,Chile,Colombia,
     Brasilia,Santiago,Bogota,
     SthAmCountries,
     file="south-america-1990.RData")