Пример #1
0
        public void DoOneZipCode(string zipCode)
        {
            // Get all zip rows from ZipStreetsDownloaded
            var    isSingle           = false;
            var    found              = false;
            string singleLdsStateCode = null;
            var    streetsTable       = ZipStreetsDownloaded.GetLookupDataByZipCode(zipCode, 0);

            if (streetsTable.Count > 0)
            {
                found = true;

                // Fetch and summarize the USZD data
                var ldsDictionary = new Dictionary <LdsInfo, object>();
// ReSharper disable InconsistentNaming
                var zip4s = CreateZipPlus4List(streetsTable);
// ReSharper restore InconsistentNaming
                var uszdTable = Uszd.GetDataByZip4List(zipCode, zip4s, 0);
                foreach (var row in uszdTable)
                {
                    var stateCode = StateCache.StateCodeFromLdsStateCode(row.LdsStateCode);
                    singleLdsStateCode = row.LdsStateCode;
                    var ldsInfo = new LdsInfo(stateCode, row.Congress, row.StateSenate,
                                              row.StateHouse, row.County);
                    ldsDictionary[ldsInfo] = null;
                }
                if (ldsDictionary.Count == 1)
                {
                    isSingle = true;
                    var singleLdsInfo = ldsDictionary.Keys.Single();
                    //List<string> fields = new List<string>();
                    var csvWriter = new SimpleCsvWriter();
                    //AddField(fields, zipCode);
                    //AddField(fields, singleLdsInfo.Congress.Substring(1));
                    //AddField(fields, singleLdsInfo.StateSenate);
                    //AddField(fields, singleLdsInfo.StateHouse);
                    //AddField(fields, singleLdsStateCode);
                    //AddField(fields, singleLdsInfo.County);
                    //AddField(fields, singleLdsInfo.StateCode);
                    //TextWriter.WriteLine(string.Join(",", fields));
                    csvWriter.AddField(zipCode);
                    csvWriter.AddField(singleLdsInfo.Congress.Substring(1));
                    csvWriter.AddField(singleLdsInfo.StateSenate);
                    csvWriter.AddField(singleLdsInfo.StateHouse);
                    csvWriter.AddField(singleLdsStateCode);
                    csvWriter.AddField(singleLdsInfo.County);
                    csvWriter.AddField(singleLdsInfo.StateCode);
                    csvWriter.Write(_TextWriter);
                }
            }

            if (found)
            {
                Report("{0}: {1}", zipCode, isSingle ? "Single" : "Multiple");
            }
        }
Пример #2
0
        private static Dictionary <string, LdsInfo> GetLdsInfoDictionary(string zip5,
                                                                         IEnumerable <string> zip4List)
        {
            var ldsDictionary = new Dictionary <string, LdsInfo>();
            var table         = Uszd.GetDataByZip4List(zip5, zip4List, 0);

            foreach (var row in table)
            {
                var stateCode = StateCache.StateCodeFromLdsStateCode(row.LdsStateCode);
                ldsDictionary[row.Zip4] = new LdsInfo(stateCode, row.Congress,
                                                      row.StateSenate, row.StateHouse, row.County);
            }
            return(ldsDictionary);
        }
