Skip to content

Commit

Permalink
Merge branch 'master' of github.com:islam-dev/waqt.org
Browse files Browse the repository at this point in the history
  • Loading branch information
ahmedre committed Jul 28, 2011
2 parents 5481652 + 8e83d9f commit 701a103
Show file tree
Hide file tree
Showing 3 changed files with 171 additions and 14 deletions.
117 changes: 117 additions & 0 deletions geo.php
@@ -0,0 +1,117 @@
<?php
// implementation of geohasing functions
// based on http://github.com/davetroy/geohash-js/blob/master/geohash.js

class GeoHashUtils {
private static $base32 = '0123456789bcdefghjkmnpqrstuvwxyz';

private static $neighbors = array(
'odd' => array('bottom' => '238967debc01fg45kmstqrwxuvhjyznp',
'top' => 'bc01fg45238967deuvhjyznpkmstqrwx',
'left' => '14365h7k9dcfesgujnmqp0r2twvyx8zb',
'right' => 'p0r21436x8zb9dcf5h7kjnmqesgutwvy'),
'even' => array('right' => 'bc01fg45238967deuvhjyznpkmstqrwx',
'left' => '238967debc01fg45kmstqrwxuvhjyznp',
'top' => 'p0r21436x8zb9dcf5h7kjnmqesgutwvy',
'bottom' => '14365h7k9dcfesgujnmqp0r2twvyx8zb'));

private static $borders = array(
'odd' => array('bottom' => '0145hjnp', 'top' => 'bcfguvyz',
'left' => '028b', 'right' => 'prxz'),
'even' => array('right' => 'bcfguvyz', 'left' => '0145hjnp',
'top' => 'prxz', 'bottom' => '028b'));

private static $bits = array(16, 8, 4, 2, 1);
private static $latRange = array(-90.0, 90.0);
private static $lngRange = array(-180.0, 180.0);

public static function getNeighbors($geohash){
$neighbors = array();
$neighbors[0] = GeoHashUtils::calcNeighbors($geohash, 'top');
$neighbors[1] = GeoHashUtils::calcNeighbors($neighbors[0], 'right');
$neighbors[2] = GeoHashUtils::calcNeighbors($geohash, 'right');
$neighbors[3] = GeoHashUtils::calcNeighbors($neighbors[2], 'bottom');
$neighbors[4] = GeoHashUtils::calcNeighbors($geohash, 'bottom');
$neighbors[5] = GeoHashUtils::calcNeighbors($neighbors[4], 'left');
$neighbors[6] = GeoHashUtils::calcNeighbors($geohash, 'left');
$neighbors[7] = GeoHashUtils::calcNeighbors($neighbors[6], 'top');

return $neighbors;
}

public static function calcNeighbors($geohash, $direction){
$geohash = strtolower($geohash);
$last = $geohash[strlen($geohash)-1];
$type = (strlen($geohash) % 2)? 'odd' : 'even';
$base = substr($geohash, 0, strlen($geohash)-1);

$b = GeoHashUtils::$borders[$type];
$n = GeoHashUtils::$neighbors[$type];
$val = strpos($b[$direction], $last);
if (($val !== false) && ($val != -1))
$base = GeoHashUtils::calcNeighbors($base, $direction);

$ni = strpos($n[$direction], $last);
return $base . GeoHashUtils::$base32[$ni];
}

public static function deHashisize($geohash){
$isEven = true;
$lat = GeoHashUtils::$latRange;
$lng = GeoHashUtils::$lngRange;

for ($i=0; $i<strlen($geohash); $i++){
$c = $geohash[$i];
$cd = strpos(GeoHashUtils::$base32, $c);
for ($j=0; $j<5; $j++){
$mask = GeoHashUtils::$bits[$j];
$val = ($cd & $mask)? 0 : 1;
if ($isEven)
$lng[$val] = ($lng[0] + $lng[1]) / 2;
else $lat[$val] = ($lat[0] + $lat[1]) / 2;
$isEven = !$isEven;
}
}

return array('latitude' => $lat, 'longitude' => $lng);
}

public static function geoHashize($latitude, $longitude){
$ch = 0;
$bit = 0;
$geohash = "";
$isEven = true;
$precision = 12;
$lat = GeoHashUtils::$latRange;
$lng = GeoHashUtils::$lngRange;

while (strlen($geohash) < $precision){
if ($isEven){
$mid = ($lng[0] + $lng[1]) / 2;
if ($longitude > $mid){
$ch |= GeoHashUtils::$bits[$bit];
$lng[0] = $mid;
}
else $lng[1] = $mid;
}
else {
$mid = ($lat[0] + $lat[1]) / 2;
if ($latitude > $mid){
$ch |= GeoHashUtils::$bits[$bit];
$lat[0] = $mid;
}
else $lat[1] = $mid;
}

$isEven = !$isEven;
if ($bit < 4) $bit++;
else {
$geohash .= GeoHashUtils::$base32[$ch];
$bit = 0;
$ch = 0;
}
}

return $geohash;
}
}
58 changes: 44 additions & 14 deletions prayertimes.inc
Expand Up @@ -20,12 +20,50 @@ class PrayerTimes {
return unserialize($res);
}

