Import von Bastis Zonen, Laden und Speichern, Reihenfolge der Zonen (Sequence) ändern

This commit is contained in:
Daniel Schick 2022-12-27 17:55:00 +01:00
parent ad37e2da50
commit dbdb3b9e47
6 changed files with 206 additions and 95 deletions

View File

@ -35,14 +35,13 @@
this.buttonDeleteGroup = new System.Windows.Forms.Button();
this.groupBoxZones = new System.Windows.Forms.GroupBox();
this.checkBoxZoneActive = new System.Windows.Forms.CheckBox();
this.buttonSaveZone = new System.Windows.Forms.Button();
this.textBoxZone = new System.Windows.Forms.TextBox();
this.buttonDeleteZone = new System.Windows.Forms.Button();
this.buttonEditZone = new System.Windows.Forms.Button();
this.buttonImportZone = new System.Windows.Forms.Button();
this.buttonZoneDown = new System.Windows.Forms.Button();
this.buttonZoneUp = new System.Windows.Forms.Button();
this.listBoxZones = new System.Windows.Forms.ListBox();
this.buttonImportZone = new System.Windows.Forms.Button();
this.groupBoxAssignments = new System.Windows.Forms.GroupBox();
this.textBoxGroup = new System.Windows.Forms.TextBox();
this.buttonSaveGroup = new System.Windows.Forms.Button();
@ -64,7 +63,7 @@
this.comboBoxGroup.FormattingEnabled = true;
this.comboBoxGroup.Location = new System.Drawing.Point(63, 11);
this.comboBoxGroup.Name = "comboBoxGroup";
this.comboBoxGroup.Size = new System.Drawing.Size(137, 23);
this.comboBoxGroup.Size = new System.Drawing.Size(180, 23);
this.comboBoxGroup.TabIndex = 1;
this.comboBoxGroup.SelectedIndexChanged += new System.EventHandler(this.comboBoxGroup_SelectedIndexChanged);
//
@ -92,11 +91,9 @@
// groupBoxZones
//
this.groupBoxZones.Controls.Add(this.checkBoxZoneActive);
this.groupBoxZones.Controls.Add(this.buttonSaveZone);
this.groupBoxZones.Controls.Add(this.textBoxZone);
this.groupBoxZones.Controls.Add(this.buttonDeleteZone);
this.groupBoxZones.Controls.Add(this.buttonEditZone);
this.groupBoxZones.Controls.Add(this.buttonImportZone);
this.groupBoxZones.Controls.Add(this.buttonZoneDown);
this.groupBoxZones.Controls.Add(this.buttonZoneUp);
this.groupBoxZones.Controls.Add(this.listBoxZones);
@ -110,27 +107,18 @@
// checkBoxZoneActive
//
this.checkBoxZoneActive.AutoSize = true;
this.checkBoxZoneActive.Location = new System.Drawing.Point(237, 27);
this.checkBoxZoneActive.Location = new System.Drawing.Point(401, 29);
this.checkBoxZoneActive.Name = "checkBoxZoneActive";
this.checkBoxZoneActive.Size = new System.Drawing.Size(59, 19);
this.checkBoxZoneActive.TabIndex = 11;
this.checkBoxZoneActive.Text = "Active";
this.checkBoxZoneActive.UseVisualStyleBackColor = true;
//
// buttonSaveZone
//
this.buttonSaveZone.Enabled = false;
this.buttonSaveZone.Image = global::AISAdmin.Properties.Resources.disk_blue;
this.buttonSaveZone.Location = new System.Drawing.Point(733, 22);
this.buttonSaveZone.Name = "buttonSaveZone";
this.buttonSaveZone.Size = new System.Drawing.Size(37, 31);
this.buttonSaveZone.TabIndex = 10;
this.buttonSaveZone.UseVisualStyleBackColor = true;
this.buttonSaveZone.Click += new System.EventHandler(this.buttonSaveZone_Click);
this.checkBoxZoneActive.CheckedChanged += new System.EventHandler(this.checkBoxZoneActive_CheckedChanged);
//
// textBoxZone
//
this.textBoxZone.Location = new System.Drawing.Point(525, 27);
this.textBoxZone.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
this.textBoxZone.Location = new System.Drawing.Point(568, 25);
this.textBoxZone.Name = "textBoxZone";
this.textBoxZone.ReadOnly = true;
this.textBoxZone.Size = new System.Drawing.Size(202, 23);
@ -138,9 +126,10 @@
//
// buttonDeleteZone
//
this.buttonDeleteZone.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
this.buttonDeleteZone.Enabled = false;
this.buttonDeleteZone.Image = global::AISAdmin.Properties.Resources.delete;
this.buttonDeleteZone.Location = new System.Drawing.Point(482, 22);
this.buttonDeleteZone.Location = new System.Drawing.Point(525, 20);
this.buttonDeleteZone.Name = "buttonDeleteZone";
this.buttonDeleteZone.Size = new System.Drawing.Size(37, 31);
this.buttonDeleteZone.TabIndex = 8;
@ -149,30 +138,21 @@
//
// buttonEditZone
//
this.buttonEditZone.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
this.buttonEditZone.Enabled = false;
this.buttonEditZone.Image = global::AISAdmin.Properties.Resources.pencil;
this.buttonEditZone.Location = new System.Drawing.Point(439, 22);
this.buttonEditZone.Location = new System.Drawing.Point(482, 20);
this.buttonEditZone.Name = "buttonEditZone";
this.buttonEditZone.Size = new System.Drawing.Size(37, 31);
this.buttonEditZone.TabIndex = 6;
this.buttonEditZone.UseVisualStyleBackColor = true;
this.buttonEditZone.Click += new System.EventHandler(this.buttonEditZone_Click);
//
// buttonImportZone
//
this.buttonImportZone.Image = global::AISAdmin.Properties.Resources.import2;
this.buttonImportZone.Location = new System.Drawing.Point(396, 22);
this.buttonImportZone.Name = "buttonImportZone";
this.buttonImportZone.Size = new System.Drawing.Size(37, 31);
this.buttonImportZone.TabIndex = 5;
this.buttonImportZone.UseVisualStyleBackColor = true;
this.buttonImportZone.Click += new System.EventHandler(this.buttonImportZone_Click);
//
// buttonZoneDown
//
this.buttonZoneDown.Enabled = false;
this.buttonZoneDown.Image = global::AISAdmin.Properties.Resources.arrow_down_green;
this.buttonZoneDown.Location = new System.Drawing.Point(194, 145);
this.buttonZoneDown.Location = new System.Drawing.Point(358, 145);
this.buttonZoneDown.Name = "buttonZoneDown";
this.buttonZoneDown.Size = new System.Drawing.Size(37, 31);
this.buttonZoneDown.TabIndex = 4;
@ -183,7 +163,7 @@
//
this.buttonZoneUp.Enabled = false;
this.buttonZoneUp.Image = global::AISAdmin.Properties.Resources.arrow_up_green;
this.buttonZoneUp.Location = new System.Drawing.Point(194, 22);
this.buttonZoneUp.Location = new System.Drawing.Point(358, 22);
this.buttonZoneUp.Name = "buttonZoneUp";
this.buttonZoneUp.Size = new System.Drawing.Size(37, 31);
this.buttonZoneUp.TabIndex = 3;
@ -197,8 +177,19 @@
this.listBoxZones.ItemHeight = 15;
this.listBoxZones.Location = new System.Drawing.Point(6, 22);
this.listBoxZones.Name = "listBoxZones";
this.listBoxZones.Size = new System.Drawing.Size(182, 154);
this.listBoxZones.Size = new System.Drawing.Size(346, 154);
this.listBoxZones.TabIndex = 0;
this.listBoxZones.SelectedIndexChanged += new System.EventHandler(this.listBoxZones_SelectedIndexChanged);
//
// buttonImportZone
//
this.buttonImportZone.Image = global::AISAdmin.Properties.Resources.import2;
this.buttonImportZone.Location = new System.Drawing.Point(702, 6);
this.buttonImportZone.Name = "buttonImportZone";
this.buttonImportZone.Size = new System.Drawing.Size(37, 31);
this.buttonImportZone.TabIndex = 5;
this.buttonImportZone.UseVisualStyleBackColor = true;
this.buttonImportZone.Click += new System.EventHandler(this.buttonImportZone_Click);
//
// groupBoxAssignments
//
@ -221,7 +212,7 @@
//
this.buttonSaveGroup.Enabled = false;
this.buttonSaveGroup.Image = global::AISAdmin.Properties.Resources.disk_blue;
this.buttonSaveGroup.Location = new System.Drawing.Point(501, 6);
this.buttonSaveGroup.Location = new System.Drawing.Point(745, 6);
this.buttonSaveGroup.Name = "buttonSaveGroup";
this.buttonSaveGroup.Size = new System.Drawing.Size(37, 31);
this.buttonSaveGroup.TabIndex = 5;
@ -238,6 +229,7 @@
this.Controls.Add(this.groupBoxAssignments);
this.Controls.Add(this.groupBoxZones);
this.Controls.Add(this.buttonDeleteGroup);
this.Controls.Add(this.buttonImportZone);
this.Controls.Add(this.buttonNewGroup);
this.Controls.Add(this.comboBoxGroup);
this.Controls.Add(this.label1);
@ -266,7 +258,6 @@
private Button buttonZoneUp;
private TextBox textBoxGroup;
private Button buttonSaveGroup;
private Button buttonSaveZone;
private TextBox textBoxZone;
private Button buttonDeleteZone;
private Button buttonEditZone;

