예제 #1
0
        public static void RefineAddressWithGeography(IAddressGeography addr)
        {
            UspsLookupResult usps = GeographyServices.LookupUspsAddress(addr);

            if (usps.Result == LookupResult.Success)
            {
                addr.Quality = (addr.Quality & 0xfff0) | 0x01;
                addr.CopyFrom(usps);

                Match  suffixMatch = Regex.Match(addr.Street, "^(.+) (APT|BSMT|#|BLDG|DEPT|FL|FRNT|HNGR|KEY|LBBY|LOT|LOWR|OFC|PH|PIER|REAR|RM|SIDE|SLIP|SPC|STOP|STE|TRLR|UNIT|UPPR)(.*)$");
                string suffix      = System.Threading.Thread.CurrentThread.CurrentCulture.TextInfo.ToTitleCase(suffixMatch.Groups[2].Value.ToLowerInvariant())
                                     + suffixMatch.Groups[3].Value;

                MapsLookupResult maps = GeographyServices.GeocodeAddress(usps);
                if (maps.Result == LookupResult.Success)
                {
                    addr.Quality = (addr.Quality & 0xff0f) | (maps.Quality << 4);
                    string mapsStreet = maps.Street + (suffixMatch.Success ? (" " + suffix) : "");
                    if (addr.Street.Equals(mapsStreet, StringComparison.OrdinalIgnoreCase))
                    {
                        addr.CopyFrom(maps);
                        addr.Street = mapsStreet;
                    }
                    addr.Location = maps.Geography;
                }
            }
        }
예제 #2
0
        public static UspsLookupResult LookupUspsAddress(IAddress addr)
        {
            UspsLookupResult result = new UspsLookupResult {
                Result = LookupResult.Error
            };

            System.Net.WebClient client = new System.Net.WebClient();

            int    retries = 3;
            string html    = "";

            while (retries-- > 0)
            {
                html = client.DownloadString(string.Format("https://tools.usps.com/go/ZipLookupResultsAction!input.action?resultMode=0&companyName=&address1={0}&address2=&city={1}&state={2}&urbanCode=&postalCode=&zip={3}",
                                                           HttpUtility.UrlEncode(addr.Street),
                                                           HttpUtility.UrlEncode(addr.City),
                                                           HttpUtility.UrlEncode(addr.State),
                                                           HttpUtility.UrlEncode(addr.Zip)));
                if (!html.ToLowerInvariant().Contains("error page"))
                {
                    break;
                }
                html = "";
            }

            /*
             * <div class="data">
             *
             *    <p class="std-address">
             *
             *
             *
             *        <span class="address1 range">STREET ADDRESS RESULT</span><br />
             *
             *
             *      <span class="city range">CITY RESULT</span> <span class="state range">WA</span> <span class="zip" style="">ZIP5 RESULT</span><span class="hyphen">&#45;</span><span class="zip4">ZIP+4 RESULT</span>
             */
            var match = Regex.Match(Regex.Replace(html, "[\\r\\n\\s]+", " "), "<div class=\"data\">(.*?)</div> </li>", RegexOptions.IgnoreCase | RegexOptions.Multiline);

            if (match.Success == false)
            {
                throw new InvalidOperationException("Unexpected result format: \n" + html);
            }

            XDocument matchBody = XDocument.Parse(match.Groups[1].Value.Replace("&trade;", ""));

            result.Street = matchBody.Descendants().First(f => f.Attribute("class") != null && f.Attribute("class").Value.Contains("address1")).Value;
            result.City   = matchBody.Descendants().First(f => f.Attribute("class") != null && f.Attribute("class").Value.Contains("city")).Value;
            result.State  = matchBody.Descendants().First(f => f.Attribute("class") != null && f.Attribute("class").Value.Contains("state")).Value;
            result.Zip    = matchBody.Descendants().First(f => f.Attribute("class") != null && f.Attribute("class").Value.Contains("zip")).Value;
            result.Result = LookupResult.Success;

            return(result);
        }
예제 #3
0
        public static UspsLookupResult LookupUspsAddress(IAddress addr)
        {
            UspsLookupResult result = new UspsLookupResult {
                Result = LookupResult.Error
            };

            System.Net.WebClient client = new System.Net.WebClient();

            int    retries = 3;
            string html    = "";

            while (retries-- > 0)
            {
                html = client.DownloadString(string.Format("http://zip4.usps.com/zip4/zcl_0_results.jsp?visited=1&pagenumber=0&firmname=&address2={0}&address1=&city={1}&state={2}&urbanization=&zip5={3}",
                                                           HttpUtility.UrlEncode(addr.Street),
                                                           HttpUtility.UrlEncode(addr.City),
                                                           HttpUtility.UrlEncode(addr.State),
                                                           HttpUtility.UrlEncode(addr.Zip)));
                if (!html.ToLowerInvariant().Contains("error page"))
                {
                    break;
                }
                html = "";
            }

            MatchCollection matches = Regex.Matches(html.Replace("\n", ""), "<tr>\\s*<td[^>]+headers=\"(.*?)\"(.*?)</tr>", RegexOptions.IgnoreCase);

            if (matches.Count == 1)
            {
                if (matches[0].Groups[1].Value.ToLowerInvariant() == "full")
                {
                    Match sub = Regex.Match(matches[0].Groups[2].Value,
                                            ">(.*?)</td>", RegexOptions.IgnoreCase);
                    if (sub.Success)
                    {
                        string[] fields = Regex.Split(sub.Groups[1].Value, @"<\s*br\s*\/?\s*>", RegexOptions.IgnoreCase);
                        result.Street = fields[0].Trim();
                        string[] others = fields[1].Split(new[] { "&nbsp;" }, StringSplitOptions.None);

                        result.City  = others[0].Trim();
                        result.State = others[1].Trim();
                        result.Zip   = others[3].Trim();

                        if (!string.IsNullOrEmpty(others[2]))
                        {
                            throw new InvalidOperationException("Found 3rd field: " + others[2].Trim());
                        }

                        result.Result = LookupResult.Success;
                    }
                    else
                    {
                        result.Result = LookupResult.Error;
                    }
                }
                else
                {
                    result.Result = LookupResult.Error;
                }
            }
            else if (matches.Count > 1)
            {
                result.Result = LookupResult.Range;
            }

            return(result);
        }
