AddressLookupStore.cs 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. using InABox.Core;
  2. using NPOI.HSSF.Record;
  3. using NPOI.OpenXmlFormats.Spreadsheet;
  4. namespace InABox.Database.Stores;
  5. public class AddressLookupStore : Store<AddressLookup>
  6. {
  7. private static List<AddressLookup> _cache= new();
  8. private static IQueryProviderFactory? _queryProviderFactory = null;
  9. public override void Init()
  10. {
  11. base.Init();
  12. // Load the cache file into memory
  13. string cachefile = Path.Combine(CoreUtils.GetPath(), "GNAF_CORE.psv");
  14. if (File.Exists(cachefile))
  15. {
  16. DateTime start = DateTime.Now;
  17. Logger.Send(LogType.Information,"",$"Loading Address Lookup File: {cachefile}");
  18. using (StreamReader sr = new(cachefile))
  19. {
  20. var headers = sr.ReadLine().Split('|').ToList();
  21. int streetfield = headers.IndexOf("ADDRESS_LABEL");
  22. int statefield = headers.IndexOf("STATE");
  23. int latfield = headers.IndexOf("LATITUDE");
  24. int lngfield = headers.IndexOf("LONGITUDE");
  25. while (!sr.EndOfStream)
  26. {
  27. var line = sr.ReadLine().Split('|').ToArray();
  28. if (string.Equals(line[statefield], "WA"))
  29. {
  30. _cache.Add(new AddressLookup()
  31. {
  32. Address = line[streetfield],
  33. Latitude = double.Parse(line[latfield]),
  34. Longitude = double.Parse(line[lngfield])
  35. });
  36. }
  37. }
  38. }
  39. Logger.Send(LogType.Information,"",$"Found {_cache.Count} addresses in {(DateTime.Now - start):g}");
  40. }
  41. else
  42. Logger.Send(LogType.Information,"",$"Address Lookup File: {cachefile} not found!");
  43. }
  44. protected override CoreTable OnQuery(Filter<AddressLookup>? filter, Columns<AddressLookup>? columns, SortOrder<AddressLookup>? sort, CoreRange? range)
  45. {
  46. _queryProviderFactory ??= this.GetQueryProviderFactory();
  47. var result = new CoreTable();
  48. var cols = columns ?? Columns.All<AddressLookup>();
  49. result.Columns.AddRange(cols.Select(x => new CoreColumn(x.Property)));
  50. foreach (var lookup in _cache)
  51. {
  52. if (filter == null || filter.Match(lookup, _queryProviderFactory))
  53. {
  54. var row = result.NewRow();
  55. result.LoadRow(lookup);
  56. result.Rows.Add(row);
  57. }
  58. }
  59. return result;
  60. }
  61. }