using System;
using System.Collections.Generic;
using NetTopologySuite.Geometries;
using NetTopologySuite.IO;
namespace Deltares.Maps
{
///
/// Reads data from ShapeFiles
///
///
///
public class ShapeFileReader : FileReader, IFeatureReader
{
public ShapeFileReader(IFile file)
: base(file)
{
}
public ShapeFileReader(string fileName)
: base(new ShapeFileLocation(fileName))
{
}
///
/// Gets or sets the value indicating geometry attribute or feature data should be read
/// If set to false the attribute data will be read form the .dbf file (default)
///
public bool IgnoreAttributeData { get; set; }
#region IFeatureReader Members
public override IEnumerable Read()
{
using (var reader = new ShapefileDataReader(File.FullPath, GeometryFactory.Default))
{
int count = 0;
while (reader.Read())
{
var feature = new NtsFeature
{
Geometry = reader.Geometry
};
if (!IgnoreAttributeData)
{
for (int i = 0; i < reader.DbaseHeader.NumFields; i++)
{
string colName = reader.DbaseHeader.Fields[i].Name;
object rowValue = reader.GetValue(i);
if (!feature.Attributes.Exists(colName))
{
feature.Attributes.AddAttribute(colName, rowValue);
}
}
}
count++;
yield return feature;
}
if (reader.RecordCount != count)
{
string message = string.Format(
"The shape file '{0}' contains multiple records but only one (composite) shape. " +
"This is not yet supported. The current implementation of the shape file reader only works " +
"with shape files where each shape has exactly one database record.",
File.FullPath);
throw new NotSupportedException(message);
}
}
}
#endregion
}
}