<?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();
}

extract($_POST);

//print_r($_POST);


//===============================================
//	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 = 12;
$APPLICATION_ID = 22;
$MAP_NAME = "./scmap/orig.png";
$MAP_NAME_ZOOM = "./scmap/webpage2.png";
$ZOOM_MAP_DEGREE = 36.029;

$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);
$aa = 10000;
//===============================================
//	HTML-HEAD-
//	備考		：	
//	更新		：	2012/10/18 KMK-Teraoka
//===============================================
print "<html>";
print "<head>";
//print "<meta http-equiv=\"content-type\" content=\"text/html;charset=utf-8\">";
//print "<meta http-equiv=\"Refresh\" content=\"30\">";
print "<link rel=\"stylesheet\" type=\"text/css\" href=\"/ansys/config/css/style.css\">";
print "<link rel=\"shortcut icon\" href=\"/ansys/config/favicon.ico\">";
print "<title>". $SCRIPT_TITLE. "</title>";

//print "<script src='./lib/jquery-2.0.3/jquery-2.0.3.min.js'></script>";
//print "<script src='./lib/jquery-ui-1.10.3/jquery-ui.min.js'></script>";
//print "<script src='./lib/three-67/three.min.js'></script>";
//print "<script src='./lib/three-67/TrackballControls_kmkt.js'></script>";
//print "<script src='./lib/three-67/helvetiker_bold.typeface.js'></script>";
//print "<script src='./lib/jquery-cookie/jquery.cookie.js'></script>";

print "</head>";

//===============================================
//	HTML-BODY部開始-
//	備考		：	
//	更新		：	2012/10/18 KMK-Teraoka
//===============================================
print "<body>";

print "<center>";
print "<font size=\"3\">";

print "<b>";
print "". $SCRIPT_TITLE. "<br>";
print "</b>";
print "</font>";
print "</center>";

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]);
	
	$AreaFileName = "./area.txt";
	
	if(!strcmp($AreaInput,"入力"))
	{
		if($Lat1 && $Lon1 && $Lat2 && $Lon2 && $Lat3 && $Lon3)
		{
			
			$fp = fopen($AreaFileName, "w");
			fwrite($fp, $Lat1 ."," .$Lon1 ."#" .$Lat2 ."," .$Lon2 ."#" .$Lat3 ."," .$Lon3);
			if($Lat4 && $Lon4) fwrite($fp, "#" .$Lat4 ."," .$Lon4);
			if($Lat5 && $Lon5) fwrite($fp, "#" .$Lat5 ."," .$Lon5);
			if($Lat6 && $Lon6) fwrite($fp, "#" .$Lat6 ."," .$Lon6);
			fclose($fp);
		}
		else
		{
			
			
		}
	}
	
	
	//20151001 追加
	
	$AreaData = file_get_contents($AreaFileName);
	$Area = split("#", $AreaData);
	
	// ===============================================
	// 基準点情報
	// 備考		：	
	// 更新		：	2012/11/29 KMK-Teraoka
	// ===============================================
//	$ltLon = 139.793416;						//経度(左上)
//	$ltLat =  35.592797;						//緯度(左上)
//	$rbLon = 139.981100;						//経度(右下)
//	$rbLat =  35.666697;						//緯度(右下)
	
//	$ltX =  149;								// 左上X座標
//	$ltY =  486;								// 左上Y座標
//	$rbX = 1010;								// 右下X座標
//	$rbY =   71;								// 右下Y座標

	$ltLon = 140.62380;						//経度(左上)
	$ltLat =  36.42613;						//緯度(左上)
	$rbLon = 140.61533;						//経度(右下)
	$rbLat =  36.41504;						//緯度(右下)
	
	$ltX =  107;								// 左上X座標
	$ltY =  358;								// 左上Y座標
	$rbX = 924;								// 右下X座標
	$rbY = 853;								// 右下Y座標


	$diffX = $rbX - $ltX;						// X座標ピクセルの差を求める
	$diffY = $rbY - $ltY;						// Y座標ピクセルの差を求める
	
	$spX = number_format(($rbLat - $ltLat) / $diffX, 10);		// X座標の倍率を求める
