Example #1
0
        /// <summary>
        /// Gets the geographic co ordinates and radius for a location by querying a GeoSource endpoint
        /// </summary>
        /// <param name="geoString">Location name</param>
        /// <param name="geoSource">Geosource to query</param>
        /// <returns>the geo point</returns>
        public static GeocodePoint GetGeoData(string geoString, GeoSource geoSource)
        {
            GeocodePoint geocodePoint = null;

            switch (geoSource)
            {
            case GeoSource.VirtualEarth:
                geocodePoint = GetGeoDataFromVirtualEarth(geoString);
                break;

            case GeoSource.LoMoLobby:
                geocodePoint = GetGeoDataFromLobby(geoString);
                break;
            }

            return(geocodePoint);
        }
Example #2
0
        /// <summary>
        /// Queries the LomoLobby for the Location data - Lat,Lon,BoundingBox
        /// </summary>
        /// <param name="geoString">Search text having the location name</param>
        /// <returns>GeocodePoint that encapsulates lat,lon,boundingbox and radius</returns>
        private static GeocodePoint GetGeoDataFromLobby(string geoString)
        {
            string       strSearchResults;
            GeocodePoint geocodePoint = null;

            using (var webClient = new WebClient())
            {
                string geoApiUrl = string.Format(LoMoLobbyApi, geoString);
                strSearchResults = webClient.DownloadString(geoApiUrl);
            }

            if (!string.IsNullOrEmpty(strSearchResults))
            {
                var lobbyResults = JsonConvert.DeserializeObject <dynamic>(strSearchResults);
                if (lobbyResults != null && lobbyResults.locations.Count > 0 && lobbyResults.locations[0].RelatedWordsFromQuery.Count > 0)
                {
                    double centerLatitude  = 0.0;
                    double centerLongitude = 0.0;
                    double southLatitude   = 0.0;
                    double northLatitude   = 0.0;
                    double eastLongitude   = 0.0;
                    double westLongitude   = 0.0;

                    if (lobbyResults.locations[0].CenterLatitude != null)
                    {
                        centerLatitude = lobbyResults.locations[0].CenterLatitude.Value;
                    }

                    if (lobbyResults.locations[0].CenterLongtitude != null)
                    {
                        centerLongitude = lobbyResults.locations[0].CenterLongtitude.Value;
                    }

                    if (lobbyResults.locations[0].SouthLatitude != null)
                    {
                        southLatitude = lobbyResults.locations[0].SouthLatitude.Value;
                    }

                    if (lobbyResults.locations[0].NorthLatitude != null)
                    {
                        northLatitude = lobbyResults.locations[0].NorthLatitude.Value;
                    }

                    if (lobbyResults.locations[0].EastLongitude != null)
                    {
                        eastLongitude = lobbyResults.locations[0].EastLongitude.Value;
                    }

                    if (lobbyResults.locations[0].WestLongitude != null)
                    {
                        westLongitude = lobbyResults.locations[0].WestLongitude.Value;
                    }

                    if (centerLatitude != 0.0 && centerLongitude != 0.0 && southLatitude != 0.0 && northLatitude != 0.0 && eastLongitude != 0.0 && westLongitude != 0.0)
                    {
                        BoundingBox boundingBox = new BoundingBox
                        {
                            SouthLatitude = southLatitude,
                            NorthLatitude = northLatitude,
                            EastLongitude = eastLongitude,
                            WestLongitude = westLongitude
                        };
                        double radius = GetRadiusFromBoundingBox(boundingBox);
                        geocodePoint = new GeocodePoint
                        {
                            LocationName = lobbyResults.locations[0].RelatedWordsFromQuery[0].Value,
                            Latitude     = centerLatitude,
                            Longitude    = centerLongitude,
                            Radius       = radius,
                            BoundingBox  = boundingBox
                        };
                    }
                }
            }

            return(geocodePoint);
        }
Example #3
0
        /// <summary>
        /// Queries the LomoLobby for the Location data - Lat,Lon,BoundingBox
        /// </summary>
        /// <param name="geoString">Location Name</param>
        /// <returns>GeocodePoint that encapsulates lat,lon,boundingbox and radius</returns>
        private static GeocodePoint GetGeoDataFromVirtualEarth(string geoString)
        {
            if (string.IsNullOrEmpty(geoString))
            {
                return(null);
            }

            var geoServiceUrl = string.Format(GeoCodeAPI, geoString);
            var client        = new WebClient();
            var str           = client.DownloadString(geoServiceUrl);
            var xml           = XElement.Parse(str);

            var geocodePoint = new GeocodePoint {
                LocationName = geoString
            };
            var pointNode = xml.Descendants(VENameSpace + "Point").FirstOrDefault();
            var latNode   = pointNode != null?pointNode.Descendants(VENameSpace + "Latitude").FirstOrDefault() : null;

            var lonNode = pointNode != null?pointNode.Descendants(VENameSpace + "Longitude").FirstOrDefault() : null;

            if (latNode != null && lonNode != null)
            {
                geocodePoint.Latitude  = double.Parse(latNode.Value);
                geocodePoint.Longitude = double.Parse(lonNode.Value);
            }

            var boundingBoxNode = xml.Descendants(VENameSpace + "BoundingBox").FirstOrDefault();
            var southLatNode    = boundingBoxNode != null?boundingBoxNode.Descendants(VENameSpace + "SouthLatitude").FirstOrDefault() : null;

            var northLatNode = boundingBoxNode != null?boundingBoxNode.Descendants(VENameSpace + "NorthLatitude").FirstOrDefault() : null;

            var westLonNode = boundingBoxNode != null?boundingBoxNode.Descendants(VENameSpace + "WestLongitude").FirstOrDefault() : null;

            var eastLonNode = boundingBoxNode != null?boundingBoxNode.Descendants(VENameSpace + "EastLongitude").FirstOrDefault() : null;

            if (southLatNode != null && northLatNode != null && westLonNode != null && eastLonNode != null)
            {
                geocodePoint.Radius = 0.5 * GetGeoDistanceMeters(
                    double.Parse(southLatNode.Value),
                    double.Parse(westLonNode.Value),
                    double.Parse(northLatNode.Value),
                    double.Parse(eastLonNode.Value));
                var boundingBox = new BoundingBox
                {
                    SouthLatitude = double.Parse(southLatNode.Value),
                    NorthLatitude = double.Parse(northLatNode.Value),
                    EastLongitude = double.Parse(eastLonNode.Value),
                    WestLongitude = double.Parse(westLonNode.Value)
                };
                geocodePoint.BoundingBox = boundingBox;
            }

            var addressNode = xml.Descendants(VENameSpace + "Address").FirstOrDefault();

            if (addressNode != null)
            {
                var adminDistrict = addressNode.Descendants(VENameSpace + "AdminDistrict").FirstOrDefault();
                var countryRegion = addressNode.Descendants(VENameSpace + "CountryRegion").FirstOrDefault();
                var locality      = addressNode.Descendants(VENameSpace + "Locality").FirstOrDefault();
                var postalCode    = addressNode.Descendants(VENameSpace + "PostalCode").FirstOrDefault();

                geocodePoint.Location = new GeoLocation()
                {
                    AdminDistrict = adminDistrict != null ? adminDistrict.Value : string.Empty,
                    CountryRegion = countryRegion != null ? countryRegion.Value : string.Empty,
                    Locality      = locality != null ? locality.Value : string.Empty,
                    PostalCode    = postalCode != null ? postalCode.Value : string.Empty
                };
            }

            return(geocodePoint);
        }