[공공데이터 포털] 버스 정류소 API 가져오기
버스 정류소 API를 받으시려면 공공데이터 포털에서 버스 정류소를 검색!
먼저 버스 정류소 API를 받습니다.
API를 받은 뒤 아래와 같이 코딩합니다.
url='http://openapi.tago.go.kr/openapi/service/BusSttnInfoInqireService/getSttnNoList?serviceKey='
library(xml2);library(rvest);library(RCurl);library(XML)
# 도시 코드를 조회합니다.
url='http://openapi.tago.go.kr/openapi/service/BusSttnInfoInqireService/getCtyCodeList?serviceKey='
(raw.data <- xmlTreeParse(paste0(url,api), useInternalNodes = TRUE, encoding = "utf-8"))
#조회한 도시코드를 활용
url='http://openapi.tago.go.kr/openapi/service/BusSttnInfoInqireService/getSttnNoList?serviceKey='
raw.data <- xmlTreeParse(paste0(url,api,'&cityCode=도시코드&numOfRows=1'), useInternalNodes = TRUE, encoding = "utf-8")
rootNode <- xmlRoot(raw.data)
n=as.character(xmlSApply(rootNode[[2]][['totalCount']],xmlValue))
(raw.data <- xmlTreeParse(paste0(url,api,'&cityCode=37060&numOfRows=',n), useInternalNodes = TRUE, encoding = "utf-8"))
rootNode <- xmlRoot(raw.data)
items <- rootNode[[2]][['items']]
data=NULL
for(i in 1:xmlSize(rootNode[[2]][['items']])){
items_temp=xmlSApply(items[[i]],xmlValue)
lat=as.numeric(items_temp[[1]])
lon=as.numeric(items_temp[[2]])
id=items_temp[[3]]
nm=items_temp[[4]]
no=as.numeric(items_temp[[5]])
data=rbind(data,data.frame(lat,lon,id,nm,no))
}
data
#버스 노선정보 또한 같은방법으로
url='http://openapi.tago.go.kr/openapi/service/BusRouteInfoInqireService/getRouteNoList?serviceKey='
(raw.data <- xmlTreeParse(paste0(url,api,'&cityCode=37060&numOfRows=1'), useInternalNodes = TRUE, encoding = "utf-8"))
rootNode <- xmlRoot(raw.data)
(raw.data <- xmlTreeParse(paste0(url,api,'&cityCode=37060&numOfRows=',xmlSApply(rootNode[[2]][['totalCount']],xmlValue)), useInternalNodes = TRUE, encoding = "utf-8"))
rootNode <- xmlRoot(raw.data)
items <- rootNode[[2]][['items']]
xmlSize(rootNode[[2]][['items']])
data2=NULL
for(i in 1:xmlSize(rootNode[[2]][['items']])){
items_temp=xmlSApply(items[[i]],xmlValue)
df=data.frame(t(items_temp),stringsAsFactors = F)
df$endvehicletime=as.numeric(df$endvehicletime)
df$startvehicletime=as.numeric(df$startvehicletime)
data2=rbind(data2,df)
}
data2
#버스 경유지
url='http://openapi.tago.go.kr/openapi/service/BusRouteInfoInqireService/getRouteAcctoThrghSttnList?serviceKey='
data_list=list()
for(id in unique(data2$routeid)){
(raw.data <- xmlTreeParse(paste0(url,api,'&numOfRows=1&cityCode=37060&routeId=',id), useInternalNodes = TRUE, encoding = "utf-8"))
rootNode <- xmlRoot(raw.data)
n=xmlSApply(rootNode[[2]][['totalCount']],xmlValue)
(raw.data <- xmlTreeParse(paste0(url,api,'&numOfRows=',n,'&cityCode=37060&routeId=',id), useInternalNodes = TRUE, encoding = "utf-8"))
rootNode <- xmlRoot(raw.data)
items <- rootNode[[2]][['items']]
data3=NULL
for(i in 1:xmlSize(rootNode[[2]][['items']])){
items_temp=xmlSApply(items[[i]],xmlValue)
df=data.frame(t(items_temp),stringsAsFactors = F)
df$gpslati=as.numeric(df$gpslati)
df$gpslong=as.numeric(df$gpslong)
data3=rbind(data3,df)
}
data_list[[id]]=data3
}
library(data.table)
data3=rbindlist(data_list)
data3
'R' 카테고리의 다른 글
[ggmap] pie chart 그리기 예제 (0) | 2019.11.26 |
---|---|
ddply와 setDT 비교 (0) | 2019.11.11 |
숫자가 너무커서 제대로 안불러와질 때 (0) | 2019.09.16 |
apply 와 sapply의 차이 (0) | 2019.07.15 |
문자열 다루기와 텍스트 마이닝 (0) | 2019.07.07 |