View File

@ -7,8 +7,7 @@ namespace AISAdmin
public partial class Main : Form
{
private AIS_SQLiteStorage? _storage;
private BindingList<MonitorGroup>? _monitorGroups = new BindingList<MonitorGroup>();
private BindingList<MonitorZone>? _monitorZones;
private BindingList<MonitorGroup>? _monitorGroups = new();
private MonitorGroup? _currentGroup;
public Main()
@ -19,39 +18,50 @@ namespace AISAdmin
private void buttonZoneUp_Click(object sender, EventArgs e)
{
MonitorZone mz = (MonitorZone)this.listBoxZones.SelectedItem;
int i = this.listBoxZones.SelectedIndex;
BindingList<MonitorZone> zones = (BindingList<MonitorZone>)this.listBoxZones.DataSource;
if (mz != null && (i > 0))
{
MonitorZone prevZone = zones[i - 1];
mz.Sequence -= 1;
prevZone.Sequence += 1;
zones[i - 1] = mz;
zones[i] = prevZone;
this.listBoxZones.SelectedIndex = i - 1;
}
}
private void buttonZoneDown_Click(object sender, EventArgs e)
{
MonitorZone mz = (MonitorZone)this.listBoxZones.SelectedItem;
int i = this.listBoxZones.SelectedIndex;
BindingList<MonitorZone> zones = (BindingList<MonitorZone>) this.listBoxZones.DataSource;
if (mz != null && (i < (zones.Count - 1)))
{
MonitorZone nextZone = zones[i + 1];
mz.Sequence += 1;
nextZone.Sequence -= 1;
zones[i + 1] = mz;
zones[i] = nextZone;
this.listBoxZones.SelectedIndex = i + 1;
}
}
private void buttonImportZone_Click(object sender, EventArgs e)
{
using (OpenFileDialog ofd = new OpenFileDialog())
{
using OpenFileDialog ofd = new();
ofd.Filter = "Google Earth KML files (*.kml)|*.kml|All files (*.*)|*.*";
ofd.RestoreDirectory = true;
ofd.Multiselect = true;
ofd.Multiselect = false;
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);
}
}
}
}
_monitorGroups = new BindingList<MonitorGroup>(MonitorGroup.LoadGroups(ofd.FileName));
this.comboBoxGroup.DataSource = _monitorGroups;
if (_monitorGroups.Count > 0)
this.comboBoxGroup.SelectedIndex = 0;
this.buttonSaveGroup.Enabled = _monitorGroups.Count > 0;
this.listBoxZones.Enabled = _monitorGroups.Count > 0;
}
}
@ -65,11 +75,6 @@ namespace AISAdmin
}
private void buttonSaveZone_Click(object sender, EventArgs e)
{
}
private void buttonNewGroup_Click(object sender, EventArgs e)
{
this.textBoxGroup.ReadOnly = false;
@ -85,18 +90,19 @@ namespace AISAdmin
private void buttonSaveGroup_Click(object sender, EventArgs e)
{
if(this.textBoxGroup.Text != null)
// save everything
foreach(MonitorGroup mg in this._monitorGroups)
{
string groupName = this.textBoxGroup.Text.Trim();
if(groupName.Length > 0)
this._storage?.Save(mg);
foreach(MonitorZone mz in mg.Zones)
{
_currentGroup.Name = groupName;
if(_storage.Save(_currentGroup))
{
if ((bool)!(_monitorGroups?.Contains(_currentGroup)))
this._monitorGroups.Add(_currentGroup);
if (mz.MonitorGroupId < 1) mz.MonitorGroupId = mg.Id;
this._storage?.Save(mz);
this.comboBoxGroup.SelectedItem = _currentGroup;
foreach(GeoPoint vertex in mz.Vertices)
{
if(vertex.MonitorZoneId < 1) vertex.MonitorZoneId = mz.Id;
this._storage?.Save(vertex);
}
}
}
@ -106,11 +112,21 @@ namespace AISAdmin
{
_storage = new AIS_SQLiteStorage(null);
_monitorGroups = new BindingList<MonitorGroup>(_storage.LoadGroups());
_monitorZones = new BindingList<MonitorZone>(_storage.LoadMonitorZones());
List<MonitorZone> zones = _storage.LoadMonitorZones();
foreach(MonitorGroup mg in _monitorGroups)
{
foreach(MonitorZone mz in zones)
{
if (mz.MonitorGroupId == mg.Id)
mg.Zones.Add(mz);
}
}
this.comboBoxGroup.DataSource = _monitorGroups;
this.comboBoxGroup.DisplayMember = "Name";
if (_monitorGroups.Count > 0)
this.comboBoxGroup.SelectedIndex = 0;
this.buttonSaveGroup.Enabled = _monitorGroups.Count > 0;
this.listBoxZones.Enabled = _monitorGroups.Count > 0;
}
private void comboBoxGroup_SelectedIndexChanged(object sender, EventArgs e)
@ -118,15 +134,8 @@ namespace AISAdmin
_currentGroup = (MonitorGroup) this.comboBoxGroup.SelectedItem;
if (_currentGroup != null)
{
this.textBoxGroup.Text = _currentGroup.Name;
List<MonitorZone> groupZones = new List<MonitorZone>();
foreach(MonitorZone mZone in _monitorZones)
{
if (mZone.MonitorGroupId == _currentGroup.Id)
groupZones.Add(mZone);
}
groupZones.Sort();
this.listBoxZones.DataSource = groupZones;
_currentGroup.Zones.Sort();
this.listBoxZones.DataSource = new BindingList<MonitorZone>(_currentGroup.Zones);
}
else
{
@ -135,6 +144,27 @@ namespace AISAdmin
this.buttonSaveGroup.Enabled = false;
this.textBoxGroup.ReadOnly = true;
}
this.buttonZoneDown.Enabled = (this._currentGroup != null) && (this._currentGroup.Zones.Count > 1);
this.buttonZoneUp.Enabled = (this._currentGroup != null) && (this._currentGroup.Zones.Count > 1);
}
private void checkBoxZoneActive_CheckedChanged(object sender, EventArgs e)
{
MonitorZone mz = (MonitorZone)this.listBoxZones.SelectedItem;
if (mz != null)
{
mz.Active = this.checkBoxZoneActive.Checked;
}
}
private void listBoxZones_SelectedIndexChanged(object sender, EventArgs e)
{
MonitorZone mz = (MonitorZone) this.listBoxZones.SelectedItem;
if(mz != null)
{
this.checkBoxZoneActive.Checked = mz.Active;
this.textBoxZone.Text = mz.Name;
}
}
}
}

