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; }
private static void FillPlacemarkWithGeometry(ref KmlPlaceMark placeMark, ref SharpKml.Dom.Geometry geo) { if (geo is SharpKml.Dom.Polygon) { SharpKml.Dom.Polygon polygon = geo as SharpKml.Dom.Polygon; if (polygon.OuterBoundary != null) { placeMark.Geometry = new GMapCommonType.Polygon(CoordinatesToPoints(polygon.OuterBoundary.LinearRing.Coordinates)); } } if (geo is SharpKml.Dom.Point) { SharpKml.Dom.Point point = geo as SharpKml.Dom.Point; placeMark.Geometry = new Point2D(point.Coordinate.Longitude, point.Coordinate.Latitude); } if (geo is SharpKml.Dom.LineString) { SharpKml.Dom.LineString str = geo as SharpKml.Dom.LineString; placeMark.Geometry = new Polyline(CoordinatesToPoints(str.Coordinates)); } }
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 { } }
/// <summary> /// Converts JSON geometry to KML geometry. /// </summary> /// <param name="geometry">ArcGIS JSON geometry</param> /// <returns><see cref="Geometry"/></returns> public static Geometry JsonToKmlGeometry(Dictionary<string, object> geometry) { Geometry placemarkGeometry; if (geometry.Keys.Contains("x")) { // Create point geometry // x and y are decimals; var x = Convert.ToDouble(geometry["x"]); var y = Convert.ToDouble(geometry["y"]); placemarkGeometry = new Point { Coordinate = new Vector(y, x) }; } else if (geometry.Keys.Contains("rings")) { // Create polygon geometry var rings = (ArrayList)geometry["rings"]; var linearRings = from ArrayList ring in rings select new LinearRing { Coordinates = new CoordinateCollection(from ArrayList point in ring select new Vector( Convert.ToDouble(point[1]), Convert.ToDouble(point[0]) )) }; if (linearRings.Count() > 1) { var polygon = new Polygon(); polygon.OuterBoundary = new OuterBoundary { LinearRing = linearRings.ElementAt(0) }; var innerBoundaries = from lr in linearRings.Skip(1) select new InnerBoundary { LinearRing = lr }; foreach (var ib in innerBoundaries) { polygon.AddInnerBoundary(ib); } placemarkGeometry = polygon; } else { placemarkGeometry = linearRings.First(); } } else if (geometry.Keys.Contains("paths")) { // Create line geometry var paths = (ArrayList)geometry["paths"]; var lineStrings = from ArrayList path in paths select new LineString { Coordinates = new CoordinateCollection(from ArrayList point in path select new Vector( Convert.ToDouble(point[1]), Convert.ToDouble(point[0]) )) }; if (lineStrings.Count() > 1) { var multiGeo = new MultipleGeometry(); foreach (var ls in lineStrings) { multiGeo.AddGeometry(ls); } placemarkGeometry = multiGeo; } else { placemarkGeometry = lineStrings.First(); } } else { placemarkGeometry = null; } return placemarkGeometry; }
private static void processKML(SharpKml.Dom.Element Element) { try { // log.Info(Element.ToString() + " " + Element.Parent); } catch { } SharpKml.Dom.Document doc = Element as SharpKml.Dom.Document; SharpKml.Dom.Placemark pm = Element as SharpKml.Dom.Placemark; SharpKml.Dom.Folder folder = Element as SharpKml.Dom.Folder; SharpKml.Dom.Polygon polygon = Element as SharpKml.Dom.Polygon; SharpKml.Dom.LineString ls = Element as SharpKml.Dom.LineString; MultipleGeometry geom = Element as MultipleGeometry; if (doc != null) { foreach (var feat in doc.Features) { //Console.WriteLine("feat " + feat.GetType()); //processKML((Element)feat); } } else if (folder != null) { foreach (SharpKml.Dom.Feature feat in folder.Features) { //Console.WriteLine("feat "+feat.GetType()); //processKML(feat); } } else if (pm != null) { } else if (polygon != null) { GMapPolygon kmlpolygon = new GMapPolygon(new List <PointLatLng>(), polygon.Id); kmlpolygon.Stroke.Color = Color.Purple; kmlpolygon.Fill = new SolidBrush(Color.FromArgb(30, Color.Blue)); foreach (var loc in polygon.OuterBoundary.LinearRing.Coordinates) { kmlpolygon.Points.Add(new PointLatLng(loc.Latitude, loc.Longitude)); } kmlpolygonsoverlay.Polygons.Add(kmlpolygon); } else if (ls != null) { GMapRoute kmlroute = new GMapRoute(new List <PointLatLng>(), "kmlroute"); kmlroute.Stroke.Color = Color.Purple; foreach (var loc in ls.Coordinates) { kmlroute.Points.Add(new PointLatLng(loc.Latitude, loc.Longitude)); } kmlpolygonsoverlay.Routes.Add(kmlroute); } else if (geom != null) { foreach (var geometry in geom.Geometry) { processKML(geometry); } } }
private void ProcessPolygonGeometry(Polygon f) { var outerRing = _geometryFactory.CreateLinearRing( f.OuterBoundary.LinearRing.Coordinates.Select(crd => new Coordinate(crd.Longitude, crd.Latitude)).ToArray()); var innerHoles = new List<ILinearRing>(); foreach (var hole in f.InnerBoundary) { innerHoles.Add(_geometryFactory.CreateLinearRing( hole.LinearRing.Coordinates.Select(crd => new Coordinate(crd.Longitude, crd.Latitude)).ToArray())); } var pGeom = _geometryFactory.CreatePolygon(outerRing, innerHoles.ToArray()); AddGeometryToCollection(f.GetParent<Placemark>(), pGeom); }
private Polygon CreateKmlPolygon(IPolygon polygon) { var kmlPolygon = new Polygon(); var ring = new LinearRing { Coordinates = new CoordinateCollection() }; kmlPolygon.OuterBoundary = new OuterBoundary { LinearRing = ring }; foreach (var coordinate in polygon.ExteriorRing.Coordinates) { ring.Coordinates.Add(new Vector(coordinate.Y, coordinate.X)); } foreach (var interiorRing in polygon.InteriorRings) { var innerBoundary = new InnerBoundary(); kmlPolygon.AddInnerBoundary(innerBoundary); ring = new LinearRing { Coordinates = new CoordinateCollection() }; innerBoundary.LinearRing = ring; foreach (var coordinate in interiorRing.Coordinates) { ring.Coordinates.Add(new Vector(coordinate.Y, coordinate.X)); } } return kmlPolygon; }