User:Grin/Balaton 2010

From OpenStreetMap Wiki
Jump to navigation Jump to search

A Plattensee régi feldolgozásának hackjei és az új feldolgozás… másik hackjei.

Régi kódok

tms.php

Ez a kód generálta dinamikusan a régi tile-okat.

<?php

// $_SERVER['REQUEST_URI'] = '/balaton/0/19/288239/184617.jpg';
// header('Content-Type: text/plain; charset=utf-8');
// var_dump($_SERVER['REQUEST_URI']); exit;

$sort_by_size = false;

try {
	if (!preg_match('#^/([^/]+)/([0-9]+)/([0-9]+)/([0-9]+)/([0-9]+)\.(.+)$#i', $_SERVER['REQUEST_URI'], $regs)) throw new HttpException('invalid request');

	$map = $regs[1];
	$switch = $regs[2];
	$zoom = $regs[3];
	$col = $regs[4];
	$row = $regs[5];
	$ext = $regs[6];

	if ($switch >= 1000) throw new Exception('invalid image number');
	
	$json = file_get_contents('tiles.json');
	$tiles = json_decode($json, true);
	$images = array();
	foreach ($tiles as $image => $zooms) {
		if (!in_bbox($zooms[$zoom], $col, $row)) continue;
		if ($sort_by_size) {
			$filename = "$image/$zoom/$col/$row.$ext";
			$filesize = filesize($filename);
			$images[$image] = $filesize;
		} else {
			$distance = distance_from_center($zooms[$zoom], $col, $row);
			if ($distance === null) continue;
			$images[$image] = $distance;
		}
	}
	if (!count($images)) throw new HttpException('no image here');
	if ($sort_by_size) {
		arsort($images);
	} else {
		asort($images);	
	}
	$keys = array_keys($images);
	if (!isset($keys[$switch])) throw new HttpException('no more images here');
	$choosen = $keys[$switch];
	$location = "/$map/$choosen/$zoom/$col/$row.$ext";

	header('HTTP/1.0 302 Moved temporarily');
	header('Location: ' . $location);
	header('X-image-count: ' . count($keys));
	header('X-image-value: ' . round($image[$choosen], 1));
			
} catch (HttpException $e) {
	header('HTTP/1.0 404 Not Found');
	header('Content-type: text/plain; charset=utf-8');
	echo $e->getMessage();
}

function in_bbox ($minmax, $col, $row) {
	if ($col < $minmax[0] ||
		$col > $minmax[1] ||
		$row < $minmax[2] ||
		$row > $minmax[3]) return false;
	return true;
}

function distance_from_center ($minmax, $col, $row) {

	$midcol = (int) (($minmax[0] + $minmax[1])/2);
	$midrow = (int) (($minmax[2] + $minmax[3])/2);
	
	$dcol = $col - $midcol;
	$drow = $row - $midrow;
	
	$distance = sqrt($dcol*$dcol + $drow*$drow);
	return $distance;
}

class HttpException extends Exception {}

Új kód

Statikus lap, no php.