Example #1
0
        Placemark CreatePlaceMark(string title, SharpKml.Dom.Geometry geometry)
        {
            Placemark placeMark = new Placemark();

            placeMark.Name     = title;
            placeMark.Geometry = geometry;
            return(placeMark);
        }
Example #2
0
 private NetTopologySuite.Geometries.Geometry KmlGeometryToGeometry(SharpKml.Dom.Geometry geometry)
 {
     NetTopologySuite.Geometries.Geometry result = null;
     if (geometry is SharpKml.Dom.Point)
     {
         var kmlPoint = geometry as SharpKml.Dom.Point;
         result = new NetTopologySuite.Geometries.Point(new Coordinate(kmlPoint.Coordinate.Longitude, kmlPoint.Coordinate.Latitude));
     }
     else if (geometry is SharpKml.Dom.Polygon)
     {
         var kmlPolygon = geometry as SharpKml.Dom.Polygon;
         if (kmlPolygon.OuterBoundary == null || kmlPolygon.OuterBoundary.LinearRing == null || kmlPolygon.OuterBoundary.LinearRing.Coordinates == null)
         {
             throw new Exception("Polygon is null");
         }
         var coordinates = new Coordinate[kmlPolygon.OuterBoundary.LinearRing.Coordinates.Count];
         int i           = 0;
         foreach (var coordinate in kmlPolygon.OuterBoundary.LinearRing.Coordinates)
         {
             coordinates[i++] = new Coordinate(coordinate.Longitude, coordinate.Latitude);
         }
         result = new NetTopologySuite.Geometries.Polygon(new NetTopologySuite.Geometries.LinearRing(coordinates));
     }
     else if (geometry is SharpKml.Dom.MultipleGeometry)
     {
         var mgeometry = geometry as SharpKml.Dom.MultipleGeometry;
         var polygons  = new List <NetTopologySuite.Geometries.Polygon>();
         foreach (var poly in mgeometry.Geometry)
         {
             var kmlPolygon = poly as SharpKml.Dom.Polygon;
             if (kmlPolygon.OuterBoundary == null || kmlPolygon.OuterBoundary.LinearRing == null || kmlPolygon.OuterBoundary.LinearRing.Coordinates == null)
             {
                 throw new Exception("Polygon is null");
             }
             var coordinates = new Coordinate[kmlPolygon.OuterBoundary.LinearRing.Coordinates.Count];
             int i           = 0;
             foreach (var coordinate in kmlPolygon.OuterBoundary.LinearRing.Coordinates)
             {
                 coordinates[i++] = new Coordinate(coordinate.Longitude, coordinate.Latitude);
             }
             polygons.Add(new NetTopologySuite.Geometries.Polygon(new NetTopologySuite.Geometries.LinearRing(coordinates)));
         }
         result = new NetTopologySuite.Geometries.MultiPolygon(polygons.ToArray());
     }
     return(result);
 }
        public static List <KmlPlaceMark> GetPlaceMarksFromKmlFile(string kmlFile)
        {
            string compatibleXml = GetCompatibleXml(new StreamReader(kmlFile).ReadToEnd());

            using (MemoryStream stream = new MemoryStream(Encoding.UTF8.GetBytes(compatibleXml)))
            {
                KmlFile             file = KmlFile.Load(stream);
                List <KmlPlaceMark> list = new List <KmlPlaceMark>();
                foreach (Placemark placemark in file.Root.Flatten().OfType <Placemark>())
                {
                    try
                    {
                        Geometry geo = placemark.Geometry;
                        if (geo != null)
                        {
                            if (geo is MultipleGeometry)
                            {
                                MultipleGeometry geometry2 = geo as MultipleGeometry;
                                if (geometry2.Geometry != null)
                                {
                                    foreach (Geometry geometry3 in geometry2.Geometry)
                                    {
                                        KmlPlaceMark placeMark = new KmlPlaceMark
                                        {
                                            Name     = placemark.Name,
                                            StyleUrl = placemark.StyleUrl.ToString()
                                        };
                                        if (placemark.Description != null)
                                        {
                                            placeMark.Description = placemark.Description.Text;
                                        }
                                        Geometry geometry4 = geometry3;
                                        FillPlacemarkWithGeometry(ref placeMark, ref geometry4);
                                        list.Add(placeMark);
                                    }
                                }
                            }
                            else
                            {
                                KmlPlaceMark mark2 = new KmlPlaceMark
                                {
                                    Name = placemark.Name
                                };
                                if (placemark.Description != null)
                                {
                                    mark2.Description = placemark.Description.Text;
                                }
                                FillPlacemarkWithGeometry(ref mark2, ref geo);
                                list.Add(mark2);
                            }
                        }
                        continue;
                    }
                    catch
                    {
                        continue;
                    }
                }
                return(list);
            }
        }