From ad37e2da502bdd0e493baac05496b20820a2ab88 Mon Sep 17 00:00:00 2001 From: Daniel Schick Date: Mon, 26 Dec 2022 09:09:21 +0100 Subject: [PATCH] AIS work in progress --- AIS/AISAdmin/Main.Designer.cs | 18 +----- AIS/AISAdmin/Main.cs | 73 ++++++++++++++++------- AIS/bsmd.AIS2Service/AIS_SQLiteStorage.cs | 4 +- AIS/bsmd.AIS2Service/MonitorZone.cs | 35 ++++++++++- 4 files changed, 87 insertions(+), 43 deletions(-) diff --git a/AIS/AISAdmin/Main.Designer.cs b/AIS/AISAdmin/Main.Designer.cs index fb5bc81d..70c35312 100644 --- a/AIS/AISAdmin/Main.Designer.cs +++ b/AIS/AISAdmin/Main.Designer.cs @@ -32,7 +32,6 @@ this.label1 = new System.Windows.Forms.Label(); this.comboBoxGroup = new System.Windows.Forms.ComboBox(); this.buttonNewGroup = new System.Windows.Forms.Button(); - this.buttonEditGroup = new System.Windows.Forms.Button(); this.buttonDeleteGroup = new System.Windows.Forms.Button(); this.groupBoxZones = new System.Windows.Forms.GroupBox(); this.checkBoxZoneActive = new System.Windows.Forms.CheckBox(); @@ -72,24 +71,13 @@ // buttonNewGroup // this.buttonNewGroup.Image = global::AISAdmin.Properties.Resources.document_plain_new; - this.buttonNewGroup.Location = new System.Drawing.Point(206, 6); + this.buttonNewGroup.Location = new System.Drawing.Point(249, 6); this.buttonNewGroup.Name = "buttonNewGroup"; this.buttonNewGroup.Size = new System.Drawing.Size(37, 31); this.buttonNewGroup.TabIndex = 2; this.buttonNewGroup.UseVisualStyleBackColor = true; this.buttonNewGroup.Click += new System.EventHandler(this.buttonNewGroup_Click); // - // buttonEditGroup - // - this.buttonEditGroup.Enabled = false; - this.buttonEditGroup.Image = global::AISAdmin.Properties.Resources.pencil; - this.buttonEditGroup.Location = new System.Drawing.Point(249, 6); - this.buttonEditGroup.Name = "buttonEditGroup"; - this.buttonEditGroup.Size = new System.Drawing.Size(37, 31); - this.buttonEditGroup.TabIndex = 3; - this.buttonEditGroup.UseVisualStyleBackColor = true; - this.buttonEditGroup.Click += new System.EventHandler(this.buttonEditGroup_Click); - // // buttonDeleteGroup // this.buttonDeleteGroup.Enabled = false; @@ -250,7 +238,6 @@ this.Controls.Add(this.groupBoxAssignments); this.Controls.Add(this.groupBoxZones); this.Controls.Add(this.buttonDeleteGroup); - this.Controls.Add(this.buttonEditGroup); this.Controls.Add(this.buttonNewGroup); this.Controls.Add(this.comboBoxGroup); this.Controls.Add(this.label1); @@ -270,8 +257,7 @@ private Label label1; private ComboBox comboBoxGroup; - private Button buttonNewGroup; - private Button buttonEditGroup; + private Button buttonNewGroup; private Button buttonDeleteGroup; private GroupBox groupBoxZones; private GroupBox groupBoxAssignments; diff --git a/AIS/AISAdmin/Main.cs b/AIS/AISAdmin/Main.cs index f27a5565..ccc1df40 100644 --- a/AIS/AISAdmin/Main.cs +++ b/AIS/AISAdmin/Main.cs @@ -1,4 +1,5 @@ using bsmd.AIS2Service; +using System.ComponentModel; namespace AISAdmin { @@ -6,12 +7,14 @@ namespace AISAdmin public partial class Main : Form { private AIS_SQLiteStorage? _storage; - private List? _monitorGroups; - private List? _monitorZones; + private BindingList? _monitorGroups = new BindingList(); + private BindingList? _monitorZones; + private MonitorGroup? _currentGroup; public Main() { InitializeComponent(); + _currentGroup = null; } private void buttonZoneUp_Click(object sender, EventArgs e) @@ -26,7 +29,30 @@ namespace AISAdmin private void buttonImportZone_Click(object sender, EventArgs e) { - + using (OpenFileDialog ofd = new OpenFileDialog()) + { + ofd.Filter = "Google Earth KML files (*.kml)|*.kml|All files (*.*)|*.*"; + ofd.RestoreDirectory = true; + ofd.Multiselect = true; + if (ofd.ShowDialog() == DialogResult.OK) + { + for (int i = 0, seq = this._monitorGroups.Count; i < ofd.FileNames.Length; i++, seq++) + { + MonitorZone mz = MonitorZone.ImportFromKML(ofd.FileNames[i]); + if(mz != null) + { + mz.MonitorGroupId = this._currentGroup.Id; + mz.Sequence = seq; + _storage.Save(mz); + foreach(GeoPoint gp in mz.Vertices) + { + gp.MonitorZoneId = mz.Id; + _storage.Save(gp); + } + } + } + } + } } private void buttonEditZone_Click(object sender, EventArgs e) @@ -48,12 +74,9 @@ namespace AISAdmin { this.textBoxGroup.ReadOnly = false; this.buttonSaveGroup.Enabled = true; - } - - private void buttonEditGroup_Click(object sender, EventArgs e) - { - - } + _currentGroup = new MonitorGroup(-1, ""); + this.textBoxGroup.ResetText(); + } private void buttonDeleteGroup_Click(object sender, EventArgs e) { @@ -67,37 +90,39 @@ namespace AISAdmin string groupName = this.textBoxGroup.Text.Trim(); if(groupName.Length > 0) { - MonitorGroup mg = new MonitorGroup(-1, groupName); - if(_storage.Save(mg)) + _currentGroup.Name = groupName; + if(_storage.Save(_currentGroup)) { - this._monitorGroups?.Add(mg); - this.comboBoxGroup.SelectedItem = mg; + if ((bool)!(_monitorGroups?.Contains(_currentGroup))) + this._monitorGroups.Add(_currentGroup); + + this.comboBoxGroup.SelectedItem = _currentGroup; } } - } - this.buttonSaveGroup.Enabled = false; - this.textBoxGroup.ReadOnly = true; + } } private void Main_Load(object sender, EventArgs e) { _storage = new AIS_SQLiteStorage(null); - _monitorGroups = _storage.LoadGroups(); - _monitorZones = _storage.LoadMonitorZones(); + _monitorGroups = new BindingList(_storage.LoadGroups()); + _monitorZones = new BindingList(_storage.LoadMonitorZones()); this.comboBoxGroup.DataSource = _monitorGroups; this.comboBoxGroup.DisplayMember = "Name"; + if (_monitorGroups.Count > 0) + this.comboBoxGroup.SelectedIndex = 0; } private void comboBoxGroup_SelectedIndexChanged(object sender, EventArgs e) { - MonitorGroup mGroup = (MonitorGroup) this.comboBoxGroup.SelectedItem; - if (mGroup != null) - { - this.textBoxGroup.Text = mGroup.Name; + _currentGroup = (MonitorGroup) this.comboBoxGroup.SelectedItem; + if (_currentGroup != null) + { + this.textBoxGroup.Text = _currentGroup.Name; List groupZones = new List(); foreach(MonitorZone mZone in _monitorZones) { - if (mZone.MonitorGroupId == mGroup.Id) + if (mZone.MonitorGroupId == _currentGroup.Id) groupZones.Add(mZone); } groupZones.Sort(); @@ -107,6 +132,8 @@ namespace AISAdmin { this.listBoxZones.DataSource = null; this.textBoxGroup.ResetText(); + this.buttonSaveGroup.Enabled = false; + this.textBoxGroup.ReadOnly = true; } } } diff --git a/AIS/bsmd.AIS2Service/AIS_SQLiteStorage.cs b/AIS/bsmd.AIS2Service/AIS_SQLiteStorage.cs index 831bb217..3f894c91 100644 --- a/AIS/bsmd.AIS2Service/AIS_SQLiteStorage.cs +++ b/AIS/bsmd.AIS2Service/AIS_SQLiteStorage.cs @@ -122,14 +122,14 @@ namespace bsmd.AIS2Service return monitorZones; } - bool Save(MonitorZone mZone) + public bool Save(MonitorZone mZone) { if (mZone == null) return false; if(mZone.Id <= 0) { // insert - string insertZoneString = $"INSERT INTO monitor_zone (name, active, monitor_group_id, sequence) VALUES ('{mZone.Name}', {mZone.Active}, {mZone.MonitorGroupId}, {mZone.Sequence}"; + string insertZoneString = $"INSERT INTO monitor_zone (name, active, monitor_group_id, sequence) VALUES ('{mZone.Name}', {mZone.Active}, {mZone.MonitorGroupId}, {mZone.Sequence})"; SQLiteCommand cmd = new SQLiteCommand(insertZoneString, _connection); int insertedRows = cmd.ExecuteNonQuery(); cmd.Dispose(); diff --git a/AIS/bsmd.AIS2Service/MonitorZone.cs b/AIS/bsmd.AIS2Service/MonitorZone.cs index b94a25bb..4145e67d 100644 --- a/AIS/bsmd.AIS2Service/MonitorZone.cs +++ b/AIS/bsmd.AIS2Service/MonitorZone.cs @@ -6,9 +6,12 @@ using System; using System.Collections.Generic; using System.Drawing; +using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; +using System.Web.UI.WebControls; +using System.Xml.Linq; namespace bsmd.AIS2Service { @@ -36,7 +39,7 @@ namespace bsmd.AIS2Service { #region fields - private readonly string _name; + private string _name; private readonly List _zones = new List(); #endregion @@ -54,7 +57,7 @@ namespace bsmd.AIS2Service public List Zones { get { return _zones; } } - public string Name { get { return _name; } } + public string Name { get { return _name; } set { _name = value; } } #endregion } @@ -97,6 +100,34 @@ namespace bsmd.AIS2Service public long MonitorGroupId { get; set; } + public static MonitorZone ImportFromKML(string filename) + { + MonitorZone result = null; + if (File.Exists(filename)) + { + XDocument kml = XDocument.Load(filename); + XNamespace ns = "http://www.opengis.net/kml/2.2"; + + string name = kml.Root.Element(ns + "Document").Element(ns + "name").Value; + if (name.EndsWith(".kml")) name = name.Substring(0, name.Length - 4); + result = new MonitorZone(-1, name); + + // now find all vertices + string[] vertices = kml.Root.Element(ns + "Document").Element(ns + "Placemark").Element(ns + "Polygon").Element(ns + "outerBoundaryIs").Element(ns + "LinearRing").Element(ns + "coordinates").Value.Split(' '); + for (int i = 0; i < vertices.Length - 1; i++) + { + string[] pointElems = vertices[i].Trim().Split(','); + if (pointElems.Length != 3) continue; + GeoPoint gp = new GeoPoint(-1); + gp.Lon = Double.Parse(pointElems[0], System.Globalization.NumberFormatInfo.InvariantInfo); + gp.Lat = Double.Parse(pointElems[1], System.Globalization.NumberFormatInfo.InvariantInfo); + result.Vertices.Add(gp); + } + } + + return result; + } + #endregion #region public methods