// Copyright (C) Stichting Deltares 2016. All rights reserved.
//
// This file is part of Ringtoets.
//
// Ringtoets 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 Core.Common.Base.Geometry;
using Core.Common.IO.Exceptions;
using Core.Common.Utils;
using Core.Common.Utils.Builders;
using Core.Components.Gis.Data;
using Core.Components.Gis.IO.Readers;
using Ringtoets.Common.IO.Properties;
using CoreCommonUtilsResources = Core.Common.Utils.Properties.Resources;
namespace Ringtoets.Common.IO.Structures
{
///
/// This class is responsible for reading structures for instances.
///
public class StructureLocationReader : IDisposable
{
private const string idAttributeName = "KWKIDENT";
private const string nameAttributeName = "KWKNAAM";
private readonly PointShapeFileReader pointsShapeFileReader;
///
/// Initializes a new instance of the class.
///
/// The shape file path.
/// Thrown when is invalid.
/// Thrown when:
/// - points to a file that does not exist.
/// - does not only contain point features.
/// - does not contain all of the required attributes.
///
public StructureLocationReader(string shapeFilePath)
{
FileUtils.ValidateFilePath(shapeFilePath);
if (!File.Exists(shapeFilePath))
{
string message = new FileReaderErrorMessageBuilder(shapeFilePath)
.Build(CoreCommonUtilsResources.Error_File_does_not_exist);
throw new CriticalFileReadException(message);
}
pointsShapeFileReader = OpenPointsShapeFile(shapeFilePath);
CheckRequiredAttributePresence(shapeFilePath);
}
///
/// Gets the number of structures present in the shapefile.
///
public int GetStructureCount
{
get
{
return pointsShapeFileReader.GetNumberOfFeatures();
}
}
///
/// Retrieve a based on the next point feature in the shapefile.
///
/// Thrown when either:
///
/// - The shapefile misses a value for a required attribute.
/// - The shapefile has an attribute whose type is incorrect.
///
/// A based on the next point feature in the shapefile.
public StructureLocation GetNextStructureLocation()
{
MapPointData mapPointData = (MapPointData) pointsShapeFileReader.ReadFeature();
IDictionary attributes = mapPointData.Features.First().MetaData;
string attributeIdValue = GetIdAttributeValue(attributes);
string attributeNameValue = GetNameAttributeValue(attributes, attributeIdValue);
Point2D point = mapPointData.Features.First().MapGeometries.First().PointCollections.First().First();
if (attributeIdValue == null)
{
throw new LineParseException(string.Format(Resources.StructuresReader_GetNextStructure_Invalid_KWKIDENT, idAttributeName));
}
return new StructureLocation(attributeIdValue, attributeNameValue, point);
}
public void Dispose()
{
pointsShapeFileReader.Dispose();
}
private static string GetIdAttributeValue(IDictionary attributes)
{
var attributeIdValue = attributes[idAttributeName] as string;
return attributeIdValue;
}
private string GetNameAttributeValue(IDictionary attributes, string defaultName)
{
if (!pointsShapeFileReader.HasAttribute(nameAttributeName))
{
return defaultName;
}
var attributeNameValue = attributes[nameAttributeName] as string;
return string.IsNullOrWhiteSpace(attributeNameValue) ? defaultName : attributeNameValue;
}
///
/// Open a shapefile containing structures as point features.
///
/// Shape file path.
/// Thrown when is invalid.
/// Thrown when:
///
/// - points to a file that doesn't exist.
/// - The shapefile has non-point geometries in it.
/// - An unexpected error occurred when reading the shapefile.
///
/// Return an instance of .
private static PointShapeFileReader OpenPointsShapeFile(string shapeFilePath)
{
try
{
return new PointShapeFileReader(shapeFilePath);
}
catch (CriticalFileReadException e)
{
if (e.InnerException is ApplicationException)
{
string message = new FileReaderErrorMessageBuilder(shapeFilePath)
.Build(Resources.PointShapefileReader_File_can_only_contain_points);
throw new CriticalFileReadException(message, e);
}
throw;
}
}
private void CheckRequiredAttributePresence(string shapeFilePath)
{
if (!pointsShapeFileReader.HasAttribute(idAttributeName))
{
string fullMessage = new FileReaderErrorMessageBuilder(shapeFilePath).Build(string.Format(Resources.ProfileLocationReader_CheckRequiredAttributePresence_Missing_attribute_0_, idAttributeName));
throw new CriticalFileReadException(fullMessage);
}
}
}
}