static StyleSelector createPlacemarkLineStyleMap(Style normalStyle, Style highlightStyle) { // Set up style map StyleMapCollection styleMapCollection = new StyleMapCollection(); styleMapCollection.Id = String.Format("stylemap"); // Create the normal line pair Pair normalPair = new Pair(); normalPair.StyleUrl = new Uri(String.Format("#{0}", normalStyle.Id), UriKind.Relative); normalPair.State = StyleState.Normal; // Create the highlight line pair Pair highlightPair = new Pair(); highlightPair.StyleUrl = new Uri(String.Format("#{0}", highlightStyle.Id), UriKind.Relative); highlightPair.State = StyleState.Highlight; // Attach both pairs to the map styleMapCollection.Add(normalPair); styleMapCollection.Add(highlightPair); return(styleMapCollection); }
private StyleSelector CreateStyleMap(Uri url) { // This is the same order as the C++ version - Normal then Highlight var map = new StyleMapCollection(); map.Add(this.CreatePair(StyleState.Normal, url)); map.Add(this.CreatePair(StyleState.Highlight, url)); return map; }
public static void AddStylesForPolygon(Document document, string[] styleNames) { // adding a stylemap that can be referenced from the elements Color32[] polyColors = { new Color32(80, 0, 0, 255), new Color32(255, 255, 255, 255) }; Color32[] lineColors = { new Color32(255, 0, 0, 255), new Color32(255, 255, 255, 255) }; bool[] polyFills = { true, false }; bool[] polyOutlines = { true, true }; // create two styles, both contain definitions for LineStyle and PolygonStyle for (int i = 0; i < styleNames.Length; i++) { StyleMapCollection smc = new StyleMapCollection(); Style[] stylePolyAndLine = { new Style(), new Style() }; PolygonStyle stPoly = new PolygonStyle(); stPoly.Color = polyColors[i]; stPoly.ColorMode = ColorMode.Normal; stPoly.Fill = polyFills[i]; stPoly.Outline = polyOutlines[i]; LineStyle stLine = new LineStyle(); stLine.Color = lineColors[i]; stLine.ColorMode = ColorMode.Normal; stylePolyAndLine[0].Id = styleNames[i] + "_Normal"; stylePolyAndLine[0].Polygon = stPoly; stylePolyAndLine[0].Line = stLine; document.AddStyle(stylePolyAndLine[0]); stylePolyAndLine[1].Id = styleNames[i] + "_High"; stylePolyAndLine[1].Polygon = stPoly; stylePolyAndLine[1].Line = stLine; document.AddStyle(stylePolyAndLine[1]); // create a StyleMap collection and add above Styles as a pair // with different Style States smc.Id = styleNames[i]; Pair[] pr = { new Pair(), new Pair() }; pr[0].State = StyleState.Normal; pr[0].StyleUrl = new Uri("#" + styleNames[i] + "_Normal", UriKind.Relative); smc.Add(pr[0]); pr[1].State = StyleState.Highlight; pr[1].StyleUrl = new Uri("#" + styleNames[i] + "_High", UriKind.Relative); smc.Add(pr[1]); // add the stylemap collection to the document (in the same way as a style) document.AddStyle(smc); } }
/// <summary> /// Retrieves link for "normal" style icon, from given style map /// </summary> /// <param name="kml">kml file where the style map is in</param> /// <param name="styleMap">style map to search</param> /// <returns>style URL, or null when style couldn't be retrieved</returns> private static string GetStyleMapNormalStyleIconLink(KmlFile kml, StyleMapCollection styleMap) { var normalStyle = styleMap.First(x => x.State.HasValue && x.State.Value == StyleState.Normal); if (normalStyle != null) { string normalStyleUrl = normalStyle.StyleUrl.ToString(); if (normalStyleUrl.StartsWith("#")) { var iconStyle = kml.FindStyle(normalStyleUrl.Substring(1)); if (iconStyle != null && iconStyle is Style icon) { return(icon.Icon.Icon.Href.ToString()); } } } return(null); }
public static Style FindStyleByStyleURL(KmlFile kmlFile, string styleUrl) { SharpKml.Dom.Style style = null; if (!String.IsNullOrEmpty(styleUrl)) { if (styleUrl.StartsWith("#")) { styleUrl = styleUrl.Substring(1); } SharpKml.Dom.StyleSelector styleSelector = kmlFile.Styles.FirstOrDefault(s => s.Id == styleUrl); if (styleSelector != null && styleSelector is StyleMapCollection) { StyleMapCollection styleMapCollection = styleSelector as StyleMapCollection; styleMapCollection.ToList().ForEach(element => { if (element is Pair) { Pair pair = element as Pair; if (pair.State != null && pair.State == StyleState.Highlight) { string styleUrl2 = pair.StyleUrl.OriginalString; if (!String.IsNullOrEmpty(styleUrl2)) { if (styleUrl2.StartsWith("#")) { styleUrl2 = styleUrl2.Substring(1); } SharpKml.Dom.StyleSelector styleSelector2 = kmlFile.Styles.FirstOrDefault(s => s.Id == styleUrl2); if (styleSelector2 != null && styleSelector2 is SharpKml.Dom.Style) { style = styleSelector2 as SharpKml.Dom.Style; } } } } }); } } return(style); }
private StyleMapCollection GenerateNewStyle(string str, Feature feature, Location loc) { Color32 c = GetRandomColor(); StyleMapCollection stylemap = new StyleMapCollection(); StyleMapCollection stylemap2 = new StyleMapCollection(); Style s_normal = new Style() { Icon = new IconStyle { Icon = new IconStyle.IconLink(new Uri("http://maps.google.com/mapfiles/kml/shapes/placemark_circle.png")), Color = c }, Id = $"sn_{str}", Label = new LabelStyle() { Scale = 0.5 }, Line = new LineStyle() { Color = new Color32(255 / 2, c.Blue, c.Green, c.Red) } }; Style s_nolabels = new Style() { Icon = new IconStyle { Icon = new IconStyle.IconLink(new Uri("http://maps.google.com/mapfiles/kml/shapes/placemark_circle.png")), Color = c }, Id = $"sn_{str}_nolabels", Label = new LabelStyle() { Scale = 0.01 }, Line = new LineStyle() { Color = new Color32(255 / 2, c.Blue, c.Green, c.Red) } }; Style s_highlight = new Style() { Icon = new IconStyle { Icon = new IconStyle.IconLink(new Uri("http://maps.google.com/mapfiles/kml/shapes/placemark_circle_highlight.png")), Color = c }, Line = new LineStyle() { Color = new Color32(255, c.Blue, c.Green, c.Red), Width = 4 }, Id = $"sn_{str}_highlight", }; stylemap.Id = $"msn_{str}"; stylemap.Add(new Pair() { State = StyleState.Normal, StyleUrl = new Uri($"#{s_normal.Id}", UriKind.Relative) }); stylemap.Add(new Pair() { State = StyleState.Highlight, StyleUrl = new Uri($"#{s_highlight.Id}", UriKind.Relative) }); stylemap2.Id = $"msn_{str}_nolabels"; stylemap2.Add(new Pair() { State = StyleState.Normal, StyleUrl = new Uri($"#{s_nolabels.Id}", UriKind.Relative) }); stylemap2.Add(new Pair() { State = StyleState.Highlight, StyleUrl = new Uri($"#{s_highlight.Id}", UriKind.Relative) }); feature.AddStyle(stylemap); feature.AddStyle(stylemap2); feature.AddStyle(s_normal); feature.AddStyle(s_nolabels); feature.AddStyle(s_highlight); if (loc != null) { loc.StyleMap = stylemap; loc.StyleMapNoLabels = stylemap2; } return(Labels == LabelMode.All ? stylemap : stylemap2); }
public void ProduceKml(string file) { Kml doc = new Kml(); Document d = new Document() { Name = Path.GetFileName(file) }; doc.Feature = d; Folder root = new Folder() { Name = "Network" }; d.AddFeature(root); StyleMapCollection style_default = GenerateNewStyle("default", d, null); List <string> folders = (from i in Locations.Values where !string.IsNullOrWhiteSpace(i.ParentName) select i.ParentName).Distinct().ToList(); foreach (var str in folders) { Folder fPlacemark = new Folder() { Name = str }; Folder fPath = new Folder() { Name = "Paths" }; root.AddFeature(fPlacemark); if (!Locations.ContainsKey(str)) { Locations.Add(str, new Location() { Name = str, PlacemarkFolder = fPlacemark }); } else { fPlacemark.AddFeature(fPath); Locations[str].PathFolder = fPath; Locations[str].PlacemarkFolder = fPlacemark; } GenerateNewStyle(str.Sanitize(), d, Locations[str]); } foreach (var loc in Locations.Values) { var parent = (from i in Locations.Values where i.Name == loc.ParentName select i).FirstOrDefault(); loc.Parent = parent; } foreach (var loc in Locations.Values) { if (loc.Coordinates == null) { continue; } Placemark plPoint = new Placemark(); Placemark plPath = new Placemark(); if (loc.Parent != null) { switch (Labels) { case LabelMode.All: plPoint.StyleUrl = new Uri($"#msn_{loc.ParentName.Sanitize()}", UriKind.Relative); break; case LabelMode.Some: if (IsImportant(loc.Name)) { plPoint.StyleUrl = new Uri($"#msn_{loc.ParentName.Sanitize()}", UriKind.Relative); } else { plPoint.StyleUrl = new Uri($"#msn_{loc.ParentName.Sanitize()}_nolabels", UriKind.Relative); } break; case LabelMode.None: plPoint.StyleUrl = new Uri($"#msn_{loc.ParentName.Sanitize()}_nolabels", UriKind.Relative); break; } } else if (loc.StyleMap != null) { switch (Labels) { case LabelMode.All: case LabelMode.Some: plPoint.StyleUrl = new Uri($"#{loc.StyleMap.Id}", UriKind.Relative); break; case LabelMode.None: plPoint.StyleUrl = new Uri($"#{loc.StyleMapNoLabels.Id}", UriKind.Relative); break; } } else { plPoint.StyleUrl = new Uri($"#{style_default.Id}", UriKind.Relative); } plPoint.Name = loc.Name; plPoint.Geometry = new SharpKml.Dom.Point() { Coordinate = loc.Coordinates }; Folder placemarkFolder = loc.Parent?.PlacemarkFolder ?? loc.PlacemarkFolder ?? root; placemarkFolder.AddFeature(plPoint); if (loc.Parent?.Coordinates != null) { plPath.StyleUrl = new Uri($"#msn_{loc.ParentName.Sanitize()}", UriKind.Relative); plPath.Name = loc.Name + " Path"; LineString l = new LineString(); l.Coordinates = new CoordinateCollection(); l.Coordinates.Add(loc.Coordinates); l.Coordinates.Add(loc.Parent.Coordinates); plPath.Geometry = l; loc.Parent.PathFolder.AddFeature(plPath); } } Folder linksFolder = new Folder() { Name = "Links" }; Style s = new Style() { Id = $"sn_links", Line = new LineStyle() { Color = new Color32(255, 255, 0, 0), Width = 4.0 } }; root.AddStyle(s); if (Links.Count > 0) { root.AddFeature(linksFolder); } foreach (Link link in Links) { Placemark p = new Placemark() { Name = link.Name }; LineString l = new LineString(); l.Coordinates = new CoordinateCollection(); l.Coordinates.Add(link.Location1.Coordinates); l.Coordinates.Add(link.Location2.Coordinates); p.Geometry = l; linksFolder.AddFeature(p); p.StyleUrl = new Uri($"#{s.Id}", UriKind.Relative); } Serializer serializer = new Serializer(); serializer.Serialize(doc); File.WriteAllText(file, serializer.Xml); }