//	$spY = number_format(($ltLat - $rbLat) / $diffY, 10);		// Y座標の倍率を求める
	$spY = number_format(($rbLon - $ltLon) / $diffY, 10);		// Y座標の倍率を求める

	// ===============================================
	// 基準点情報2
	// 備考		：	
	// 更新		：	2012/11/29 KMK-Teraoka
	// ===============================================
	$ltLonZm = 139.8023727;						//経度(左上)
	$ltLatZm =  35.58572163;					//緯度(左上)
	$rbLonZm = 139.8100264;						//経度(右下)
	$rbLatZm =  35.57928836;					//緯度(右下)
	
	$ltXZm =  18;								// 左上X座標
	$ltYZm =  72;								// 左上Y座標
	$rbXZm = 399;								// 右下X座標
	$rbYZm = 139;								// 右下Y座標
	
	// 回転させてから比率を求める
	$ltXZm =  17 * cos(deg2rad($ZOOM_MAP_DEGREE)) -  73 * sin(deg2rad($ZOOM_MAP_DEGREE));
	$ltYZm =  17 * sin(deg2rad($ZOOM_MAP_DEGREE)) +  73 * cos(deg2rad($ZOOM_MAP_DEGREE));
	$rbXZm = 384 * cos(deg2rad($ZOOM_MAP_DEGREE)) - 136 * sin(deg2rad($ZOOM_MAP_DEGREE));
	$rbYZm = 384 * sin(deg2rad($ZOOM_MAP_DEGREE)) + 136 * cos(deg2rad($ZOOM_MAP_DEGREE));
	
	$diffXZm = $rbXZm - $ltXZm;						// X座標ピクセルの差を求める
	$diffYZm = $rbYZm - $ltYZm;						// Y座標ピクセルの差を求める
	
	$spXZm = number_format(($rbLonZm - $ltLonZm) / $diffXZm, 10);		// X座標の倍率を求める
	$spYZm = number_format(($ltLatZm - $rbLatZm) / $diffYZm, 10);		// Y座標の倍率を求める
	
	// ===============================================
	// GD処理準備
	// 備考		：	
	// 更新		：	2010/09/08 KMK T.Teraoka
	// ===============================================
	$src = ImageCreateFromPNG($MAP_NAME);						//パレットへ貼り付ける画像指定(地図画像)
//	$src2 = ImageCreateFromPNG($MAP_NAME_ZOOM);
	$img = imagecreatetruecolor($imgSize[0], $imgSize[1]);			//パレットの作成
//	$zoomImg = imagecreatetruecolor($zoomImgSize[0], $zoomImgSize[1]);
	imagecopy($img, $src, 0, 0, 0, 0, $imgSize[0], $imgSize[1]);	//画像をパレットへ貼り付け
//	imagecopy($zoomImg, $src2, 0, 0, 0, 0, $zoomImgSize[0], $zoomImgSize[1]);
	
	$font1 = "/usr/share/fonts/vlgothic/VL-Gothic-Regular.ttf";
	imagesetthickness($img, $THIC);
	imagesetthickness($img, $THIC);
	
	// 使用色の準備
	$Black = imagecolorallocate($img, 0,0,0);
//	$BlackZm = imagecolorallocate($zoomImg, 0,0,0);
	$White = imagecolorallocate($img, 255, 255, 255);
//	$WhiteZm = imagecolorallocate($zoomImg, 255, 255, 255);
	
	// 更新時間をマップに表示する
	//$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("データベースへの接続に失敗しました");
	}

//20151001

