// Copyright (C) Stichting Deltares 2024. All rights reserved. // // This file is part of the application DAM - UI. // // DAM - UI is free software: you can redistribute it and/or modify // it under the terms of the GNU General Public License as published by // the Free Software Foundation, either version 3 of the License, or // (at your option) any later version. // // This program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. // // You should have received a copy of the GNU General Public License // along with this program. If not, see . // // All names, logos, and references to "Deltares" are registered trademarks of // Stichting Deltares and remain full property of Stichting Deltares at all times. // All rights reserved. using System; using System.Collections.Generic; using System.IO; using System.Linq; using NetTopologySuite.IO; using NetTopologySuite.IO.Esri; 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.DirectorySeparatorChar : 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"); } IEnumerable innerFeatures = repository.Features.Select(p => p.GetInnerFeature()); Shapefile.WriteAllFeatures(innerFeatures, path + name); } }