4 public function __construct()
6 $file = __DIR__ . '/../cache/geocode.sq3';
7 $new = !file_exists($file);
8 $this->db = new PDO('sqlite:' . $file);
10 $this->db->exec(<<<SQL
20 $this->fetchStmt = $this->db->prepare(
21 'SELECT address, lat, lon FROM coords WHERE address = :address'
23 $this->insertStmt = $this->db->prepare(
25 . '(address, lat, lon, expiration)'
27 . '(:address, :lat, :lon, :expiration)'
31 public function getPrivateCoords($entry)
33 return $this->getCoords($entry['homestreet']);
36 public function getBusinessCoords($entry)
38 if (!isset($entry['street']) || !isset($entry['location'])) {
41 return $this->getCoords(
42 $entry['street'] . ', ' . $entry['zip'] . ' ' . $entry['location']
43 . ', ' . $entry['state'] . ', ' . $entry['country']
47 public function getCoords($strAddress)
49 $strAddress = str_replace(array("\r", "\n", ' OT '), ' ', $strAddress);
50 $this->fetchStmt->execute(array(':address' => $strAddress));
51 $coords = $this->fetchStmt->fetchObject();
52 if ($coords !== false) {
53 if ($coords->lat === null) {
59 $coords = $this->geoCode($strAddress);
60 if ($coords === null) {
61 $this->insertStmt->execute(
63 'address' => $strAddress,
66 'expiration' => time() + 86400
72 $this->insertStmt->execute(
74 'address' => $strAddress,
75 'lat' => $coords->lat,
76 'lon' => $coords->lon,
77 'expiration' => time() + 86400 * 7
83 public function geoCode($strAddress)
85 //Docs: http://open.mapquestapi.com/nominatim/
86 $url = 'http://open.mapquestapi.com/nominatim/v1/search'
89 . '&q=' . urlencode($strAddress);
91 $context = stream_context_create(
94 'user_agent' => 'ConTagged LDAP address book'
98 $json = file_get_contents($url, false, $context);
99 if ($json === false) {
102 $data = json_decode($json);
103 if ($data === null || $data === false) {
107 if (count($data) == 0) {
111 return (object) array(
112 'lat' => $data[0]->lat,
113 'lon' => $data[0]->lon,
114 'address' => $strAddress
118 public function cleanup()
121 'DELETE FROM coords WHERE expiration < ' . time()