$shipCol = imagecolorallocate($img, 255, 0, 0);
			if(count($Area) > 2)
			{
				//ImageTTFText($img, 12, 0, 500, 500, $Black, $font1, count($Area));
				$pointA = array();
				$pointB = array();
				$pointC = array();
				$pointD = array();
				$pointE = array();
				
				if($Area[0])
				{
					$Adata =split(",", $Area[0]);
					$pointA["x"] = ($Adata[0]-$ltLat) / $spX + $ltX;
					$pointA["y"] = ($Adata[1]- $ltLon) / $spY + $ltY;
				}

				if($Area[1])
				{
					$Bdata =split(",", $Area[1]);
					$pointB["x"] = ($Bdata[0]- $ltLat) / $spX + $ltX;
					$pointB["y"] = ($Bdata[1]- $ltLon) / $spY + $ltY;
				}
				
				if($Area[2])
				{
					$Cdata =split(",", $Area[2]);
					$pointC["x"] = ($Cdata[0]- $ltLat) / $spX + $ltX;
					$pointC["y"] = ($Cdata[1]- $ltLon) / $spY + $ltY;
				}

				if($Area[3])
				{
					$Ddata =split(",", $Area[3]);
					$pointD["x"] = ($Ddata[0]- $ltLat) / $spX + $ltX;
					$pointD["y"] = ($Ddata[1]- $ltLon) / $spY + $ltY;
				}
				
				if($Area[4])
				{
					$Edata =split(",", $Area[4]);
					$pointE["x"] = ($Edata[0] -$ltLat) / $spX + $ltX;
					$pointE["y"] = ($Edata[1] -$ltLon) / $spY + $ltY;
				}
				
				if($Area[5])
				{
					$Fdata =split(",", $Area[5]);
					$pointF["x"] = ($Fdata[0] -$ltLat) / $spX + $ltX;
					$pointF["y"] = ($Fdata[1] -$ltLon) / $spY + $ltY;
				}


//				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]);

//				ImageTTFText($img, 12, 0, 500, 500, $Black, $font1, $pointA["x"] ."__" .$pointA["y"]);
//				ImageTTFText($img, 12, 0, 500, 520, $Black, $font1, $pointB["x"]."__" .$pointB["y"]);
//				ImageTTFText($img, 12, 0, 500, 540, $Black, $font1, $pointC["x"]."__" .$pointC["y"]);
//				ImageTTFText($img, 12, 0, 500, 560, $Black, $font1, $pointD["x"]."__" .$pointD["y"]);

				imageline($img, $pointA["x"], $pointA["y"], $pointB["x"], $pointB["y"], $shipCol);
				imageline($img, $pointB["x"], $pointB["y"], $pointC["x"], $pointC["y"], $shipCol);
				if($pointD)
				{
					imageline($img, $pointC["x"], $pointC["y"], $pointD["x"], $pointD["y"], $shipCol);
					
					if($pointE)
					{
						imageline($img, $pointD["x"], $pointD["y"], $pointE["x"], $pointE["y"], $shipCol);
						if($pointF)
						{
							imageline($img, $pointE["x"], $pointE["y"], $pointF["x"], $pointF["y"], $shipCol);
							imageline($img, $pointF["x"], $pointF["y"], $pointA["x"], $pointA["y"], $shipCol);
						}
						else
						{
							imageline($img, $pointE["x"], $pointE["y"], $pointA["x"], $pointA["y"], $shipCol);
						}
					}
					else
					{
						imageline($img, $pointD["x"], $pointD["y"], $pointA["x"], $pointA["y"], $shipCol);
					}
				}
				else
				{
					imageline($img, $pointC["x"], $pointC["y"], $pointA["x"], $pointA["y"], $shipCol);
				}
				
						
			}


	// ===============================================
	// 位置を取得する
	// 備考		：	
	// 更新		：	2012/11/29 KMK T.Teraoka
	// ===============================================
	$selTerm = "SELECT * FROM qgis.ts_terminal";
	$selTerm .= " WHERE const_id='". $CONST_ID. "'";
	$selTerm .= " AND app_id='". $APPLICATION_ID. "'";
	//$selTerm .= " AND mesdatetime >= '". $ENABLE_DATETIME. "'";
	$selTerm .= " ORDER BY android_id";
	$selTerm .= ";";
	//$_log->write($selTerm. "\n");
	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);
	
	$row = pg_num_rows($resTerm);
	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)
		{
			// 基準点1
			//
			if (strcmp($rowTerm["android_id"], "a237b48012d8873e") != 0 && strcmp($rowTerm["android_id"], "26f784b2293665d0") != 0 && strcmp($rowTerm["android_id"], "db9739dd1cfbc823") != 0)
			{
				$point = array();
//				$point["x"] = ($rowTerm["lon"] - $ltLon) / $spX + $ltX;
//				$point["y"] = ($ltLat - $rowTerm["lat"]) / $spY + $ltY;
				$point["x"] = ($rowTerm["lat"] - $ltLat) / $spX + $ltX;
				$point["y"] = ($rowTerm["lon"] - $ltLon) / $spY + $ltY;
				
//				$rowTerm["lon"] = $point["x"] * $spX - $ltX * $spX + $ltLon;
//				$rowTerm["lat"] = $ltLat - $point["y"] * $spY + $ltY*$spY
				
				$shipSimbol = retShipSimbol($point, $rowTerm["direction"], 0);
				if ($shipSimbol == -1)
				{
					$ellipSize = 8;
					imagefilledellipse($img, $point["x"], $point["y"], $ellipSize, $ellipSize, $shipCol);
				}
				else
				{
					imageline($img, $shipSimbol[0]["x"], $shipSimbol[0]["y"], $shipSimbol[1]["x"], $shipSimbol[1]["y"], $shipCol);
					imageline($img, $shipSimbol[2]["x"], $shipSimbol[2]["y"], $shipSimbol[3]["x"], $shipSimbol[3]["y"], $shipCol);
					imageline($img, $shipSimbol[3]["x"], $shipSimbol[3]["y"], $shipSimbol[4]["x"], $shipSimbol[4]["y"], $shipCol);
					imageline($img, $shipSimbol[4]["x"], $shipSimbol[4]["y"], $shipSimbol[2]["x"], $shipSimbol[2]["y"], $shipCol);
				}
				
				$length = strlen($rowTerm["terminal_name"]);
				imagefilledrectangle($img, $point["x"] + 5, $point["y"] - 45, $point["x"] + 5 + ($length * 8), $point["y"] - 22, $White);
				imagerectangle($img, $point["x"] + 5, $point["y"] - 45, $point["x"] + 5 + ($length * 8), $point["y"] - 22, $shipCol);
				ImageTTFText($img, 12, 0, $point["x"] + 15, $point["y"] - 27, $Black, $font1, $rowTerm["terminal_name"]);
			}
			
			// 基準点2
			$point = array();
			$px = ($rowTerm["lon"] - $ltLonZm) / $spXZm + $ltXZm;
			$py = ($ltLatZm - $rowTerm["lat"]) / $spYZm + $ltYZm;
			
			$point["x"] = $px * cos(deg2rad(-$ZOOM_MAP_DEGREE)) - $py * sin(deg2rad(-$ZOOM_MAP_DEGREE));
			$point["y"] = $px * sin(deg2rad(-$ZOOM_MAP_DEGREE)) + $py * cos(deg2rad(-$ZOOM_MAP_DEGREE));
			
			if (strcmp($rowTerm["android_id"], "a237b48012d8873e") != 0 && strcmp($rowTerm["android_id"], "26f784b2293665d0") != 0 && strcmp($rowTerm["android_id"], "db9739dd1cfbc823") != 0)
			{
				$shipSimbol = retShipSimbol($point, $rowTerm["direction"], $ZOOM_MAP_DEGREE);
				if ($shipSimbol == -1)
				{
					$ellipSize = 8;
					imagefilledellipse($zoomImg, $point["x"], $point["y"], $ellipSize, $ellipSize, $zmShipCol);
				}
				else
				{
					imageline($zoomImg, $shipSimbol[0]["x"], $shipSimbol[0]["y"], $shipSimbol[1]["x"], $shipSimbol[1]["y"], $zmShipCol);
					imageline($zoomImg, $shipSimbol[2]["x"], $shipSimbol[2]["y"], $shipSimbol[3]["x"], $shipSimbol[3]["y"], $zmShipCol);
					imageline($zoomImg, $shipSimbol[3]["x"], $shipSimbol[3]["y"], $shipSimbol[4]["x"], $shipSimbol[4]["y"], $zmShipCol);
					imageline($zoomImg, $shipSimbol[4]["x"], $shipSimbol[4]["y"], $shipSimbol[2]["x"], $shipSimbol[2]["y"], $zmShipCol);
				}
				$length = strlen($rowTerm["terminal_name"]);
				imagefilledrectangle($zoomImg, $point["x"] + 30, $point["y"] - 40, $point["x"] + 30 + ($length * 8), $point["y"] - 17, $White);
				imagerectangle($zoomImg, $point["x"] + 30, $point["y"] - 40, $point["x"] + 30 + ($length * 8), $point["y"] - 17, $zmShipCol);
				imageline($zoomImg, $point["x"] + 30, $point["y"] - 17, $point["x"], $point["y"], $zmShipCol);
				ImageTTFText($zoomImg, 12, 0, $point["x"] + 40, $point["y"] - 22, $BlackZm, $font1, $rowTerm["terminal_name"]);
			}
			else
			{
				$ellipSize = 8;
				imagefilledellipse($zoomImg, $point["x"], $point["y"], $ellipSize, $ellipSize, $zmSyunCol);
				
				$length = strlen($rowTerm["terminal_name"]);
				imagefilledrectangle($zoomImg, $point["x"] + 30, $point["y"] - 40, $point["x"] + 30 + ($length * 8), $point["y"] - 17, $White);
				imagerectangle($zoomImg, $point["x"] + 30, $point["y"] - 40, $point["x"] + 30 + ($length * 8), $point["y"] - 17, $zmSyunCol);
				imageline($zoomImg, $point["x"] + 30, $point["y"] - 17, $point["x"], $point["y"], $zmSyunCol);
				ImageTTFText($zoomImg, 12, 0, $point["x"] + 40, $point["y"] - 22, $BlackZm, $font1, $rowTerm["terminal_name"]);
			}
		}
	}
	/*
	//-----------------------------------------------------------------------------------
	// Debug
	$FUNABASHI_ROUTE_LAT = 35.59561081;
	$FUNABASHI_ROUTE_LON = 139.9843314;
	
	$COUNT_AREA = array(
		array("lat"	=>	35.62678039,	"lon"	=>	140.0013295),
		array("lat"	=>	35.63562604,	"lon"	=>	140.0137459),
		array("lat"	=>	35.62880063,	"lon"	=>	140.0254795),
		array("lat"	=>	35.62017771,	"lon"	=>	140.0109185)
	);
//	$COUNT_AREA = array(
//		array("lat"	=>	35.585232,	"lon"	=>	139.804459),
//		array("lat"	=>	35.581812,	"lon"	=>	139.800081),
//		array("lat"	=>	35.579438,	"lon"	=>	139.804716),
//		array("lat"	=>	35.582580,	"lon"	=>	139.808836)
//	);
	
	$xa = ($FUNABASHI_ROUTE_LON - $ltLon) / $spX + $ltX;
	$ya = ($ltLat - $FUNABASHI_ROUTE_LAT) / $spY + $ltY;
	imagefilledellipse($img, $xa, $ya, 8, 8, $Black);
	
	$pointa = array();
	for($iCount = 0; $iCount < 4; $iCount++)
	{
		$pointa[$iCount]["x"] = ($COUNT_AREA[$iCount]["lon"] - $ltLon) / $spX + $ltX;
		$pointa[$iCount]["y"] = ($ltLat - $COUNT_AREA[$iCount]["lat"]) / $spY + $ltY;
		
		if ($iCount > 0)
		{
			imageline($img, $pointa[$iCount]["x"], $pointa[$iCount]["y"], $pointa[$iCount - 1]["x"], $pointa[$iCount - 1]["y"], $Black);
		}
		if ($iCount == 3)
		{
			imageline($img, $pointa[$iCount]["x"], $pointa[$iCount]["y"], $pointa[0]["x"], $pointa[0]["y"], $Black);
		}
	}
	*/
	//-----------------------------------------------------------------------------------
	
