How to convert GeoJson to KML using open source dll in C#

547 Views Asked by At

Please suggest me any open source that convert the GeoJson to KML File. I have spent too much times but I didn't found any open source. Please find the below link for paid source https://products.aspose.com/gis/net/

1

There are 1 best solutions below

0
Ashutosh Singh On

I found one way to convert GeoJson to KML by writing my own code.

        XmlDocument xmlDoc = new XmlDocument();
        XmlDeclaration xmlDeclaration = xmlDoc.CreateXmlDeclaration("1.0", "utf-8", null);

        // Create the root element
        XmlElement rootNode = xmlDoc.CreateElement("kml");
        rootNode.SetAttribute("xmlns", @"http://earth.google.com/kml/2.1");
        xmlDoc.InsertBefore(xmlDeclaration, xmlDoc.DocumentElement);
        xmlDoc.AppendChild(rootNode);

        // Add the Document
        XmlElement documentNode = CreateElement(xmlDoc, "Document", "Test KML File");
        rootNode.AppendChild(documentNode);

        // Create Folder in KML 
        XmlElement entityFolderNode = CreateElement(xmlDoc, "Folder", geoHtml.EntityTypeName);
         documentNode.SetAttribute("id", Convert.ToString(Guid.NewGuid()));

         //Prepare Schema ( This is using to show the fields on popup )
         XmlElement schema = CreateSchema(xmlDoc, fields);
         documentNode.AppendChild(schema);

         // Create Point Marker
         var pointMarkNode = GetPointMarkerData(xmlDoc, coordinates, colorID="");                                
        documentNode.AppendChild(pointMarkNode);

         //Create LineString Marker
         var lineMarkNode = GetLineStringMarkerData(xmlDoc, coordinates, colorID="");                                            
         documentNode.AppendChild(lineMarkNode);

        //Save the xmlDDocument into KML file 
         xmlDoc.Save("c:\temp\KMLFile.kml");


 /// <summary>
    /// Create Schema which will show on popup
    /// </summary>
    /// <param name="xmlDoc"></param>
    /// <param name="geoHtml"></param>
    /// <returns></returns>
    public XmlElement CreateSchema(XmlDocument xmlDoc, TemplateHtmlData geoHtml)
    {
        XmlElement schemaNode = xmlDoc.CreateElement("Schema");
        schemaNode.SetAttribute("id", Convert.ToString(geoHtml.EntityTypeName));

        var dynObj = JObject.Parse(geoHtml.FileJson);
        if (dynObj.Count > 0)
        {
            foreach (var admObj in dynObj)//Here begins the slow part.
            {
                XmlElement simpleField = xmlDoc.CreateElement("SimpleField");
                simpleField.SetAttribute("name", admObj.Key);
                simpleField.SetAttribute("type", "string");
                schemaNode.AppendChild(simpleField);
            }
        }
        return schemaNode;
    }

    /// <summary>
    /// Create Document, Folder based on element Type and add name in child
    /// </summary>
    /// <param name="xmlDoc"></param>
    /// <param name="element"></param>
    /// <param name="name"></param>
    /// <returns></returns>
    public XmlElement CreateElement(XmlDocument xmlDoc, string elementType, string name)
    {
        XmlElement documentNode = xmlDoc.CreateElement(elementType);
        XmlElement documentNameNode = xmlDoc.CreateElement("name");
        XmlText nameText = xmlDoc.CreateTextNode(name);
        documentNode.AppendChild(documentNameNode);
        documentNameNode.AppendChild(nameText);
        return documentNode;
    }

    public XmlElement CreateColorElement(XmlDocument xmlDoc, string id, string colorName)
    {
        XmlElement styleNode = xmlDoc.CreateElement("Style");
        styleNode.SetAttribute("id", id);
        XmlElement labelStyleNode = xmlDoc.CreateElement("LineStyle");
        styleNode.AppendChild(labelStyleNode);
        XmlElement colorNode = xmlDoc.CreateElement("color");
        XmlText colorText = xmlDoc.CreateTextNode(colorName);
        labelStyleNode.AppendChild(colorNode);
        colorNode.AppendChild(colorText);
        return styleNode;
    }

    /// <summary>
    /// Get Xml Element by creating the Point type of element
    /// </summary>
    /// <param name="xmlDoc"></param>
    /// <param name="geoJson"></param>
    /// <returns></returns>
    public XmlElement GetPointMarkerData(XmlDocument xmlDoc, List<dynamic> coordinates, string colorID)
    {
        XmlElement pointMarkNode = CreateElement(xmlDoc, "Placemark", geoJson.properties.name);

        XmlElement pointNode = xmlDoc.CreateElement("Point");
        pointMarkNode.AppendChild(pointNode);
        XmlElement coordsNode = xmlDoc.CreateElement("coordinates");
        XmlText coordsText = xmlDoc.CreateTextNode(string.Format("{0},{1},{2}", Convert.ToString(coordinates[0]), Convert.ToString(coordinates[1]), 0));
        pointNode.AppendChild(coordsNode);
        coordsNode.AppendChild(coordsText);

        //Show Field Information on Popup when double click on Assets name
        //XmlElement extendedElement = CreateExtendedElement(xmlDoc, geoJson);
        //pointMarkNode.AppendChild(extendedElement);

        //Call the Color element and set in Point element
        XmlElement styleURL = xmlDoc.CreateElement("styleUrl");
        XmlText colorsText = xmlDoc.CreateTextNode("#" + colorID);
        styleURL.AppendChild(colorsText);
        pointMarkNode.AppendChild(styleURL);

        return pointMarkNode;
    }

    /// <summary>
    ///  Get Xml Element by creating the Point type of element
    /// </summary>
    /// <param name="xmlDoc"></param>
    /// <param name="geoJson"></param>
    /// <returns></returns>
    public XmlElement GetLineStringMarkerData(XmlDocument xmlDoc, List<dynamic> coordinates, string colorID)
    {
        XmlElement lineStringMarkNode = CreateElement(xmlDoc, "Placemark", "LineString Name");

        XmlElement pointNode = xmlDoc.CreateElement("LineString");
        lineStringMarkNode.AppendChild(pointNode);
        XmlElement coordsNode = xmlDoc.CreateElement("coordinates");
        string coords = string.Empty;

        for (int index = 0; index < coordinates.Count(); index++)
        {
            coords += " " + Convert.ToString(coordinates[index][0]) + "," + Convert.ToString(coordinates[index][1]);
        }
        XmlText coordsText = xmlDoc.CreateTextNode(coords);
        pointNode.AppendChild(coordsNode);
        coordsNode.AppendChild(coordsText);

        //Show Field Information on Popup when double click on Assets name
       // XmlElement extendedElement = CreateExtendedElement(xmlDoc, geoJson);
       // lineStringMarkNode.AppendChild(extendedElement);

        //Call the Color element and set in linestring element
        XmlElement styleURL = xmlDoc.CreateElement("styleUrl");
        XmlText colorsText = xmlDoc.CreateTextNode("#" + colorID);
        styleURL.AppendChild(colorsText);
        lineStringMarkNode.AppendChild(styleURL);

        return lineStringMarkNode;
    }

Above mentioned way is custom method to create folder, schema, document, style etc in XML and once save the xmlDocument in kml file then it will convert to kml file.

This required only one dll (using System.Xml;)