예제 #1
0
        /// <summary>
        /// Geocodes and returns the result.
        /// </summary>
        /// <param name="street"></param>
        /// <param name="houseNumber"></param>
        /// <param name="country"></param>
        /// <param name="postalCode"></param>
        /// <param name="commune"></param>
        /// <returns></returns>
        public IGeoCoderResult Code(
            string country,
            string postalCode,
            string commune,
            string street,
            string houseNumber)
        {
            // build the request url.
            var builder = new StringBuilder();

            builder.Append(street);
            builder.Append(" ");
            builder.Append(houseNumber);
            builder.Append(" ");
            builder.Append(postalCode);
            builder.Append(" ");
            builder.Append(commune);
            builder.Append(" ");
            builder.Append(country);
            builder.Append(" ");
            string url = string.Format(System.Globalization.CultureInfo.InvariantCulture,
                                       _geocodingUrl + "&format=xml&polygon=1&addressdetails=1", builder);

            // create the source and get the xml.
            IXmlSource source = this.DownloadXml(url);

            // create the kml.
            var search_doc = new SearchDocument(source);

            // check if there are responses.
            var res = new GeoCoderResult();

            res.Accuracy = AccuracyEnum.UnkownLocationLevel;

            if (search_doc.Search is searchresults)
            {
                searchresults result_v1 = search_doc.Search as searchresults;
                if (result_v1.place != null && result_v1.place.Length > 0)
                {
                    double latitude;
                    double longitude;

                    if (double.TryParse(result_v1.place[0].lat, System.Globalization.NumberStyles.Float,
                                        System.Globalization.CultureInfo.InvariantCulture, out latitude)
                        &&
                        double.TryParse(result_v1.place[0].lon, System.Globalization.NumberStyles.Float,
                                        System.Globalization.CultureInfo.InvariantCulture, out longitude))
                    {
                        res.Latitude  = latitude;
                        res.Longitude = longitude;
                        res.Text      = result_v1.place[0].display_name;

                        switch (result_v1.place[0].@class)
                        {
                        case "place":
                            switch (result_v1.place[0].type)
                            {
                            case "town":
                                res.Accuracy = AccuracyEnum.TownLevel;
                                break;

                            case "house":
                                res.Accuracy = AccuracyEnum.AddressLevel;
                                break;
                            }
                            break;

                        case "highway":
                            res.Accuracy = AccuracyEnum.StreetLevel;
                            break;

                        case "boundary":
                            res.Accuracy = AccuracyEnum.PostalCodeLevel;
                            break;
                        }
                    }
                }
            }
            else if (search_doc.Search is OsmSharp.Xml.Nominatim.Reverse.v1.reversegeocode)
            {
                reversegeocode result_v1 = search_doc.Search as OsmSharp.Xml.Nominatim.Reverse.v1.reversegeocode;
                if (result_v1.result != null && result_v1.result.Length > 0)
                {
                    double latitude;
                    double longitude;

                    if (double.TryParse(result_v1.result[0].lat, System.Globalization.NumberStyles.Float,
                                        System.Globalization.CultureInfo.InvariantCulture, out latitude)
                        &&
                        double.TryParse(result_v1.result[0].lon, System.Globalization.NumberStyles.Float,
                                        System.Globalization.CultureInfo.InvariantCulture, out longitude))
                    {
                        res.Latitude  = latitude;
                        res.Longitude = longitude;
                        res.Text      = result_v1.result[0].Value;
                        res.Accuracy  = AccuracyEnum.UnkownLocationLevel;
                    }
                }
            }

            return(res);
        }
예제 #2
0
        /// <summary>
        /// Geocodes and returns the result.
        /// </summary>
        /// <param name="street"></param>
        /// <param name="houseNumber"></param>
        /// <param name="country"></param>
        /// <param name="postalCode"></param>
        /// <param name="commune"></param>
        /// <returns></returns>
        public IGeoCoderResult Code(
            string country,
            string postalCode,
            string commune,
            string street,
            string houseNumber)
        {
            // build the request url.
            var builder = new StringBuilder();
            builder.Append(street);
            builder.Append(" ");
            builder.Append(houseNumber);
            builder.Append(" ");
            builder.Append(postalCode);
            builder.Append(" ");
            builder.Append(commune);
            builder.Append(" ");
            builder.Append(country);
            builder.Append(" ");
            string url = string.Format(System.Globalization.CultureInfo.InvariantCulture,
                _geocodingUrl + "&format=xml&polygon=1&addressdetails=1", builder);

            // create the source and get the xml.
            IXmlSource source = this.DownloadXml(url);

            // create the kml.
            var search_doc = new SearchDocument(source);

            // check if there are responses.
            var res = new GeoCoderResult();
            res.Accuracy = AccuracyEnum.UnkownLocationLevel;

            if (search_doc.Search is searchresults)
            {
                searchresults result_v1 = search_doc.Search as searchresults;
                if (result_v1.place != null && result_v1.place.Length > 0)
                {
                    double latitude;
                    double longitude;

                    if (double.TryParse(result_v1.place[0].lat, System.Globalization.NumberStyles.Float,
                                        System.Globalization.CultureInfo.InvariantCulture, out latitude)
                        &&
                        double.TryParse(result_v1.place[0].lon, System.Globalization.NumberStyles.Float,
                                        System.Globalization.CultureInfo.InvariantCulture, out longitude))
                    {
                        res.Latitude = latitude;
                        res.Longitude = longitude;
                        res.Text = result_v1.place[0].display_name;

                        switch (result_v1.place[0].@class)
                        {
                            case "place":
                                switch (result_v1.place[0].type)
                                {
                                    case "town":
                                        res.Accuracy = AccuracyEnum.TownLevel;
                                        break;
                                    case "house":
                                        res.Accuracy = AccuracyEnum.AddressLevel;
                                        break;
                                }
                                break;
                            case "highway":
                                res.Accuracy = AccuracyEnum.StreetLevel;
                                break;
                            case "boundary":
                                res.Accuracy = AccuracyEnum.PostalCodeLevel;
                                break;
                        }
                    }
                }
            }
            else if (search_doc.Search is OsmSharp.Xml.Nominatim.Reverse.v1.reversegeocode)
            {
                reversegeocode result_v1 = search_doc.Search as OsmSharp.Xml.Nominatim.Reverse.v1.reversegeocode;
                if (result_v1.result != null && result_v1.result.Length > 0)
                {
                    double latitude;
                    double longitude;

                    if (double.TryParse(result_v1.result[0].lat, System.Globalization.NumberStyles.Float,
                                        System.Globalization.CultureInfo.InvariantCulture, out latitude)
                        &&
                        double.TryParse(result_v1.result[0].lon, System.Globalization.NumberStyles.Float,
                                        System.Globalization.CultureInfo.InvariantCulture, out longitude))
                    {
                        res.Latitude = latitude;
                        res.Longitude = longitude;
                        res.Text = result_v1.result[0].Value;
                        res.Accuracy = AccuracyEnum.UnkownLocationLevel;
                    }
                }
            }

            return res;
        }