Importing shapefiles¶
options(width=100)
The data used here are available from Upsala Conflict Data Program.
# To download the data, use the *R*-code
#
# download.file("https://ucdp.uu.se/downloads/ged/ucdp-ged-poly-v-1-1-shape.zip",
# "ucdp-ged-poly-v-1-1-shape.zip")
unzip("ucdp-ged-poly-v-1-1-shape.zip",list=TRUE)
Name Length Date
1 ARCGIS Readme.txt 251 2012-03-28 16:07:00
2 UCDP GED Conflict Polygons Codebook version 1.1-2011.pdf 1006764 2012-03-28 16:02:00
3 UCDPGEDpoly.dbf 744174 2012-03-21 16:09:00
4 UCDPGEDpoly.prj 145 2012-03-21 16:09:00
5 UCDPGEDpoly.sbn 4820 2012-03-21 16:09:00
6 UCDPGEDpoly.sbx 372 2012-03-21 16:09:00
7 UCDPGEDpoly.shp 73892 2012-03-21 16:09:00
8 UCDPGEDpoly.shx 4100 2012-03-21 16:09:00
9 UCDPGEDpolyyear.dbf 1907966 2012-03-21 16:08:00
10 UCDPGEDpolyyear.prj 145 2012-03-21 16:08:00
11 UCDPGEDpolyyear.sbn 11620 2012-03-21 16:08:00
12 UCDPGEDpolyyear.sbx 764 2012-03-21 16:08:00
13 UCDPGEDpolyyear.shp 165460 2012-03-21 16:08:00
14 UCDPGEDpolyyear.shx 9156 2012-03-21 16:08:00
unzip("ucdp-ged-poly-v-1-1-shape.zip",
files=c(
"UCDPGEDpoly.shx",
"UCDPGEDpoly.shp",
"UCDPGEDpoly.dbf"
))
The following makes use of the sf package. You may need to install it from
CRAN using the code
install.packages("sf")
if you want to run this on your computer. (The
package is already installed on the notebook container, however.)
library(sf)
# All three of the following lines are equivalent:
UCDPGEDpoly <- st_read("UCDPGEDpoly.shx")
Linking to GEOS 3.9.0, GDAL 3.2.2, PROJ 7.2.1
Reading layer `UCDPGEDpoly' from data source `/home/elff/webdevel/sphinx/elff.eu/book/data-management-r/08-spatial-geographical-data/UCDPGEDpoly.shx' using driver `ESRI Shapefile'
Simple feature collection with 500 features and 20 fieldsgeometry type: POLYGON
dimension: XY
bbox: xmin: -17.3381 ymin: -34.1831 xmax: 50.8833 ymax: 37.0542CRS: NA
UCDPGEDpoly <- st_read("UCDPGEDpoly.shp")
Reading layer `UCDPGEDpoly' from data source `/home/elff/webdevel/sphinx/elff.eu/book/data-management-r/08-spatial-geographical-data/UCDPGEDpoly.shp' using driver `ESRI Shapefile'
Simple feature collection with 500 features and 20 fieldsgeometry type: POLYGON
dimension: XY
bbox: xmin: -17.3381 ymin: -34.1831 xmax: 50.8833 ymax: 37.0542CRS: NA
UCDPGEDpoly <- st_read("UCDPGEDpoly.dbf")
Reading layer `UCDPGEDpoly' from data source `/home/elff/webdevel/sphinx/elff.eu/book/data-management-r/08-spatial-geographical-data/UCDPGEDpoly.dbf' using driver `ESRI Shapefile'
Simple feature collection with 500 features and 20 fieldsgeometry type: POLYGON
dimension: XY
bbox: xmin: -17.3381 ymin: -34.1831 xmax: 50.8833 ymax: 37.0542CRS: NA
str(UCDPGEDpoly)
Classes 'sf' and 'data.frame': 500 obs. of 21 variables:
$ pName : chr "LNE-1-17-A" "LNE-1-189A-A" "LNE-1-19-A" "LNE-1-191-A" ...
$ lat : num 4.32 -4.31 12.72 -5.63 15.5 ...
$ lon : num 18.6 15.3 20.5 12.5 22.9 ...
$ type_of_vi: num 1 1 1 1 1 1 1 1 1 1 ...
$ dyadID_1 : num 17 0 19 191 20 210 23 0 0 811 ...
$ Split_dyad: num 0 1 0 0 0 0 0 1 1 0 ...
$ dyad_name : chr "Government of Central African Republic - Military faction (Forces of Andr<U+00E9> Kolingba)" "Government of the Republic of Congo - Cobras" "Government of Chad - MOSANAT" "Government of Angola - FLEC-R" ...
$ conflict_n: chr "Central African Republic" "Congo" "Chad" "Angola (Cabinda)" ...
$ conflict_I: num 222 214 91 192 91 141 91 191 177 268 ...
$ side_A : chr "Government of Central African Republic" "Government of the Republic of Congo" "Government of Chad" "Government of Angola" ...
$ side_A_ID : num 482 484 483 540 483 520 483 615 432 530 ...
$ side_B : chr "Military faction (Forces of Andr<U+00E9> Kolingba)" "Cobras" "MOSANAT" "FLEC-R" ...
$ side_B_ID : num 1406 1398 1290 1392 1288 ...
$ no_of_even: num 11 23 2 2 3 11 3 2 2 2 ...
$ low_est : num 231 1396 50 0 0 ...
$ best_est : num 231 1396 50 0 0 ...
$ high_est : num 331 3456 50 72 930 ...
$ date_first: Date, format: "2001-05-27" "1997-06-05" "1989-03-03" ...
$ date_fir_1: Date, format: "2001-06-01" "1997-06-06" "1989-03-03" ...
$ date_last : Date, format: "2001-12-01" "1997-10-16" "1989-03-03" ...
$ geometry :sfc_POLYGON of length 500; first list element: List of 1
..$ : num [1:5, 1:2] 18.6 18.6 18.6 18.6 18.6 ...
..- attr(*, "class")= chr [1:3] "XY" "POLYGON" "sfg"
- attr(*, "sf_column")= chr "geometry"
- attr(*, "agr")= Factor w/ 3 levels "constant","aggregate",..: NA NA NA NA NA NA NA NA NA NA ...
..- attr(*, "names")= chr [1:20] "pName" "lat" "lon" "type_of_vi" ...
print(UCDPGEDpoly[c(7,18,20)])
Simple feature collection with 500 features and 3 fieldsgeometry type: POLYGON
dimension: XY
bbox: xmin: -17.3381 ymin: -34.1831 xmax: 50.8833 ymax: 37.0542CRS: NA
First 10 features:
dyad_name
1 Government of Central African Republic - Military faction (Forces of Andr<U+00E9> Kolingba)
2 Government of the Republic of Congo - Cobras
3 Government of Chad - MOSANAT
4 Government of Angola - FLEC-R
5 Government of Chad - Islamic Legion
6 Government of Somalia - USC
7 Government of Chad - MPS
8 Government of Algeria - Takfir wa'l Hijra
9 Government of Mali - ATNMC
10 Government of Ethiopia - IGLF
date_first date_last geometry
1 2001-05-27 2001-12-01 POLYGON ((18.5833 4.31667, ...
2 1997-06-05 1997-10-16 POLYGON ((15.315 -4.37972, ...
3 1989-03-03 1989-03-03 POLYGON ((20.9052 9.88969, ...
4 1994-02-07 1994-11-15 POLYGON ((12.5167 -5.63333,...
5 1990-04-03 1990-11-16 POLYGON ((22.7833 14.9667, ...
6 1990-11-18 1991-01-28 POLYGON ((45.3667 2.01667, ...
7 1990-11-14 1990-11-25 POLYGON ((22.7833 14.9667, ...
8 1991-11-29 1991-12-09 POLYGON ((6.88333 33.2833, ...
9 2008-05-06 2008-12-20 POLYGON ((-6.0197 14.6437, ...
10 1991-10-10 1991-10-10 POLYGON ((42.1258 9.25944, ...
- R file: importing-shapefiles.R
- Rmarkdown file: importing-shapefiles.Rmd
- Jupyter notebook file: importing-shapefiles.ipynb
- Interactive version of the Jupyter notebook (shuts down after 60s):
- Interactive version of the Jupyter notebook (sign in required):