IATA到国家API

Tho*_*sen 3 api

我正在开发一个应用程序,需要根据IATA代码(http://en.wikipedia.org/wiki/IATA_airport_code)查找国家/地区代码(ISO-3166 alpha2).

是否有(最好是免费的)API?

mjv*_*mjv 8

我在这里提交的是一个不完整的解决方案,既可以解决手头的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

  • First peruse the kind of desired items on Freebase, to get familiar with what the site has to offer in this area, and get an informal "feel" for the breadth and depth of the information available for this kind of items.
    For example, one may peek into various airport instances found for the Airport Type on Freebase
  • Once the type(s) of items desired is identified, one can look into their schema(s) (many ways to get there, for example link at bottom of page)
    Here's the schema for our Airport Type example.
  • The schema shows what fields of information are collected for a given type. Beware however that while the schema can be relatively complete and elaborate, the individual instances of the corresponding type may not all have these fields properly filled-in: verify informally with ad hoc reviews of instances or with lists of instances as explained below.
  • Return to the list of instances view and modify this view by adding and removing columns. This is done, respectively, with the orange "+" button on the top left of the list and with the small "x" button next to each column header.
    See how the tree shown in the "Add new column" section (shown after pressing the "+") allows drilling down the schema of the type that we start with or of types connected to it.
    In the case of Airports, I quickly removed the "image", "article" and "airline"-related fields, to make room, and added the IATA code, and drilled in the "Location" to find the Country and from there the ISO code. This connection to "Country" was a bit tricky, I had to look into the "Contained by" which itself is another "Location" and, using the "More" brought up the "Country" type.
  • Once you have a tabular listing, with the desired fields, and maybe with a few extra fields used for review purposes, you can export the corresponding MQL code by picking the "MQL" link in the "Use Data from this Collection", at the bottom of the page. This pops-up a window where you can copy the MQL snippet, although it is probably better to proceed to the Query Editor. (You may still need to copy the MQL code and paste it back into the editor, due to a bug with the application which doesn't bring that MQL when pressing the "View in Query Editor" button)
  • The query editor provides a convenient way of tweaking this initial/default query and testing it. You may need to get familiar with MQL language, it is generally rather intuitive.
  • When the query produces precisely what we need, it is time to to make it a "One-liner" (formatting convenience button at bottom of editor screen) which can then be used inside a URL to the Freebase MQL Read service (see example at the top of this answer).
  • A bit more testing and tweaking (e.g. fighting the JSON syntax errors occasionally introduced by typos while copying/editing the string to the URL) et... Voila !
  • There is actually a lot more that can be done with Freebase, for example using the Google Client Library to facilitate the integration with the MQL service.

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.