Пример #3
0
        public bool DoOneZipCode(string zipCode)
        {
            // Get all zip rows from ZipStreetsDownloaded
            var    isSingle           = false;
            var    found              = false;
            string singleLdsStateCode = null;
            var    streetsTable       = ZipStreetsDownloaded.GetLookupDataByZipCode(zipCode, 0);

            if (streetsTable.Count > 0)
            {
                found = true;

                // Fetch and summarize the USZD data
                var ldsDictionary = new Dictionary <LdsInfo, object>();
                var zip4S         = CreateZipPlus4List(streetsTable);
                var uszdTable     = Uszd.GetDataByZip4List(zipCode, zip4S, 0);
                foreach (var row in uszdTable)
                {
                    var stateCode = StateCache.StateCodeFromLdsStateCode(row.LdsStateCode);
                    singleLdsStateCode = row.LdsStateCode;
                    var ldsInfo = new LdsInfo(stateCode, row.Congress, row.StateSenate,
                                              row.StateHouse, row.County);
                    ldsDictionary[ldsInfo] = null;
                }
                if (ldsDictionary.Count == 1)
                {
                    isSingle = true;
                    var singleLdsInfo = ldsDictionary.Keys.Single();
                    if (!_SuppressUpdate)
                    {
                        ZipSingleUszd.Insert(zipCode: zipCode,
                                             congress: singleLdsInfo.Congress.Substring(1),
                                             // LdsInfo padds it to 3
                                             stateSenate: singleLdsInfo.StateSenate,
                                             stateHouse: singleLdsInfo.StateHouse,
                                             ldsStateCode: singleLdsStateCode, county: singleLdsInfo.County,
                                             stateCode: singleLdsInfo.StateCode);
                    }
                }
            }

            if (found)
            {
                Report("{0}: {1}", zipCode, isSingle ? "Single" : "Multiple");
            }

            return(isSingle);
        }
Пример #4
0
        private void ApplyLdsInfo(IList <StreetAnalysisData> dataList)
        {
            if ((dataList == null) || (dataList.Count == 0))
            {
                return;
            }

            // This dictionary caches the Zip+4 values
            var ldsDictionary = new Dictionary <string, LdsInfo>();

            // In the first pass we resolve all range values and collect the
            // single values;
            foreach (var data in dataList) // We skip all PO BOXes here. They are handled separately
            {
                if (data.StreetName != PoBox)
                {
                    try
                    {
                        ValidatePlus4s(data);

                        if (data.Plus4Low == data.Plus4High)
                        {
                            ldsDictionary[data.Plus4Low] = null;
                        }
                        else
                        {
                            var group = new Dictionary <LdsInfo, object>();

                            var uszdTable = Uszd.GetDataByZipPlus4Range(data.ZipCode,
                                                                        data.Plus4Low, data.Plus4High, 0);
                            foreach (var uszdRow in uszdTable)
                            {
                                var stateCode =
                                    StateCache.StateCodeFromLdsStateCode(uszdRow.LdsStateCode);
                                group[
                                    new LdsInfo(stateCode, uszdRow.Congress, uszdRow.StateSenate,
                                                uszdRow.StateHouse, uszdRow.County)] = null;
                            }

                            if (group.Count == 0)
                            {
                                _Statistics.MissingZip4Range++;
                                throw new VoteException(
                                          "*{0} missing Lds info range {1}-{2} {1}-{3}", data.UpdateKey,
                                          data.ZipCode, data.Plus4Low, data.Plus4High);
                            }

                            if (group.Count > 1)
                            {
                                _Statistics.MultipleZip4Range++;
                                if (data.StreetName == "PO BOX")
                                {
                                    throw new VoteException(
                                              "{0} multiple PO BOX range {1}-{2} {1}-{3}", data.UpdateKey,
                                              data.ZipCode, data.Plus4Low, data.Plus4High);
                                }
                                throw new VoteException(
                                          "{0} multiple Lds info range {1}-{2} {1}-{3}", data.UpdateKey,
                                          data.ZipCode, data.Plus4Low, data.Plus4High);
                            }

                            data.LdsInfo = group.Keys.Single();
                        }
                    }
                    catch (VoteException ex)
                    {
                        if (!ex.Message.StartsWith("*"))
                        {
                            ReportError(ex.Message);
                        }
                        data.SetMissingLdsInfo();
                    }
                }
            }

            // Now we get all the ldsInfos
            var table = Uszd.GetDataByZip4List(dataList[0].ZipCode, ldsDictionary.Keys, 0);

            foreach (var row in table)
            {
                var stateCode = StateCache.StateCodeFromLdsStateCode(row.LdsStateCode);
                ldsDictionary[row.Zip4] = new LdsInfo(stateCode, row.Congress,
                                                      row.StateSenate, row.StateHouse, row.County);
            }

            // Finally, we apply the ldsInfos to the non-ranged data items
            foreach (var data in dataList)
            {
                if ((data.StreetName != PoBox) && (data.Plus4Low == data.Plus4High))
                {
                    LdsInfo ldsInfo;
                    if (ldsDictionary.TryGetValue(data.Plus4Low, out ldsInfo) &&
                        (ldsInfo != null))
                    {
                        data.LdsInfo = ldsInfo;
                    }
                    else
                    {
                        _Statistics.MissingZip4++;
                        //LogMissingZipPlus4(data.ZipCode, data.Plus4Low);
                        //AppendErrorsText("{0} missing Lds info {1}-{2}",
                        //  data.UpdateKey, data.ZipCode, data.Plus4Low);
                        data.SetMissingLdsInfo();
                    }
                }
            }
        }