//	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
	// ===============================================
	imagepng($img, $imgFileName);
	imagedestroy($img);
//	imagedestroy($zoomImg);
	imagedestroy($src);
//	imagedestroy($src2);
	
	// ===============================================
	// 上で保存した画像を表示
	// 備考		：	
	// 更新		：	2010/09/08 KMK T.Teraoka
	// ===============================================
	print "<table border=\"0\" bgcolor=\"#000000\" cellspacing=\"0\" cellpadding=\"0\">";
	print "<tr>";
	print "<td>";
	print "<table border=\"0\" bgcolor=\"#000000\" cellspacing=\"1\" cellpadding=\"0\">";
	print "<tr>";
	print "<td>";
	print "<table border=\"0\" cellspacing=\"0\" cellpadding=\"3\">";
	print "<tr bgcolor=\"#FFFFFF\">";
	print "<td align=\"left\">";
//	print "<img src=\"". $imgFileName. "?t=$now_t\" galleryimg=\"no\" width=\"881\" height=\"590\">";
//	print "<img src=\"". $imgFileName. "?t=$now_t\" galleryimg=\"no\" width=\"945\" height=\"1022\">";
	print  "<canvas id='canvas' width='945'height='1022'></canvas>";
	print "</td>";
	print "<td align=\"left\" valign=\"top\">";

