4.3 Import Shapefile data
The shapefile contains polygons delimiting the woods of the Montreal agglomeration and information about the forest composition (found here).
# Download shapefile from web page in your working directory
if (!file.exists("data/bois.dbf")) {
download.file("https://data.montreal.ca/dataset/29791562-f050-401e-b405-5c1fbf427f65/resource/9fa20d3a-5dee-43d6-9bc9-5d86fe225e16/download/bois.zip", destfile = "bois.zip")
unzip("bois.zip", exdir = "data")
unlink("bois.zip")
}
# Read shapefile in R
bois <- st_read(dsn = "data", layer = "bois")
#R> Reading layer `bois' from data source `/home/runner/work/rInSpace/rInSpace/data' using driver `ESRI Shapefile'
#R> Simple feature collection with 2716 features and 4 fields
#R> Geometry type: MULTIPOLYGON
#R> Dimension: XY
#R> Bounding box: xmin: -73.97557 ymin: 45.40957 xmax: -73.48204 ymax: 45.69943
#R> Geodetic CRS: WGS 84
#R> Simple feature collection with 6 features and 4 fields
#R> Geometry type: MULTIPOLYGON
#R> Dimension: XY
#R> Bounding box: xmin: -73.86056 ymin: 45.43293 xmax: -73.49655 ymax: 45.69584
#R> Geodetic CRS: WGS 84
#R> OBJECTID GR_ESSENC Shape_Leng Shape_Area geometry
#R> 1 2 Feuillus 515.4464 7199.995 MULTIPOLYGON (((-73.49961 4...
#R> 2 3 Feuillus 364.7666 8354.159 MULTIPOLYGON (((-73.51461 4...
#R> 3 5 Feuillus 134.2015 1139.293 MULTIPOLYGON (((-73.56527 4...
#R> 4 6 Feuillus 336.2568 4222.548 MULTIPOLYGON (((-73.4967 45...
#R> 5 8 Feuillus 510.9489 5168.669 MULTIPOLYGON (((-73.85932 4...
#R> 6 9 Feuillus 605.2992 10936.294 MULTIPOLYGON (((-73.85413 4...
The bois
dataset has been turned into a MULTIPOLYGON
object and has the same
CRS (EPSG: 4326) than the sample points we have manipulated above. This allows
us to work directly with the two objects otherwise we should have transformed
one dataset using the CRS of the other. To plot only the geometry and not all
attributes, we retrieve the geometry list-column using st_geometry()
:
To plot the polygons with a thematic color scale according to one attribute of interest, we actually subset the object (here we use the name of the column):