public GeoDecoder(string countrysrc,string geosrc,string timezonesrc) { //Exception mapping //Note this maps a city to a different city not a timezone Dictionary<string, string> exceptionMap = new Dictionary<string, string> (); exceptionMap["CN/Hong Kong"]="CN/Beijing"; exceptionMap["China/Hong Kong"]="China/Beijing"; //init string[] lines; //decode timezones lines = timezonesrc.Split ('\n'); for(int i=0; i<lines.Length; i++){ string line = lines [i]; if (line.Length==0 || line [0] == '#') continue; string[] tabs = line.Split ('\t'); TimeZone timezone = new TimeZone ( tabs [0], tabs [1], tabs [2], tabs [3] ); timezoneDict [timezone.TimeZoneId] = timezone; } //decode countries lines = countrysrc.Split ('\n'); for(int i=0; i<lines.Length; i++){ string line = lines [i]; if (line.Length==0 || line [0] == '#') continue; string[] tabs = line.Split ('\t'); CountryInfo country = new CountryInfo ( tabs[0], tabs[1], tabs[2], tabs[3], tabs[4], tabs[5], tabs[6], tabs[7], tabs[8], tabs[9], tabs[10], tabs[11], tabs[12], tabs[13], tabs[14], tabs[15], tabs[16], tabs[17], tabs[18] ); countryDict [country.ISO] = country; } //decode geonames lines = geosrc.Split ('\n'); for(int i=0; i<lines.Length; i++){ string line = lines [i]; if (line.Length==0 || line [0] == '#') continue; string[] tabs = line.Split ('\t'); GeoName geoname = new GeoName ( tabs [0], tabs [1], tabs [2], tabs [3], tabs [4], tabs [5], tabs [6], tabs [7], tabs [8], tabs [9], tabs [10], tabs [11], tabs [12], tabs [13], tabs [14], tabs [15], tabs [16], timezoneDict[tabs [17]], tabs [18] ); CountryInfo country = countryDict [geoname.countrycode]; GeoName old=null; if(finalDict.TryGetValue (String.Format("{0}/{1}", country.ISO, geoname.asciiname), out old)){ if (old.population > geoname.population) continue; } finalDict [String.Format("{0}/{1}", country.ISO, geoname.asciiname)] = geoname; finalDict [String.Format("{0}/{1}", country.ISO3, geoname.asciiname)] = geoname; finalDict [String.Format("{0}/{1}", country.ISONumeric, geoname.asciiname)] = geoname; finalDict [String.Format("{0}/{1}", country.Country, geoname.asciiname)] = geoname; finalDict [String.Format("{0}/{1}", country.ISO, geoname.name)] = geoname; finalDict [String.Format("{0}/{1}", country.ISO3, geoname.name)] = geoname; finalDict [String.Format("{0}/{1}", country.ISONumeric, geoname.name)] = geoname; finalDict [String.Format("{0}/{1}", country.Country, geoname.name)] = geoname; if(geoname.alternatenames!=null && geoname.alternatenames.Length>0){ string[] alternate_names = geoname.alternatenames.Split (','); foreach(string alternate_name in alternate_names){ finalDict [String.Format("{0}/{1}", country.ISO, alternate_name)] = geoname; finalDict [String.Format("{0}/{1}", country.ISO3, alternate_name)] = geoname; finalDict [String.Format("{0}/{1}", country.ISONumeric, alternate_name)] = geoname; finalDict [String.Format("{0}/{1}", country.Country, alternate_name)] = geoname; } } } //map exceptions foreach (var exception in exceptionMap) { finalDict [exception.Key] = finalDict [exception.Value]; } }
public GeoDecoder(string countrysrc, string geosrc, string timezonesrc, Dictionary <string, string> exceptionMap = null) { //init string[] lines; //decode timezones lines = timezonesrc.Split('\n'); for (int i = 0; i < lines.Length; i++) { string line = lines [i]; if (line.Length == 0 || line [0] == '#') { continue; } string[] tabs = line.Split('\t'); TimeZone timezone = new TimeZone( tabs [0], tabs [1], tabs [2], tabs [3] ); timezoneDict [timezone.TimeZoneId] = timezone; } //decode countries lines = countrysrc.Split('\n'); for (int i = 0; i < lines.Length; i++) { string line = lines [i]; if (line.Length == 0 || line [0] == '#') { continue; } string[] tabs = line.Split('\t'); CountryInfo country = new CountryInfo( tabs[0], tabs[1], tabs[2], tabs[3], tabs[4], tabs[5], tabs[6], tabs[7], tabs[8], tabs[9], tabs[10], tabs[11], tabs[12], tabs[13], tabs[14], tabs[15], tabs[16], tabs[17], tabs[18] ); countryDict [country.ISO] = country; } //decode geonames lines = geosrc.Split('\n'); for (int i = 0; i < lines.Length; i++) { string line = lines [i]; if (line.Length == 0 || line [0] == '#') { continue; } string[] tabs = line.Split('\t'); GeoName geoname = new GeoName( tabs [0], tabs [1], tabs [2], tabs [3], tabs [4], tabs [5], tabs [6], tabs [7], tabs [8], tabs [9], tabs [10], tabs [11], tabs [12], tabs [13], tabs [14], tabs [15], tabs [16], timezoneDict[tabs [17]], tabs [18] ); CountryInfo country = countryDict [geoname.countrycode]; GeoName old = null; if (finalDict.TryGetValue(String.Format("{0}/{1}", country.ISO, geoname.asciiname), out old)) { if (old.population > geoname.population) { continue; } } finalDict [String.Format("{0}/{1}", country.ISO, geoname.asciiname)] = geoname; finalDict [String.Format("{0}/{1}", country.ISO3, geoname.asciiname)] = geoname; finalDict [String.Format("{0}/{1}", country.ISONumeric, geoname.asciiname)] = geoname; finalDict [String.Format("{0}/{1}", country.Country, geoname.asciiname)] = geoname; finalDict [String.Format("{0}/{1}", country.ISO, geoname.name)] = geoname; finalDict [String.Format("{0}/{1}", country.ISO3, geoname.name)] = geoname; finalDict [String.Format("{0}/{1}", country.ISONumeric, geoname.name)] = geoname; finalDict [String.Format("{0}/{1}", country.Country, geoname.name)] = geoname; if (geoname.alternatenames != null && geoname.alternatenames.Length > 0) { string[] alternate_names = geoname.alternatenames.Split(','); foreach (string alternate_name in alternate_names) { finalDict [String.Format("{0}/{1}", country.ISO, alternate_name)] = geoname; finalDict [String.Format("{0}/{1}", country.ISO3, alternate_name)] = geoname; finalDict [String.Format("{0}/{1}", country.ISONumeric, alternate_name)] = geoname; finalDict [String.Format("{0}/{1}", country.Country, alternate_name)] = geoname; } } } //map exceptions foreach (var exception in exceptionMap) { finalDict [exception.Key] = finalDict [exception.Value]; } }