크롤링을 활용한 미세먼지 실시간 시각화하기
library(RSelenium)
list.of.packages <- c('classInt','rgdal',"xml2","XML","leaflet","leaflet.extras","classInt",'rjson','rgdal','protolite','sp','maps','maptools')
new.packages <- list.of.packages[!(list.of.packages %in% installed.packages()[,"Package"])]
if(length(new.packages)) install.packages(new.packages, dependencies = TRUE)
map <- readOGR( "skorea_municipalities_geo_simple.json")
port1=4568L
remDr<-rsDriver(port=port1)
remDr$open()
url='https://www.airkorea.or.kr/web/sidoCompare?itemCode=10008&pMENU_NO=102'
remDr$navigate(url)
remDr$screenshot(display = T)
source=remDr$getPageSource()[[1]]
data=XML::readHTMLTable(source,stringsAsFactors = FALSE)[[1]]
remDr$close()
head(data)
rownames(data)<-data[,1]
data=data[,-1]
data=t(data)
data=data.frame(area=rownames(data),HourMean=data[,1])
rownames(data)=NULL
data$area=as.character(data$area)
data$HourMean=as.numeric(data$HourMean)
setwd('/home/ducj/data/data/shpfile/shp/CTPRVN_201703')
map=readOGR('TL_SCCO_CTPRVN.shp',encoding = 'euc-kr',stringsAsFactors = F)
map@data$CTP_KOR_NM<-c('서울','부산','대구','인천','광주','대전','울산','세종','경기','강원','충북','충남','전북','전남','경북','경남','제주')
map@data=merge(map@data,data,by.x='CTP_KOR_NM',by.y='area',all=T)
map@data=map@data[order(map@data$CTPRVN_CD),]
plot(map,col=map@data$HourMean)
map=spTransform(map,"+init=epsg:4326")
map=spTransform(map,CRS("+proj=longlat +ellps=GRS80"))
#addDrawToolbar는 선 다각형 사각형을 바로 지도에 나타낼 수 있게 해준다.
map2=leaflet() %>%
addTiles() %>%
addDrawToolbar(
editOptions=editToolbarOptions(selectedPathOptions=selectedPathOptions())
)
p <- colorBin("YlOrRd", domain = map@data$HourMean, bins = classIntervals(map@data$HourMean, 7)$brks)
map2%>%
addPolygons(data=map,color = ~p(HourMean),stroke=F, smoothFactor = 0.2,fillOpacity = .5)%>%
addLegend(data=map,pal=p,values=~HourMean,position = 'bottomright',title = date())
'R > crawling' 카테고리의 다른 글
네이버 실시간 검색어 크롤링하기 (0) | 2019.07.04 |
---|---|
기상청 자료 다운로드 (0) | 2019.05.22 |
XML package를 활용한 정적 크롤링 (0) | 2019.04.15 |
PlotGoogleMaps 사용해 AWS, ASOS 위치 나타내기 (0) | 2019.04.11 |
RSelenium을 활용하여 위경도 가져오기 (0) | 2019.03.21 |