Example #1
0
    private void addItemToList(clsZipCode zipCode)
    {
        int    padding = 7;
        string result  = zipCode.ZipCode.PadRight(padding)
                         + zipCode.State.PadRight(padding)
                         + zipCode.City.PadRight(padding * 3)
                         + Math.Round(zipCode.Latitude, 2).ToString().PadRight(padding * 2)
                         + Math.Round(zipCode.Longitude, 2).ToString();

        lstDisplayEntries.Items.Add(result);
    }
Example #2
0
    private clsZipCode getZipCodeInformationAtRecordNumber(string zipCodeToSearch)
    {
        string pathFileNameBinary = Path.Combine(Application.StartupPath,
                                                 "ZipCodes_1990Census_sortedAscending.bin");
        const int recordLength = 109;

        clsZipCode zipCode = new clsZipCode();

        using (FileStream fs = new FileStream(pathFileNameBinary, FileMode.Open))
            using (BinaryReader br = new BinaryReader(fs))
            {
                long numberOfRecords = br.BaseStream.Length / recordLength;

                long start  = 0;
                long middle = numberOfRecords / 2;
                long end    = numberOfRecords;

                bool found = false;
                while (start <= end)
                {
                    long recordPos = middle * recordLength;
                    br.BaseStream.Seek(recordPos, SeekOrigin.Begin);
                    zipCode.BinaryDeserialize(br);

                    if (String.Compare(zipCode.ZipCode, zipCodeToSearch) == 0)
                    {
                        found = true;
                        break;
                    }
                    else if (String.Compare(zipCode.ZipCode, zipCodeToSearch) < 0)
                    {
                        start  = middle + 1;
                        middle = (end + start) / 2;
                    }
                    else
                    {
                        end    = middle - 1;
                        middle = (start + end) / 2;
                    }
                }

                if (found == false)
                {
                    showMessage("The Zip Code " + zipCodeToSearch + " was not found.");
                    return(null);
                }
            }
        return(zipCode);
    }
Example #3
0
    private void btnCalculate_Click(object sender, EventArgs e)
    {
        string firstZipCode  = txtFirstZipCode.Text.Trim();
        string secondZipCode = txtSecondZipCode.Text.Trim();

        if (firstZipCode.Length == 0 || secondZipCode.Length == 0 || firstZipCode == secondZipCode)
        {
            showMessage("Enter two different Zip Codes");
            return;
        }

        clsZipCode zipCode1 = getZipCodeInformationAtRecordNumber(firstZipCode);

        if (zipCode1 == null)
        {
            return;
        }

        txtFirstState.Text = zipCode1.State;
        txtFirstCity.Text  = zipCode1.City;

        clsZipCode zipCode2 = getZipCodeInformationAtRecordNumber(secondZipCode);

        if (zipCode2 == null)
        {
            return;
        }
        txtSecondCity.Text  = zipCode2.City;
        txtSecondState.Text = zipCode2.State;

        // Calculating the distance between the two zip codes

        double radiusOfEarth = 6371.00;
        double dLat          = (zipCode2.Latitude - zipCode1.Latitude) * Math.PI / 180;
        double dLon          = (zipCode2.Longitude - zipCode1.Longitude) * Math.PI / 180;

        double a = Math.Sin(dLat / 2) * Math.Sin(dLat / 2)
                   + Math.Cos(zipCode1.Latitude * Math.PI / 180) * Math.Cos(zipCode2.Latitude * Math.PI / 180)
                   * Math.Sin(dLon / 2) * Math.Sin(dLon / 2);
        double c               = 2 * Math.Atan2(Math.Sqrt(a), Math.Sqrt(1 - a));
        double distanceInKm    = radiusOfEarth * c;
        double distanceInMiles = 0.621371 * distanceInKm;

        txtDistanceInKM.Text    = Math.Round(distanceInKm, 2).ToString();
        txtDistanceInMiles.Text = Math.Round(distanceInMiles, 2).ToString();
    }
