library(leaflet)
library(rgeos)
## rgeos version: 0.3-21, (SVN revision 540)
##  GEOS runtime version: 3.5.0-CAPI-1.9.0 r4084 
##  Linking to sp version: 1.2-4 
##  Polygon checking: TRUE
library(ggplot2)
library(RColorBrewer)
options(scipen=9999)
.libPaths("S:/Programs/R-Packages/RLibrary")

library(leaflet)
library(htmltools)
library(data.table)
library(maptools)
## Loading required package: sp
## Checking rgeos availability: TRUE
library(rgdal)
## rgdal: version: 1.2-5, (SVN revision 648)
##  Geospatial Data Abstraction Library extensions to R successfully loaded
##  Loaded GDAL runtime: GDAL 2.0.1, released 2015/09/15
##  Path to GDAL shared files: S:/Programs/R-Packages/RLibrary/rgdal/gdal
##  Loaded PROJ.4 runtime: Rel. 4.9.2, 08 September 2015, [PJ_VERSION: 492]
##  Path to PROJ.4 shared files: S:/Programs/R-Packages/RLibrary/rgdal/proj
##  Linking to sp version: 1.2-4
library(geosphere)
## 
## Attaching package: 'geosphere'
## The following object is masked from 'package:htmltools':
## 
##     span
wgs.84 <- CRSargs(CRS("+init=epsg:4326"))
twd.97 <- CRSargs(CRS("+init=epsg:3826"))
TWD97.TM2.zone.119 <- CRSargs(CRS("+init=epsg:3825"))
TWD97.TM2.zone.121 <- CRSargs(CRS("+init=epsg:3826"))

Town <- readShapeSpatial("D:/地圖/村里_201712/VILLAGE_MOI_WGS84_1061225.shp", IDvar="VILLCODE", proj4string=CRS(twd.97))


##醫院責任畫圖
# data clean
library(data.table)
library(sf)
## Linking to GEOS 3.6.1, GDAL 2.2.0, proj.4 4.9.3
hos <- fread("D:/RData/外傷/Hosipital_Address.csv")
head(hos)
##    hosipital_id 醫療機構代碼                               機構名稱 縣市別
## 1:          156   1231050017 天主教耕莘醫療財團法人耕莘醫院安康院區 新北市
## 2:          108    634030014                 臺北榮民總醫院蘇澳分院 宜蘭縣
## 3:          154   1531120038                           瑞芳礦工醫院 新北市
## 4:          104   1234020011       財團法人天主教靈醫會羅東聖母醫院 宜蘭縣
## 5:          103   1134020019     醫療財團法人羅許基金會羅東博愛醫院 宜蘭縣
## 6:          107   1534050024                               杏和醫院 宜蘭縣
##                                                  評定等級
## 1:                                     一般級急救責任醫院
## 2:                                     一般級急救責任醫院
## 3:                                     一般級急救責任醫院
## 4: 中度級急救責任醫院具備急性腦中風及急性冠心症重度級能力
## 5:                                     重度級急救責任醫院
## 6:                                     一般級急救責任醫院
##                                         機構地址 type
## 1: 新北市新店區安坑段車子路211.217-1.212-1(局部) 一般
## 2:                   宜蘭縣蘇澳鎮蘇濱路一段301號 一般
## 3:                     新北市瑞芳區一坑路71之2號 一般
## 4:                     宜蘭縣羅東鎮中正南路160號 中度
## 5:  宜蘭縣羅東鎮南昌街81、83號、站前南路61、63號 重度
## 6:                   宜蘭縣礁溪鄉礁溪路四段129號 一般
##                                          Address
## 1: 新北市新店區安坑段車子路211.217-1.212-1(局部)
## 2:                   宜蘭縣蘇澳鎮蘇濱路一段301號
## 3:                     新北市瑞芳區一坑路71之2號
## 4:                     宜蘭縣羅東鎮中正南路160號
## 5:  宜蘭縣羅東鎮南昌街81、83號、站前南路61、63號
## 6:                   宜蘭縣礁溪鄉礁溪路四段129號
##                         Response_Address twd_97_x twd_97_y
## 1:                找不到指定的門牌地址。 300947.3  2760775
## 2: 宜蘭縣蘇澳鎮港邊里11鄰蘇濱路一段301號 335926.4  2723300
## 3:    新北市瑞芳區龍川里2鄰一坑路71之2號 330901.4  2778047
## 4:    宜蘭縣羅東鎮南昌里6鄰中正南路160號 328003.0  2729605
## 5:       宜蘭縣羅東鎮南昌里7鄰南昌街99號 327971.7  2729708
## 6:  宜蘭縣礁溪鄉大忠村7鄰礁溪路四段129號 327751.3  2746124
setnames(hos,c("twd_97_x","twd_97_y"),c("lon","lat"))
hos <- hos[,.(機構名稱, type, lon, lat)]
library(sf)
hos_xy <- st_as_sf(x=hos, coords=c("lon","lat"), crs=3826)
hos_xy<-st_transform(x=hos_xy, crs=4326)

