Files
GEOVizor_PHP/monitoring/pscripts/routes.php
2023-11-07 19:51:49 +06:00

230 lines
7.8 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
//Выбираю маршруты в GeoJSON и отправляю клиенту
require_once("../../monitoring/config.php");
$db = connectToDB();
//$db->exec('SET NAMES utf8');
//$db->exec("SET time_zone = '+00:00'");
function sendError($msg)
{
$json='{"errorCode":1,"errorMessage":'.json_encode($msg,JSON_UNESCAPED_UNICODE).'}';
header('Content-Type: application/json');
echo $json;
exit;
}
$fn=0;
if(isset($_GET['fn'])) { $fn=$_GET['fn']; }
if($fn=='0') //Вернуть список маршрутов
{
$sql = '
SELECT
id
,name
,coalesce((select c.name from main.companies c join main.routes_companies rc on c.id=rc.company_id where rc.del=false and c.del=false and rc.route_id=r.id and c.company_type_id=2 limit 1),\'\') as station
,coalesce((select c.name from main.companies c join main.routes_companies rc on c.id=rc.company_id where rc.del=false and c.del=false and rc.route_id=r.id and c.company_type_id=1 limit 1),\'\') as carriers
,ST_NPoints(geom) as count
,round(ST_Length_Spheroid(geom,\'SPHEROID["WGS 84",6378137,298.257223563]\')/1000) as length
,ST_y(ST_StartPoint(geom)) lat
,ST_X(ST_StartPoint(geom)) lon
FROM
main.routes r
where del=false
ORDER BY name;
';
try
{
$res = $db->query($sql);
}catch (Exception $e)
{ echo $e->getMessage();
$res = null;
}
//Массив объектов
$json ='[';
//Перебираю и последовательно отправляю не отправленные пломбы
while ($row = $res->fetch(PDO::FETCH_ASSOC))
{
$json .="\n";
$json .='{';
$json .='"id":"'.$row['id']."\",\n";
$json .='"name":'.json_encode($row['name'],JSON_UNESCAPED_UNICODE).",\n";
$json .='"station":'.json_encode($row['station'],JSON_UNESCAPED_UNICODE).",\n";
$json .='"carriers":'.json_encode($row['carriers'],JSON_UNESCAPED_UNICODE).",\n";
$json .='"count":"'.$row['count']."\",\n";
$json .='"length":"'.$row['length']."\",\n";
$json .='"lat":'.json_encode($row['lat']).",\n";
$json .='"lon":'.json_encode($row['lon']).",\n";
//Расписание для маршрута
$sql='select
ts.id,
CASE WHEN ts.direction=true THEN \'Прямое\' WHEN ts.direction=false THEN \'Обратное\' ELSE \'Не задано\' END as direction,
ts.time,
coalesce(c.name,\'\') as carrier
from
main.trips_schedules ts
left join main.companies c on c.id=ts.company_id
where
ts.route_id='.$row['id'].'
order by
ts.direction desc,ts.time';
try
{
$res2 = $db->query($sql);
}catch (Exception $e)
{ echo $e->getMessage();
$res2 = null;
}
$json .='"schedules":['."\n";
while ($row2 = $res2->fetch(PDO::FETCH_ASSOC))
{
$json .="{";
$json .='"direction":'.json_encode($row2['direction'],JSON_UNESCAPED_UNICODE).',';
$json .='"time":'.json_encode($row2['time'],JSON_UNESCAPED_UNICODE).',';
$json .='"carrier":'.json_encode($row2['carrier'],JSON_UNESCAPED_UNICODE);
$json .="},";
}
if($json[strlen($json) - 1]==','){
$json=substr ( $json , 0, strlen($json)-1 );
}
$json .="],\n";
//Контрольные точки для маршрута
$sql='select
rc.id,
CASE WHEN rc.direction=true THEN \'Прямое\' WHEN rc.direction=false THEN \'Обратное\' ELSE \'Не задано\' END as direction,
rc.time,
coalesce(rc.name,\'\') as name,
ST_X(ST_Centroid(rc.geom)) lon,
ST_Y(ST_Centroid(rc.geom)) lat
from
main.routes_checkpoints rc
where
rc.route_id='.$row['id'].'
order by
rc.direction desc,rc.time';
try
{
$res2 = $db->query($sql);
}catch (Exception $e)
{ echo $e->getMessage();
$res2 = null;
}
$json .='"checkpoints":['."\n";
while ($row2 = $res2->fetch(PDO::FETCH_ASSOC))
{
$json .="{";
$json .='"id":'.json_encode($row2['id'],JSON_UNESCAPED_UNICODE).',';
$json .='"direction":'.json_encode($row2['direction'],JSON_UNESCAPED_UNICODE).',';
$json .='"time":'.json_encode($row2['time'],JSON_UNESCAPED_UNICODE).',';
$json .='"name":'.json_encode($row2['name'],JSON_UNESCAPED_UNICODE).",\n";
$json .='"lat":'.json_encode($row2['lat']).",\n";
$json .='"lon":'.json_encode($row2['lon'])."\n";
$json .="},";
}
if($json[strlen($json) - 1]==','){
$json=substr ( $json , 0, strlen($json)-1 );
}
$json .="]\n";
$json .='},';
}
if($json[strlen($json) - 1]==','){
$json=substr ( $json , 0, strlen($json)-1 );
}
$json .=']';
header('Content-Type: application/json');
echo $json;
}else
if($fn=='1') //Вернуть GEOJSON по ID записи
{
if(!isset($HTTP_RAW_POST_DATA))
{ $HTTP_RAW_POST_DATA = file_get_contents("php://input");
}
$object = json_decode($HTTP_RAW_POST_DATA);
if($object->id=='') $object->id='null';
/*if($object->cnumber=='') $object->cnumber='null'; else $object->cnumber='"%'.$object->cnumber.'%"'; //Гос. номер
if($object->tnumber=='') $object->tnumber='null'; else $object->tnumber='"%'.$object->tnumber.'%"'; //Номер ТД
if($object->country_seal_begin=='') $object->country_seal_begin='null'; //Страна пломб.
if($object->country_seal_end=='') $object->country_seal_end='null'; //Страна распломб.*/
//Выбираю геометрию
$sql="select ST_AsGeoJSON(geom,6,0) as geom from main.routes where id=".$object->id.";";
try
{
$res = $db->query($sql);
}catch (Exception $e)
{ echo $e->getMessage();
$res = null;
}
$json="{}";
if($res!=NULL && $res->rowCount()>0)
{
while ($row = $res->fetch(PDO::FETCH_NUM))
{
$json=$row[0];
}
}
header('Content-Type: application/json');
echo $json;
exit;
}else if($fn=='2') //Вернуть GEOJSON по ID записи
{
if(!isset($HTTP_RAW_POST_DATA))
{ $HTTP_RAW_POST_DATA = file_get_contents("php://input");
}
$object = json_decode($HTTP_RAW_POST_DATA);
if(!property_exists($object,'id') or $object->id=='') $object->id='null';
//Выбираю геометрию
$sql="select ST_AsGeoJSON(geom,6,0) as geom from main.routes_checkpoints where id=".$object->id.";";
try
{
$res = $db->query($sql);
}catch (Exception $e)
{
sendError($e->getMessage());
}
$json="{}";
if($res!=NULL && $res->rowCount()>0)
{
while ($row = $res->fetch(PDO::FETCH_NUM))
{
$json=$row[0];
}
}
header('Content-Type: application/json');
echo $json;
exit;
}else if($fn=='3') //Удалить маршрут
{
if(!isset($HTTP_RAW_POST_DATA))
{ $HTTP_RAW_POST_DATA = file_get_contents("php://input");
}
$object = json_decode($HTTP_RAW_POST_DATA);
if($object->id=='') $object->id='null';
$sql="update main.routes r set del=true where id=".$object->id;
try
{
$res = $db->query($sql);
}catch (Exception $e)
{
sendError($e->getMessage());
}
$json="{}";
header('Content-Type: application/json');
echo $json;
exit;
}else
{
sendError("Неизвестная функция!");
}