<?php
function retShipSimbol($point_, $direction_, $mapDegree_)
{
	$ret = -1;
	if ($direction_ == -1)
		return -1;
	
	$ret = array();
	$ret[0]["x"] = $point_["x"];
	$ret[0]["y"] = $point_["y"];
	
	$ret[1]["x"] = floor($point_["x"] + (25 * cos(deg2rad($direction_ - 90 - $mapDegree_))));
	$ret[1]["y"] = floor($point_["y"] + (25 * sin(deg2rad($direction_ - 90 - $mapDegree_))));
	
	$ret[2]["x"] = floor($point_["x"] + (12 * cos(deg2rad($direction_ - 90 - $mapDegree_))));
	$ret[2]["y"] = floor($point_["y"] + (12 * sin(deg2rad($direction_ - 90 - $mapDegree_))));
	
	$ret[3]["x"] = floor($point_["x"] + (8 * cos(deg2rad($direction_ - 90 + 120 - $mapDegree_))));
	$ret[3]["y"] = floor($point_["y"] + (8 * sin(deg2rad($direction_ - 90 + 120 - $mapDegree_))));
	
	$ret[4]["x"] = floor($point_["x"] + (8 * cos(deg2rad($direction_ - 90 - 120 - $mapDegree_))));
	$ret[4]["y"] = floor($point_["y"] + (8 * sin(deg2rad($direction_ - 90 - 120 - $mapDegree_))));
	
	return $ret;
}

function deleteFile($dir)
{
	if (!$handle=opendir($dir))
		return;
	while($filename=readdir($handle))
	{
		if(!preg_match("/^\./", $filename))
		{
			if (!unlink("$dir/$filename"));
			return;
		}
	}
}


//-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
//	自動でクラスを参照する
//	関数名	：	__autoload()
//	備考	：	参照先は./class/XXX.phpとする
//	更新	：	2012/10/19 KMK-Teraoka
//-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
function __autoload($className)
{
	$fPath = "./class/" . str_replace("_", "/", $className) . ".php";
	require_once $fPath;
}

//===============================================
//	DEBUG_MODE
//	備考		：	
//	更新		：	2012/10/19 KMK-Teraoka
//===============================================
define("DEBUG_MODE", "0");
Debug::setMode(DEBUG_MODE);
if (DEBUG_MODE == 1)
{
	Stopwatch::start();
}

//===============================================
//	CommonUtilities
//	備考		：	
//	更新		：	2012/11/29 KMK-Teraoka
//===============================================
$SCRIPT_TITLE = "工事車両運行管理システム　-位置確認-";
$PENTA_OCEAN = "五洋・菅原特定建設工事共同企業体";
$TAIL_PENTA_OCEAN = "penta-ocean";

$HOST = "192.168.100.153";
$DB = "ansys";
$USER = "postgres";
$PASS = "am#71ZJ*";
$LOG_FILE = "./log/view.log";

$CONST_ID = 14;
$APPLICATION_ID = 25;
$MAP_NAME = "./scmap/orig.png";

$R_VAL = array(255, 204, 204, 204, 204, 153, 153, 100, 102, 102, 255, 153);
$G_VAL = array(102, 255, 102, 153, 255,   0,  51,   0, 204,  51,   0, 153);
$B_VAL = array(  0,   0, 102, 255, 255,  51, 255,   0, 153, 255, 153,   0);

$ENABLE_SEC = 600;
$ENABLE_DATETIME = date("Y-m-d H:i:s", time() - $ENABLE_SEC);

