setPath.js
17.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
const axios = require('axios');
const convert = require('xml-js');
const moment = require('moment');
const apiKey = '';
const reverseGeocoding = async (_x, _y) => {
try {
var result = await axios.get("http://apis.vworld.kr/coord2jibun.do?x=" + _x + "&y=" + _y + "&output=xml&epsg=epsg:4326&apiKey=");
result = convert.xml2js(result.data, {compact: true, spaces: 4});
result = JSON.parse(JSON.stringify(result));
var cityName = result.result.ADDR._cdata.split(" ")[0];
return cityName
} catch (e) {
console.error(e);
}
};
const subwayArrivalTime = async (stationID, wayCode) => {
try {
let today = new Date();
let day = today.getDay();//요일
let hours = today.getHours();//시
let minutes = today.getMinutes();//분
var result = await axios.get("https://api.odsay.com/v1/api/subwayTimeTable?lang=0&stationID=" + stationID + "&wayCode=" + wayCode + `&showExpressTime=1&apiKey=${apiKey}`);
} catch (e) {
console.error(e);
}
}
//subwayArrivalTime(216,1);
const seoulBusStationID = async (stationID) => {
try {
var result = await axios.get("https://api.odsay.com/v1/api/busStationInfo?lang=0&stationID=" + parseInt(stationID) + `&apiKey=${apiKey}`);
result = result.data;
var _stationID = result.result.arsID;
_stationID = _stationID.replace("-", "");
return _stationID;
} catch (e) {
console.error(e);
}
}
const seoulBusArrivalTime = async (stationID, busNum) => {
try {
var _stationID = await seoulBusStationID(stationID);
var result = await axios.get('http://ws.bus.go.kr/api/rest/stationinfo/getStationByUid?serviceKey' + _stationID);
//console.log(res.data);
result = convert.xml2js(result.data, {compact: true, spaces: 4});
result = JSON.parse(JSON.stringify(result));
//console.log(result.ServiceResult.msgBody.itemList);
var arrList = result.ServiceResult.msgBody.itemList;
//console.log(arrList);
for (var i = 0; i < arrList.length; i++) {
if (arrList[i].rtNm._text == busNum) {
var msg = new Object();
msg.msg1 = arrList[i].arrmsg1._text;
msg.msg2 = arrList[i].arrmsg2._text;
msg.timeInterval = 7;
return msg;
}
}
var msg = new Object();
msg.msg1 = "도착예정 없음";
msg.msg2 = "도착예정 없음";
msg.timeInterval = 7;
return msg;
} catch (e) {
console.error(e);
}
};
const gyeonggiLocalData = async (stationID, busID) => {
try {
var result = await axios.get("https://api.odsay.com/v1/api/busStationInfo?lang=0&stationID=" + parseInt(stationID) + `&apiKey=${apiKey}`);
result = result.data;
var stationLocalId = result.result.localStationID;
for (var i = 0; i < result.result.lane.length; i++) {
if (result.result.lane[i].busID == busID) {
busLocalId = result.result.lane[i].busLocalBlID;
return [stationLocalId, busLocalId];
}
}
} catch (e) {
console.error(e);
}
};
const gyeonggiBusArrivalTime = async (stationID, busID) => {
try {
var localData = await gyeonggiLocalData(stationID, busID);
var stationLocalID = localData[0];
var busLocalID = localData[1];
var result = await axios.get('http://openapi.gbis.go.kr/ws/rest/busarrivalservice/station?&stationId=' + stationLocalID);
result = convert.xml2js(result.data, {compact: true, spaces: 4});
result = JSON.parse(JSON.stringify(result));
var msg = new Object();
if (result.response.msgHeader.resultMessage._text == "결과가 존재하지 않습니다.") {
msg.msg1 = "도착 정보 없음";
msg.msg2 = "도착 정보 없음";
msg.timeInterval = 7;
console.log(msg);
return msg;
} else if (result.response.msgHeader.resultMessage._text == '정상적으로 처리되었습니다.') {
var arrList = result.response.msgBody.busArrivalList;
for (var i = 0; i < arrList.length; i++) {
var item = arrList[i];
if (item.routeId._text == busLocalID) {
msg.msg1 = item.predictTime1._text + "분 남음" + "(" + item.locationNo1._text + "개 역 전에 도착)";
if (parseInt(item.predictTime2._text) > 0) {
msg.msg2= msg.msg2 = item.predictTime2._text + "분 남음" + "(" + item.locationNo2._text + "개 역 전에 도착)";
msg.timeInterval = (parseInt(item.predictTime2._text) - parseInt(item.predictTime1._text))/60;//초
console.log("time interval,",msg.timeInterval);
} else {
msg.msg2="도착 정보 없음";
msg.timeInterval = parseInt(item.predictTime1._text);
}
console.log(msg);
return msg;//JSON타입 데이터
}
}
msg.msg1 = "도착 정보 없음";
msg.msg2 = "도착 정보 없음";
msg.timeInterval = 7;
console.log(msg);
return msg;
}
} catch (e) {
console.error(e);
}
}
function printSubwayInfo(subPath) {
console.log("-------지하철 이동---------");
console.log("소요시간:", subPath.time);
console.log("총 정거장수:", subPath.stationCnt);
console.log("지하철 정보:");
for (var i = 0; i < subPath.laneList.length; i++) {
console.log(subPath.laneList[i].name);
}
console.log("출발역:", subPath.startName);
console.log("도착역:", subPath.endName);
console.log("station 리스트");
console.log("------노선-------");
for (var i = 0; i < subPath.stationList.length; i++) {
console.log("|")
console.log(subPath.stationList[i]);
}
}
function printBusInfo(subPath) {
console.log("--------버스 이동----------");
console.log("소요시간:", subPath.time);
console.log(subPath.stationCnt, "개 정류장 이동");
for (var i = 0; i < subPath.arrivalInfo.length; i++) {
console.log("버스 번호:", subPath.arrivalInfo[i].busNo);
console.log(subPath.arrivalInfo[i].msg);
console.log("-----------------------");
}
//console.log("대체버스:",subPath.busNumberList);
console.log("출발역:", subPath.startName);
console.log("도착역:", subPath.endName);
console.log("-------노선--------");
for (var i = 0; i < subPath.stationList.length; i++) {
console.log("|");
console.log(subPath.stationList[i].stationName);
}
}
function printWalkInfo(subPath) {
console.log("--------도보 이동----------");
console.log("도보 이동 시간:", subPath.time);
console.log("도보 이동 거리:", subPath.distance);
}
const searchPubTransPath = async (sx, sy, ex, ey, avgSpeed, endTime) => {
//출발지점x좌표, 출발지점 y좌표, 도착지점 x좌표, 도착지점 y좌표, 보행자 평균 속도, 희망 도착시간(stringtype 2digit->hour, 2digit->min)
try {
var result = await axios.get("https://api.odsay.com/v1/api/searchPubTransPath?SX=" + sx + "&SY=" + sy + "&EX=" + ex + "&EY=" + ey + `&apiKey=${apiKey}`);
result = result.data;
var endTime = moment(endTime).format('HH:mm').split(':');
console.log(endTime);
var arrivalTime = parseInt(endTime[0]) * 60 + parseInt(endTime[1]);//endTime의 시각을 minute단위로 바꿈
if (result.result.searchType == 0) {//도시내 이동
var pathList = new Array();
for (var i = 0; i < result.result.path.length; i++) {
var path = result.result.path[i];
console.log("========================================================================================");
console.log("========================================================================================");
console.log(i + 1, "번째 경로");
var pathObj = new Object();
if (path.pathType == 1) {
pathObj.pathType = "지하철";
} else if (path.pathType == 2) {
pathObj.pathType = "버스";
} else {
pathObj.pathType = "지하철&버스";
}
pathObj.walkTime = 0;
pathObj.info = path.info;
pathObj.walkDistance = 0;
pathObj.busTimeInterval = 0;
console.log(pathObj.pathType);
console.log("총소요시간:", pathObj.info.totalTime);
console.log("비용:", pathObj.info.payment);
console.log("출발역:", pathObj.info.firstStartStation);
console.log("도착역:", pathObj.info.lastEndStation);
console.log("도보:", pathObj.info.totalWalk, " 이동");
console.log("총", pathObj.info.totalStationCount, "개 역 이동");
console.log("버스:", pathObj.info.busStationCount, "개 역 이동");
console.log("지하철:", pathObj.info.subwayStationCount, "개 역 이동");
console.log("=======경로 상세정보========");
pathObj.subPathList = new Array();
//console.log(pathObj.info);
for (var j = 0; j < path.subPath.length; j++) {
var subPath = new Object();
if (path.subPath[j].trafficType == 1) {//지하철 환승
subPath.trafficType = "지하철";
subPath.time = path.subPath[j].sectionTime;
subPath.stationCnt = path.subPath[j].stationCount;
subPath.laneList = path.subPath[j].lane;
subPath.startName = path.subPath[j].startName;
subPath.startID = path.subPath[j].startID;
subPath.endName = path.subPath[j].endName;
subPath.endID = path.subPath[j].endID;
subPath.stationList = new Array();
for (var k = 0; k < path.subPath[j].passStopList.stations.length; k++) {
subPath.stationList.push(path.subPath[j].passStopList.stations[k].stationName);
}
printSubwayInfo(subPath);
} else if (path.subPath[j].trafficType == 2) {//버스=>실시간 정보!
subPath.trafficType = "버스";
subPath.time = path.subPath[j].sectionTime;//총소요시간
subPath.stationCnt = path.subPath[j].stationCount;
subPath.startName = path.subPath[j].startName;
subPath.startID = path.subPath[j].startID;
subPath.endName = path.subPath[j].endName;
subPath.endID = path.subPath[j].endID;
subPath.stationList = path.subPath[j].passStopList.stations;
subPath.arrivalInfo = new Array();
var cityName = await reverseGeocoding(path.subPath[j].passStopList.stations[0].x, path.subPath[j].passStopList.stations[0].y);
if (cityName == "서울특별시") {//stationID와 busNo로 도착 예정시간 추출
//seoulBusArrivalTime(path.subPath[j].passStopList.stations[0].stationID,busNumberList);
for (var a = 0; a < path.subPath[j].lane.length; a++) {
var busArrivalInfoItem = new Object();
var _msg = await seoulBusArrivalTime(subPath.startID, path.subPath[j].lane[a].busNo);
busArrivalInfoItem.busID = path.subPath[j].lane[a].busID;
busArrivalInfoItem.busNo = path.subPath[j].lane[a].busNo;
busArrivalInfoItem.msg = _msg;
subPath.arrivalInfo.push(busArrivalInfoItem);
pathObj.busTimeInterval = _msg.timeInterval; //초->분단위로 바꿈
console.log(_msg.timeInterval);
if (a == 2)//버스 최대 3개까지만 출력시킴
break;
}
printBusInfo(subPath);
} else if (cityName == "경기도") {
for (var a = 0; a < path.subPath[j].lane.length; a++) {
var busArrivalInfoItem = new Object();
var _msg = await gyeonggiBusArrivalTime(subPath.startID, path.subPath[j].lane[a].busID);
busArrivalInfoItem.busID = path.subPath[j].lane[a].busID;
busArrivalInfoItem.busNo = path.subPath[j].lane[a].busNo;
busArrivalInfoItem.msg = _msg;
subPath.arrivalInfo.push(busArrivalInfoItem);
console.log("----------메시지메시지-----------", _msg);
pathObj.busTimeInterval = _msg.timeInterval;
if (a == 2)//버스 최대 3개까지만 출력
break;
}
printBusInfo(subPath);
} else {
//서울, 경기를 제외한 지역은 실시간 정보 제공 불가
printBusInfo(subPath);
}
} else {//도보이동
pathObj.walkTime += path.subPath[j].sectionTime;
pathObj.walkDistance += path.subPath[j].distance;
subPath.trafficType = "도보";
subPath.time = path.subPath[j].sectionTime;
subPath.distance = path.subPath[j].distance;
printWalkInfo(subPath);
}
pathObj.subPathList.push(subPath);
}
pathList.push(pathObj);
var optTotalTime = pathObj.info.totalTime - pathObj.walkTime + pathObj.walkDistance / avgSpeed;//사용자의 보행속도를 고려한 이동시간
console.log("arrrrrrrrrrr",arrivalTime)
var departureTime1 = parseInt(arrivalTime - optTotalTime);
if(departureTime1<0){
departureTime1+=1440;
}
if(path.pathType==1){//지하철의 경우 시간간격 4분으로
pathObj.busTimeInterval=4;
}
var departureTime2 = parseInt(arrivalTime - optTotalTime - pathObj.busTimeInterval);
if(departureTime2<0){
departureTime2+=1440;
}
console.log(pathObj.busTimeInterval);
console.log("dp1",departureTime1);
console.log("dp2",departureTime2);
console.log("pathType",path.pathType);
console.log("departure time 1",departureTime1);
console.log("departure time 2",departureTime2);
var hour1 = departureTime1 / 60;
var min1 = departureTime1 % 60;
var hour2 = departureTime2 / 60;
var min2 =departureTime2 % 60;
pathObj.info.hour1=parseInt(hour1).toString();
pathObj.info.min1=parseInt(min1).toString();
pathObj.info.hour2=parseInt(hour2).toString();
pathObj.info.min2=parseInt(min2).toString();
console.log(pathObj.info.hour1,"시 ",pathObj.info.min1,"분");
console.log(pathObj.info.hour2,"시 ",pathObj.info.min2,"분");
if (i == 2)//경로 3개까지만 출력
break;
}
return {pathList};
} else if (result.result.searchType == 1) {//도시간 이동
var path = new Object();
path.trainList = new Array();
for (var j = 0; j < result.result.trainRequest.count; j++) {
var train = new Object();
train = result.result.trainRequest.OBJ[j];
path.trainList.push(train);
if (j == 2)
break;
}
path.exBusList = new Array();
for (var j = 0; j < result.result.exBusRequest.count; j++) {
var exBus = new Object();
exBus = reuslt.result.exBusRequest.OBJ[j];
path.exBusList.push(exBus);
if (j == 2)
break;
}
path.outBusList = new Array();
for (var j = 0; j < result.result.outBusRequest.count; j++) {
var outBus = new Object();
outBus = result.result.outBusRequest.OBJ[j];
path.outBusList.push(outBus);
if (j == 2)
break;
}
return path;
}
} catch (e) {
console.error(e);
}
};
searchPubTransPath(126.999451,37.266670, 126.986990,37.541386,60,"00:10");
module.exports = searchPubTransPath;