In this hands-on exercise, you will learn how to handle geospatial data in R by using appropriate R packages.
By the end of this hands-on exercise, you should aquire the following competencies:
Before you can start using R, you are required to extract the necessary data sets from the appropriate source
Before we get started, it is important for us to ensure that all the R packages we need have been installed.
The code chunk:
packages = c('sp', 'rgdal', 'rgeos', 'sf', 'tidyverse')
for (p in packages){
if(!require(p, character.only = T)){
install.packages(p)
}
library(p,character.only = T)
}
In this hands-on exercise, you are reuiqred to import the follwing geospatial data into R:
The code chunk below uses st_read() function of sf package to import MP14_SUBZONE_WEB_PL data into R as simple feature dataframes.
sf_mpsz = st_read(dsn = "data/geospatial",
layer = "MP14_SUBZONE_WEB_PL")
## Reading layer `MP14_SUBZONE_WEB_PL' from data source `D:\IS415-AY2020-21T1\03-Hands-on Exercises\Hands-on_Ex02\data\geospatial' using driver `ESRI Shapefile'
## Simple feature collection with 323 features and 15 fields
## geometry type: MULTIPOLYGON
## dimension: XY
## bbox: xmin: 2667.538 ymin: 15748.72 xmax: 56396.44 ymax: 50256.33
## projected CRS: SVY21
The code chunk below uses st_read() function of sf package to import the CyclingPath layer into R as simple feature dataframes.
sf_cyclingpath = st_read(dsn = "data/geospatial",
layer = "CyclingPath")
## Reading layer `CyclingPath' from data source `D:\IS415-AY2020-21T1\03-Hands-on Exercises\Hands-on_Ex02\data\geospatial' using driver `ESRI Shapefile'
## Simple feature collection with 1625 features and 2 fields
## geometry type: LINESTRING
## dimension: XY
## bbox: xmin: 12711.19 ymin: 28711.33 xmax: 42626.09 ymax: 48948.15
## projected CRS: SVY21
sf_preschool = st_read("data/geospatial/pre-schools-location-kml.kml")
## Reading layer `PRESCHOOLS_LOCATION' from data source `D:\IS415-AY2020-21T1\03-Hands-on Exercises\Hands-on_Ex02\data\geospatial\pre-schools-location-kml.kml' using driver `KML'
## Simple feature collection with 1359 features and 2 fields
## geometry type: POINT
## dimension: XYZ
## bbox: xmin: 103.6824 ymin: 1.248403 xmax: 103.9897 ymax: 1.462134
## z_range: zmin: 0 zmax: 0
## geographic CRS: WGS 84
You can check the contents of mpsz_rg data object by using summary.
The code chunk:
summary(sf_mpsz)
## OBJECTID SUBZONE_NO SUBZONE_N SUBZONE_C
## Min. : 1.0 Min. : 1.000 Length:323 Length:323
## 1st Qu.: 81.5 1st Qu.: 2.000 Class :character Class :character
## Median :162.0 Median : 4.000 Mode :character Mode :character
## Mean :162.0 Mean : 4.625
## 3rd Qu.:242.5 3rd Qu.: 6.500
## Max. :323.0 Max. :17.000
## CA_IND PLN_AREA_N PLN_AREA_C REGION_N
## Length:323 Length:323 Length:323 Length:323
## Class :character Class :character Class :character Class :character
## Mode :character Mode :character Mode :character Mode :character
##
##
##
## REGION_C INC_CRC FMEL_UPD_D X_ADDR
## Length:323 Length:323 Min. :2014-12-05 Min. : 5093
## Class :character Class :character 1st Qu.:2014-12-05 1st Qu.:21864
## Mode :character Mode :character Median :2014-12-05 Median :28465
## Mean :2014-12-05 Mean :27257
## 3rd Qu.:2014-12-05 3rd Qu.:31674
## Max. :2014-12-05 Max. :50425
## Y_ADDR SHAPE_Leng SHAPE_Area geometry
## Min. :19579 Min. : 871.5 Min. : 39438 MULTIPOLYGON :323
## 1st Qu.:31776 1st Qu.: 3709.6 1st Qu.: 628261 epsg:NA : 0
## Median :35113 Median : 5211.9 Median : 1229894 +proj=tmer...: 0
## Mean :36106 Mean : 6524.4 Mean : 2420882
## 3rd Qu.:39869 3rd Qu.: 6942.6 3rd Qu.: 2106483
## Max. :49553 Max. :68083.9 Max. :69748299
Let’s view the first few records in the spatial data object.
The code chunk
head(sf_mpsz, n=4)
## Simple feature collection with 4 features and 15 fields
## geometry type: MULTIPOLYGON
## dimension: XY
## bbox: xmin: 26403.48 ymin: 28369.47 xmax: 32362.39 ymax: 30396.46
## projected CRS: SVY21
## OBJECTID SUBZONE_NO SUBZONE_N SUBZONE_C CA_IND PLN_AREA_N
## 1 1 1 MARINA SOUTH MSSZ01 Y MARINA SOUTH
## 2 2 1 PEARL'S HILL OTSZ01 Y OUTRAM
## 3 3 3 BOAT QUAY SRSZ03 Y SINGAPORE RIVER
## 4 4 8 HENDERSON HILL BMSZ08 N BUKIT MERAH
## PLN_AREA_C REGION_N REGION_C INC_CRC FMEL_UPD_D X_ADDR
## 1 MS CENTRAL REGION CR 5ED7EB253F99252E 2014-12-05 31595.84
## 2 OT CENTRAL REGION CR 8C7149B9EB32EEFC 2014-12-05 28679.06
## 3 SR CENTRAL REGION CR C35FEFF02B13E0E5 2014-12-05 29654.96
## 4 BM CENTRAL REGION CR 3775D82C5DDBEFBD 2014-12-05 26782.83
## Y_ADDR SHAPE_Leng SHAPE_Area geometry
## 1 29220.19 5267.381 1630379.3 MULTIPOLYGON (((31495.56 30...
## 2 29782.05 3506.107 559816.2 MULTIPOLYGON (((29092.28 30...
## 3 29974.66 1740.926 160807.5 MULTIPOLYGON (((29932.33 29...
## 4 29933.77 3313.625 595428.9 MULTIPOLYGON (((27131.28 30...
To view the spatial data, plot function can be used.
The code chunk:
plot(sf_mpsz)
Task 2: Assigning EPSG code to sf_mpsz simple feature dataframe.
The solution:
First, checking the projection of sf_mpsxz
st_crs(sf_mpsz)
## Coordinate Reference System:
## User input: SVY21
## wkt:
## PROJCRS["SVY21",
## BASEGEOGCRS["SVY21[WGS84]",
## DATUM["World Geodetic System 1984",
## ELLIPSOID["WGS 84",6378137,298.257223563,
## LENGTHUNIT["metre",1]],
## ID["EPSG",6326]],
## PRIMEM["Greenwich",0,
## ANGLEUNIT["Degree",0.0174532925199433]]],
## CONVERSION["unnamed",
## METHOD["Transverse Mercator",
## ID["EPSG",9807]],
## PARAMETER["Latitude of natural origin",1.36666666666667,
## ANGLEUNIT["Degree",0.0174532925199433],
## ID["EPSG",8801]],
## PARAMETER["Longitude of natural origin",103.833333333333,
## ANGLEUNIT["Degree",0.0174532925199433],
## ID["EPSG",8802]],
## PARAMETER["Scale factor at natural origin",1,
## SCALEUNIT["unity",1],
## ID["EPSG",8805]],
## PARAMETER["False easting",28001.642,
## LENGTHUNIT["metre",1],
## ID["EPSG",8806]],
## PARAMETER["False northing",38744.572,
## LENGTHUNIT["metre",1],
## ID["EPSG",8807]]],
## CS[Cartesian,2],
## AXIS["(E)",east,
## ORDER[1],
## LENGTHUNIT["metre",1,
## ID["EPSG",9001]]],
## AXIS["(N)",north,
## ORDER[2],
## LENGTHUNIT["metre",1,
## ID["EPSG",9001]]]]
Next, assigning EPSG 3414 to sf_mpsz simple feature dataframe.
sf_mpsz3414 <- st_set_crs(sf_mpsz, 3414)
Lets check the CSR again.
st_crs(sf_mpsz3414)
## Coordinate Reference System:
## User input: EPSG:3414
## wkt:
## PROJCRS["SVY21 / Singapore TM",
## BASEGEOGCRS["SVY21",
## DATUM["SVY21",
## ELLIPSOID["WGS 84",6378137,298.257223563,
## LENGTHUNIT["metre",1]]],
## PRIMEM["Greenwich",0,
## ANGLEUNIT["degree",0.0174532925199433]],
## ID["EPSG",4757]],
## CONVERSION["Singapore Transverse Mercator",
## METHOD["Transverse Mercator",
## ID["EPSG",9807]],
## PARAMETER["Latitude of natural origin",1.36666666666667,
## ANGLEUNIT["degree",0.0174532925199433],
## ID["EPSG",8801]],
## PARAMETER["Longitude of natural origin",103.833333333333,
## ANGLEUNIT["degree",0.0174532925199433],
## ID["EPSG",8802]],
## PARAMETER["Scale factor at natural origin",1,
## SCALEUNIT["unity",1],
## ID["EPSG",8805]],
## PARAMETER["False easting",28001.642,
## LENGTHUNIT["metre",1],
## ID["EPSG",8806]],
## PARAMETER["False northing",38744.572,
## LENGTHUNIT["metre",1],
## ID["EPSG",8807]]],
## CS[Cartesian,2],
## AXIS["northing (N)",north,
## ORDER[1],
## LENGTHUNIT["metre",1]],
## AXIS["easting (E)",east,
## ORDER[2],
## LENGTHUNIT["metre",1]],
## USAGE[
## SCOPE["unknown"],
## AREA["Singapore"],
## BBOX[1.13,103.59,1.47,104.07]],
## ID["EPSG",3414]]
The solution:
First, checking the projection of sf_preschool
st_crs(sf_preschool)
## Coordinate Reference System:
## User input: WGS 84
## wkt:
## GEOGCRS["WGS 84",
## DATUM["World Geodetic System 1984",
## ELLIPSOID["WGS 84",6378137,298.257223563,
## LENGTHUNIT["metre",1]]],
## PRIMEM["Greenwich",0,
## ANGLEUNIT["degree",0.0174532925199433]],
## CS[ellipsoidal,2],
## AXIS["geodetic latitude (Lat)",north,
## ORDER[1],
## ANGLEUNIT["degree",0.0174532925199433]],
## AXIS["geodetic longitude (Lon)",east,
## ORDER[2],
## ANGLEUNIT["degree",0.0174532925199433]],
## ID["EPSG",4326]]
Next, transforming sf_preschool simple feature dataframe onto svy21 projected coordinate system (i.e. EPSG 3414)
sf_preschool3414 <- st_transform(sf_preschool, 3414)
st_crs(sf_preschool3414)
## Coordinate Reference System:
## User input: EPSG:3414
## wkt:
## PROJCRS["SVY21 / Singapore TM",
## BASEGEOGCRS["SVY21",
## DATUM["SVY21",
## ELLIPSOID["WGS 84",6378137,298.257223563,
## LENGTHUNIT["metre",1]]],
## PRIMEM["Greenwich",0,
## ANGLEUNIT["degree",0.0174532925199433]],
## ID["EPSG",4757]],
## CONVERSION["Singapore Transverse Mercator",
## METHOD["Transverse Mercator",
## ID["EPSG",9807]],
## PARAMETER["Latitude of natural origin",1.36666666666667,
## ANGLEUNIT["degree",0.0174532925199433],
## ID["EPSG",8801]],
## PARAMETER["Longitude of natural origin",103.833333333333,
## ANGLEUNIT["degree",0.0174532925199433],
## ID["EPSG",8802]],
## PARAMETER["Scale factor at natural origin",1,
## SCALEUNIT["unity",1],
## ID["EPSG",8805]],
## PARAMETER["False easting",28001.642,
## LENGTHUNIT["metre",1],
## ID["EPSG",8806]],
## PARAMETER["False northing",38744.572,
## LENGTHUNIT["metre",1],
## ID["EPSG",8807]]],
## CS[Cartesian,2],
## AXIS["northing (N)",north,
## ORDER[1],
## LENGTHUNIT["metre",1]],
## AXIS["easting (E)",east,
## ORDER[2],
## LENGTHUNIT["metre",1]],
## USAGE[
## SCOPE["unknown"],
## AREA["Singapore"],
## BBOX[1.13,103.59,1.47,104.07]],
## ID["EPSG",3414]]
Although simple feature data.frame is gaining popularity again sp’s Spatial* classes, there are, however, many geospatial analysis packages require the input geospatial data in sp’s Spatial* classes. In this section, you will learn how to convert simple feature data.frame to sp’s Spatial* class.
The code chunk below uses as_Spatial() of sf package to convert sf_preschool3414 simple feature data.frame to sp’s Spatial* class.
sp_preschool <- as_Spatial(sf_preschool3414)
## Warning in showSRID(uprojargs, format = "PROJ", multiline = "NO"): Discarded datum Unknown based on WGS84 ellipsoid in CRS definition,
## but +towgs84= values preserved
Notice that the output is a SpatialPointsDataFrame class.
You can check the content of the SpatialPointsDataFrame by using summary() as shown in the code chunk below.
summary(sp_preschool)
## Object of class SpatialPointsDataFrame
## Coordinates:
## min max
## coords.x1 11203.01 45404.24
## coords.x2 25667.60 49300.88
## coords.x3 0.00 0.00
## Is projected: TRUE
## proj4string :
## [+proj=tmerc +lat_0=1.36666666666667 +lon_0=103.833333333333 +k=1
## +x_0=28001.642 +y_0=38744.572 +ellps=WGS84 +towgs84=0,0,0,0,0,0,0
## +units=m +no_defs]
## Number of points: 1359
## Data attributes:
## Name Description
## Length:1359 Length:1359
## Class :character Class :character
## Mode :character Mode :character
DIY: Using the steps you had learned, convert sf_mpsz3414 and sf_mpsz simple feature data.frame to sp’s Spatial* classes. After the conversion, examine the output spatial classes carefully. Write short notes to decribe your onservation of the output spatial classes.
The scenario:
The authority is planning to upgrade the exiting cycling path. To do so, they need to acquire 5 metres reserve land on the both sides of the current cycling path. You are tasked to determine the extend of the land need to be acquired and their total areas.
The solution:
Creating 5-meter buffers around cycling path and calculate the total area of the buffers.
sf_buffer_cycling <- st_buffer(sf_cyclingpath,
dist=5, nQuadSegs = 30)
sf_buffer_cycling$AREA <- st_area(sf_buffer_cycling)
sum(sf_buffer_cycling$AREA)
## 773143.9 [m^2]
hist(sf_buffer_cycling$AREA)
The scenario:
A pre-school services group want to find out numbers of pre-school in each Planning Subzone.
The solution:
The code chunk below first identify pre-schools located inside each Planning Subzone by using st_intersects(). Then, the length() is used to calculate numbers of pre-school fall inside each planning subzone.
sf_mpsz3414$`PreSch Count`<- lengths(st_intersects(sf_mpsz3414, sf_preschool3414))
Warning: You should not confuse with st_intersection().
You can check the summary statistics of the newly derived PreSch Count field by using summary() as shown in the code chunk below.
summary(sf_mpsz3414$`PreSch Count`)
## Min. 1st Qu. Median Mean 3rd Qu. Max.
## 0.000 0.000 2.000 4.207 6.000 37.000
To list the planning subzone with the most number of of pre-school, the top_n() of dplyr package is used as shown in the code chunk below.
top_n(sf_mpsz3414, 1, `PreSch Count`)
## Simple feature collection with 1 feature and 16 fields
## geometry type: MULTIPOLYGON
## dimension: XY
## bbox: xmin: 23449.05 ymin: 46001.23 xmax: 25594.22 ymax: 47996.47
## projected CRS: SVY21 / Singapore TM
## OBJECTID SUBZONE_NO SUBZONE_N SUBZONE_C CA_IND PLN_AREA_N PLN_AREA_C
## 1 290 3 WOODLANDS EAST WDSZ03 N WOODLANDS WD
## REGION_N REGION_C INC_CRC FMEL_UPD_D X_ADDR Y_ADDR
## 1 NORTH REGION NR C90769E43EE6B0F2 2014-12-05 24506.64 46991.63
## SHAPE_Leng SHAPE_Area geometry PreSch Count
## 1 6603.608 2553464 MULTIPOLYGON (((24786.75 46... 37
Quiz: Calculate the density of pre-school by planning subzone. With the help of appropriate graphical method, describe the distribution of the newly derive variable.
The code chunk below uses st_area() of sf package to derived the area of each planning subzone.
sf_mpsz3414$Area <- sf_mpsz3414 %>%
st_area()
sf_mpsz3414 <- sf_mpsz3414 %>%
mutate(`PreSch Density` = `PreSch Count`/Area * 1000000)
ggplot(data=sf_mpsz3414,
aes(x= as.numeric(`PreSch Density`)))+
geom_histogram(bins=20,
color="black", fill="light blue")
ggplot(data=sf_mpsz3414,
aes(y = `PreSch Count`, x= as.numeric(`PreSch Density`)))+
geom_point(color="black", fill="light blue")
In this section, you will learn how to handle geospatial data in shapefile format using sp, gdal and rgeos packages in R.
In this section, you will learn how to import MP14_SUBZONE_WEB_PL GIS layer into R. It is stored in shapefile format. The spatial data model of this GIS data are polygon objects.
To import the GIS data layer into R, readOGR() from rgdal package will be used.
The data importing task is performed by using the code chunk below:
mpsz_sp <- readOGR(dsn = "data/geospatial",
layer = "MP14_SUBZONE_WEB_PL")
## OGR data source with driver: ESRI Shapefile
## Source: "D:\IS415-AY2020-21T1\03-Hands-on Exercises\Hands-on_Ex02\data\geospatial", layer: "MP14_SUBZONE_WEB_PL"
## with 323 features
## It has 15 fields
Notice that mpsz_sp is in SpatialPolygonDataFrame.
You can check the contents of mpsz_sp data object by using summary().
The code chunk:
summary(mpsz_sp)
## Object of class SpatialPolygonsDataFrame
## Coordinates:
## min max
## x 2667.538 56396.44
## y 15748.721 50256.33
## Is projected: TRUE
## proj4string :
## [+proj=tmerc +lat_0=1.36666666666667 +lon_0=103.833333333333 +k=1
## +x_0=28001.642 +y_0=38744.572 +datum=WGS84 +units=m +no_defs]
## Data attributes:
## OBJECTID SUBZONE_NO SUBZONE_N SUBZONE_C
## Min. : 1.0 Min. : 1.000 Length:323 Length:323
## 1st Qu.: 81.5 1st Qu.: 2.000 Class :character Class :character
## Median :162.0 Median : 4.000 Mode :character Mode :character
## Mean :162.0 Mean : 4.625
## 3rd Qu.:242.5 3rd Qu.: 6.500
## Max. :323.0 Max. :17.000
## CA_IND PLN_AREA_N PLN_AREA_C REGION_N
## Length:323 Length:323 Length:323 Length:323
## Class :character Class :character Class :character Class :character
## Mode :character Mode :character Mode :character Mode :character
##
##
##
## REGION_C INC_CRC FMEL_UPD_D X_ADDR
## Length:323 Length:323 Length:323 Min. : 5093
## Class :character Class :character Class :character 1st Qu.:21864
## Mode :character Mode :character Mode :character Median :28465
## Mean :27257
## 3rd Qu.:31674
## Max. :50425
## Y_ADDR SHAPE_Leng SHAPE_Area
## Min. :19579 Min. : 871.5 Min. : 39438
## 1st Qu.:31776 1st Qu.: 3709.6 1st Qu.: 628261
## Median :35113 Median : 5211.9 Median : 1229894
## Mean :36106 Mean : 6524.4 Mean : 2420882
## 3rd Qu.:39869 3rd Qu.: 6942.6 3rd Qu.: 2106483
## Max. :49553 Max. :68083.9 Max. :69748299
Let’s view the first few records in the mpsz_sp.
The code chunk
head(mpsz_sp, n=4)
## An object of class "SpatialPolygonsDataFrame"
## Slot "data":
## OBJECTID SUBZONE_NO SUBZONE_N SUBZONE_C CA_IND PLN_AREA_N
## 0 1 1 MARINA SOUTH MSSZ01 Y MARINA SOUTH
## 1 2 1 PEARL'S HILL OTSZ01 Y OUTRAM
## 2 3 3 BOAT QUAY SRSZ03 Y SINGAPORE RIVER
## 3 4 8 HENDERSON HILL BMSZ08 N BUKIT MERAH
## PLN_AREA_C REGION_N REGION_C INC_CRC FMEL_UPD_D X_ADDR
## 0 MS CENTRAL REGION CR 5ED7EB253F99252E 2014/12/05 31595.84
## 1 OT CENTRAL REGION CR 8C7149B9EB32EEFC 2014/12/05 28679.06
## 2 SR CENTRAL REGION CR C35FEFF02B13E0E5 2014/12/05 29654.96
## 3 BM CENTRAL REGION CR 3775D82C5DDBEFBD 2014/12/05 26782.83
## Y_ADDR SHAPE_Leng SHAPE_Area
## 0 29220.19 5267.381 1630379.3
## 1 29782.05 3506.107 559816.2
## 2 29974.66 1740.926 160807.5
## 3 29933.77 3313.625 595428.9
##
## Slot "polygons":
## [[1]]
## An object of class "Polygons"
## Slot "Polygons":
## [[1]]
## An object of class "Polygon"
## Slot "labpt":
## [1] 31595.84 29220.19
##
## Slot "area":
## [1] 1630379
##
## Slot "hole":
## [1] FALSE
##
## Slot "ringDir":
## [1] 1
##
## Slot "coords":
## [,1] [,2]
## [1,] 31495.56 30140.01
## [2,] 31980.96 29671.60
## [3,] 32332.90 29331.98
## [4,] 32362.39 29303.52
## [5,] 32362.32 29303.47
## [6,] 32277.93 29241.61
## [7,] 32277.63 29241.39
## [8,] 32242.98 29215.99
## [9,] 32243.85 29212.25
## [10,] 32244.71 29204.11
## [11,] 32244.40 29197.33
## [12,] 32243.11 29190.23
## [13,] 32241.86 29185.54
## [14,] 32245.84 29178.98
## [15,] 32246.53 29177.84
## [16,] 32247.80 29175.75
## [17,] 32247.50 29175.44
## [18,] 32249.28 29172.36
## [19,] 32249.35 29172.24
## [20,] 32250.47 29170.31
## [21,] 32254.52 29163.30
## [22,] 32255.45 29161.51
## [23,] 32255.47 29161.48
## [24,] 32255.50 29161.43
## [25,] 32256.61 29162.25
## [26,] 32276.38 29176.71
## [27,] 32276.14 29177.44
## [28,] 32276.00 29178.19
## [29,] 32275.98 29178.33
## [30,] 32275.95 29178.94
## [31,] 32276.05 29179.98
## [32,] 32276.32 29180.99
## [33,] 32276.76 29181.93
## [34,] 32277.36 29182.79
## [35,] 32278.10 29183.53
## [36,] 32278.96 29184.13
## [37,] 32279.90 29184.57
## [38,] 32280.91 29184.84
## [39,] 32281.95 29184.93
## [40,] 32282.99 29184.84
## [41,] 32284.00 29184.57
## [42,] 32284.95 29184.13
## [43,] 32285.81 29183.53
## [44,] 32286.55 29182.79
## [45,] 32287.15 29181.93
## [46,] 32287.59 29180.99
## [47,] 32287.86 29179.98
## [48,] 32287.95 29178.94
## [49,] 32287.89 29178.07
## [50,] 32287.70 29177.21
## [51,] 32287.39 29176.40
## [52,] 32286.96 29175.64
## [53,] 32286.43 29174.94
## [54,] 32285.81 29174.34
## [55,] 32284.96 29173.74
## [56,] 32284.02 29173.30
## [57,] 32283.02 29173.03
## [58,] 32281.99 29172.94
## [59,] 32280.96 29173.02
## [60,] 32279.95 29173.28
## [61,] 32279.01 29173.71
## [62,] 32278.15 29174.29
## [63,] 32256.92 29158.73
## [64,] 32257.83 29157.00
## [65,] 32261.83 29149.37
## [66,] 32270.03 29133.76
## [67,] 32273.35 29127.05
## [68,] 32281.72 29110.09
## [69,] 32295.80 29076.93
## [70,] 32303.86 29052.94
## [71,] 32312.76 29024.82
## [72,] 32317.12 28996.06
## [73,] 32316.77 28973.86
## [74,] 32310.75 28915.59
## [75,] 32306.43 28897.24
## [76,] 32306.09 28896.26
## [77,] 32297.72 28872.16
## [78,] 32290.71 28856.11
## [79,] 32286.03 28845.38
## [80,] 32280.67 28835.20
## [81,] 32270.45 28818.77
## [82,] 32259.31 28804.55
## [83,] 32254.93 28799.78
## [84,] 32237.60 28780.92
## [85,] 32232.68 28775.32
## [86,] 32221.54 28763.14
## [87,] 32211.38 28754.91
## [88,] 32199.90 28746.34
## [89,] 32176.95 28729.86
## [90,] 32164.93 28721.58
## [91,] 32123.16 28690.51
## [92,] 32117.45 28686.44
## [93,] 32071.63 28654.08
## [94,] 32020.66 28616.17
## [95,] 32007.66 28606.51
## [96,] 31959.53 28570.52
## [97,] 31894.78 28523.78
## [98,] 31822.59 28471.04
## [99,] 31799.89 28454.37
## [100,] 31795.32 28451.01
## [101,] 31758.36 28423.18
## [102,] 31684.71 28369.47
## [103,] 31677.24 28374.54
## [104,] 31533.48 28468.39
## [105,] 31321.75 28602.94
## [106,] 31142.15 28717.50
## [107,] 31071.28 28762.71
## [108,] 31013.24 28804.30
## [109,] 30794.28 28944.99
## [110,] 30844.87 29021.96
## [111,] 30895.45 29098.90
## [112,] 30946.64 29176.78
## [113,] 30967.70 29208.81
## [114,] 30994.35 29257.31
## [115,] 31040.78 29341.21
## [116,] 31045.45 29349.65
## [117,] 31083.68 29446.41
## [118,] 31115.16 29545.41
## [119,] 31132.64 29648.87
## [120,] 31139.24 29724.88
## [121,] 31141.71 29816.83
## [122,] 31143.66 29900.60
## [123,] 31146.15 30000.00
## [124,] 31146.33 30007.18
## [125,] 31147.27 30044.77
## [126,] 31147.43 30053.59
## [127,] 31147.43 30053.94
## [128,] 31148.18 30096.48
## [129,] 31148.38 30107.67
## [130,] 31172.87 30107.41
## [131,] 31173.51 30107.41
## [132,] 31182.48 30107.31
## [133,] 31183.11 30107.31
## [134,] 31203.62 30107.09
## [135,] 31214.23 30106.16
## [136,] 31225.67 30105.23
## [137,] 31255.07 30103.28
## [138,] 31300.83 30101.17
## [139,] 31313.90 30100.22
## [140,] 31330.24 30099.23
## [141,] 31358.01 30097.30
## [142,] 31366.19 30097.22
## [143,] 31375.99 30097.11
## [144,] 31389.06 30096.16
## [145,] 31398.89 30097.69
## [146,] 31409.53 30099.22
## [147,] 31422.64 30101.53
## [148,] 31442.30 30105.42
## [149,] 31446.39 30106.19
## [150,] 31455.45 30112.64
## [151,] 31462.04 30117.48
## [152,] 31464.52 30119.90
## [153,] 31469.46 30123.12
## [154,] 31477.68 30127.13
## [155,] 31491.67 30135.98
## [156,] 31494.14 30137.59
## [157,] 31495.56 30140.01
##
##
##
## Slot "plotOrder":
## [1] 1
##
## Slot "labpt":
## [1] 31595.84 29220.19
##
## Slot "ID":
## [1] "0"
##
## Slot "area":
## [1] 1630379
##
##
## [[2]]
## An object of class "Polygons"
## Slot "Polygons":
## [[1]]
## An object of class "Polygon"
## Slot "labpt":
## [1] 28679.06 29782.05
##
## Slot "area":
## [1] 559816.2
##
## Slot "hole":
## [1] FALSE
##
## Slot "ringDir":
## [1] 1
##
## Slot "coords":
## [,1] [,2]
## [1,] 29092.28 30021.89
## [2,] 29119.64 30006.70
## [3,] 29144.01 29992.32
## [4,] 29144.01 29992.31
## [5,] 29177.00 29973.48
## [6,] 29213.87 29952.87
## [7,] 29230.43 29943.43
## [8,] 29237.23 29939.55
## [9,] 29249.98 29932.29
## [10,] 29280.76 29914.32
## [11,] 29297.14 29904.94
## [12,] 29304.44 29900.73
## [13,] 29325.78 29887.32
## [14,] 29333.93 29882.00
## [15,] 29313.73 29858.63
## [16,] 29289.72 29827.32
## [17,] 29264.50 29794.43
## [18,] 29228.88 29751.28
## [19,] 29209.26 29727.58
## [20,] 29189.03 29704.77
## [21,] 29161.20 29723.07
## [22,] 29147.20 29734.55
## [23,] 29138.42 29741.75
## [24,] 29138.17 29741.47
## [25,] 29129.70 29731.90
## [26,] 29121.57 29721.95
## [27,] 29119.02 29718.94
## [28,] 29107.57 29705.40
## [29,] 29094.00 29716.53
## [30,] 29069.41 29686.61
## [31,] 29058.14 29673.52
## [32,] 29058.08 29673.58
## [33,] 29039.84 29688.52
## [34,] 29018.15 29662.08
## [35,] 28998.38 29637.99
## [36,] 28998.32 29638.04
## [37,] 28966.77 29663.94
## [38,] 28959.94 29669.54
## [39,] 28959.85 29669.39
## [40,] 28958.77 29667.58
## [41,] 28957.82 29665.71
## [42,] 28957.01 29663.77
## [43,] 28956.33 29661.78
## [44,] 28955.79 29659.74
## [45,] 28955.40 29657.67
## [46,] 28955.15 29655.59
## [47,] 28955.05 29653.48
## [48,] 28954.54 29613.70
## [49,] 28954.41 29603.45
## [50,] 28954.41 29603.07
## [51,] 28954.48 29600.91
## [52,] 28954.72 29598.76
## [53,] 28955.11 29596.63
## [54,] 28942.33 29596.64
## [55,] 28898.27 29596.67
## [56,] 28893.21 29596.67
## [57,] 28889.66 29596.67
## [58,] 28883.36 29596.68
## [59,] 28883.21 29596.68
## [60,] 28882.47 29596.68
## [61,] 28881.85 29596.68
## [62,] 28874.60 29596.63
## [63,] 28874.19 29596.63
## [64,] 28874.52 29589.82
## [65,] 28874.52 29589.72
## [66,] 28874.57 29583.71
## [67,] 28874.61 29579.32
## [68,] 28874.38 29576.00
## [69,] 28874.08 29573.67
## [70,] 28873.59 29570.94
## [71,] 28872.83 29567.85
## [72,] 28871.67 29564.26
## [73,] 28869.52 29559.18
## [74,] 28865.72 29553.06
## [75,] 28864.67 29551.37
## [76,] 28855.31 29539.95
## [77,] 28848.14 29531.29
## [78,] 28838.95 29520.20
## [79,] 28830.90 29509.95
## [80,] 28827.98 29505.10
## [81,] 28822.93 29493.61
## [82,] 28820.83 29479.58
## [83,] 28820.98 29470.89
## [84,] 28821.39 29464.75
## [85,] 28821.99 29452.18
## [86,] 28822.03 29443.41
## [87,] 28822.06 29438.02
## [88,] 28823.49 29426.19
## [89,] 28814.49 29410.39
## [90,] 28805.91 29417.72
## [91,] 28801.68 29421.34
## [92,] 28795.67 29426.48
## [93,] 28794.40 29425.65
## [94,] 28794.06 29425.43
## [95,] 28792.88 29424.61
## [96,] 28791.42 29423.63
## [97,] 28789.60 29422.50
## [98,] 28788.69 29421.96
## [99,] 28787.04 29421.05
## [100,] 28785.46 29420.30
## [101,] 28782.69 29418.96
## [102,] 28782.09 29418.69
## [103,] 28780.57 29418.02
## [104,] 28779.42 29417.50
## [105,] 28779.16 29417.39
## [106,] 28777.47 29416.65
## [107,] 28774.15 29415.18
## [108,] 28767.66 29412.56
## [109,] 28761.54 29410.25
## [110,] 28758.42 29408.98
## [111,] 28753.36 29407.31
## [112,] 28752.35 29406.96
## [113,] 28751.26 29406.61
## [114,] 28750.89 29406.49
## [115,] 28745.71 29404.77
## [116,] 28743.65 29404.21
## [117,] 28742.94 29403.98
## [118,] 28739.74 29403.09
## [119,] 28732.66 29401.13
## [120,] 28732.62 29401.12
## [121,] 28732.53 29401.09
## [122,] 28732.49 29401.08
## [123,] 28732.36 29401.05
## [124,] 28732.32 29401.04
## [125,] 28732.27 29401.03
## [126,] 28732.23 29401.01
## [127,] 28732.15 29400.99
## [128,] 28732.10 29400.98
## [129,] 28732.02 29400.96
## [130,] 28731.99 29400.95
## [131,] 28731.95 29400.94
## [132,] 28731.88 29400.92
## [133,] 28731.84 29400.91
## [134,] 28731.80 29400.90
## [135,] 28731.76 29400.89
## [136,] 28731.68 29400.87
## [137,] 28731.64 29400.86
## [138,] 28731.56 29400.84
## [139,] 28731.51 29400.83
## [140,] 28731.43 29400.81
## [141,] 28731.39 29400.80
## [142,] 28731.22 29400.76
## [143,] 28731.18 29400.74
## [144,] 28731.13 29400.73
## [145,] 28731.09 29400.72
## [146,] 28731.00 29400.70
## [147,] 28730.96 29400.69
## [148,] 28730.88 29400.67
## [149,] 28730.86 29400.66
## [150,] 28730.80 29400.65
## [151,] 28730.78 29400.64
## [152,] 28730.63 29400.61
## [153,] 28730.62 29400.60
## [154,] 28730.50 29400.57
## [155,] 28730.49 29400.57
## [156,] 28728.43 29400.00
## [157,] 28726.10 29399.50
## [158,] 28723.14 29398.82
## [159,] 28720.78 29398.30
## [160,] 28717.00 29397.51
## [161,] 28714.97 29397.11
## [162,] 28712.65 29396.61
## [163,] 28711.70 29396.46
## [164,] 28710.05 29396.20
## [165,] 28705.64 29395.43
## [166,] 28699.69 29394.55
## [167,] 28696.71 29394.08
## [168,] 28696.19 29394.05
## [169,] 28687.35 29393.23
## [170,] 28678.45 29393.09
## [171,] 28678.44 29393.09
## [172,] 28678.20 29393.08
## [173,] 28677.70 29393.11
## [174,] 28674.17 29393.27
## [175,] 28671.42 29393.47
## [176,] 28668.02 29393.78
## [177,] 28665.03 29394.19
## [178,] 28663.41 29394.38
## [179,] 28662.44 29394.49
## [180,] 28658.88 29395.15
## [181,] 28655.53 29395.76
## [182,] 28652.79 29396.42
## [183,] 28648.62 29397.49
## [184,] 28645.27 29398.50
## [185,] 28642.43 29399.42
## [186,] 28640.80 29400.00
## [187,] 28638.82 29400.72
## [188,] 28636.38 29401.66
## [189,] 28633.48 29402.85
## [190,] 28631.20 29403.85
## [191,] 28628.87 29404.94
## [192,] 28626.33 29406.19
## [193,] 28624.21 29407.30
## [194,] 28621.70 29408.67
## [195,] 28620.91 29409.12
## [196,] 28619.55 29409.91
## [197,] 28619.14 29410.16
## [198,] 28617.61 29411.09
## [199,] 28617.52 29411.14
## [200,] 28617.27 29411.29
## [201,] 28615.21 29412.60
## [202,] 28614.60 29413.01
## [203,] 28612.93 29414.12
## [204,] 28612.91 29414.14
## [205,] 28612.79 29414.21
## [206,] 28612.81 29414.22
## [207,] 28611.99 29414.77
## [208,] 28611.89 29414.84
## [209,] 28611.88 29414.85
## [210,] 28611.80 29414.91
## [211,] 28608.45 29417.35
## [212,] 28608.45 29417.35
## [213,] 28605.10 29419.97
## [214,] 28604.85 29419.76
## [215,] 28570.60 29389.79
## [216,] 28569.67 29388.98
## [217,] 28569.17 29388.55
## [218,] 28568.56 29388.01
## [219,] 28568.55 29388.00
## [220,] 28544.48 29366.95
## [221,] 28544.43 29366.91
## [222,] 28534.71 29358.40
## [223,] 28533.82 29357.63
## [224,] 28533.12 29357.01
## [225,] 28514.37 29368.33
## [226,] 28485.96 29385.98
## [227,] 28451.83 29407.33
## [228,] 28423.78 29424.73
## [229,] 28405.57 29436.06
## [230,] 28382.50 29450.42
## [231,] 28344.38 29478.94
## [232,] 28295.63 29525.17
## [233,] 28295.58 29525.22
## [234,] 28257.94 29565.95
## [235,] 28246.92 29577.79
## [236,] 28241.13 29584.01
## [237,] 28225.29 29611.35
## [238,] 28197.60 29680.09
## [239,] 28197.60 29680.10
## [240,] 28194.87 29686.69
## [241,] 28192.86 29691.80
## [242,] 28177.60 29730.39
## [243,] 28167.67 29772.84
## [244,] 28167.66 29772.89
## [245,] 28160.23 29808.82
## [246,] 28161.28 29831.40
## [247,] 28161.29 29831.64
## [248,] 28161.90 29844.59
## [249,] 28163.59 29857.44
## [250,] 28164.63 29865.39
## [251,] 28166.98 29875.84
## [252,] 28172.77 29901.65
## [253,] 28180.47 29935.14
## [254,] 28181.84 29941.14
## [255,] 28187.37 29965.23
## [256,] 28191.49 29957.97
## [257,] 28195.30 29951.27
## [258,] 28195.86 29950.28
## [259,] 28197.70 29947.03
## [260,] 28198.60 29945.46
## [261,] 28200.19 29942.65
## [262,] 28201.38 29940.57
## [263,] 28201.48 29940.38
## [264,] 28206.88 29930.87
## [265,] 28223.93 29944.50
## [266,] 28226.93 29967.18
## [267,] 28241.49 29978.26
## [268,] 28279.49 29982.41
## [269,] 28312.54 29947.25
## [270,] 28330.28 29943.70
## [271,] 28330.06 29936.73
## [272,] 28339.69 29919.97
## [273,] 28365.07 29976.32
## [274,] 28364.31 29980.53
## [275,] 28385.82 30023.19
## [276,] 28393.40 30038.23
## [277,] 28407.93 30043.31
## [278,] 28420.35 30007.78
## [279,] 28517.84 30043.45
## [280,] 28505.92 30077.55
## [281,] 28545.39 30091.34
## [282,] 28584.65 30105.06
## [283,] 28587.26 30113.59
## [284,] 28587.37 30113.95
## [285,] 28594.41 30136.92
## [286,] 28603.72 30139.48
## [287,] 28629.73 30146.63
## [288,] 28665.45 30160.38
## [289,] 28692.41 30169.23
## [290,] 28717.34 30175.13
## [291,] 28753.25 30177.73
## [292,] 28771.26 30170.84
## [293,] 28784.89 30163.54
## [294,] 28790.30 30160.65
## [295,] 28807.82 30149.08
## [296,] 28816.30 30142.56
## [297,] 28831.91 30130.54
## [298,] 28878.24 30095.66
## [299,] 28879.66 30094.59
## [300,] 28884.02 30091.31
## [301,] 28933.79 30057.52
## [302,] 28933.82 30057.51
## [303,] 28971.67 30040.90
## [304,] 29018.61 30026.64
## [305,] 29032.31 30025.75
## [306,] 29092.28 30021.89
##
##
##
## Slot "plotOrder":
## [1] 1
##
## Slot "labpt":
## [1] 28679.06 29782.05
##
## Slot "ID":
## [1] "1"
##
## Slot "area":
## [1] 559816.2
##
##
## [[3]]
## An object of class "Polygons"
## Slot "Polygons":
## [[1]]
## An object of class "Polygon"
## Slot "labpt":
## [1] 29654.96 29974.66
##
## Slot "area":
## [1] 160807.5
##
## Slot "hole":
## [1] FALSE
##
## Slot "ringDir":
## [1] 1
##
## Slot "coords":
## [,1] [,2]
## [1,] 29932.33 29879.12
## [2,] 29947.32 29858.02
## [3,] 29870.46 29801.97
## [4,] 29870.30 29801.90
## [5,] 29870.06 29801.80
## [6,] 29856.98 29796.12
## [7,] 29855.45 29795.46
## [8,] 29854.31 29795.06
## [9,] 29821.15 29783.46
## [10,] 29807.32 29778.62
## [11,] 29805.14 29777.86
## [12,] 29765.70 29796.52
## [13,] 29626.80 29862.23
## [14,] 29608.83 29870.72
## [15,] 29603.60 29862.26
## [16,] 29600.15 29856.58
## [17,] 29543.03 29762.39
## [18,] 29533.88 29766.63
## [19,] 29511.98 29781.51
## [20,] 29462.23 29808.94
## [21,] 29422.12 29831.06
## [22,] 29368.58 29860.64
## [23,] 29361.09 29865.11
## [24,] 29346.03 29874.10
## [25,] 29335.48 29880.99
## [26,] 29333.93 29882.00
## [27,] 29351.26 29902.05
## [28,] 29361.35 29913.94
## [29,] 29384.98 29941.81
## [30,] 29420.83 29983.62
## [31,] 29435.37 30000.56
## [32,] 29460.76 30029.98
## [33,] 29517.22 30096.69
## [34,] 29569.89 30161.12
## [35,] 29640.05 30247.18
## [36,] 29764.89 30176.91
## [37,] 29768.11 30174.61
## [38,] 29777.30 30167.36
## [39,] 29787.21 30159.60
## [40,] 29788.41 30158.10
## [41,] 29792.19 30154.08
## [42,] 29807.70 30130.08
## [43,] 29822.21 30104.59
## [44,] 29839.23 30070.09
## [45,] 29851.25 30042.10
## [46,] 29889.79 29947.11
## [47,] 29901.81 29922.11
## [48,] 29932.33 29879.12
##
##
##
## Slot "plotOrder":
## [1] 1
##
## Slot "labpt":
## [1] 29654.96 29974.66
##
## Slot "ID":
## [1] "2"
##
## Slot "area":
## [1] 160807.5
##
##
## [[4]]
## An object of class "Polygons"
## Slot "Polygons":
## [[1]]
## An object of class "Polygon"
## Slot "labpt":
## [1] 26782.83 29933.77
##
## Slot "area":
## [1] 595428.9
##
## Slot "hole":
## [1] FALSE
##
## Slot "ringDir":
## [1] 1
##
## Slot "coords":
## [,1] [,2]
## [1,] 27131.28 30059.73
## [2,] 27088.33 29709.17
## [3,] 27071.07 29647.46
## [4,] 26931.60 29369.88
## [5,] 26883.28 29391.62
## [6,] 26864.66 29400.00
## [7,] 26817.20 29421.36
## [8,] 26753.19 29449.72
## [9,] 26731.48 29459.34
## [10,] 26711.05 29468.39
## [11,] 26680.47 29481.93
## [12,] 26632.81 29500.24
## [13,] 26524.50 29537.14
## [14,] 26459.37 29560.34
## [15,] 26418.43 29572.74
## [16,] 26403.48 29576.74
## [17,] 26404.25 29591.35
## [18,] 26404.74 29595.83
## [19,] 26405.74 29630.12
## [20,] 26409.62 29642.59
## [21,] 26412.67 29693.85
## [22,] 26431.51 29693.30
## [23,] 26435.19 29746.89
## [24,] 26435.37 29749.52
## [25,] 26437.21 29776.33
## [26,] 26438.24 29794.67
## [27,] 26439.84 29823.71
## [28,] 26441.84 29862.06
## [29,] 26444.11 29903.43
## [30,] 26493.13 29900.77
## [31,] 26494.68 29930.16
## [32,] 26501.55 30060.77
## [33,] 26502.14 30072.08
## [34,] 26503.48 30097.30
## [35,] 26497.86 30103.61
## [36,] 26498.84 30115.00
## [37,] 26427.54 30119.15
## [38,] 26416.97 30120.06
## [39,] 26422.31 30184.64
## [40,] 26426.98 30231.36
## [41,] 26438.40 30346.59
## [42,] 26440.47 30361.12
## [43,] 26451.03 30396.46
## [44,] 27209.21 30330.50
## [45,] 27195.74 30282.48
## [46,] 27165.70 30208.08
## [47,] 27137.47 30104.12
## [48,] 27131.28 30059.73
##
##
##
## Slot "plotOrder":
## [1] 1
##
## Slot "labpt":
## [1] 26782.83 29933.77
##
## Slot "ID":
## [1] "3"
##
## Slot "area":
## [1] 595428.9
##
##
##
## Slot "plotOrder":
## [1] 1 4 2 3
##
## Slot "bbox":
## min max
## x 26403.48 32362.39
## y 28369.47 30396.46
##
## Slot "proj4string":
## CRS arguments:
## +proj=tmerc +lat_0=1.36666666666667 +lon_0=103.833333333333 +k=1
## +x_0=28001.642 +y_0=38744.572 +datum=WGS84 +units=m +no_defs
To view the spatial data, plot() of Base R can be used.
The code chunk:
plot(mpsz_sp)
Using the functiions you had learned, import the Pre-School and Cycling Path GIS data files into R spatial objects.
The solution:
The pre-schools GIS data is in kml format. Before we can import the data file into R, we will use ogrListLayers function of rgdal package to check the actual data structure of the kml data file.
ogrListLayers("data/geospatial/pre-schools-location-kml.kml")
## [1] "PRESCHOOLS_LOCATION"
## attr(,"driver")
## [1] "KML"
## attr(,"nlayers")
## [1] 1
Notice that the file called pre-schools-location-kml is just the folder (refer to the list above). In order to important the layer, we need to use PRESCHOOL_LOCATION layer instead.
The code chunk below will do the trick.
preschool <- readOGR("data/geospatial/pre-schools-location-kml.kml",
"PRESCHOOLS_LOCATION")
## OGR data source with driver: KML
## Source: "D:\IS415-AY2020-21T1\03-Hands-on Exercises\Hands-on_Ex02\data\geospatial\pre-schools-location-kml.kml", layer: "PRESCHOOLS_LOCATION"
## with 1359 features
## It has 2 fields
In this section, you will learn how to import a line geospatial data into R. The geospatial data is the CyclingPath shapefile from LTA DataMall (https://www.mytransport.sg/content/mytransport/home/dataMall.html)
cyclingpath <- readOGR (dsn = "data/geospatial",
layer = "CyclingPath")
## OGR data source with driver: ESRI Shapefile
## Source: "D:\IS415-AY2020-21T1\03-Hands-on Exercises\Hands-on_Ex02\data\geospatial", layer: "CyclingPath"
## with 1625 features
## It has 2 fields
mpsz_svy21 <- spTransform(mpsz_sp,
CRS("+init=epsg:3414"))
Now, it is your turn to change the projection system of the preschool data set from wgs84 to svy21.
The solution:
preschool_svy21 <- spTransform(preschool,
CRS("+init=epsg:3414"))
The scenario
The authority is planning to upgrade the exiting cycling path. To do so, they need to acquire 5 metres reserve land on the both sides of the current cycling path. You are tasked to determine the extend of the land need to be acquired and their total areas.
The solution:
buf_cyclingpath <- gBuffer(cyclingpath, width = 5)
The solution:
buf_cyclingpath <- gBuffer(cyclingpath, byid = TRUE,
width = 5)
The solution:
buf_cyclingpath@data$Area <- gArea(buf_cyclingpath,
byid = TRUE)
sum(buf_cyclingpath@data$Area)
## [1] 771024.9