Example #1
0
 public Airport(string airportCode, string city, State state, Geocode geocode)
 {
     AirportCode = airportCode;
     City        = city;
     State       = state;
     Geocode     = geocode;
 }
Example #2
0
        public void GeocodeAddress()
        {
            var response = Geocoder.Geocode(this);

            if (!string.IsNullOrEmpty(response.StreetAddress))
            {
                StreetAddress = response.StreetAddress;
            }
            if (response.City != null)
            {
                City = response.City;
            }
            if (response.County != null)
            {
                County = response.County;
            }
            if (response.State != null)
            {
                State = response.State;
            }
            if (response.ZipCode != null)
            {
                ZipCode = response.ZipCode;
            }
            Geocode = response.Geocode;
        }
        /// <summary>
        /// Uses Great Circle Distance Formula to find the distance between two geocodes in miles.
        /// </summary>
        /// <param name="response"></param>
        /// <returns></returns>
        public double DistanceTo(Geocode geocode)
        {
            const double radiusOfEarthInMiles = 3958.76;

            double lat1 = Latitude * Math.PI / 180;
            double lat2 = geocode.Latitude * Math.PI / 180;
            double lon1 = Longitude * Math.PI / 180;
            double lon2 = geocode.Longitude * Math.PI / 180;

            double x = Math.Sin(lat1) * Math.Sin(lat2) + Math.Cos(lat1) * Math.Cos(lat2) * Math.Cos(lon2 - lon1);
            return radiusOfEarthInMiles * Math.Atan2(Math.Sqrt(1 - Math.Pow(x, 2)), x);
        }
 public Address(string streetAddress, string city, string state, string zipCode, string county, Geocode geocode, bool unknown)
 {
     StreetAddress = streetAddress;
     if(!unknown)
     {
         State = new State(state);
         City = CityList.GetCity(city, State);
         ZipCode = ZipCodeList.GetZipCode(zipCode);
         County = CountyList.GetCounty(county, State);
         Geocode = geocode;
     }
 }
Example #5
0
 public Address(string streetAddress, string city, string state, string zipCode, string county, Geocode geocode, bool unknown)
 {
     StreetAddress = streetAddress;
     if (!unknown)
     {
         State   = new State(state);
         City    = CityList.GetCity(city, State);
         ZipCode = ZipCodeList.GetZipCode(zipCode);
         County  = CountyList.GetCounty(county, State);
         Geocode = geocode;
     }
 }
Example #6
0
        /// <summary>
        /// Uses Great Circle Distance Formula to find the distance between two geocodes in miles.
        /// </summary>
        /// <param name="response"></param>
        /// <returns></returns>
        public double DistanceTo(Geocode geocode)
        {
            const double radiusOfEarthInMiles = 3958.76;

            double lat1 = Latitude * Math.PI / 180;
            double lat2 = geocode.Latitude * Math.PI / 180;
            double lon1 = Longitude * Math.PI / 180;
            double lon2 = geocode.Longitude * Math.PI / 180;

            double x = Math.Sin(lat1) * Math.Sin(lat2) + Math.Cos(lat1) * Math.Cos(lat2) * Math.Cos(lon2 - lon1);

            return(radiusOfEarthInMiles * Math.Atan2(Math.Sqrt(1 - Math.Pow(x, 2)), x));
        }
        private static Address ParseGoogleGeocoderResponse(XContainer doc)
        {
            XNamespace earthNamespace   = "http://earth.google.com/kml/2.0";
            XNamespace addressNamespace = "urn:oasis:names:tc:ciq:xsdschema:xAL:2.0";
            string     state            = null;
            string     county           = null;
            string     city             = null;
            string     streetAddress    = null;
            string     zipCode          = null;
            var        geocodeSetter    = new Dictionary <string, Action <string> >
            {
                { "AdministrativeAreaName", value => state = value },
                { "SubAdministrativeAreaName", value => county = value },
                { "LocalityName", value => city = value },
                { "ThoroughfareName", value => streetAddress = value },
                { "PostalCodeNumber", value => zipCode = value }
            };

            var code = doc.Descendants(earthNamespace + "code").Single().Value;

            if (code != Success)
            {
                if (code == NoLocationFound)
                {
                    return(null);
                }
                throw new InvalidOperationException("Google failed to geocode request: code " + code);
            }

            var placemark = doc.Descendants(earthNamespace + "Placemark").First();

            foreach (var pair in geocodeSetter)
            {
                var element = placemark.Descendants(addressNamespace + pair.Key).SingleOrDefault();
                if (element != null)
                {
                    pair.Value(element.Value);
                }
            }

            var latitudeAndLongitude = placemark.Descendants(earthNamespace + "LatLonBox").Single();
            var geocode = new Geocode(
                double.Parse(latitudeAndLongitude.Attribute("north").Value),
                double.Parse(latitudeAndLongitude.Attribute("east").Value));

            return(new Address(streetAddress, city, state, zipCode, county, geocode));
        }
 public static City FindClosestCity(Geocode geocode)
 {
     Cities.Sort((a, b) => a.Geocode.DistanceTo(geocode).CompareTo(b.Geocode.DistanceTo(geocode)));
     return Cities.First();
 }
