public string Write(Guid listGuid) { string kml = string.Empty; Serializer serializer = new Serializer(); Kml _kml = new Kml(); Folder folder = new Folder(); UseWeb(spWeb => { SPList list = spWeb.Lists.GetList(listGuid, true); SPField field = list.GetGeoField(); if (field != null) { foreach (SPListItem item in list.Items) { string wkt = item[field.Id] as string; SimpleWKTReader wktReader = new SimpleWKTReader(); var simpleGeometry = wktReader.Parse(wkt); if (simpleGeometry.GeometryType == GeometryTypes.Point) { var _point = (OpenSMIL.Server.SimpleFeature.GeomtryTypes.Point)simpleGeometry; SharpKml.Dom.Point point = new SharpKml.Dom.Point(); point.Coordinate = new Vector(_point.Lat, _point.Lon); Placemark placemark = CreatePlaceMark(item.Title, point); folder.AddFeature(placemark); } else if (simpleGeometry.GeometryType == GeometryTypes.LineString) { var _lineString = (OpenSMIL.Server.SimpleFeature.GeomtryTypes.LineString)simpleGeometry; SharpKml.Dom.LineString line = new SharpKml.Dom.LineString(); line.Coordinates = CreateCoordinateCollection(_lineString.Points); Placemark placeMark = CreatePlaceMark(item.Title, line); folder.AddFeature(placeMark); } else if (simpleGeometry.GeometryType == GeometryTypes.Polygon) { var _polygon = (OpenSMIL.Server.SimpleFeature.GeomtryTypes.Polygon)simpleGeometry; OuterBoundary outerBoundary = new OuterBoundary(); outerBoundary.LinearRing = new LinearRing(); outerBoundary.LinearRing.Coordinates = CreateCoordinateCollection(_polygon.Points); SharpKml.Dom.Polygon polygon = new SharpKml.Dom.Polygon(); polygon.OuterBoundary = outerBoundary; polygon.Extrude = true; Placemark placeMark = CreatePlaceMark(item.Title, polygon); folder.AddFeature(placeMark); } } _kml.Feature = folder; } }); serializer.Serialize(_kml); return serializer.Xml; }
protected void GenerateKml() { var id = Convert.ToInt32(Server.HtmlEncode(Request.QueryString["Code"])); var document = new Document(); document.Id = "Document"; document.Name = "Document"; Description dsc = new Description(); dsc.Text = @"<h1>Car's Tracking</h1> "; CoordinateCollection coordinates = new CoordinateCollection(); DataTable dt = new DataTable(); try { DataSet ds = FieldAreaViewMethof.getfieldAreaValue(id); dt = ds.Tables[0]; string isreference = ds.Tables[0].Rows[0]["isReferencePoint"].ToString(); if (isreference == "True") { dt.Rows[0].Delete(); } foreach (DataRow dr in dt.Rows) { double lon = double.Parse(ParseDMS(dr["Longitude"].ToString()).ToString()); double lat = double.Parse(ParseDMS(dr["Latitude"].ToString()).ToString()); coordinates.Add(new Vector(lat, lon, 0)); } OuterBoundary outerBoundary = new OuterBoundary(); outerBoundary.LinearRing = new LinearRing(); outerBoundary.LinearRing.Coordinates = coordinates; // Polygon Setting: Polygon polygon = new Polygon(); polygon.Extrude = true; polygon.AltitudeMode = AltitudeMode.ClampToGround; polygon.OuterBoundary = outerBoundary; //Color Style Setting: byte byte_Color_R = 150, byte_Color_G = 150, byte_Color_B = 150, byte_Color_A = 100; //you may get your own color by other method var style = new SharpKml.Dom.Style(); style.Polygon = new PolygonStyle(); style.Polygon.ColorMode = SharpKml.Dom.ColorMode.Normal; style.Polygon.Color = new Color32(byte_Color_A, byte_Color_B, byte_Color_G, byte_Color_R); //Set the polygon and style to the Placemark: Placemark placemark = new Placemark(); placemark.Name = "Kukrail"; placemark.Geometry = polygon; placemark.AddStyle(style); //Finally to the document and save it document.AddFeature(placemark); var kml = new Kml(); kml.Feature = document; KmlFile kmlFile = KmlFile.Create(kml, true); if (File.Exists(Server.MapPath("~") + "MAP.kml")) { File.Delete(Server.MapPath("~") + "MAP.kml"); } using (var stream = System.IO.File.OpenWrite(Server.MapPath("~") + "MAP.kml")) { kmlFile.Save(stream); } } catch (Exception exc) { Response.Write(exc.Message); } finally { } }