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); }
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); }
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(); } } } }
public static TigerLookupData Lookup(double latitude, double longitude) { var result = new TigerLookupData(); lock (ShapeFile.Sync) { var countyIndex = CountyShapeFile.GetShapeIndexContainingPoint(new PointD(longitude, latitude), 0.0); if (countyIndex < 0) { return(result); } var countyValues = CountyShapeFile.GetAttributeFieldValues(countyIndex); result.StateCode = StateCache.StateCodeFromLdsStateCode(countyValues[0].Trim()); result.County = countyValues[1].Trim(); var congressIndex = CongressShapeFile.GetShapeIndexContainingPoint(new PointD(longitude, latitude), 0.0); if (congressIndex >= 0) { var congressValues = CongressShapeFile.GetAttributeFieldValues(congressIndex); result.Congress = Translate(TableTypeCongressionalDistrict, result.StateCode, congressValues[1].Trim()); } var upperIndex = UpperShapeFile.GetShapeIndexContainingPoint(new PointD(longitude, latitude), 0.0); if (upperIndex >= 0) { var upperValues = UpperShapeFile.GetAttributeFieldValues(upperIndex); result.Upper = Translate(TableTypeStateSenate, result.StateCode, upperValues[1].Trim()); } var lowerIndex = LowerShapeFile.GetShapeIndexContainingPoint(new PointD(longitude, latitude), 0.0); if (lowerIndex >= 0) { var lowerValues = LowerShapeFile.GetAttributeFieldValues(lowerIndex); result.Lower = Translate(TableTypeStateHouse, result.StateCode, lowerValues[1].Trim()); } var districtIndex = DistrictShapeFile.GetShapeIndexContainingPoint(new PointD(longitude, latitude), 0.0); if (districtIndex >= 0) { var districtValues = DistrictShapeFile.GetAttributeFieldValues(districtIndex); result.District = districtValues[2].Trim(); } var placeIndex = PlaceShapeFile.GetShapeIndexContainingPoint(new PointD(longitude, latitude), 0.0); if (placeIndex >= 0) { var placeValues = PlaceShapeFile.GetAttributeFieldValues(placeIndex); result.Place = placeValues[1].Trim(); } var elementaryIndex = ElementaryShapeFile.GetShapeIndexContainingPoint(new PointD(longitude, latitude), 0.0); if (elementaryIndex >= 0) { var elementaryValues = ElementaryShapeFile.GetAttributeFieldValues(elementaryIndex); result.Elementary = elementaryValues[1].Trim(); } var secondaryIndex = SecondaryShapeFile.GetShapeIndexContainingPoint(new PointD(longitude, latitude), 0.0); if (secondaryIndex >= 0) { var secondaryValues = SecondaryShapeFile.GetAttributeFieldValues(secondaryIndex); result.Secondary = secondaryValues[1].Trim(); } var unifiedIndex = UnifiedShapeFile.GetShapeIndexContainingPoint(new PointD(longitude, latitude), 0.0); if (unifiedIndex >= 0) { var unifiedValues = UnifiedShapeFile.GetAttributeFieldValues(unifiedIndex); result.Unified = unifiedValues[1].Trim(); } var cityCouncilIndex = CityCouncilShapeFile.GetShapeIndexContainingPoint(new PointD(longitude, latitude), 0.0); if (cityCouncilIndex >= 0) { var cityCouncilValues = CityCouncilShapeFile.GetAttributeFieldValues(cityCouncilIndex); result.CityCouncil = cityCouncilValues[1].Trim(); } var countySupervisorsIndex = CountySupervisorsShapeFile.GetShapeIndexContainingPoint(new PointD(longitude, latitude), 0.0); if (countySupervisorsIndex >= 0) { var countySupervisorsValues = CountySupervisorsShapeFile.GetAttributeFieldValues(countySupervisorsIndex); result.CountySupervisors = countySupervisorsValues[2].Trim(); } } return(result); }