//////////////////////


?>
    <script src="http://code.jquery.com/jquery-1.8.3.min.js"></script>
    <script>
       draw();
        /**
         * canvas 操作用の context を取得する。
         */
         var flag = 1;
         
        function getCtx() {
          var canvas = document.getElementById('canvas');
          return canvas.getContext('2d');
        };

	function draw() {
		flag = 1;
		var ctx2 = getCtx();
	  if ( ! canvas || ! canvas.getContext ) { return false; }
	  /* Imageオブジェクトを生成 */
	  var img = new Image();
	  img.src = "<?php echo $imgFileName; ?>";
	  /* 画像が読み込まれるのを待ってから処理を続行 */
	  img.onload = function() {
	    ctx2.drawImage(img, 0, 0);
	  };
	  };


        /**
         * canvas 内のクリックした座標に青薄色の四角形を描画する
         */
      
        function drawRectAtClickedPos(ev)
        {
          // クリックした座標を取得する。
          var posX = ev.offsetX,
              posY = ev.offsetY;
          var ctx = getCtx();
          var posXc = posX * <?php echo $spX; ?> - <?php echo $ltX; ?> * <?php echo $spX; ?> + <?php echo $ltLat; ?> ;
          var posYc = posY * <?php echo $spY; ?> - <?php echo $ltY; ?> * <?php echo $spY; ?> + <?php echo $ltLon; ?> ;
          // クリックした箇所に半透明の青色の四角を描画する。
          ctx.fillStyle = 'rgba(255, 0, 0, 0.5)';
          ctx.fillRect(posX, posY, 10, 10);
          ctx.fillText(flag,posX-5,posY-5);
 //         ctx.fillText(posXc,posX-5,posY-20);
 //         ctx.fillText(posYc,posX-5,posY-30);
			
			if(flag == 1)
			{
				document.area.Lat1.value=posXc;
				document.area.Lon1.value=posYc;
			}
			else if(flag == 2)
			{
				document.area.Lat2.value=posXc;
				document.area.Lon2.value=posYc;
			}
			else if(flag == 3)
			{
				document.area.Lat3.value=posXc;
				document.area.Lon3.value=posYc;
			}
			else if(flag == 4)
			{
				document.area.Lat4.value=posXc;
				document.area.Lon4.value=posYc;
			}
			else if(flag == 5)
			{
				document.area.Lat5.value=posXc;
				document.area.Lon5.value=posYc;
			}
			else if(flag == 6)
			{
				document.area.Lat6.value=posXc;
				document.area.Lon6.value=posYc;
			}

          flag = flag+1;
          
        };

        // canvas のクリックに紐付ける。
        $('canvas').click(drawRectAtClickedPos);
