Skip to content

Commit

Permalink
maps: added pixel size info
Browse files Browse the repository at this point in the history
  • Loading branch information
Anrijs committed Apr 23, 2019
1 parent 12e6b65 commit 1b9c856
Show file tree
Hide file tree
Showing 2 changed files with 127 additions and 15 deletions.
94 changes: 88 additions & 6 deletions www/lib/tifftools.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,72 @@ function has_prefix_t($string, $prefix) {
}

function getLatLon($info) {
$pt = explode("(", $info)[2];
$pt = explode(")", $pt)[0];
$pt = explode(",", $pt);

$lon = trim($pt[0]);
$lat = trim($pt[1]);

$lat_pts = explode("\"", $lat);
$lon_pts = explode("\"", $lon);

$hemi = $lon_pts[1];
$pole = $lat_pts[1];

$lat_pts = explode("d", $lat_pts[0]);
$lon_pts = explode("d", $lon_pts[0]);

$lat_dd = intval($lat_pts[0]);
$lon_dd = intval($lon_pts[0]);

$lat_pts = explode("'", $lat_pts[1]);
$lon_pts = explode("'", $lon_pts[1]);

$lat_mm = intval($lat_pts[0]);
$lon_mm = intval($lon_pts[0]);

$lat_ss = floatval($lat_pts[1]);
$lon_ss = floatval($lon_pts[1]);

$lat_mm += ($lat_ss/60);
$lon_mm += ($lon_ss/60);

$outlat = $lat + ($lat_mm/60);
$outlon = $lon + ($lon_mm/60);

if (pole == "S") {
$outlat = -$outlat;
}
if ($hemi == "W") {
$outlon = $outlon;
}

return array($outlat, $outlon);
}

function getPixelSize($info) {
$pt = explode("(", $info)[1];
$pt = explode(")", $pt)[0];
$pt = explode(",", $pt);

$lon = $pt[0];
$lat = $pt[1];
$px1 = $pt[0] * 100000;
$px2 = $pt[1] * 100000;

return array($lat, $lon);
return array($px1, $px2);
}

function getTiffSize($info) {
$pt = trim(str_replace("Size is", "", $info));
$pt = explode(",", $pt);

$x = trim($pt[0]);
$y = trim($pt[1]);

return array($x, $y);
}


