carmen-ruby / carmen

A repository of geographic regions for Ruby
Other
1.17k stars 276 forks source link

Continent data #133

Open rafbm opened 10 years ago

rafbm commented 10 years ago

Would it make sense to include information about continents in Carmen?

Geonames has a nice database of countries with their corresponding continent code (parseable file). I ran a little script and it turns out all Carmen::Country.all.map(&:code) are included.

These APIs would be useful:

europe = Carmen::Continent.coded('EU')
# <#Carmen::Continent name="Europe">

europe.countries # Alias: `europe.subregions`
# [<#Carmen::Country name="Andorra">, <#Carmen::Country name="Albania">, <#Carmen::Country name="Austria">, ...]

netherlands = Carmen::Country.coded('NL')
# <#Carmen::Country name="Netherlands">

netherlands.continent # Alias: `netherlands.parent`
# <#Carmen::Continent name="Europe">

I could work on this.

jim commented 10 years ago

I spent a little time thinking about how to accomplish this a while back, but didn't start writing any code for it. The challenge is deciding if the current YAML file structure is extended an additional level, or if it is worth special-casing continents to avoid doing so.

I would be interested in what you were thinking, as this would be a nice addition to the library.

emptyflask commented 10 years ago

:+1: This would be useful.

Adding another YAML level is probably the best approach.

wizztjh commented 10 years ago

I am thinking of putting continent list and continent code in each of the country in world.yml Do you guys think it is a good idea

world.yml

--- 
- alpha_2_code: AU
  alpha_3_code: AUS
  numeric_code: "001"
  continent_code: OC
  type: country
- alpha_2_code: FO
  alpha_3_code: FRO
  numeric_code: "002"
  continent_code: EU
  type: country
- alpha_2_code: EE
  alpha_3_code: EST
  continent_code: EU
  numeric_code: "003"
  type: country
- continent_code: EU
  type: continent
- continent_code: OC
  type: continent
- continent_code: AS
  type: continent
wizztjh commented 10 years ago

@emptyflask what do you mean by adding another YAML level?

joekur commented 9 years ago

:+1: I'm in a similar boat of either extending carmen's functionality to include this or rolling my own solution. Looks like this is kind of an old issue; would love to hear if any of you have pursued this further.

joekur commented 9 years ago

FWIW I'll give my 2c on previous comments. The complication behind extending the yaml structure to another level is that you would need to read multiple files to get a full list of countries (we don't need have this complication for subregions because we only ever care about a single country's subregions). My gut feeling is that @wizztjh's solution is simpler. Would we just have another yml alongside world.yml with the continent data?

Just to feel out what an API would be like:

Carmen::Continent.all # => [<#Carmen::Continent name="North America">, ...]

na = Carmen::Continent.coded("NA")
na = Carmen::Continent.named("North America")
na.countries # => [<#Carmen::Country name="United States">, ...]

us = Carmen::Country.coded("US")
us.continent # => <#Carmen::Continent name="North America">

Missing anything?

rafbm commented 9 years ago

By the way sorry for my lack of comments. I opened the issue saying I could work on this but truth is I didn’t end up having enough time. Feel free to get this started, anyone!

I think the API you suggest makes sense @joekur. With added aliases for na.countries => na.subregions and us.continent => us.parent.

joekur commented 9 years ago

I've dug into it a little more. Adding another layer would actually be more elegant because it would follow the tree structure that's already in place. Ideally the structure would be World > Continents > Countries. But (and this is a big 'but') - this would break existing users' yaml overrides. For the same reason I'm also wary of changing Carmen::World's functionality. I'd like to be elegant, but at the same time I'd like this to be merged, and bumping major versions is no small decision. Would love to hear from @jim

jim commented 9 years ago

Adding another YAML layer for continents would probably be OK.

Breaking other user's YAML overrides is not a big deal as long as this was clearly stated in the docs and the version number is bumped accordingly. The implementations of Country.coded et al would also need to be updated to somehow search through all countries and not just those belonging to a single continent.

I haven't had much time to work on this library recently, so thank you to everyone for working together to address this feature request!

wizztjh commented 9 years ago

After I added a continent to the spec_data, the overlay spec failed because the data type is fort. Which been filtered out in Country.query_collection

I think all data in the Country.all should be country. I will change the spec_data of overlay data

- alpha_2_code: SE
  alpha_3_code: SEA
  numeric_code: "004"
  common_name: Sealand
  name: Sealand
  official_name: The Principality of Sealand
  type: fort

the commit dddc9d24c63fc77f10e530e6cd1e38cd3a0ec0a6

ghost commented 9 years ago

Any update on this @wizztjh? :)

wizztjh commented 9 years ago

Hi Dain, No progress from me. Occupied with something else. On Mar 13, 2015 3:10 AM, "Dain Miller" notifications@github.com wrote:

Any update on this @wizztjh https://github.com/wizztjh? :)

— Reply to this email directly or view it on GitHub https://github.com/jim/carmen/issues/133#issuecomment-78574442.

ghost commented 9 years ago

Thanks for the status @wizztjh. Will leave it open for the time being :)

j15e commented 9 years ago

I found https://github.com/beefsack/ruby-continent

It seems to match properly with every Carmen country

countries = Carmen::Country.all
countries.map {|c| Continent.by_alpha_2_code(c.code) || nil }.compact.size == countries.size
# true
kaka-ruto commented 4 years ago

It would be good if the above made it to the documentation. I didn't know you can Carmen::Country.all