If you’re anything like me, you probably generate a ton of KML so I wanted to pass along a quick little method that generates a KML file from data in a C# DataSet. The real cool part about this is that it never writes anything to disc and it will always generate a unique file (based on a GUID). It’s also important to note that the icon path is set in the web.config file so it can be configured after the application is deployed.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 | public void kmlFile() { DataTable tbl = (DataTable)Session["DataTable"]; // Create a temporary file name using a GUID and start writing the File object tempGUID = System.Guid.NewGuid(); // Grab the icon path in the web.confg file. object iconPath; iconPath = ConfigurationManager.AppSettings["kmlIcon"]; // Start writing the KML Response.Write("\n"); Response.Write("\n"); Response.Write("My KML\n"); Response.Write("<!-- \n"); Response.Write("<IconStyle>\n"); Response.Write("<icon>\n"); Response.Write(" <href>" + iconPath + "</href>\n"); Response.Write("</icon>\n"); Response.Write("\n"); Response.Write(" -->\n"); Response.Write("\n"); Response.Write("-96.1501026686213\n"); Response.Write("37.95371359340249\n"); Response.Write("0\n"); Response.Write("4429395.882870826\n"); Response.Write("0\n"); Response.Write("\n"); // Loop through the dataset and populate the placemarks. string Name = string.Empty; string owner = string.Empty; string class = string.Empty; foreach (DataRow dr in tbl.Rows) { // Replace bad characters with good ones. Name = dr["Name"].ToString(); Name = Name.Replace("&", "&"); Name = Name.Replace("\\", """); Name = Name.Replace("<", "<"); Name = Name.Replace(">", ">"); owner = dr["Owner"].ToString(); owner = owner.Replace("&", "&"); owner = owner.Replace("\\", """); owner = owner.Replace("<", "<"); owner = owner.Replace(">", ">"); // Continue writing the KML Response.Write("\n"); Response.Write("" + Name + "\n"); Response.Write(" #pushpin\n"); Response.Write("\n"); Response.Write("< ![CDATA[\n"); Response.Write("\n"); Response.Write("<head>\n"); Response.Write("\n"); Response.Write("\n"); Response.Write("<body>\n"); Response.Write(" <h3 align=\"center\"><font COLOR=\"#FF0000\">" + class + "</font></h3> \n"); Response.Write(" <hr />\n"); Response.Write("<b>Name:</b> " + dr["Name"] + ""); Response.Write(""); Response.Write("<b>Owner:</b> " + owner + ""); Response.Write(""); Response.Write("<b>Latitude:</b> " + dr["Latitude"] + ""); Response.Write(""); Response.Write("<b>Longitide:</b> " + dr["Longitude"] + ""); Response.Write(""); Response.Write(" <hr />"); Response.Write(" <h3 align=\"center\"><font COLOR=\"#FF0000\">" + class + "</font></h3> \n"); Response.Write("</body>\n"); Response.Write("\n"); Response.Write("]]>"); Response.Write("\n"); Response.Write("\n"); Response.Write("" + dr["Longitude"] + ", " + dr["Latitude"] + "\n"); Response.Write("\n"); Response.Write("\n"); } Response.Write(""); Response.Write("\n"); // Response.Write the data with the Encoding "KML". This will force the default KML // reader to launch when the file is done writing. Response.AddHeader("content-disposition", "attachment; filename=" + tempGUID + ".kml"); Response.ContentType = "application/vnd.google-earth.kml+xml"; Response.AppendHeader("Content-Encoding", "kml"); } } |
Enjoy!
More from Adam Estrada
- GmapEZ
- Oracle Spatial and USER_SDO_GEOM_METADATA
- SpatiaLite 2.4.0
- GeoDC Kickoff…
- SpatiaLite and Smart Phones
Adam Estrada Recommends
- Adam - The Indian iPad killer (MtaraM)
- Update from Google Map on my Problem (Maploser)
- Layar for the iPhone (Maploser)
dsadasdasd