public void TestExecuteIntersectionQuery() { 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 fds = new SharpMap.Data.FeatureDataSet(); var bbox = shp.GetExtents(); //narrow it down bbox.ExpandBy(-0.425*bbox.Width, -0.425*bbox.Height); //Just to avoid that initial query does not impose performance penalty shp.DoTrueIntersectionQuery = false; shp.ExecuteIntersectionQuery(bbox, fds); fds.Tables.RemoveAt(0); //Perform query once more var sw = new System.Diagnostics.Stopwatch(); sw.Start(); shp.ExecuteIntersectionQuery(bbox, fds); sw.Stop(); System.Console.WriteLine("Queried using just envelopes:\n" + fds.Tables[0].Rows.Count + " in " + sw.ElapsedMilliseconds + "ms."); fds.Tables.RemoveAt(0); shp.DoTrueIntersectionQuery = true; sw.Reset(); sw.Start(); shp.ExecuteIntersectionQuery(bbox, fds); sw.Stop(); System.Console.WriteLine("Queried using prepared geometries:\n" + fds.Tables[0].Rows.Count + " in " + sw.ElapsedMilliseconds + "ms."); }
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 TestSetUp() { using (var sf = new SharpMap.Data.Providers.ShapeFile(GetTestDataFilePath("roads_ugl.shp"))) { sf.Open(); var fds = new SharpMap.Data.FeatureDataSet(); sf.ExecuteIntersectionQuery(sf.GetExtents(), fds); _provider = new SharpMap.Data.Providers.GeometryFeatureProvider(fds.Tables[0]); } }
public void TestSetUp() { using (var sf = new SharpMap.Data.Providers.ShapeFile(GetTestDataFilePath("roads_ugl.shp"))) { sf.Open(); var fds = new SharpMap.Data.FeatureDataSet(); sf.ExecuteIntersectionQuery(sf.GetExtents(), fds); _provider = new SharpMap.Data.Providers.GeometryFeatureProvider(fds.Tables[0]); } }
public void TestExecuteIntersectionQueryWithFilterDelegate() { 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 fds = new SharpMap.Data.FeatureDataSet(); var bbox = shp.GetExtents(); //narrow it down bbox.ExpandBy(-0.425 * bbox.Width, -0.425 * bbox.Height); //Just to avoid that initial query does not impose performance penalty shp.DoTrueIntersectionQuery = false; shp.FilterDelegate = JustTracks; shp.ExecuteIntersectionQuery(bbox, fds); fds.Tables.RemoveAt(0); //Perform query once more var sw = new System.Diagnostics.Stopwatch(); sw.Start(); shp.ExecuteIntersectionQuery(bbox, fds); sw.Stop(); System.Console.WriteLine("Queried using just envelopes:\n" + fds.Tables[0].Rows.Count + " in " + sw.ElapsedMilliseconds + "ms."); fds.Tables.RemoveAt(0); shp.DoTrueIntersectionQuery = true; sw.Reset(); sw.Start(); shp.ExecuteIntersectionQuery(bbox, fds); sw.Stop(); System.Console.WriteLine("Queried using prepared geometries:\n" + fds.Tables[0].Rows.Count + " in " + sw.ElapsedMilliseconds + "ms."); }
public void DoTest() { SharpMap.Data.Providers.ShapeFile sf = new SharpMap.Data.Providers.ShapeFile("cities.shp"); SharpMap.Data.FeatureDataSet fds = new SharpMap.Data.FeatureDataSet(); sf.ExecuteIntersectionQuery(sf.GetExtents(), fds); SharpMap.Data.Providers.FeatureProvider gfp = new SharpMap.Data.Providers.FeatureProvider(fds.Tables[0]); System.Data.DataTable dt = new System.Data.DataTable("external"); dt.Columns.Add("city", typeof(string)); dt.Columns.Add("males", typeof(int)); dt.Columns.Add("females", typeof(int)); }
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"; }
public void DoTest() { SharpMap.Data.Providers.ShapeFile sf = new SharpMap.Data.Providers.ShapeFile("cities.shp"); SharpMap.Data.FeatureDataSet fds = new SharpMap.Data.FeatureDataSet(); sf.ExecuteIntersectionQuery(sf.GetExtents(), fds); SharpMap.Data.Providers.FeatureProvider gfp = new SharpMap.Data.Providers.FeatureProvider(fds.Tables[0]); System.Data.DataTable dt = new System.Data.DataTable("external"); dt.Columns.Add("city", typeof (string)); dt.Columns.Add("males", typeof (int)); dt.Columns.Add("females", typeof (int)); }
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(); }
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; }