public static IEnumerable <GeonamesCityEntity> Parse(
            string path,
            Dictionary <int, Dictionary <string, List <GeonamesAlternateNameEntity> > > alternateNamesDict,
            Dictionary <string, GeonamesAdminEntity> admin1Dict,
            Dictionary <string, GeonamesAdminEntity> admin2Dict,
            Dictionary <string, GeonamesCountryEntity> countriesDict,
            Dictionary <int, HashSet <string> > geonamesIdsToCLLICodes,
            Dictionary <int, HashSet <string> > geonamesIdsToUNLOCODECodes)
        {
            using (var file = new StreamReader(path))
            {
                string line;

                while ((line = file.ReadLine()) != null)
                {
                    if (string.IsNullOrWhiteSpace(line))
                    {
                        continue;
                    }

                    if (line.TrimStart().StartsWith("#"))
                    {
                        continue;
                    }

                    var parse  = line.Split(new char[] { '\t' });
                    var entity = GeonamesCityEntity.Parse(parse, alternateNamesDict, admin1Dict, admin2Dict, countriesDict, geonamesIdsToCLLICodes, geonamesIdsToUNLOCODECodes);

                    yield return(entity);
                }
            }
        }
Exemplo n.º 2
0
        public static GeonamesCityEntity Parse(
            string[] parts,
            Dictionary <int, Dictionary <string, List <GeonamesAlternateNameEntity> > > alternateNamesDict,
            Dictionary <string, GeonamesAdminEntity> admin1Dict,
            Dictionary <string, GeonamesAdminEntity> admin2Dict,
            Dictionary <string, GeonamesCountryEntity> countriesDict,
            Dictionary <int, HashSet <string> > geonamesIdsToCLLICodes,
            Dictionary <int, HashSet <string> > geonamesIdsToUNLOCODECodes)
        {
            var entity = new GeonamesCityEntity();

            if (parts.Length != 19)
            {
                throw new ArgumentException("Expecting 19 columns as input");
            }

            int geonameId;

            if (int.TryParse(parts[0], out geonameId))
            {
                entity.Id = geonameId;
            }
            else
            {
                throw new ArgumentException("Could not parse Geonames ID");
            }

            HashSet <string> clliCodes;

            if (geonamesIdsToCLLICodes.TryGetValue(geonameId, out clliCodes))
            {
                entity.CLLICodes = clliCodes;
            }

            HashSet <string> unlocodeCodes;

            if (geonamesIdsToUNLOCODECodes != null && geonamesIdsToUNLOCODECodes.TryGetValue(geonameId, out unlocodeCodes))
            {
                entity.UNLOCODECodes = unlocodeCodes;
            }

            entity.Name      = parts[1];
            entity.AsciiName = parts[2];

            var alternateNamesBlacklist = new HashSet <string>()
            {
                "link", // Wikipedia link
                "post", // Post code
                "phon", // Phone?
                string.Empty
            };

            entity.AlternateNames = FilterAlternateNames(alternateNamesDict, geonameId, isoLanguageBlacklist: alternateNamesBlacklist);

            var oldAlternateNames = ParseMultiValue(rawData: parts[3], splitChar: ',');

            var airportCodesWhitelist = new HashSet <string>()
            {
                "icao",
                "iata",
                "faac",
                "tcid"
            };

            entity.AirportCodes = FilterAlternateNames(alternateNamesDict, geonameId, isoLanguageWhitelist: airportCodesWhitelist);

            double latitude;

            if (double.TryParse(parts[4], out latitude))
            {
                entity.Latitude = latitude;
            }
            else
            {
                throw new ArgumentException("Could not parse Latitude");
            }

            double longitude;

            if (double.TryParse(parts[5], out longitude))
            {
                entity.Longitude = longitude;
            }
            else
            {
                throw new ArgumentException("Could not parse Longitude");
            }

            entity.FeatureClass          = parts[6][0];
            entity.FeatureCode           = parts[7];
            entity.CountryCode           = parts[8];
            entity.AlternateCountryCodes = ParseMultiValue(rawData: parts[9], splitChar: ',');
            entity.Admin1Code            = parts[10];
            entity.Admin2Code            = parts[11];
            entity.Admin3Code            = parts[12];
            entity.Admin4Code            = parts[13];

            uint population;

            if (uint.TryParse(parts[14], out population))
            {
                entity.Population = population;
            }
            else
            {
                throw new ArgumentException("Could not parse Population");
            }

            int elevation;

            if (!string.IsNullOrEmpty(parts[15]) && int.TryParse(parts[15], out elevation))
            {
                entity.Elevation = elevation;
            }

            entity.DigitalElevationModel = parts[16];
            entity.Timezone     = parts[17];
            entity.LastModified = DateTime.ParseExact(parts[18], "yyyy-MM-dd", CultureInfo.InvariantCulture);

            if (admin1Dict != null && !string.IsNullOrWhiteSpace(entity.CountryCode) && !string.IsNullOrWhiteSpace(entity.Admin1Code))
            {
                var concatenatedCodes = string.Format(CultureInfo.InvariantCulture, "{0}.{1}", entity.CountryCode, entity.Admin1Code);

                GeonamesAdminEntity admin1Entity;

                if (admin1Dict.TryGetValue(concatenatedCodes, out admin1Entity))
                {
                    entity.Admin1Entity = admin1Entity;
                }
            }

            if (admin2Dict != null && !string.IsNullOrWhiteSpace(entity.CountryCode) && !string.IsNullOrWhiteSpace(entity.Admin1Code) && !string.IsNullOrWhiteSpace(entity.Admin2Code))
            {
                var concatenatedCodes = string.Format(CultureInfo.InvariantCulture, "{0}.{1}.{2}", entity.CountryCode, entity.Admin1Code, entity.Admin2Code);

                GeonamesAdminEntity admin2Entity;

                if (admin2Dict.TryGetValue(concatenatedCodes, out admin2Entity))
                {
                    entity.Admin2Entity = admin2Entity;
                }
            }

            if (countriesDict != null && !string.IsNullOrWhiteSpace(entity.CountryCode))
            {
                GeonamesCountryEntity countryEntity;

                if (countriesDict.TryGetValue(entity.CountryCode, out countryEntity))
                {
                    entity.CountryEntity = countryEntity;
                }
                else
                {
                    throw new ArgumentException(string.Format(CultureInfo.InvariantCulture, "Could not find a country entity with code: {0}", entity.CountryCode));
                }
            }

            return(entity);
        }