diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..08d6b7c --- /dev/null +++ b/LICENSE @@ -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. diff --git a/README.md b/README.md index b64132c..1ca3049 100644 --- a/README.md +++ b/README.md @@ -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 ------------ @@ -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. + + diff --git a/config.php b/config.php new file mode 100644 index 0000000..638c374 --- /dev/null +++ b/config.php @@ -0,0 +1,4 @@ + false)); -/* $query = $db->prepare('CREATE TABLE "timezone" ( "id" SERIAL, "location" "geography", @@ -14,7 +13,6 @@ $query = $db->prepare('ALTER TABLE "timezone" OWNER TO gisgroup'); $query->execute(); -*/ $data = file('tz_cities.txt'); diff --git a/inc.php b/inc.php index efb6fdf..261a6a3 100644 --- a/inc.php +++ b/inc.php @@ -1,7 +1,5 @@ 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()) { @@ -42,4 +29,4 @@ function timezoneFromLocation($latitude, $longitude) { function get($k) { return array_key_exists($k, $_GET) ? $_GET[$k] : FALSE; -} \ No newline at end of file +}