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

281 lines
6.2 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');
2012-04-24 16:16:27 +00:00
$this->dbh = new PDO("pgsql:dbname=qinghaihu;host=localhost", "gis", "gispassword" );
}
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";
$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 获得变量值
/*
* dataAction() ajax获取数据用于绘图
*
*
*
*
*/
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')))
{
$data = array(
"error"=>"参数错误"
);
$this->jsonexit($data);
return true;
}
$sql = "select station_id as sid from daily_meteorological_data group by station_id";
$sth = $this->dbh->prepare($sql);
$sth->execute();
$stations = $sth->fetchAll();
$datas = array();
$label = "";
$title = "";
if($base == "meteorology")
{
if($ac=='temperature')
{
$title = "日平均温度";
$label = "";
$datas = $this->getMeteorologyDataByStationId($ac,$label);
}//日平均温度
if($ac == 'windspeed')
{
$title = "日平均风速";
$label = "m/s";
$datas = $this->getMeteorologyDataByStationId($ac,$label);
}//日平均风速 m/s
if($ac == 'precipitation')
{
$title = "日合计降水量";
$label = "mm";
$datas = $this->getMeteorologyDataByStationId($ac,$label);
}//日合计降水量 mm
if($ac == 'sunshine')
{
$title = "日照小时数";
$label = "h";
$datas = $this->getMeteorologyDataByStationId($ac,$label);
}//日照小时数 h
}//气象数据
$data = array(
"label"=>$label,
"title"=>$title,
"datas"=>$datas
);
$this->jsonexit($data);
return true;
}
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)
)
),
//第三条.......
)
*/