hos[,long:=st_coordinates(hos_xy)[,1]]
##                                    機構名稱 type       lon     lat
##   1: 天主教耕莘醫療財團法人耕莘醫院安康院區 一般 300947.30 2760775
##   2:                 臺北榮民總醫院蘇澳分院 一般 335926.43 2723300
##   3:                           瑞芳礦工醫院 一般 330901.38 2778047
##   4:       財團法人天主教靈醫會羅東聖母醫院 中度 328003.02 2729605
##   5:     醫療財團法人羅許基金會羅東博愛醫院 重度 327971.69 2729708
##  ---                                                              
## 195:           奇美醫療財團法人佳里奇美醫院 中度 166425.75 2564638
## 196:                           連江縣立醫院 一般 144943.65 2894790
## 197:   三軍總醫院澎湖分院附設民眾診療服務處 中度 105399.74 2606414
## 198:                     衛生福利部澎湖醫院 中度 103546.42 2607533
## 199:                     衛生福利部金門醫院 中度 -11871.72 2706255
##          long
##   1: 121.5045
##   2: 121.8486
##   3: 121.8022
##   4: 121.7707
##   5: 121.7704
##  ---         
## 195: 120.1837
## 196: 119.9492
## 197: 119.5837
## 198: 119.5654
## 199: 118.4178
hos[,lat:=st_coordinates(hos_xy)[,2]]
##                                    機構名稱 type       lon      lat
##   1: 天主教耕莘醫療財團法人耕莘醫院安康院區 一般 300947.30 24.95398
##   2:                 臺北榮民總醫院蘇澳分院 一般 335926.43 24.61410
##   3:                           瑞芳礦工醫院 一般 330901.38 25.10860
##   4:       財團法人天主教靈醫會羅東聖母醫院 中度 328003.02 24.67143
##   5:     醫療財團法人羅許基金會羅東博愛醫院 重度 327971.69 24.67237
##  ---                                                               
## 195:           奇美醫療財團法人佳里奇美醫院 中度 166425.75 23.18170
## 196:                           連江縣立醫院 一般 144943.65 26.16084
## 197:   三軍總醫院澎湖分院附設民眾診療服務處 中度 105399.74 23.55462
## 198:                     衛生福利部澎湖醫院 中度 103546.42 23.56455
## 199:                     衛生福利部金門醫院 中度 -11871.72 24.44054
##          long
##   1: 121.5045
##   2: 121.8486
##   3: 121.8022
##   4: 121.7707
##   5: 121.7704
##  ---         
## 195: 120.1837
## 196: 119.9492
## 197: 119.5837
## 198: 119.5654
## 199: 118.4178
library(stringr)
hos[ str_detect(type,"一"), type_new:=1]
##                                    機構名稱 type       lon      lat
##   1: 天主教耕莘醫療財團法人耕莘醫院安康院區 一般 300947.30 24.95398
##   2:                 臺北榮民總醫院蘇澳分院 一般 335926.43 24.61410
##   3:                           瑞芳礦工醫院 一般 330901.38 25.10860
##   4:       財團法人天主教靈醫會羅東聖母醫院 中度 328003.02 24.67143
##   5:     醫療財團法人羅許基金會羅東博愛醫院 重度 327971.69 24.67237
##  ---                                                               
## 195:           奇美醫療財團法人佳里奇美醫院 中度 166425.75 23.18170
## 196:                           連江縣立醫院 一般 144943.65 26.16084
## 197:   三軍總醫院澎湖分院附設民眾診療服務處 中度 105399.74 23.55462
## 198:                     衛生福利部澎湖醫院 中度 103546.42 23.56455
## 199:                     衛生福利部金門醫院 中度 -11871.72 24.44054
##          long type_new
##   1: 121.5045        1
##   2: 121.8486        1
##   3: 121.8022        1
##   4: 121.7707       NA
##   5: 121.7704       NA
##  ---                  
## 195: 120.1837       NA
## 196: 119.9492        1
## 197: 119.5837       NA
## 198: 119.5654       NA
## 199: 118.4178       NA
hos[ str_detect(type,"中"), type_new:=2]
##                                    機構名稱 type       lon      lat
##   1: 天主教耕莘醫療財團法人耕莘醫院安康院區 一般 300947.30 24.95398
##   2:                 臺北榮民總醫院蘇澳分院 一般 335926.43 24.61410
##   3:                           瑞芳礦工醫院 一般 330901.38 25.10860
##   4:       財團法人天主教靈醫會羅東聖母醫院 中度 328003.02 24.67143
##   5:     醫療財團法人羅許基金會羅東博愛醫院 重度 327971.69 24.67237
##  ---                                                               
## 195:           奇美醫療財團法人佳里奇美醫院 中度 166425.75 23.18170
## 196:                           連江縣立醫院 一般 144943.65 26.16084
## 197:   三軍總醫院澎湖分院附設民眾診療服務處 中度 105399.74 23.55462
## 198:                     衛生福利部澎湖醫院 中度 103546.42 23.56455
## 199:                     衛生福利部金門醫院 中度 -11871.72 24.44054
##          long type_new
##   1: 121.5045        1
##   2: 121.8486        1
##   3: 121.8022        1
##   4: 121.7707        2
##   5: 121.7704       NA
##  ---                  
## 195: 120.1837        2
## 196: 119.9492        1
## 197: 119.5837        2
## 198: 119.5654        2
## 199: 118.4178        2
hos[ str_detect(type,"重"), type_new:=3]
##                                    機構名稱 type       lon      lat
##   1: 天主教耕莘醫療財團法人耕莘醫院安康院區 一般 300947.30 24.95398
##   2:                 臺北榮民總醫院蘇澳分院 一般 335926.43 24.61410
##   3:                           瑞芳礦工醫院 一般 330901.38 25.10860
##   4:       財團法人天主教靈醫會羅東聖母醫院 中度 328003.02 24.67143
##   5:     醫療財團法人羅許基金會羅東博愛醫院 重度 327971.69 24.67237
##  ---                                                               
## 195:           奇美醫療財團法人佳里奇美醫院 中度 166425.75 23.18170
## 196:                           連江縣立醫院 一般 144943.65 26.16084
## 197:   三軍總醫院澎湖分院附設民眾診療服務處 中度 105399.74 23.55462
## 198:                     衛生福利部澎湖醫院 中度 103546.42 23.56455
## 199:                     衛生福利部金門醫院 中度 -11871.72 24.44054
##          long type_new
##   1: 121.5045        1
##   2: 121.8486        1
##   3: 121.8022        1
##   4: 121.7707        2
##   5: 121.7704        3
##  ---                  
## 195: 120.1837        2
## 196: 119.9492        1
## 197: 119.5837        2
## 198: 119.5654        2
## 199: 118.4178        2
setnames(hos, "機構名稱","Hosipital_Name")
hos <- hos[,.(Hosipital_Name, type,type_new, long, lat)]
# 不分顏色
m <-leaflet(data = hos[1:199,]) %>% addTiles() %>%
  addMarkers(~long, ~lat, popup = ~as.character(type), label = ~as.character(type))
m
# by color
library(leaflet)
df.20 <- hos[1:199,]
getColor <- function(hos) {
  sapply(hos$type_new, function(type_new) {
    if(type_new == 1) {
      "green"
    } else if(type_new  == 2) {
      "orange"
    } else {
      "red"
    } })
}

icons <- awesomeIcons(
  icon = 'ios-close',
  iconColor = 'black',
  library = 'ion',
  markerColor = getColor(df.20)
)
g <-leaflet(df.20) %>% addTiles() %>%
  addAwesomeMarkers(~long, ~lat, icon=icons, label=~as.character(type))

g
##Marker Clusters
cluste_m <- leaflet(hos) %>% addTiles() %>% addMarkers(clusterOptions = markerClusterOptions())
## Assuming 'long' and 'lat' are longitude and latitude, respectively
cluste_m