private static void DoTestCopy(CoordinateSequenceFactory factory, int dimension) { // arrange var sequence = CreateSequenceFromOrdinates(factory, dimension); if (sequence.Count <= 7) { TestContext.WriteLine("sequence has a size of " + sequence.Count + ". Execution of this test needs a sequence " + "with more than 6 coordinates."); return; } var fullCopy = factory.Create(sequence.Count, dimension, 0); var partialCopy = factory.Create(sequence.Count - 5, dimension, 0); // act CoordinateSequences.Copy(sequence, 0, fullCopy, 0, sequence.Count); CoordinateSequences.Copy(sequence, 2, partialCopy, 0, partialCopy.Count); // assert for (int i = 0; i < fullCopy.Count; i++) { CheckCoordinateAt(sequence, i, fullCopy, i, dimension); } for (int i = 0; i < partialCopy.Count; i++) { CheckCoordinateAt(sequence, 2 + i, partialCopy, i, dimension); } // ToDo test if dimensions don't match }
private void CheckDim4_M1(CoordinateSequenceFactory factory) { var seq = factory.Create(5, 4, 1); InitProgression(seq); Assert.AreEqual(4, seq.Dimension, "Dimension should be 4"); Assert.IsTrue(seq.HasZ, "Z should be present"); Assert.IsTrue(seq.HasM, "M should be present"); var coord = seq.GetCoordinate(4); Assert.IsTrue(coord is CoordinateZM); var coordZM = (CoordinateZM)coord; Assert.AreEqual(4.0, coord.X); Assert.AreEqual(4.0, coord.Y); Assert.AreEqual(4.0, coordZM.Z); Assert.AreEqual(4.0, coordZM.M); var array = seq.ToCoordinateArray(); Assert.AreEqual(coord, array[4]); Assert.IsTrue(coord != array[4]); Assert.IsTrue(IsEqual(seq, array)); var copy = factory.Create(array); Assert.IsTrue(IsEqual(copy, array)); var copy2 = factory.Create(seq); Assert.IsTrue(IsEqual(copy2, array)); }
private void CheckDim3(CoordinateSequenceFactory factory) { var seq = factory.Create(5, 3, 0); InitProgression(seq); Assert.AreEqual(3, seq.Dimension, "Dimension should be 3"); Assert.IsTrue(seq.HasZ, "Z should be present"); Assert.IsTrue(!seq.HasM, "M should not be present"); var coord = seq.GetCoordinate(4); Assert.IsTrue(coord.GetType() == typeof(CoordinateZ)); var coordZ = (CoordinateZ)coord; Assert.AreEqual(4.0, coord.X); Assert.AreEqual(4.0, coord.Y); Assert.AreEqual(4.0, coordZ.Z); var array = seq.ToCoordinateArray(); Assert.AreEqual(coord, array[4]); Assert.IsTrue(coord != array[4]); Assert.IsTrue(IsEqual(seq, array)); var copy = factory.Create(array); Assert.IsTrue(IsEqual(copy, array)); var copy2 = factory.Create(seq); Assert.IsTrue(IsEqual(copy2, array)); }
public void CheckDim4(CoordinateSequenceFactory factory) { var seq = factory.Create(5, 4, 0); InitProgression(seq); Assert.AreEqual(4, seq.Dimension, "Dimension should be 4"); Assert.IsTrue(seq.HasZ, "Z should be present"); Assert.IsTrue(!seq.HasM, "M should not be present"); var coord = seq.GetCoordinate(4); Assert.AreEqual(4.0, coord.X); Assert.AreEqual(4.0, coord.Y); Assert.AreEqual(4.0, coord.Z); Assert.AreEqual(4.0, coord[Ordinate.Spatial4]); var array = seq.ToCoordinateArray(); Assert.AreEqual(coord, array[4]); Assert.IsTrue(coord != array[4]); Assert.IsTrue(IsEqual(seq, array)); var copy = factory.Create(array); Assert.IsTrue(IsEqual(copy, array)); var copy2 = factory.Create(seq); Assert.IsTrue(IsEqual(copy2, array)); }
private void CheckDim2(int size, CoordinateSequenceFactory factory) { var seq = factory.Create(size, 2, 0); InitProgression(seq); Assert.AreEqual(2, seq.Dimension, "Dimension should be 2"); Assert.IsTrue(!seq.HasZ, "Z should not be present"); Assert.IsTrue(!seq.HasM, "M should not be present"); int indexLast = size - 1; double valLast = indexLast; var coord = seq.GetCoordinate(indexLast); Assert.IsTrue(coord.GetType() == typeof(Coordinate)); Assert.AreEqual(valLast, coord.X); Assert.AreEqual(valLast, coord.Y); var array = seq.ToCoordinateArray(); Assert.AreEqual(coord, array[indexLast]); Assert.IsTrue(coord != array[indexLast]); Assert.IsTrue(IsEqual(seq, array)); var copy = factory.Create(array); Assert.IsTrue(IsEqual(copy, array)); var copy2 = factory.Create(seq); Assert.IsTrue(IsEqual(copy2, array)); }
private static CoordinateSequence InsertTopologyExceptionPoint(Coordinate coord, CoordinateSequence seq, CoordinateSequenceFactory factory) { var res = factory.Create(2 * seq.Count, seq.Ordinates); if (Replace(seq.GetCoordinate(0), coord)) { res.SetOrdinate(0, Ordinate.X, coord.X); res.SetOrdinate(0, Ordinate.Y, coord.Y); res.SetOrdinate(0, Ordinate.Z, coord.Z); } else { res.SetOrdinate(0, Ordinate.X, seq.GetOrdinate(0, Ordinate.X)); res.SetOrdinate(0, Ordinate.Y, seq.GetOrdinate(0, Ordinate.Y)); res.SetOrdinate(0, Ordinate.Z, seq.GetOrdinate(0, Ordinate.Z)); } var last = res.GetCoordinate(0); int off = 0; for (int i = 1; i < seq.Count; i++) { var curr = seq.GetCoordinate(i); bool add = true; if (Replace(curr, coord)) { res.SetOrdinate(i + off, Ordinate.X, coord.X); res.SetOrdinate(i + off, Ordinate.Y, coord.Y); res.SetOrdinate(i + off, Ordinate.Z, coord.Z); add = false; } else if (last.Distance(coord) + coord.Distance(curr) <= 1.000000000000000000001 * last.Distance(curr)) { res.SetOrdinate(i + off, Ordinate.X, coord.X); res.SetOrdinate(i + off, Ordinate.Y, coord.Y); res.SetOrdinate(i + off, Ordinate.Z, coord.Z); off += 1; } if (add) { res.SetOrdinate(i + off, Ordinate.X, seq.GetOrdinate(i, Ordinate.X)); res.SetOrdinate(i + off, Ordinate.Y, seq.GetOrdinate(i, Ordinate.Y)); res.SetOrdinate(i + off, Ordinate.Z, seq.GetOrdinate(i, Ordinate.Z)); } last = curr; } var tmp = factory.Create(seq.Count + off, seq.Ordinates); CoordinateSequences.Copy(res, 0, tmp, 0, tmp.Count); return(tmp); }
private static Geometry CreatePoint(Ordinates ordinates, bool empty) { if (empty) { return(Factory.CreatePoint((CoordinateSequence)null)); } var seq = CsFactory.Create(1, ordinates); foreach (var o in ToOrdinateArray(ordinates)) { seq.SetOrdinate(0, o, RandomOrdinate(o, Factory.PrecisionModel)); } return(Factory.CreatePoint(seq)); }
private static CoordinateSequence CreateCircularString(CoordinateSequenceFactory factory, int dimension, Coordinate center, double radius, double startAngle, int numPoints) { const int numSegmentsCircle = 48; const double angleCircle = 2 * Math.PI; const double angleStep = angleCircle / numSegmentsCircle; var sequence = factory.Create(numPoints, dimension, 0); var pm = new PrecisionModel(100); double angle = startAngle; for (int i = 0; i < numPoints; i++) { double dx = Math.Cos(angle) * radius; sequence.SetOrdinate(i, Ordinate.X, pm.MakePrecise(center.X + dx)); double dy = Math.Sin(angle) * radius; sequence.SetOrdinate(i, Ordinate.Y, pm.MakePrecise(center.Y + dy)); // set other ordinate values to predictable values for (int j = 2; j < dimension; j++) { sequence.SetOrdinate(i, j, Math.Pow(10, j - 1) * i); } angle += angleStep; angle %= angleCircle; } return(sequence); }
protected static CoordinateSequence AddCoordinateToSequence(CoordinateSequence sequence, CoordinateSequenceFactory factory, double x, double y, double?z, double?m) { // Create a new sequence var newSequence = factory.Create(sequence.Count + 1, sequence.Dimension, sequence.Measures); // Copy old values int maxDim = sequence.Dimension; for (int i = 0; i < sequence.Count; i++) { for (int dim = 0; dim < maxDim; dim++) { newSequence.SetOrdinate(i, dim, sequence.GetOrdinate(i, dim)); } } // new coordinate newSequence.SetX(sequence.Count, x); newSequence.SetY(sequence.Count, y); if (z.HasValue) { newSequence.SetZ(sequence.Count, z.Value); } if (m.HasValue) { newSequence.SetM(sequence.Count, m.Value); } return(newSequence); }
/** * Constructs a MLineString using the given Coordinates; a null or empty * array will create an empty MLineString. * * @param coordinates * array of MCoordinate defining this geometry's vertices * @see #createLineString(com.vividsolutions.jts.geom.Coordinate[]) * @return An instance of MLineString containing the coordinates */ public MLineString CreateMLineString(MCoordinate[] coordinates) { return(CreateMLineString( coordinates != null ? CoordinateSequenceFactory.Create(coordinates) : null)); }
private static CoordinateSequence CreateSequenceFromOrdinates(CoordinateSequenceFactory csFactory, int dim) { var sequence = csFactory.Create(ordinateValues.Length, dim, 0); for (int i = 0; i < ordinateValues.Length; i++) { sequence.SetOrdinate(i, Ordinate.X, ordinateValues[i][0]); sequence.SetOrdinate(i, Ordinate.Y, ordinateValues[i][1]); } return(FillNonPlanarDimensions(sequence)); }
private void CheckDimInvalid(CoordinateSequenceFactory factory) { try { var seq = factory.Create(5, 2, 1); Assert.Fail("Dimension=2/Measure=1 (XM) not supported"); } catch (ArgumentException) { } }
private static CoordinateSequence CreateTestSequence(CoordinateSequenceFactory csFactory, int size, int dim) { var cs = csFactory.Create(size, dim, 0); // initialize with a data signature where coords look like [1, 10, 100, ...] for (int i = 0; i < size; i++) { for (int d = 0; d < dim; d++) { cs.SetOrdinate(i, d, i * Math.Pow(10, d)); } } return(cs); }
/// <summary> /// Function to return a coordinate sequence that is ensured to be closed. /// </summary> /// <param name="sequence">The base sequence</param> /// <param name="factory">The factory to use in case we need to create a new sequence</param> /// <returns>A closed coordinate sequence</returns> private static CoordinateSequence EnsureClosedSequence(CoordinateSequence sequence, CoordinateSequenceFactory factory) { //This sequence won't serve a valid linear ring if (sequence.Count < 3) { return(null); } //The sequence is closed var start = sequence.GetCoordinate(0); int lastIndex = sequence.Count - 1; var end = sequence.GetCoordinate(lastIndex); if (start.Equals2D(end)) { return(sequence); } // The sequence is not closed // 1. Test for a little offset, in that case simply correct x- and y- ordinate values const double eps = 1E-7; if (start.Distance(end) < eps) { sequence.SetX(lastIndex, start.X); sequence.SetY(lastIndex, start.Y); return(sequence); } // 2. Close the sequence by adding a new point, this is heavier var newSequence = factory.Create(sequence.Count + 1, sequence.Dimension, sequence.Measures); int maxDim = sequence.Dimension; for (int i = 0; i < sequence.Count; i++) { for (int dim = 0; dim < maxDim; dim++) { newSequence.SetOrdinate(i, dim, sequence.GetOrdinate(i, dim)); } } for (int dim = 0; dim < maxDim; dim++) { newSequence.SetOrdinate(sequence.Count, dim, sequence.GetOrdinate(0, dim)); } return(newSequence); }
/// <summary> /// Creates a MultiPoint using the given CoordinateSequence; a null or empty CoordinateSequence will /// create an empty MultiPoint. /// </summary> /// <param name="coordinates">A CoordinateSequence possibly empty, or null.</param> public virtual IMultiPoint CreateMultiPoint(ICoordinateSequence coordinates) { if (coordinates == null) { coordinates = CoordinateSequenceFactory.Create(new Coordinate[] { }); } List <IPoint> points = new List <IPoint>(); for (int i = 0; i < coordinates.Count; i++) { points.Add(CreatePoint(coordinates.GetCoordinate(i))); } return(new MultiPoint(points.ToArray())); }
/// <summary> /// Creates a <see cref="IMultiPoint"/> using the given CoordinateSequence. /// A null or empty CoordinateSequence will create an empty MultiPoint. /// </summary> /// <param name="coordinates">A CoordinateSequence (possibly empty), or <c>null</c>.</param> /// <returns>A <see cref="IMultiPoint"/> object</returns> public IMultiPoint CreateMultiPoint(ICoordinateSequence coordinates) { if (coordinates == null) { coordinates = CoordinateSequenceFactory.Create(new Coordinate[] { }); } List <IPoint> points = new List <IPoint>(); for (int i = 0; i < coordinates.Count; i++) { ICoordinateSequence seq = CoordinateSequenceFactory.Create(1, coordinates.Ordinates); CoordinateSequences.Copy(coordinates, i, seq, 0, 1); points.Add(CreatePoint(seq)); } return(CreateMultiPoint(points.ToArray())); }
private static CoordinateSequence CreateRing(CoordinateSequenceFactory factory, int ring, LinearRingOrientation orientation) { var coordinates = CreateCcwRing(ring); if (orientation == LinearRingOrientation.Clockwise) { CoordinateArrays.Reverse(coordinates); } if (orientation == LinearRingOrientation.DontCare) { if (reverseSequence) { CoordinateArrays.Reverse(coordinates); } reverseSequence = !reverseSequence; } return(factory.Create(coordinates)); }
/// <summary> /// Reads a <see cref="Polygon"/> from the input stream. /// </summary> /// <param name="reader">The binary reader.</param> /// <param name="factory">The geometry factory to use for geometry creation.</param> /// <param name="ordinates">The ordinates to read. <see cref="Ordinates.XY"/> are always read.</param> /// <returns>The LineString.</returns> protected Polygon ReadPolygon(BinaryReader reader, GeometryFactory factory, Ordinates ordinates) { int numRings = reader.ReadInt32(); if (numRings == 0) { return(_factory.CreatePolygon(_coordinateSequenceFactory.Create(0, ordinates))); } var exteriorRing = ReadLinearRing(reader, factory, ordinates); var interiorRings = new LinearRing[numRings - 1]; for (int i = 0; i < interiorRings.Length; i++) { interiorRings[i] = ReadLinearRing(reader, factory, ordinates); } return(factory.CreatePolygon(exteriorRing, interiorRings)); }
/// <summary> /// Reads a <see cref="CoordinateSequence"/> from the stream /// </summary> /// <param name="reader">The binary reader</param> /// <param name="factory">The geometry factory to use for geometry creation.</param> /// <param name="precisionModel">The precision model used to make x- and y-ordinates precise.</param> /// <param name="numPoints">The number of points in the coordinate sequence.</param> /// <param name="receivedOrdinates">The ordinates to read. <see cref="Ordinates.XY"/> are always read.</param> /// <returns>The coordinate sequence</returns> protected CoordinateSequence ReadCoordinateSequence(BinaryReader reader, CoordinateSequenceFactory factory, PrecisionModel precisionModel, int numPoints, Ordinates receivedOrdinates) { var outputOrdinates = receivedOrdinates; if (HandleOrdinates != Ordinates.None) { outputOrdinates &= HandleOrdinates; } var sequence = factory.Create(numPoints, outputOrdinates); bool receivedZ = receivedOrdinates.HasFlag(Ordinates.Z); bool receivedM = receivedOrdinates.HasFlag(Ordinates.M); bool outputtingZ = outputOrdinates.HasFlag(Ordinates.Z) && sequence.HasZ; bool outputtingM = outputOrdinates.HasFlag(Ordinates.M) && sequence.HasM; for (int i = 0; i < numPoints; i++) { sequence.SetX(i, precisionModel.MakePrecise(reader.ReadDouble())); sequence.SetY(i, precisionModel.MakePrecise(reader.ReadDouble())); if (receivedZ) { double z = reader.ReadDouble(); if (outputtingZ) { sequence.SetZ(i, z); } } if (receivedM) { double m = reader.ReadDouble(); if (outputtingM) { sequence.SetM(i, m); } } } return(sequence); }
private static CoordinateSequence SetDimension(CoordinateSequenceFactory fact, CoordinateSequence seq, int dimension) { if (seq.Dimension == dimension && seq.Measures == 0) { return(seq); } var res = fact.Create(seq.Count, dimension, 0); dimension = Math.Min(dimension, seq.Dimension); for (int i = 0; i < seq.Count; i++) { for (int j = 0; j < dimension; j++) { res.SetOrdinate(i, j, seq.GetOrdinate(i, j)); } } return(res); }
private static CoordinateSequence CreateAlmostRing(CoordinateSequenceFactory factory, int dimension, int num) { if (num > 4) { num = 4; } var sequence = factory.Create(num, dimension, 0); if (num == 0) { return(FillNonPlanarDimensions(sequence)); } sequence.SetOrdinate(0, Ordinate.X, 10); sequence.SetOrdinate(0, Ordinate.Y, 10); if (num == 1) { return(FillNonPlanarDimensions(sequence)); } sequence.SetOrdinate(1, Ordinate.X, 20); sequence.SetOrdinate(1, Ordinate.Y, 10); if (num == 2) { return(FillNonPlanarDimensions(sequence)); } sequence.SetOrdinate(2, Ordinate.X, 20); sequence.SetOrdinate(2, Ordinate.Y, 20); if (num == 3) { return(FillNonPlanarDimensions(sequence)); } sequence.SetOrdinate(3, Ordinate.X, 10.00001); sequence.SetOrdinate(3, Ordinate.Y, 9.99999); return(FillNonPlanarDimensions(sequence)); }
// If X or Y is null, return an empty Point private Point makePointValid(Point point) { CoordinateSequence sequence = point.CoordinateSequence; GeometryFactory factory = point.Factory; CoordinateSequenceFactory csFactory = factory.CoordinateSequenceFactory; if (sequence.Count == 0) { return(point); } else if (Double.IsNaN(sequence.GetOrdinate(0, 0)) || Double.IsNaN(sequence.GetOrdinate(0, 1))) { return(factory.CreatePoint(csFactory.Create(0, sequence.Dimension))); } else if (sequence.Count == 1) { return(point); } else { throw new Exception(); //throw new RuntimeException("JTS cannot Create a point from a CoordinateSequence containing several points"); } }
/// <summary> /// Creates a <see cref="IMultiPoint"/> using the given Coordinates. /// A null or empty array will create an empty MultiPoint. /// </summary> /// <param name="coordinates">An array (without null elements), or an empty array, or <c>null</c></param> /// <returns>A <see cref="IMultiPoint"/> object</returns> public IMultiPoint CreateMultiPointFromCoords(Coordinate[] coordinates) { return(CreateMultiPoint(coordinates != null ? CoordinateSequenceFactory.Create(coordinates) : null)); }
/// <summary> /// Function to read a coordinate sequence. /// </summary> /// <param name="reader">The reader</param> /// <param name="size">The number of ordinates</param> /// <param name="cs">The coordinate system</param> /// <returns>The read coordinate sequence.</returns> protected CoordinateSequence ReadCoordinateSequence(BinaryReader reader, int size, CoordinateSystem cs) { var sequence = _sequenceFactory.Create(size, ToOrdinates(cs)); for (int i = 0; i < size; i++) { double x = reader.ReadDouble(); double y = reader.ReadDouble(); if (_precisionModel != null) { x = _precisionModel.MakePrecise(x); } if (_precisionModel != null) { y = _precisionModel.MakePrecise(y); } sequence.SetOrdinate(i, 0, x); sequence.SetOrdinate(i, 1, y); switch (cs) { case CoordinateSystem.XY: continue; case CoordinateSystem.XYZ: double z = reader.ReadDouble(); if (HandleOrdinate(Ordinate.Z)) { sequence.SetOrdinate(i, 2, z); } break; case CoordinateSystem.XYM: double m = reader.ReadDouble(); if (HandleOrdinate(Ordinate.M)) { sequence.SetOrdinate(i, 2, m); } break; case CoordinateSystem.XYZM: z = reader.ReadDouble(); if (HandleOrdinate(Ordinate.Z)) { sequence.SetOrdinate(i, 2, z); } m = reader.ReadDouble(); if (HandleOrdinate(Ordinate.M)) { sequence.SetOrdinate(i, 3, m); } break; default: throw new ArgumentException(string.Format("Coordinate system not supported: {0}", cs)); } } return(sequence); }
/// <summary> /// Creates a LineString using the given Coordinates; a null or empty array will /// create an empty LineString. Consecutive points must not be equal. /// </summary> /// <param name="coordinates">An array without null elements, or an empty array, or null.</param> /// <returns></returns> public virtual ILineString CreateLineString(ICoordinate[] coordinates) { return(CreateLineString(coordinates != null ? CoordinateSequenceFactory.Create(coordinates) : null)); }
/// <summary> /// Creates an empty Point /// </summary> /// <returns> /// An empty Point /// </returns> public IPoint CreatePoint() { return(CreatePoint(CoordinateSequenceFactory.Create(0, CoordinateSequenceFactory.Ordinates))); }
/// <summary> /// Creates a Point using the given Coordinate. /// A <c>null</c> coordinate creates an empty Geometry. /// </summary> /// <param name="coordinate">a Coordinate, or null</param> /// <returns>A <see cref="IPoint"/> object</returns> public IPoint CreatePoint(Coordinate coordinate) { return(CreatePoint(coordinate != null ? CoordinateSequenceFactory.Create(new[] { coordinate }) : null)); }
/// <summary>Creates an empty LinearRing</summary> /// <returns>An empty LinearRing</returns> public ILinearRing CreateLinearRing() { return(CreateLinearRing(CoordinateSequenceFactory.Create(0, CoordinateSequenceFactory.Ordinates))); }
/// <summary> /// Creates a <c>LinearRing</c> using the given <c>Coordinates</c>; a null or empty array /// creates an empty LinearRing. The points must form a closed and simple /// linestring. Consecutive points must not be equal. /// </summary> /// <param name="coordinates">An array without null elements, or an empty array, or null.</param> /// <returns>A <see cref="ILinearRing"/> object</returns> /// <exception cref="ArgumentException"> If the ring is not closed, or has too few points</exception> public ILinearRing CreateLinearRing(Coordinate[] coordinates) { return(CreateLinearRing(coordinates != null ? CoordinateSequenceFactory.Create(coordinates) : null)); }
/// <summary>Creates an empty Polygon</summary> /// <returns>An empty Polygon</returns> public IPolygon CreatePolygon() { return(CreatePolygon(CoordinateSequenceFactory.Create(0, CoordinateSequenceFactory.Ordinates))); }