%PDF- %PDF-
Mini Shell

Mini Shell

Direktori : /www/old2/_music/diplomka/diplomka/src/cluster/
Upload File :
Create Path :
Current File : /www/old2/_music/diplomka/diplomka/src/cluster/cluster.php

<pre>
<?php
//header("Content-type: text/plain");
include "dibi.min.php";
include_once "Portal.php";
include_once "PortalList.php";

define('MAX_DISTANCE', 50.0);

/** Converts E6 angle to radians
 * @param int $e6 E6 formatted angle
 * @return float Angle in radians
 */
function e6ToRad($e6)
{
    $PI = 3.1415;
    return ($e6 / 1e6) * ($PI / 180);
}

/** Computes disnance between two E6 formatted coordinates (cosine law usage)
 * @param int $lat1 Latitude of first point
 * @param int $lng1 Longitude of first point
 * @param int $lat2 Latituide of second point
 * @param int $lng2 Longitude of second point
 * @return float Distance in meters
 */
function getDistance($lat1, $lng1, $lat2, $lng2)
{
    $EARTH_RADIUS_METERS = 6367000.0;
    return  acos(sin(e6ToRad($lat1))*sin(e6ToRad($lat2)) + cos(e6ToRad($lat1))*cos(e6ToRad($lat2))*cos(e6ToRad($lng2)-e6ToRad($lng1)))*$EARTH_RADIUS_METERS;
}

function mergeClusters(&$arr, $mainCluster, $mergedCluster)
{
    foreach($arr as &$p)
    {
        if($p->cluster == $mergedCluster)
        {
            $p->cluster = $mainCluster;
        }
    }
    unset($p);
}


dibi::connect(array(
    'driver'   => 'mysql',
    'host'     => 'localhost',
    'username' => 'waritko',
    'password' => 'blade666',
    'database' => 'ingress',
    'charset'  => 'utf8',
));

$portals = new PortalList();
$start = 0;

$res = dibi::query("select [data] from [preprocessed]");
foreach($res as $r)
{
    $portals->addPortal(unserialize($r->data));
}

if(isset($_GET['start']))
{
    $start = new DateTime();
    $start->setTimestamp($_GET['start']);
}
else
{
    //$start = $portals->getStartTime();
    $start = new DateTime("2013-10-09 10:05:00");
}

/* CLUSTERING */
// Get portals for specific time period
//$data = $portals->getPortals(new DateTime("2013-10-09 10:05:00"));
$data = $portals->getPortals($start);

$currClusterId = 1;
$maxClusterId = 1;

$dist = array();

foreach($data as &$portal)
{
    $currClusterId = $maxClusterId;
    if($portal->cluster == 0) $maxClusterId++; // Increase cluster ID if not set for portal
    $portal->cluster = $currClusterId;
    foreach($data as &$d)
    {
        $distance = getDistance($portal->latitude, $portal->longitude, $d->latitude, $d->longitude);
        $dist[] = $distance;
        if($distance < MAX_DISTANCE && $distance > 0.1) // To discount same portal twice
        {
            if($d->cluster == 0)
            {
                $d->cluster = $currClusterId;
            }
            else
            {
                mergeClusters($data, $d->cluster, $currClusterId);
                $currClusterId = $d->cluster;
            }
        }
    }
}

// Filter out too small clusters(less than 5 portals)
$clusters = array();
$out = array();

// Debug output of clusters
for($i = 0; $i < $maxClusterId; $i++)
{
    $clusters[$i] = array();
    foreach($data as $p)
    {
        if($p->cluster == ($i+1))
        {
            $clusters[$i][] = $p;
        }
    }
}
foreach($clusters as $c)
{
    if(count($c) >= 3)
    {
        $out[] = $c;
    }
}

// And print out found clusters
$i = 1;
foreach($out as $o)
{
    echo "CLUSTER #$i\n";
    echo "===============\n";
    foreach($o as $portal)
    {
        echo $portal->name . "\n";
    }
    echo "\n\n";
    $i++;
}

$prev = $start->getTimestamp() - 1800;
$next = $start->getTimestamp() + 1800;
echo "\n\n";
echo "<a href=\"cluster.php?start=$prev\">&lt;&lt; Previous</a>    <a href=\"cluster.php?start=$next\">Next &gt;&gt;</a>";
?></pre>

Zerion Mini Shell 1.0