public void ReadByGeoFilter_ReadShapeDataAfterReaderObjectDisposed_ShouldThrowException() { Envelope boundsWithWholeTriangle = new Envelope(-1.17459, -1.00231, -1.09803, -0.80861); // Arrange. m_TempFiles = new TempFileCloudUploader[] { new TempFileCloudUploader("test.shp", ShpFiles.Read("UnifiedChecksMaterial")), new TempFileCloudUploader("test.dbf", DbfFiles.Read("UnifiedChecksMaterial")), }; m_shapeDataReader = new ShapeDataReader(new ShapefileStreamProviderRegistry(GetProvider(m_TempFiles[0].Path), GetProvider(m_TempFiles[1].Path))); // Act. IEnumerable <IShapefileFeature> results = m_shapeDataReader.ReadByMBRFilter(boundsWithWholeTriangle); // Assert. Assert.IsNotNull(results); IShapefileFeature result = results.Single(); // Dispose of the reader object. m_shapeDataReader.Dispose(); // Try reading dbf data. IGeometry table = result.Geometry; }
public void ReadByGeoFilter_ReadShapeDataAfterReaderObjectDisposed_ShouldThrowException() { Envelope boundsWithWholeTriangle = new Envelope(-1.17459, -1.00231, -1.09803, -0.80861); // Arrange. m_TempFiles = new TempFileWriter[] { new TempFileWriter(".shp", ShpFiles.Read("UnifiedChecksMaterial")), new TempFileWriter(".dbf", DbfFiles.Read("UnifiedChecksMaterial")), }; m_shapeDataReader = new ShapeDataReader(m_TempFiles[0].Path); // Act. IEnumerable <IShapefileFeature> results = m_shapeDataReader.ReadByMBRFilter(boundsWithWholeTriangle); // Assert. Assert.IsNotNull(results); IShapefileFeature result = results.Single(); // Dispose of the reader object. m_shapeDataReader.Dispose(); // Try reading dbf data. Assert.Catch <InvalidOperationException>(() => { IGeometry table = result.Geometry; }); }
public void ReadByGeoFilter_ReadAllInBounds_ShouldReturnAllShapesAndCorrectDbfData() { // Arrange. m_TempFiles = new TempFileWriter[] { new TempFileWriter(".shp", ShpFiles.Read("UnifiedChecksMaterial")), new TempFileWriter(".dbf", DbfFiles.Read("UnifiedChecksMaterial")), }; IPolygon[] expectedResult = new Polygon[] { new Polygon(new LinearRing(new Coordinate[] { new Coordinate(-0.815656565656566, -0.439393939393939), new Coordinate(-0.353535353535354, -0.795454545454545), new Coordinate(-0.888888888888889, -0.929292929292929), new Coordinate(-1.151515151515152, -0.419191919191919), new Coordinate(-0.815656565656566, -0.439393939393939), })), new Polygon(new LinearRing(new Coordinate[] { new Coordinate(0.068181818181818, 0.578282828282829), new Coordinate(0.421717171717172, 0.070707070707071), new Coordinate(-0.457070707070707, 0.080808080808081), new Coordinate(0.068181818181818, 0.578282828282829), })) }; string[] expectedShapeMetadata = new string[] { "Rectangle", "Triangle" }; m_shapeDataReader = new ShapeDataReader(m_TempFiles[0].Path); // Act. IEnumerable <IShapefileFeature> results = m_shapeDataReader.ReadByMBRFilter(m_shapeDataReader.ShapefileBounds); // Assert. Assert.IsNotNull(results); int currIndex = 0; foreach (IShapefileFeature result in results) { Assert.IsNotNull(result); Assert.IsInstanceOf <ShapefileFeature>(result); ShapefileFeature sf = (ShapefileFeature)result; Assert.AreEqual(sf.FeatureId, currIndex); Assert.IsNotNull(result.Attributes); HelperMethods.AssertPolygonsEqual(result.Geometry as IPolygon, expectedResult[currIndex]); object shapeNameData = result.Attributes["ShapeName"]; Assert.IsInstanceOf <string>(shapeNameData); Assert.AreEqual((string)shapeNameData, expectedShapeMetadata[currIndex++]); } }
/// <summary> /// Read each row in the file and sum up the value in each row for the attribute specified /// </summary> /// <param name="attributeName"></param> public void LogEachRow(string attributeName) { if (String.IsNullOrEmpty(attributeName) || String.IsNullOrWhiteSpace(attributeName)) { throw new InvalidOperationException("attributeName cant be null or empty"); } try { sum = 0; var mbr = reader.ShapefileBounds; var result = reader.ReadByMBRFilter(mbr); var coll = result.GetEnumerator(); while (coll.MoveNext()) { var item = coll.Current; try { foreach (string att in item.Attributes.GetNames()) { var obj = item.Attributes.GetOptionalValue(attributeName); ///We would like to have the value for each row logged if (obj is double num) { Logging($"[Item:{item.FeatureId}] contains numberic value of {num} for [Attribute:{attributeName}]"); sum += num; } else if (obj == null) { Logging($"[Item:{item.FeatureId}] does not contains a value of [Attribute:{attributeName}] "); } else { Logging($"[Item:{item.FeatureId}] contains a non-numberic value of {obj} for [Attribute:{attributeName}]. "); } } } catch (Exception) { Logging($"Error occurs while reading data from {item.FeatureId.ToString()}"); } } } catch (Exception) { throw; } }
public void ReadByGeoFilter_ReadWithRectangleMBRPartiallyInBounds_ShouldReturnRectangle() { Envelope boundsWithWholeTriangle = new Envelope(-1.17459, -1.00231, -1.09803, -0.80861); // Arrange. m_TempFiles = new TempFileWriter[] { new TempFileWriter(".shp", ShpFiles.Read("UnifiedChecksMaterial")), new TempFileWriter(".dbf", DbfFiles.Read("UnifiedChecksMaterial")), }; Polygon expectedTriangle = new Polygon(new LinearRing(new Coordinate[] { new Coordinate(-0.815656565656566, -0.439393939393939), new Coordinate(-0.353535353535354, -0.795454545454545), new Coordinate(-0.888888888888889, -0.929292929292929), new Coordinate(-1.151515151515152, -0.419191919191919), new Coordinate(-0.815656565656566, -0.439393939393939), })); string expectedShapeMetadata = "Rectangle"; m_shapeDataReader = new ShapeDataReader(m_TempFiles[0].Path); // Act. IEnumerable <IShapefileFeature> results = m_shapeDataReader.ReadByMBRFilter(boundsWithWholeTriangle); // Assert. Assert.IsNotNull(results); IShapefileFeature result = results.Single(); Assert.IsNotNull(result); Assert.IsInstanceOf <ShapefileFeature>(result); Assert.AreEqual(((ShapefileFeature)result).FeatureId, 0); Assert.IsNotNull(result.Attributes); HelperMethods.AssertPolygonsEqual(result.Geometry as IPolygon, expectedTriangle); object shapeNameData = result.Attributes["ShapeName"]; Assert.IsInstanceOf <string>(shapeNameData); Assert.AreEqual((string)shapeNameData, expectedShapeMetadata); }
public void ReadByGeoFilter_ReadWithWholeRectangleInBoundsAndFlagSetToTrue_ShouldReturnRectangle() { var boundsWithWholeTriangle = new Envelope(-1.39510, -0.12716, -1.13938, -0.22977); // Arrange. m_TempFiles = new TempFileWriter[] { new TempFileWriter(".shp", ShpFiles.Read("UnifiedChecksMaterial")), new TempFileWriter(".dbf", DbfFiles.Read("UnifiedChecksMaterial")), }; var expectedTriangle = new Polygon(new LinearRing(new Coordinate[] { new Coordinate(-0.815656565656566, -0.439393939393939), new Coordinate(-0.353535353535354, -0.795454545454545), new Coordinate(-0.888888888888889, -0.929292929292929), new Coordinate(-1.151515151515152, -0.419191919191919), new Coordinate(-0.815656565656566, -0.439393939393939), })); string expectedShapeMetadata = "Rectangle"; m_shapeDataReader = new ShapeDataReader(m_TempFiles[0].Path); // Act. var results = m_shapeDataReader.ReadByMBRFilter(boundsWithWholeTriangle, true); // Assert. Assert.IsNotNull(results); var result = results.Single(); Assert.IsNotNull(result); Assert.IsInstanceOf <ShapefileFeature>(result); Assert.AreEqual(((ShapefileFeature)result).FeatureId, 0); Assert.IsNotNull(result.Attributes); HelperMethods.AssertPolygonsEqual(result.Geometry as IPolygon, expectedTriangle); object shapeNameData = result.Attributes["ShapeName"]; Assert.IsInstanceOf <string>(shapeNameData); Assert.AreEqual((string)shapeNameData, expectedShapeMetadata); }
public void ReadByGeoFilter_ReadWithRectanglePartiallyInBoundsAndFlagSetToTrue_ShouldReturnRectangle() { Envelope boundsWithWholeTriangle = new Envelope(-0.93340, -0.38902, -0.73281, -0.29179); // Arrange. m_TempFiles = new TempFileCloudUploader[] { new TempFileCloudUploader("test.shp", ShpFiles.Read("UnifiedChecksMaterial")), new TempFileCloudUploader("test.dbf", DbfFiles.Read("UnifiedChecksMaterial")), }; Polygon expectedTriangle = new Polygon(new LinearRing(new Coordinate[] { new Coordinate(-0.815656565656566, -0.439393939393939), new Coordinate(-0.353535353535354, -0.795454545454545), new Coordinate(-0.888888888888889, -0.929292929292929), new Coordinate(-1.151515151515152, -0.419191919191919), new Coordinate(-0.815656565656566, -0.439393939393939), })); string expectedShapeMetadata = "Rectangle"; m_shapeDataReader = new ShapeDataReader(new ShapefileStreamProviderRegistry(GetProvider(m_TempFiles[0].Path), GetProvider(m_TempFiles[1].Path))); // Act. IEnumerable <IShapefileFeature> results = m_shapeDataReader.ReadByMBRFilter(boundsWithWholeTriangle, true); // Assert. Assert.IsNotNull(results); IShapefileFeature result = results.Single(); Assert.IsNotNull(result); Assert.IsInstanceOf <ShapefileFeature>(result); Assert.AreEqual(((ShapefileFeature)result).FeatureId, 0); Assert.IsNotNull(result.Attributes); HelperMethods.AssertPolygonsEqual(result.Geometry as IPolygon, expectedTriangle); object shapeNameData = result.Attributes["ShapeName"]; Assert.IsInstanceOf <string>(shapeNameData); Assert.AreEqual((string)shapeNameData, expectedShapeMetadata); }
public void ReadByGeoFilter_ReadWithWholeTriangleInBounds_ShouldReturnTriangle() { Envelope boundsWithWholeTriangle = new Envelope(-0.62331, 0.63774, -0.02304, 0.76942); // Arrange. m_TempFiles = new TempFileWriter[] { new TempFileWriter(".shp", ShpFiles.Read("UnifiedChecksMaterial")), new TempFileWriter(".dbf", DbfFiles.Read("UnifiedChecksMaterial")), }; Polygon expectedTriangle = new Polygon(new LinearRing(new Coordinate[] { new Coordinate(0.068181818181818, 0.578282828282829), new Coordinate(0.421717171717172, 0.070707070707071), new Coordinate(-0.457070707070707, 0.080808080808081), new Coordinate(0.068181818181818, 0.578282828282829), })); string expectedShapeMetadata = "Triangle"; m_shapeDataReader = new ShapeDataReader(m_TempFiles[0].Path); // Act. IEnumerable <IShapefileFeature> results = m_shapeDataReader.ReadByMBRFilter(boundsWithWholeTriangle); // Assert. Assert.IsNotNull(results); IShapefileFeature result = results.Single(); Assert.IsNotNull(result); Assert.IsInstanceOf <ShapefileFeature>(result); Assert.AreEqual(((ShapefileFeature)result).FeatureId, 1); Assert.IsNotNull(result.Attributes); HelperMethods.AssertPolygonsEqual(result.Geometry as IPolygon, expectedTriangle); object shapeNameData = result.Attributes["ShapeName"]; Assert.IsInstanceOf <string>(shapeNameData); Assert.AreEqual((string)shapeNameData, expectedShapeMetadata); }
public void ReadByGeoFilter_ReadWithNoShapeInBoundsAndFlagSetToTrue_ShouldReturnEmptyEnumerable() { Envelope boundsWithWholeTriangle = new Envelope(-1.17459, -1.00231, -1.09803, -1.5); // Arrange. m_TempFiles = new TempFileWriter[] { new TempFileWriter(".shp", ShpFiles.Read("UnifiedChecksMaterial")), new TempFileWriter(".dbf", DbfFiles.Read("UnifiedChecksMaterial")), }; m_shapeDataReader = new ShapeDataReader(m_TempFiles[0].Path); // Act. IEnumerable <IShapefileFeature> results = m_shapeDataReader.ReadByMBRFilter(boundsWithWholeTriangle, true); // Assert. Assert.IsNotNull(results); Assert.IsFalse(results.Any()); }
public void ReadByGeoFilter_ReadWithRectangleMBRPartiallyInBoundsAndFlagSetToTrue_ShouldReturnNoGeometries() { var boundsWithWholeTriangle = new Envelope(-1.17459, -1.00231, -1.09803, -0.80861); // Arrange. m_TempFiles = new TempFileWriter[] { new TempFileWriter(".shp", ShpFiles.Read("UnifiedChecksMaterial")), new TempFileWriter(".dbf", DbfFiles.Read("UnifiedChecksMaterial")), }; m_shapeDataReader = new ShapeDataReader(m_TempFiles[0].Path); // Act. var results = m_shapeDataReader.ReadByMBRFilter(boundsWithWholeTriangle, true); // Assert. Assert.IsNotNull(results); Assert.IsFalse(results.Any()); }
public static void ImportShapeFileIntoDatabase(string filePath) { ShapeDataReader reader = new ShapeDataReader(filePath); GeometryFactory factory = new GeometryFactory(); ShapefileDataReader shapeFileData = new ShapefileDataReader("", factory); DbaseFieldDescriptor[] fieldsInformation = shapeFileData.DbaseHeader.Fields; Envelope mb = reader.ShapefileBounds; IEnumerable <IShapefileFeature> result = reader.ReadByMBRFilter(mb); IAttributesTable columnAttributes = result.First().Attributes; IShapefileFeature[] rowInformation = result.ToArray(); //CREATE TABLE COLUMNS RunSqlQuery(CreateTableColumns(columnAttributes, fieldsInformation)); //INSERTING TABLE VALUES InsertTableValues(result.Count(), rowInformation).ForEach(x => RunSqlQuery(x)); }
public void ReadByGeoFilter_ReadWithNoShapeInBounds_ShouldReturnEmptyEnumerable() { Envelope boundsWithWholeTriangle = new Envelope(-1.17459, -1.00231, -1.09803, -1.5); // Arrange. m_TempFiles = new TempFileCloudUploader[] { new TempFileCloudUploader("test.shp", ShpFiles.Read("UnifiedChecksMaterial")), new TempFileCloudUploader("test.dbf", DbfFiles.Read("UnifiedChecksMaterial")), }; m_shapeDataReader = new ShapeDataReader(new ShapefileStreamProviderRegistry(GetProvider(m_TempFiles[0].Path), GetProvider(m_TempFiles[1].Path))); // Act. IEnumerable <IShapefileFeature> results = m_shapeDataReader.ReadByMBRFilter(boundsWithWholeTriangle); // Assert. Assert.IsNotNull(results); Assert.IsFalse(results.Any()); }
private void ConvertToSketchup(string path, string name) { // Read source shape file ShapeDataReader reader = new ShapeDataReader($"{Path.Combine(path, name)}.shp"); var mbr = reader.ShapefileBounds; var result = reader.ReadByMBRFilter(mbr); double bx; double by; var shapeFeature = result.ToList()[0]; bx = shapeFeature.BoundingBox.MinX; by = shapeFeature.BoundingBox.MinY; List <Coordinate> coordsForSketchup = new List <Coordinate>(); foreach (var co in shapeFeature.Geometry.Coordinates) { coordsForSketchup.Add(new Coordinate(co.X - bx, co.Y - by, 0)); } var csFactory = new CoordinateSystemFactory(); var ctFactory = new CoordinateTransformationFactory(); var osgb36 = csFactory.CreateFromWkt("PROJCS[\"OSGB 1936 / British National Grid\",GEOGCS[\"OSGB 1936\",DATUM[\"OSGB_1936\",SPHEROID[\"Airy 1830\",6377563.396,299.3249646,AUTHORITY[\"EPSG\",\"7001\"]],TOWGS84[446.448,-125.157,542.06,0.15,0.247,0.842,-20.489],AUTHORITY[\"EPSG\",\"6277\"]],PRIMEM[\"Greenwich\",0,AUTHORITY[\"EPSG\",\"8901\"]],UNIT[\"degree\",0.0174532925199433,AUTHORITY[\"EPSG\",\"9122\"]],AUTHORITY[\"EPSG\",\"4277\"]],PROJECTION[\"Transverse_Mercator\"],PARAMETER[\"latitude_of_origin\",49],PARAMETER[\"central_meridian\",-2],PARAMETER[\"scale_factor\",0.9996012717],PARAMETER[\"false_easting\",400000],PARAMETER[\"false_northing\",-100000],UNIT[\"metre\",1,AUTHORITY[\"EPSG\",\"9001\"]],AXIS[\"Easting\",EAST],AXIS[\"Northing\",NORTH],AUTHORITY[\"EPSG\",\"27700\"]]"); var wgs84 = csFactory.CreateFromWkt("GEOGCS[\"WGS 84\",DATUM[\"WGS_1984\",SPHEROID[\"WGS 84\",6378137,298.257223563,AUTHORITY[\"EPSG\",\"7030\"]],AUTHORITY[\"EPSG\",\"6326\"]],PRIMEM[\"Greenwich\",0,AUTHORITY[\"EPSG\",\"8901\"]],UNIT[\"degree\",0.0174532925199433,AUTHORITY[\"EPSG\",\"9122\"]],AUTHORITY[\"EPSG\",\"4326\"]]"); var transform = ctFactory.CreateFromCoordinateSystems(osgb36, wgs84); var lowerLeftOSGB = new Coordinate(bx, by); var lowerLeftWGS = transform.MathTransform.Transform(lowerLeftOSGB); // Create a sketchup file with the building outline in (Not yet georefed) SketchUpNET.SketchUp skp = new SketchUpNET.SketchUp(); skp.Latitude = lowerLeftWGS.Y; skp.Longitude = lowerLeftWGS.X; //skp.LoadModel("NewSquare2.skp"); // Nothing is initialised by default, and we need to init it all even stuff we don't use skp.Components = new System.Collections.Generic.Dictionary <string, SketchUpNET.Component>(); skp.Curves = new System.Collections.Generic.List <SketchUpNET.Curve>(); skp.Groups = new System.Collections.Generic.List <SketchUpNET.Group>(); skp.Instances = new System.Collections.Generic.List <SketchUpNET.Instance>(); skp.Materials = new System.Collections.Generic.Dictionary <string, SketchUpNET.Material>(); skp.Layers = new System.Collections.Generic.List <SketchUpNET.Layer>(); skp.Layers.Add(new SketchUpNET.Layer("Layer0")); skp.Edges = new System.Collections.Generic.List <SketchUpNET.Edge>(); for (int current = 0; current < (coordsForSketchup.Count - 1); current++) { skp.Edges.Add(new SketchUpNET.Edge( new SketchUpNET.Vertex(coordsForSketchup[current].X, coordsForSketchup[current].Y, 0), new SketchUpNET.Vertex(coordsForSketchup[current + 1].X, coordsForSketchup[current + 1].Y, 0), skp.Layers[0].Name )); } var surface = new SketchUpNET.Surface(); surface.Layer = skp.Layers[0].Name; surface.Normal = new SketchUpNET.Vector(0, 0, -1); surface.OuterEdges = new SketchUpNET.Loop(); surface.OuterEdges.Edges = new System.Collections.Generic.List <SketchUpNET.Edge>(); foreach (var edge in skp.Edges) { surface.OuterEdges.Edges.Add(edge); } surface.Vertices = new System.Collections.Generic.List <SketchUpNET.Vertex>(); for (int current = 0; current < (coordsForSketchup.Count - 1); current++) { surface.Vertices.Add(new SketchUpNET.Vertex(coordsForSketchup[current].X, coordsForSketchup[current].Y, 0)); } skp.Surfaces = new System.Collections.Generic.List <SketchUpNET.Surface>(); skp.Surfaces.Add(surface); skp.WriteNewModel("Temp.skp"); skp.SaveAs("Temp.skp", SketchUpNET.SKPVersion.V2017, $"{Path.Combine(path, name)}.skp"); File.Delete("Temp.skp"); }