This short tutorial explains how to create KML File using C#. It has all the details to set the IDE, a list of steps to define the program flow, and a sample code demonstrating how to make a KML file using C#. It will guide you in defining custom attributes, setting their values for the KML file, and drawing various geometric shapes.
Steps to Create a KML using C#
- Set the environment to use Aspose.GIS for .NET by installing it into your project for KML file creation
- Call the CreateLayer() method in the Drivers.Kml namespace to create a VectorLayer object
- Define feature attributes by setting their names and types
- Create an object of the Polygon class
- Define a linear ring, add the outer boundary lat/long pairs, and set it as an exterior ring for the polygon
- Call the ConstructFeature()method in the Layer class and set the above polygon as its geometry
- Define optional attributes for styling and add it to the Layer object
These steps summarize the development of a KML generator using C#. Create a KML layer, define a set of attributes for use in the KML, create the geometry object such as a Polygon, and define the outer boundary with latitude/longitude. Construct a feature, set its geometry, and add it to the layer with the required attributes.
Code to Generate KML File using C#
using Aspose.Gis; | |
using Aspose.Gis.Geometries; | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
var license = new License();//License for creating KML file | |
license.SetLicense("license.lic"); // Load the license to avoid limitations | |
using (var kmlLayer = Drivers.Kml.CreateLayer("rectangle.kml")) | |
{ | |
kmlLayer.Attributes.Add(new FeatureAttribute("name", AttributeDataType.String)); | |
kmlLayer.Attributes.Add(new FeatureAttribute("description", AttributeDataType.String)); | |
var polygonGeometry = new Polygon();// Define the geometry for a rectangle | |
// Outer boundary (clockwise direction) | |
var boundaryRing = new LinearRing(); | |
boundaryRing.AddPoint(-122.084, 37.422); // Lower-left corner | |
boundaryRing.AddPoint(-122.084, 37.423); // Upper-left corner | |
boundaryRing.AddPoint(-122.083, 37.423); // Upper-right corner | |
boundaryRing.AddPoint(-122.083, 37.422); // Lower-right corner | |
boundaryRing.AddPoint(-122.084, 37.422); // Close the loop | |
polygonGeometry.ExteriorRing = boundaryRing; | |
var kmlFeature = kmlLayer.ConstructFeature(); | |
kmlFeature.Geometry = polygonGeometry; | |
kmlFeature.SetValue("name", "Rectangle Shape"); | |
kmlFeature.SetValue("description", "This polygon represents a rectangular area"); | |
kmlLayer.Add(kmlFeature); | |
} | |
} | |
} |
This code demonstrates the process of Google Earth KML file creation using C#. For drawing a line, set the geometry as a LineString with an array of points start and end points as mentioned in the commented sample code. You can create attributes of various types using the AttributeDataType class such as String, Integer, Boolean, and Double.
This tutorial has taught us how to make a KML file for Google Maps using C#. For creating a Shapefile, refer to the article on how to create a Shapefile in C#.