Example #1
0
function calculate_box($lat, $lon, $wide)
{
    $lat_unit_distance = calculate_distance($lat, $lon, $lat + 1, $lon);
    $lat_delta = $wide / (2 * $lat_unit_distance);
    $lon_unit_distance = calculate_distance($lat, $lon, $lat, $lon + 1);
    $lon_delta = $wide / (2 * $lon_unit_distance);
    return array($lat - $lat_delta, $lon - $lon_delta, $lat + $lat_delta, $lon + $lon_delta);
}
Example #2
0
 protected function calculate_distance($lat1, $lng1, $lat2, $lng2, $miles = FALSE)
 {
     if (defined('STRICT_TYPES') && CAMEL_CASE == '1') {
         return (double) self::parameters(['lat1' => DT::FLOAT, 'lng1' => DT::FLOAT, 'lat2' => DT::FLOAT, 'lng2' => DT::FLOAT, 'miles' => DT::BOOL])->call(__FUNCTION__)->with($lat1, $lng1, $lat2, $lng2, $miles)->returning(DT::FLOAT);
     } else {
         return (double) calculate_distance($lat1, $lng1, $lat2, $lng2, $miles);
     }
 }
Example #3
0
 public function testCheckDistance()
 {
     $lat_one = 50;
     $lng_one = -10;
     $lat_two = 30;
     $lng_two = -5;
     $distance = calculate_distance($lat_one, $lng_one, $lat_two, $lng_two);
     $this->assertEquals(2265.57, $distance);
 }
Example #4
0
function get_thing_near_node($lat, $lon, $max_distance, $q1, $q2)
{
    $square = calculate_box($lat, $lon, $max_distance * 2);
    $halts = mysql_query(sprintf($q1, $square[0], $square[2], $square[1], $square[3]));
    $railways = array();
    while ($halt = mysql_fetch_assoc($halts)) {
        $distance = calculate_distance($lat, $lon, $halt['lat'], $halt['lon']);
        if ($distance > $max_distance) {
            continue;
        }
        $railway_ids_query = mysql_query(sprintf($q2, $halt['id']));
        while (list($railway_id, $ordering) = mysql_fetch_row($railway_ids_query)) {
            if (!isset($railways[$railway_id]) || $distance < $railways[$railway_id]['distance']) {
                $railways[$railway_id] = array('distance' => $distance, 'node' => $halt, 'ordering' => $ordering);
            }
        }
    }
    return $railways;
}
Example #5
0
function process_data($resource)
{
    $customers_in_range = [];
    $line_number = 0;
    while ($line = fgets($resource)) {
        $line_number++;
        $customer = json_decode($line);
        if (!$customer) {
            echo "Unable to parse record on line {$line_number}\n";
            continue;
        }
        //check distance
        $distance = calculate_distance($_ENV['office_lat'], $_ENV['office_lng'], $customer->latitude, $customer->longitude);
        if ($distance < 100) {
            //add customer to list
            $customer->distance = $distance;
            $customers_in_range[$customer->user_id] = $customer;
        }
    }
    return $customers_in_range;
}
Example #6
0
/**
 * This function is useful only when there's a railway with the same ordering for two points.
 **/
