5

How can I get a two-letter ISO 3166-1 country code in DBpedia? More specifically, I'm querying the SPARQL endpoint like this:

SELECT DISTINCT ?iata ?country where {
  ?airport a dbo:Airport.
  ?airport dbo:iataLocationIdentifier ?iata.
  ?airport dbp:location ?country.
  ?country a dbo:Country.
}

I'd like to get ISO 3166-1 code instead of country name. I couldn't find a predicate for that property, even though the codes are present in country infoboxes in Wikipedia. How can I achieve my goal?

Bolo
  • 258
  • 1
  • 8

2 Answers2

5

There is dbp:iso3166code, but it seems it's not set for a lot of countries.

svick
  • 869
  • 4
  • 9
4

DBpedia

The answer provided by @svick will probably be most useful one in the long run, but for the time being dbp:iso3166code is unusable.

However, I noticed that (apparently) all the country pages are the targets of redirects of this kind:

dbr:ISO_3166-1:DO dbo:wikiPageRedirects dbr:Dominican_Republic

Therefore, I can extract ISO 3166-1 code like this:

SELECT DISTINCT ?iata ?iso3166 where {
  ?airport a dbo:Airport.
  ?airport dbo:iataLocationIdentifier ?iata.
  FILTER regex(?iata, "^[A-Z]{3}$")
  ?airport dbp:location ?country.
  ?country a dbo:Country.
  ?redir dbo:wikiPageRedirects ?country.
  FILTER regex(?redir, "ISO_3166-1:")
  BIND(REPLACE(str(?redir), '^.*ISO_3166-1:', '') AS ?iso3166)
}

Wikidata

Update. It turns out that Wikidata has much cleaner data, and as a bonus the query is shorter (although not immediately human-readable):

SELECT ?iata ?iso3166
WHERE 
{
    ?apt wdt:P31 wd:Q1248784 ;
         wdt:P238 ?iata ;
         wdt:P17 ?country .
    ?country wdt:P297 ?iso3166 .
}

Click here to see the query in action.

Bolo
  • 258
  • 1
  • 8