/// <summary> /// Gets the reference line map data. /// </summary> /// <param name="lineShapeReader">The line shape reader.</param> /// <param name="shapeFilePath">The shapefile path.</param> /// <returns>The map data representing the reference line.</returns> /// <exception cref="CriticalFileReadException"> /// When either: /// <list type="bullet"> /// <item>There isn't exactly 1 polyline in the shapefile.</item> /// <item>The shapefile doesn't contains lines.</item> /// </list> /// </exception> private static MapLineData GetReferenceLineMapData(PolylineShapeFileReader lineShapeReader, string shapeFilePath) { if (lineShapeReader.GetNumberOfFeatures() != 1) { string message = new FileReaderErrorMessageBuilder(shapeFilePath) .Build(RiskeerCommonIOResources.ReferenceLineReader_File_must_contain_1_polyline); throw new CriticalFileReadException(message); } return((MapLineData)lineShapeReader.ReadFeature(RiskeerCommonDataResources.ReferenceLine_DisplayName)); }
/// <summary> /// Validates the file by checking if all mandatory attributes are present in the shape file. /// </summary> /// <param name="polylineShapeFileReader">The opened shape file reader.</param> /// <param name="shapeFilePath">The file path to the shape file.</param> /// <exception cref="CriticalFileReadException">Thrown when the shape file lacks one of the mandatory attributes.</exception> private static void ValidateExistenceOfRequiredAttributes(PolylineShapeFileReader polylineShapeFileReader, string shapeFilePath) { IEnumerable <string> missingAttributes = GetMissingAttributes(polylineShapeFileReader); if (missingAttributes.Any()) { string message = string.Format(RiskeerCommonIOResources.ReferenceLinesMetaReader_File_0_lacks_required_Attribute_1_, shapeFilePath, string.Join("', '", missingAttributes)); throw new CriticalFileReadException(message); } }
/// <summary> /// Reads the current features in the shape file into a collection of <see cref="ReferenceLineMeta"/> objects. /// </summary> /// <param name="shapeFilePath">The file path to the shape file.</param> /// <returns>The created collection of <see cref="ReferenceLineMeta"/> objects.</returns> /// <exception cref="ArgumentException">Thrown when <paramref name="shapeFilePath"/> is invalid.</exception> /// <exception cref="CriticalFileReadException">Thrown when current feature in the shape file: /// <list type="bullet"> /// <item><paramref name="shapeFilePath"/> points to a file that does not exist.</item> /// <item>The shape file does not contain the mandatory attributes.</item> /// <item>Has an empty <see cref="assessmentsectionIdAttributeKey"/> attribute.</item> /// <item>The shape file has non-line geometries in it.</item> /// </list></exception> public static IEnumerable <ReferenceLineMeta> ReadReferenceLinesMetas(string shapeFilePath) { ValidateFilePath(shapeFilePath); using (PolylineShapeFileReader reader = OpenPolyLineShapeFile(shapeFilePath)) { ValidateExistenceOfRequiredAttributes(reader, shapeFilePath); return(ReadReferenceLinesMetas(reader)); } }
public void ParameteredConstructor_ExpectedValues() { // Setup string testFilePath = TestHelper.GetTestDataPath(TestDataPath.Core.Components.Gis.IO, "traject_10-1.shp"); // Call using (var reader = new PolylineShapeFileReader(testFilePath)) { // Assert Assert.IsInstanceOf <ShapeFileReaderBase>(reader); } }
public void ReadShapeFile_WithoutName_MapDataHasDefaultName(string name) { // Setup string shapeWithOneLine = TestHelper.GetTestDataPath(TestDataPath.Core.Components.Gis.IO, "traject_10-1.shp"); using (var reader = new PolylineShapeFileReader(shapeWithOneLine)) { // Call var line = (MapLineData)reader.ReadShapeFile(name); // Assert Assert.AreEqual("Lijn", line.Name); } }
public void ReadLine_WithName_ApplyNameToMapData(string name) { // Setup string shapeWithOneLine = TestHelper.GetTestDataPath(TestDataPath.Core.Components.Gis.IO, "traject_10-1.shp"); using (var reader = new PolylineShapeFileReader(shapeWithOneLine)) { // Call var line = (MapLineData)reader.ReadFeature(name); // Assert Assert.AreEqual(name, line.Name); } }
public void HasAttribute_VariousCases_ReturnTrueIfMatchesInProperCaseHasBeenFound(string attributeName, bool expectedResult) { // Setup string shapefileFilePath = TestHelper.GetTestDataPath(TestDataPath.Core.Components.Gis.IO, "Multiple_PolyLine_with_ID.shp"); using (var reader = new PolylineShapeFileReader(shapefileFilePath)) { // Call bool result = reader.HasAttribute(attributeName); // Assert Assert.AreEqual(expectedResult, result); } }
public void GetNumberOfLines_EmptyLineShapeFile_ReturnZero() { // Setup string shapeWithOneLine = TestHelper.GetTestDataPath(TestDataPath.Core.Components.Gis.IO, "Empty_PolyLine_with_ID.shp"); using (var reader = new PolylineShapeFileReader(shapeWithOneLine)) { // Call int count = reader.GetNumberOfFeatures(); // Assert Assert.AreEqual(0, count); } }
public void GetNumberOfLines_ShapeFileWithMultipleLineFeatures_ReturnThatNumberOfFeatures() { // Setup string shapeWithMultipleLines = TestHelper.GetTestDataPath(TestDataPath.Core.Components.Gis.IO, "Multiple_PolyLine_with_ID.shp"); using (var reader = new PolylineShapeFileReader(shapeWithMultipleLines)) { // Call int count = reader.GetNumberOfFeatures(); // Assert Assert.AreEqual(4, count); } }
public void HasAttribute_AttributeInShapefile_ReturnTrue() { // Setup string shapefileFilePath = TestHelper.GetTestDataPath(TestDataPath.Core.Components.Gis.IO, "Multiple_PolyLine_with_ID.shp"); using (var reader = new PolylineShapeFileReader(shapefileFilePath)) { // Call bool result = reader.HasAttribute("id"); // Assert Assert.IsTrue(result); } }
public void GetNumberOfLines_ShapeFileWithOneLineFeature_ReturnOne() { // Setup string shapeWithOneLine = TestHelper.GetTestDataPath(TestDataPath.Core.Components.Gis.IO, "traject_10-1.shp"); using (var reader = new PolylineShapeFileReader(shapeWithOneLine)) { // Call int count = reader.GetNumberOfFeatures(); // Assert Assert.AreEqual(1, count); } }
/// <summary> /// Reads an instance of <see cref="ReferenceLine"/> from a shapefile containing one polyline. /// </summary> /// <param name="shapeFilePath">The file path to the shapefile.</param> /// <returns>The reference line created from the data in the shapefile.</returns> /// <exception cref="ArgumentException">Thrown when <paramref name="shapeFilePath"/> is invalid.</exception> /// <exception cref="CriticalFileReadException">Thrown when either: /// <list type="bullet"> /// <item><paramref name="shapeFilePath"/> points to a file that does not exist.</item> /// <item>There isn't exactly 1 polyline in the shapefile.</item> /// <item>Shapefile contains a multi-polyline.</item> /// <item>An unexpected error occurred when reading the shapefile.</item> /// </list> /// </exception> public ReferenceLine ReadReferenceLine(string shapeFilePath) { IOUtils.ValidateFilePath(shapeFilePath); if (!File.Exists(shapeFilePath)) { string message = new FileReaderErrorMessageBuilder(shapeFilePath) .Build(CoreCommonUtilResources.Error_File_does_not_exist); throw new CriticalFileReadException(message); } using (var lineShapeReader = new PolylineShapeFileReader(shapeFilePath)) { MapLineData lineMapData = GetReferenceLineMapData(lineShapeReader, shapeFilePath); return(CreateReferenceLine(lineMapData, shapeFilePath)); } }
private static IEnumerable <ReferenceLineMeta> ReadReferenceLinesMetas(PolylineShapeFileReader reader) { var referenceLinesMetas = new List <ReferenceLineMeta>(); ReferenceLineMeta referenceLinesMeta; do { referenceLinesMeta = ReadReferenceLinesMeta(reader); if (referenceLinesMeta != null) { referenceLinesMetas.Add(referenceLinesMeta); } } while (referenceLinesMeta != null); return(referenceLinesMetas); }
private static IEnumerable <string> GetMissingAttributes(PolylineShapeFileReader polylineShapeFileReader) { if (!polylineShapeFileReader.HasAttribute(assessmentsectionIdAttributeKey)) { yield return(assessmentsectionIdAttributeKey); } if (!polylineShapeFileReader.HasAttribute(signalFloodingProbabilityValueAttributeKey)) { yield return(signalFloodingProbabilityValueAttributeKey); } if (!polylineShapeFileReader.HasAttribute(maximumAllowableFloodingProbabilityValueAttributeKey)) { yield return(maximumAllowableFloodingProbabilityValueAttributeKey); } }
public void ReadLine_WhenAtEndOfShapeFile_ReturnNull(string shapeFileName) { // Setup string linesShapefileFilePath = TestHelper.GetTestDataPath(TestDataPath.Core.Components.Gis.IO, shapeFileName); using (var reader = new PolylineShapeFileReader(linesShapefileFilePath)) { for (var i = 0; i < reader.GetNumberOfFeatures(); i++) { reader.ReadFeature(); } // Call var line = reader.ReadFeature() as MapLineData; // Assert Assert.IsNull(line); } }
public void ReadShapeFile_ShapeFileWithOneLineFeature_ReturnShape() { // Setup string shapeWithOneLine = TestHelper.GetTestDataPath(TestDataPath.Core.Components.Gis.IO, "traject_10-1.shp"); using (var reader = new PolylineShapeFileReader(shapeWithOneLine)) { // Call var line = (MapLineData)reader.ReadShapeFile(); // Assert Assert.IsNotNull(line); MapFeature[] features = line.Features.ToArray(); Assert.AreEqual(1, features.Length); MapGeometry[] geometries = features[0].MapGeometries.ToArray(); Assert.AreEqual(1, geometries.Length); IEnumerable <Point2D>[] pointCollections = geometries[0].PointCollections.ToArray(); Assert.AreEqual(1, pointCollections.Length); Point2D[] firstPointCollection = pointCollections[0].ToArray(); Assert.AreEqual(1669, firstPointCollection.Length); Assert.AreEqual(202714.219, firstPointCollection[457].X, 1e-6); Assert.AreEqual(507775.781, firstPointCollection[457].Y, 1e-6); Assert.AreEqual(6, features[0].MetaData.Count); Assert.AreEqual("A", features[0].MetaData["CATEGORIE"]); Assert.AreEqual("10", features[0].MetaData["DIJKRING"]); Assert.AreEqual(19190.35000000, features[0].MetaData["LENGTE_TRJ"]); Assert.AreEqual("1:1000", features[0].MetaData["Ondergrens"]); Assert.AreEqual("1:3000", features[0].MetaData["Signalerin"]); Assert.AreEqual("10-1", features[0].MetaData["TRAJECT_ID"]); Assert.AreEqual("CATEGORIE", line.SelectedMetaDataAttribute); } }
private static MapLineData ReadMapLineData(PolylineShapeFileReader polylineShapeFileReader) { return((MapLineData)polylineShapeFileReader.ReadFeature()); }
public void ReadShapeFile_ShapeFileWithMultipleLineFeatures_ReturnShapes() { // Setup string shapeWithMultipleLines = TestHelper.GetTestDataPath(TestDataPath.Core.Components.Gis.IO, "Multiple_PolyLine_with_ID.shp"); using (var reader = new PolylineShapeFileReader(shapeWithMultipleLines)) { // Precondition Assert.AreEqual(4, reader.GetNumberOfFeatures()); // Call var lines = (MapLineData)reader.ReadShapeFile(); // Assert MapFeature[] features = lines.Features.ToArray(); Assert.AreEqual(4, features.Length); Assert.AreEqual(1, lines.MetaData.Count()); Assert.AreEqual("id", lines.SelectedMetaDataAttribute); #region Assertions for 'line1' MapGeometry[] geometries1 = features[0].MapGeometries.ToArray(); Assert.AreEqual(1, geometries1.Length); IEnumerable <Point2D>[] line1PointCollections = geometries1[0].PointCollections.ToArray(); Assert.AreEqual(1, line1PointCollections.Length); Point2D[] line1FirstPointCollection = line1PointCollections[0].ToArray(); Assert.AreEqual(15, line1FirstPointCollection.Length); Assert.AreEqual(-1.514151, line1FirstPointCollection[2].X, 1e-6); Assert.AreEqual(-0.879717, line1FirstPointCollection[2].Y, 1e-6); Assert.AreEqual(1, features[0].MetaData.Count); Assert.AreEqual(4, features[0].MetaData["id"]); #endregion #region Assertions for 'line2' MapGeometry[] geometries2 = features[1].MapGeometries.ToArray(); Assert.AreEqual(1, geometries2.Length); IEnumerable <Point2D>[] line2PointCollections = geometries2[0].PointCollections.ToArray(); Assert.AreEqual(1, line2PointCollections.Length); Point2D[] line2FirstPointCollection = line2PointCollections[0].ToArray(); Assert.AreEqual(6, line2FirstPointCollection.Length); Assert.AreEqual(-2.028302, line2FirstPointCollection[3].X, 1e-6); Assert.AreEqual(-0.382075, line2FirstPointCollection[3].Y, 1e-6); Assert.AreEqual(1, features[1].MetaData.Count); Assert.AreEqual(3, features[1].MetaData["id"]); #endregion #region Assertions for 'line3' MapGeometry[] geometries3 = features[2].MapGeometries.ToArray(); Assert.AreEqual(1, geometries3.Length); IEnumerable <Point2D>[] line3PointCollections = geometries3[0].PointCollections.ToArray(); Assert.AreEqual(1, line3PointCollections.Length); Point2D[] line3FirstPointCollection = line3PointCollections[0].ToArray(); Assert.AreEqual(13, line3FirstPointCollection.Length); Assert.AreEqual(0.891509, line3FirstPointCollection[12].X, 1e-6); Assert.AreEqual(-0.122641, line3FirstPointCollection[12].Y, 1e-6); Assert.AreEqual(1, features[2].MetaData.Count); Assert.AreEqual(2, features[2].MetaData["id"]); #endregion #region Assertions for 'line4' MapGeometry[] geometries4 = features[3].MapGeometries.ToArray(); Assert.AreEqual(1, geometries4.Length); IEnumerable <Point2D>[] line4PointCollections = geometries4[0].PointCollections.ToArray(); Assert.AreEqual(1, line4PointCollections.Length); Point2D[] line4FirstPointCollection = line4PointCollections[0].ToArray(); Assert.AreEqual(6, line4FirstPointCollection.Length); Assert.AreEqual(-2.070754, line4FirstPointCollection[0].X, 1e-6); Assert.AreEqual(0.73584906, line4FirstPointCollection[0].Y, 1e-6); Assert.AreEqual(1, features[3].MetaData.Count); Assert.AreEqual(1, features[3].MetaData["id"]); #endregion } }
private static ReferenceLineMeta ReadReferenceLinesMeta(PolylineShapeFileReader reader) { MapLineData lineData = ReadMapLineData(reader); return(lineData == null ? null : CreateReferenceLineMeta(lineData)); }
private ReadResult <FeatureBasedMapData> ReadFeatureBasedMapData() { try { string shapeFileName = Path.GetFileNameWithoutExtension(FilePath); Shapefile featureSet = Shapefile.OpenFile(FilePath); FeatureBasedMapData importedData; switch (featureSet.FeatureType) { case FeatureType.Point: case FeatureType.MultiPoint: using (ShapeFileReaderBase reader = new PointShapeFileReader(FilePath)) { importedData = reader.ReadShapeFile(shapeFileName); } break; case FeatureType.Line: using (ShapeFileReaderBase reader = new PolylineShapeFileReader(FilePath)) { importedData = reader.ReadShapeFile(shapeFileName); } break; case FeatureType.Polygon: using (ShapeFileReaderBase reader = new PolygonShapeFileReader(FilePath)) { importedData = reader.ReadShapeFile(shapeFileName); } break; default: throw new CriticalFileReadException(Resources.FeatureBasedMapDataImporter_Import_ShapeFile_Contains_Unsupported_Data); } return(new ReadResult <FeatureBasedMapData>(false) { Items = new[] { importedData } }); } catch (ArgumentException) { return(HandleCriticalFileReadError(Resources.FeatureBasedMapDataImporter_Import_File_does_not_contain_geometries)); } catch (FileNotFoundException) { return(HandleCriticalFileReadError(Resources.FeatureBasedMapDataImporter_Import_File_does_not_exist_or_misses_needed_files)); } catch (IOException) { return(HandleCriticalFileReadError(Resources.FeatureBasedMapDataImporter_Import_An_error_occurred_when_trying_to_read_the_file)); } catch (CriticalFileReadException e) { return(HandleCriticalFileReadError(e.Message)); } catch (Exception) { // Because NullReferenceException or NotImplementedException when reading in a corrupt shape file // from a third party library is expected, we catch all the exceptions here. return(HandleCriticalFileReadError(Resources.FeatureBasedMapDataImporter_Import_An_error_occurred_when_trying_to_read_the_file)); } }
/// <summary> /// Initializes a new instance of the <see cref="FailureMechanismSectionReader"/> class. /// </summary> /// <param name="shapeFilePath">The shape file path.</param> /// <exception cref="ArgumentException"><paramref name="shapeFilePath"/> is invalid.</exception> /// <exception cref="CriticalFileReadException">Thrown when either: /// <list type="bullet"> /// <item><paramref name="shapeFilePath"/> points to a file that doesn't exist.</item> /// <item>The shapefile has non-line geometries in it.</item> /// <item>An unexpected error occurred when reading the shapefile.</item> /// </list> /// </exception> public FailureMechanismSectionReader(string shapeFilePath) { IOUtils.ValidateFilePath(shapeFilePath); filePath = shapeFilePath; polylineShapeFileReader = new PolylineShapeFileReader(filePath); }