function is_way_inverted($railway_id, $type, $from_railway, $to_railway)
{
    $railway_ways_func = "get_" . $type . "_ways";
    $railway_by_ordering_func = "get_" . $type . "_way_by_ordering";
    $actual_way = $railway_by_ordering_func($railway_id, $from_railway['ordering']);
    // Looks for the previous way if it exists. Otherwise, finds the next one and change de comparision order.
    if ($from_railway['ordering'] > RAILWAY_MIN_ORDERING) {
        $previous_way = $railway_by_ordering_func($railway_id, $from_railway['ordering'] - 1);
        $reference_node = get_node_from_way_ids($actual_way, $previous_way);
        $invert_comparision = false;
    } else {
        $next_way = $railway_by_ordering_func($railway_id, $from_railway['ordering'] + 1);
        $reference_node = get_node_from_way_ids($actual_way, $next_way);
        $invert_comparision = true;
    }
    $from2reference_distance = calculate_distance($from_railway["node"]["lat"], $from_railway["node"]["lon"], $reference_node["lat"], $reference_node["lon"]);
    $to2reference_distance = calculate_distance($to_railway["node"]["lat"], $to_railway["node"]["lon"], $reference_node["lat"], $reference_node["lon"]);
    if ($from2reference_distance > $to2reference_distance ^ $invert_comparision) {
        return true;
    }
    return false;
}
function suburb($lat = 0, $lon = 0)
{
    // header('content-type: text/plain');
    require_once 'lib/idiorm.php';
    $databaseName = "govhack15";
    ORM::configure("mysql:host=mysql02.c7vaazdx09rf.ap-southeast-2.rds.amazonaws.com;dbname={$databaseName}");
    ORM::configure('username', 'ben');
    ORM::configure('password', 'Password1');
    // 4 queries for all 4 vector directions
    // $q1 = ORM::for_table('locations')->find_many();
    // var_dump($q1);
    $q1 = ORM::for_table('locations')->select_many('suburb', 'lat', 'lon')->where_gte('lat', $lat)->where_gte('lon', $lon)->limit(5)->order_by_asc('lat')->order_by_asc('lon')->find_many();
    $q2 = ORM::for_table('locations')->select_many('suburb', 'lat', 'lon')->where_gte('lat', $lat)->where_lt('lon', $lon)->limit(5)->order_by_asc('lat')->order_by_desc('lon')->find_many();
    $q3 = ORM::for_table('locations')->select_many('suburb', 'lat', 'lon')->where_lt('lat', $lat)->where_gte('lon', $lon)->limit(5)->order_by_desc('lat')->order_by_asc('lon')->find_many();
    $q4 = ORM::for_table('locations')->select_many('suburb', 'lat', 'lon')->where_lt('lat', $lat)->where_lt('lon', $lon)->limit(5)->order_by_desc('lat')->order_by_desc('lon')->find_many();
    // Select closest long
    $closest = false;
    $smallestStraightLineDist = 9999;
    foreach (array($q1, $q2, $q3, $q4) as $resultSet) {
        foreach ($resultSet as $record) {
            $thisSuburbDist = calculate_distance($lat, $lon, $record->lat, $record->lon);
            // echo "Checking $record->suburb, $record->lat, $record->lon, dist $thisSuburbDist m", PHP_EOL;
            if ($thisSuburbDist < $smallestStraightLineDist) {
                $smallestStraightLineDist = $thisSuburbDist;
                $closest = $record;
                // echo "---- NEW CLOSEST FOUND, $record->suburb", PHP_EOL;
            }
        }
    }
    // Now we have the closest suburb
    $final = ORM::for_table('locations')->where('suburb', $closest->suburb)->find_array();
    // echo "final", PHP_EOL;
    // var_dump($final);
    return $final[0];
    // json encode it later
}
Example #8
0
                $maxDistance = $r['total'];
            }
        }
        if ($maxDistance > 0) {
            foreach ($reindeer as $i => $r) {
                if ($r['total'] >= $maxDistance) {
                    $reindeer[$i]['points']++;
                }
            }
        }
    } while ($sec < $seconds);
    return $reindeer;
}
$max = 0;
$part2 = star2($reindeer, $total);
foreach ($reindeer as &$item) {
    $item['total'] = calculate_distance($item, $total);
    if ($max < $item['total']) {
        $max = $item['total'];
    }
}
$m2 = 0;
foreach ($part2 as $item) {
    if ($m2 < $item['points']) {
        $m2 = $item['points'];
    }
}
//print_r($reindeer);
echo "MAX: {$max}\n";
echo "PART@: {$m2}";
//print_r($part2);
function is_in_scanrange($uid, $sid)
{
    global $uid;
    // erstmal check ob eine eigene bzw. allieerte flotte im system ist
    $is_in_range = false;
    if (is_a_fleet_in_system($uid, $sid)) {
        $is_in_range = true;
    } else {
        // flotten von alliierten im system?
        $allied = get_allied_ids($uid);
        for ($i = 0; $i < sizeof($allied); $i++) {
            if (is_a_fleet_in_system($allied[$i], $sid)) {
                $is_in_range = true;
                break;
            }
        }
        // ist es denn wenigstens in irgendeinem scanradius des users oder eines allierten?
        if (!$is_in_range) {
            $sth = mysql_query("select max(radius) from scanradius");
            if (!$sth || mysql_num_rows($sth) == 0) {
                return 0;
            }
            $max_scanrange = mysql_fetch_row($sth);
            $systems = get_systems_in_scanrange($sid, $max_scanrange[0]);
            for ($i = 0; $i < sizeof($systems); $i++) {
                $own_star = is_own_star($systems[$i]);
                // auch ob er alliiert ist
                if ($own_star) {
                    // nen gebäude mit ner aussreichenden scanrange?
                    $sth = mysql_query("select p.uid,p.id,s.x,s.y from planets as p, systems as s where p.sid='{$systems[$i]}' and s.id='{$systems[$i]}'");
                    if (!$sth) {
                        return 0;
                    }
                    while ($planets = mysql_fetch_array($sth)) {
                        if ($planets["uid"] == $uid || is_allied($uid, $planets["uid"])) {
                            $planets_scan_range = get_max_scan_range_by_pid($planets["id"]);
                            if ($planets_scan_range) {
                                $system_coords = get_system_coords($sid);
                                $system_x = substr($system_coords, 0, strpos($system_coords, ":"));
                                $system_y = substr($system_coords, strpos($system_coords, ":") + 1);
                                $entfernung = calculate_distance($planets["x"], $planets["y"], $system_x, $system_y);
                                if ($entfernung <= $planets_scan_range) {
                                    $is_in_range = true;
                                }
                            }
                        }
                    }
                }
                // end if
                // oder vielleicht ne flotte?
                if (!$is_in_range) {
                    // $sth = mysql_query("select s.radius,fi.uid,sy.x,sy.y from scanradius as s,systems as sy, fleet as f, fleetinfo as fi where f.prod_id = s.prod_id and f.fid = fi.fid and fi.sid='$systems[$i]' and and sy.id='$systems[$i]'");
                    $sth = mysql_query("select fid, uid, sid from fleet_info where sid=" . $systems[$i] . "");
                    if (!$sth) {
                        return 0;
                    }
                    if (mysql_num_rows($sth) != 0) {
                        while ($flotten = mysql_fetch_array($sth)) {
                            if ($flotten["uid"] == $uid || is_allied($uid, $flotten["uid"])) {
                                $sth2 = mysql_query("select max(s.radius) from scanradius as s, fleet as f where f.fid=" . $flotten["fid"] . " and f.prod_id=s.prod_id");
                                $fleet_scan_range = mysql_fetch_row($sth2);
                                if ($fleet_scan_range) {
                                    $system_coords = get_system_coords($sid);
                                    $system_x = substr($system_coords, 0, strpos($system_coords, ":"));
                                    $system_y = substr($system_coords, strpos($system_coords, ":") + 1);
                                    $fleet_coords = get_system_coords($flotten["{$sid}"]);
                                    $fleet_x = substr($fleet_coords, 0, strpos($fleet_coords, ":"));
                                    $fleet_y = substr($fleet_coords, strpos($fleet_coords, ":") + 1);
                                    $entfernung = calculate_distance($fleet_x, $fleet_y, $system_x, $system_y);
                                    if ($entfernung <= $fleet_scan_range) {
                                        $is_in_range = true;
                                    }
                                }
                            }
                        }
                    }
                    //if (!$sth)
                    //return 0;
                }
            }
            // end for
        }
    }
    return $is_in_range;
}