private void Level3UsingMatchedName(CodedLocation location, bool useCache) { if (string.IsNullOrEmpty(location.Name3)) { return; } // get the matched name and try again Level3Match match; if (useCache) { match = matchedNamesCache.Level3Match( location.GeoCode1.Name, location.GeoCode2.Name, location.Name3); } else { match = SavedLevel3Match(location); } if (match != null) { // try with the matched name location.Name3 = match.Level3; Level3UsingGazetteer(location); } }
public void GetLocationCodes_Level1Correct_Level1CodeAdded() { // Arrange // gazetteer data - contains codes for names1 and names2 GazetteerRecords gazetteerRecords = GazetteerTestData(); // input data - no level 3 supplied string[] inputNames = { "P1", null, null }; Location location = new Location( inputNames[0]); // no saved matches MatchProviderStub matchProviderStub = MatchProviderStubEmpty(inputNames); Coder coder = new Coder( gazetteerRecords.GadmList(), matchProviderStub.MatchProvider()); // Act CodedLocation codedLocation = coder.GetCodes(location); // Assert // level 1 code only added Assert.AreEqual(codes1[0], codedLocation.GeoCode1.Code); Assert.AreEqual(names1[0], codedLocation.GeoCode1.Name); Assert.AreEqual(null, codedLocation.GeoCode2); Assert.AreEqual(null, codedLocation.GeoCode3); }
private CodedLocation FindCodes( Coder coder, DataRow dataRow, bool useCache) { //create location, use the original name Location location = new Location(); location.Name1 = dataRow[ColumnHeaders.Level1].ToString(); // level 2 is optional if (!string.IsNullOrEmpty(ColumnHeaders.Level2)) { location.Name2 = dataRow[ColumnHeaders.Level2].ToString(); } // level 3 is optional if (!string.IsNullOrEmpty(ColumnHeaders.Level3)) { location.Name3 = dataRow[ColumnHeaders.Level3].ToString(); } // get codes CodedLocation codedLocation = coder.GetCodes(location, useCache); return(codedLocation); }
public void GetCodes_Leve1And3CorrectAndLevel2HasSavedMatch_AllCodesAdded() { // Arrange // gazetteer data - contains codes for names1 and names2 GazetteerRecords gazetteerRecords = GazetteerTestData(); // input data - level 2 miss-spelt string[] inputNames = { "P1", "T1x", "V1" }; Location location = new Location( inputNames[0], inputNames[1], inputNames[2]); //saved matches MatchProviderStub matchProviderStub = MatchProviderStubLevel2(inputNames); Coder coder = new Coder( gazetteerRecords.GadmList(), matchProviderStub.MatchProvider()); // Act CodedLocation codedLocation = coder.GetCodes(location); // Assert // code 1, 2 and 3 codes added Assert.AreEqual(codes1[0], codedLocation.GeoCode1.Code); Assert.AreEqual(names1[0], codedLocation.GeoCode1.Name); Assert.AreEqual(codes1[1], codedLocation.GeoCode2.Code); Assert.AreEqual(names1[1], codedLocation.GeoCode2.Name); Assert.AreEqual(codes1[2], codedLocation.GeoCode3.Code); Assert.AreEqual(names1[2], codedLocation.GeoCode3.Name); }
private void AddUsedMatchNames(CodedLocation codedLocation) { InputColumnHeaders columnHeaders = geoCoder.MatchedNameColumnHeaders(); // add the actual name used to get the code if different to that on the input if (codedLocation.IsName1Different()) { dataGridView1.Rows[selectedRowIndex].Cells[columnHeaders.Level1] .Value = codedLocation.GeoCode1.Name; } if (codedLocation.IsName2Different()) { dataGridView1.Rows[selectedRowIndex].Cells[columnHeaders.Level2] .Value = codedLocation.GeoCode2.Name; } if (codedLocation.IsName3Different()) { dataGridView1.Rows[selectedRowIndex].Cells[columnHeaders.Level3] .Value = codedLocation.GeoCode3.Name; } }
private void AddCodes(CodedLocation codedLocation) { // add the codes to the input data InputColumnHeaders columnHeaders = geoCoder.LocationCodeColumnHeaders(); if (codedLocation.GeoCode1 != null) { dataGridView1.Rows[selectedRowIndex].Cells[columnHeaders.Level1] .Value = codedLocation.GeoCode1.Code; } if (codedLocation.GeoCode2 != null) { dataGridView1.Rows[selectedRowIndex].Cells[columnHeaders.Level2] .Value = codedLocation.GeoCode2.Code; } if (codedLocation.GeoCode3 != null) { dataGridView1.Rows[selectedRowIndex].Cells[columnHeaders.Level3] .Value = codedLocation.GeoCode3.Code; } // add the names used to generate those codes as information for the user. AddUsedMatchNames(codedLocation); }
GetLocationCodes_GazContainsDuplicateCombinedLevel2And3_CorrectCodesAdded() { // Arrange // gazetteer data GazetteerRecords gazetteerRecords = GazetteerTestData(); // input data - string[] inputNames = { "P1", "T1V", "1" }; // names 4 Location location = new Location( inputNames[0], inputNames[1], inputNames[2]); // no saved matches MatchProviderStub matchProviderStub = MatchProviderStubEmpty(inputNames); Coder coder = new Coder( gazetteerRecords.GadmList(), matchProviderStub.MatchProvider()); // Act CodedLocation codedLocation = coder.GetCodes(location); // Assert // correct codes added (codes 4) Assert.AreEqual(codes4[0], codedLocation.GeoCode1.Code); Assert.AreEqual(codes4[1], codedLocation.GeoCode2.Code); Assert.AreEqual(codes4[2], codedLocation.GeoCode3.Code); }
public void GetLocationCodes_Level1Incorrect_NoCodesAdded() { // Arrange // gazetteer data - contains codes for names1 and names2 GazetteerRecords gazetteerRecords = GazetteerTestData(); // input data - level 2 miss-spelt string[] inputNames = { "P1x", "T1", "V1" }; Location location = new Location( inputNames[0], inputNames[1], inputNames[2]); // no saved matches MatchProviderStub matchProviderStub = MatchProviderStubEmpty(inputNames); Coder coder = new Coder( gazetteerRecords.GadmList(), matchProviderStub.MatchProvider()); // Act CodedLocation codedLocation = coder.GetCodes(location); // Assert // no codes added Assert.AreEqual(null, codedLocation.GeoCode1); Assert.AreEqual(null, codedLocation.GeoCode2); Assert.AreEqual(null, codedLocation.GeoCode3); }
GetLocationCodes_Leve1And2CorrectAndLevel3Incorrect_Level1And2CodeAddedOnly() { // Arrange // gazetteer data - contains codes for names1 and names2 GazetteerRecords gazetteerRecords = GazetteerTestData(); // input data - level 3 miss-spelt string[] inputNames = { "P1", "T1", "V1x" }; Location location = new Location( inputNames[0], inputNames[1], inputNames[2]); // no saved matches MatchProviderStub matchProviderStub = MatchProviderStubEmpty(inputNames); Coder coder = new Coder( gazetteerRecords.GadmList(), matchProviderStub.MatchProvider()); // Act CodedLocation codedLocation = coder.GetCodes(location); // Assert // code 1 and 2 only are added, no level 3 code added Assert.AreEqual(codes1[0], codedLocation.GeoCode1.Code); Assert.AreEqual(names1[0], codedLocation.GeoCode1.Name); Assert.AreEqual(codes1[1], codedLocation.GeoCode2.Code); Assert.AreEqual(names1[1], codedLocation.GeoCode2.Name); Assert.AreEqual(null, codedLocation.GeoCode3); }
private void GetLevel3Code(CodedLocation location, bool useCache) { Level3UsingGazetteer(location); if (location.GeoCode3 == null) { Level3UsingMatchedName(location, useCache); } }
private void Level1UsingGazetteer(CodedLocation location) { if (string.IsNullOrEmpty(location.Name1)) { return; } location.GeoCode1 = dictionary.GetLevel1Code( location.Name1); }
private void Level2UsingGazetteer(CodedLocation location) { if (string.IsNullOrEmpty(location.Name2)) { return; } location.GeoCode2 = dictionary.GetLevel2Code( location.GeoCode1.Name, location.Name2); }
/// <summary> /// Adds the codes and the names used to find those codes, to the input data. /// </summary> /// <param name="coder">The location codes.</param> public void CodeAll(Coder coder) { coder.RefreshMatchedNamesCache(); foreach (DataRow dataRow in Data.Rows) { CodedLocation codedLocation = FindCodes( coder, dataRow, UseMatchedNamesCache); ClearExistingCodes(dataRow); AddCodes(codedLocation, dataRow); } }
/// <summary> /// Gets the location codes. /// </summary> /// <param name="location">The location.</param> /// <param name="useMatchedNamesCache">If true, uses the cache of matched names</param> /// <returns>The location with the codes</returns> public CodedLocation GetCodes( Location location, bool useMatchedNamesCache = false) { CodedLocation codedLocation = new CodedLocation(location); GetLevel1Code(codedLocation, useMatchedNamesCache); if (codedLocation.GeoCode1 != null) { GetLevel2Code(codedLocation, useMatchedNamesCache); if (codedLocation.GeoCode2 != null) { GetLevel3Code(codedLocation, useMatchedNamesCache); } } return(codedLocation); }
private Level1Match SavedLevel1Match(CodedLocation location) { IEnumerable <Level1Match> matches = matchProvider.GetMatches(location.Name1).ToList(); int count = matches.Count(); if (count > 1) { // there must only be a max of one saved match for any given input. var msg = string.Format( "[{0}] matched names found for the input [{1}]", count, location.Name1); throw new InvalidOperationException(msg); } Level1Match match = matches.FirstOrDefault(); return(match); }
private static void AddCodes(CodedLocation codedLocation, DataRow dataRow) { if (codedLocation.GeoCode1 != null) { dataRow[Level1CodeColumnName] = codedLocation.GeoCode1.Code; } if (codedLocation.GeoCode2 != null) { dataRow[Level2CodeColumnName] = codedLocation.GeoCode2.Code; } if (codedLocation.GeoCode3 != null) { dataRow[Level3CodeColumnName] = codedLocation.GeoCode3.Code; } // add the names used to generate those codes as information for the user. AddUsedMatchNames(codedLocation, dataRow); }
private static void AddUsedMatchNames( CodedLocation codedLocation, DataRow dataRow) { // add the actual name used to get the code if different to that on the input if (codedLocation.IsName1Different()) { dataRow[Level1MatchedColumnName] = codedLocation.GeoCode1.Name; } if (codedLocation.IsName2Different()) { dataRow[Level2MatchedColumnName] = codedLocation.GeoCode2.Name; } if (codedLocation.IsName3Different()) { dataRow[Level3MatchedColumnName] = codedLocation.GeoCode3.Name; } }
private void UpdateRow() { string originalLevel1 = txtLevel1Original.Text; string originalLevel2 = txtLevel2Original.Text; string originalLevel3 = txtLevel3Original.Text; Location location2 = new Location( originalLevel1, originalLevel2, originalLevel3); CodedLocation codedLocation = geoCoder.AddLocationCodes(location2); ClearExistingCodes(); AddCodes(codedLocation); int rowCount = dataGridView1.RowCount; DisplayRecords(); if (dataGridView1.RowCount == rowCount) { SelectNextRow(); } }
public void GetLocationCodes_Level1GazetteerAltName_AllCodesAdded() { // Arrange // gazetteer data - contains codes for names1 and names2 and // an alternate name for name1 string[] altNames = { "P1A", null, null }; GazetteerRecords gazetteerRecords = GazetteerTestData(altNames); // gazetteerRecords.AddLine(names2, codes2); // input data - Level1 contains alt spelling, the rest are spelt correctly string[] inputNames = { "P1A", "T1", "V1" }; Location location = new Location( inputNames[0], inputNames[1], inputNames[2]); // no saved matches MatchProviderStub matchProviderStub = MatchProviderStubEmpty(inputNames); Coder coder = new Coder( gazetteerRecords.GadmList(), matchProviderStub.MatchProvider()); // Act CodedLocation codedLocation = coder.GetCodes(location); // Assert // code 1, 2 and 3 codes added Assert.AreEqual(codes1[0], codedLocation.GeoCode1.Code); Assert.AreEqual(names1[0], codedLocation.GeoCode1.Name); Assert.AreEqual(codes1[1], codedLocation.GeoCode2.Code); Assert.AreEqual(names1[1], codedLocation.GeoCode2.Name); Assert.AreEqual(codes1[2], codedLocation.GeoCode3.Code); Assert.AreEqual(names1[2], codedLocation.GeoCode3.Name); }