示例#1
0
 public void TestUsCityStateZipCtor()
 {
     var addrData = new AddressData {StateAbbrv = "NV", PostalCode = "89421"};
     var testResult = new UsCityStateZip(addrData);
     Assert.AreNotEqual("New York", testResult.City);
     System.Diagnostics.Debug.WriteLine(testResult.City);
 }
示例#2
0
 public void TestUsCityStateZipCtorPickWithMsa()
 {
     var addrData = new AddressData { StateAbbrv = "FL", PostalCode = "32701" };
     var testResult = new UsCityStateZip(addrData);
     Assert.AreNotEqual("New York", testResult.City);
     Assert.IsNotNull(testResult.Msa);
     System.Diagnostics.Debug.WriteLine(testResult.City);
 }
示例#3
0
 protected CityArea(AddressData d)
 {
     data = d;
 }
示例#4
0
        /// <summary>
        /// Fetches, at random, a <see cref="UsCityStateZip"/> by zip code prefix
        /// </summary>
        /// <param name="zipCodePrefix">
        /// Optional, will be picked at random with respect to population if whitespace or null.
        /// </param>
        /// <returns></returns>
        /// <remarks>
        /// Ranking is by population totals sourced from
        /// https://www.census.gov/geo/maps-data/data/docs/rel/zcta_cbsa_rel_10.txt
        /// </remarks>
        public static UsCityStateZip American(string zipCodePrefix = null)
        {
            const string HAS_HIGH_SCHOOL = "has-high-school";
            const string VALUE = "value";
            //set defaults
            var ctz = new AddressData
            {
                PostalCode = $"{UsCityStateZip.DF_ZIPCODE_PREFIX}{Etx.IntNumber(1, 99):00}",
                StateAbbrv = UsCityStateZip.DF_STATE_ABBREV
            };

            //pick a zip code prefix at random
            if (String.IsNullOrWhiteSpace(zipCodePrefix))
                zipCodePrefix = NAmerUtil.RandomAmericanZipWithRespectToPop() ?? UsCityStateZip.DF_ZIPCODE_PREFIX;

            //x-ref it to the zip code data
            var randZipCode =
                TreeData.AmericanZipCodeData.SelectSingleNode($"//{ZIP_CODE_PLURAL}//{ZIP_CODE_SINGULAR}[@{PREFIX}='{zipCodePrefix}']");
            if (randZipCode?.ParentNode?.Attributes?[NAME] == null)
            {
                ctz.City = UsCityStateZip.DF_CITY_NAME;
                return new UsCityStateZip(ctz);
            }

            //get the containing us state
            ctz.StateName =  randZipCode.ParentNode.Attributes[NAME].Value;
            var nfState = UsState.GetStateByName(ctz.StateName) ??
                          UsState.GetStateByPostalCode(UsCityStateZip.DF_STATE_ABBREV);

            ctz.StateAbbrv = nfState.StateAbbrv ?? UsCityStateZip.DF_STATE_ABBREV;
            ctz.PostalCodeSuffix = $"{Etx.MyRand.Next(1, 9999):0000}";

            if (!randZipCode.HasChildNodes)
            {
                ctz.PostalCode = $"{zipCodePrefix}{Etx.IntNumber(1, 99):00}";
            }
            else
            {
                //pick a particular zip-code (ZIP5)
                var zipCodes =
                    randZipCode.ChildNodes.Cast<XmlElement>()
                        .Where(
                            x =>
                                x.Attributes[HAS_HIGH_SCHOOL] != null &&
                                x.Attributes[HAS_HIGH_SCHOOL].Value == bool.TrueString)
                        .Select(x => x.Attributes[VALUE].Value).ToArray();
                if (zipCodes.Length <= 0)
                    return new UsCityStateZip(ctz);
                var pickNum = Etx.IntNumber(0, zipCodes.Length - 1);
                ctz.PostalCode = zipCodes[pickNum];
            }
            return new UsCityStateZip(ctz);
        }
示例#5
0
        internal static void GetState(string lastLine, AddressData addrData)
        {
            var regex = new Regex(STATE_CODE_REGEX, RegexOptions.IgnoreCase);
            if (!regex.IsMatch(lastLine))
                return;
            var matches = regex.Match(lastLine);

            var state = matches.Groups.Count >= 1 && matches.Groups[0].Success &&
                        matches.Groups[0].Captures.Count > 0
                ? matches.Groups[0].Captures[0].Value
                : String.Empty;

            addrData.StateAbbrv = state.Trim();
        }
