Geocoder
Since Camel 2.12
Only producer is supported
The Geocoder component is used for looking up geocodes (latitude and longitude) for a given address, or reverse lookup.
The component uses either a hosted Nominatim server or the Java API for Google Geocoder library.
Maven users will need to add the following dependency to their pom.xml for this component:
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-geocoder</artifactId>
<version>x.x.x</version>
<!-- use the same version as your Camel core version -->
</dependency> Configuring Options
Camel components are configured on two separate levels:
-
component level
-
endpoint level
Configuring Component Options
At the component level, you set general and shared configurations that are, then, inherited by the endpoints. It is the highest configuration level.
For example, a component may have security settings, credentials for authentication, urls for network connection and so forth.
Some components only have a few options, and others may have many. Because components typically have pre-configured defaults that are commonly used, then you may often only need to configure a few options on a component; or none at all.
You can configure components using:
-
the Component DSL.
-
in a configuration file (
application.properties,*.yamlfiles, etc). -
directly in the Java code.
Configuring Endpoint Options
You usually spend more time setting up endpoints because they have many options. These options help you customize what you want the endpoint to do. The options are also categorized into whether the endpoint is used as a consumer (from), as a producer (to), or both.
Configuring endpoints is most often done directly in the endpoint URI as path and query parameters. You can also use the Endpoint DSL and DataFormat DSL as a type safe way of configuring endpoints and data formats in Java.
A good practice when configuring options is to use Property Placeholders.
Property placeholders provide a few benefits:
-
They help prevent using hardcoded urls, port numbers, sensitive information, and other settings.
-
They allow externalizing the configuration from the code.
-
They help the code to become more flexible and reusable.
The following two sections list all the options, firstly for the component followed by the endpoint.
Component Options
The Geocoder component supports the following options which are listed below.
| Name | Description | Default | Type |
|---|---|---|---|
Whether the producer should be started lazy (on the first message). By starting lazy you can use this to allow CamelContext and routes to startup in situations where a producer may otherwise fail during starting and cause the route to fail being started. By deferring this startup to be lazy then the startup failure can be handled during routing messages via Camel’s routing error handlers. Beware that when the first message is processed then creating and starting the producer may take a little time and prolong the total processing time of the processing. | false | boolean | |
Whether autowiring is enabled. This is used for automatic autowiring options (the option must be marked as autowired) by looking up in the registry to find if there is a single instance of matching type, which then gets configured on the component. This can be used for automatic configuring JDBC data sources, JMS connection factories, AWS Clients, etc. | true | boolean | |
To use a custom com.google.maps.GeoApiContext for the requests to Google. When it is set, the apiKey, clientId, clientKey and proxy options of the endpoints are not used. | GeoApiContext |
Endpoint Options
The Geocoder endpoint is configured using URI syntax:
geocoder:address:latlng
With the following path and query parameters:
Path Parameters
| Name | Description | Default | Type |
|---|---|---|---|
The address to look up the latitude and longitude of, such as Paris, France, written after address: in the URI. With Google, the address current looks up the location of the IP address that the request comes from. The CamelGeoCoderAddress header replaces this option. | String | ||
The latitude and longitude to look up the address of, written after latlng: in the URI as two decimal numbers separated by a comma, such as 48.8566,2.3522. The CamelGeoCoderLatlng header replaces this option. A latitude and longitude, from this option or the header, takes precedence over an address. | String |
Query Parameters
| Name | Description | Default | Type |
|---|---|---|---|
Whether to leave the message body as it is and only set the headers. If false, the producer sets the body to the result: an array of com.google.maps.model.GeocodingResult for Google, or the JSON response of Nominatim as a String. | false | boolean | |
The language of the answer (such as the address, the country and the city), as a language code such as en, de or fr. It is sent as the accept-language parameter to Nominatim and as the language parameter to Google. | en | String | |
The base URL of the Nominatim server, such as https://nominatim.openstreetmap.org, to which the producer adds /search or /reverse. Required when type is NOMINATIM, and not used for Google. | String | ||
The geocoding service: GOOGLE for the Google Geocoding API, or NOMINATIM for the Nominatim server at serverUrl. If not set, Google is used. Enum values:
| GeoCoderType | ||
Whether the producer should be started lazy (on the first message). By starting lazy you can use this to allow CamelContext and routes to startup in situations where a producer may otherwise fail during starting and cause the route to fail being started. By deferring this startup to be lazy then the startup failure can be handled during routing messages via Camel’s routing error handlers. Beware that when the first message is processed then creating and starting the producer may take a little time and prolong the total processing time of the processing. | false | boolean | |
The domain for NTLM proxy authentication. The component does not use this option, as proxyAuthMethod accepts only Basic. | String | ||
The host for NTLM proxy authentication. The component does not use this option, as proxyAuthMethod accepts only Basic. | String | ||
The authentication method for the proxy. Only Basic is supported: when it is set, the producer authenticates to the proxy with proxyAuthUsername and proxyAuthPassword, which are sent Base64 encoded in the Proxy-Authorization header. Another value, such as Digest, fails when the producer starts. Enum values:
| String | ||
The password for the proxy authentication, used when proxyAuthMethod is set. | String | ||
The user name for the proxy authentication, used when proxyAuthMethod is set. | String | ||
The host name of the HTTP proxy for the requests to Google. The proxy is used only when proxyPort is also set, and not for Nominatim. | String | ||
The port of the HTTP proxy for the requests to Google. The proxy is used only when proxyHost is also set. | Integer | ||
The API key of the Google Maps Platform, which the producer uses when clientId is not set. Google needs either this key or clientId and clientKey. It is not used for Nominatim. | String | ||
The client ID of the Google Maps Platform Premium Plan. When it is set, the producer signs the requests to Google with it and clientKey instead of using apiKey. The geolocation of the address current does not accept a client ID. | String | ||
The URL signing secret (cryptographic key) that belongs to clientId, with which the producer signs the requests to Google. | String |
Message Headers
The Geocoder component supports the following message header(s), which is/are listed below:
| Name | Description | Default | Type |
|---|---|---|---|
CamelGeoCoderAddress (producer) Constant: | The address. The producer reads it as the address to look up, which replaces the address of the endpoint, and sets it to the formatted address of the first result. | String | |
CamelGeoCoderLatlng (producer) Constant: | The latitude and longitude, separated by a comma. The producer reads it as the location to look up, which replaces the latlng of the endpoint and takes precedence over an address, and sets it to the location of the first result. For Nominatim, it sets a space after the comma. | String | |
| Constant: | The latitude of the first result, with 8 decimals. | String | |
| Constant: | The longitude of the first result, with 8 decimals. | String | |
CamelGeoCoderStatus (producer) Constant: | Required The status of the lookup: OK, or ZERO_RESULTS when no place is found. When Google rejects the request, it is REQUEST_DENIED, OVER_QUERY_LIMIT, OVER_DAILY_LIMIT, INVALID_REQUEST or UNKNOWN_ERROR, and for an error answer of Nominatim it is ERROR. The producer sets the other headers only when the status is OK, except that for the address current it sets CamelGeoCoderLatlng to the geolocated location also when the status is ZERO_RESULTS. Enum values:
| GeocoderStatus | |
CamelGeoCoderRegionCode (producer) Constant: | The code of the first-level administrative area, such as a state. With Google, the short name of the administrative_area_level_1 address component, and with Nominatim the state_code of the address in upper case. | String | |
CamelGeoCoderRegionName (producer) Constant: | The name of the first-level administrative area, such as a state. With Google, the long name of the administrative_area_level_1 address component, and with Nominatim the state of the address. | String | |
| Constant: | The name of the city. With Google, the long name of the locality address component, and with Nominatim the city of the address. | String | |
CamelGeoCoderCountryLong (producer) Constant: | The name of the country, such as France. | String | |
CamelGeoCoderCountryShort (producer) Constant: | The two-letter country code (ISO 3166-1 alpha-2) in upper case, such as FR. | String | |
CamelGeoCoderPostalCode (producer) Constant: | The postal code. | String |
Message Body
The message body at each side of the Geocoder component: Consumer is the body of the messages the consumer creates, Consumer reply the bodies a consumer that answers its caller can reply with, Producer input the bodies the producer can send or write, and Producer output the body after the producer.
| Side | Body | Description |
|---|---|---|
Producer input |
| The body is not read: the address or the latitude and longitude come from the CamelGeoCoderAddress or CamelGeoCoderLatlng header, or else from the endpoint. |
Producer output |
| With Google, the array of com.google.maps.model.GeocodingResult, empty for no results; unchanged when Google denies the request, with the status in the CamelGeoCoderStatus header. With Nominatim, its jsonv2 response as text: an array of one place for an address (empty for no results), an object for a latitude and longitude. The first result is also in the headers.
|
The bodies are the ones with the default options. A side that is not listed is not known, and tooling does not guess it. The carriers, the sides and the tools that use them are described in Message Body Types.
Exchange data format
Notice not all headers may be provided depending on available data and mode in use (address vs. latlng).
Body using a Nominatim Server
Camel will deliver the body as a String with the JSON response of Nominatim (the jsonv2 format): an array with one place for an address, and an object for a latitude and longitude. When Nominatim finds no place (an empty array for an address, or {"error":"Unable to geocode"} for a latitude and longitude), the CamelGeoCoderStatus header is ZERO_RESULTS, and when it answers with an error, the header is ERROR.
Body using a Google Server
Camel will deliver the body as an array of com.google.maps.model.GeocodingResult (empty when there are no results).
If the address is "current" then the current location is found by geolocation, and the body is the array of GeocodingResult of that location as well. When Google denies the request, the body is left as-is and the CamelGeoCoderStatus header has the status.
If the option headersOnly is set to true then the message body is left as-is, and only headers will be added to the Exchange.
Using a proxy
The requests to Google can go through an HTTP proxy, set with proxyHost and proxyPort. The proxy is not used for Nominatim.
When the proxy needs authentication, set proxyAuthMethod=Basic with proxyAuthUsername and proxyAuthPassword. Only Basic authentication is supported: the user name and password are sent Base64 encoded in the Proxy-Authorization header, so use a proxy you reach over a network you trust. Another proxyAuthMethod, such as Digest, fails when the producer starts.
Examples
In the example below, we get the latitude and longitude for Paris, France
-
Java
-
XML
-
YAML
from("direct:start")
.to("geocoder:address:Paris, France?type=NOMINATIM&serverUrl=https://nominatim.openstreetmap.org"); <route>
<from uri="direct:start"/>
<to uri="geocoder:address:Paris, France?type=NOMINATIM&serverUrl=https://nominatim.openstreetmap.org"/>
</route> - route:
from:
uri: direct:start
steps:
- to:
uri: "geocoder:address:Paris, France"
parameters:
type: NOMINATIM
serverUrl: "https://nominatim.openstreetmap.org" If you provide a header with the CamelGeoCoderAddress then that overrides the endpoint configuration, so to get the location of Copenhagen, Denmark we can send a message with a headers as shown:
template.sendBodyAndHeader("direct:start", "Hello", "CamelGeoCoderAddress", "Copenhagen, Denmark"); To get the address for a latitude and longitude we can do:
-
Java
-
XML
-
YAML
from("direct:start")
.to("geocoder:latlng:40.714224,-73.961452")
.log("Location ${header.CamelGeoCoderAddress} is at lat/lng: ${header.CamelGeoCoderLatlng} and in country ${header.CamelGeoCoderCountryShort}"); <route>
<from uri="direct:start"/>
<to uri="geocoder:latlng:40.714224,-73.961452"/>
<log message="Location ${header.CamelGeoCoderAddress} is at lat/lng: ${header.CamelGeoCoderLatlng} and in country ${header.CamelGeoCoderCountryShort}"/>
</route> - route:
from:
uri: direct:start
steps:
- to:
uri: "geocoder:latlng:40.714224,-73.961452"
- log:
message: "Location ${header.CamelGeoCoderAddress} is at lat/lng: ${header.CamelGeoCoderLatlng} and in country ${header.CamelGeoCoderCountryShort}" Which will log
Location 285 Bedford Avenue, Brooklyn, NY 11211, USA is at lat/lng: 40.71412890,-73.96140740 and in country US
To get the current location using the Google GeoCoder, you can use "current" as the address as shown:
-
Java
-
XML
-
YAML
from("direct:start")
.to("geocoder:address:current"); <route>
<from uri="direct:start"/>
<to uri="geocoder:address:current"/>
</route> - route:
from:
uri: direct:start
steps:
- to:
uri: geocoder:address:current