Exemplo n.º 1
0
        /// <summary>
        /// Get coordinates for a given addresses. Only used by the service class, do not call elsewhere.
        /// </summary>
        /// <param name="address"></param>
        /// <param name="type"></param>
        /// <param name="correctAddresses">Set this to use address laundering prior to each request. Default: false</param>
        /// <exception cref="AddressCoordinatesException">Thrown if address has critical spelling erros(see inner exception) or if no address coordinates correspond to the entered address.</exception>
        /// <returns></returns>
        public Coordinates GetCoordinates(Address address, Coordinates.CoordinatesType type, bool correctAddresses = false)
        {
            Address correctedAddress = address;

            if (!correctAddresses)
            {
                try
                {
                    AddressLaundering launderer = new AddressLaundering();
                    correctedAddress = launderer.LaunderAddress(address);
                }
                catch (AddressLaunderingException e)
                {
                    throw new AddressCoordinatesException("Errors in address, see inner exception.", e);
                }
            }

            var request = CreateCoordRequest(correctedAddress.StreetName, correctedAddress.StreetNumber, correctedAddress.ZipCode.ToString());

            string addressesString = ExecuteAndRead(request);
            var    addresses       = ParseJson(addressesString);

            if (!addresses.Any())
            {
                request = CreateCoordRequest(correctedAddress.StreetName, null, correctedAddress.ZipCode.ToString());

                addressesString = ExecuteAndRead(request);
                addresses       = ParseJson(addressesString);
            }

            if (!addresses.Any())
            {
                throw new AddressCoordinatesException("No coordinates returned.");
            }

            Coordinates addressCoordinates = new Coordinates {
                Type = type
            };

            if (addresses[0].adgangsadresse.vejstykke.navn == correctedAddress.StreetName &&
                addresses[0].adgangsadresse.postnummer.nr == correctedAddress.ZipCode.ToString())
            {
                addressCoordinates.Longitude = addresses[0].adgangsadresse.adgangspunkt.koordinater[0].ToString().Replace(",", ".");
                addressCoordinates.Latitude  = addresses[0].adgangsadresse.adgangspunkt.koordinater[1].ToString().Replace(",", ".");

                //addressCoordinates.Longitude = addressCoordinates.Longitude.Remove(addressCoordinates.Longitude.IndexOf('.') + 1 + COORD_DECIMALS);
                //addressCoordinates.Latitude = addressCoordinates.Latitude.Remove(addressCoordinates.Latitude.IndexOf('.') + 1 + COORD_DECIMALS);
            }
            else
            {
                throw new AddressCoordinatesException("The addresses returned differ highly from the original, streetname does not exist in zipcode area.");
            }

            return(addressCoordinates);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Get coordinates for a given addresses. Use this method for single calls outside the service class.
        /// </summary>
        /// <param name="address"></param>
        /// <param name="correctAddresses">Set this to use address laundering prior to each request, the corrected values will be replaced. Default: false</param>
        /// <exception cref="AddressCoordinatesException">Thrown if address has critical spelling errors(see inner exception) or if no address coordinates correspond to the entered address.</exception>
        /// <returns></returns>
        public Address GetAddressCoordinates(Address address, bool correctAddresses = false)
        {
            Address correctedAddress = address;

            try
            {
                if (address.Latitude != null && address.Longitude != null && address.Latitude != "" && address.Longitude != "")
                {
                    return(correctedAddress);
                }
                AddressLaundering launderer = new AddressLaundering();
                correctedAddress = launderer.LaunderAddress(address);
                return(correctedAddress);
            }
            catch (AddressLaunderingException e)
            {
                throw new AddressCoordinatesException("En valgt adresse kunne ikke vaskes.", e);
            }
            //var request = CreateCoordRequest(correctedAddress.StreetName, correctedAddress.StreetNumber, correctedAddress.ZipCode.ToString());

            //string addressesString = ExecuteAndRead(request);
            //var addresses = ParseJson(addressesString);

            //if (!addresses.Any())
            //{
            //    request = CreateCoordRequest(address.StreetName, null, address.ZipCode.ToString());

            //    addressesString = ExecuteAndRead(request);
            //    addresses = ParseJson(addressesString);
            //}

            //if (!addresses.Any())
            //{
            //    throw new AddressCoordinatesException("No coordinates returned.");
            //}

            //if (addresses[0].adgangsadresse.vejstykke.navn == address.StreetName
            //    && addresses[0].adgangsadresse.postnummer.nr == address.ZipCode.ToString())
            //{
            //    correctedAddress.Longitude = addresses[0].adgangsadresse.adgangspunkt.koordinater[0].ToString().Replace(",", ".");
            //    correctedAddress.Latitude = addresses[0].adgangsadresse.adgangspunkt.koordinater[1].ToString().Replace(",", ".");

            //    correctedAddress.Longitude = correctedAddress.Longitude.Remove(correctedAddress.Longitude.IndexOf('.') + 1 + CoordDecimals);
            //    correctedAddress.Latitude = correctedAddress.Latitude.Remove(correctedAddress.Latitude.IndexOf('.') + 1 + CoordDecimals);
            //}
            //else
            //{
            //    throw new AddressCoordinatesException("The addresses returned differ highly from the original, streetname does not exist in zipcode area.");
            //}

            //return correctedAddress;
        }