private void SHP2KML(string shpFile, string kmlFile) { SharpMap.Data.Providers.ShapeFile shp = new SharpMap.Data.Providers.ShapeFile(shpFile); SharpMap.Data.FeatureDataSet fds = new SharpMap.Data.FeatureDataSet(); shp.Open(); shp.ExecuteIntersectionQuery(shp.GetExtents(), fds); System.Data.DataTable dt = fds.Tables[0]; KMLib.KMLRoot kml = new KMLib.KMLRoot(); for (int i = 0; i < dt.Rows.Count; i++ ) { SharpMap.Data.FeatureDataRow row = dt.Rows[i] as SharpMap.Data.FeatureDataRow; StringBuilder description = new StringBuilder(); for (int j = 0; j < dt.Columns.Count; j++) { description.Append("<p><b>" + capitalize(dt.Columns[j].ToString()) + "</b>: " + row[j] + "</p>\n"); } if (row.Geometry is SharpMap.Geometries.Point) { var pointGeom = row.Geometry as SharpMap.Geometries.Point; var placemark = new KMLib.Feature.Placemark(); placemark.Point = new KMLib.Geometry.KmlPoint((float) pointGeom.X, (float) pointGeom.Y); placemark.description = description.ToString(); kml.Document.Add(placemark); } else if (row.Geometry is SharpMap.Geometries.Polygon) { var polygonGeom = row.Geometry as SharpMap.Geometries.Polygon; var placemark = new KMLib.Feature.Placemark(); var polygon = new KMLib.Polygon(); var boundary = new KMLib.BoundaryIs(); foreach (SharpMap.Geometries.Point vertex in polygonGeom.ExteriorRing.Vertices) { boundary.LinearRing.Coordinates.Add(new Core.Geometry.Point3D(vertex.X, vertex.Y)); } boundary.LinearRing.CloseRing(); boundary.LinearRing.Extrude = true; polygon.OuterBoundaryIs = boundary; var interiorRing = new KMLib.BoundaryIs(); for (int j = 0; j < polygonGeom.NumInteriorRing; j++) { foreach (SharpMap.Geometries.Point point in polygonGeom.InteriorRing(j).Vertices) { interiorRing.LinearRing.Coordinates.Add(new Core.Geometry.Point3D(point.X, point.Y)); } interiorRing.LinearRing.CloseRing(); } polygon.InnerBoundaryIs = interiorRing; placemark.Polygon = polygon; kml.Document.Add(placemark); } } kml.Save(kmlFile); shp.Close(); btnConvert.Content = "Done Converting"; }
private void SHP2KML(string shpFile, string kmlFile) { SharpMap.Data.Providers.ShapeFile shp = new SharpMap.Data.Providers.ShapeFile(shpFile); SharpMap.Data.FeatureDataSet fds = new SharpMap.Data.FeatureDataSet(); shp.Open(); shp.ExecuteIntersectionQuery(shp.GetExtents(), fds); System.Data.DataTable dt = fds.Tables[0]; KMLib.KMLRoot kml = new KMLib.KMLRoot(); for (int i = 0; i < dt.Rows.Count; i++) { SharpMap.Data.FeatureDataRow row = dt.Rows[i] as SharpMap.Data.FeatureDataRow; StringBuilder description = new StringBuilder(); for (int j = 0; j < dt.Columns.Count; j++) { description.Append("<p><b>" + capitalize(dt.Columns[j].ToString()) + "</b>: " + row[j] + "</p>\n"); } if (row.Geometry is SharpMap.Geometries.Point) { var pointGeom = row.Geometry as SharpMap.Geometries.Point; var placemark = new KMLib.Feature.Placemark(); placemark.Point = new KMLib.Geometry.KmlPoint((float)pointGeom.X, (float)pointGeom.Y); placemark.description = description.ToString(); kml.Document.Add(placemark); } else if (row.Geometry is SharpMap.Geometries.Polygon) { var polygonGeom = row.Geometry as SharpMap.Geometries.Polygon; var placemark = new KMLib.Feature.Placemark(); var polygon = new KMLib.Polygon(); var boundary = new KMLib.BoundaryIs(); foreach (SharpMap.Geometries.Point vertex in polygonGeom.ExteriorRing.Vertices) { boundary.LinearRing.Coordinates.Add(new Core.Geometry.Point3D(vertex.X, vertex.Y)); } boundary.LinearRing.CloseRing(); boundary.LinearRing.Extrude = true; polygon.OuterBoundaryIs = boundary; var interiorRing = new KMLib.BoundaryIs(); for (int j = 0; j < polygonGeom.NumInteriorRing; j++) { foreach (SharpMap.Geometries.Point point in polygonGeom.InteriorRing(j).Vertices) { interiorRing.LinearRing.Coordinates.Add(new Core.Geometry.Point3D(point.X, point.Y)); } interiorRing.LinearRing.CloseRing(); } polygon.InnerBoundaryIs = interiorRing; placemark.Polygon = polygon; kml.Document.Add(placemark); } } kml.Save(kmlFile); shp.Close(); btnConvert.Content = "Done Converting"; }