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();
        }
        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);
        }
Beispiel #3
0
        /// <summary>
        /// Gets the minimal value, depending on the type of coordinate (X/Y/Z/M)
        /// </summary>
        /// <param name="type"></param>
        /// <returns></returns>
        public decimal GetMin(StCoordType type)
        {
            switch (type)
            {
            case StCoordType.X:
                return(MinX);

            case StCoordType.Y:
                return(MinY);

            case StCoordType.Z:
                if (!_hasZ)
                {
                    throw new ArgumentException("This STCoordinatesSystem was not initialized for Z coordinates. Please provide a regular minZ value in the constructor");
                }
                return(MinZ);

            default:
                throw new ArgumentException();
            }
        }
        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);
        }
        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 #6
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 #7
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));
        }
Beispiel #8
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));
        }
 public StCoordinate(decimal val, StCoordType type)
 {
     Value = val;
     Bytes = StCoordinatesSystemHelper.GetBytesFromValue(Value, StParameters.CoordinatesSystem, type);
 }
Beispiel #10
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));
        }
 public StCoordinate(List<byte> points, StCoordType type)
     : this(points.ToArray(), type)
 {
 }
 public StCoordinate(byte[] points, StCoordType type)
 {
     Bytes = points;
     Value = StCoordinatesSystemHelper.GetValueFromBytes(Bytes, StParameters.CoordinatesSystem, type);
 }
Beispiel #13
0
 public StCoordinate(decimal val, StCoordType type)
 {
     Value = val;
     Bytes = StCoordinatesSystemHelper.GetBytesFromValue(Value, StParameters.CoordinatesSystem, type);
 }
Beispiel #14
0
 public StCoordinate(List <byte> points, StCoordType type)
     : this(points.ToArray(), type)
 {
 }
Beispiel #15
0
 public StCoordinate(byte[] points, StCoordType type)
 {
     Bytes = points;
     Value = StCoordinatesSystemHelper.GetValueFromBytes(Bytes, StParameters.CoordinatesSystem, type);
 }