<commit_before>﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Text;
using System.Web.Script.Serialization;
using MapWinGIS;

namespace MWLite.Core.GeoLocation
{
    public static class GeoLocationHelper
    {
        private static string _license = "";

        public static string License
        {
            get { return _license; }
        }

        public static Extents FindLocation(string query)
        {
            string json = ResquestLocation(query);
            return ParseResult(json);
        }

        private static string ResquestLocation(string query)
        {
            //var url = "https://maps.googleapis.com/maps/api/geocode/json?address=" + query;
            var url = string.Format("http://nominatim.openstreetmap.org/search/{0}?format=json&limit=1", query);
            var request = (HttpWebRequest)WebRequest.Create(url);
            
            request.Referer = "mapwingis.codeplex.com";
            request.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.9.1.7) Gecko/20091221 Firefox/3.5.7";
            request.Timeout = 5000;
            request.ReadWriteTimeout = request.Timeout * 6;
            request.Accept = "*/*";
            string result = "";

            try
            {
                using (var response = request.GetResponse() as HttpWebResponse)
                {
                    if (response != null)
                    {
                        if (response.StatusCode == HttpStatusCode.OK)
                        {
                            using (Stream responseStream = response.GetResponseStream())
                            {
                                if (responseStream != null)
                                {
                                    using (var read = new StreamReader(responseStream, Encoding.UTF8))
                                    {
                                        result = read.ReadToEnd();
                                    }
                                }
                            }
                        }
                        response.Close();
                    }
                }
            }
            catch
            {
                throw new ApplicationException("Failed to run HTTP request.");
            }
            return result;
        }

        private static Extents ParseResult(string result)
        {
            try
            {
                var serializer = new JavaScriptSerializer();
                serializer.RegisterConverters(new[] { new DynamicJsonConverter() });
                dynamic obj = serializer.Deserialize(result, typeof(object));

                var numbers = new List<double>();
                var list = new List<string>();
                
                for (int i = 0; i < 4; i++)
			    {
                    numbers.Add(double.Parse(obj[0].boundingbox[i]));
			    }

                //numbers.Add((double)obj.results[0].geometry.bounds.northeast.lat);
                //numbers.Add((double)obj.results[0].geometry.bounds.northeast.lng);
                //numbers.Add((double)obj.results[0].geometry.bounds.southwest.lat);
                //numbers.Add((double)obj.results[0].geometry.bounds.southwest.lng);

                Extents box = null;
                if (numbers.Count == 4)<commit_msg>Demo app: invariant culture to parse strings returned by geolocation.<commit_after>﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Text;
using System.Web.Script.Serialization;
using MapWinGIS;
using System.Globalization;

namespace MWLite.Core.GeoLocation
{
    public static class GeoLocationHelper
    {
        private static string _license = "";

        public static string License
        {
            get { return _license; }
        }

        public static Extents FindLocation(string query)
        {
            string json = ResquestLocation(query);
            return ParseResult(json);
        }

        private static string ResquestLocation(string query)
        {
            //var url = "https://maps.googleapis.com/maps/api/geocode/json?address=" + query;
            var url = string.Format("http://nominatim.openstreetmap.org/search/{0}?format=json&limit=1", query);
            var request = (HttpWebRequest)WebRequest.Create(url);
            
            request.Referer = "mapwingis.codeplex.com";
            request.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.9.1.7) Gecko/20091221 Firefox/3.5.7";
            request.Timeout = 5000;
            request.ReadWriteTimeout = request.Timeout * 6;
            request.Accept = "*/*";
            string result = "";

            try
            {
                using (var response = request.GetResponse() as HttpWebResponse)
                {
                    if (response != null)
                    {
                        if (response.StatusCode == HttpStatusCode.OK)
                        {
                            using (Stream responseStream = response.GetResponseStream())
                            {
                                if (responseStream != null)
                                {
                                    using (var read = new StreamReader(responseStream, Encoding.UTF8))
                                    {
                                        result = read.ReadToEnd();
                                    }
                                }
                            }
                        }
                        response.Close();
                    }
                }
            }
            catch
            {
                throw new ApplicationException("Failed to run HTTP request.");
            }
            return result;
        }

        private static Extents ParseResult(string result)
        {
            try
            {
                var serializer = new JavaScriptSerializer();
                serializer.RegisterConverters(new[] { new DynamicJsonConverter() });
                dynamic obj = serializer.Deserialize(result, typeof(object));

                var numbers = new List<double>();
                var list = new List<string>();
                
                for (int i = 0; i < 4; i++)
			    {
                    numbers.Add(double.Parse(obj[0].boundingbox[i], CultureInfo.InvariantCulture));
			    }

                Extents box = null;
                if (numbers.Count == 4)
{'fraction_non_alphanumeric': 0.09478381289429143, 'fraction_numerical': 0.010155408524388367, 'mean_word_length': 1.2530329289428077, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 7, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 10, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3011350', 'n_tokens_mistral': 1631, 'n_tokens_neox': 1499, 'n_words': 327}