// 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.DikeProfiles
{
///
/// This class is responsible for reading map locations for instances.
///
public class ProfileLocationReader : IDisposable
{
private const string idAttributeName = "ID";
private const string nameAttributeName = "Naam";
private const string offsetAttributeName = "X0";
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 ProfileLocationReader(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();
}
///
/// Gets the number of profile locations present in the shapefile.
///
public int GetLocationCount
{
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 ProfileLocation GetNextProfileLocation()
{
MapPointData mapPointData = (MapPointData) pointsShapeFileReader.ReadFeature();
IDictionary attributes = mapPointData.Features.First().MetaData;
string attributeIdValue = GetIdAttributeValue(attributes);
string attributeNameValue = GetNameAttributeValue(attributes);
double attributeX0Value = GetOffsetAttributeValue(attributes);
Point2D point = mapPointData.Features.First().MapGeometries.First().PointCollections.First().First();
try
{
return new ProfileLocation(attributeIdValue, attributeNameValue, attributeX0Value, point);
}
catch (ArgumentException exception)
{
throw new LineParseException(exception.Message);
}
}
public void Dispose()
{
pointsShapeFileReader.Dispose();
}
///
/// Open a shapefile containing dike locations 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.GetType() == typeof(ApplicationException))
{
string message = new FileReaderErrorMessageBuilder(shapeFilePath)
.Build(Resources.PointShapefileReader_File_can_only_contain_points);
throw new CriticalFileReadException(message, e);
}
throw;
}
}
private static double GetOffsetAttributeValue(IDictionary attributes)
{
var attributeX0Value = attributes[offsetAttributeName] as double?;
if (attributeX0Value == null)
{
throw new LineParseException(Resources.ProfileLocationReader_GetProfileLocations_Invalid_X0);
}
return attributeX0Value.Value;
}
private static string GetNameAttributeValue(IDictionary attributes)
{
var attributeNameValue = attributes[nameAttributeName] as string;
return attributeNameValue;
}
private static string GetIdAttributeValue(IDictionary attributes)
{
var attributeIdValue = attributes[idAttributeName] as string;
return attributeIdValue;
}
private void CheckRequiredAttributePresence()
{
IEnumerable requiredAttributes = new[]
{
idAttributeName,
nameAttributeName,
offsetAttributeName
};
foreach (string attribute in requiredAttributes)
{
if (!pointsShapeFileReader.HasAttribute(attribute))
{
throw new CriticalFileReadException(
string.Format(Resources.ProfileLocationReader_CheckRequiredAttributePresence_Missing_attribute_0_, attribute));
}
}
}
}
}