public static SharpMap.Map InitializeMap(float angle) { var dataSource = new SharpMap.Data.Providers.ShapeFile( string.Format("{0}/roads.shp", ShapefileSample.PathOsm), true); var fds = new SharpMap.Data.FeatureDataSet(); dataSource.Open(); dataSource.ExecuteIntersectionQuery(dataSource.GetExtents(), fds); dataSource.Close(); var gfp = new SharpMap.Data.Providers.GeometryFeatureProvider(fds.Tables[0]); var vl = new SharpMap.Layers.VectorLayer("roads", gfp) { CoordinateTransformation = LayerTools.Dhdn2ToWgs84 }; var ll = new SharpMap.Layers.LabelLayer("labels") { DataSource = gfp, CoordinateTransformation = LayerTools.Dhdn2ToWgs84, LabelColumn = "name", MultipartGeometryBehaviour = SharpMap.Layers.LabelLayer.MultipartGeometryBehaviourEnum.Largest, }; ll.Style.Halo = new System.Drawing.Pen(System.Drawing.Color.Red); //ll.Style.IgnoreLength = true; var map = new SharpMap.Map(); map.Layers.Add(vl); map.Layers.Add(ll); map.Layers.Add(vl); map.ZoomToExtents(); return map; }
public void TestDeleteAfterClose() { string test; CopyShapeFile(GetTestDataFilePath("SPATIAL_F_SKARVMUFF.shp"), out test); var shp = new SharpMap.Data.Providers.ShapeFile(test); shp.Open(); shp.Close(); var succeeded = true; foreach (var file in System.IO.Directory.GetFiles(System.IO.Path.GetDirectoryName(test), System.IO.Path.GetFileNameWithoutExtension(test) + ".*")) { try { System.IO.File.Delete(file); } catch (System.Exception) { System.Console.WriteLine("Failed to delete '{0}'", file); succeeded = false; } } NUnit.Framework.Assert.IsTrue(succeeded); }
public void TestReadValueZFromLineStringZShapeFile() { var file = GetTestDataFilePath("LineString_With_Z.shp"); var sh = new SharpMap.Data.Providers.ShapeFile(file, true); sh.Open(); var fc = sh.GetFeatureCount(); NUnit.Framework.Assert.AreEqual(1221, fc); NUnit.Framework.Assert.AreEqual(0, sh.GetObjectIDsInView(sh.GetExtents())[0]); var featsInView = sh.GetGeometriesInView(new GeoAPI.Geometries.Envelope(sh.GetExtents())); NUnit.Framework.Assert.AreEqual(1221, featsInView.Count); foreach (var item in featsInView) { NUnit.Framework.Assert.IsNotNull(item.Coordinate.Z); } NUnit.Framework.Assert.AreEqual(featsInView[0].Coordinates[0].Z, 35.865); NUnit.Framework.Assert.AreEqual(featsInView[0].Coordinates[1].Z, 35.743); NUnit.Framework.Assert.AreEqual(featsInView[1].Coordinates[0].Z, 35.518); NUnit.Framework.Assert.AreEqual(featsInView[1].Coordinates[1].Z, 35.518); NUnit.Framework.Assert.AreEqual(featsInView[2].Coordinates[0].Z, 37.438); NUnit.Framework.Assert.AreEqual(featsInView[2].Coordinates[1].Z, 37.441); NUnit.Framework.Assert.AreEqual(featsInView[3].Coordinates[0].Z, 37.441); NUnit.Framework.Assert.AreEqual(featsInView[3].Coordinates[1].Z, 37.441); sh.Close(); }
public void TestReadValueZFromPointZShapeFile() { var file = GetTestDataFilePath("Point_With_Z.shp"); var sh = new SharpMap.Data.Providers.ShapeFile(file, true); sh.Open(); var fc = sh.GetFeatureCount(); NUnit.Framework.Assert.AreEqual(1149, fc); NUnit.Framework.Assert.AreEqual(0, sh.GetObjectIDsInView(sh.GetExtents())[0]); var featsInView = sh.GetGeometriesInView(new GeoAPI.Geometries.Envelope(sh.GetExtents())); NUnit.Framework.Assert.AreEqual(1149, featsInView.Count); foreach (var item in featsInView) { NUnit.Framework.Assert.IsNotNull(item.Coordinate.Z); } NUnit.Framework.Assert.AreEqual(featsInView[0].Coordinate.Z, 146.473); NUnit.Framework.Assert.AreEqual(featsInView[1].Coordinate.Z, 181.374); NUnit.Framework.Assert.AreEqual(featsInView[2].Coordinate.Z, 146.676); NUnit.Framework.Assert.AreEqual(featsInView[3].Coordinate.Z, 181.087); NUnit.Framework.Assert.AreEqual(featsInView[4].Coordinate.Z, 169.948); NUnit.Framework.Assert.AreEqual(featsInView[5].Coordinate.Z, 169.916); sh.Close(); }
public void TestReadPointZShapeFile() { var file = GetTestDataFilePath("SPATIAL_F_SKARVMUFF.shp"); var sh = new SharpMap.Data.Providers.ShapeFile(file, true); var fc = sh.GetFeatureCount(); NUnit.Framework.Assert.AreEqual(4342, fc); sh.Open(); var featsInView = sh.GetGeometriesInView(new GeoAPI.Geometries.Envelope(sh.GetExtents())); NUnit.Framework.Assert.AreEqual(4342, featsInView.Count); sh.Close(); }
public void TestGetFeature() { NUnit.Framework.Assert.IsTrue(System.IO.File.Exists(GetTestDataFilePath("SPATIAL_F_SKARVMUFF.shp")), "Specified shapefile is not present!"); var shp = new SharpMap.Data.Providers.ShapeFile(GetTestDataFilePath("SPATIAL_F_SKARVMUFF.shp"), false, false); shp.Open(); var feat = shp.GetFeature(0); NUnit.Framework.Assert.IsNotNull(feat); shp.Close(); }
public void TestReadPointZShapeFile() { var file = GetTestDataFilePath("SPATIAL_F_SKARVMUFF.shp"); var sh = new SharpMap.Data.Providers.ShapeFile(file, true); var fc = sh.GetFeatureCount(); NUnit.Framework.Assert.AreEqual(4342, fc); sh.Open(); var featsInView = sh.GetGeometriesInView(new GeoAPI.Geometries.Envelope(sh.GetExtents())); NUnit.Framework.Assert.AreEqual(4342, featsInView.Count); sh.Close(); }
public void TestReadPointZShapeFile() { string file = TestUtility.GetPathToTestFile("SPATIAL_F_SKARVMUFF.shp"); var sh = new SharpMap.Data.Providers.ShapeFile(file, true); sh.Open(); int fc = sh.GetFeatureCount(); NUnit.Framework.Assert.AreEqual(4342, fc); NUnit.Framework.Assert.AreEqual(0, sh.GetObjectIDsInView(sh.GetExtents())[0]); var featsInView = sh.GetGeometriesInView(new GeoAPI.Geometries.Envelope(sh.GetExtents())); NUnit.Framework.Assert.AreEqual(4342, featsInView.Count); sh.Close(); }
public void TestDeleteAfterClose() { string test; CopyShapeFile(GetTestDataFilePath("SPATIAL_F_SKARVMUFF.shp"), out test); var shp = new SharpMap.Data.Providers.ShapeFile(test); shp.Open(); shp.Close(); var succeeded = true; foreach (var file in System.IO.Directory.GetFiles(System.IO.Path.GetDirectoryName(test), System.IO.Path.GetFileNameWithoutExtension(test) + ".*")) { try { System.IO.File.Delete(file); } catch (System.Exception) { System.Console.WriteLine("Failed to delete '{0}'", file); succeeded = false; } } NUnit.Framework.Assert.IsTrue(succeeded); }
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"; }
static void Main(string[] args) { string appPath = typeof(Program).Assembly.Location.Substring(0, typeof(Program).Assembly.Location.LastIndexOf("\\")); // delete old db if (System.IO.File.Exists(string.Format(@"{0}\Data\db.sqlite", appPath))) { System.IO.File.Delete(string.Format(@"{0}\Data\db.sqlite", appPath)); } // initialize sqlite String slPath = appPath.Substring(0, appPath.LastIndexOf('\\')); slPath = slPath.Substring(0, slPath.LastIndexOf('\\')); slPath = slPath.Substring(0, slPath.LastIndexOf('\\')); slPath = slPath + "\\SpatialLite"; String path = Environment.GetEnvironmentVariable("path"); if (path == null) { path = ""; } if (!path.ToLowerInvariant().Contains(slPath.ToLowerInvariant())) { Environment.SetEnvironmentVariable("path", slPath + ";" + path); } var cn = new SQLiteConnection(string.Format(@"Data Source={0}\Data\db.sqlite;Version=3;", appPath)); cn.Open(); SQLiteCommand cm = new SQLiteCommand(String.Format("SELECT load_extension('{0}');", "libspatialite-4.dll"), cn); cm.ExecuteNonQuery(); // create geometry table cm = new SQLiteCommand( @"CREATE TABLE WorldGeom (" + @"ID INTEGER PRIMARY KEY AUTOINCREMENT, " + @"Geometry BLOB NOT NULL);", cn); cm.ExecuteNonQuery(); // create feature data table cm = new SQLiteCommand( @"CREATE TABLE WorldData (" + @"ID INTEGER PRIMARY KEY AUTOINCREMENT, " + @"ISO2 VARCHAR(2) NOT NULL, " + @"ISO3 VARCHAR(3) NOT NULL, " + @"Name TEXT NOT NULL, " + @"Region TEXT NOT NULL, " + @"Area DOUBLE NOT NULL, " + @"Pop DOUBLE NOT NULL);", cn); cm.ExecuteNonQuery(); // copy shape data to sqlite var shapeFile = appPath + @"\Data\world_countries_boundary_file_world_2002.shp"; var shp = new SharpMap.Data.Providers.ShapeFile(shapeFile); shp.Open(); FeatureDataSet ds = new FeatureDataSet(); shp.ExecuteIntersectionQuery(new SharpMap.Geometries.BoundingBox(double.MinValue, double.MinValue, double.MaxValue, double.MaxValue), ds); foreach (FeatureDataRow row in ds.Tables[0].Rows) { var bytes = SharpMap.Converters.WellKnownBinary.GeometryToWKB.Write(row.Geometry); cm = new SQLiteCommand("INSERT INTO WorldGeom (Geometry) VALUES (GeomFromWkb(@wkb, -1))", cn); cm.Parameters.Add("Geometry", DbType.Object); cm.Parameters.AddWithValue("@wkb", bytes); cm.ExecuteNonQuery(); cm = new SQLiteCommand("INSERT INTO WorldData (ISO2, ISO3, Name, Region, Area, Pop) VALUES (@iso2, @iso3, @name, @region, @area, @pop)", cn); cm.Parameters.AddWithValue("@iso2", row["ISO_2_CODE"]); cm.Parameters.AddWithValue("@iso3", row["ISO_3_CODE"]); cm.Parameters.AddWithValue("@name", row["NAME"]); cm.Parameters.AddWithValue("@region", row["REGION"]); cm.Parameters.AddWithValue("@area", row["AREA"]); cm.Parameters.AddWithValue("@pop", row["POP2005"]); cm.ExecuteNonQuery(); } shp.Close(); // create spatial index cm = new SQLiteCommand("SELECT CreateMbrCache('WorldGeom', 'Geometry');", cn); cm.ExecuteNonQuery(); }
public void TestGetFeature() { NUnit.Framework.Assert.IsTrue(System.IO.File.Exists(GetTestDataFilePath("SPATIAL_F_SKARVMUFF.shp")), "Specified shapefile is not present!"); var shp = new SharpMap.Data.Providers.ShapeFile(GetTestDataFilePath("SPATIAL_F_SKARVMUFF.shp"), false, false); shp.Open(); var feat = shp.GetFeature(0); NUnit.Framework.Assert.IsNotNull(feat); shp.Close(); }
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"; }
static void Main(string[] args) { string appPath = typeof(Program).Assembly.Location.Substring(0, typeof(Program).Assembly.Location.LastIndexOf("\\")); // delete old db if (System.IO.File.Exists(string.Format(@"{0}\Data\db.sqlite", appPath))) { System.IO.File.Delete(string.Format(@"{0}\Data\db.sqlite", appPath)); } var mod_spatialite_folderPath = (IntPtr.Size == 4) ? "mod_spatialite-4.4.0-RC0-win-x86" : "mod_spatialite-4.4.0-RC0-win-amd64"; //using relative path, cannot use absolute path, dll load will fail var slPath = appPath.Substring(0, appPath.LastIndexOf('\\')); slPath = slPath.Substring(0, slPath.LastIndexOf('\\')); slPath = slPath.Substring(0, slPath.LastIndexOf('\\')); string path = slPath + @"\SpatialLite\" + mod_spatialite_folderPath + ";" + Environment.GetEnvironmentVariable("Path", EnvironmentVariableTarget.Machine); Environment.SetEnvironmentVariable("Path", path, EnvironmentVariableTarget.Process); var cn = new SQLiteConnection(string.Format(@"Data Source={0}\Data\db.sqlite;Version=3;", appPath)); cn.Open(); cn.LoadExtension("mod_spatialite"); var cm = new SQLiteCommand("SELECT InitSpatialMetadata();", cn); cm.ExecuteNonQuery(); // create geometry table cm = new SQLiteCommand( @"CREATE TABLE WorldGeom (" + @"ID INTEGER PRIMARY KEY AUTOINCREMENT)", cn); cm.ExecuteNonQuery(); cm = new SQLiteCommand("SELECT AddGeometryColumn('WorldGeom', 'Geometry', 4326, 'GEOMETRY', 2)", cn); cm.ExecuteNonQuery(); // create feature data table cm = new SQLiteCommand( @"CREATE TABLE WorldData (" + @"ID INTEGER PRIMARY KEY AUTOINCREMENT, " + @"ISO2 VARCHAR(2) NOT NULL, " + @"ISO3 VARCHAR(3) NOT NULL, " + @"Name TEXT NOT NULL, " + @"Region TEXT NOT NULL, " + @"Area DOUBLE NOT NULL, " + @"Pop DOUBLE NOT NULL);", cn); cm.ExecuteNonQuery(); cm = new SQLiteCommand("SELECT CreateMbrCache('WorldGeom', 'Geometry');", cn); cm.ExecuteNonQuery(); // copy shape data to sqlite var shapeFile = appPath + @"\Data\world_countries_boundary_file_world_2002.shp"; var shp = new SharpMap.Data.Providers.ShapeFile(shapeFile); shp.Open(); FeatureDataSet ds = new FeatureDataSet(); shp.ExecuteIntersectionQuery(new SharpMap.Geometries.BoundingBox(double.MinValue, double.MinValue, double.MaxValue, double.MaxValue), ds); foreach (FeatureDataRow row in ds.Tables[0].Rows) { var bytes = SharpMap.Converters.WellKnownBinary.GeometryToWKB.Write(row.Geometry); cm = new SQLiteCommand("INSERT INTO WorldGeom (Geometry) VALUES (GeomFromWkb(@wkb, 4326))", cn); cm.Parameters.AddWithValue("@wkb", bytes); cm.ExecuteNonQuery(); cm = new SQLiteCommand("INSERT INTO WorldData (ISO2, ISO3, Name, Region, Area, Pop) VALUES (@iso2, @iso3, @name, @region, @area, @pop)", cn); cm.Parameters.AddWithValue("@iso2", row["ISO_2_CODE"]); cm.Parameters.AddWithValue("@iso3", row["ISO_3_CODE"]); cm.Parameters.AddWithValue("@name", row["NAME"]); cm.Parameters.AddWithValue("@region", row["REGION"]); cm.Parameters.AddWithValue("@area", row["AREA"]); cm.Parameters.AddWithValue("@pop", row["POP2005"]); cm.ExecuteNonQuery(); } shp.Close(); // anlyze table cm = new SQLiteCommand("ANALYZE WorldGeom;", cn); cm.ExecuteNonQuery(); cn.Close(); }
public static SharpMap.Map InitializeMap(float angle) { var dataSource = new SharpMap.Data.Providers.ShapeFile( string.Format("{0}/roads.shp", ShapefileSample.PathOsm), true); var fds = new SharpMap.Data.FeatureDataSet(); dataSource.Open(); dataSource.ExecuteIntersectionQuery(dataSource.GetExtents(), fds); dataSource.Close(); var gfp = new SharpMap.Data.Providers.GeometryFeatureProvider(fds.Tables[0]); var vl = new SharpMap.Layers.VectorLayer("roads", gfp) { CoordinateTransformation = LayerTools.Dhdn2ToWgs84 }; var ll = new SharpMap.Layers.LabelLayer("labels") { DataSource = gfp, CoordinateTransformation = LayerTools.Dhdn2ToWgs84, LabelColumn = "name", MultipartGeometryBehaviour = SharpMap.Layers.LabelLayer.MultipartGeometryBehaviourEnum.Largest, }; ll.Style.Halo = new System.Drawing.Pen(System.Drawing.Color.Red); //ll.Style.IgnoreLength = true; var map = new SharpMap.Map(); map.Layers.Add(vl); map.Layers.Add(ll); map.Layers.Add(vl); map.ZoomToExtents(); return map; }