예제 #1
0
        //private Location SaveCooridnate(string postalCode )
        //{
        //    var coordinate = GetCoordinate(postalCode);

        //    if (coordinate != null && coordinate.Latitude != null && coordinate.Longitude != null)
        //        return coordinate;

        //    var locationLatLong = this.GetAll()?.FirstOrDefault(x => x.Name.EqualsIgnoreCase(postalCode) && x.Latitude != null && x.Longitude != null);

        //    if (locationLatLong != null)
        //    {
        //        //todo add as coordinate
        //        return locationLatLong;
        //    }

        //    //todo Get lat and long

        //    //then update or insert
        //    var location = this.GetAll()?.FirstOrDefault(x => x.Name.EqualsIgnoreCase(postalCode));

        //    if (location == null)
        //    {   //add  as coordinate
        //    }

        //    if (ConfigurationManager.AppSettings["google.maps.requestperday"] != null)
        //        _apiRequestsPerDay = ConfigurationManager.AppSettings["google.maps.requestperday"].ToString().ConvertTo<int>();

        //    if (ConfigurationManager.AppSettings["google.maps.requestpersecond"] != null)
        //        _apiRequestsPerSecond = ConfigurationManager.AppSettings["google.maps.requestpersecond"].ToString().ConvertTo<int>();

        //    // if (geos.Count > _apiRequestsPerDay)
        //    //      geos = geos.Take(_apiRequestsPerDay).ToList();

        //    long totalElapsedTime = 0;
        //    int millisecondCap = 1000; //or 1 second.
        //    //If we go below this time on all the requests then we'll go over the throttle limit.
        //    int minRequesTimeThreshold = millisecondCap / _apiRequestsPerSecond;
        //    Stopwatch stopwatch = new Stopwatch();

        //    int index = 1;

        //    var address = this.GetFullAddress(location);  //location.Address1 + " " + location.City + " " + location.State + " " + location.Postal;
        //                                                  //location.Name = address;


        //    //var requestUri = string.Format("https://maps.googleapis.com/maps/api/geocode/xml?key=YOURGOOGLEAPIKEY&address={0}&sensor=false", Uri.EscapeDataString(address));

        //    var requestUri = string.Format("https://maps.googleapis.com/maps/api/geocode/xml?key=YOURGOOGLEAPIKEY&address={0}&sensor=false", Uri.EscapeDataString(address));
        //    stopwatch.Restart(); // Begin timing.
        //    var request = WebRequest.Create(requestUri);
        //    var response = request.GetResponse();
        //    var xdoc = XDocument.Load(response.GetResponseStream());
        //    // var xdoc = XDocument.Parse(_googleGeoXml); //test parse
        //    var status = xdoc.Element("GeocodeResponse").Element("status");

        //    switch (status.Value)
        //    {
        //        case GoogleGeo.ResponseStatus.OK:
        //            // location.DescriptionEx = xdoc.ToString();
        //            var result = xdoc.Element("GeocodeResponse").Element("result");
        //            var locationElement = result.Element("geometry").Element("location");
        //            location.Latitude = locationElement.Element("lat").Value.ConvertTo<float>();
        //            location.Longitude = locationElement.Element("lng").Value.ConvertTo<float>();

        //            //DataQuery dqFC = new DataQuery();
        //            // dqFC.SQL = string.Format("UPDATE FireDeptIncidents SET Latitude={0}, Longitude={1} WHERE inci_id='{2}'", location.Latitude, location.Longitude, location.inci_id);
        //            // int res = fim.Update(dqFC);

        //            // SetProgressBar(index, "updated:" + address);
        //            break;

        //        case GoogleGeo.ResponseStatus.OverLimit:
        //            //SetProgressBar(-1, "Status: OverLimit");
        //            //todo log this
        //            return null;

        //        case GoogleGeo.ResponseStatus.Denied:
        //            //todo log this
        //            // SetProgressBar(-1, "Status: Denied");
        //            //SetProgressBar(0, xdoc.ToString());
        //            return null;
        //    }

        //    // Stop timing.
        //    stopwatch.Stop();
        //    long elapsedTime = stopwatch.ElapsedMilliseconds;//How long it took to get and process the response.

        //    if (elapsedTime < minRequesTimeThreshold)
        //    {
        //        //SetProgressBar(-1, "suspending for:" + (minRequesTimeThreshold - (int)elapsedTime).ToString());
        //        Thread.Sleep(minRequesTimeThreshold - (int)elapsedTime);//sleep is in milliseconds
        //        totalElapsedTime += elapsedTime;

        //        // millisecond =   .001 or 10−3 or 1 / 1000
        //        //so 1 request every 100 milliseconds
        //    }
        //}

        //public void ImportZipCodes(string pathToFile) {
        //    ZipCodes codes = LoadZipCodeCoordinates(pathToFile);

        //    foreach (int zipCode in codes.Keys)
        //    {
        //        ZipCode loc = codes[zipCode];
        //        SaveCooridnate(loc.Code.ToString(), loc.State, loc.Latitude, loc.Longitude);

        //    }
        //    return;
        //}

        //   Columns 1-2: United States Postal Service State Abbreviation
        //   Columns 3-66: Name (e.g. 35004 5-Digit ZCTA - there are no post office names)
        //   Columns 67-75: Total Population (2000)
        //   Columns 76-84: Total Housing Units (2000)
        //   Columns 85-98: Land Area (square meters) - Created for statistical purposes only.
        //   Columns 99-112: Water Area (square meters) - Created for statistical purposes only.
        //   Columns 113-124: Land Area (square miles) - Created for statistical purposes only.
        //   Columns 125-136: Water Area (square miles) - Created for statistical purposes only.
        //   Columns 137-146: Latitude (decimal degrees) First character is blank or "-" denoting North or South latitude respectively
        //   Columns 147-157: Longitude (decimal degrees) First character is blank or "-" denoting East or West longitude respectively
        private ZipCodes LoadZipCodeCoordinates(string pathToFile)
        {
            ZipCodes codes = new ZipCodes();

            string[] fileLines = File.ReadAllLines(pathToFile);

            string sep = "\t";

            foreach (string line in fileLines)
            {
                if (string.IsNullOrWhiteSpace(line))
                {
                    continue;
                }
                string code = ""; //int
                double lat  = 0;
                double lon  = 0;

                string[] tokens = line.Split(sep.ToCharArray());

                //if (!Int32.TryParse(line.Substring(2, 5), out code) ||
                //    !double.TryParse(line.Substring(136, 10), out lat) ||
                //    !double.TryParse(line.Substring(146, 10), out lon))
                //    continue;// skip lines that aren't valid
                if ( //!Int32.TryParse(tokens[0], out code) ||
                    !double.TryParse(tokens[5], out lat) ||
                    !double.TryParse(tokens[6], out lon))
                {
                    continue;// skip lines that aren't valid
                }
                if (codes.ContainsKey(code))
                {
                    continue;  // there are a few duplicates due to state boundaries,   ignore them
                }
                codes.Add(code, new ZipCode()
                {
                    //  State = line.Substring(0, 2),
                    Code      = code,
                    Latitude  = ZipCode.ToRadians(lat),
                    Longitude = ZipCode.ToRadians(lon),
                });
            }
            return(codes);
        }
예제 #2
0
        /// <summary>
        /// Looks us a zipcode for authorized users
        /// </summary>
        /// <param name="caller">The user; will verify caller is autorized</param>
        /// <param name="zipcode">The zipcode to lookup</param>
        /// <returns>City, state for specified zipcode or error description.</returns>
        public string Lookup(string caller, string zipcode)
        {
            string result;

            // check that caller is authorized
            if (IsAuthorized(caller))
            {
                // do lookup
                result = ZipCodes.ContainsKey(zipcode) ? ZipCodes[zipcode] : Resources.UNKOWN_ZIPCCODE_MSG;
            }
            else
            {
                // unauthorized user message
                result = Resources.UNAUTHORIZED_USER;
            }

            // log call
            LogResults(caller, zipcode, result);

            return(result);
        }