</script>

<?php

print "<br />";

	// ===============================================
	// 帳票出力Formの作成
	// 備考		：	
	// 更新		：	2013/06/17 KMK T.Teraoka
	// ===============================================
	print "<form action=\"". "../output/transport/output.php". "\" method=\"post\" style=\"display:inline\">";
	print "<input type=\"submit\" name=\"btnPrint\" value=\"csv出力\" style=\"width:90px\">";
	print "<br />";
	print "<br />";
	$nowDate = explode(",", date("m,d", time()));
	print "<input type=\"text\" name=\"txtTargetM\" value=\"". $nowDate[0]. "\" maxlength=\"2\" style=\"width:20px\">";
	print "&nbsp;月";
	print "&nbsp;&nbsp;&nbsp;";
	print "<input type=\"text\" name=\"txtTargetD\" value=\"". $nowDate[1]. "\" maxlength=\"2\" style=\"width:20px\">";
	print "&nbsp;日";
	print "</form>";
	print "<BR><BR>";



/*
print "<script type='text/javascript'>";
    //マウスのXY座標を取得
print "function getMouseXY_ie(){"; //IE
//print "document.xy.screen_x.value=event.screenX;";
//print "document.xy.screen_y.value=event.screenY;";
//print "document.xy.page_x.value=event.pageX;";
//print "document.xy.page_y.value=event.pageY;";
//print "document.xy.client_x.value=event.clientX;";
//print "document.xy.client_y.value=event.clientY;";
print "document.xy.offset_x.value=event.offsetX;";
print "document.xy.offset_y.value=event.offsetY;";
print "}";
print "function getMouseXY(event){"; // Firefox, Netscape
//print "document.xy.screen_x.value=event.screenX;";
//print "document.xy.screen_y.value=event.screenY;";
//print "document.xy.page_x.value=event.pageX;";
//print "document.xy.page_y.value=event.pageY;";
//print "document.xy.client_x.value=event.clientX;";
//print "document.xy.client_y.value=event.clientY;";

//$rowTerm["lon"] = $point["x"] * $spX - $ltX * $spX + $ltLon;
//$rowTerm["lat"] = $ltLat - $point["y"] * $spY + $ltY*$spY

print "document.xy.offset_x.value=event.offsetX * " .$spX ." -" .$ltX ." *" .$spX  ."+" .$ltLon .";";
print "document.xy.offset_y.value=" .$ltLat ."- event.offsetY" ."*" .$spY ."+" .$ltY ."*" .$spY .";";
print "}";
    // onMouseMoveイベントが発生したら、マウスのXY座標を取得する処理を実行
print "document.onmousemove=(document.all) ? getMouseXY_ie : getMouseXY;";

print "document.onmousemove=(document.all) ? getMouseXY_ie : getMouseXY;";

print "function MouseClickFunc(event){";
print "element.style.backgroundColor = '#4f4';";
print "element_result.value = 'マウスがクリックされた (type:\"" + e.type + "\" button:' + event.button + ')' + element_result.value;";
print "	}";

print "</script>";
*/









