KML example I took it from
here. At the end my KML file gonna look like:
<?xml version="1.0" encoding="utf-8"?>
<kml xmlns="http://www.opengis.net/kml/2.2">
<Document>
<name>Paths</name>
<description>test</description>
<Style id="red">
<LineStyle>
<color>7f0000ff</color>
<width>4</width>
</LineStyle>
<PolyStyle>
<color>7f0000ff</color>
</PolyStyle>
</Style>
<Placemark>
<name>Picture 1</name>
<Snippet>This is a picture</Snippet>
<description><![CDATA[Location: 19.8619823, 45.2547803<br> Elevation: 838,0 m<br> Time Created: 12/02/2021 09:57:23 BRT<br> <img src="IMG_20130710_140741.jpg"><br><img src="IMG_20130710_140741.jpg"><br>>]]></description>
<Point>
<coordinates>19.8619823, 45.2547803</coordinates>
</Point>
</Placemark>
<Placemark>
<name>Absolute Extruded</name>
<description><![CDATA[Transparent green wall with yellow outlines]]></description>
<styleUrl>#yellowLineGreenPoly</styleUrl>
<LineString>
<extrude>1</extrude>
<tessellate>1</tessellate>
<altitudeMode>absolute</altitudeMode>
<coordinates>19.8619823, 45.2547803, 2357
19.8683723, 45.2524965, 2357
</coordinates>
</LineString>
</Placemark>
</Document>
</kml>
The model:
using System.Xml;
using System.Xml.Serialization;
namespace CreateKmlFromFiles
{
public class KmlModel
{
[XmlRoot("kml", Namespace = "http://www.opengis.net/kml/2.2")]
public class Kml
{
public Document Document { get; set; }
}
public class Document
{
public string name { get; set; }
public string description { get; set; }
public Style Style { get; set; }
[XmlElement("Placemark")]
public Placemark[] Placemark { get; set; }
}
public class Style
{
[XmlAttribute("id")]
public string id { get; set; }
public LineStyle LineStyle { get; set; }
public PolyStyle PolyStyle { get; set; }
}
public class LineStyle
{
public string color { get; set; }
public string width { get; set; }
}
public class PolyStyle
{
public string color { get; set; }
}
public class Placemark
{
public string name { get; set; }
public string Snippet { get; set; }
public XmlCDataSection description { get; set; }
public string styleUrl { get; set; }
public Point Point { get; set; }
public LineString LineString { get; set; }
}
public class LineString
{
public string extrude { get; set; }
public string tessellate { get; set; }
public string altitudeMode { get; set; }
public string coordinates { get; set; }
}
public class Point
{
public string coordinates { get; set; }
}
}
}
Program:
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Serialization;
using MetadataExtractor;
using MetadataExtractor.Formats.Exif;
using Newtonsoft.Json.Linq;
using Directory = System.IO.Directory;
namespace CreateKmlFromFiles
{
class Program
{
static void Main(string[] args)
{
StringBuilder sb = new StringBuilder();
foreach (string file in Directory.GetFiles(@"C:\someFilesWithGpsLocations"))
{
JObject myJObject = JObject.Parse(File.ReadAllText(file));
sb.Append($"{myJObject["lng"]}, {myJObject["lat"]}, 2357 {Environment.NewLine}");
}
KmlModel.Kml kmlModel = new KmlModel.Kml
{
Document = new KmlModel.Document
{
name = "Paths"
, description = "test"
, Style = new KmlModel.Style
{
id = "red"
, LineStyle = new KmlModel.LineStyle
{
color = "7f0000ff"
, width = "4"
}
, PolyStyle = new KmlModel.PolyStyle
{
color = "7f0000ff"
}
}
, Placemark = new KmlModel.Placemark[]
{
new()
{
name = "Picture 1"
, Snippet = "This is a picture"
, description = new XmlDocument().CreateCDataSection(
"Location: 19.8619823, 45.2547803<br> Elevation: 838,0 m"
+ "<br> Time Created: 12/02/2021 09:57:23 BRT"
+ "<br> <img src=\"IMG_20130710_140741.jpg\">"
+ "<br><img src=\"IMG_20130710_140741.jpg\"><br>>"
)
, Point = new KmlModel.Point
{
coordinates = "19.8619823, 45.2547803"
}
},
new()
{
name = "Absolute Extruded"
, description = new XmlDocument().CreateCDataSection("Transparent green wall with yellow outlines")
, styleUrl = "#yellowLineGreenPoly"
, LineString = new KmlModel.LineString
{
extrude = "1"
, tessellate = "1"
, altitudeMode = "absolute"
, coordinates = sb.ToString()
}
}
}
}
};
TextWriter txtWriter = new StreamWriter
(
Path.ChangeExtension
(
System.Reflection.Assembly.GetEntryAssembly()?.Location
, ".kml"
) ?? string.Empty);
XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
ns.Add("", "http://www.opengis.net/kml/2.2");
XmlSerializer xmlSerializer = new XmlSerializer(typeof(KmlModel.Kml));
xmlSerializer.Serialize(txtWriter, kmlModel, ns);
txtWriter.Close();
}
}
}
Difference between
this example, and this one, is that Placemark is now declared as list of Placemarks:
[XmlElement("Placemark")]
public Placemark[] Placemark { get; set; }
and description:
public XmlCDataSection description { get; set; }
where I had to create CDATA section, like:
description = new XmlDocument().CreateCDataSection("Transparent green wall with yellow outlines")