class Nominatim in Geolocation Field 8.3
Same name and namespace in other branches
- 8.2 modules/geolocation_leaflet/src/Plugin/geolocation/Geocoder/Nominatim.php \Drupal\geolocation_leaflet\Plugin\geolocation\Geocoder\Nominatim
Provides the Nominatim API.
Plugin annotation
@Geocoder(
id = "nominatim",
name = @Translation("Nominatim"),
description = @Translation("See https://wiki.openstreetmap.org/wiki/Nominatim for details."),
locationCapable = true,
boundaryCapable = true,
frontendCapable = false,
reverseCapable = true,
)
Hierarchy
- class \Drupal\Component\Plugin\PluginBase implements DerivativeInspectionInterface, PluginInspectionInterface
- class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, MessengerTrait, StringTranslationTrait
- class \Drupal\geolocation\GeocoderBase implements ContainerFactoryPluginInterface, GeocoderInterface
- class \Drupal\geolocation_leaflet\Plugin\geolocation\Geocoder\Nominatim implements GeocoderInterface
- class \Drupal\geolocation\GeocoderBase implements ContainerFactoryPluginInterface, GeocoderInterface
- class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, MessengerTrait, StringTranslationTrait
Expanded class hierarchy of Nominatim
File
- modules/
geolocation_leaflet/ src/ Plugin/ geolocation/ Geocoder/ Nominatim.php, line 24
Namespace
Drupal\geolocation_leaflet\Plugin\geolocation\GeocoderView source
class Nominatim extends GeocoderBase implements GeocoderInterface {
/**
* Nominatim base URL.
*
* @var string
*/
protected static $nominatimBaseUrl = 'https://nominatim.openstreetmap.org';
/**
* {@inheritdoc}
*/
public function geocode($address) {
if (empty($address)) {
return FALSE;
}
$request_url_base = $this
->getRequestUrlBase();
$url = Url::fromUri($request_url_base . '/search/' . $address, [
'query' => [
'email' => $this
->getRequestEmail(),
'limit' => 1,
'format' => 'json',
'connect_timeout' => 5,
],
]);
try {
$result = Json::decode(\Drupal::httpClient()
->get($url
->toString())
->getBody());
} catch (RequestException $e) {
watchdog_exception('geolocation', $e);
return FALSE;
}
$location = [];
if (empty($result[0])) {
return FALSE;
}
else {
$location['location'] = [
'lat' => $result[0]['lat'],
'lng' => $result[0]['lon'],
];
}
if (!empty($result[0]['boundingbox'])) {
$location['boundary'] = [
'lat_north_east' => $result[0]['boundingbox'][1],
'lng_north_east' => $result[0]['boundingbox'][3],
'lat_south_west' => $result[0]['boundingbox'][0],
'lng_south_west' => $result[0]['boundingbox'][2],
];
}
if (!empty($result[0]['display_name'])) {
$location['address'] = $result[0]['display_name'];
}
return $location;
}
/**
* {@inheritdoc}
*/
public function reverseGeocode($latitude, $longitude) {
$request_url_base = $this
->getRequestUrlBase();
$url = Url::fromUri($request_url_base . '/reverse/', [
'query' => [
'lat' => $latitude,
'lon' => $longitude,
'email' => $this
->getRequestEmail(),
'limit' => 1,
'format' => 'json',
'connect_timeout' => 5,
'addressdetails' => 1,
'zoom' => 18,
],
]);
try {
$result = Json::decode(\Drupal::httpClient()
->get($url
->toString())
->getBody());
} catch (RequestException $e) {
watchdog_exception('geolocation', $e);
return FALSE;
}
if (empty($result['address'])) {
return FALSE;
}
$address_atomics = [];
foreach ($result['address'] as $component => $value) {
switch ($component) {
case 'house_number':
$address_atomics['houseNumber'] = $value;
break;
case 'road':
$address_atomics['road'] = $value;
break;
case 'town':
$address_atomics['village'] = $value;
break;
case 'city':
$address_atomics['city'] = $value;
break;
case 'county':
$address_atomics['county'] = $value;
break;
case 'postcode':
$address_atomics['postcode'] = $value;
break;
case 'state':
$address_atomics['state'] = $value;
break;
case 'country':
$address_atomics['country'] = $value;
break;
case 'country_code':
$address_atomics['countryCode'] = strtoupper($value);
break;
case 'suburb':
$address_atomics['suburb'] = $value;
break;
}
}
return [
'atomics' => $address_atomics,
'elements' => $this
->addressElements($address_atomics),
'formatted_address' => empty($result['display_name']) ? '' : $result['display_name'],
];
}
/**
* Retrieve base URL from setting or default.
*
* @return string
* Base URL.
*/
protected function getRequestUrlBase() {
$config = \Drupal::config('geolocation_leaflet.nominatim_settings');
if (!empty($config
->get('nominatim_base_url'))) {
$request_url = $config
->get('nominatim_base_url');
}
else {
$request_url = self::$nominatimBaseUrl;
}
return $request_url;
}
/**
* Nominatim should be called with a request E-Mail.
*
* @return string
* Get Request Email.
*/
protected function getRequestEmail() {
$config = \Drupal::config('geolocation_leaflet.nominatim_settings');
if (!empty($config
->get('nominatim_email'))) {
$request_email = $config
->get('nominatim_email');
}
else {
$request_email = \Drupal::config('system.site')
->get('mail');
}
return $request_email;
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
DependencySerializationTrait:: |
protected | property | An array of entity type IDs keyed by the property name of their storages. | |
DependencySerializationTrait:: |
protected | property | An array of service IDs keyed by property name used for serialization. | |
DependencySerializationTrait:: |
public | function | 1 | |
DependencySerializationTrait:: |
public | function | 2 | |
GeocoderBase:: |
protected | property | Country formatter manager. | |
GeocoderBase:: |
protected | function | Get formatted address elements from atomics. | |
GeocoderBase:: |
public static | function |
Creates an instance of the plugin. Overrides ContainerFactoryPluginInterface:: |
1 |
GeocoderBase:: |
public | function |
Attach geocoding logic to input element. Overrides GeocoderInterface:: |
4 |
GeocoderBase:: |
protected | function | Return plugin default settings. | 2 |
GeocoderBase:: |
public | function |
Return additional options form. Overrides GeocoderInterface:: |
2 |
GeocoderBase:: |
public | function | Return plugin settings. | |
GeocoderBase:: |
public | function |
Process the form built above. Overrides GeocoderInterface:: |
|
GeocoderBase:: |
public | function |
GoogleGeocoderBase constructor. Overrides PluginBase:: |
1 |
MessengerTrait:: |
protected | property | The messenger. | 29 |
MessengerTrait:: |
public | function | Gets the messenger. | 29 |
MessengerTrait:: |
public | function | Sets the messenger. | |
Nominatim:: |
protected static | property | Nominatim base URL. | |
Nominatim:: |
public | function |
Geocode an address. Overrides GeocoderBase:: |
|
Nominatim:: |
protected | function | Nominatim should be called with a request E-Mail. | |
Nominatim:: |
protected | function | Retrieve base URL from setting or default. | |
Nominatim:: |
public | function |
Reverse geocode an address. Overrides GeocoderBase:: |
|
PluginBase:: |
protected | property | Configuration information passed into the plugin. | 1 |
PluginBase:: |
protected | property | The plugin implementation definition. | 1 |
PluginBase:: |
protected | property | The plugin_id. | |
PluginBase:: |
constant | A string which is used to separate base plugin IDs from the derivative ID. | ||
PluginBase:: |
public | function |
Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the definition of the plugin implementation. Overrides PluginInspectionInterface:: |
3 |
PluginBase:: |
public | function |
Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface:: |
|
PluginBase:: |
public | function | Determines if the plugin is configurable. | |
StringTranslationTrait:: |
protected | property | The string translation service. | 1 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. |