1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768 |
- using InABox.Core;
- using NPOI.HSSF.Record;
- using NPOI.OpenXmlFormats.Spreadsheet;
- namespace InABox.Database.Stores;
- public class AddressLookupStore : Store<AddressLookup>
- {
-
- private static List<AddressLookup> _cache= new();
- private static IQueryProviderFactory? _queryProviderFactory = null;
-
- public override void Init()
- {
- base.Init();
- // Load the cache file into memory
- string cachefile = Path.Combine(CoreUtils.GetPath(), "GNAF_CORE.psv");
- if (File.Exists(cachefile))
- {
- DateTime start = DateTime.Now;
- Logger.Send(LogType.Information,"",$"Loading Address Lookup File: {cachefile}");
- using (StreamReader sr = new(cachefile))
- {
- var headers = sr.ReadLine().Split('|').ToList();
- int streetfield = headers.IndexOf("ADDRESS_LABEL");
- int statefield = headers.IndexOf("STATE");
- int latfield = headers.IndexOf("LATITUDE");
- int lngfield = headers.IndexOf("LONGITUDE");
- while (!sr.EndOfStream)
- {
- var line = sr.ReadLine().Split('|').ToArray();
- if (string.Equals(line[statefield], "WA"))
- {
- _cache.Add(new AddressLookup()
- {
- Address = line[streetfield],
- Latitude = double.Parse(line[latfield]),
- Longitude = double.Parse(line[lngfield])
- });
-
- }
- }
- }
- Logger.Send(LogType.Information,"",$"Found {_cache.Count} addresses in {(DateTime.Now - start):g}");
- }
- else
- Logger.Send(LogType.Information,"",$"Address Lookup File: {cachefile} not found!");
- }
- protected override CoreTable OnQuery(Filter<AddressLookup>? filter, Columns<AddressLookup>? columns, SortOrder<AddressLookup>? sort, CoreRange? range)
- {
- _queryProviderFactory ??= this.GetQueryProviderFactory();
- var result = new CoreTable();
- var cols = columns ?? Columns.All<AddressLookup>();
- result.Columns.AddRange(cols.Select(x => new CoreColumn(x.Property)));
- foreach (var lookup in _cache)
- {
- if (filter == null || filter.Match(lookup, _queryProviderFactory))
- {
- var row = result.NewRow();
- result.LoadRow(lookup);
- result.Rows.Add(row);
- }
- }
- return result;
- }
- }
|