public Encoder(int precision, ThirdDimension thirdDimension, int thirdDimPrecision)
 {
     _latConverter   = new Converter(precision);
     _lngConverter   = new Converter(precision);
     _zConverter     = new Converter(thirdDimPrecision);
     _thirdDimension = thirdDimension;
     _result         = new StringBuilder();
     EncodeHeader(precision, (int)_thirdDimension, thirdDimPrecision);
 }
            private void DecodeHeader()
            {
                long header = 0;

                DecodeHeaderFromString(_encoded, ref _index, ref header);
                _precision         = (int)(header & 0b_1111);           // we pick the first 4 bits only
                header           >>= 4;
                _thirdDimension    = (ThirdDimension)(header & 0b_111); // we pick the first 3 bits only
                _thirdDimPrecision = (int)((header >> 3) & 0b_1111);
            }
        public void TestInvalidThirdDimension()
        {
            var pairs = new List <LatLngZ>();

            pairs.Add(new LatLngZ(50.1022829, 8.6982122));
            ThirdDimension invalid = (ThirdDimension)999;

            Assert.Throws <ArgumentException>(() =>
            {
                PolylineEncoderDecoder.Encode(pairs, 5, invalid, 0);
            });
        }
        public void EncodedFlexilineMatchesDecodedResultTest()
        {
            using (var decodedEnumerator = File.ReadLines("../../../../../../test/round_half_up/decoded.txt").GetEnumerator())
            {
                foreach (var flexiline in File.ReadLines("../../../../../../test/round_half_up/encoded.txt"))
                {
                    decodedEnumerator.MoveNext();
                    var testResultStr = decodedEnumerator.Current;

                    var expectedResult = ParseExpectedTestResult(testResultStr);

                    var decoded = PolylineEncoderDecoder.Decode(flexiline);
                    var encoded = PolylineEncoderDecoder.Encode(
                        decoded,
                        expectedResult.Precision.Precision2d,
                        expectedResult.Precision.Type3d,
                        expectedResult.Precision.Precision3d);

                    ThirdDimension thirdDimension = PolylineEncoderDecoder.GetThirdDimension(encoded);
                    Assert.AreEqual(expectedResult.Precision.Type3d, thirdDimension);

                    for (int i = 0; i < decoded.Count; i++)
                    {
                        AssertEqualWithPrecision(
                            expectedResult.Coordinates[i].Lat,
                            decoded[i].Lat,
                            expectedResult.Precision.Precision2d);

                        AssertEqualWithPrecision(
                            expectedResult.Coordinates[i].Lng,
                            decoded[i].Lng,
                            expectedResult.Precision.Precision2d);

                        AssertEqualWithPrecision(
                            expectedResult.Coordinates[i].Z,
                            decoded[i].Z,
                            expectedResult.Precision.Precision3d);
                    }

                    if (flexiline != encoded)
                    {
                        Console.WriteLine($@"WARNING expected {flexiline} but got {encoded}");
                    }
                }
            }
        }
        /// <summary>
        /// Encode the list of coordinate triples.
        /// The third dimension value will be eligible for encoding only when ThirdDimension is other than ABSENT.
        /// This is lossy compression based on precision accuracy.
        /// </summary>
        /// <param name="coordinates">coordinates {@link List} of coordinate triples that to be encoded.</param>
        /// <param name="precision">Floating point precision of the coordinate to be encoded.</param>
        /// <param name="thirdDimension">{@link ThirdDimension} which may be a level, altitude, elevation or some other custom value</param>
        /// <param name="thirdDimPrecision">Floating point precision for thirdDimension value</param>
        /// <returns>URL-safe encoded {@link String} for the given coordinates.</returns>
        public static string Encode(List <LatLngZ> coordinates, int precision, ThirdDimension thirdDimension, int thirdDimPrecision)
        {
            if (coordinates == null || coordinates.Count == 0)
            {
                throw new ArgumentException("Invalid coordinates!");
            }

            if (!Enum.IsDefined(typeof(ThirdDimension), thirdDimension))
            {
                throw new ArgumentException("Invalid thirdDimension");
                //thirdDimension = ThirdDimension.Absent;
            }

            Encoder enc = new Encoder(precision, thirdDimension, thirdDimPrecision);

            foreach (var coordinate in coordinates)
            {
                enc.Add(coordinate);
            }

            return(enc.GetEncoded());
        }
 public Precision(int precision2d, int precision3d = 0, ThirdDimension type3d = ThirdDimension.Absent)
 {
     Precision2d = precision2d;
     Precision3d = precision3d;
     Type3d      = type3d;
 }