public static function getTimezoneInfoFromCache($geohash){
mysql_pconnect('localhost', 'waqt', WAQT_SQL_PASSWORD);
mysql_select_db('waqt');

$q = 'select dst_offset, gmt_offset, timezone from ' .
"timezoneCache where geohash='$geohash'";
$res = mysql_query($q) or null;

$ret = null;
if (($res != null) & ($row = mysql_fetch_assoc($res))){
$ret = array();
$ret['dstOffset'] = $row['dst_offset'];
$ret['gmtOffset'] = $row['gmt_offset'];
$ret['timezoneId'] = $row['timezone'];
}

if ($ret != null)
return array('rescode' => 200, 'result' => $ret);
return $ret;
}

public static function saveTimezoneInfoToCache($geohash, $json){
$lat = $json['lat'];
$lng = $json['lng'];
$tz = mysql_real_escape_string($json['timezoneId']);
$rawOffset = $json['rawOffset'];
$dstOffset = $json['dstOffset'];
$gmtOffset = $json['gmtOffset'];

$q = 'insert into timezoneCache(geohash, latitude, longitude, ' .
'timezone, raw_offset, dst_offset, gmt_offset) values(' .
"'$geohash', $lat, $lng, '$tz', $rawOffset, $dstOffset, " .
"$gmtOffset)";
mysql_query($q);
}

public static function getTimezone($lat, $long){
require_once('geo.php');
$geohash = GeoHashUtils::geoHashize($lat, $long);
$ret = PrayerTimes::getTimezoneInfoFromCache($geohash);
if ($ret != null) return $ret;

$url = "http://ws.geonames.org/timezoneJSON?lat=$lat&lng=$long";

// xml interface
// $url = "http://ws.geonames.org/timezone?lat=$lat&lng=$long";

$url .= "&username=" . USERNAME;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
Expand All @@ -36,17 +74,9 @@ class PrayerTimes {
$ret = $info['http_code'];
curl_close($ch);

/* specifically for xml interface
$result = array();
if (preg_match('/\<timezoneId>(.*)<\/timezoneId>/', $res, $matches))
$result['timezoneId'] = $matches[1];
if (preg_match('/\<gmtOffset>(.*)<\/gmtOffset>/', $res, $matches))
$result['gmtOffset'] = $matches[1];
if (preg_match('/\<dstOffset>(.*)</dstOffset>/', $res, $matches))
$result['dstOffset'] = $matches[1];
return array('rescode' => $ret, 'result' => $result); */
return array('rescode' => $ret, 'result' => json_decode($res, true));
$json = json_decode($res, true);
PrayerTimes::saveTimezoneInfoToCache($geohash, $json);
return array('rescode' => $ret, 'result' => $json);
}

public static function getPrayerTimes($q, $method = 4){
Expand Down
10 changes: 10 additions & 0 deletions waqt.sql
@@ -0,0 +1,10 @@
create table timezoneCache(
geohash varchar(15) not null,
latitude double not null,
longitude double not null,
timezone varchar(50) not null,
raw_offset int not null,
dst_offset int not null,
gmt_offset int not null,
primary key(geohash)
) default character set 'utf8';

0 comments on commit 701a103

Please sign in to comment.