Example #1
0
        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);
            }
        }
Example #2
0
        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);
            }
        }
Example #3
0
        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 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));
            }
        }