Example #9
0
 public Address(string streetAddress, string city, string state, string zipCode, string county, Geocode geocode)
     : this(streetAddress, city, state, zipCode, county, geocode, false)
 {
 }
 public County(string name, State state, Geocode geocode)
 {
     Name = name;
     State = state;
     Geocode = geocode;
 }
 public static IEnumerable <County> FindNearbyCounties(Geocode geocode, double rangeInMiles)
 {
     return(Counties.Where(c => c.Geocode.DistanceTo(geocode) < rangeInMiles));
 }
 public static County FindClosestCounty(Geocode geocode)
 {
     Counties.Sort((a, b) => a.Geocode.DistanceTo(geocode).CompareTo(b.Geocode.DistanceTo(geocode)));
     return(Counties.First());
 }
        private static Address ParseGoogleGeocoderResponse(XContainer doc)
        {
            XNamespace earthNamespace = "http://earth.google.com/kml/2.0";
            XNamespace addressNamespace = "urn:oasis:names:tc:ciq:xsdschema:xAL:2.0";
            string state = null;
            string county = null;
            string city = null;
            string streetAddress = null;
            string zipCode = null;
            var geocodeSetter = new Dictionary<string, Action<string>>
            {
                {"AdministrativeAreaName", value => state = value},
                {"SubAdministrativeAreaName", value => county = value},
                {"LocalityName", value => city = value},
                {"ThoroughfareName", value => streetAddress = value},
                {"PostalCodeNumber", value => zipCode = value}
            };

            var code = doc.Descendants(earthNamespace + "code").Single().Value;
            if(code != Success)
            {
                if(code == NoLocationFound)
                {
                    return null;
                }
                throw new InvalidOperationException("Google failed to geocode request: code " + code);
            }

            var placemark = doc.Descendants(earthNamespace + "Placemark").First();

            foreach (var pair in geocodeSetter)
            {
                var element = placemark.Descendants(addressNamespace + pair.Key).SingleOrDefault();
                if (element != null)
                {
                    pair.Value(element.Value);
                }
            }

            var latitudeAndLongitude = placemark.Descendants(earthNamespace + "LatLonBox").Single();
            var geocode = new Geocode(
                double.Parse(latitudeAndLongitude.Attribute("north").Value),
                double.Parse(latitudeAndLongitude.Attribute("east").Value));

            return new Address(streetAddress, city, state, zipCode, county, geocode);
        }
 public static IEnumerable<City> FindNearbyCities(Geocode geocode, double rangeInMiles)
 {
     return Cities.Where(c => c.Geocode.DistanceTo(geocode) < rangeInMiles);
 }
 public static IEnumerable<Airport> FindNearbyAirports(Geocode geocode, double rangeInMiles)
 {
     return Airports.Where(a => a.Geocode.DistanceTo(geocode) < rangeInMiles);
 }
 public ZipCode(string code, Geocode geocode)
 {
     Code = code;
     Geocode = geocode;
 }
 public Address(string streetAddress, string city, string state, string zipCode, string county, Geocode geocode)
     : this(streetAddress, city, state, zipCode, county, geocode, false)
 {
 }
 public static IEnumerable <Airport> FindNearbyAirports(Geocode geocode, double rangeInMiles)
 {
     return(Airports.Where(a => a.Geocode.DistanceTo(geocode) < rangeInMiles));
 }
 public static Airport FindClosestAirport(Geocode geocode)
 {
     Airports.Sort((a, b) => a.Geocode.DistanceTo(geocode).CompareTo(b.Geocode.DistanceTo(geocode)));
     return(Airports.First());
 }
Example #20
0
 public ZipCode(string code, Geocode geocode)
 {
     Code    = code;
     Geocode = geocode;
 }
Example #21
0
 public City(string name, State state, Geocode geocode)
 {
     Name    = name;
     State   = state;
     Geocode = geocode;
 }
 public static Airport FindClosestAirport(Geocode geocode)
 {
     Airports.Sort((a, b) => a.Geocode.DistanceTo(geocode).CompareTo(b.Geocode.DistanceTo(geocode)));
     return Airports.First();
 }