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.7.1, GDAL 2.4.0, PROJ 5.2.0
library(cshapes)
Loading required package: sp
Loading required package: maptools
Checking rgeos availability: TRUE
Loading required package: plyr
cshapes.1990 <- cshp(as.Date("1990-01-01"))
cshapes.1990 <- as(cshapes.1990,"sf")
Warning message:
“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 fields
geometry type: MULTIPOLYGON
dimension: XY
bbox: xmin: -180 ymin: -55.90223 xmax: 180 ymax: 83.11387
CRS: +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.07945 1...
3 Venezuela 916782.2172 Caracas 1 MULTIPOLYGON (((-66.31029 1...
4 Samoa 2955.2124 Apia 15 MULTIPOLYGON (((-172.5965 -...
5 Tonga 464.7473 Nuku'alofa 14 MULTIPOLYGON (((-175.1453 -...
6 Argentina 2787442.0977 Buenos Aires 1 MULTIPOLYGON (((-71.85916 -...
7 Bolivia 1092697.4356 La Paz 1 MULTIPOLYGON (((-62.19884 -...
8 Brazil 8523619.5715 Brasilia 21 MULTIPOLYGON (((-44.69501 -...
9 Chile 745808.4936 Santiago 1 MULTIPOLYGON (((-73.0421 -4...
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 fields
geometry type: POINT
dimension: XY
bbox: xmin: -175 ymin: -41.3 xmax: 179 ymax: 64.15
CRS: +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 message:
“plotting the first 9 out of 24 attributes; use max.plot = 24 to plot all”

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")
Downloadable R script and interactive version
- R Script: converting-sp-to-sf.R
- Interactive version (shuts down after 60s):
- Interactive version (sign in required):
Explanation
The link with the “jupyterhub” icon directs you to an interactive Jupyter1 notebook, which runs inside a Docker container2. There are two variants of the interative notebook. One shuts down after 60 seconds and does not require a sign it. The other requires sign in using your ORCID3 credentials, yet shuts down only after 24 hours. (There is no guarantee that such a container persists that long, it may be shut down earlier for maintenance purposes.) After shutdown all data within the container will be reset, i.e. all files created by the user will be deleted.4
Above you see a rendered version of the Jupyter notebook.5
- 1
-
For more information about Jupyter see
http://jupyter.org
. The Jupyter notebooks make use of the IRKernel package. - 2
-
For more information about Docker see
https://docs.docker.com/
. The container images were created with repo2docker, while containers are run with docker spawner. - 3
-
ORCID is a free service for the authentication of researchers. It also allows to showcase publications and contributions to the academic community such as peer review.. See
https://info.orcid.org/what-is-orcid/
for more information. - 4
-
The Jupyter notebooks come with NO WARRANTY whatsoever. They are provided for educational and illustrative purposes only. Do not use them for production work.
- 5
-
The notebook is rendered with the help of the nbsphinx extension.