Skip to content

Commit

Permalink
Add setup instructions and separate out config file with database con…
Browse files Browse the repository at this point in the history
…nection info.
  • Loading branch information
aaronpk committed Aug 31, 2012
1 parent 5accf10 commit 5c4daa3
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 20 deletions.
30 changes: 30 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
BSD License
-----------

Copyright (c) 2012 by Geoloqi, Inc.
All rights reserved.

Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:

1. Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.

2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.

3. Neither the name of Django nor the names of its contributors may be used
to endorse or promote products derived from this software without
specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 changes: 33 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,31 @@
Example
-------

curl -u geoloqi:api "http://timezone-api.geoloqi.com/timezone?latitude=45.5118&longitude=-122.6433"
curl "http://api.example.com/timezone?latitude=45.5118&longitude=-122.6433"

or, if you don't like query string parameters:

curl -u geoloqi:api http://timezone-api.geoloqi.com/timezone/45.5118/-122.6433
curl http://api.example.com/timezone/45.5118/-122.6433

Example response:

{
timezone: "America/Los_Angeles",
offset: "-07:00",
seconds: -25200
}

Setup
-----

You will first need to create a PostGIS-enabled database.

Set up your database config in `config.php`, then run `import.php` which will create
the timezone table and import all the data from the tz_cities.txt file.

After configuring Nginx or another web server, you will be able to make requests
like the example above.


Nginx Config
------------
Expand All @@ -31,3 +51,14 @@ Nginx Config
}
}

License
-------

This code is available under the BSD license. See LICENSE for full details.

Data compiled from the GeoNames.org `cities15000.zip` file available here:
http://download.geonames.org/export/dump/ under a Creative Commons Attribution 3.0 License.

The Data is provided "as is" without warranty or any representation of accuracy, timeliness or completeness.


4 changes: 4 additions & 0 deletions config.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?php
define('PDO_DSN', 'pgsql:dbname=timezone;host=127.0.0.1');
define('PDO_USER', 'root');
define('PDO_PASS', '');
2 changes: 0 additions & 2 deletions import.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
$db = new PDO(PDO_DSN, PDO_USER, PDO_PASS, array(PDO::ATTR_PERSISTENT => false));


/*
$query = $db->prepare('CREATE TABLE "timezone" (
"id" SERIAL,
"location" "geography",
Expand All @@ -14,7 +13,6 @@

$query = $db->prepare('ALTER TABLE "timezone" OWNER TO gisgroup');
$query->execute();
*/


$data = file('tz_cities.txt');
Expand Down
19 changes: 3 additions & 16 deletions inc.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
<?php
define('PDO_DSN', 'pgsql:dbname=timezone;host=127.0.0.1');
define('PDO_USER', 'loqi');
define('PDO_PASS', 'tinydino');
include('config.php');

function timezoneFromLocation($latitude, $longitude) {
global $db;
Expand All @@ -16,21 +14,10 @@ function timezoneFromLocation($latitude, $longitude) {
$result = $tz['timezone'];
}

// $latN = $latitude - 1 % 180;
// $latS = $latitude + 1 % 180;
// $lngE = $longitude + 1 % 360;
// $lngW = $longitude - 1 % 360;

// $timezones = $db->prepare("SELECT timezone FROM timezone WHERE lat > $latN AND lat <= $latS AND lng > $lngW AND lng < $lngE ORDER BY st_distance(ST_GeographyFromText('SRID=4326;POINT(" . $longitude . ' ' . $latitude . ")'), location) LIMIT 1");
// $timezones->execute();
// while($tz = $timezones->fetch()) {
// $result = $tz['timezone'];
// }

if($result)
return $result;

// If nothing found that close, check again within 6000km
// If nothing was found that close, check again within 6000km
$timezones = $db->prepare("SELECT timezone FROM timezone WHERE ST_DWithin(ST_GeographyFromText('SRID=4326;POINT(" . $longitude . ' ' . $latitude . ")'), location, 6000000, false) order by st_distance(ST_GeographyFromText('SRID=4326;POINT(" . $longitude . ' ' . $latitude . ")'), location) LIMIT 1");
$timezones->execute();
while($tz = $timezones->fetch()) {
Expand All @@ -42,4 +29,4 @@ function timezoneFromLocation($latitude, $longitude) {

function get($k) {
return array_key_exists($k, $_GET) ? $_GET[$k] : FALSE;
}
}

0 comments on commit 5c4daa3

Please sign in to comment.