function getGdalInfo($filepath) {
$cmd = "gdalinfo " . $filepath;
$tiff_info = shell_exec($cmd . " 2>&1; echo $?");
Expand Down Expand Up @@ -57,6 +113,14 @@ function getGdalInfo($filepath) {
if (has_prefix_t($info, "Lower Right")) {
$lr = getLatLon($info);
}
if (has_prefix_t($info, "Pixel Size")) {
$tiff_info["px"] = getPixelSize($info);
}
if (has_prefix_t($info, "Size is")) {
$tiff_info["size"] = getTiffSize($info);
}


}

$lxtop = haversineGreatCircleDistance($ul[0], $ul[1], $ur[0], $ur[1]);
Expand All @@ -73,10 +137,13 @@ function getGdalInfo($filepath) {

$s = ($a+$b+$c)/2;

$area1 = sqrt($s*($s-$a)*($s-$b)*($s-$c));
$area1 = sqrt(abs($s*($s-$a)*($s-$b)*($s-$c)));

$s = ($a+$d+$c)/2;
$area2 = sqrt($s*($s-$a)*($s-$d)*($s-$c));

$area2 = sqrt(abs($s*($s-$a)*($s-$d)*($s-$c)));

$temp = $s*($s-$a)*($s-$b)*($s-$c);

$area = $area1 + $area2;
$p = $a + $b + $c + $d;
Expand All @@ -91,11 +158,26 @@ function getGdalInfo($filepath) {
$tiff_info["s"] = round($area); //sqm
$tiff_info["p"] = round($p); //m

if (!array_key_exists("px",$tiff_info)) {
$x = 0;
$y = 0;

if (array_key_exists("size", $tiff_info)) {
$x = $lxtop / $tiff_info["size"][0];
$y = $lyright / $tiff_info["size"][1];
}

$tiff_info["px"] = array($x,$y);
}

return $tiff_info;
}

function genLocationFile($mapuid, $tiff_info) {

}
?>

if (isset($_GET["test"])) {
print_r(getGdalInfo("/var/www/html/maps/" . $_GET["test"]));
}
?>
48 changes: 39 additions & 9 deletions www/maps.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,19 @@
include "lib/filetools.php";
include "lib/tifftools.php";

function regenerateInfo($target_dir, $target_file) {
// GeoTIFF info
$tiff_info = getGdalInfo($target_file);

$infofile = fopen($target_dir . "info", "w");
fwrite($infofile, $tiff_info["p"] . "," . $tiff_info["s"] . "," . $tiff_info["px"]); // preimeter, area
fclose($infofile);
}

function endsWith($haystack, $needle) {
$length = strlen($needle);

return $length === 0 ||
return $length === 0 ||
(substr($haystack, -$length) === $needle);
}

Expand Down Expand Up @@ -39,41 +48,62 @@ function endsWith($haystack, $needle) {
$tifflnk = $_R["maps"].$map."/".$tifname;
$tiffdl = " <a href=\"". $tifflnk . "\">Download TIFF</a>";

$infogen = 0;

$infofile = $_R["maps"] . $map . "/info";
if (!file_exists($infofile)) {
$infook = file_exists($infofile);
if ($infook) {
$tiff_info = explode(",",file_get_contents($infofile));
if (sizeof($tiff_info) < 4) {
$infook = 0;
$infogen++;
}
}

if (!$infook) {
$infogen++;
// generate tiff info

// GeoTIFF info
$tiff_info = getGdalInfo($_R["maps"] . $map . "/" . $tifname);

$tiff_info = array($tiff_info["p"], $tiff_info["s"], $tiff_info["px"][0], $tiff_info["px"][1]);

$info = fopen($infofile, "w");
fwrite($info, $tiff_info["p"] . "," . $tiff_info["s"]); // perimeter, area
fwrite($info, implode(",", $tiff_info)); // perimeter, area, px size
fclose($info);

}

if (sizeof($tiff_info) < 3) {
// regenerate
}

$tiff_info = explode(",",file_get_contents($infofile));
$t_p = (round($tiff_info[0])) . "m";
$t_s = (round($tiff_info[1]));
$t_px = (round(($tiff_info[2]+$tiff_info[3])*50)/100);

if ($t_s > 100000) {
if ($t_s > 1000000) {
$t_s = (round($t_s/10000)/100) . "km2";
} else if ($t_s > 10000) {
$t_s = (round($t_s/100)/100) . "ha";
} else {
$t_s = $t_s . "m2";
}

$tiffinfo = "<small>S: ${t_s}</small><br><small>P: ${t_p}</small>";
$tiffinfo = "<small>S: ${t_s}</small><br><small>P: ${t_p}</small><br><small>{$t_px} px/cm</small>";

$body .= '<tr><td>'.$pos++.'</td><td>'.$img64a.$name.'</td><td>'.$tifname."<br><small>".$fname.$tiffdl."</small>".'</td><td>2018-07-05 09:10:12</td><td>'.human_filesize($fsize).'</td><td>'.$tiffinfo.'</td><td><a href="maps.add2layer.php?uid='.$map.'" class="btn btn-sm btn-success">Add to layer</a>';
$body .= "\n" . '<a href="maps.rm.php?uid='.$map.'" onclick="return confirm(\'Are you sure? Tiles will be still vissible in layers.\nMap '.$name.' will be deleted forever.\')" class="btn btn-sm btn-danger">Delete</a>'."\n";

$body .= '</td>';
$body .= '</td>';
}
$body .= '</table>';
$body .= '<div class="float-right"><a href="maps.add.php" class="btn btn-info">Add map</a></div>';

$contents["tab"] = "Maps";
$contents["header"] = "";
$contents["script"] = "";
$contents["header"] = "";
$contents["script"] = "";
$contents["body"] = $body;
// draw template
include 'templates/template.main.php';
Expand Down

0 comments on commit 1b9c856

Please sign in to comment.