Matt Jacobsen
Matt Jacobsen

Reputation: 5884

Get a MS GeoId from a 3 lettor ISO code (ISO 3166-1 alpha-3)

I have a 3 letter ISO code (e.g. DEU for germany, USA for America...) and would like to get the GeoID as listed in this table, also available here How can I do that using either straight .NET, P/Invoking or through the MapPoint PIA.

e.g. given "SWE", I would like to retrieve 221.

System.Globalisation.RegionInfo looks kinda promising - but it expects 2 letter codes (US instead of USA). To that end, a 3 letter -> 2 letter conversion would also work.

Upvotes: 0

Views: 1403

Answers (3)

Tim Sylvester
Tim Sylvester

Reputation: 23158

I think you'll have to manually set up the mapping to begin with, unless someone has already done it. With a little tinkering you could use a db or similar to do a join on the names and probably get 90% or more of them, then fill in the rest by hand.

I would keep the mapping in an external file, database table, etc., so it's not hard-coded (not sure how likely the GeoID values are to be added or updated...) and load them into a hashtable/dictionary if you need to be able to look them up very quickly.

Edit:

Here you go, I was bored. There were about 30 that I couldn't match, the rest lined up fine with a little tweaking. Full outer join FTW.

(isocode,geoid,isoname,msname)
'AFG','0x3','Afghanistan','Afghanistan'
'AGO','0x9','Angola','Angola'
'AIA','0x12C','Anguilla','Anguilla'
'ALA',null,'Åland Islands',null
'ALB','0x6','Albania','Albania'
'AND','0x8','Andorra','Andorra'
'ANT','0x14D','Netherlands Antilles','Netherlands Antilles'
'ARE','0xE0','United Arab Emirates','United Arab Emirates'
'ARG','0xB','Argentina','Argentina'
'ARM','0x7','Armenia','Armenia'
'ASM','0xA','American Samoa','American Samoa'
'ATA','0x12D','Antarctica','Antarctica'
'ATF','0x13F','French Southern and Antarctic Lands','French Southern and Antarctic Lands'
'ATG','0x2','Antigua and Barbuda','Antigua and Barbuda'
'AUS','0xC','Australia','Australia'
'AUT','0xE','Austria','Austria'
'AZE','0x5','Azerbaijan','Azerbaijan'
'BDI','0x26','Burundi','Burundi'
'BEL','0x15','Belgium','Belgium'
'BEN','0x1C','Benin','Benin'
'BFA','0xF5','Burkina Faso','Burkina Faso'
'BGD','0x17','Bangladesh','Bangladesh'
'BGR','0x23','Bulgaria','Bulgaria'
'BHR','0x11','Bahrain','Bahrain'
'BHS','0x16','Bahamas, The','Bahamas, The'
'BIH','0x19','Bosnia and Herzegovina','Bosnia and Herzegovina'
'BLM',null,'Saint Barthélemy',null
'BLR','0x1D','Belarus','Belarus'
'BLZ','0x18','Belize','Belize'
'BMU','0x14','Bermuda','Bermuda'
'BOL','0x1A','Bolivia','Bolivia'
'BRA','0x20','Brazil','Brazil'
'BRB','0x12','Barbados','Barbados'
'BRN','0x25','Brunei','Brunei'
'BTN','0x22','Bhutan','Bhutan'
'BVT','0x132','Bouvet Island','Bouvet Island'
'BWA','0x13','Botswana','Botswana'
'CAF','0x37','Central African Republic','Central African Republic'
'CAN','0x27','Canada','Canada'
'CCK','0x137','Cocos (Keeling) Islands','Cocos (Keeling) Islands'
'CHE','0xDF','Switzerland','Switzerland'
'CHL','0x2E','Chile','Chile'
'CHN','0x2D','China','China'
'CIV','0x77','Côte d''Ivoire','Côte d''Ivoire'
'CMR','0x31','Cameroon','Cameroon'
'COD','0x2C','Congo, DRC','Congo, DRC'
'COG','0x2B','Congo','Congo'
'COK','0x138','Cook Islands','Cook Islands'
'COL','0x33','Colombia','Colombia'
'COM','0x32','Comoros','Comoros'
'CPV','0x39','Cape Verde','Cape Verde'
'CRI','0x36','Costa Rica','Costa Rica'
'CUB','0x38','Cuba','Cuba'
'CXR','0x135','Christmas Island','Christmas Island'
'CYM','0x133','Cayman Islands','Cayman Islands'
'CYP','0x3B','Cyprus','Cyprus'
'CZE','0x4B','Czech Republic','Czech Republic'
'DEU','0x5E','Germany','Germany'
'DJI','0x3E','Djibouti','Djibouti'
'DMA','0x3F','Dominica','Dominica'
'DNK','0x3D','Denmark','Denmark'
'DOM','0x41','Dominican Republic','Dominican Republic'
'DZA','0x4','Algeria','Algeria'
'ECU','0x42','Ecuador','Ecuador'
'EGY','0x43','Egypt','Egypt'
'ERI','0x47','Eritrea','Eritrea'
'ESH',null,'Western Sahara',null
'ESP','0xD9','Spain','Spain'
'EST','0x46','Estonia','Estonia'
'ETH','0x49','Ethiopia','Ethiopia'
'FIN','0x4D','Finland','Finland'
'FJI','0x4E','Fiji','Fiji'
'FLK','0x13B','Falkland Islands (Islas Malvinas)','Falkland Islands (Islas Malvinas)'
'FRA','0x54','France','France'
'FRO','0x51','Faroe Islands','Faroe Islands'
'FSM','0x50','Micronesia','Micronesia'
'GAB','0x57','Gabon','Gabon'
'GBR','0xF2','United Kingdom','United Kingdom'
'GEO','0x58','Georgia','Georgia'
'GGY','0x144','Guernsey','Guernsey'
'GHA','0x59','Ghana','Ghana'
'GIN','0x64','Guinea','Guinea'
'GLP','0x141','Guadeloupe','Guadeloupe'
'GMB','0x56','Gambia, The','Gambia, The'
'GNB','0xC4','Guinea-Bissau','Guinea-Bissau'
'GNQ','0x45','Equatorial Guinea','Equatorial Guinea'
'GRC','0x62','Greece','Greece'
'GRD','0x5B','Grenada','Grenada'
'GRL','0x5D','Greenland','Greenland'
'GTM','0x63','Guatemala','Guatemala'
'GUF','0x13D','French Guiana','French Guiana'
'GUM','0x142','Guam','Guam'
'GUY','0x65','Guyana','Guyana'
'HKG','0x68','Hong Kong','Hong Kong'
'HMD','0x145','Heard Island and McDonald Islands','Heard Island and McDonald Islands'
'HND','0x6A','Honduras','Honduras'
'HRV','0x6C','Croatia','Croatia'
'HTI','0x67','Haiti','Haiti'
'HUN','0x6D','Hungary','Hungary'
'IDN','0x6F','Indonesia','Indonesia'
'IMN','0x3B16','Isle of Man','Isle of Man'
'IND','0x71','India','India'
'IOT','0x72','British Indian Ocean Territory','British Indian Ocean Territory'
'IRL','0x44','Ireland','Ireland'
'IRN','0x74','Iran','Iran'
'IRQ','0x79','Iraq','Iraq'
'ISL','0x6E','Iceland','Iceland'
'ISR','0x75','Israel','Israel'
'ITA','0x76','Italy','Italy'
'JAM','0x7C','Jamaica','Jamaica'
'JEY','0x148','Jersey','Jersey'
'JOR','0x7E','Jordan','Jordan'
'JPN','0x7A','Japan','Japan'
'KAZ','0x89','Kazakhstan','Kazakhstan'
'KEN','0x81','Kenya','Kenya'
'KGZ','0x82','Kyrgyzstan','Kyrgyzstan'
'KHM','0x28','Cambodia','Cambodia'
'KIR','0x85','Kiribati','Kiribati'
'KNA','0xCF','Saint Kitts and Nevis','Saint Kitts and Nevis'
'KOR','0x86','Korea','Korea'
'KWT','0x88','Kuwait','Kuwait'
'LAO','0x8A','Laos','Laos'
'LBN','0x8B','Lebanon','Lebanon'
'LBR','0x8E','Liberia','Liberia'
'LBY','0x94','Libya','Libya'
'LCA','0xDA','Saint Lucia','Saint Lucia'
'LIE','0x91','Liechtenstein','Liechtenstein'
'LKA','0x2A','Sri Lanka','Sri Lanka'
'LSO','0x92','Lesotho','Lesotho'
'LTU','0x8D','Lithuania','Lithuania'
'LUX','0x93','Luxembourg','Luxembourg'
'LVA','0x8C','Latvia','Latvia'
'MAC','0x97','Macau SAR','Macau SAR'
'MAF',null,'Saint Martin (France)',null
'MAR','0x9F','Morocco','Morocco'
'MCO','0x9E','Monaco','Monaco'
'MDA','0x98','Moldova','Moldova'
'MDG','0x95','Madagascar','Madagascar'
'MDV','0xA5','Maldives','Maldives'
'MEX','0xA6','Mexico','Mexico'
'MHL','0xC7','Marshall Islands','Marshall Islands'
'MKD','0x4CA2','Macedonia, FYROM','Macedonia, FYROM'
'MLI','0x9D','Mali','Mali'
'MLT','0xA3','Malta','Malta'
'MMR','0x1B','Myanmar','Myanmar'
'MNE',null,'Montenegro',null
'MNG','0x9A','Mongolia','Mongolia'
'MNP','0x151','Northern Mariana Islands','Northern Mariana Islands'
'MOZ','0xA8','Mozambique','Mozambique'
'MRT','0xA2','Mauritania','Mauritania'
'MSR','0x14C','Montserrat','Montserrat'
'MTQ','0x14A','Martinique','Martinique'
'MUS','0xA0','Mauritius','Mauritius'
'MWI','0x9C','Malawi','Malawi'
'MYS','0xA7','Malaysia','Malaysia'
'MYT','0x14B','Mayotte','Mayotte'
'NAM','0xFE','Namibia','Namibia'
'NCL','0x14E','New Caledonia','New Caledonia'
'NER','0xAD','Niger','Niger'
'NFK','0x150','Norfolk Island','Norfolk Island'
'NGA','0xAF','Nigeria','Nigeria'
'NIC','0xB6','Nicaragua','Nicaragua'
'NLD','0xB0','Netherlands','Netherlands'
'NOR','0xB1','Norway','Norway'
'NPL','0xB2','Nepal','Nepal'
'NRU','0xB4','Nauru','Nauru'
'NZL','0xB7','New Zealand','New Zealand'
'OMN','0xA4','Oman','Oman'
'PAK','0xBE','Pakistan','Pakistan'
'PAN','0xC0','Panama','Panama'
'PCN','0x153','Pitcairn Islands','Pitcairn Islands'
'PER','0xBB','Peru','Peru'
'PHL','0xC9','Philippines','Philippines'
'PLW','0xC3','Palau','Palau'
'PNG','0xC2','Papua New Guinea','Papua New Guinea'
'POL','0xBF','Poland','Poland'
'PRI','0xCA','Puerto Rico','Puerto Rico'
'PRK','0x83','North Korea','North Korea'
'PRT','0xC1','Portugal','Portugal'
'PRY','0xB9','Paraguay','Paraguay'
'PSE','0xB8','Palestinian Authority','Palestinian Authority'
'PYF','0x13E','French Polynesia','French Polynesia'
'QAT','0xC5','Qatar','Qatar'
'REU','0xC6','Réunion','Réunion'
'ROU','0xC8','Romania','Romania'
'RUS','0xCB','Russia','Russia'
'RWA','0xCC','Rwanda','Rwanda'
'SAU','0xCD','Saudi Arabia','Saudi Arabia'
'SDN','0xDB','Sudan','Sudan'
'SEN','0xD2','Senegal','Senegal'
'SGP','0xD7','Singapore','Singapore'
'SGS','0x156','South Georgia and the South Sandwich Islands','South Georgia and the South Sandwich Islands'
'SHN','0x157','Saint Helena','Saint Helena'
'SJM','0xDC','Svalbard and Jan Mayen','Svalbard and Jan Mayen'
'SLB','0x1E','Solomon Islands','Solomon Islands'
'SLE','0xD5','Sierra Leone','Sierra Leone'
'SLV','0x48','El Salvador','El Salvador'
'SMR','0xD6','San Marino','San Marino'
'SOM','0xD8','Somalia','Somalia'
'SPM','0xCE','Saint Pierre and Miquelon','Saint Pierre and Miquelon'
'SRB','0x10D','Serbia','Serbia'
'STP','0xE9','São Tomé and Príncipe','São Tomé and Príncipe'
'SUR','0xB5','Suriname','Suriname'
'SVK','0x8F','Slovakia','Slovakia'
'SVN','0xD4','Slovenia','Slovenia'
'SWE','0xDD','Sweden','Sweden'
'SWZ','0x104','Swaziland','Swaziland'
'SYC','0xD0','Seychelles','Seychelles'
'SYR','0xDE','Syria','Syria'
'TCA','0x15D','Turks and Caicos Islands','Turks and Caicos Islands'
'TCD','0x29','Chad','Chad'
'TGO','0xE8','Togo','Togo'
'THA','0xE3','Thailand','Thailand'
'TJK','0xE4','Tajikistan','Tajikistan'
'TKL','0x15B','Tokelau','Tokelau'
'TKM','0xEE','Turkmenistan','Turkmenistan'
'TLS','0x6F60E7','Timor-Leste','Timor-Leste'
'TON','0xE7','Tonga','Tonga'
'TTO','0xE1','Trinidad and Tobago','Trinidad and Tobago'
'TUN','0xEA','Tunisia','Tunisia'
'TUR','0xEB','Turkey','Turkey'
'TUV','0xEC','Tuvalu','Tuvalu'
'TWN','0xED','Taiwan','Taiwan'
'TZA','0xEF','Tanzania','Tanzania'
'UGA','0xF0','Uganda','Uganda'
'UKR','0xF1','Ukraine','Ukraine'
'UMI',null,'United States Minor Outlying Islands',null
'URY','0xF6','Uruguay','Uruguay'
'USA','0xF4','United States','United States'
'UZB','0xF7','Uzbekistan','Uzbekistan'
'VAT','0xFD','Vatican City','Vatican City'
'VCT','0xF8','Saint Vincent and the Grenadines','Saint Vincent and the Grenadines'
'VEN','0xF9','Venezuela','Venezuela'
'VGB','0x15F','Virgin Islands, British','Virgin Islands, British'
'VIR','0xFC','Virgin Islands','Virgin Islands'
'VNM','0xFB','Vietnam','Vietnam'
'VUT','0xAE','Vanuatu','Vanuatu'
'WLF','0x160','Wallis and Futuna','Wallis and Futuna'
'WSM','0x103','Samoa','Samoa'
'YEM','0x105','Yemen','Yemen'
'ZAF','0xD1','South Africa','South Africa'
'ZMB','0x107','Zambia','Zambia'
'ZWE','0x108','Zimbabwe','Zimbabwe'
null,'0x5A',null,'Gibraltar'
null,'0x7D',null,'Jan Mayen'
null,'0x7F',null,'Johnston Atoll'
null,'0x102',null,'Wake Island'
null,'0x12E',null,'Aruba'
null,'0x12F',null,'Ascension Island'
null,'0x130',null,'Ashmore and Cartier Islands'
null,'0x131',null,'Baker Island'
null,'0x134',null,'Channel Islands'
null,'0x136',null,'Clipperton Island'
null,'0x139',null,'Coral Sea Islands'
null,'0x13A',null,'Diego Garcia'
null,'0x143',null,'Guantanamo Bay'
null,'0x146',null,'Howland Island'
null,'0x147',null,'Jarvis Island'
null,'0x149',null,'Kingman Reef'
null,'0x14F',null,'Niue'
null,'0x152',null,'Palmyra Atoll'
null,'0x154',null,'Rota Island'
null,'0x155',null,'Saipan'
null,'0x15A',null,'Tinian Island'
null,'0x15C',null,'Tristan da Cunha'
null,'0x52FA',null,'Midway Islands'

