/// <summary> /// Gets the addresses that share a postcode /// </summary> /// <param name="postcode">The postcode.</param> /// <exception cref="WebException"></exception> /// <returns></returns> public IList <BS7666Address> AddressesFromPostcode(string postcode) { var query = Regex.Replace(postcode, "[^A-Za-z0-9]", String.Empty); if (String.IsNullOrEmpty(query)) { return(new List <BS7666Address>()); } try { using (var client = new WebClient()) { if (_proxyProvider != null) { client.Proxy = _proxyProvider.CreateProxy(); } client.Headers.Add("Authorization", "Bearer " + _authenticationToken); var queryUrl = String.Format(_locateApiUrl.ToString(), query); using (var stream = new StreamReader(client.OpenRead(queryUrl))) { var json = stream.ReadToEnd(); var results = JsonConvert.DeserializeObject <LocateApiAddressResult[]>(json); var addresses = new List <BS7666Address>(); foreach (var result in results) { var address = new BS7666Address(result.presentation.property, String.Empty, result.presentation.street, String.Empty, result.presentation.town, result.presentation.area, result.presentation.postcode) { Uprn = result.uprn, Usrn = result.details?.usrn, GeoCoordinate = new GeoCoordinate() { Latitude = result.location.latitude, Longitude = result.location.longitude } }; addresses.Add(address); } return(addresses); } } } catch (WebException exception) { if (exception.Message.Contains("(422) Unprocessable Entity")) { return(new List <BS7666Address>()); } throw; } }
/// <summary> /// Gets a URL for displaying a map on the website of the given address /// </summary> /// <param name="address">The address to show a map of</param> /// <param name="displayName">The display name of the page to link from</param> /// <param name="returnUrl">The URL to return to, if not the current page</param> /// <returns>URI of a page which will display the map, or null if the map is not available</returns> /// <remarks>returnUrl property is useful for CMS, where the URL of the current page is translated on-the-fly</remarks> private static string GetWebsiteMapUrl(BS7666Address address, string displayName, Uri returnUrl) { if (address == null || address.Postcode == null || address.Postcode.Length == 0) { return(null); } if (String.IsNullOrEmpty(address.AdministrativeArea) || String.Compare(address.AdministrativeArea.ToUpperInvariant(), "EAST SUSSEX", true, CultureInfo.CurrentCulture) != 0) { return(null); } if (address.Town != null && address.Town.Length > 0 && address.Town.ToUpperInvariant().IndexOf("BRIGHTON", StringComparison.Ordinal) > -1) { return(null); } // if no dots, assume it's an internal dev box and re-use the current host // otherwise use the live site HttpContext ctx = HttpContext.Current; string host = (ctx != null && ctx.Request.Url.Host.IndexOf(".", StringComparison.Ordinal) == -1) ? ctx.Request.Url.Host : "www.eastsussex.gov.uk"; // Build up URL StringBuilder url = new StringBuilder(Uri.UriSchemeHttp).Append("://").Append(host).Append("/contactus/map.aspx"); url.Append("?postcode=").Append(Uri.EscapeDataString(address.Postcode)); if (address.GeoCoordinate != null) { url.Append("&e=").Append(Uri.EscapeDataString(address.GeoCoordinate.Easting.ToString(CultureInfo.CurrentCulture))); url.Append("&n=").Append(Uri.EscapeDataString(address.GeoCoordinate.Northing.ToString(CultureInfo.CurrentCulture))); } if (address.Paon != null && address.Paon.Length > 0) { url.Append("&paon=").Append(Uri.EscapeDataString(address.Paon)); } url.Append("&mapOf=").Append(Uri.EscapeDataString(HttpUtility.HtmlDecode(address.GetSimpleAddress().ToString()))); if (returnUrl != null) { url.Append("&backToUrl=").Append(Uri.EscapeDataString(returnUrl.ToString())); } if (displayName != null && displayName.Length > 0) { url.Append("&backTo=").Append(Uri.EscapeDataString(displayName)); } return(url.ToString()); }
public override IEnumerable <object> ProcessSubmittedValue(Field field, IEnumerable <object> postedValues, HttpContextBase context) { // We don't want the posted value, which is only used to pass validation. // We want the values from all the fields that make up the selected address. if (context.Request.HttpMethod.ToUpperInvariant() == "POST") { var address = new BS7666Address() { Uprn = context.Request.Form[field.Id + ".Uprn"], Usrn = context.Request.Form[field.Id + ".Usrn"], Paon = context.Request.Form[field.Id + ".Paon"], StreetName = context.Request.Form[field.Id + ".StreetName"], Locality = context.Request.Form[field.Id + ".Locality"], Town = context.Request.Form[field.Id + ".Town"], AdministrativeArea = context.Request.Form[field.Id + ".AdministrativeArea"], Postcode = context.Request.Form[field.Id + ".Postcode"] }; double parsed; if (!String.IsNullOrEmpty(context.Request.Form[field.Id + ".GeoCoordinate.Latitude"])) { if (double.TryParse(context.Request.Form[field.Id + ".GeoCoordinate.Latitude"], out parsed)) { address.GeoCoordinate.Latitude = parsed; } } if (!String.IsNullOrEmpty(context.Request.Form[field.Id + ".GeoCoordinate.Longitude"])) { if (double.TryParse(context.Request.Form[field.Id + ".GeoCoordinate.Longitude"], out parsed)) { address.GeoCoordinate.Longitude = parsed; } } // Store two values for the field: a human-readable version and a JSON object including metadata return(new object[] { address.GetSimpleAddress().ToString(), JsonConvert.SerializeObject(address) }); } // There is no value when the control is initially loaded on a GET request return(new object[0]); }
/// <summary> /// Initializes a new instance of the <see cref="SimpleAddressControl"/> class. /// </summary> /// <param name="tag">The tag.</param> /// <param name="address">The address.</param> public SimpleAddressControl(HtmlTextWriterTag tag, BS7666Address address) : base(tag) { this.Address = address; }
/// <summary> /// Initializes a new instance of the <see cref="SimpleAddressControl"/> class. /// </summary> /// <param name="tag">An HTML tag.</param> /// <param name="address">The address.</param> public SimpleAddressControl(string tag, BS7666Address address) : base(tag) { this.Address = address; }
/// <summary> /// Initializes a new instance of the <see cref="SimpleAddressControl"/> class. /// </summary> /// <param name="address">The address.</param> public SimpleAddressControl(BS7666Address address) : base(HtmlTextWriterTag.Span) { this.Address = address; }
/// <summary> /// Called by the ASP.NET page framework to notify server controls that use composition-based implementation to create any child controls they contain in preparation for posting back or rendering. /// </summary> protected override void CreateChildControls() { // Ensure there is an address if (this.Address == null) { return; } // Ensure we use the "adr" class for the "adr" microformat this.CssClass = (this.CssClass += " adr").TrimStart(); // Create a copy of the address so we can correct the case without affecting the external properties BS7666Address addrCopy = new BS7666Address(String.IsNullOrEmpty(this.Address.Paon) ? String.Empty : this.Address.Paon.Trim(), String.IsNullOrEmpty(this.Address.Saon) ? String.Empty : this.Address.Saon.Trim(), String.IsNullOrEmpty(this.Address.StreetName) ? String.Empty : this.Address.StreetName.Trim(), String.IsNullOrEmpty(this.Address.Locality) ? String.Empty : this.Address.Locality.Trim(), String.IsNullOrEmpty(this.Address.Town) ? String.Empty : this.Address.Town.Trim(), String.IsNullOrEmpty(this.Address.AdministrativeArea) ? String.Empty : this.Address.AdministrativeArea.Trim(), String.IsNullOrEmpty(this.Address.Postcode) ? String.Empty : this.Address.Postcode.Trim()); if (addrCopy.StreetName != null && addrCopy.StreetName.ToUpper(CultureInfo.CurrentCulture) == addrCopy.StreetName) { addrCopy.StreetName = CultureInfo.CurrentCulture.TextInfo.ToTitleCase(addrCopy.StreetName.ToLower(CultureInfo.CurrentCulture)); } if (addrCopy.Locality != null && addrCopy.Locality.ToUpper(CultureInfo.CurrentCulture) == addrCopy.Locality) { addrCopy.Locality = CultureInfo.CurrentCulture.TextInfo.ToTitleCase(addrCopy.Locality.ToLower(CultureInfo.CurrentCulture)); } if (addrCopy.Town != null && addrCopy.Town.ToUpper(CultureInfo.CurrentCulture) == addrCopy.Town) { addrCopy.Town = CultureInfo.CurrentCulture.TextInfo.ToTitleCase(addrCopy.Town.ToLower(CultureInfo.CurrentCulture)); } if (addrCopy.AdministrativeArea != null && addrCopy.AdministrativeArea.ToUpper(CultureInfo.CurrentCulture) == addrCopy.AdministrativeArea) { addrCopy.AdministrativeArea = CultureInfo.CurrentCulture.TextInfo.ToTitleCase(addrCopy.AdministrativeArea.ToLower(CultureInfo.CurrentCulture)); } // Add space to postcode if missing if (addrCopy.Postcode != null && addrCopy.Postcode.IndexOf(" ", StringComparison.Ordinal) == -1) { if (addrCopy.Postcode.Length == 6) { addrCopy.Postcode = addrCopy.Postcode.Substring(0, 3) + " " + addrCopy.Postcode.Substring(3, 3); } else if (addrCopy.Postcode.Length == 7) { addrCopy.Postcode = addrCopy.Postcode.Substring(0, 4) + " " + addrCopy.Postcode.Substring(4, 3); } } // Create a presentational address based on PAF rules for a Simple Address // 1st line of simple address contains the SAON, if there is one bool hasSaon = (addrCopy.Saon != null && addrCopy.Saon.Trim().Length > 0); if (hasSaon) { using (HtmlGenericControl saon = new HtmlGenericControl("span")) { saon.Attributes["class"] = "extended-address"; // adr saon.InnerHtml = HighlightFormat1Name(addrCopy.Saon); this.Controls.Add(saon); this.Controls.Add(new LiteralControl(Separator)); } } if (!hasSaon && !SimpleAddress.IsFormat1Name(addrCopy.Paon)) { // If PAON is more than just a number and line one still spare, we have enough lines // to put PAON and street address on their own separate lines (1st line and 2nd line) if (addrCopy.Paon.Trim().Length > 0) { using (HtmlGenericControl paon = new HtmlGenericControl("span")) { paon.Attributes["class"] = "street-address"; // adr paon.InnerHtml = HighlightFormat1Name(addrCopy.Paon); this.Controls.Add(paon); this.Controls.Add(new LiteralControl(Separator)); } } if (addrCopy.StreetName.Trim().Length > 0) { using (HtmlGenericControl street = new HtmlGenericControl("span")) { street.Attributes["class"] = "street-address"; // adr street.InnerHtml = HighlightFormat1Name(addrCopy.StreetName); this.Controls.Add(street); this.Controls.Add(new LiteralControl(Separator)); } } } else { // If line 1 used by SAON, or PAON is just a number, cram PAON and StreetName onto one line using (HtmlGenericControl street = new HtmlGenericControl("span")) { street.Attributes["class"] = "street-address"; // adr bool hasPaon = (addrCopy.Paon != null && addrCopy.Paon.Length > 0); if (hasPaon && addrCopy.StreetName != null && addrCopy.StreetName.Length > 0) { if (Regex.IsMatch(this.Address.Paon, "^[0-9]{1,2}[-/]?[0-9]{0,2}[A-Z]?$", RegexOptions.IgnoreCase)) { street.InnerHtml = String.Format(CultureInfo.CurrentCulture, "{0} {1}", HighlightFormat1Name(addrCopy.Paon), HighlightFormat1Name(addrCopy.StreetName)); } else { street.InnerHtml = String.Format(CultureInfo.CurrentCulture, "{0}, {1}", HighlightFormat1Name(addrCopy.Paon), HighlightFormat1Name(addrCopy.StreetName)); } } if (hasPaon && (addrCopy.StreetName == null || addrCopy.StreetName.Length == 0)) { street.InnerHtml = HighlightFormat1Name(addrCopy.Paon); } else if ((addrCopy.Paon == null || addrCopy.Paon.Length == 0) && (addrCopy.StreetName != null && addrCopy.StreetName.Length > 0)) { street.InnerHtml = HighlightFormat1Name(addrCopy.StreetName); } if (street.InnerText.Length > 0) { this.Controls.Add(street); this.Controls.Add(new LiteralControl(Separator)); } } } // 3rd line of simple address contains the locality if (addrCopy.Locality != null && addrCopy.Locality.Trim().Length > 0) { using (HtmlGenericControl locality = new HtmlGenericControl("span")) { locality.Attributes["class"] = "street-address"; // adr locality.InnerText = addrCopy.Locality; this.Controls.Add(locality); this.Controls.Add(new LiteralControl(Separator)); } } // 4th line of simple address contains the town if (addrCopy.Town != null && addrCopy.Town.Trim().Length > 0) { using (HtmlGenericControl town = new HtmlGenericControl("span")) { town.Attributes["class"] = "locality"; // adr town.InnerText = addrCopy.Town; this.Controls.Add(town); this.Controls.Add(new LiteralControl(Separator)); } } // 5th line of simple address contains administrative area and postcode bool hasAdministrativeArea = (addrCopy.AdministrativeArea != null && addrCopy.AdministrativeArea.Trim().Length > 0); bool hasPostcode = (addrCopy.Postcode != null && addrCopy.Postcode.Trim().Length > 0); if (hasAdministrativeArea) { using (HtmlGenericControl administrativeArea = new HtmlGenericControl("span")) { administrativeArea.Attributes["class"] = "region"; // adr administrativeArea.InnerText = addrCopy.AdministrativeArea; this.Controls.Add(administrativeArea); } } if (hasAdministrativeArea && hasPostcode) { this.Controls.Add(new LiteralControl(" ")); } if (hasPostcode) { using (HtmlGenericControl postcode = new HtmlGenericControl("span")) { postcode.Attributes["class"] = "postal-code"; // adr postcode.InnerText = addrCopy.Postcode; this.Controls.Add(postcode); } } // Ensure we don't end with a separator if (this.Controls.Count > 1) { while (this.Controls[this.Controls.Count - 1].GetType() == typeof(LiteralControl)) { this.Controls.Remove(this.Controls[this.Controls.Count - 1]); } } // Ensure the control isn't visible if it has no content if (this.Controls.Count == 0) { this.Visible = false; } }
/// <summary> /// Event arguments which allow the passing of an E-GIF/BS7666-compliant address /// </summary> public AddressEventArgs() { this.SimpleAddress = new SimpleAddress(); this.BS7666Address = new BS7666Address(); }