示例#6
0
        internal static void GetZipCode(string lastLine, AddressData addrData)
        {
            //zip code
            var regex = new Regex(ZIP_CODE_REGEX, RegexOptions.IgnoreCase);
            if (!regex.IsMatch(lastLine))
                return;
            var matches = regex.Match(lastLine);

            var fiveDigitZip = matches.Groups.Count >= 2 && matches.Groups[1].Success &&
                               matches.Groups[1].Captures.Count > 0
                ? matches.Groups[1].Captures[0].Value
                : String.Empty;
            var zipPlusFour = matches.Groups.Count >= 3 && matches.Groups[2].Success &&
                              matches.Groups[2].Captures.Count > 0
                ? matches.Groups[2].Captures[0].Value
                : String.Empty;

            addrData.PostalCode = fiveDigitZip.Trim();
            addrData.PostalCodeSuffix = zipPlusFour.Replace("-", String.Empty).Trim();
        }
示例#7
0
        internal static void GetCity(string lastLine, AddressData addrData)
        {
            //city
            var city = lastLine;
            if (addrData.PostalCode.Length > 0)
                city = city.Replace(addrData.PostalCode, String.Empty);
            if (addrData.PostalCodeSuffix.Length > 0)
                city = city.Replace($"-{addrData.PostalCodeSuffix}", String.Empty);
            if (addrData.StateAbbrv.Length > 0 && city.Contains(" " + addrData.StateAbbrv))
                city = city.Replace(" " + addrData.StateAbbrv, String.Empty);
            if (addrData.StateAbbrv.Length > 0 && city.Contains("," + addrData.StateAbbrv))
                city = city.Replace("," + addrData.StateAbbrv, String.Empty);

            addrData.City = city.Replace(",", String.Empty).Trim();
        }
示例#8
0
        public static bool TryParse(string lastLine, out UsCityStateZip cityStateZip)
        {
            if (String.IsNullOrWhiteSpace(lastLine))
            {
                cityStateZip = null;
                return false;
            }
            lastLine = lastLine.Trim();

            var addrData = new AddressData
                           {
                               PostalCode = String.Empty,
                               PostalCodeSuffix = String.Empty,
                               StateAbbrv = String.Empty,
                               City = String.Empty
                           };
            GetZipCode(lastLine, addrData);

            GetState(lastLine, addrData);

            GetCity(lastLine, addrData);

            cityStateZip = new UsCityStateZip(addrData);
            return true;
        }
示例#9
0
 public CaCityProvidencePost(AddressData d)
     : base(d)
 {
 }
示例#10
0
 public UsCityStateZip(AddressData d)
     : base(d)
 {
     var xmlNode = GetCityXmlNode();
     if (xmlNode == null)
         return;
     ParseCityXmlNode(xmlNode);
 }
示例#11
0
        /// <summary>
        /// Fetches, at random, a <see cref="Canadian"/> in which the values are only somewhat related.
        /// The Providence and City are related while the only first three characters of the Postal Code are actually realted.
        /// The last three are always '4Z4'.
        /// </summary>
        /// <returns></returns>
        public static CaCityProvidencePost Canadian()
        {
            const string POSTAL_CODE = "postal-code";
            const string DF_FIRST_THREE_CHARS = "M5A";
            const string DF_CITY = "Toronto";
            const string DF_LAST_THREE_CHARS = "4Z4";
            const string MUNICIPALITY = "municipality";
            const string ABBREVIATION = "abbreviation";

            var ctz = new AddressData();
            var postalCodes = TreeData.CanadianPostalCodeData.SelectNodes($"//{POSTAL_CODE}");
            var dfReturn = new CaCityProvidencePost(ctz);
            if (postalCodes == null)
                return dfReturn;

            var pickOne = Etx.MyRand.Next(0, postalCodes.Count);

            var randPostalCode = postalCodes[pickOne] as XmlElement;
            if(randPostalCode == null)
                return dfReturn;

            var providenceElem = randPostalCode.ParentNode as XmlElement;
            if (providenceElem == null)
                return dfReturn;

            ctz.StateAbbrv = providenceElem.GetAttribute(ABBREVIATION);
            ctz.StateName = providenceElem.GetAttribute(NAME);

            var postalPrefix = randPostalCode.GetAttribute(PREFIX);
            postalPrefix = string.IsNullOrWhiteSpace(postalPrefix) ? DF_FIRST_THREE_CHARS : postalPrefix;
            ctz.PostalCode = $"{postalPrefix} {DF_LAST_THREE_CHARS}";

            var municipalityNode = randPostalCode.ChildNodes.OfType<XmlElement>()
                .FirstOrDefault(x => x.LocalName == MUNICIPALITY && !string.IsNullOrWhiteSpace(x.InnerText));

            ctz.City = municipalityNode?.InnerText ?? DF_CITY;

            return new CaCityProvidencePost(ctz);
        }