Exemple #1
0
        public void Yahoo_GeoPlanet_Place_Postal_ShouldBeConvertedToNull_WhenEmptyOrWhiteSpace()
        {
            var it = new Place
            {
                Postal = "   ",
            };

            it.ShouldNotBeNull();
            it.Postal.ShouldBeNull();
        }
Exemple #2
0
        public void Yahoo_GeoPlanet_Place_ShouldOverrideToString()
        {
            var it = new Place
            {
                Name = "place name",
                Type = new PlaceType { Name = "type name" }
            };

            it.ShouldNotBeNull();
            it.ToString().ShouldEqual("place name (type name)");
        }
Exemple #3
0
        public void Yahoo_GeoPlanet_Place_ShouldBePublic()
        {
            var it = new Place
            {
                WoeId = 4,
                Language = "language",
                Uri = new Uri("http://ngeo.codeplex.com"),
                Name = "name",
                Type = new PlaceType(),
                Country = new Admin(),
                Admin1 = new Admin(),
                Admin2 = new Admin(),
                Admin3 = new Admin(),
                Locality1 = new Locality(),
                Locality2 = new Locality(),
                Postal = "postal",
                AreaRank = 44,
                PopulationRank = 69,
                Center = new Point(),
                BoundingBox = new BoundingBox(),
            };

            it.ShouldNotBeNull();
            it.WoeId.ShouldEqual(4);
            it.Language.ShouldNotBeNull();
            it.Uri.ShouldNotBeNull();
            it.Name.ShouldNotBeNull();
            it.Type.ShouldNotBeNull();
            it.Country.ShouldNotBeNull();
            it.Admin1.ShouldNotBeNull();
            it.Admin2.ShouldNotBeNull();
            it.Admin3.ShouldNotBeNull();
            it.Locality1.ShouldNotBeNull();
            it.Locality2.ShouldNotBeNull();
            it.Postal.ShouldNotBeNull();
            it.AreaRank.ShouldEqual(44);
            it.PopulationRank.ShouldEqual(69);
            it.Center.ShouldNotBeNull();
            it.BoundingBox.ShouldNotBeNull();
        }
Exemple #4
0
        internal static int Handle(WoeIdByCoordinates query, IContainGeoNames geoNamesContainer, IContainGeoPlanet geoPlanetContainer)
        {
            if (!query.Coordinates.Latitude.HasValue)
            {
                throw new ArgumentException("Query's Coordinates.Latitude is null.");
            }
            if (!query.Coordinates.Longitude.HasValue)
            {
                throw new ArgumentException("Query's Coordinates.Longitude is null.");
            }

            int?woeId = null;

            // first, invoke geonames geocoder
            var retryCount = 0;
            IEnumerable <Toponym> geoNames = null;

            while (geoNames == null && retryCount < 6)
            {
                ++retryCount;
                geoNames = geoNamesContainer.FindNearbyPlaceName(new NearbyPlaceNameFinder
                {
                    Latitude  = query.Coordinates.Latitude.Value,
                    Longitude = query.Coordinates.Longitude.Value,
                    Language  = "en",
                });
            }
            if (geoNames == null)
            {
                throw new ApplicationException("Querying GeoNames service resulted in null result.");
            }
            geoNames = geoNames.ToArray();

            foreach (var geoName in geoNames)
            {
                // try to concord with woeid
                var concordance = geoPlanetContainer.Concordance(ConcordanceNamespace.GeoNames, geoName.GeoNameId);
                if (concordance == null || concordance.WoeId <= 0)
                {
                    continue;
                }

                // make sure place exists
                var geoPlanetPlace = geoPlanetContainer.Place(concordance.WoeId);
                if (geoPlanetPlace == null)
                {
                    continue;
                }

                woeId = concordance.WoeId;
                break;
            }

            // if there is still no WOE ID, try a textual search
            if (!woeId.HasValue)
            {
                foreach (var geoName in geoNames)
                {
                    var searchText = new StringBuilder(geoName.Name);
                    if (!string.IsNullOrWhiteSpace(geoName.Admin3Name))
                    {
                        searchText.Append(" " + geoName.Admin3Name);
                    }
                    if (!string.IsNullOrWhiteSpace(geoName.Admin2Name))
                    {
                        searchText.Append(" " + geoName.Admin2Name);
                    }
                    if (!string.IsNullOrWhiteSpace(geoName.Admin1Name))
                    {
                        searchText.Append(" " + geoName.Admin1Name);
                    }
                    searchText.Append(", " + geoName.CountryName);

                    var geoPlanetPlaces = geoPlanetContainer.Places(searchText.ToString().Replace("/", "-"))
                                          .Where(x => x.WoeId > 0).ToArray();

                    NGeo.Yahoo.GeoPlanet.Place geoPlanetPlace = null;
                    if (geoPlanetPlaces.Length == 1)
                    {
                        geoPlanetPlace = geoPlanetPlaces.Single();
                    }
                    else if (geoPlanetPlaces.Length > 1)
                    {
                        // when multiple results are found, pick the one with
                        // the closest lat & lng match
                        var gn = geoName;
                        Func <NGeo.Yahoo.GeoPlanet.Place, double> coordinateComparer = x =>
                        {
                            // is this lat/lng greater or less than the geonames lat/lng?
                            var latDiff = x.Center.Latitude > gn.Latitude ? x.Center.Latitude - gn.Latitude : gn.Latitude - x.Center.Latitude;
                            var lngDiff = x.Center.Longitude > gn.Longitude ? x.Center.Longitude - gn.Longitude : gn.Longitude - x.Center.Longitude;
                            return(latDiff + lngDiff);
                        };
                        var closest    = geoPlanetPlaces.OrderBy(coordinateComparer).ToArray();
                        var separation = closest.Select(coordinateComparer).First();
                        if (separation < 1)
                        {
                            geoPlanetPlace = closest.First();
                        }
                    }

                    if (geoPlanetPlace != null)
                    {
                        woeId = geoPlanetPlace.WoeId;
                    }
                }
            }

            return(woeId.HasValue
                ? woeId.Value
                : GeoPlanetPlace.EarthWoeId);
        }