<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>OMGM - New map added</title>
<meta http-equiv="Content-Type" content="text/html;charset=ISO-8859-1">
<link href="resources/main.css" rel="stylesheet" type="text/css">
</head>
<body>
<div id="navigation">
<?
include "menu.inc";
?>
</div>
<h1>New map added</h1>
<?php
define("LOG_LEVEL", 0);
include_once "define.php";
include "gm.php";
include "db.php";
$epsg = $_POST["epsg"];
$imageFileArr = $_FILES["imageFile"];
$worldFileArr = $_FILES["worldFile"];
$password = $_POST["password"];
$dataToSave = array();
$dataToSave["epsg"] = $epsg;
$dataToSave["name"] = $_POST["name"];
$dataToSave["contactName"] = $_POST["contactName"];
$dataToSave["contactInfo"] = $_POST["contactInfo"];
if( strcmp($password, ADMINPASSWORD) != 0){
echo "<p>Invalid password</p>";
}
elseif( strcmp($epsg, "choose") == 0){
echo "<p>No projection chosen</p>";
}
else{
echo "<pre>";
$errors = checkForErrors();
if(count($errors)>0){
printErrors($errors);
}
else{
$baseDir = "./";
$uploadDir = $baseDir."uploads/";
$uploaded = moveUploadedTmpFiles($uploadDir);
$reproj = createReprojectedFile($uploadDir);
$projable = createProjectableFile($uploadDir);
$tileable = createTileableFile($uploadDir);
if(count($uploaded["ERRORS"])>0){
printErrors($uploaded["ERRORS"]);
}
else{
$db = new Db();
debugObj($uploaded);
debugObj($projable);
debugObj($reproj);
debugObj($tileable);
$bbox = reproject($epsg, $uploaded, $projable, $reproj, $tileable);
debugObj($bbox);
$idOfImage = -1;
if($bbox){
$idOfImage = createTiles($bbox, $tileable, $dataToSave);
}
else{
printErrors(array("Could not reproject image"));
}
cleanup($idOfImage, $uploaded, $projable, $reproj, $tileable);
}
}
echo "</pre>";
}
?>
</body>
</html>
<?php
function debug($txt){
display($txt, 0);
}
function info($txt){
display($txt, 1);
}
function debugObj($o){
if($logLevel == LOG_LEVEL){
print_r($o);
}
}
function display($txt, $level){
if( $level >= LOG_LEVEL ){
echo $txt."<br>";
}
}
function deleteFile($file){
debug("Delete file: ".$file);
@unlink($file);
}
function cleanup($idOfImage, $uploaded, $projable, $reproj, $tileable){
$originalsDir = "originals/";
if($idOfImage > 0){
$from = $uploaded["IMAGE_FILE"];
$to = $originalsDir."image_".$idOfImage.".".$uploaded["IMAGE_SUFFIX"];
debug("Rename ".$from." ===> ".$to);
rename($from,$to);
}
else{
deleteFile($uploaded["IMAGE_FILE"]);
}
deleteFile($reproj["IMAGE_FILE"]);
deleteFile($projable["IMAGE_FILE"]);
deleteFile($tileable["IMAGE_FILE"]);
deleteFile($projable["WORLD_FILE"]);
deleteFile($uploaded["WORLD_FILE"]);
deleteFile($tileable["IMAGE_FILE"].".aux.xml");
deleteFile($uploaded["ZIP_FILE"]);
}
function printErrors($errors){
echo "<ul class=\"errors\">\n";
foreach($errors as $error){
echo "<li>$error</li>\n";
}
echo "</ul>";
}
function createTileableFile($uploadDir){
$unique = getUniqueFilename($uploadDir);
$reproj = array();
$reproj["NAME"] = $unique;
$reproj["IMAGE_FILE"] = $uploadDir . $unique . ".png";
$reproj["IMAGE_SUFFIX"] = "png";
return $reproj;
}
function createReprojectedFile($uploadDir){
$unique = getUniqueFilename($uploadDir);
$reproj = array();
$reproj["NAME"] = $unique;
$reproj["IMAGE_FILE"] = $uploadDir . $unique . ".tif";
$reproj["IMAGE_SUFFIX"] = "tif";
return $reproj;
}
function createProjectableFile($uploadDir){
$unique = getUniqueFilename($uploadDir);
$reproj = array();
$reproj["NAME"] = $unique;
$reproj["IMAGE_FILE"] = $uploadDir . $unique . ".tif";
$reproj["IMAGE_SUFFIX"] = "tif";
$reproj["WORLD_FILE"] = $uploadDir . $unique . ".tfw";
$reproj["WORLD_SUFFIX"] = "tfw";
return $reproj;
}
function checkForErrors(){
$errors = array();
$imageFileArr = $_FILES["imageFile"];
$worldFileArr = $_FILES["worldFile"];
if($imageFileArr["error"] > 0 ){
$errors[] = "Image file error: ".$imageFileArr["error"];
}
if($worldFileArr["error"] > 0 ){
$errors[] = "World file error: ".$worldFileArr["error"];
}
return $errors;
}
function getUniqueFilename($path){
$fileName = "";
do{
$fileName = md5(microtime());
}while( file_exists($path.$fileName) );
return $fileName;
}
function moveUploadedTmpFiles($uploadDir){
$imageFileArr = $_FILES["imageFile"];
$worldFileArr = $_FILES["worldFile"];
$imageFilePath = $imageFileArr["tmp_name"];
$worldFilePath = $worldFileArr["tmp_name"];
$imageFileSuffix = strrchr($imageFileArr["name"], ".");
$worldFileSuffix = strrchr($worldFileArr["name"], ".");
$unique = getUniqueFilename($uploadDir);
$imageFile = $uploadDir . $unique . $imageFileSuffix;
$imageTifFile = $uploadDir . $unique . ".tif";
$worldFile = $uploadDir . $unique . $worldFileSuffix;
$errors = array();
if(!move_uploaded_file($imageFilePath, $imageFile)){
$errors[] = "Error uploading image file";
}
if(!move_uploaded_file($worldFilePath, $worldFile)){
$errors[] = "Error uploading world file";
}
$cmd = "unzip -o $imageFile -d $uploadDir";
debug($cmd);
$last = exec($cmd);
$from = getFilenameFromZipOutput($last);
debug("Rename $from to $imageTifFile");
rename( $from, $imageTifFile);
return array("ERRORS"=>$errors, "NAME"=>$unique,
"IMAGE_SUFFIX"=>"tif", "WORLD_SUFFIX"=>$worldFileSuffix,
"IMAGE_FILE"=>$imageTifFile, "WORLD_FILE"=>$worldFile, "ZIP_FILE"=>$imageFile);
}
function getFilenameFromZipOutput($last){
$p = split(":", $last);
return trim($p[1]);
}
function createSrs($epsg){
$srs = "wkt_".$epsg.".prf";
if(!file_exists($srs)){
$srs = "EPSG:".$epsg;
}
return $srs;
}
function getCoords($line){
$lng = substr($line, 15, 10);
$lat = substr($line, 28, 10);
return array("LAT"=>$lat, "LNG"=>$lng);
}
function parseBbox($outArr){
$bbox = array();
foreach($outArr as $out){
$pos = strpos($out, "Lower Left");
if( $pos !== false ){
$bbox["LL"] = getCoords($out);
}
$pos = strpos($out, "Upper Right");
if( $pos !== false ){
$bbox["UR"] = getCoords($out);
}
$pos = strpos($out, "Upper Left");
if( $pos !== false ){
$bbox["UL"] = getCoords($out);
}
$pos = strpos($out, "Lower Right");
if( $pos !== false ){
$bbox["LR"] = getCoords($out);
}
}
return $bbox;
}
function reproject($epsg, $uploaded, $projable, $reproj, $tileable){
$retCode = null;
$output = array();
/*
// create a tiff of uploaded image
$cmd = GDALPATH."gdal_translate -co \"PHOTOMETRIC=RGB\" -of GTiff ".$uploaded["IMAGE_FILE"]." ".$projable["IMAGE_FILE"];
debug($cmd);
exec($cmd, $output, $retCode);
*/
/*
debug("Copy ".$uploaded["WORLD_FILE"]." to ". $projable["WORLD_FILE"]);
copy($uploaded["WORLD_FILE"], $projable["WORLD_FILE"]);
*/
$srs = createSrs($epsg);
//$params = "-co \"TFW=YES\" -co \"PHOTOMETRIC=RGB\" -dstalpha -s_srs $srs -t_srs EPSG:4326";
$params = "-dstalpha -s_srs $srs -t_srs EPSG:4326";
$cmd = GDALPATH."/gdalwarp $params ".$uploaded["IMAGE_FILE"]." ".$reproj["IMAGE_FILE"];
debug($cmd);
exec($cmd, $output, $retCode);
if($retCode != 0) return false;
$output = array();
$cmd = GDALPATH."/gdalinfo ".$reproj["IMAGE_FILE"];
debug($cmd);
exec($cmd, $output, $retCode);
$bbox = parseBbox($output);
$cmd = GDALPATH."/gdal_translate -of PNG ".$reproj["IMAGE_FILE"]." ".$tileable["IMAGE_FILE"];
debug($cmd);
exec($cmd, $output, $retCode);
if($retCode != 0){
return false;
}
else{
return $bbox;
}
}
function createTilesAtZoom($zoom, $bbox, $tileable, $tilesDir, $mapId){
global $db;
$sw = $bbox["LL"]["LAT"].",".$bbox["LL"]["LNG"];
$ne = $bbox["UR"]["LAT"].",".$bbox["UR"]["LNG"];
$srcIm = imagecreatefrompng($tileable["IMAGE_FILE"]);
$iw = imagesx($srcIm);
$ih = imagesy($srcIm);
$ims = myCreateImage($iw,$ih);
imagecopy($ims, $srcIm, 0,0, 0,0, $iw,$ih);
imagedestroy($srcIm);
$box = array();
list($south, $west) = split(",", $sw);
$d1 = Google_RelPix($south, $west, $zoom);
$box[] = $south;
$box[] = $west;
list($north, $east) = split(",", $ne);
$d2 = Google_RelPix($north, $east, $zoom);
$box[] = $north;
$box[] = $east;
list($top, $left) = Google_RelPix($north, $west, $zoom);
$ht = abs( $d1[0] - $d2[0] );
$wd = abs( $d1[1] - $d2[1] );
$im = myCreateImage($wd, $ht, false);
imagecopyresampled($im, $ims, 0,0, 0,0, $wd, $ht, $iw, $ih);
$tiles = Google_Tiles($south, $west, $north, $east, $zoom);
$count = 0;
for($i = 0; $i < count($tiles); $i++){
$tile = $tiles[$i];
$count++;
$fileName = "x".$tile["NAMEX"]."_y".$tile["NAMEY"].".png";
$imx = myCreateImage(256,256, true);
imagecopy( $imx, $im, 0,0, $tile["PXW"] - $left, $tile["PYN"] - $top, 256,256);
$file = $tilesDir.$zoom."/".$fileName;
if( file_exists($file) ){
debug("Tile ".$tile["NAMEX"].",".$tile["NAMEY"]." already exist, merge tiles");
$existingIm = imagecreatefrompng($file);
$alreadyExistingTiles = $db->getTilesByName($tile["NAMEX"], $tile["NAMEY"]);
print_r($alreadyExistingTiles);
if( count($alreadyExistingTiles) == 1){ // the first merge
$oldMapId = $alreadyExistingTiles[0]["mapId"];
debug("Rename $file to ".$file."_".$oldMapId);
rename($file, $file."_".$oldMapId);
$db->setMerged($oldMapId, $tile["NAMEX"], $tile["NAMEY"]);
}
else{
debug("Delete ".$file);
unlink($file);
}
// save new map as unique tile
debug("Save new as unique ".$file."_".$mapId);
imagepng($imx, $file."_".$mapId);
$db->addTile($mapId, $zoom, $tile["NAMEX"], $tile["NAMEY"]);
$db->setMerged($mapId, $tile["NAMEX"], $tile["NAMEY"]);
imagecopymerge($imx, $existingIm, 0,0, 0,0, 256,256, 100);
imagepng($imx, $file);
imagedestroy($existingIm);
imagedestroy($imx);
}
else{
imagepng($imx, $file);
imagedestroy($imx);
$db->addTile($mapId, $zoom, $tile["NAMEX"], $tile["NAMEY"]);
}
}
imagedestroy($im);
imagedestroy($ims);
info("Created ".$count." tiles at zoom ".$zoom);
return $count;
}
function myCreateImage($w, $h, $alphaBlend = true){
$newim = imagecreatetruecolor($w, $h);
imagealphablending($newim, $alphaBlend);
imagesavealpha($newim, !$alphaBlend);
$trans_color = imagecolorallocatealpha($newim, 0,0,0,127);
imagefill($newim, 0,0, $trans_color);
imagecolortransparent($newim, $trans_color);
return $newim;
}
function createTiles($bbox, $tileable, $dataToSave){
global $db;
$dbId = $db->addMap($bbox, $dataToSave);
if($dbId > -1){
$tilesDir = "tilesdir/";
$totalCreated = 0;
$from = 8;
$to = 16;
for($zoom=$from; $zoom <= $to; $zoom++){
$dir = $tilesDir.$zoom;
$ok = true;
if( !file_exists( $dir ) ){
if(mkdir($dir)){
debug($dir." did not exist but is now created");
}
else{
debug($dir." did not exist and was NOT created");
$ok = false;
}
}
if($ok){
$numCreated = createTilesAtZoom($zoom, $bbox, $tileable, $tilesDir, $dbId);
$totalCreated += $numCreated;
}
else{
info("Tiles was not created for zoom ".$zoom);
}
}
}
info("Created total $totalCreated tiles.");
return $dbId;
}
?>