Created
January 16, 2016 15:14
-
-
Save airbreather/d227a19c74799c6b45e4 to your computer and use it in GitHub Desktop.
My coord sequence... uses stuff from https://github.com/airbreather/AirBreather.Common and I don't feel like taking it out for this
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using AirBreather.Common.Utilities; | |
using GeoAPI.Geometries; | |
namespace TestingGround | |
{ | |
internal struct CoordVal : IEquatable<CoordVal> | |
{ | |
internal double X; | |
internal double Y; | |
internal CoordVal(double x, double y) | |
{ | |
this.X = x; | |
this.Y = y; | |
} | |
public static bool Equals(CoordVal first, CoordVal second) => first.X == second.X && first.Y == second.Y; | |
public static int GetHashCode(CoordVal val) => HashCodeUtility.Seed.HashWith(val.X).HashWith(val.Y); | |
public static string ToString(CoordVal val) => ToStringUtility.Begin(val).AddProperty(nameof(val.X), val.X).AddProperty(nameof(val.Y), val.Y).End(); | |
public override bool Equals(object obj) => obj is CoordVal && Equals(this, (CoordVal)obj); | |
public bool Equals(CoordVal other) => Equals(this, other); | |
public override int GetHashCode() => GetHashCode(this); | |
public override string ToString() => ToString(this); | |
} | |
public sealed class SuperCoordSeq : ICoordinateSequence | |
{ | |
private readonly CoordVal[] vals; | |
public SuperCoordSeq(int length) | |
{ | |
this.vals = length == 0 ? Array.Empty<CoordVal>() : new CoordVal[length]; | |
} | |
public SuperCoordSeq(ICoordinateSequence seq) | |
{ | |
SuperCoordSeq superSeq = seq as SuperCoordSeq; | |
if (superSeq != null) | |
{ | |
this.vals = (CoordVal[])superSeq.vals.Clone(); | |
return; | |
} | |
this.vals = new CoordVal[seq.Count]; | |
for (int i = 0; i < this.vals.Length; i++) | |
{ | |
this.vals[i].X = seq.GetX(i); | |
this.vals[i].Y = seq.GetY(i); | |
} | |
} | |
public SuperCoordSeq(Coordinate[] coords) | |
{ | |
this.vals = new CoordVal[coords.Length]; | |
for (int i = 0; i < this.vals.Length; i++) | |
{ | |
Coordinate coord = coords[i]; | |
this.vals[i].X = coord.X; | |
this.vals[i].Y = coord.Y; | |
} | |
} | |
private SuperCoordSeq(CoordVal[] vals) | |
{ | |
this.vals = vals; | |
} | |
public int Count => this.vals.Length; | |
public int Dimension => 2; | |
public Ordinates Ordinates => Ordinates.XY; | |
object ICloneable.Clone() => this.Clone(); | |
public SuperCoordSeq Clone() => new SuperCoordSeq((CoordVal[])this.vals.Clone()); | |
public Envelope ExpandEnvelope(Envelope env) | |
{ | |
foreach (var val in this.vals) | |
{ | |
env.ExpandToInclude(val.X, val.Y); | |
} | |
return env; | |
} | |
public Coordinate GetCoordinate(int i) => this.GetCoordinateCopy(i); | |
public Coordinate GetCoordinateCopy(int i) | |
{ | |
Coordinate c = new Coordinate(); | |
this.GetCoordinate(i, c); | |
return c; | |
} | |
public void GetCoordinate(int index, Coordinate coord) | |
{ | |
coord.X = this.vals[index].X; | |
coord.Y = this.vals[index].Y; | |
} | |
public double GetOrdinate(int index, Ordinate ordinate) | |
{ | |
switch (ordinate) | |
{ | |
case Ordinate.X: | |
return this.vals[index].X; | |
case Ordinate.Y: | |
return this.vals[index].Y; | |
} | |
throw new ArgumentOutOfRangeException(nameof(ordinate), ordinate, "Must be X or Y"); | |
} | |
public double GetX(int index) => this.vals[index].X; | |
public double GetY(int index) => this.vals[index].Y; | |
ICoordinateSequence ICoordinateSequence.Reversed() => this.Reversed(); | |
public SuperCoordSeq Reversed() | |
{ | |
var newVals = (CoordVal[])this.vals.Clone(); | |
Array.Reverse(newVals); | |
return new SuperCoordSeq(newVals); | |
} | |
public void SetOrdinate(int index, Ordinate ordinate, double value) | |
{ | |
switch (ordinate) | |
{ | |
case Ordinate.X: | |
this.vals[index].X = value; | |
return; | |
case Ordinate.Y: | |
this.vals[index].Y = value; | |
return; | |
} | |
throw new ArgumentOutOfRangeException(nameof(ordinate), ordinate, "Must be X or Y"); | |
} | |
public Coordinate[] ToCoordinateArray() => Array.ConvertAll(this.vals, val => new Coordinate(val.X, val.Y)); | |
} | |
public sealed class SuperCoordSeqFactory : ICoordinateSequenceFactory | |
{ | |
public Ordinates Ordinates => Ordinates.XY; | |
public ICoordinateSequence Create(ICoordinateSequence coordSeq) => new SuperCoordSeq(coordSeq); | |
public ICoordinateSequence Create(Coordinate[] coordinates) => new SuperCoordSeq(coordinates); | |
public ICoordinateSequence Create(int size, Ordinates ordinates) | |
{ | |
if (ordinates != Ordinates.XY) | |
{ | |
throw new ArgumentException("Must be XY", nameof(ordinates)); | |
} | |
return new SuperCoordSeq(size); | |
} | |
public ICoordinateSequence Create(int size, int dimension) | |
{ | |
if (dimension != 2) | |
{ | |
throw new ArgumentException("Must be 2", nameof(dimension)); | |
} | |
return new SuperCoordSeq(size); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment