95 lines
2.9 KiB
PHP
95 lines
2.9 KiB
PHP
<?php
|
|
|
|
function area_to_area($area1, $area2, $date1, $date2, $t, $c='#FF0000') {
|
|
global $pgconn;
|
|
$data = [];
|
|
$q = "SELECT * FROM geofence_area WHERE id=${area1}";
|
|
$r = pg_query($pgconn, $q);
|
|
$area1i = pg_fetch_assoc($r);
|
|
$boundaries = json_decode($area1i["boundaries"], true);
|
|
$polygonCoords = [];
|
|
|
|
foreach ($boundaries["polygon"] as $coord) {
|
|
$polygonCoords[] = "$coord[0] $coord[1]";
|
|
}
|
|
$polygonCoordsString1 = implode(",", $polygonCoords);
|
|
|
|
$q = "SELECT * FROM geofence_area WHERE id=${area2}";
|
|
$r = pg_query($pgconn, $q);
|
|
$area2i = pg_fetch_assoc($r);
|
|
$boundaries = json_decode($area2i["boundaries"], true);
|
|
$polygonCoords = [];
|
|
foreach ($boundaries["polygon"] as $coord) {
|
|
$polygonCoords[] = "$coord[0] $coord[1]";
|
|
}
|
|
$polygonCoordsString2 = implode(",", $polygonCoords);
|
|
|
|
$q = "SELECT a.travel_date,a.cost";
|
|
$q.= ",b.latitude AS location_start_lat,b.longitude AS location_start_lng";
|
|
$q.= ",c.latitude AS location_end_lat,c.longitude AS location_end_lng";
|
|
$q.= " FROM quotes a, address b, address c";
|
|
$q.= " WHERE a.cost>0 AND a.transport_provider_id=${t}";
|
|
$q.= " AND b.id=a.location_start_id AND c.id=a.location_end_id";
|
|
$q.= " AND a.travel_date>'${date1} 00:00' AND a.travel_date<'${date2} 23:59'";
|
|
$q.= " AND ST_Contains(ST_GeomFromText('POLYGON((${polygonCoordsString1}))', 4326), b.geometry::geometry)";
|
|
$q.= " AND ST_Contains(ST_GeomFromText('POLYGON((${polygonCoordsString2}))', 4326), c.geometry::geometry)";
|
|
$q.= " ORDER BY a.travel_date";
|
|
|
|
$r = pg_query($pgconn, $q);
|
|
while ($f=pg_fetch_assoc($r)) {
|
|
$f['origin'] = $area1i['name'];
|
|
$f['destination'] = $area2i['name'];
|
|
$f['c'] = $c;
|
|
$f['time'] = strtotime($f['travel_date']);
|
|
$data[] = $f;
|
|
}
|
|
return [$data,$area1i["name"]." to ".$area2i["name"],$boundaries["polygon"]];
|
|
}
|
|
|
|
function trend_line($regression1, $data) {
|
|
$i = 0;
|
|
$pub1 = array();
|
|
$xs1 = [];
|
|
$ys1 = [];
|
|
$ys2 = [];
|
|
foreach ($data as $f) {
|
|
$t = $f['time'];
|
|
$pub1[$t] = array($t,$f['cost']);
|
|
$xs1[] = [$t];
|
|
$ys1[] = $f['cost'];
|
|
$i++;
|
|
}
|
|
if ($i>0) {
|
|
$regression1->train($xs1,$ys1);
|
|
}
|
|
foreach ($pub1 as $key=>$f) {
|
|
$y = $regression1->predict( [$f[0]] );
|
|
$f[2] = $y;
|
|
$pub1[$key] = $f;
|
|
}
|
|
return [$pub1,$xs1,$ys2];
|
|
}
|
|
|
|
function variation($data, $i, $f, $t) {
|
|
global $dMin, $dMax, $tVar, $tVar0;
|
|
$tVar[] = $f['cost'];
|
|
$trend = $t[$f['time']][2];
|
|
$delta = $f['cost'] - $trend;
|
|
$variation = stats_variance($tVar);
|
|
if ($delta>$dMax) $dMax = $delta;
|
|
if ($delta<$dMin) $dMin = $delta;
|
|
$tVar0[$f['time']] = $variation;
|
|
/* {
|
|
"travel_date":"2020-05-10 14:54:04+00",
|
|
"cost":"12",
|
|
"location_start_lat":"1.319292",
|
|
"location_start_lng":"103.9126091",
|
|
"location_end_lat":"1.316269",
|
|
"location_end_lng":"103.977615",
|
|
"origin":"Central East - Eunos",
|
|
"destination":"Far East - Changi",
|
|
"c":"#5555ff"} */
|
|
//if ($i<5) error_log(json_encode($t));
|
|
return [$trend, $delta, $variation];
|
|
}
|