Binary file not shown.

View File

@ -80,7 +80,7 @@ namespace bsmd.AIS2Service
{
lvCmd.Parameters.Clear();
lvCmd.Parameters.AddWithValue("@ID", mz.Id);
SQLiteDataReader lvReader = lvCmd.ExecuteReader();
reader = lvCmd.ExecuteReader();
if(reader.HasRows)
{
while(reader.Read())
@ -92,7 +92,7 @@ namespace bsmd.AIS2Service
mz.Vertices.Add(gp);
}
}
lvReader.Close();
reader.Close();
}
lvCmd.Dispose();
@ -101,9 +101,9 @@ namespace bsmd.AIS2Service
SQLiteCommand laCmd = new SQLiteCommand(loadAssignmentsString, _connection);
foreach (MonitorZone mz in monitorZones)
{
lvCmd.Parameters.Clear();
lvCmd.Parameters.AddWithValue("@ID", mz.Id);
SQLiteDataReader lvReader = laCmd.ExecuteReader();
laCmd.Parameters.Clear();
laCmd.Parameters.AddWithValue("@ID", mz.Id);
reader = laCmd.ExecuteReader();
if (reader.HasRows)
{
while (reader.Read())
@ -115,7 +115,7 @@ namespace bsmd.AIS2Service
mz.Assignments.Add(ma);
}
}
lvReader.Close();
reader.Close();
}
laCmd.Dispose();
@ -284,7 +284,7 @@ namespace bsmd.AIS2Service
public bool Save(GeoPoint geoPoint)
{
if (geoPoint == null) return false;
string saveGeoPointString = $"INSERT INTO zone_vertex (monitor_zone_id, latitude, longitude) VALUES ({geoPoint.MonitorZoneId}, {geoPoint.Lat}, {geoPoint.Lon})";
string saveGeoPointString = $"INSERT INTO zone_vertex (monitor_zone_id, latitude, longitude) VALUES ({geoPoint.MonitorZoneId}, {geoPoint.Lat.ToDotString()}, {geoPoint.Lon.ToDotString()})";
SQLiteCommand cmd = new SQLiteCommand(saveGeoPointString, _connection);
int insertedRows = cmd.ExecuteNonQuery();
cmd.Dispose();

View File

@ -12,6 +12,7 @@ using System.Text;
using System.Threading.Tasks;
using System.Web.UI.WebControls;
using System.Xml.Linq;
using log4net;
namespace bsmd.AIS2Service
{
@ -37,6 +38,9 @@ namespace bsmd.AIS2Service
public class MonitorGroup : DBEntity
{
private static ILog _log = LogManager.GetLogger(typeof(MonitorGroup));
#region fields
private string _name;
@ -60,13 +64,79 @@ namespace bsmd.AIS2Service
public string Name { get { return _name; } set { _name = value; } }
#endregion
#region public static methods
/// <summary>
/// Da Basti nun eine Datei mit allen Elementen exportiert gibt es eine einzelne Funktion, die alles
/// importiert
/// </summary>
/// <param name="filename"></param>
/// <returns></returns>
public static List<MonitorGroup> LoadGroups(string filename)
{
List<MonitorGroup> groups = new List<MonitorGroup>();
try
{
if (File.Exists(filename))
{
XDocument kml = XDocument.Load(filename);
XNamespace ns = "http://www.opengis.net/kml/2.2";
foreach(XElement rootFolderNode in kml.Root.Element(ns + "Document").Element(ns + "Folder").Elements(ns + "Folder"))
{
MonitorGroup mg = new MonitorGroup(-1, rootFolderNode.Element(ns + "name").Value);
int sequence = 1;
foreach(XElement placemark in rootFolderNode.Elements(ns + "Placemark"))
{
MonitorZone mz = new MonitorZone(-1, placemark.Element(ns + "name").Value);
mz.Active = true;
mz.Sequence = sequence;
// now add all vertices
string[] vertices = 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);
mz.Vertices.Add(gp);
}
mg.Zones.Add(mz);
sequence++;
}
groups.Add(mg);
}
}
}
catch(Exception ex)
{
_log.Error(ex.ToString());
}
return groups;
}
#endregion
#region public methods
public override string ToString()
{
return this.Name;
}
#endregion
}
#endregion
#region class MonitorZone
public class MonitorZone : DBEntity
public class MonitorZone : DBEntity, IComparable<MonitorZone>
{
#region fields
@ -100,6 +170,10 @@ namespace bsmd.AIS2Service
public long MonitorGroupId { get; set; }
#endregion
#region public static methods
public static MonitorZone ImportFromKML(string filename)
{
MonitorZone result = null;
@ -150,6 +224,16 @@ namespace bsmd.AIS2Service
return result;
}
public int CompareTo(MonitorZone other)
{
return this.Sequence.CompareTo(other.Sequence);
}
public override string ToString()
{
return String.Format("{0} (Seq.:{1} #Vert.:{2}", this.Name, this.Sequence, this.Vertices.Count);
}
#endregion
}

View File

@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
@ -25,5 +26,10 @@ namespace bsmd.AIS2Service
else
flags = (T)(object)(flagsValue & (~flagValue));
}
public static string ToDotString(this double value)
{
return value.ToString(CultureInfo.GetCultureInfo("en-GB"));
}
}
}