# 노선 번호에 대한 노선 ID 확인 install.packages("XML") library(XML) library(dplyr) library(sf) library(mapview) busRtNm <- "402" API_key <- "OckdUErbAGthsbx6VYITeQ9xzEpUow%2F25CZWigZoPo3JVEW%2FU%2Bpd%2B2ZiN06BuDRn3Zjfh21AHXvgg8cWnuP9Hw%3D%3D" url <- paste("http://ws.bus.go.kr/api/rest/busRouteInfo/getBusRouteList?ServiceKey=", API_key, "&strSrch=", busRtNm,sep="") xmefile <- xmlParse(url) xmlRoot(xmefile) # p.252 df <- xmlToDataFrame(getNodeSet(xmefile, "//itemList")) head(df) df_busRoute <- subset(df, busRouteNm==busRtNm) df_busRoute df_busRoute$busRouteId # p.253 # 노선 ID에 대한 버스 실시간 위치 정보 확인 API_key <- "OckdUErbAGthsbx6VYITeQ9xzEpUow%2F25CZWigZoPo3JVEW%2FU%2Bpd%2B2ZiN06BuDRn3Zjfh21AHXvgg8cWnuP9Hw%3D%3D" url <- paste("http://ws.bus.go.kr/api/rest/buspos/getBusPosByRtid?ServiceKey=", API_key, "&busRouteId=", "100100063", sep="") xmefile <- xmlParse(url) xmlRoot(xmefile) # p.254 df <- xmlToDataFrame(getNodeSet(xmefile, "//itemList")) df gpsX <- as.numeric(as.character(df$gpsX)) gpsY <- as.numeric(as.character(df$gpsY)) gc <- data.frame(lon=gpsX, lat=gpsY) gc gc <- st_as_sf(gc, coords = c("lon", "lat"), crs = 4326) mapview(gc, hide =TRUE) ######################