//テスト用
//print "<form action='#' name='xy'>";
print "<form action=\"". "view.php". "\" name='area' method=\"post\" style=\"display:inline\">";
print "    <table>";
//print "        <tr><td>offsetX（クリックした要素上のX座標）</td><td>=</td><td><input type='text' name='offset_x' /></td></tr>";
//print "        <tr><td>offsetY（クリックした要素上のY座標）</td><td>=</td><td><input type='text' name='offset_y' /></td></tr>";
print "    </table>";
//print "</form>";



//	print "<form action=\"". "view.php". "\" method=\"post\" style=\"display:inline\">";
	print "<input type=\"submit\" name=\"AreaInput\" value=\"入力\" style=\"width:50px\">";
//	print "<BR>";
print "<input type='button' value='ﾘｾｯﾄ' onclick='draw();'>";
	print "<BR>";
/*
	print "&nbsp;<B>Point1</B> <BR>";
	print "&nbsp;緯度";
	print "<input type=\"text\" name='offset_x' maxlength=\"10\" style=\"width:100px\">";
	print "<BR>";
	print "&nbsp;経度";
	print "<input type=\"text\" name='offset_y' maxlength=\"10\" style=\"width:100px\">";
	print "<BR><BR>";
*/

	print "&nbsp;<B>Point1</B> <BR>";
	print "&nbsp;緯度";
	print "<input type=\"text\" name=\"Lat1\" maxlength=\"10\" style=\"width:100px\">";
	print "<BR>";
	print "&nbsp;経度";
	print "<input type=\"text\" name=\"Lon1\"  maxlength=\"10\" style=\"width:100px\">";
	print "<BR><BR>";

	print "&nbsp;<B>Point2</B> <BR>";
	print "&nbsp;緯度";
	print "<input type=\"text\" name=\"Lat2\"  maxlength=\"10\" style=\"width:100px\">";
	print "<BR>";
	print "&nbsp;経度";
	print "<input type=\"text\" name=\"Lon2\"  maxlength=\"10\" style=\"width:100px\">";
	print "<BR><BR>";

	print "&nbsp;<B>Point3</B> <BR>";
	print "&nbsp;緯度";
	print "<input type=\"text\" name=\"Lat3\"  maxlength=\"10\" style=\"width:100px\">";
	print "<BR>";
	print "&nbsp;経度";
	print "<input type=\"text\" name=\"Lon3\"  maxlength=\"10\" style=\"width:100px\">";
	print "<BR><BR>";

	print "&nbsp;<B>Point4</B> <BR>";
	print "&nbsp;緯度";
	print "<input type=\"text\" name=\"Lat4\" maxlength=\"10\" style=\"width:100px\">";
	print "<BR>";
	print "&nbsp;経度";
	print "<input type=\"text\" name=\"Lon4\" maxlength=\"10\" style=\"width:100px\">";
	print "<BR><BR>";

	print "&nbsp;<B>Point5</B> <BR>";
	print "&nbsp;緯度";
	print "<input type=\"text\" name=\"Lat5\" maxlength=\"10\" style=\"width:100px\">";
	print "<BR>";
	print "&nbsp;経度";
	print "<input type=\"text\" name=\"Lon5\" maxlength=\"10\" style=\"width:100px\">";
	print "<BR><BR>";

	print "&nbsp;<B>Point6</B> <BR>";
	print "&nbsp;緯度";
	print "<input type=\"text\" name=\"Lat6\" maxlength=\"10\" style=\"width:100px\">";
	print "<BR>";
	print "&nbsp;経度";
	print "<input type=\"text\" name=\"Lon6\" maxlength=\"10\" style=\"width:100px\">";

	print "</form>";

	print "</td>";
	print "</tr>";
	print "</table>";
	print "</td>";
	print "</tr>";
	print "</table>";
	print "</td>";
	print "</tr>";
	print "</table>";
}
catch(Exception $exc)
{
	$_log->write($exc->getMessage(). "\n");
}

// 戻る
//print "<br />";
print "<a href=\"../index.php\">戻る</a>";

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

//===============================================
//	HTML終了処理
//	備考		：	
//	更新		：	2012/10/18 KMK-Teraoka
//===============================================
print "</body>";
print "</html>";


//print "        <tr><td>screenX（モニタ画面上のX座標）</td><td>=</td><td><input type='text' name='screen_x' /></td></tr>";
//print "        <tr><td>screenY（モニタ画面上のY座標）</td><td>=</td><td><input type='text' name='screen_y' /></td></tr>";
//print "        <tr><td>pageX（ページ上のX座標）</td><td>=</td><td><input type='text' name='page_x' />　※Firefox、Netscapeのみ</td></tr>";
//print "        <tr><td>pageY（ページ上のY座標）</td><td>=</td><td><input type='text' name='page_y' />　※Firefox、Netscapeのみ</td></tr>";
//print "        <tr><td>clientX（ウィンドウ上のX座標）</td><td>=</td><td><input type='text' name='client_x' /> </td></tr>";
//print "        <tr><td>clientY（ウィンドウ上のY座標）</td><td>=</td><td><input type='text' name='client_y' /> </td></tr>";




?>
