private static CoordinateSequence CreateSequence(Ordinates ordinateFlags, double[] xy) { // get the number of dimension to verify size of provided ordinate values array int dimension = RequiredDimension(ordinateFlags); // inject additional values double[] ordinateValues = InjectZM(ordinateFlags, xy); // get the required size of the sequence int size = Math.DivRem(ordinateValues.Length, dimension, out int remainder); if (remainder != 0) { throw new ArgumentException("ordinateFlags and number of provided ordinate values don't match"); } // create a sequence capable of storing all ordinate values. var res = GetCSFactory(ordinateFlags) .Create(size, RequiredDimension(ordinateFlags), OrdinatesUtility.OrdinatesToMeasures(ordinateFlags)); // fill in values int k = 0; for (int i = 0; i < ordinateValues.Length; i += dimension) { for (int j = 0; j < dimension; j++) { res.SetOrdinate(k, j, ordinateValues[i + j]); } k++; } return(res); }
/// <summary> /// Creates an instance of this class /// </summary> /// <param name="coordinates">The coordinates</param> /// <param name="ordinates"></param> public DotSpatialAffineCoordinateSequence(IReadOnlyCollection <Coordinate> coordinates, Ordinates ordinates) : base(coordinates?.Count ?? 0, OrdinatesUtility.OrdinatesToDimension(ordinates & Ordinates.XYZM), OrdinatesUtility.OrdinatesToMeasures(ordinates & Ordinates.XYZM)) { coordinates = coordinates ?? Array.Empty <Coordinate>(); _xy = new double[2 * coordinates.Count]; if (HasZ) { _z = new double[coordinates.Count]; } if (HasM) { _m = new double[coordinates.Count]; } var xy = MemoryMarshal.Cast <double, XYStruct>(_xy); using (var coordinatesEnumerator = coordinates.GetEnumerator()) { for (int i = 0; i < xy.Length; i++) { if (!coordinatesEnumerator.MoveNext()) { ThrowForInconsistentCount(); } var coordinate = coordinatesEnumerator.Current; xy[i].X = coordinate.X; xy[i].Y = coordinate.Y; if (_z != null) { _z[i] = coordinate.Z; } if (_m != null) { _m[i] = coordinate.M; } } if (coordinatesEnumerator.MoveNext()) { ThrowForInconsistentCount(); } } void ThrowForInconsistentCount() => throw new ArgumentException("IReadOnlyCollection<T>.Count is inconsistent with IEnumerable<T> implementation.", nameof(coordinates)); }
/// <summary> /// Creates a sequence based on the given coordinate sequence. /// </summary> /// <param name="coordSeq">The coordinate sequence.</param> /// <param name="ordinates">The ordinates to copy</param> public DotSpatialAffineCoordinateSequence(CoordinateSequence coordSeq, Ordinates ordinates) : base(coordSeq?.Count ?? 0, OrdinatesUtility.OrdinatesToDimension(ordinates & Ordinates.XYZM), OrdinatesUtility.OrdinatesToMeasures(ordinates & Ordinates.XYZM)) { if (coordSeq is DotSpatialAffineCoordinateSequence dsCoordSeq) { _xy = (double[])dsCoordSeq._xy.Clone(); if (HasZ) { _z = ((double[])dsCoordSeq._z?.Clone()) ?? NullOrdinateArray(Count); } if (HasM) { _m = ((double[])dsCoordSeq._m?.Clone()) ?? NullOrdinateArray(Count); } } else { _xy = new double[2 * Count]; if (HasZ) { _z = new double[Count]; } if (HasM) { _m = new double[Count]; } var xy = MemoryMarshal.Cast <double, XYStruct>(_xy); for (int i = 0; i < xy.Length; i++) { xy[i].X = coordSeq.GetX(i); xy[i].Y = coordSeq.GetY(i); if (_z != null) { _z[i] = coordSeq.GetZ(i); } if (_m != null) { _m[i] = coordSeq.GetM(i); } } } }
/// <summary> /// Constructs a sequence of a given size, populated with new Coordinates. /// </summary> /// <param name="size">The size of the sequence to create.</param> /// <param name="ordinates">The kind of ordinates.</param> public DotSpatialAffineCoordinateSequence(int size, Ordinates ordinates) : base(size, OrdinatesUtility.OrdinatesToDimension(ordinates & Ordinates.XYZM), OrdinatesUtility.OrdinatesToMeasures(ordinates & Ordinates.XYZM)) { _xy = new double[2 * size]; if (HasZ) { _z = NullOrdinateArray(size); } if (HasM) { _m = NullOrdinateArray(size); } }