Пример #5
0
        private void FillInLegislativeDistricts()
        {
            AppendStatusText("Starting...");

            int  rowCount            = 0;
            int  fromUszd            = 0;
            int  successfulLookups   = 0;
            int  unsuccessfulLookups = 0;
            bool done = false;

            while (!done)
            {
                using (var reader = Addresses.GetDataReaderForLegislativeCoding(
                           FillInLegislativeDistrictsOnlyEmailsCheckBox.Checked,
                           FillInLegislativeDistrictsOnlyUncodedCheckBox.Checked, 0))
                {
                    try
                    {
                        while (reader.Read())
                        {
                            rowCount++;
                            string congressionalDistrict = string.Empty.ZeroPad(
                                Addresses.CongressionalDistrictMaxLength);
                            string stateSenateDistrict = string.Empty.ZeroPad(
                                Addresses.StateSenateDistrictMaxLength);
                            string stateHouseDistrict = string.Empty.ZeroPad(
                                Addresses.StateHouseDistrictMaxLength);
                            string county = string.Empty.ZeroPad(
                                Addresses.CountyMaxLength);
                            bool lookup = true;
                            if (reader.Zip5.Length == 5 && reader.Zip4.Length == 4)
                            {
                                // use USZD
                                var uszdTable = Uszd.GetDataByZip5Zip4(reader.Zip5, reader.Zip4);
                                if (uszdTable.Count > 0)
                                {
                                    var uszdRow = uszdTable[0];
                                    congressionalDistrict = uszdRow.Congress;
                                    stateSenateDistrict   = uszdRow.StateSenate;
                                    stateHouseDistrict    = uszdRow.StateHouse;
                                    county = uszdRow.County;
                                    lookup = false;
                                    fromUszd++;
                                }
                            }
                            if (lookup)
                            {
                                var result = AddressFinder.Find(reader.Address + ' ' +
                                                                reader.City + ' ' + reader.StateCode, null);
                                if (result.Success)
                                {
                                    congressionalDistrict = result.Congress;
                                    stateSenateDistrict   = result.StateSenate;
                                    stateHouseDistrict    = result.StateHouse;
                                    county = result.County;
                                    successfulLookups++;
                                }
                                else
                                {
                                    unsuccessfulLookups++;
                                }
                            }
                            // congresionalDistrictFixup
                            if (congressionalDistrict.Length == 3 && congressionalDistrict[0] == '0')
                            {
                                congressionalDistrict = congressionalDistrict.Substring(1);
                            }
                            var addressesTable = Addresses.GetDataById(reader.Id);
                            addressesTable[0].CongressionalDistrict = congressionalDistrict;
                            addressesTable[0].StateSenateDistrict   = stateSenateDistrict;
                            addressesTable[0].StateHouseDistrict    = stateHouseDistrict;
                            addressesTable[0].County             = county;
                            addressesTable[0].DistrictLookupDate = DateTime.Now.Date;
                            Addresses.UpdateTable(addressesTable);
                            if (rowCount % 1000 == 0)
                            {
                                AppendStatusText("{0} rows processed", rowCount);
                            }
                        }
                        done = true;
                    }
                    catch { }
                }
            }

            AppendStatusText("{0} rows with email and missing legislative districts found", rowCount);
            AppendStatusText("{0} filled in from USZD", fromUszd);
            AppendStatusText("{0} successful lookups", successfulLookups);
            AppendStatusText("{0} unsuccessful lookups", unsuccessfulLookups);
            AppendStatusText("Completed.");
        }