using System.Collections.Generic;
using System.IO;
using GeoAPI.Geometries;
using GisSharpBlog.NetTopologySuite.Geometries;
namespace GisSharpBlog.NetTopologySuite.IO.Handlers
{
///
/// Abstract class that defines the interfaces that other 'Shape' handlers must implement.
///
public abstract class ShapeHandler
{
protected int bbindex = 0;
protected double[] bbox;
protected ShapeGeometryType type;
protected IGeometry geom;
///
/// Returns the ShapeType the handler handles.
///
public abstract ShapeGeometryType ShapeType { get; }
///
/// Reads a stream and converts the shapefile record to an equilivent geometry object.
///
/// The stream to read.
/// The geometry factory to use when making the object.
/// The Geometry object that represents the shape file record.
public abstract IGeometry Read(BigEndianBinaryReader file, IGeometryFactory geometryFactory);
///
/// Writes to the given stream the equilivent shape file record given a Geometry object.
///
/// The geometry object to write.
/// The stream to write to.
/// The geometry factory to use.
public abstract void Write(IGeometry geometry, BinaryWriter file, IGeometryFactory geometryFactory);
///
/// Gets the length in bytes the Geometry will need when written as a shape file record.
///
/// The Geometry object to use.
/// The length in 16bit words the Geometry will use when represented as a shape file record.
public abstract int GetLength(IGeometry geometry);
///
///
///
///
///
public static IEnvelope GetEnvelopeExternal(IEnvelope envelope)
{
// Get envelope in external coordinates
ICoordinate min = new Coordinate(envelope.MinX, envelope.MinY);
ICoordinate max = new Coordinate(envelope.MaxX, envelope.MaxY);
IEnvelope bounds = new Envelope(min.X, max.X, min.Y, max.Y);
return bounds;
}
///
/// Get Envelope in external coordinates.
///
///
///
///
public static IEnvelope GetEnvelopeExternal(IPrecisionModel precisionModel, IEnvelope envelope)
{
return GetEnvelopeExternal(envelope);
}
///
///
///
///
///
public static bool HasZValue(ShapeGeometryType shapeType)
{
return shapeType == ShapeGeometryType.PointZ ||
shapeType == ShapeGeometryType.PointZM ||
shapeType == ShapeGeometryType.LineStringZ ||
shapeType == ShapeGeometryType.LineStringZM ||
shapeType == ShapeGeometryType.PolygonZ ||
shapeType == ShapeGeometryType.PolygonZM;
}
///
///
///
///
///
public static bool HasMValue(ShapeGeometryType shapeType)
{
return shapeType == ShapeGeometryType.PointM ||
shapeType == ShapeGeometryType.PointZM ||
shapeType == ShapeGeometryType.LineStringM ||
shapeType == ShapeGeometryType.LineStringZM ||
shapeType == ShapeGeometryType.PolygonM ||
shapeType == ShapeGeometryType.PolygonZM;
}
///
///
///
///
///
public static bool IsPoint(ShapeGeometryType shapeType)
{
return shapeType == ShapeGeometryType.Point ||
shapeType == ShapeGeometryType.PointZ ||
shapeType == ShapeGeometryType.PointM ||
shapeType == ShapeGeometryType.PointZM;
}
///
///
///
///
///
public static bool IsMultiPoint(ShapeGeometryType shapeType)
{
return shapeType == ShapeGeometryType.MultiPoint ||
shapeType == ShapeGeometryType.MultiPointZ ||
shapeType == ShapeGeometryType.MultiPointM ||
shapeType == ShapeGeometryType.MultiPointZM;
}
///
///
///
///
///
public static bool IsLineString(ShapeGeometryType shapeType)
{
return shapeType == ShapeGeometryType.LineString ||
shapeType == ShapeGeometryType.LineStringZ ||
shapeType == ShapeGeometryType.LineStringM ||
shapeType == ShapeGeometryType.LineStringZM;
}
///
///
///
///
///
public static bool IsPolygon(ShapeGeometryType shapeType)
{
return shapeType == ShapeGeometryType.Polygon ||
shapeType == ShapeGeometryType.PolygonZ ||
shapeType == ShapeGeometryType.PolygonM ||
shapeType == ShapeGeometryType.PolygonZM;
}
///
///
///
///
protected bool HasZValue()
{
return HasZValue(type);
}
///
///
///
///
protected bool HasMValue()
{
return HasMValue(type);
}
///
///
///
///
protected bool IsPoint()
{
return IsPoint(type);
}
///
///
///
///
protected bool IsMultiPoint()
{
return IsMultiPoint(type);
}
///
///
///
///
protected bool IsLineString()
{
return IsLineString(type);
}
///
///
///
///
protected bool IsPolygon()
{
return IsPolygon(type);
}
///
///
///
///
///
protected void GetZValue(BigEndianBinaryReader file, IDictionary data)
{
double z = file.ReadDouble();
// data.Add(ShapeGeometryType.PointZ, z);
}
///
///
///
///
///
protected void GetMValue(BigEndianBinaryReader file, IDictionary data)
{
double m = file.ReadDouble();
// data.Add(ShapeGeometryType.PointM, m);
}
protected void GrabZMValue(BigEndianBinaryReader file)
{
if (HasZValue() || HasMValue())
{
IDictionary data = new Dictionary(2);
if (HasZValue())
{
GetZValue(file, data);
}
if (HasMValue())
{
GetMValue(file, data);
}
// geom.UserData = data;
}
}
///
///
///
///
protected void GrabZMValues(BigEndianBinaryReader file)
{
if (HasZValue() || HasMValue())
{
IDictionary[] datas =
new Dictionary[geom.NumPoints];
if (HasZValue())
{
bbox[bbindex++] = file.ReadDouble();
bbox[bbindex++] = file.ReadDouble();
for (int i = 0; i < geom.NumPoints; i++)
{
if (datas[i] == null)
{
datas[i] = new Dictionary(2);
}
GetZValue(file, datas[i]);
}
}
if (HasMValue())
{
bbox[bbindex++] = file.ReadDouble();
bbox[bbindex++] = file.ReadDouble();
for (int i = 0; i < geom.NumPoints; i++)
{
if (datas[i] == null)
{
datas[i] = new Dictionary(2);
}
GetMValue(file, datas[i]);
}
}
// geom.UserData = datas;
}
}
///
///
///
///
protected int GetBoundingBoxLength()
{
bbindex = 0;
int bblength = 4;
if (HasZValue())
{
bblength += 2;
}
if (HasMValue())
{
bblength += 2;
}
return bblength;
}
}
}