Example #4
0
    public frmMain()
    {
        InitializeComponent();
        string pathFileName = Path.Combine(Application.StartupPath, "zipcodes_1990Census.txt");
        string input;

        using (StreamReader sr = new StreamReader(pathFileName))
        {
            input = sr.ReadLine();
            while (input != null)
            {
                clsZipCode zipCode = new clsZipCode();
                zipCode.Deserialize(input);
                mZipCodeList.Add(zipCode);
                input = sr.ReadLine();
            }
            sr.Close();
        }

        radAscending.Checked = true;
    }
Example #5
0
    private void btnConvertFile_Click(object sender, EventArgs e)
    {
        string pathFileNameText = Path.Combine(Application.StartupPath,
                                               "ZipCodes_1990Census_sortedAscending.txt");
        string input;

        if (File.Exists(pathFileNameText) == false)
        {
            MessageBox.Show(pathFileNameText + " does not exist",
                            Text, MessageBoxButtons.OK, MessageBoxIcon.Information);
            return;
        }

        using (StreamReader sr = new StreamReader(pathFileNameText))
        {
            input = sr.ReadLine();
            while (input != null)
            {
                clsZipCode zipCode = new clsZipCode();
                zipCode.Deserialize(input);
                mZipCodeList.Add(zipCode);
                input = sr.ReadLine();
            }
            sr.Close();
        }

        string pathFileNameBinary = Path.Combine(Application.StartupPath, "ZipCodes_1990Census_sortedAscending.bin");

        using (FileStream fs = new FileStream(pathFileNameBinary, FileMode.Create))
            using (BinaryWriter bw = new BinaryWriter(fs))
            {
                foreach (clsZipCode zipCode in mZipCodeList)
                {
                    zipCode.BinarySerialize(bw);
                }
            }
        showMessage("A binary file of " + mZipCodeList.Count + " has been created");
    }
Example #6
0
    private void loadListBox()
    {
        string     formattedOutput;
        clsZipCode currentZipCode;
        string     searchFor;

        mZipCodeResultsAL.Clear();

        searchFor = txtSearchFor.Text.ToUpper();

        lstZipCodes.Items.Clear();
        string sql = "SELECT * FROM ZipCodes ORDER BY City, State, ZipCode";

        if (searchFor.Length != 0)
        {
            sql = "SELECT * FROM ZipCodes "
                  + "WHERE ZipCode = " + toSql(searchFor) + " OR City LIKE " + toSql(searchFor)
                  + "ORDER BY City, State, ZipCode";
        }

        using (OleDbConnection db = new OleDbConnection())
        {
            string fullFileName;
            string connectionString;

            fullFileName     = Path.Combine(Application.StartupPath, mDatabaseFileName);
            connectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + fullFileName;

            db.ConnectionString = connectionString;
            db.Open();
            using (OleDbCommand cmd = new OleDbCommand(sql, db))
                using (OleDbDataReader rdr = cmd.ExecuteReader())
                {
                    while (rdr.Read() == true)
                    {
                        clsZipCode zipCode = new clsZipCode();
                        zipCode.ZipCode     = (string)rdr["ZipCode"];
                        zipCode.State       = (string)rdr["State"];
                        zipCode.City        = (string)rdr["City"];
                        zipCode.Longitude   = (double)rdr["Longitude"];
                        zipCode.Latitude    = (double)rdr["Latitude"];
                        zipCode.LastUpdated = (DateTime)rdr["LastUpdated"];
                        zipCode.Active      = (bool)rdr["Active"];

                        mZipCodeResultsAL.Add(zipCode);
                    }
                }
            db.Close();
        }

        for (int i = 0; i < mZipCodeResultsAL.Count; i++)
        {
            if (i == 100)
            {
                messageBoxOK("Displaying the first 100 results.");
                break;
            }
            currentZipCode   = (clsZipCode)mZipCodeResultsAL[i];
            formattedOutput  = currentZipCode.ZipCode.PadRight(10);
            formattedOutput += currentZipCode.City.PadRight(25);
            formattedOutput += currentZipCode.State.PadRight(5);
            formattedOutput += currentZipCode.Longitude.ToString("##0.000").PadLeft(7);
            formattedOutput += currentZipCode.Latitude.ToString("##0.000").PadLeft(9);

            lstZipCodes.Items.Add(formattedOutput);
        }
    }