library(rgdal);library(sp)
library (splancs) #Objects: SpatialPoints
setwd("D:/_Lab_Data/R/R_Labs")
TWN <- readOGR(dsn = "shapefiles", layer = "Townships", encoding="big5")
## OGR data source with driver: ESRI Shapefile
## Source: "shapefiles", layer: "Townships"
## with 368 features and 5 fields
## Feature type: wkbPolygon with 2 dimensions
Flood <- readOGR(dsn = "shapefiles", layer = "flood50", encoding="big5")
## OGR data source with driver: ESRI Shapefile
## Source: "shapefiles", layer: "flood50"
## with 5103 features and 5 fields
## Feature type: wkbPolygon with 2 dimensions
Schools <- readOGR(dsn = "shapefiles", layer = "tpecity_school", encoding="big5")
## OGR data source with driver: ESRI Shapefile
## Source: "shapefiles", layer: "tpecity_school"
## with 198 features and 3 fields
## Feature type: wkbPoint with 2 dimensions
x1<-TWN[1,] # x1 is SpatialPolygonDataFrame. It includes the 1st polygon with all the attribute
head(x1@data)
## UNI_ID Town County Area Code
## 0 1 板橋區 新北市 23 1
x2<-TWN[,1] # x2 is SpatialPolygonDataFrame. It includes ALL polygons with the first field
head(x2@data)
## UNI_ID
## 0 1
## 1 2
## 2 3
## 3 4
## 4 5
## 5 6
x3<-TWN["Code"] # x3 is SpatialPolygonDataFrame. It includes ALL polygons with the field "Code"
head(x3@data)
## Code
## 0 1
## 1 2
## 2 3
## 3 4
## 4 5
## 5 6
sel<-c(T,T,T)
X.SEL<-TWN[sel,] # X.SEL is SpatialPolygonDataFram. Selecting the first 3 polygons with all the attribute
head(X.SEL@data)
## UNI_ID Town County Area Code
## 0 1 板橋區 新北市 23 1
## 1 2 三重區 新北市 16 2
## 2 3 中和區 新北市 20 3
## 3 4 永和區 新北市 6 4
## 4 5 新莊區 新北市 20 5
## 5 6 新店區 新北市 120 6
y<-TWN@data[1,] # y is data.frame. It includes the 1st record
y1<-TWN@data[,1] # y1 is vector. The values of the 1st field(UNI_ID).
Selecting points of A inside or on Polygon B–> A[B,]; over(A, B)
# selecting SCHOOLS within the FLOOD
Schools.Flood <- Schools[Flood, ]
# [Question] I would like to return the list of the schools which are OUT OF the flood.
OUT<-over(Schools, Flood) # OUT is data.frame.
sel2<-is.na(OUT$Id)
Schools.OUT<-Schools[sel2,] # Schools.OUT--> The schools which are out of the flood
plot(Flood, col='cyan')
plot(Schools.OUT, col='red', pch=20, add=T)
poly1coord<-TWN@polygons[[1]]@Polygons[[1]]@coords # TWN's 1st polygon's vertices Coordinates
POLY1<-as.points(poly1coord[,1], poly1coord[,2])
plot(POLY1)
poly2coord<-TWN@polygons[[2]]@Polygons[[1]]@coords # TWN's 2nd polygon's vertices Coordinates
POLY2<-as.points(poly2coord[,1], poly2coord[,2])
plot(POLY2)
TWN_County<-aggregate(x = TWN["Code"], by = list(TWN@data$County), FUN = length)
head(TWN_County@data, n=22)
## Group.1 Code
## 1 宜蘭縣 12
## 2 花蓮縣 13
## 3 金門縣 6
## 4 南投縣 13
## 5 屏東縣 33
## 6 苗栗縣 18
## 7 桃園縣 13
## 8 高雄市 38
## 9 基隆市 7
## 10 連江縣 4
## 11 雲林縣 20
## 12 新北市 29
## 13 新竹市 3
## 14 新竹縣 13
## 15 嘉義市 2
## 16 嘉義縣 18
## 17 彰化縣 26
## 18 臺中市 29
## 19 臺北市 12
## 20 臺東縣 16
## 21 臺南市 37
## 22 澎湖縣 6
library(dplyr)
TWN_County@data <-rename(TWN_County@data, Counts= Code)
plot(TWN_County); nrow(TWN_County)
## [1] 22