Permalink
Browse files

Initial import

  • Loading branch information...
toin0u committed Feb 6, 2013
0 parents commit 59509d7eda725b25d45362518731992bdc5de663
@@ -0,0 +1,6 @@
vendor/
composer.lock
composer.phar
phpunit.xml
php-cs-fixer.phar
.DS_Store
@@ -0,0 +1,12 @@
language: php
php:
- 5.3
- 5.4
- 5.5
before_script:
- curl -s http://getcomposer.org/installer | php
- php composer.phar install --dev --prefer-source
script: phpunit --coverage-text
@@ -0,0 +1,6 @@
CHANGELOG
=========
### 0.1.0 (????-??-??) ###
n/a
19 LICENSE
@@ -0,0 +1,19 @@
Copyright (c) 2013 Antoine Corcy <contact@sbin.dk>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is furnished
to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
203 README.md
@@ -0,0 +1,203 @@
Geotools
========
**Geotools** is a PHP geo-related library, built atop [Geocoder](https://github.com/willdurand/Geocoder) and
[React](https://github.com/reactphp/react) libraries.
Features
--------
* Batch geocode & reverse process in **serie** / in **parallel**.
* Calcul the distance in **meter** (by default), **km** or **mile** between two coordinates using **flat**,
**haversine** or **vincenty** algorythms.
* Calcul the **initial bearing** from the origin coordinate to the destination coordinate in degrees.
* Calcul the **caridnal point** (direction) from the origin coordinate to the destination coordinate.
* Calcul the **half-way point** (coordinate) between the origin and the destination coordinates.
* Calcul the **destination point** with given bearing in degrees and a distance in meters.
* ... more to come ...
Installation
------------
The recommended way to install Geotools is through [composer](http://getcomposer.org).
Run these commands to install composer, Geotools and its dependencies:
``` bash
% wget http://getcomposer.org/composer.phar
% php composer.phar init --require="toin0u/geotools:*" -n
% php composer.phar install
```
Now you can add the autoloader, and you will have access to the library:
``` php
<?php
require 'vendor/autoload.php';
```
If you don't use neither **Composer** nor a _ClassLoader_ in your application, just require the provided autoloader:
``` php
<?php
require_once 'src/autoload.php';
```
Usage & API
-----------
### Coordinate ###
**Geotools** is built atop [Geocoder](https://github.com/willdurand/Geocoder). It means it's possible to use the
`/Geocoder/Result/ResultInterface` directly but it's also possible to use a simple array with its latitude and
longitude.
``` php
<?php
$coordinate = new \Geotools\Coordinate\Coordinate($geocoderResult);
// or
$coordinate = new \Geotools\Coordinate\Coordinate(array(48.8234055, 2.3072664));
// ...
```
### Batch ###
It provides a very handy way to batch geocode and reverse in serie and in parallel.
Thanks to [Geocoder](https://github.com/willdurand/Geocoder) and [React](https://github.com/reactphp/react) libraries.
```php
<?php
$geocoder = new \Geocoder\Geocoder();
$adapter = new \Geocoder\HttpAdapter\CurlHttpAdapter();
$geocoder->registerProviders(array(
new \Geocoder\Provider\GoogleMapsProvider($adapter), // ok
new \Geocoder\Provider\OpenStreetMapsProvider($adapter), // ok
new \Geocoder\Provider\BingMapsProvider($adapter, '<FAKE_API_KEY>'), // throws InvalidCredentialsException
new \Geocoder\Provider\YandexProvider($adapter), // ok
new \Geocoder\Provider\FreeGeoIpProvider($adapter), // throws UnsupportedException
new \Geocoder\Provider\HostIpProvider($adapter), // throws UnsupportedException
new \Geocoder\Provider\GeoipProvider(), // throws UnsupportedException
));
$geotools = new \Geotools\Geotools();
$results = $geotools->batch($geocoder)->geocode('10 rue Gambetta, Paris, France')->parallel();
$dumper = new \Geocoder\Dumper\WktDumper();
foreach ($results as $providerName => $providerResult) {
// if a provider throws an exception (UnsupportedException, InvalidCredentialsException ...)
// an empty /Geocoder/Result/Geocoded instance is returned so we can easily use a dumper
// and/or formatter from the Geocoder library
printf("%s: %s\n", $providerName, $dumper->dump($providerResult));
}
```
You should get something like:
```
google_maps: POINT(2.307266 48.823405)
openstreetmaps: POINT(2.391636 48.863936)
bing_maps: POINT(0.000000 0.000000)
yahoo: POINT(2.389020 48.863281)
yandex: POINT(2.225684 48.874010)
free_geo_ip: POINT(0.000000 0.000000)
host_ip: POINT(0.000000 0.000000)
geoip: POINT(0.000000 0.000000)
```
Batch reverse geocoding is something like:
``` php
<?php
// ...
$coordinate = new \Geotools\Coordinate\Coordinate(array(48.8234055, 2.3072664));
$results = $geotools->batch($geocoder)->reverse($coordinate)->parallel();
// ...
```
### Distance ###
It provides tools to calculate the distance in meter (by default), km or mile between two coordinates
using flat (most performant), haversine or vincenty (most accurate) algorythms.
``` php
<?php
$geotools = new \Geotools\Geotools();
$coordA = new \Geotools\Coordinate\Coordinate(array(48.8234055, 2.3072664));
$coordB = new \Geotools\Coordinate\Coordinate(array(43.296482, 5.36978));
echo $geotools->from($coordA)->to($coordB)->distance()->in('km')->flat(); // 659.16650524477
echo $geotools->from($coordA)->to($coordB)->distance()->in('m')->haversine(); // 659.02191298475
echo $geotools->from($coordA)->to($coordB)->distance()->in('mile')->vincenty(); // 658.30753717626
```
### Point ###
It provides tools to calculate the initial bearing in degrees, the cardinal direction, the middle point
and the destination point. The middle and the destination points returns a `\Geotools\Coordinate\Coordinate` object.
``` php
<?php
$geotools = new \Geotools\Geotools();
$coordA = new \Geotools\Coordinate\Coordinate(array(48.8234055, 2.3072664));
$coordB = new \Geotools\Coordinate\Coordinate(array(43.296482, 5.36978));
echo $geotools->from($coordA)->to($coordB)->point()->bearing(); // 157 (degrees)
echo $geotools->from($coordA)->to($coordB)->point()->cardinal(); // SSE (SouthEastEast)
$middlePoint = $geotools->from($coordA)->to($coordB)->point()->middle(); // \Geotools\Coordinate\Coordinate
echo $middlePoint->getLatitude(); // 46.070143125815
echo $middlePoint->getLongitude(); // 3.9152401085931
$destinationPoint = $geotools->from($coordA)->point()->destination(180, 200000); // \Geotools\Coordinate\Coordinate
echo $destinationPoint->getLatitude(); // 47.026774663314
echo $destinationPoint->getLongitude(); // 2.3072664
```
Unit Tests
----------
Rename the `phpunit.xml.dist` file to `phpunit.xml`.
``` bash
% php composer.phar install --dev
```
Once installed, just launch the following command:
``` bash
% phpunit
```
Credits
-------
* Antoine Corcy <contact@sbin.dk>
* [All contributors](https://github.com/toin0u/Geotools/contributors)
* **Geotools** is influenced by [Geokit](https://github.com/jsor/Geokit),
[Geotools-for-CodeIgniter](https://github.com/weejames/Geotools-for-CodeIgniter),
[geotools-php](https://github.com/jillesvangurp/geotools-php) and so on...
Changelog
---------
[See the changelog file](CHANGELOG.md)
License
-------
Geotools is released under the MIT License. See the bundled [LICENSE](LICENSE) file for details.
@@ -0,0 +1,3 @@
<?php
require_once __DIR__ . '/vendor/autoload.php';
@@ -0,0 +1,35 @@
{
"name": "toin0u/geotools",
"type": "library",
"description": "Geo-related tools PHP 5.3 library",
"keywords": ["geotools", "geometry", "geocoder", "geocoding", "geoip", "bounds", "distance", "batch"],
"homepage": "https://github.com/toin0u/Geotools",
"license": "MIT",
"authors": [
{
"name": "Antoine Corcy",
"email": "contact@sbin.dk",
"homepage": "http://sbin.dk"
}
],
"support": {
"issues": "https://github.com/toin0u/Geotools/issues",
"source": "https://github.com/toin0u/Geotools"
},
"require": {
"php": ">=5.3.0",
"willdurand/geocoder": "1.2.*",
"react/async": "dev-master"
},
"require-dev": {
"react/react": "0.2.*"
},
"autoload": {
"psr-0": { "Geotools": "src/" }
},
"extra": {
"branch-alias": {
"dev-master": "0.1-dev"
}
}
}
@@ -0,0 +1,23 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit backupGlobals="false"
backupStaticAttributes="false"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false"
syntaxCheck="false"
bootstrap="tests/bootstrap.php"
>
<testsuites>
<testsuite name="Geotools Test Suite">
<directory>./tests/</directory>
</testsuite>
</testsuites>
<filter>
<whitelist>
<directory>./src/Geotools/</directory>
</whitelist>
</filter>
</phpunit>
@@ -0,0 +1,75 @@
<?php
/**
* This file is part of the Geotools library.
*
* (c) Antoine Corcy <contact@sbin.dk>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Geotools;
/**
* @author Antoine Corcy <contact@sbin.dk>
*/
abstract class AbstractGeotools
{
/**
* The radius of the Earth in meters.
* @see http://en.wikipedia.org/wiki/Earth_radius
*
* @var double
*/
const EARTH_RADIUS = 6378136.047;
/**
* The ratio meters per mile.
*
* @var double
*/
const METERS_PER_MILE = 1609.344;
/**
* The kilometer unit.
*
* @var string
*/
const KILOMETER_UNIT = 'km';
/**
* The mile unit.
*
* @var string
*/
const MILE_UNIT = 'mile';
/**
* The cardinal points / directions (the four cardinal directions,
* the four ordinal directions, plus eight further divisions).
*
* @var array
*/
protected $cardinalPoints = array(
'N', 'NNE', 'NE', 'ENE',
'E', 'ESE', 'SE', 'SSE',
'S', 'SSW', 'SW', 'WSW',
'W', 'WNW', 'NW', 'NNW',
'N'
);
/**
* The origin coordinate.
*
* @var CoordinateInterface
*/
protected $from;
/**
* The destination coordinate.
*
* @var CoordinateInterface
*/
protected $to;
}
Oops, something went wrong.

0 comments on commit 59509d7

Please sign in to comment.