ere
ere

Reputation: 1779

freebase api getting a country code for a city

I'm trying to use freebase to return a cities related country ISO code.

I have this MQL which works but it doesn't work on certain cities and there's probably a more straight forward way to do the same thing?

{
  "id": "/en/amarillo",
  "/location/location/containedby": [
    "/location/country/iso3166_1_alpha2": null,
    "type": "/location/country"
  ]

}​

It doesn't work occasionally for smaller cities. Even better would be a way to use the Freebase suggest (which I use to gather the city) to return the ISO code in the same request?

jQuery ->
    $("#city_name")
        .suggest(type: "/location/citytown")
        .bind "fb-select", (e, data) ->
            $("#city_freebase_id").val data.id

Upvotes: 1

Views: 736

Answers (1)

Tom Morris
Tom Morris

Reputation: 10540

That's basically how I'd do it, but, as you've discovered, the data isn't incredibly fully populated. One tweak that you could make to improve your odds of finding something would be to go up an additional level in case, for example, the city is contained by a state which is contained by a country. You're also missing a set of curly braces in your subquery which will probably cause your query to fail in the API (the query editor silently fixes up stuff like this).

A revised query that goes up an additional level and fixes the braces would look like this:

{
  "id":   "/en/avondale",
  "/location/location/containedby": [{
    "/location/country/iso3166_1_alpha2": null,
    "type":          "/location/country"
  }],
  "l2:/location/location/containedby": [{
    "id":null,
    "/location/location/containedby": [{
      "/location/country/iso3166_1_alpha2": null,
      "type":          "/location/country"
    }]
  }]
}​

The search API has an mql_output parameter that could be used to do what you want, but I'm not sure it's used by Freebase Suggest. It's not documented to be available. If it's not in the code either, it wouldn't be too hard to hack the widget to add this.

Upvotes: 2

Related Questions