113 lines
3.6 KiB
C#
113 lines
3.6 KiB
C#
// Copyright (c) 2023 - schick Informatik
|
|
// bsmd.AIS2Service [MonitorGroup.cs]: Daniel Schick
|
|
// Description: Group container for zones
|
|
//
|
|
using log4net;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
using System.Xml.Linq;
|
|
|
|
namespace bsmd.AIS2Service
|
|
{
|
|
#region class MonitorGroup
|
|
|
|
public class MonitorGroup : DBEntity
|
|
{
|
|
|
|
private static readonly ILog _log = LogManager.GetLogger(typeof(MonitorGroup));
|
|
|
|
#region fields
|
|
|
|
private string _name;
|
|
private readonly List<MonitorZone> _zones = new List<MonitorZone>();
|
|
|
|
#endregion
|
|
|
|
#region Construction
|
|
|
|
public MonitorGroup(long id, string name) : base(id)
|
|
{
|
|
_name = name;
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region Properties
|
|
|
|
public List<MonitorZone> Zones { get { return _zones; } }
|
|
|
|
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
|
|
}
|