//GPoint rotateCoord( double x, double y, double bearing, double pivot_x, double pivot_y ) //{ // const double halfC = Math.PI / 180.0; // double r = bearing * halfC; // double sin = Math.Sin(r); // double cos = Math.Cos(r); // //double sin = Math.Sin(bearing); // //double cos = Math.Cos(bearing); // // translate point back to origin: // c._x -= pivot_x; // c._y -= pivot_y; // // rotate point // //double nx = bearing > 0 ? (c._x * cos + c._y * sin) : (c._x * cos - c._y * sin); // //double ny = bearing > 0 ? (-1* c._x * sin + c._y * cos) : (c._x * sin + c._y * cos); // //double nx = bearing > 0 ? (c._x * cos - c._y * sin) :(c._x * cos + c._y * sin); // //double ny = bearing > 0 ? (c._x * sin + c._y * cos) : (-1 * c._x * sin + c._y * cos); // double nx = (c._x * cos - c._y * sin); // double ny = (c._x * sin + c._y * cos); // // translate point back: // c.x = nx*2 + pivot_x; // c.y = ny + pivot_y; //} void checkBoundary( coord c ) { if( _leftTop == null ) { _leftTop = new coord(); _rightBottom = new coord(); _leftTop._x = c._x; _leftTop._y = c._y; _rightBottom._x = c._x; _rightBottom._y = c._y; } else { _leftTop._x = c._x < _leftTop._x ? c._x : _leftTop._x; _leftTop._y = c._y < _leftTop._y ? c._y : _leftTop._y; _rightBottom._x = c._x > _rightBottom._x ? c._x : _rightBottom._x; _rightBottom._y = c._y > _rightBottom._y ? c._y : _rightBottom._y; } }
private void readXML( ) { string FileName = KMLIO.ShowFileDialog(); if (FileName == string.Empty) return; XDocument xDoc = System.Xml.Linq.XDocument.Load(FileName); string xNs = "{" + xDoc.Root.Name.Namespace.ToString() + "}"; // style parsing var styleList = from s in xDoc.Descendants(xNs + "Style") select s; foreach (var i in styleList)//v { style newStyle = new style(); newStyle.id = "#" + i.Attribute("id").Value; newStyle.label_color = i.Element(xNs + "LabelStyle").Element(xNs + "color").Value; newStyle.line_color = i.Element(xNs + "LineStyle").Element(xNs + "color").Value; newStyle.poly_color = i.Element(xNs + "PolyStyle").Element(xNs + "color").Value; styles.Add(newStyle); } // site parsing var coordsStr = from f in xDoc.Descendants(xNs + "Placemark") // where elementToFind.Contains(f.Parent.Element(xNs + "name").Value + f.Element(xNs + "name").Value) //select f.Element(xNs + "LineString").Element(xNs + "coordinates"); select f; //int seq = 0; //Console.WriteLine(coordsStr); foreach (var i in coordsStr) { var y = i.Element(xNs + "MultiGeometry").Descendants(xNs + "Polygon").Descendants(xNs + "outerBoundaryIs").Descendants(xNs + "LinearRing").Descendants(xNs + "coordinates"); char[] delemeters = { ',', ' ' }; site newSite = new site(); newSite.id = i.Attribute("id").Value; newSite.styleId = i.Element(xNs + "styleUrl").Value; newSite.name = i.Element(xNs + "name").Value; newSite.points = y.ElementAt(0).Value.ToString().TrimStart().Split(delemeters).ToList(); while (newSite.points.Remove("0")) ; newSite.styleRef = (from style in styles where style.id == newSite.styleId select style).ElementAt(0); // assert even Debug.Assert( (newSite.points.Count % 2) == 0 ); for (int j = 0; j < newSite.points.Count; j += 2 ) { coord c = new coord(); c._x = double.Parse(newSite.points[j]); c._y = double.Parse(newSite.points[j+1]); checkBoundary(c); newSite.coords.Add(c); } //Console.WriteLine("({0}/{1}) : {2} : {3} : {4}", ++seq, points.Count, i.Attribute("id").Value, i.Element(xNs + "name").Value, i.Element(xNs + "styleUrl").Value); sites.Add(newSite); } Console.WriteLine(coordsStr.Count()); Console.WriteLine("{0}:{1}", (_leftTop._y + _rightBottom._y) / 2, (_leftTop._x + _rightBottom._x) / 2); foreach( var s in sites ) { foreach( var i in s.coords ) { GPoint org = gmap.FromLatLngToLocal(gmap.Position); GPoint p = gmap.FromLatLngToLocal(new GMap.NET.PointLatLng(i._y, i._x) ); GPoint rp = rotatePosition( p, 0, org ); PointLatLng f = gmap.FromLocalToLatLng((int)rp.X, (int)rp.Y); i.x = f.Lng; i.y = f.Lat; } } }