GeoFenceGrid.cs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. // using System;
  2. // using System.Collections.Generic;
  3. // using System.Drawing;
  4. // using System.Linq;
  5. // using System.Windows.Controls;
  6. // using System.Windows.Media.Imaging;
  7. // using Comal.Classes;
  8. // using Google.Maps;
  9. // using InABox.Clients;
  10. // using InABox.Core;
  11. // using InABox.DynamicGrid;
  12. // using InABox.WPF;
  13. // using Microsoft.Win32;
  14. // using PRSDesktop.Forms;
  15. // using Path = System.IO.Path;
  16. //
  17. // namespace PRSDesktop;
  18. //
  19. // public class GeoFenceGrid : DynamicDataGrid<GeoFence>
  20. // {
  21. // private static BitmapImage MAP = PRSDesktop.Resources.map.AsBitmapImage();
  22. //
  23. // protected override void Init()
  24. // {
  25. // base.Init();
  26. // HiddenColumns.Add(x=>x.LastUpdate);
  27. // HiddenColumns.Add(x=>x.MinX);
  28. // HiddenColumns.Add(x=>x.MaxX);
  29. // HiddenColumns.Add(x=>x.MinY);
  30. // HiddenColumns.Add(x=>x.MaxY);
  31. // HiddenColumns.Add(x=>x.Geometry);
  32. // AddButton("Import", PRSDesktop.Resources.mapmarker.AsBitmapImage(), ImportGeoJSON);
  33. // ActionColumns.Add(new DynamicImageColumn((r) => MAP, MapClick));
  34. //
  35. // }
  36. //
  37. // protected override void DoReconfigure(DynamicGridOptions options)
  38. // {
  39. // base.DoReconfigure(options);
  40. // options.PageSize = 1000;
  41. // options.RecordCount = true;
  42. // }
  43. //
  44. // private bool MapClick(CoreRow? row)
  45. // {
  46. // if (row == null)
  47. // return false;
  48. // var longitude = (row.Get<GeoFence,double>(x=>x.MinX) + row.Get<GeoFence,double>(x=>x.MaxX)) / 2.0;
  49. // var latitude = (row.Get<GeoFence,double>(x=>x.MinY) + row.Get<GeoFence,double>(x=>x.MaxY)) / 2.0;
  50. // var timestamp = row.Get<GeoFence, DateTime>(x=>x.LastUpdate);
  51. // var geometry = row.Get<GeoFence, string>(x=>x.Geometry);
  52. // var form = new MapForm(latitude, longitude, timestamp, geometry);
  53. // form.ShowDialog();
  54. // return false;
  55. // }
  56. //
  57. //
  58. // private bool ImportGeoJSON(Button button, CoreRow[] rows)
  59. // {
  60. // OpenFileDialog ofd = new();
  61. // ofd.Filter = "GeoJSON Files (*.geojson)|*.geojson";
  62. // if (ofd.ShowDialog() == true)
  63. // {
  64. // Progress.ShowModal("Loading File", progress =>
  65. // {
  66. // var geojson = GeoJSONFile.Load(ofd.FileName);
  67. // if (geojson != null)
  68. // {
  69. // var queue = geojson.Features?.ToQueue() ?? new Queue<GeoJSONFeature>();
  70. // int fTotal = queue.Count;
  71. // while (queue.Count > 0)
  72. // {
  73. // List<GeoFence> updates = new();
  74. // var chunk = queue.Dequeue(1000).ToArray();
  75. // progress.Report($"Processing {((fTotal - queue.Count)*100.0F/fTotal):F2}% complete)");
  76. //
  77. // var fulladdresses = chunk.Select(x=>x.Properties?.FullAddress() ?? string.Empty).Distinct().ToArray();
  78. // var existing = Client.Query(new Filter<GeoFence>(x => x.FullAddress).InList(fulladdresses))
  79. // .ToObjects<GeoFence>().ToList();
  80. //
  81. // foreach (var record in chunk)
  82. // {
  83. // if (record.Properties == null || record.Geometry == null)
  84. // continue;
  85. //
  86. // string id = record.Properties.ID();
  87. // string fulladdress = record.Properties.FullAddress();
  88. //
  89. //
  90. // if (string.IsNullOrWhiteSpace(id) || string.IsNullOrWhiteSpace(fulladdress))
  91. // continue;
  92. //
  93. // var update = existing.FirstOrDefault(x => string.Equals(x.FullAddress, fulladdress));
  94. // if (update == null)
  95. // {
  96. // update = new GeoFence();
  97. // update.FullAddress = fulladdress;
  98. // existing.Add(update);
  99. // }
  100. //
  101. // update.Street = record.Properties.Street();
  102. // update.City = record.Properties.Suburb();
  103. // update.State = record.Properties.State();
  104. // update.Country = record.Properties.Country();
  105. // update.PostCode = record.Properties.Postcode();
  106. //
  107. // var bounds = record.Geometry.Bounds();
  108. // update.MinX = bounds.Item1.X;
  109. // update.MinY = bounds.Item1.Y;
  110. // update.MaxX = bounds.Item2.X;
  111. // update.MaxY = bounds.Item2.Y;
  112. //
  113. // var geometry =
  114. // Serialization.Deserialize<Dictionary<string, List<PointF>>>(update.Geometry) ?? new Dictionary<string, List<PointF>>();
  115. // geometry[id] = record.Geometry.Points();
  116. // update.Geometry = Serialization.Serialize(geometry);
  117. //
  118. // if (!updates.Contains(update) && (update.ID == Guid.Empty || update.IsChanged()))
  119. // updates.Add(update);
  120. // }
  121. // if (updates.Any())
  122. // Client.Save(updates, $"Loaded from {Path.GetFileName(ofd.FileName)}");
  123. // }
  124. //
  125. // }
  126. // });
  127. //
  128. // }
  129. //
  130. // return true;
  131. // }
  132. //
  133. // }