Пример #1
0
        private GeocodeResult <Result> GetBestGeocodeResult(GoogleGeocodeResult geocodeResult, SourceAddress source)
        {
            var result = new GeocodeResult <Result>();

            if (geocodeResult.Results.Count == 0)
            {
                result.Status = geocodeResult.Status;
                return(result);
            }

            var best = geocodeResult.Results[0]; //The best result

            var typeAllowed = best.Types.Contains(TypesAlloweds.FirstOrDefault(x => x.Equals(best.Types[0])));

            if (typeAllowed)
            {
                var postalCode = best.AddressComponents.Where(x => x.Types[0].Equals("postal_code")).FirstOrDefault();
                if (postalCode != null)
                {
                    result.Status = postalCode.LongName.Equals(source.PostalCode) && best.Geometry.LocationType.Equals("ROOFTOP") ? "OK" : "TO_CHECK";
                }
                else
                {
                    result.Status = "TO_CHECK";
                }
            }
            else
            {
                result.Status = "TO_CHECK";
            }

            result.Result = best;
            return(result);
        }
Пример #2
0
        public async Task GeocodeAsync(SourceAddress source)
        {
            Uri geocodeRequest = new Uri(BuildUrlRequest(source));

            using (HttpClient client = new HttpClient())
            {
                client.DefaultRequestHeaders.Accept.Add(
                    new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));

                var response = await client.GetAsync(geocodeRequest);

                if (response.IsSuccessStatusCode)
                {
                    string content = await response.Content.ReadAsStringAsync();

                    var geocodeResult = JsonConvert.DeserializeObject <GoogleGeocodeResult>(content);

                    GeocodeResult <Result> bestResult = GetBestGeocodeResult(geocodeResult, source);

                    string freeformAddress = bestResult.Result == null ? "NULL" : bestResult.Result.FormattedAddress;
                    string resultType      = bestResult.Result == null ? "NULL" : bestResult.Result.Types[0];
                    ReportComparer.SaveAndDisplay(Path, $"Google Geocode;{source.Id};{source.Address};{source.Locality};{source.PostalCode};{source.Country};{resultType};{freeformAddress};{bestResult.Status};{geocodeResult.Results.Count}");
                }
                else
                {
                    //var error = await response.Content.ReadAsAsync<ErrorResponse>();
                    Console.WriteLine("Google HTTP Error");
                    ReportComparer.SaveAndDisplay(Path, $"Google Geocode;{source.Id};{source.Address};{source.Locality};{source.PostalCode};{source.Country};NULL;NULL;ZERO_RESULTS;0");
                }
            }
        }
Пример #3
0
        public async Task GeocodeAsync(SourceAddress source)
        {
            using (HttpClient client = new HttpClient())
            {
                client.DefaultRequestHeaders.Accept.Add(
                    new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));

                var response = await client.GetAsync(BuildUrlRequest(source));

                if (response.IsSuccessStatusCode)
                {
                    string content = await response.Content.ReadAsStringAsync();

                    ArcGisGeocodeResult       geocode    = JsonConvert.DeserializeObject <ArcGisGeocodeResult>(content);
                    GeocodeResult <Candidate> bestResult = GetBestGeocodeResult(geocode, source);

                    string freeformAddress = bestResult.Result == null ? "NULL" : bestResult.Result.Address;
                    string resultType      = bestResult.Result == null ? "NULL" : bestResult.Result.Attributes.AddrType;

                    ReportComparer.SaveAndDisplay(Path, $"ArcGis;{source.Id};{source.Address};{source.Locality};{source.PostalCode};{source.Country};{resultType};{freeformAddress};{bestResult.Status};{geocode.Candidates.Count};{CountByScore(geocode)}");
                }
                else
                {
                    //var error = await response.Content.ReadAsAsync<ErrorResponse>();
                    Console.WriteLine("ArcGis HTTP Error");
                    ReportComparer.SaveAndDisplay(Path, $"ArcGis;{source.Id};{source.Address};{source.Locality};{source.PostalCode};{source.Country};NULL;NULL;ZERO_RESULT;0");
                }
            }
        }
Пример #4
0
        private GeocodeResult <Resource> GetBestGeocodeResult(BingMapsGeocodeResult geocodeResult, SourceAddress source)
        {
            var result = new GeocodeResult <Resource>();


            if (geocodeResult.ResourceSets[0].Resources.Count == 0)
            {
                result.Status = "ZERO_RESULT";
                return(result);
            }

            var best = geocodeResult.ResourceSets[0].Resources[0]; //The best result

            if (!best.Address.CountryRegion.Equals("Spain"))
            {
                result.Status = "ZERO_RESULT";
                return(result);
            }

            /* EntityType:
             *
             *  Neighborhood:A section of a populated place that is typically well-known, but often with indistinct boundaries.
             *  Roadblock: Like Street
             *  Address: indicates a precise street address
             * Ref:https://docs.microsoft.com/en-us/bingmaps/rest-services/locations/location-data
             * Ref:https://docs.microsoft.com/en-us/bingmaps/spatial-data-services/public-data-sources/poi-entity-types
             */

            if (best.EntityType.Equals("Address") || best.EntityType.Equals("Roadblock"))
            {
                if (best.MatchCodes[0].Equals("Good") && (best.Confidence.Equals("High") || best.Confidence.Equals("Medium")))
                {
                    result.Status = best.Address.PostalCode.Equals(source.PostalCode) ? "OK" : "TO_CHECK";
                }
                else
                {
                    result.Status = "TO_CHECK";
                }
            }
            else
            {
                result.Status = "TO_CHECK";
            }

            result.Result = best;
            return(result);
        }
Пример #5
0
        private GeocodeResult <Candidate> GetBestGeocodeResult(ArcGisGeocodeResult geocodeResult, SourceAddress source)
        {
            var result = new GeocodeResult <Candidate>();

            if (geocodeResult.Candidates.Count == 0)
            {
                result.Status = "ZERO_RESULTS";
                return(result);
            }

            var best = geocodeResult.Candidates[0]; //The best result

            if (string.IsNullOrEmpty(best.Attributes.Country))
            {
                result.Status = "ZERO_RESULT";
                return(result);
            }

            if (!best.Attributes.Country.Equals("ESP"))
            {
                result.Status = "ZERO_RESULT";
                return(result);
            }

            var typeAllowed = best.Attributes.AddrType.Equals(TypesAlloweds.FirstOrDefault(x => x.Equals(best.Attributes.AddrType)) ?? string.Empty);

            if (typeAllowed)
            {
                result.Status = best.Attributes.Postal.Equals(source.PostalCode) ? "OK" : "TO_CHECK";
            }
            else
            {
                result.Status = "TO_CHECK";
            }

            result.Result = best;
            return(result);
        }