Create polygon in a KML file

Folks
The following C# code will help you in creating a polygon for Google Earth based KML file. Have a look: –

public void AddPolygon(XmlDocument doc, XmlNode DocumentNode, int lat, int lon, int gridsize, string name, string id)
{
XmlNode PlacemarkNode = doc.CreateElement("Placemark");
DocumentNode.AppendChild(PlacemarkNode);
XmlNode nameNode = doc.CreateElement("name");
nameNode.AppendChild(doc.CreateTextNode(name));
PlacemarkNode.AppendChild(nameNode);
XmlNode StyleNode = doc.CreateElement("styleUrl");
StyleNode.AppendChild(doc.CreateTextNode(id));
PlacemarkNode.AppendChild(StyleNode);
XmlNode PolyNode = doc.CreateElement("Polygon");
PlacemarkNode.AppendChild(PolyNode);
XmlNode tessNode = doc.CreateElement("tessellate");
tessNode.AppendChild(doc.CreateTextNode("1"));
PolyNode.AppendChild(tessNode);
XmlNode altNode = doc.CreateElement("altitudeMode");
altNode.AppendChild(doc.CreateTextNode("clampToGround"));
PolyNode.AppendChild(altNode);
XmlNode BoundNode = doc.CreateElement("outerBoundaryIs");
PolyNode.AppendChild(BoundNode);
XmlNode LineNode = doc.CreateElement("LinearRing");
BoundNode.AppendChild(LineNode);
XmlNode coordinateNode = doc.CreateElement("coordinates");

string coord = lon.ToString() + "," + lat.ToString() + ",0," + Convert.ToString(lon + gridsize) + "," + lat.ToString() + ",0," + Convert.ToString(lon + gridsize) + "," + Convert.ToString(lat + gridsize) + ",0," + lon.ToString() + "," + Convert.ToString(lat + gridsize) + ",0," + lon.ToString() + "," + lat.ToString()+ ",0";

coordinateNode.AppendChild(doc.CreateTextNode(coord));
LineNode.AppendChild(coordinateNode);
}

Advertisements

About Paritosh Gupta

I am an M.Tech from IIT Roorkee and MBA from IIM Lucknow India. I am having more than 16 years of experience in design, development, Integration and implementation of Geographical Information System for various applications in Utilities, Watershed Management and Land records domains
This entry was posted in ArcObjects with Visual C# .NET. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s