我正在开发一个应用程序,需要根据IATA代码(http://en.wikipedia.org/wiki/IATA_airport_code)查找国家/地区代码(ISO-3166 alpha2).
是否有(最好是免费的)API?
我在这里提交的是一个不完整的解决方案,既可以解决手头的IATA-to-Contry-Code问题,又可以关注结构化内容可能未被充分利用的资源:我在谈论Freebase!
这是不完整的,因为并非所有IATA代码都包括在内(虽然我猜的是相对较高的覆盖范围)或者并非都有分配给它们的国家代码(唉更常见).
我想建议的API是Freebase MQL读取服务.
此免费服务通过发送带有参数的HTTPS请求来工作,该参数以MQL(Metaweb查询语言)表示查询,并接收具有所需结果的JSON对象.
特别是请求看起来像
https://www.googleapis.com/freebase/v1/mqlread?indent=2&query=[{"type":"/aviation/airport","id":null,"limit": 25,"name":null,"sort":"name","iata": "SFO", "/location/location/containedby": [{"limit":6,"name":null,"optional": true,"sort":"name","/location/country/iso3166_1_alpha2": [{ "limit":6, "optional": false, "sort":"value", "value":null}]}],"airport_type": [{"limit":3,"name":null,"optional": true,"sort":"name","type":"/aviation/airport_type"}]}]
^--- here place the IATA code
Run Code Online (Sandbox Code Playgroud)
为了便于阅读我在多行显示相应的MQL缩进; 同样的事情只有更好的布局.
[{
"type": "/aviation/airport",
"id": null,
"limit": 25,
"name": null,
"sort": "name",
"iata": "SFO", -- <<< that's where you place the desired IATA code
"/location/location/containedby": [{
"limit": 6,
"name": null,
"optional": true,
"sort": "name",
"/location/country/iso3166_1_alpha2": [{
"limit": 6,
"optional": false,
"sort": "value",
"value": null
}]
}],
"airport_type": [{
"limit": 3,
"name": null,
"optional": true,
"sort": "name",
"type": "/aviation/airport_type"
}]
}]?
Run Code Online (Sandbox Code Playgroud)
响应如下所示:
{
"result": [
{
"name": "San Francisco International Airport",
"iata": "SFO",
"/location/location/containedby": [
{
"name": "United States of America",
"/location/country/iso3166_1_alpha2": [
{
"value": "US"
}
]
}
],
"airport_type": [
{
"type": "/aviation/airport_type",
"name": "Public"
}
],
"type": "/aviation/airport",
"id": "/en/san_francisco_international_airport"
}
]
}
Run Code Online (Sandbox Code Playgroud)
通过钻入Freebase /aviation/airport
类型并使用我在下面简要描述的各种工具,该解决方案在大约30分钟内被"掀起".
请注意,这是一种通用方法, applicable to various queries: rather than matching IATA airport codes to ISO country codes, we could, for example, get the list of the bridges build before 1950 with a span exceeding 500 feet, or look up the famous musicians born in a given city etc. Furthermore, AFAIK, the Freebase API and information is freely available. Beware, however, that there are some limitations (and some advantages!) to the content found at Freebase as compared with that obtained from specialized sources.
The information obtained from Freebase may not be as authoritative, complete or current as that obtained with APIs and Data Extracts from specialized sources. This limitation speaks to the quasi universal breadth of the information gathered at Freebase in a collaborative, wiki-like, fashion, by a mostly volunteer task force, compared with the focused, often single-purposed, information gathering performed by paid professionals at various trade organizations such as, say the IATA or the International Maritime Organization (IMO). On the other hand, Freebase, with its semantic representation of the data, provides ways of connecting bits of information in powerful ways. Whereby the authoritative sources provide mostly "tabular" data, Freebase queries can match apparently unrelated pieces of information. For example, whereby the IMO probably produces lists of seaports with their annual tonnage, their number of terminals and such, Freebase can also find, say, the films that were shot in these ports or the famous writers who were born there.
But enough with disclosures, let's see how one can produce these queries
I'd like to finish with the following suggestion: Rather than integrating this online API to your application, it is sometimes possible to download the complete list and to create a local database with it. In this fashion it may be possible to complement the data by adding rows and/or filling empty columns. This approach is particularly applicable to the IATA/Airport example -after all the list of airports and their underlying codes is relatively small and does not vary all that frequently-. This approach of course may require the local DB to be refreshed and otherwise maintained occasionally, but it removes the requirement of the online, real-time, connection to Freebase.