Get a country's continent in C#
Asked Answered
H

7

7

Is there a way to find a country's continent in C#, using the RegionInfo class or any other way?

For example, you give the country name "Denmark" and it returns "Europe".

Hundred answered 27/3, 2011 at 15:52 Comment(0)
T
5

You can store a hash table. The entries will have as key the country name and as value the continent name. Just generate this table from some public database and once you have it you can simply query it. It's also pretty fast.

http://www.geonames.org/countries/
You can get from there a database

Teece answered 27/3, 2011 at 15:57 Comment(3)
I agree with the part of the hash table but from which public database do I populate it? Do you know of any web service I can use or something?Hundred
It's not that big not to do it manually, I guess. Using something like this, maybe: geonames.org/countriesTeece
2002 called and wants its untyped collections back.Robena
T
8

Nothing like that in the .NET framework. It is also rather ambiguous with geographical definitions for Eurasia and Oceania. There are 6 distinct definitions in use. You'll need your own dbase.

Tower answered 27/3, 2011 at 16:0 Comment(0)
T
5

You can store a hash table. The entries will have as key the country name and as value the continent name. Just generate this table from some public database and once you have it you can simply query it. It's also pretty fast.

http://www.geonames.org/countries/
You can get from there a database

Teece answered 27/3, 2011 at 15:57 Comment(3)
I agree with the part of the hash table but from which public database do I populate it? Do you know of any web service I can use or something?Hundred
It's not that big not to do it manually, I guess. Using something like this, maybe: geonames.org/countriesTeece
2002 called and wants its untyped collections back.Robena
M
3

There are only a few continents and only a couple of hundred countries, so it wouldn't be very hard just to build your own table. There are differences of opinion as to just what the continents are (Australia/Oceania/Australasia/Asia being a particularly prickly one at times), and whether e.g. Turkey and Russia are in Europe, Asia, or both. Still, rolling your own at least lets you make your own call on these matters.

Mceachern answered 27/3, 2011 at 16:1 Comment(4)
+1 And to further complicate matters, there are divergent opinions on what countries qualify as countries. Man, geopolitics is hard!Wirework
@Cody, we can often just go by the ISO list of codes for names, and leave just what those names refer to for others to argue about. Still I've a few times had to build systems that reconcile other systems' different schemes and there is no perfect solution, but then these are disagreements that have often resulted in wars and guerilla conflict, so no one programmer can expect to solve them all.Mceachern
I certainly wasn't suggesting that any programmer could solve all the problems! The suggestion to use the ISO list is a good one, though.Wirework
It can be worth pointing out the clients that they can't though :)Mceachern
F
1

You can use http://products.wolframalpha.com/api/

http://www.wolframalpha.com/input/?i=continent+THECOUNTRY
http://www.wolframalpha.com/input/?i=continent+sweden

Frizz answered 27/3, 2011 at 16:22 Comment(2)
What's this got to do with C# or .NET?Wirework
I don't know if WA would be happy about an application using their website as a web service for a problem that can be solved after a couple hours of work.Materiality
H
1

I know it's an old question but i have create a library for C# and this contains the contintent information. You can found my project here Nager.Country I hope it helps you.

A nuget package is also available install-package Nager.Country

Example

ICountryProvider countryProvider = new CountryProvider();
var countryInfo = countryProvider.GetCountry(Alpha2Code.DK);
Hyperplasia answered 31/1, 2019 at 23:22 Comment(0)
S
0

RegionInfo doesn't store continent. But you can build a database using GeoNames.org Country Information. The data format is tab-delimited text in utf8 encoding. It's has all the information and more that you need.

Sanborn answered 27/3, 2011 at 16:15 Comment(0)
K
0

You can build your own lookup lists according to the continent model you need. For example, in the US and the UK we consider America as two separate continents (North America and South America), whereas people in most other countries are taught that America is a single continent.

You should also consider that some countries lay in two continents. A part of Russia is in Europe, the rest is in Asia. Same with Turkey.

Krasnoff answered 28/12, 2020 at 15:13 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.