westdc-zf1/application/default/controllers/VisualController.php

763 lines
16 KiB
PHP
Raw Normal View History

<?php
class VisualController extends Zend_Controller_Action
{
function preDispatch()
{
$this->view->config = Zend_Registry::get('config');
$this->db=Zend_Registry::get('db');
$this->dbh = new PDO("pgsql:dbname=qinghaihu;host=localhost", "gis", "gispassword" );
$auth = Zend_Auth::getInstance();
if($auth->hasIdentity())
{
$user = $auth->getIdentity();
$this->uid = $user->id;
}else{
$this->_redirect('/account/login?href=/data/visual');
}
}
function indexAction()
{
}
/****************************************************************
气象数据
*****************************************************************/
/*
* getMeteorologyValueByStation 按站点查询变量
*
* param int $sid 站点编号
* param string $field 变量类型
*
* return array
*/
function getMeteorologyValueByStation($sid,$field)
{
$sql = "select id,station_id as sid,date,$field as v from daily_meteorological_data where station_id=$sid ORDER BY date ASC";
$sth = $this->dbh->prepare($sql);
$sth->execute();
$temp = $sth->fetchAll();
$dataSet = array();
foreach ($temp as $k=>$v)
{
$dataSet[] = array(strtotime($v['date'])*1000,$v['v']);
}
unset($temp);
return $dataSet;
}// getMeteorologyValueByStation 按站点查询变量
/*
* createMeteorologyDataSet 创建气象数据
*
* param int $sid 站点id
* param string $valuetype 请求变量类型
* param String $labe 单位
* param int $round 数据精确度(小数点后位数)
*
* return array
*/
function createMeteorologyDataSet($sid,$valuetype,$label,$round=1)
{
if(empty($label))
{
$label="";
}
$varField = array(
'temperature' => 'avg_air_temperature', //日平均气温
'windspeed' => 'avg_wind_speed', //日平均风速
'precipitation' => 'sum_precipitation', //日合计降水量
'sunshine' => 'sunshine_hours' //日照小时数
);
$data = array(
"name"=>"站点:$sid",
"data"=>$this->getMeteorologyValueByStation($sid,$varField[$valuetype]),
"tooltip"=>array(
"valueDecimals"=> $round,
"valueSuffix"=> $label
)
);
return $data;
}// createMeteorologyDataSet 创建气象数据
/*
* getMeteorologyDataByStationId 获得变量值
*
* param string $valuetype 变量类型
* param string $label 单位
* param int $sid 可选,为空则查询所有站点
* param int $round 数值精确度,小数点后位数
*
* return array
*/
function getMeteorologyDataByStationId($valuetype,$label,$sid=0,$round=1)
{
if(empty($sid))
{
$sql = "select station_id as sid from daily_meteorological_data group by station_id";
$sth = $this->dbh->prepare($sql);
$sth->execute();
$stations = $sth->fetchAll();
$data = array();
foreach ($stations as $v)
{
$data[] = $this->createMeteorologyDataSet($v['sid'],$valuetype,$label,$round);
}
return $data;
}
else
{
return $this->createMeteorologyDataSet($sid,$valuetype,$label,$round=1);
}
}//getMeteorologyDataByStationId 获得气象数据变量值
/**********************************************************
水文数据
***********************************************************/
/*
* getHydrologyValueByStation 按站点查询变量
*
* param int $sid 站点编号
* param string $table 变量类型
*
* return array
*/
function getHydrologyValueByStation($sid,$t)
{
$field = array(); //查询字段
$order = array();
if(is_array($t['time']))
{
foreach ($t['time'] as $v)
{
$field[] = $v;
$order[] = $v.' ASC';
}
}else{
$field[] = $t['time'];
$order[] = $t['time'].' ASC';
}
$t['field'] .= " as v";
$field[] = $t['field'];
$field[] = $t['sf'];
$field = join(",",$field); //拼接
$order = join(',',$order);
$sql = "select $field from {$t['table']} where {$t['sf']}='$sid' ORDER BY $order";
$sth = $this->dbh->prepare($sql);
$sth->execute();
$temp = $sth->fetchAll();
$dataSet = array();
foreach ($temp as $k=>$v)
{
$time = 0;
if(!empty($v['dt']))
{
$time = strtotime($v['dt']);
}
if(!empty($v['yr']) && !empty($v['mth']))
{
$time = strtotime($v['yr'].'-'.$v['mth'].'-'.'01 00:00:00');
}
if(!empty($v['yr']) && empty($v['mth']))
{
$time = strtotime($v['yr'].'-01-01 00:00:00');
}
$dataSet[] = array($time*1000,$v['v']);
}
unset($temp);
return $dataSet;
}// getHydrologyValueByStation 按站点查询变量
/*
* createHydrologyDataSet 创建水文数据
*
* param int $sid 站点编号
* param string $valuetype 变量类型
* param string $label 单位
* param int $round 数值精确度,小数点后位数
*
* return array
*/
function createHydrologyDataSet($sid,$valuetype,$label,$round){
if(empty($label))
{
$label='';
}
$varTable = array(
"dp"=>array(
"table"=>"hy_dp_c", //表名
"field"=>"p", //值字段
"sf"=>"stcd", //站点id字段
"time"=>array("dt") //时间字段,数组
), //日降水量
"mtp"=>array(
"table"=>"hy_mtp_e",
"field"=>"p",
"sf"=>"stcd",
"time"=>array('yr','mth')
), //月降水量
"yrp"=>array(
"table"=>"hy_yrp_f",
"field"=>"p",
"sf"=>"stcd",
"time"=>array("yr")
), //年降水量
"dq"=>array(
"table"=>"hy_dq_c",
"field"=>"avq",
"sf"=>"stcd",
"time"=>array("dt")
), //日平均流量
"mtq"=>array(
"table"=>"hy_mtq_e",
"field"=>"avq",
"sf"=>"stcd",
"time"=>array('yr','mth')
), //月平均流量
"yrq"=>array(
"table"=>"hy_yrq_f",
"field"=>"avq",
"sf"=>"stcd",
"time"=>array('yr')
) //年平均流量
);//$varTable
$data = array(
"name"=>"站点:$sid",
"data"=>$this->getHydrologyValueByStation($sid,$varTable[$valuetype]),
"tooltip"=>array(
"valueDecimals"=> $round,
"valueSuffix"=> $label
)
);
return $data;
}//createHydrologyDataSet 创建水文数据
/*
* getHydrologyDataByStationId 获得水文数据变量值
*
* param string $valuetype 变量类型
* param string $label 单位
* param int $sid 站点编号,默认为空,空值将列出所有站点变量值
* param int $round 数值精确度,小数点后位数
*
* return array
*/
function getHydrologyDataByStationId($valuetype,$label,$sid=0,$round=1){
if(empty($sid))
{
//降水
$p = array('dp','mtp','yrp');
$q = array('dq','mtq','yrq');
if(in_array($valuetype,$p))
{
$sql = "select stcd from hy_dp_c group by stcd";
}
if(in_array($valuetype,$q))
{
$sql = "select stcd from hy_dq_c group by stcd";
}
$sth = $this->dbh->prepare($sql);
$sth->execute();
$stations = $sth->fetchAll();
$data = array();
foreach ($stations as $v)
{
$data[] = $this->createHydrologyDataSet($v['stcd'],$valuetype,$label,$round);
}
return $data;
}//所有站点
if(is_array($sid))
{
foreach($sid as $v)
{
}
}//多个指定站点
else
{
}//单个站点
}
//getHydrologyDataByStationId() 获得水文数据变量值
/****************************
环境监测数据
*****************************/
/*
* createAmbientDataSet 创建环境监测数据
*
* param int $sid 站点编号、名称
* param string $valuetype 变量类型
* param string $label 单位
* param int $round 数值精确度,小数点后位数
*
* return array
*/
function createAmbientDataSet($sid,$valuetype,$label,$round=4){
if(empty($label))
{
$label='';
}
$data = array(
"name"=>"站点:$sid",
"data"=>$this->getAmbientValueByStation($sid,$valuetype),
"tooltip"=>array(
"valueDecimals"=> $round,
"valueSuffix"=> $label
)
);
return $data;
}//createAmbientDataSet 创建环境监测数据的数据
/*
* getAmbientValueByStation 按站点查询变量
*
* param int $sid 站点编号
* param string $table 变量类型
*
* return array
*/
function getAmbientValueByStation($sid,$t)
{
$field = array(); //查询字段
$field[] = 'id';
$field[] = '"position"';
$field[] = 'dt';
$field[] = $t.' as v';
$field = join(",",$field); //拼接
$sql = "select $field from ambient_air where \"position\"='$sid' ORDER BY dt ASC";
$sth = $this->dbh->prepare($sql);
$sth->execute();
$temp = $sth->fetchAll();
$dataSet = array();
foreach ($temp as $k=>$v)
{
$time = 0;
if(!empty($v['dt']))
{
$time = strtotime($v['dt']);
}
if(!empty($v['yr']) && !empty($v['mth']))
{
$time = strtotime($v['yr'].'-'.$v['mth'].'-'.'01 00:00:00');
}
if(!empty($v['yr']) && empty($v['mth']))
{
$time = strtotime($v['yr'].'-01-01 00:00:00');
}
$dataSet[] = array($time*1000,$v['v']);
}
unset($temp);
return $dataSet;
}// getAmbientValueByStation 按站点查询变量
/*
* getAmbientDataByStationId 获得环境监测数据变量
*
* param string $valuetype 变量类型
* param string $label 单位
* param int $sid 站点编号,默认为空,空值将列出所有站点变量值
* param int $round 数值精确度,小数点后位数
*
* return array
*/
function getAmbientDataByStationId($valuetype,$label,$sid=0,$round=4){
if(empty($sid))
{
//降水
$p = array('tsp','pm10','so2','no2');
if(in_array($valuetype,$p))
{
$sql = "select \"position\" from ambient_air group by \"position\"";
}
$sth = $this->dbh->prepare($sql);
$sth->execute();
$stations = $sth->fetchAll();
$data = array();
foreach ($stations as $v)
{
$data[] = $this->createAmbientDataSet($v['position'],$valuetype,$label,$round);
}
return $data;
}//所有站点
if(is_array($sid))
{
}//多个指定站点
else
{
}//单个站点
}
//getAmbientDataByStationId() 获得环境监测数据变量的值
//********************************************************
/*
* dataAction() ajax获取数据
*
* param string $ac //请求的数据类型
* param string $dt //请求的数据来源(气象,水文)
*
* return view
*/
function dataAction()
{
$this->_helper->layout->disableLayout();
$this->_helper->viewRenderer->setNoRender();
$ac = $this->_request->getParam('ac');
$base = $this->_request->getParam('dt');
if(!in_array($base,array('meteorology','hydrology','ambient')))
{
$data = array(
"error"=>"参数错误"
);
$this->jsonexit($data);
return true;
}
$datas = array();
$label = "";
$title = "";
$range = 0;
$type = "line";
if($base == "meteorology")
{
if($ac=='temperature')
{
$title = "日平均温度";
$label = "";
$type = "line";
$datas = $this->getMeteorologyDataByStationId($ac,$label);
}//日平均温度
if($ac == 'windspeed')
{
$title = "日平均风速";
$label = "m/s";
$type = "line";
$datas = $this->getMeteorologyDataByStationId($ac,$label);
}//日平均风速 m/s
if($ac == 'precipitation')
{
$title = "日合计降水量";
$label = "mm";
$type = "line";
$datas = $this->getMeteorologyDataByStationId($ac,$label);
}//日合计降水量 mm
if($ac == 'sunshine')
{
$title = "日照小时数";
$label = "h";
$type = "line";
$datas = $this->getMeteorologyDataByStationId($ac,$label);
}//日照小时数 h
}//气象数据
if($base == 'hydrology')
{
if($ac == 'dp')
{
$title = "日降水量";
$label = "mm";
$type = "line";
$datas = $this->getHydrologyDataByStationId($ac,$label);
}//日降水量
if($ac == 'mtp')
{
$title = "月均降水";
$label = "mm";
$type = "line";
$datas = $this->getHydrologyDataByStationId($ac,$label);
}//月均降水
if($ac == 'yrp')
{
$title = "年均降水";
$label = "mm";
$type = "line";
$datas = $this->getHydrologyDataByStationId($ac,$label);
}//年均降水
if($ac == 'dq')
{
$title = "日均流量";
$label = "mm";
$type = "line";
$datas = $this->getHydrologyDataByStationId($ac,$label);
}
if($ac == 'mtq')
{
$title = "月均流量";
$label = "mm";
$type = "line";
$datas = $this->getHydrologyDataByStationId($ac,$label);
}
if($ac == 'yrq')
{
$title = "年均流量";
$label = "mm";
$type = "line";
$datas = $this->getHydrologyDataByStationId($ac,$label);
}
}//水文数据
if($base=="ambient")
{
if($ac=='tsp')
{
$title = "总悬浮颗粒物";
$label="";
$type = "column";
$range = 6;
$datas = $this->getAmbientDataByStationId($ac,$label);
}
if($ac=='pm10')
{
$title = "可吸入颗粒物";
$label="";
$type = "column";
$range = 6;
$datas = $this->getAmbientDataByStationId($ac,$label);
}
if($ac=='so2')
{
$title = "二氧化硫";
$label="";
$type = "column";
$range = 6;
$datas = $this->getAmbientDataByStationId($ac,$label);
}
if($ac=='no2')
{
$title = "二氧化氮";
$label="";
$type = "column";
$range = 6;
$datas = $this->getAmbientDataByStationId($ac,$label);
}
}//环境监测数据
if(empty($range))
{
$range = 0;
}
$data = array(
"label"=>$label,
"title"=>$title,
"type"=>$type,
"range"=>$range,
"datas"=>$datas
);
$this->jsonexit($data);
return true;
}//dataAction() Ajax获取数据
function utcMsTime($time=''){
if(empty($time))
return (time()+8*3600)*1000;
else
return $time*1000;
}
/*
* jsonexit() 退出并返回json数据
*
* param array $data 要返回的JSON数据可以是任意数组
*
* return application/JSON
*/
public function jsonexit($data){
$this->getResponse()->setHeader('Content-Type', 'application/json')->appendBody(json_encode($data,JSON_NUMERIC_CHECK));
return true;
}//jsonexit() 退出并返回json数据
}
/*
数据模型
"data"=>array(
//第一条曲线
"data1"=>array(
"title"=>"5米风速",
"unit"=>"m/s",
"sensor"=>"传感器XXX",
"sensor_type"=>"传感器类型",
"datas"=>array(
array($this->utcMsTime(),26.0),
array($this->utcMsTime()+3600*1000,28.1),
array($this->utcMsTime()+7200*1000,30.9),
array($this->utcMsTime()+10800*1000,32.0),
array($this->utcMsTime()+12800*1000,28.0),
array($this->utcMsTime()+13800*1000,22.0)
)
),
//第二条曲线
"data2"=>array(
"title"=>"10米风速",
"unit"=>"m/s",
"sensor"=>"传感器XXX",
"sensor_type"=>"传感器类型",
"datas"=>array(
array($this->utcMsTime(),26.0),
array($this->utcMsTime()+5600*1000,22.1),
array($this->utcMsTime()+8200*1000,35.9),
array($this->utcMsTime()+11800*1000,32.0),
array($this->utcMsTime()+13900*1000,22.0),
array($this->utcMsTime()+16800*1000,21.0)
)
),
//第三条.......
)
*/