private void btnSaveKml_Click(object sender, EventArgs e) { if (this.Boards.Count == 0 || string.IsNullOrEmpty(Helpers.GoogleApiKey.GetKey())) { return; } string filePath = Helpers.Dialogs.GetSaveKmzPath(); if (string.IsNullOrEmpty(filePath)) { return; } DataManager.KmlWriteParameters p = new KmlWriteParameters { Boards = Boards, ColorsProvider = this.ColorsProvider ?? DataManager.ColorsProvider.Create(DataManager.ColorsProvider.GroupingBy.Size, Boards), FilePath = filePath }; var writer = new DataManager.WriterBuilder().Build(p); string path = writer.Write(p); System.Diagnostics.Process.Start(path); }
public string Write(WriteParameters parameters) { KmlWriteParameters p = parameters as KmlWriteParameters; var boards = p.Boards; if (boards.Count == 0) { return(null); } var colors = boards.Select(a => p.ColorsProvider.GetBoardColor(a)).Distinct().ToList(); var styles = CreateStyles(colors).ToList(); var document = new Document(); document.Name = System.IO.Path.GetFileNameWithoutExtension(p.FilePath); foreach (var group in boards.GroupBy(a => a.Address.City)) { Folder f = new Folder(); f.Name = group.Key; foreach (var b in group) { var placemark = new Placemark(); var c = p.ColorsProvider.GetBoardColor(b); string name = string.IsNullOrEmpty(b.Code) ? (b.Address.ToShortString()) : (b.Code + " " + b.Address.ToShortString()); placemark.Name = name; var styleId = styles.First(a => a.Id == $"colorIcon_{c.R}_{c.G}_{c.B}").Id; placemark.StyleUrl = new Uri($"#{styleId}", UriKind.Relative); placemark.Geometry = new SharpKml.Dom.Point { Coordinate = new Vector(b.Location.Latitude, b.Location.Longitude) }; string description = $"{b.Type} {b.Size}, сторона {b.Side}"; if (b.Metrics != null && b.Metrics.OTS != 0) { description += $"<br>OTS: {b.Metrics.OTS} 000, GRP: {b.Metrics.GRP}"; } description += $"<img src=\"{b.Photo.ToString()}\" height =\"300px\" width=\"auto\"/>"; description = $"<![CDATA[{description}]]>"; placemark.Description = new Description { Text = description }; f.AddFeature(placemark); } document.AddFeature(f); } foreach (var s in styles) { document.AddStyle(s); } KmlFile kml = KmlFile.Create(document, false); KmzFile k = KmzFile.Create(kml); string path = p.FilePath; if (System.IO.File.Exists(path)) { System.IO.File.Delete(path); } using (var stream = System.IO.File.OpenWrite(path)) { k.Save(stream); } return(path); }