Upvotes: 1

Mihai Nita
Mihai Nita

Reputation: 5787

Enumerate all GSIDs with EnumSystemGeoID, then use the GetGeoInfo with GEO_ISO3 as GeoType.

Of course, it is best if you do that once, cache the results, then you can map both ways, as you want.

Links:

Upvotes: 1

Matt Jacobsen
Matt Jacobsen

Reputation: 5884

After dicking around with Enum.* and the reflection API, I finally solved this by enumerating all possible RegionInfo's. This was possible by getting all the CultureInfos and creating RegionInfos from them.

The following code is icky and needs to be polished.

    internal MapPoint.GeoCountry GetGeocode(string isoCountryCode)
    {
        CultureInfo[] cultures = CultureInfo.GetCultures(CultureTypes.AllCultures);
        RegionInfo region = null;
        foreach (CultureInfo culture in cultures)
        {
            try
            {
                 region = new RegionInfo(culture.Name);
                 if (String.Compare(region.ThreeLetterISORegionName, isoCountryCode, true) == 0)
                     return (GeoCountry)region.GeoId;
            }
            catch
            {
            }
        }

        return GeoCountry.geoCountryDefault;
    }

Feel free to go nuts making it generic, pretty, optimised etc :)

Upvotes: 0

Related Questions