views:

280

answers:

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.

+1  A: 
Tim Sylvester
+1 for this solution. I thought about enumerating all RegionInfo objects (they have a three letter ISO region name property) to get all three letter codes, but there doesn't seem to be an option to enumerate all regions. So unless someone finds a solution for this, your solution is the way to go, I think
OregonGhost
Codebender - I coded a solution, went for coffee, posted my code here, and then saw that you took time out to create a mapping. Now I feel guilty about the coffee.
Matt Jacobsen
No worries, that's obviously the better way to go, though building a hashtable might still be a good idea.
Tim Sylvester
A: 

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 :)

Matt Jacobsen
+1  A: 

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:

Mihai Nita
If I hadn't already found a solution without having to use interop then I'd be implementing this now. No idea how EnumSystemGeoID slipped by me.
Matt Jacobsen
If it interests you, I'd appreciate a better implementation of the accepted answer. Like you and others have said, a cache of some sort would improve matters greatly.
Matt Jacobsen