Beispiel #1
0
        public static decimal GetValueFromBytes(byte[] bytes, StCoordinatesSystem coordinatesSystem, StCoordType type)
        {
            if (type == StCoordType.M)
            {
                throw new NotSupportedException("M-Coordinates are not supported yet");
            }

            decimal val = 0;

            // The first byte will give the sign
            byte b;
            bool isNegative = IsNegative(bytes, coordinatesSystem, out b);

            val += b * coordinatesSystem.ByteFactors[0];

            // Next bytes
            for (int i = 1; i < bytes.Length; ++i)
            {
                b = bytes[i];
                if (b >= coordinatesSystem.ByteCoeff[i + 1])
                {
                    b = (byte)(b - coordinatesSystem.RestartValue);
                }
                val += b * coordinatesSystem.ByteFactors[i];
            }

            if (isNegative)
            {
                return(coordinatesSystem.GetMin(type) - val);
            }

            return(val + coordinatesSystem.GetMin(type));
        }
Beispiel #2
0
        public static byte[] GetBytesFromValue(decimal val, StCoordinatesSystem coordinatesSystem, StCoordType type)
        {
            if (type == StCoordType.M)
            {
                throw new NotSupportedException("M Coordiante is not supported yet");
            }

            bool    isNegative = false;
            decimal valFromMin = val - coordinatesSystem.GetMin(type);

            if (valFromMin < 0)
            {
                // We have to encode a negative number
                isNegative = true;
                valFromMin = Math.Abs(valFromMin);
            }

            // Search how many bytes have to be encoded
            int maxIndex = coordinatesSystem.ByteFactors.Length - 1;

            while (maxIndex > 0 && valFromMin < coordinatesSystem.ByteFactors[maxIndex])
            {
                maxIndex--;
            }

            var bytes = new List <byte>();

            // Encode utlimate byte first
            decimal div       = valFromMin / coordinatesSystem.ByteFactors[maxIndex];
            var     byteValue = (byte)Math.Floor(div);
            decimal restVal   = valFromMin - (byteValue * coordinatesSystem.ByteFactors[maxIndex]);

            bytes.Insert(0, byteValue);

            // Encode bytes from penultimate to first
            for (int i = maxIndex - 1; i >= 0; --i)
            {
                div       = restVal / coordinatesSystem.ByteFactors[i];
                byteValue = (byte)Math.Floor(div);
                restVal  -= byteValue * coordinatesSystem.ByteFactors[i];

                // byteValue cannot be < 128 (RestartValue)
                byteValue += coordinatesSystem.RestartValue;
                if (byteValue < coordinatesSystem.RestartValue || byteValue > 255)
                {
                    throw new IndexOutOfRangeException();
                }

                bytes.Insert(0, byteValue);
            }

            if (isNegative)
            {
                SetNegative(bytes, coordinatesSystem);
            }

            return(bytes.ToArray());
        }
Beispiel #3
0
        public static byte[] GetDifferenceFromBytes(byte[] initialBytes, byte[] previousBytes, StCoordinatesSystem coordinatesSystem, StCoordType type)
        {
            // We mut encode the difference with the previous coordinate instead of the real value
            decimal initialRealValue = GetValueFromBytes(initialBytes, coordinatesSystem, type);

            // Get The previous value
            decimal previousValue = GetValueFromBytes(previousBytes, coordinatesSystem, type);

            // Get the difference
            decimal difference = initialRealValue - previousValue;

            // Get the value to encode
            decimal valueToEncode = coordinatesSystem.GetMin(type) + difference;

            return(GetBytesFromValue(valueToEncode, coordinatesSystem, type));
        }
Beispiel #4
0
        public static byte[] GetBytesFromDifference(byte[] initialBytes, byte[] previousBytes, StCoordinatesSystem coordinatesSystem, StCoordType type)
        {
            // Exemple : LINESTRING (-1 -1, 0 0)
            //  - initial bytes  : { 208 156 1 } ==> the value of the point is 0
            //                                       but the difference with the previous point is encoded => 1
            //  - previous bytes : { 176 139 197 186 141 17 } ==> this is the encoding for -1
            //

            // Here we get the value decoded from the bytes representing the difference
            decimal encodedDifference = GetValueFromBytes(initialBytes, coordinatesSystem, type);

            // Get The previous value
            decimal valuePrevious   = GetValueFromBytes(previousBytes, coordinatesSystem, type);
            decimal encodedPrevious = valuePrevious - coordinatesSystem.GetMin(type);

            // Calculate new value
            decimal value = encodedPrevious + encodedDifference;

            return(GetBytesFromValue(value, coordinatesSystem, type));
        }