The problem
You have addresses or geographic coordinates (longitude and latitude) and you want to know which country the location they respresent is in.Overview
EU NUTS (an abbreviation for Nomenclature of Territorial Units for Statistics) codes are standardized codes used by the European Union to define administrative and statistical regions for the purpose of data collection and analysis. They are used to identify, classify, and aggregate data on regions within the EU for statistics, planning, and policy making.
Converting addresses and/or geocoordinates (longitude, latitude) into NUTS codes as a way to aggregate data is a common task for statistical researchers
You can use the OpenCage geocoding API to geocode addresses or coordinates in EU countries and find the relevant NUTS codes for a location.
You can test our geocoding API manually on our demo page.
Background
NUTS
annotation for each result with the subfields:
-
NUTS0
the NUTS country code
Example:DE
-
NUTS1
Example:DE9
-
NUTS2
Example:DE92
-
NUTS3
Example:DE929
Caveats / Points to Note
There are a few points you should note when working with the NUTS codes returned by the OpenCage geocoding API.
- Countries are structured differently, as is perhaps to be expected given that some countries are very large, and others much smaller. Some countries have many level 3 NUTS codes, others only one.
- Currently we return NUTS codes only for EU member states, we may in the future add candidate countries, EFTA countries, and former EU member states (the UK). If this is preventing you from becoming a customer please get in touch with us, and let's discuss.
- We do not currently offer LAU codes (the level below NUTS3)
Code Example
52.3877830, 9.7334394
- site of the OpenCage office in Hannover, Germany.
We also have libraries for over 30 other programming languages. including languages commonly used in research like python, R, stata, matlab, and many others.
<script>
var api_key = 'YOUR-API-KEY';
var coords = '52.3877830, 9.7334394'; // lat,lng
var api_url = 'https://api.opencagedata.com/geocode/v1/json'
var request_url = api_url
+ '?'
+ 'key=' + api_key
+ '&q=' + encodeURIComponent(coords)
+ '&pretty=1';
// see full list of required and optional parameters:
// https://opencagedata.com/api#forward
var request = new XMLHttpRequest();
request.open('GET', request_url, true);
request.onload = function() {
// see full list of possible response codes:
// https://opencagedata.com/api#codes
var data = JSON.parse(request.responseText);
if (request.status === 200){ // Success!
var nuts = 'no NUTS codes returned';
if (data.results[0].annotations.NUTS != null){
nuts = data.results[0].annotations.NUTS;
}
console.log('NUTS: ' + nuts);
} else {
console.log("unable to geocode! Response code: " + request.status);
console.log('error msg: ' + data.status.message);
}
};
request.onerror = function() {
// There was a connection error of some sort
console.log("unable to connect to server");
};
request.send(); // make the request
</script>
Further Reading
-
OpenCage API reference section discussing the
NUTS
annotation - OpenCage geocoding API Javascript tutorial