try
{
	// tmpディレクトリ内のファイルを削除
	deleteFile("./tmp");
	
//	$_log = new Log($LOG_FILE);
	
	// ===============================================
	// 画像情報の取得
	// 備考		：	
	// 更新		：	2012/11/29 KMK-Teraoka
	// ===============================================
	$imgSize = getimagesize($MAP_NAME);
//	$zoomImgSize = getimagesize($MAP_NAME_ZOOM);
	$imgFileName = "./tmp/". $CONST_ID. "_". $APPLICATION_ID. "_". md5(uniqid(rand(), true)) .image_type_to_extension($imgSize[2]);

//■回転角度
//	$angle = deg2rad(-32.18050349);	//
//	$angle2 =deg2rad(32.18050349);  //座標⇒ピクセル変換後､回転角度
	$angle = deg2rad(-32.18050349);	//
	$angle2 =deg2rad(32.18050349);  //座標⇒ピクセル変換後､回転角度


	// ===============================================
	// 基準点情報
	// 備考		：	
	// 更新		：	2012/11/29 KMK-Teraoka
	// ===============================================
	$ltLon = 139.6421261;						//経度(左上)
	$ltLat =  35.407007;						//緯度(左上)
	$rbLon = 139.6449963;						//経度(右下)
	$rbLat =  35.405800;						//緯度(右下)


	$ltX = (23 * cos($angle)) - (($imgSize[1]-375) * sin($angle));
	$ltY = (($imgSize[1]-375) * cos($angle)) + (23 * sin($angle));
	$rbX = (1230 * cos($angle)) - (($imgSize[1]-272) * sin($angle));
	$rbY = (($imgSize[1]-272) * cos($angle)) + (1230 * sin($angle));
	
	$diffX = $rbX - $ltX;										// X座標ピクセルの差を求める
	$diffY = $rbY - $ltY;										// Y座標ピクセルの差を求める
	
	$spX = number_format(($rbLon - $ltLon) / $diffX, 10);		// X座標の倍率を求める
	$spY = number_format(($ltLat - $rbLat) / $diffY, 10);		// Y座標の倍率を求める


	
	// ===============================================
	// GD処理準備
	// 備考		：	
	// 更新		：	2010/09/08 KMK T.Teraoka
	// ===============================================
	$src = ImageCreateFromPNG($MAP_NAME);						//パレットへ貼り付ける画像指定(地図画像)
	$img = imagecreatetruecolor($imgSize[0], $imgSize[1]);			//パレットの作成
	imagecopy($img, $src, 0, 0, 0, 0, $imgSize[0], $imgSize[1]);	//画像をパレットへ貼り付け

	
	$font1 = "/usr/share/fonts/vlgothic/VL-Gothic-Regular.ttf";
	//imagesetthickness($img, $THIC);
	//imagesetthickness($img, $THIC);
	
	// 使用色の準備
	$Black = imagecolorallocate($img, 0,0,0);
	$White = imagecolorallocate($img, 255, 255, 255);
	$Gray =  imagecolorallocate($img, 180, 174, 174);
	
	// 更新時間をマップに表示する
	//$update = date("H:i:s" ,time());
	//imagefilledpolygon($img, array(10, 10, 10, 20, 55, 20, 55, 10), 4, $White);
	//ImageTTFText ($img, 8, 0, 10, 20, $Black, $font1, $update);
	
	//================================================
	//	データベース接続
	//	備考		：	
	//	更新		：	2012/11/29 KMK-Teraoka
	//================================================
	$conn = pg_connect("
		host	=	". $HOST. "
		dbname	=	". $DB. "
		user	=	". $USER. "
		password=	". $PASS. "
	");
	if (!$conn)
	{
		throw new Exception("データベースへの接続に失敗しました");
	}

//20160126
	$HtSelect = "SELECT * FROM qgis.yokohama_point order by id;";

	//$_log->write($selTerm. "\n");
	if (!($resHt = pg_query($conn, $HtSelect)))
	{
	//		print pg_last_error() ."<BR>";
	//		throw new Exception("データ取得に失敗<br />". $DelArea. "<br />". pg_last_error(). "<br />");
	}
//		$ba = 0.6;
		$Csize =10;
		$row = pg_num_rows($resHt);
		for($iCount = 0; $iCount < $row; $iCount++)
		{

			$rowHt = pg_fetch_array($resHt, $iCount, PGSQL_ASSOC);

			if($rowHt['area_id'])
			{
				$selArea = "SELECT * FROM qgis.area_status_fill_yokohama WHERE list_id=" .$rowHt['area_id'] .";";
				if (!($resArea = pg_query($conn, $selArea)))
				{
				//		print pg_last_error() ."<BR>";
				//		throw new Exception("データ取得に失敗<br />". $DelArea. "<br />". pg_last_error(). "<br />");
				}
				$rowArea =  pg_fetch_array($resArea, 0, PGSQL_ASSOC);
				
				$ColR = "0x" .substr($rowArea['legend_color'],0,2);
				$ColG = "0x" .substr($rowArea['legend_color'],2,2);
				$ColB = "0x" .substr($rowArea['legend_color'],4,2);
				//ImageTTFText($img, 12, 0, 500, 400, $Black, $font1, $ColR .$ColR .$ColB);
				$PointCol = imagecolorallocate($img, $ColR,$ColG,$ColB);
				//$PointCol = imagecolorallocatealpha($img, $ColR,$ColG,$ColB,75);
//20160127
				//ImageTTFText($img, 12, 0, 500, 300, $Black, $font1, "____________" .$iCount);
				if($iCount < 11)
				{
					$PointCol = imagecolorallocatealpha($img, $ColR,$ColG,$ColB,75);
					imagerectangle($img, $rowHt['x'], $rowHt['y'], $rowHt['x']+25, $rowHt['y']+20, $Gray);
					imagefilledrectangle($img, $rowHt['x'], $rowHt['y'], $rowHt['x']+25, $rowHt['y']+20, $PointCol);
//					$PointCol = imagecolorallocatealpha($img, $ColR,$ColG,$ColB,75);
//					imagefilledellipse($img, $rowHt['x'], $rowHt['y'], 20, 20, $PointCol);
				}
				else if($iCount < 1277)
				{
					$PointCol = imagecolorallocatealpha($img, $ColR,$ColG,$ColB,75);
					imagerectangle($img, $rowHt['x'], $rowHt['y'], $rowHt['x']+20, $rowHt['y']+20, $Gray);
					imagefilledrectangle($img, $rowHt['x'], $rowHt['y'], $rowHt['x']+20, $rowHt['y']+20, $PointCol);
//					$PointCol = imagecolorallocatealpha($img, $ColR,$ColG,$ColB,75);
//					imagefilledellipse($img, $rowHt['x'], $rowHt['y'], 20, 20, $PointCol);
				}
				else
				{
					$PointCol = imagecolorallocatealpha($img, $ColR,$ColG,$ColB,75);
					imagerectangle($img, $rowHt['x'], $rowHt['y'], $rowHt['x']+25, $rowHt['y']+20, $Gray);
					imagefilledrectangle($img, $rowHt['x'], $rowHt['y'], $rowHt['x']+25, $rowHt['y']+20, $PointCol);
//					$PointCol = imagecolorallocatealpha($img, $ColR,$ColG,$ColB,75);
//					imagefilledellipse($img, $rowHt['x'], $rowHt['y'], 20, 20, $PointCol);
				}

			}
			else
			{
				if($iCount < 11)
				{
					$PointCol = imagecolorallocatealpha($img, $ColR,$ColG,$ColB,75);
					imagerectangle($img, $rowHt['x'], $rowHt['y'], $rowHt['x']+25, $rowHt['y']+20, $Gray);
//					$PointCol = imagecolorallocatealpha($img, $ColR,$ColG,$ColB,75);
//					imagefilledellipse($img, $rowHt['x'], $rowHt['y'], 20, 20, $PointCol);
				}
				else if($iCount < 1277)
				{
					$PointCol = imagecolorallocatealpha($img, $ColR,$ColG,$ColB,75);
					imagerectangle($img, $rowHt['x'], $rowHt['y'], $rowHt['x']+20, $rowHt['y']+20, $Gray);
//					$PointCol = imagecolorallocatealpha($img, $ColR,$ColG,$ColB,75);
//					imagefilledellipse($img, $rowHt['x'], $rowHt['y'], 20, 20, $PointCol);
				}
				else
				{
					$PointCol = imagecolorallocatealpha($img, $ColR,$ColG,$ColB,75);
					imagerectangle($img, $rowHt['x'], $rowHt['y'], $rowHt['x']+25, $rowHt['y']+20, $Gray);
//					$PointCol = imagecolorallocatealpha($img, $ColR,$ColG,$ColB,75);
//					imagefilledellipse($img, $rowHt['x'], $rowHt['y'], 20, 20, $PointCol);
				}

			}
			
			
			
//			print "<area shape=\"circle\" coords=\"" . * $ba  ."," . * $ba ."," .$Csize * $ba ."\" href=\"#\" alt=\"円形\" onclick='Fill(\"" .$rowHt['pno'] ."\");'>";
			
		}


	// ===============================================
	// 位置を取得する
	// 備考		：	
	// 更新		：	2012/11/29 KMK T.Teraoka
	// ===============================================
	$RTime = date("Y-m-d H:i:s" ,time() - 600);
	
	
	$selTerm = "SELECT * FROM qgis.ts_terminal";
	$selTerm .= " WHERE const_id='". $CONST_ID. "'";
	$selTerm .= " AND app_id='". $APPLICATION_ID. "'";
//	$selTerm .= " AND android_id='a5e4f91fce680532'";
	//$selTerm .= " AND mesdatetime >= '". $ENABLE_DATETIME. "'";
//	$selTerm .= " AND mesdatetime > '". $RTime. "'";
	$selTerm .= " ORDER BY android_id";
	$selTerm .= ";";
	
//	$fp = fopen($LOG_FILE,"a");
//	fwrite($fp,$selTerm ."\n");
//	fclose($fp);
	//$_log->write($selTerm. "\n");
	


//				ImageTTFText($img, 12, 0, 500, 400, $Black, $font1, $Area[1] ."__" .$Area[0]);
//				ImageTTFText($img, 12, 0, 500, 420, $Black, $font1, $Area[3] ."__" .$Area[2]);
//				ImageTTFText($img, 12, 0, 500, 440, $Black, $font1, $Area[5] ."__" .$Area[4]);
//				ImageTTFText($img, 12, 0, 500, 460, $Black, $font1, $Area[7] ."__" .$Area[6]);



	if (!($resTerm = pg_query($conn, $selTerm)))
	{
		throw new Exception("データ取得に失敗<br />". $resTerm. "<br />". pg_last_error(). "<br />");
	}
	
	$shipCol = imagecolorallocate($img, 255, 165, 0);
	$zmShipCol = imagecolorallocate($img, 255, 165, 0);
	$zmSyunCol = imagecolorallocate($img, 0, 102, 253);
	
	//20151027 追加
	ImageTTFText($img, 8, 0, 10, 20, $Black, $font1, date("Y-m-d H:i:s",time()));
	
	
	$row = pg_num_rows($resTerm);
	//ImageTTFText($img, 12, 0, 500, 400, $Black, $font1, $row);


//テスト用
//--------------------------------------------------------------------------------------------------------------------------------------------
/*
				$rowTerm["lat"] = "35.406441";
				$rowTerm["lon"] = "139.643662";
				$point = array();
				$posX_c = ($rowTerm["lon"] - $ltLon) / $spX + $ltX;
				$posY_c = ($ltLat - $rowTerm["lat"]) / $spY + $ltY;

				$point["x"] = ($posX_c * cos($angle2)) - ($posY_c * sin($angle2));
				$point["y"] = $imgSize[1] - (($posY_c * cos($angle2)) + ($posX_c * sin($angle2)));
//				ImageTTFText($img, 12, 0, 500, 400, $Black, $font1,  $point["x"] ."____" .$point["y"]);
				imagefilledellipse($img, $point["x"], $point["y"], 15, 15, $shipCol);

				$length = strlen("起重機船");
				imagefilledrectangle($img, $point["x"] + 5, $point["y"] - 45, $point["x"] + 5 + ($length * 6), $point["y"] - 22, $White);
				imagerectangle($img, $point["x"] + 5, $point["y"] - 45, $point["x"] + 5 + ($length * 6), $point["y"] - 22, $shipCol);
				ImageTTFText($img, 12, 0, $point["x"] + 10, $point["y"] - 27, $Black, $font1, "起重機船");
*/
//--------------------------------------------------------------------------------------------------------------------------------------------

	for($iCount = 0; $iCount < $row; $iCount++)
	{
		$rowTerm = pg_fetch_array($resTerm, $iCount, PGSQL_ASSOC);
		
		//var_dump($rowTerm);
		if (0 != strcmp($rowTerm["lat"], "") && 0 != strcmp($rowTerm["lon"], "") && strcmp($rowTerm["android_id"], "8041c3ba7d4843c1") != 0)
		{
			$point = array();
			$posX_c = ($rowTerm["lon"] - $ltLon) / $spX + $ltX;
			$posY_c = ($ltLat - $rowTerm["lat"]) / $spY + $ltY;

			$point["x"] = ($posX_c * cos($angle2)) - ($posY_c * sin($angle2));
			$point["y"] = $imgSize[1] - (($posY_c * cos($angle2)) + ($posX_c * sin($angle2)));
//			ImageTTFText($img, 12, 0, 500, 400, $Black, $font1,  $point["x"] ."____" .$point["y"]);
			imagefilledellipse($img, $point["x"], $point["y"], 15, 15, $shipCol);

			$length = strlen($rowTerm["terminal_name"]);
			imagefilledrectangle($img, $point["x"] + 5, $point["y"] - 45, $point["x"] + 5 + ($length * 6), $point["y"] - 22, $White);
			imagerectangle($img, $point["x"] + 5, $point["y"] - 45, $point["x"] + 5 + ($length * 6), $point["y"] - 22, $shipCol);
			ImageTTFText($img, 12, 0, $point["x"] + 10, $point["y"] - 27, $Black, $font1, $rowTerm["terminal_name"]);
		}
	}
	//-----------------------------------------------------------------------------------



//	imagerectangle($zoomImg, 0, 0, $zoomImgSize[0] - 1, $zoomImgSize[1] - 1, $BlackZm);
//	imagecopy($img, $zoomImg, 0, 0, 0, 0, $zoomImgSize[0], $zoomImgSize[1]);
	
	// ===============================================
	// 描画した画像を保存 & パレットを削除
	// 備考		：	
	// 更新		：	2010/09/08 KMK T.Teraoka
	// ===============================================
	header('Content-Type: image/png');
//	imagepng($img, $imgFileName);
	imagepng($img);

	imagedestroy($img);
	imagedestroy($src);
}
catch(Exception $exc)
{
//	$_log->write($exc->getMessage(). "\n");
}

//===============================================
//	Postgresql終了処理
//	備考		：	
//	更新		：	2012/10/19 KMK-Teraoka
//===============================================
pg_close($conn);

?>