예제 #4
0
        public static UspsLookupResult LookupUspsAddress(IAddress addr)
        {
            UspsLookupResult result = new UspsLookupResult { Result = LookupResult.Error };
            System.Net.WebClient client = new System.Net.WebClient();

            int retries = 3;
            string html = "";
            while (retries-- > 0)
            {
                html = client.DownloadString(string.Format("http://zip4.usps.com/zip4/zcl_0_results.jsp?visited=1&pagenumber=0&firmname=&address2={0}&address1=&city={1}&state={2}&urbanization=&zip5={3}",
                    HttpUtility.UrlEncode(addr.Street),
                    HttpUtility.UrlEncode(addr.City),
                    HttpUtility.UrlEncode(addr.State),
                    HttpUtility.UrlEncode(addr.Zip)));
                if (!html.ToLowerInvariant().Contains("error page"))
                {
                    break;
                }
                html = "";
            }

            MatchCollection matches = Regex.Matches(html.Replace("\n", ""), "<tr>\\s*<td[^>]+headers=\"(.*?)\"(.*?)</tr>", RegexOptions.IgnoreCase);

            if (matches.Count == 1)
            {
                if (matches[0].Groups[1].Value.ToLowerInvariant() == "full")
                {
                    Match sub = Regex.Match(matches[0].Groups[2].Value,
                        ">(.*?)</td>", RegexOptions.IgnoreCase);
                    if (sub.Success)
                    {
                        string[] fields = Regex.Split(sub.Groups[1].Value, @"<\s*br\s*\/?\s*>", RegexOptions.IgnoreCase);
                        result.Street = fields[0].Trim();
                        string[] others = fields[1].Split(new[] { "&nbsp;" }, StringSplitOptions.None);

                        result.City = others[0].Trim();
                        result.State = others[1].Trim();
                        result.Zip = others[3].Trim();

                        if (!string.IsNullOrEmpty(others[2]))
                        {
                            throw new InvalidOperationException("Found 3rd field: " + others[2].Trim());
                        }

                        result.Result = LookupResult.Success;
                    }
                    else
                    {
                        result.Result = LookupResult.Error;
                    }
                }
                else
                {
                    result.Result = LookupResult.Error;
                }
            }
            else if (matches.Count > 1)
            {
                result.Result = LookupResult.Range;
            }

            return result;
        }
예제 #5
0
        public static UspsLookupResult LookupUspsAddress(IAddress addr)
        {
            UspsLookupResult result = new UspsLookupResult { Result = LookupResult.Error };
              System.Net.WebClient client = new System.Net.WebClient();

              int retries = 3;
              string html = "";
              while (retries-- > 0)
              {
            html = client.DownloadString(string.Format("https://tools.usps.com/go/ZipLookupResultsAction!input.action?resultMode=0&companyName=&address1={0}&address2=&city={1}&state={2}&urbanCode=&postalCode=&zip={3}",
            HttpUtility.UrlEncode(addr.Street),
            HttpUtility.UrlEncode(addr.City),
            HttpUtility.UrlEncode(addr.State),
            HttpUtility.UrlEncode(addr.Zip)));
            if (!html.ToLowerInvariant().Contains("error page"))
            {
              break;
            }
            html = "";
              }
              /*
               * <div class="data">

            <p class="std-address">

                <span class="address1 range">STREET ADDRESS RESULT</span><br />

              <span class="city range">CITY RESULT</span> <span class="state range">WA</span> <span class="zip" style="">ZIP5 RESULT</span><span class="hyphen">&#45;</span><span class="zip4">ZIP+4 RESULT</span>
            */
              var match = Regex.Match(Regex.Replace(html, "[\\r\\n\\s]+", " "), "<div class=\"data\">(.*?)</div> </li>", RegexOptions.IgnoreCase | RegexOptions.Multiline);
              if (match.Success == false) throw new InvalidOperationException("Unexpected result format: \n" + html);

              XDocument matchBody = XDocument.Parse(match.Groups[1].Value.Replace("&trade;", ""));

              result.Street = matchBody.Descendants().First(f => f.Attribute("class") != null && f.Attribute("class").Value.Contains("address1")).Value;
              result.City = matchBody.Descendants().First(f => f.Attribute("class") != null && f.Attribute("class").Value.Contains("city")).Value;
              result.State = matchBody.Descendants().First(f => f.Attribute("class") != null && f.Attribute("class").Value.Contains("state")).Value;
              result.Zip = matchBody.Descendants().First(f => f.Attribute("class") != null && f.Attribute("class").Value.Contains("zip")).Value;
              result.Result = LookupResult.Success;

              return result;
        }