<?php
ob_start("ob_gzhandler");
header('Content-type: application/vnd.google-earth.kml+xml');
// get the time
$timesnap = date("H:i:s");
include "marks-styles.php";
$response = "";
$coords[0]=-10;
$coords[1]=-90;
$coords[2]=10;
$coords[3]=90;
function wfImageDir( $fname,$w ) {
$wgUploadDirectory="http://upload.wikimedia.org/wikipedia/commons/thumb";
$hash = md5( $fname );
$dest = $wgUploadDirectory . '/' . $hash{0} . '/' . substr( $hash, 0, 2 ).'/' .$fname."/".$w."-".$fname;
return $dest;
}
function microtime_float()
{
list($usec, $sec) = explode(" ", microtime());
return ((float)$usec + (float)$sec);
}
function placemarks ($coords0,$coords1,$coords2,$coords3)
{
Global $lang;
Global $pop;
Global $style;
Global $photo;
Global $region;
Global $notregion;
Global $source;
Global $notsource;
Global $styles;
Global $thumbs;
Global $smem;
Global $s;
Global $typen;
Global $timebegin;
Global $timebetween;
Global $sql;
Global $ssl;
$loclang=$lang;
#select red_table
$red_table=0;
if (abs($coords1-$coords3)>1) {$red_table=2;}
if (abs($coords1-$coords3)>10) {$red_table=3;}
if (abs($coords1-$coords3)>100) {$red_table=4;}
# if ($pop<>""){$popinsert=" AND pop>$pop";}
# if ($pop==-1){$popinsert=" AND pop='0' AND style NOT LIKE 'city%'";}
if ($style<>""){$styleinsert=" AND style like '$style'";}
if ($photo=="yes"){$photoinsert=" AND imagejpg IS NOT NULL";}
if ($photo=="no"){$photoinsert=" AND imagejpg IS NULL";}
# if ($region<>""){list ($reg, $sub) = split('-', $region);
# $regioninsert=" AND `Country`='$reg'";
# if ($sub<>"") {$regioninsert.=" AND `Subregion`='$sub'"; }
# }
# if ($notregion<>""){list ($reg, $sub) = split('-', $notregion);
# $regioninsert=" AND `Country`<>'$reg'";
# if ($sub<>"") {$regioninsert.=" AND `Subregion`<>'$sub'"; }
# }
if ($source<>""){$sourceinsert=" AND lang like '$source'";}
if ($notsource<>""){$sourceinsert=" AND lang not like '$notsource'";}
$lonmid = (($coords2 - $coords0)/2) + $coords0;
$latmid = (($coords3 - $coords1)/2) + $coords1;
$distan=$coords2 - $coords0;
if ($lang==""){$Titel_Column='"Titel"';} else {$Titel_Column='"T_'.$lang.'"';}
$sql='(SELECT '.$Titel_Column.' as title, pop,lon,lat,style,lang,image,imagejpg,name'
." FROM wp_coords_red".$red_table." WHERE the_geom &&
ST_SetSRID(ST_MakeBox2D(ST_Point($coords0,$coords1),
ST_Point($coords2,$coords3)),4326)".
' and '.$Titel_Column.' IS NOT NULL'.
" $popinsert$styleinsert$photoinsert$regioninsert$sourceinsert ORDER BY psize DESC LIMIT 80) ";
#$sql=$sqlleft.$coords0." AND ".$lonmid." AND `lat` BETWEEN ".$coords1." AND ".$latmid.$sqlright." UNION ";
#$sql.=$sqlleft.$lonmid." AND ".$coords2." AND `lat` BETWEEN ".$coords1." AND ".$latmid.$sqlright." UNION ";
#$sql.=$sqlleft.$coords0." AND ".$coords2." AND `lat` BETWEEN ".$latmid." AND ".$coords3.$sqlright." UNION ";
#$sql.=$sqlleft.$lonmid." AND ".$coords2." AND `lat` BETWEEN ".$latmid." AND ".$coords3.$sqlright;
#echo $sql;
$timebegin= microtime_float();
#if ($ssl) {$proto='https://secure.wikimedia.org/wikipedia/'.$loclang.'/wiki/';} else
# {$proto='http://'.$loclang.'.wikipedia.org/wiki/';}
// query the database
$res = pg_query($sql);
// check for query error
if($e = pg_last_error()) {echo $sql;die($e);}
while($row = pg_fetch_assoc($res))
{
if ($lang==""){$loclang=$row[lang];}
#print_r ($row);
$proto = $ssl ? 'https://secure.wikimedia.org/wikipedia/'.$loclang.'/wiki/' : 'http://'.$loclang.'.wikipedia.org/wiki/';
$row[title]=str_replace("?","ß",$row[title]);
$row[title]=str_replace("&","&",$row[title]);
$row[imagejpg]=str_replace("?","ß",$row[imagejpg]);
$row[imagejpg]=str_replace("&","&",$row[imagejpg]);
$name=utf8_decode ($row[title]);
$forbitten = array("&","<", ">", "'",'"');
$row[name]=str_replace($forbitten,"",$row[name]);
$subname=str_replace(' ','_',$row[name]);
if ($subname=="") {$sub="";} else {$sub="#$subname";}
$url=substr((rawurlencode($row[title])),0,250);
$row[4]=$row[style];
$type=$row[style];
if ($row[4]=="") {$type="without";}
if ($row[4]=="adm1st") {$type="without";}
if ($row[4]=="adm2nd") {$type="without";}
#if (substr($row[4], 0, 4)=="city") {$type="city";}
if (substr($row[4], 0, 4)=="moun") {$type="mountain";}
if ($row[4]=="volcano") {$type="mountain";}
if ($row[4]=="tunnel") {$type="street";}
if ($row[4]=="shopping") {$type="economy";}
if ($row[4]=="company") {$type="economy";}
$ersetze=",cemetery,zoo,theme park,wildlife san,forest,world natura,natural monu,botanic gard,oase";
if (strrpos($ersetze, $row[4])>0) {$type="park";}
$ersetze=",education,tower,gastronomy,theater,nuclear powe,hotel,hydroelectri,power plant,library,jail,nuclear faci,hall,cultural cen,habour,court,event center,cinema,boat lift,casino";
if (strrpos($ersetze, $row[4])>0) {$type="building";}
$ersetze=",reservoir,bay,strait,lake,waterfall,sea,water well,canal,geysers,canyon,river,ocean,geyser";
if (strrpos($ersetze, $row[4])>0) {$type="waterbody";}
$ersetze=",castle,manor-house,world cultur,monument,monastery,archaeologic,government,fortress,historical c,world herita,palace,pyramid,archipelago";
if (strrpos($ersetze, $row[4])>0) {$type="castle";}
$ersetze=",religious bu,temple,synagogue,mosque";
if (strrpos($ersetze, $row[4])>0) {$type="church";}
$ersetze=",mining,military,science,glacier,quarter,impact crate,dam,cape,cave,peninsula,festival,valley,concentratio,lock,cablecar";
if (strrpos($ersetze, $row[4])>0) {$type="landmark";}
if ($type=="landmark" AND $sub<>"") {$type="landmarklist";}
if (stripos($smem,"$type,") === false) {
$typen=$typen.$s[str_replace(" ","_",$type)]."\n";
$smem=$smem.$type.",";}
$image=urlencode($row[image]);
$imagejpg=urlencode($row[imagejpg]);
if (($imagejpg<>"") AND ($thumbs=="yes") AND ($sub=="")){$insertstyle=substr(md5($row[title]), 0, 4 );}
else {$insertstyle=$type;}
if ($sub<>"") {$showname=$sub;$shownameballon=$row[title];} else {$showname=$row[title];$shownameballon=$loclang.".Wikipedia";}
$response = ' <Placemark>';
$response .= "<name>$showname</name>";
$response .= ' <visibility>1</visibility>';
$response .= ' <description><![CDATA[ <a target="_blank" ';
$response .= 'href="'.$proto;
$response .= $row[title].urlencode($sub);
$response .= '">'.$shownameballon.'</a><br>';
if (($imagejpg<>"") AND ($thumbs<>"yes"))
{$response .= '<div style=" padding-top: 0.5em; padding-left: 0.5em; width:190px;height:250px; border:10px;" > <img alt="'.$row[imagejpg].'" src="'.wfImageDir($row[imagejpg],"180px").'"></a></div>';}
$response .= "<br><small>Source: $row[lang]";
$response .= "<br> style: $insertstyle </small>";
$response .= ']]></description>';
$response .= " <styleUrl>#$insertstyle</styleUrl>";
$response .= ' <Point>';
$response .= "<coordinates>$row[lon],$row[lat],0</coordinates>\n";
$response .= " </Point> </Placemark>\n";
if (($imagejpg<>"") AND ($thumbs=="yes")){
$styles .= "\n<Style id=\"".substr(md5($row[title]), 0, 4 )."\">
<IconStyle> <scale>2.5</scale>".
"<Icon> <href>".wfImageDir($row[imagejpg],"120px")."</href></Icon>".
'<hotSpot x="0.5" y="0.0" xunits="fraction" yunits="fraction"/></IconStyle>'.
'<LabelStyle><color>ffb5b5b5</color><scale>0.8</scale></LabelStyle>'.
'</Style>';
}
$response_all=$response.$response_all;
}
$timebetween= microtime_float();
#logfile
$logfile="/home/kolossos/data/logs/log9.txt";
if (date("d")=="01")
{{$counter=fopen($logfile,"a");}
$log=($timebetween-$timebegin).",".$distan.",".$lonmid.",".$latmid.",".$lang.",".$red_table.",".date("m")."\n"; #",".$dis.",".$lonmid.","$latmid.",".$sql.
fputs($counter,$log);
fclose($counter);
}
return $response_all;
}
// split the client's BBOX return by commas and spaces to obtain an array of coordinates
$BOX = pg_escape_string(urldecode( $_REQUEST["BBOX"] ));
if ($BOX==""){$BOX = pg_escape_string(urldecode( $_REQUEST["bbox"] ));}
$lang=addslashes($_REQUEST[LANG]);
#if ($_SERVER['REMOTE_ADDR']=="88.251.219.128"){exit();}
#if ($lang=="en"){exit();}
$style=addslashes($_REQUEST[style]);
$pop=addslashes($_REQUEST[pop]);
$photo=addslashes($_REQUEST[photo]);
$region=addslashes($_REQUEST[region]);
$notregion=addslashes($_REQUEST[notregion]);
$source=addslashes($_REQUEST[source]);
$notsource=addslashes($_REQUEST[notsource]);
$ssl = isset($_REQUEST['secure']) && $_REQUEST['secure'];
$coords = preg_split('/,|\s/', $BOX);
$thumbs=addslashes($_REQUEST[thumbs]);
$time_start = microtime_float();
// open psql connection
$pg = pg_connect('host=sql-mapnik dbname=u_kolossos');
// check for connection error
if($e = pg_last_error()) die($e);
// for clarity, place each coordinate into a clearly marked bottom_left or top_right variable
$bl_lon = $coords[0];
$bl_lat = $coords[1];
$tr_lon = $coords[2];
$tr_lat = $coords[3];
// calculate the approx center of the view -- note that this is innaccurate if the user is not looking straight down
$lonmid = (($coords[2] - $coords[0])/2) + $coords[0];
$latmid = (($coords[3] - $coords[1])/2) + $coords[1];
$response= placemarks ($coords[0],$coords[1],$coords[2],$coords[3]);
#$response= placemarks ($coords[0],$coords[1],$lonmid,$latmid);
#$response.= placemarks ($coords[0],$latmid,$lonmid,$coords[3]);
#$response.= placemarks ($lonmid,$coords[1],$coords[2],$latmid);
#$response.= placemarks ($lonmid,$latmid,$coords[2],$coords[3]);
pg_close($pg);
$time_end = microtime_float();
$time1=round($timebegin-$time_start,3);
$time2=round($timebetween-$timebegin,3);
$time3 = round($time_end - $timebetween,3);
?>
<kml xmlns="http://www.opengis.net/kml/2.2">
<Document>
<name>cities.kml</name>
<visibility>0</visibility>
<open>1</open>
<?php
echo $typen;
echo $styles;
echo $response;
?>
<Placemark>
<name>Info</name>
<visibility>1</visibility>
<description><![CDATA[This is a extract from <a href="http://en.wikipedia.org/wiki/Hauptseite">Wikipedia</a> from 42 languages, it use the Interwikilinks to generarte all the other languages. See for more infos: <a href="http://de.wikipedia.org/wiki/Wikipedia:WikiProjekt_Georeferenzierung/Wikipedia-World/en">Wikipedia-World</a>
<p>Created in <?php echo $time1."+".$time2."+".$time3."<p>".$sql;?> sec.
]]></description>
</Placemark>
</Document>
</kml>