PHP code example of ricorocks-digital-agency / geotools
1. Go to this page and download the library: Download ricorocks-digital-agency/geotools library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
ricorocks-digital-agency / geotools example snippets
use League\Geotools\Coordinate\Coordinate;
use League\Geotools\Coordinate\Ellipsoid;
// from an \Geocoder\Model\Address instance within Airy ellipsoid
$coordinate = new Coordinate($geocoderResult, Ellipsoid::createFromName(Ellipsoid::AIRY));
// or in an array of latitude/longitude coordinate within GRS 1980 ellipsoid
$coordinate = new Coordinate([48.8234055, 2.3072664], Ellipsoid::createFromName(Ellipsoid::GRS_1980));
// or in latitude/longitude coordinate within WGS84 ellipsoid
$coordinate = new Coordinate('48.8234055, 2.3072664');
// or in degrees minutes seconds coordinate within WGS84 ellipsoid
$coordinate = new Coordinate('48°49′24″N, 2°18′26″E');
// or in decimal minutes coordinate within WGS84 ellipsoid
$coordinate = new Coordinate('48 49.4N, 2 18.43333E');
// the result will be:
printf("Latitude: %F\n", $coordinate->getLatitude()); // 48.8234055
printf("Longitude: %F\n", $coordinate->getLongitude()); // 2.3072664
printf("Ellipsoid name: %s\n", $coordinate->getEllipsoid()->getName()); // WGS 84
printf("Equatorial radius: %F\n", $coordinate->getEllipsoid()->getA()); // 6378136.0
printf("Polar distance: %F\n", $coordinate->getEllipsoid()->getB()); // 6356751.317598
printf("Inverse flattening: %F\n", $coordinate->getEllipsoid()->getInvF()); // 298.257224
printf("Mean radius: %F\n", $coordinate->getEllipsoid()->getArithmeticMeanRadius()); // 6371007.772533
// it's also possible to modify the coordinate without creating an other coodinate
$coordinate->setFromString('40°26′47″N 079°58′36″W');
printf("Latitude: %F\n", $coordinate->getLatitude()); // 40.446388888889
printf("Longitude: %F\n", $coordinate->getLongitude()); // -79.976666666667
$geotools = new \League\Geotools\Geotools();
$coordinate = new \League\Geotools\Coordinate\Coordinate('40.446195, -79.948862');
$converted = $geotools->convert($coordinate);
// convert to decimal degrees without and with format string
printf("%s\n", $converted->toDecimalMinutes()); // 40 26.7717N, -79 56.93172W
printf("%s\n", $converted->toDM('%P%D°%N %p%d°%n')); // 40°26.7717 -79°56.93172
// convert to degrees minutes seconds without and with format string
printf("%s\n", $converted->toDegreesMinutesSeconds('<p>%P%D:%M:%S, %p%d:%m:%s</p>')); // <p>40:26:46, -79:56:56</p>
printf("%s\n", $converted->toDMS()); // 40°26′46″N, 79°56′56″W
// convert in the UTM projection (standard format)
printf("%s\n", $converted->toUniversalTransverseMercator()); // 17T 589138 4477813
printf("%s\n", $converted->toUTM()); // 17T 589138 4477813 (alias)
$geocoder = new \Geocoder\ProviderAggregator(); // or \Geocoder\TimedGeocoder
$httpClient = HttpClientDiscovery:::find();
$geocoder->registerProviders([
new \Geocoder\Provider\GoogleMaps\GoogleMaps($httpClient),
new \Geocoder\Provider\OpenStreetMap\OpenStreetMap($httpClient),
new \Geocoder\Provider\BingMaps\BingMaps($httpClient, '<FAKE_API_KEY>'), // throws InvalidCredentialsException
new \Geocoder\Provider\Yandex\Yandex($httpClient),
new \Geocoder\Provider\FreeGeoIp\FreeGeoIp($httpClient),
new \Geocoder\Provider\Geoip\Geoip(),
]);
try {
$geotools = new \League\Geotools\Geotools();
$cache = new \Cache\Adapter\PHPArray\ArrayCachePool();
$results = $geotools->batch($geocoder)->setCache($cache)->geocode([
'Paris, France',
'Copenhagen, Denmark',
'74.200.247.59',
'::ffff:66.147.244.214'
])->parallel();
} catch (\Exception $e) {
die($e->getMessage());
}
$dumper = new \Geocoder\Dumper\WktDumper();
foreach ($results as $result) {
// if a provider throws an exception (UnsupportedException, InvalidCredentialsException ...)
// an custom /Geocoder/Result/Geocoded instance is returned which embedded the name of the provider,
// the query string and the exception string. It's possible to use dumpers
// and/or formatters from the Geocoder library.
printf("%s|%s|%s\n",
$result->getProviderName(),
$result->getQuery(),
'' == $result->getExceptionMessage() ? $dumper->dump($result) : $result->getExceptionMessage()
);
}
// ... $geocoder like the previous example ...
// If you want to reverse one coordinate
try {
$results = $geotools->batch($geocoder)->reverse(
new \League\Geotools\Coordinate\Coordinate([2.307266, 48.823405])
)->parallel();
} catch (\Exception $e) {
die($e->getMessage());
}
// Or if you want to reverse geocoding 3 coordinates
$coordinates = [
new \League\Geotools\Coordinate\Coordinate([2.307266, 48.823405]),
new \League\Geotools\Coordinate\Coordinate([12.568337, 55.676097]),
new \League\Geotools\Coordinate\Coordinate('-74.005973 40.714353')),
];
$results = $geotools->batch($geocoder)->reverse($coordinates)->parallel();
// ...
php
$myEllipsoid = \League\Geotools\Coordinate\Ellipsoid::createFromArray([
'name' => 'My Ellipsoid', // The name of the Ellipsoid
'a' => 123.0, // The semi-major axis (equatorial radius) in meters
'invF' => 456.0 // The inverse flattening
]);