using System; using System.Collections.Generic; using System.IO; using System.Linq; using NetTopologySuite.IO; namespace Deltares.Maps { /// /// Wrapper class for writing repository data to ShapeFiles. /// public static class ShapeFileCreator { /// /// Creates the three (mandatory) files (.dbf, .shx, .shp) belonging to shape files /// and writes the feature data contained in the repository /// /// The output folder /// The name of the file/layer (without extension) /// The repository containing all data to be written to shapefiles /// /// If the files already exist they will be overriden /// /// /// /// When the name is empty or null. A valid name is required. /// /// /// When the repository does not contain features. /// /// /// When more then one geometry types is found in the repository /// When the repository contains types which are currently not supported: /// OgcGeometryType.Curve /// OgcGeometryType.MultiCurve /// OgcGeometryType.Surface /// OgcGeometryType.MultiSurface /// OgcGeometryType.Polygon /// OgcGeometryType.MultiPolygon /// OgcGeometryType.Geometry /// OgcGeometryType.GeometryCollection /// public static void Create(string path, string name, IFeatureRepository repository) { if (string.IsNullOrEmpty(name) || name.Trim() == string.Empty) { throw new ArgumentNullException("name"); } path = string.IsNullOrEmpty(path) || path.Trim() == "" || path.Trim() == "." ? Directory.GetCurrentDirectory() : path; if (!Directory.Exists(path)) { throw new DirectoryNotFoundException(path); } if (repository.Features.Count() == 0) { throw new ArgumentException( "The feature repository doesn't contain features. It should contain at least one feature"); } IFeature firstFeature = repository.Features.First(); var feature = new NetTopologySuite.Features.Feature(firstFeature.Geometry, firstFeature.Attributes); List coll = repository.Features .Select(f => new NetTopologySuite.Features.Feature(f.Geometry, f.Attributes)) .ToList(); DbaseFileHeader header = ShapefileDataWriter.GetHeader(feature, repository.Count); var writer = new ShapefileDataWriter(path + name) { Header = header }; writer.Write(coll); } } }