バスロケーションデータ取得のためのスクリプト例

使い方

コマンドライン引数を次のように与える。(路線ID 緯度 経度 始発時刻 終点到着時刻 取得の年月日範囲 の順)

php ./getRawLocationData.php 3 35.6679438115537 138.569540083408 072500 073300 20170729 20170805

スクリプトgetRawLocationData.phpの内容

<?php
/*
This is a script to
get bus location data from "BusLocationData1ope.php" and save these data successively.

commandline args
    routeID, bus start time, bus arrival time at the terminal, interval for extracting(start and arrival time at the terminal)

The arrival time should be the scheduled time and expected enough delay time
"begin date" and "end date" are given in the form "yyyy/mm/dd"

The command line arguments can be obtained from
   http://8tops.yamanashi.ac.jp/~toyoki/buslog2016/getLocationsYamanashi.php
*/

$routeID = $argv[1];
$lat = $argv[2];
$lon= $argv[3];
$start_time = $argv[4];
$arrival_time = $argv[5];
$begin_date  = $argv[6];
$end_date = $argv[7];

$base_url = 'http://8tops.yamanashi.ac.jp/~toyoki/buslog/BusLocation1ope.php?'
	 .  'sLat=' . $lat . "&sLon=" . $lon . "&routeID=" .$routeID;

$startSec =  strtotime($begin_date);
$endSec = strtotime($end_date);
$sec1 = 60*60*24;
for($sec = $startSec; $sec<=$endSec; $sec+=$sec1) {  // $sec is the unixtime of the day
    // start and end are given in the form 'YmdHis'
    $dateStr =  "&datetime=" . date("Ymd", $sec) . str_replace(":", "", $start_time) .  "&endDatetime=" . date("Ymd", $sec) . str_replace(":", "", $arrival_time);

    // get data and store them to the file
    $url = $base_url . $dateStr . "&da=5&aa=40";

    // get json data
    $json = file_get_contents($url);
    $dataArray = json_decode($json, true);
    if($dataArray== null) continue;

    $FP = fopen("busLoc_" . $routeID . "_" . $start_time . "_" . date("Ymd", $sec)
	      .  ".csv", "w");
    foreach($dataArray as $d) {
	fprintf($FP, "%s, %f, %f\n", $d['time'], $d['lat'], $d['lon']);
    }
    fclose($FP);
}


?>

  

pythonの場合

'''

jsonデータをリクエストするurlの例:
http://8tops.yamanashi.ac.jp/~toyoki/buslog/BusLocation1ope.php?sLat=35.6679438115537&sLon=138.569540083408&routeID=3&datetime=20170804072500&endDatetime=20170804073300&da=5&aa=40

'''
import urllib
import sys
from datetime import datetime, date, timedelta
import json

# コマンドライン引数
routeID = sys.argv[1]
lat = sys.argv[2]
lon= sys.argv[3]
start_time = sys.argv[4]
arrival_time = sys.argv[5]
begin_date  = sys.argv[6]
end_date = sys.argv[7]

base_url = 'http://8tops.yamanashi.ac.jp/~toyoki/buslog/BusLocation1ope.php?' \
           +  'sLat='  +  lat + "&sLon=" +  lon +  "&routeID="  + routeID

startSec =  datetime.strptime(begin_date, "%Y%m%d");
endSec = datetime.strptime(end_date, "%Y%m%d");

sec = startSec
while sec <= endSec:
    dateStr =  "&datetime=" + datetime.strftime(sec, "%Y%m%d") + start_time.replace(":", "") \
               + "&endDatetime=" + datetime.strftime(sec, "%Y%m%d") \
                                 + arrival_time.replace(":", "")
    url = base_url + dateStr + "&da=5&aa=40"
    html =  urllib.request.urlopen(url)
    jsonData = json.loads(html.read().decode('utf-8'))

    if jsonData is None:
        sec = sec + timedelta(days=1)
        continue

    # ファイル出力
    filename = "busLoc_" + routeID + "_" + start_time \
               +  "_"  + datetime.strftime(sec, "%Y%m%d") \
               +  ".csv"
    fp = open(filename, "w")
    for d in jsonData:
        # fp.write(d['time'] + "," + d['lat'] + "," + d['lon'] +  "\n")
        fp.write("{0},{1:f},{2:f}\n".format(d['time'], float(d['lat']), float(d['lon'])))
    fp.close()

    sec = sec + timedelta(days=1) # 次の日