Exemple #1
0
        /// <summary>
        /// Sets the parameters of the format.
        /// </summary>
        /// <param name="parameters">The parameters.</param>
        private void SetParameters(IDictionary <GeometryStreamParameter, Object> parameters)
        {
            _layout                = ResolveParameter <RawImageLayout>(SpectralGeometryStreamParameters.Layout);
            _numberOfRows          = ResolveParameter <Int32>(SpectralGeometryStreamParameters.NumerOfRows);
            _numberOfColumns       = ResolveParameter <Int32>(SpectralGeometryStreamParameters.NumberOfColumns);
            _numberOfBands         = ResolveParameter <Int32>(SpectralGeometryStreamParameters.SpectralResolution);
            _radiometricResolution = ResolveParameter <Int32>(SpectralGeometryStreamParameters.RadiometricResolution);
            _byteOrder             = ResolveParameter <ByteOrder>(SpectralGeometryStreamParameters.SpectralResolution);
            _bytesGapPerBand       = ResolveParameter <Int32>(SpectralGeometryStreamParameters.BytesGapPerBand);
            _bytesPerBandRow       = IsProvidedParameter(SpectralGeometryStreamParameters.BytesPerBandRow) ?
                                     ResolveParameter <Int32>(SpectralGeometryStreamParameters.BytesPerBandRow) :
                                     Convert.ToInt32(Math.Ceiling(_numberOfColumns * _radiometricResolution / 8.0));
            _bytesPerRow = IsProvidedParameter(SpectralGeometryStreamParameters.BytesPerRow) ?
                           ResolveParameter <Int32>(SpectralGeometryStreamParameters.BytesPerRow) :
                           (_layout == RawImageLayout.BandInterlevedByLine ? _numberOfBands * _bytesGapPerBand : (Int32)Math.Ceiling(_numberOfRows * _numberOfColumns * _radiometricResolution / 8.0));
            _bytesSkipped = ResolveParameter <Int32>(SpectralGeometryStreamParameters.BytesSkipped);

            // set raster mapping if all parameters are available
            if (parameters.ContainsKey(SpectralGeometryStreamParameters.TieCoordinate) && parameters.ContainsKey(SpectralGeometryStreamParameters.ColumnDimension) && parameters.ContainsKey(SpectralGeometryStreamParameters.RowDimension))
            {
                _mapper = RasterMapper.FromTransformation(RasterMapMode.ValueIsCoordinate,
                                                          ResolveParameter <Coordinate>(SpectralGeometryStreamParameters.TieCoordinate).X,
                                                          ResolveParameter <Coordinate>(SpectralGeometryStreamParameters.TieCoordinate).Y,
                                                          0,
                                                          ResolveParameter <Double>(SpectralGeometryStreamParameters.ColumnDimension),
                                                          ResolveParameter <Double>(SpectralGeometryStreamParameters.RowDimension),
                                                          1);
            }
        }
        /// <summary>
        /// Computes the raster mapper.
        /// </summary>
        /// <returns>The raster mapper.</returns>
        protected override RasterMapper ComputeRasterMapper()
        {
            RasterMapper mapper = null;

            // try to read from file content
            try
            {
                mapper = ComputeRasterToModelSpaceMapping();
            }
            catch { }

            if (mapper != null)
            {
                return(mapper);
            }

            // try to read from metafile
            if (_metafileReader != null)
            {
                try
                {
                    mapper = _metafileReader.ReadMapping();
                }
                catch { }
            }


            if (mapper != null)
            {
                return(mapper);
            }

            // use default mapping
            return(RasterMapper.FromTransformation(RasterMapMode.ValueIsArea, Coordinate.Empty, new CoordinateVector(1, 1, 0)));
        }
Exemple #3
0
        public void ProxyRasterConstructorTest()
        {
            // successful construction without spectral ranges and mapping
            ProxyRaster raster = new ProxyRaster(_factory.Object, _service.Object, null);

            Assert.IsFalse(raster.IsMapped);
            Assert.IsNull(raster.Mapper);
            Assert.IsTrue(raster.Coordinates.All(coordinate => !coordinate.IsValid));

            Assert.AreEqual(_service.Object.IsReadable, raster.IsReadable);
            Assert.AreEqual(_service.Object.IsWritable, raster.IsWritable);
            Assert.AreEqual(_service.Object.Dimensions, raster.Dimensions);
            Assert.AreEqual(_service.Object.Format, raster.Format);
            Assert.AreEqual(_service.Object.Dimensions.NumberOfBands, raster.NumberOfBands);
            Assert.AreEqual(_service.Object.Dimensions.NumberOfBands, raster.Bands.Count);
            Assert.AreEqual(_factory.Object, raster.Factory);

            // successful construction with mapping

            RasterMapper mapper = RasterMapper.FromTransformation(RasterMapMode.ValueIsArea, 1000, 1000, 0, 10, 10, 1);

            raster = new ProxyRaster(_factory.Object, _service.Object, mapper);
            Assert.IsTrue(raster.IsMapped);
            Assert.AreEqual(mapper, raster.Mapper);
            Assert.AreEqual(new Coordinate(1000, 1200), raster.Coordinates[0]);
            Assert.AreEqual(new Coordinate(1150, 1200), raster.Coordinates[1]);
            Assert.AreEqual(new Coordinate(1150, 1000), raster.Coordinates[2]);
            Assert.AreEqual(new Coordinate(1000, 1000), raster.Coordinates[3]);

            // argument null exception

            Assert.Throws <ArgumentNullException>(() => { raster = new ProxyRaster(_factory.Object, null, null); });
        }
Exemple #4
0
        public void ProxyRasterSetValuesTest()
        {
            _service.Setup(entity => entity.WriteValueSequence(It.IsInRange(0, 19, Moq.Range.Inclusive),
                                                               It.IsInRange(0, 14, Moq.Range.Inclusive),
                                                               It.IsInRange(0, 2, Moq.Range.Inclusive), It.IsAny <UInt32[]>()))
            .Callback(new Action <Int32, Int32, Int32, UInt32[]>((rowIndex, columnIndex, bandIndex, values) => Assert.IsTrue(values.SequenceEqual(new UInt32[] { 0, 1, 2 }))));

            // test case 1: index based

            ProxyRaster raster = new ProxyRaster(_factory.Object, _service.Object, null);

            for (Int32 j = 0; j < raster.NumberOfRows; j += 5)
            {
                for (Int32 k = 0; k < raster.NumberOfColumns; k += 5)
                {
                    raster.SetValues(j, k, new UInt32[] { 0, 1, 2 });
                }
            }

            // test case 2: coordinate based

            RasterMapper mapper = RasterMapper.FromTransformation(RasterMapMode.ValueIsCoordinate, 1000, 1000, 0, 10, 10, 1);

            raster = new ProxyRaster(_factory.Object, _service.Object, mapper);

            for (Int32 x = 1000; x < 1000 + 10 * _service.Object.Dimensions.NumberOfColumns; x += 100)
            {
                for (Int32 y = 1000; y < 1000 + 10 * _service.Object.Dimensions.NumberOfRows; y += 100)
                {
                    raster.SetValues(new Coordinate(x, y), new UInt32[] { 0, 1, 2 });
                    raster[new Coordinate(x, y)] = new UInt32[] { 0, 1, 2 };
                }
            }
        }
Exemple #5
0
        public void ProxyRasterGetValuesTest()
        {
            // index based

            ProxyRaster raster = new ProxyRaster(_factory.Object, _service.Object, null);

            for (Int32 j = 0; j < raster.NumberOfRows; j += 5)
            {
                for (Int32 k = 0; k < raster.NumberOfColumns; k += 5)
                {
                    Assert.IsTrue(_service.Object.ReadValueSequence(j, k, 0, 3).SequenceEqual(raster.GetValues(j, k)));
                    Assert.IsTrue(_service.Object.ReadValueSequence(j, k, 0, 3).SequenceEqual(raster[j, k]));
                }
            }

            // coordinate based

            RasterMapper mapper = RasterMapper.FromTransformation(RasterMapMode.ValueIsCoordinate, 1000, 1000, 0, 10, 10, 1);

            raster = new ProxyRaster(_factory.Object, _service.Object, mapper);

            for (Int32 x = 1000; x < 1000 + 10 * _service.Object.Dimensions.NumberOfColumns; x += 100)
            {
                for (Int32 y = 1000; y < 1000 + 10 * _service.Object.Dimensions.NumberOfRows; y += 100)
                {
                    Assert.IsTrue(_service.Object.ReadValueSequence((x - 1000) / 10, (y - 1000) / 10, 0, 3).SequenceEqual(raster.GetValues(new Coordinate(x, y))));
                    Assert.IsTrue(_service.Object.ReadValueSequence((x - 1000) / 10, (y - 1000) / 10, 0, 3).SequenceEqual(raster[new Coordinate(x, y)]));
                }
            }
        }
        public void RasterMapperFromTransformationTest()
        {
            // matrix

            RasterMapper mapper = RasterMapper.FromTransformation(RasterMapMode.ValueIsArea, MatrixFactory.CreateIdentity(4));

            Assert.AreEqual(RasterMapMode.ValueIsArea, mapper.Mode);
            Assert.AreEqual(MatrixFactory.CreateIdentity(4), mapper.GeometryTransformation);


            // coordinate and vector

            mapper = RasterMapper.FromTransformation(RasterMapMode.ValueIsArea, new Coordinate(300, 1000, 0), new CoordinateVector(10, 3));

            Assert.AreEqual(new Coordinate(300, 1000), mapper.Translation);
            Assert.AreEqual(RasterMapMode.ValueIsArea, mapper.Mode);
            Assert.AreEqual(10, mapper.ColumnSize);
            Assert.AreEqual(3, mapper.RowSize);
            Assert.AreEqual(new CoordinateVector(10, 0), mapper.ColumnVector);
            Assert.AreEqual(new CoordinateVector(0, 3), mapper.RowVector);


            // all values

            mapper = RasterMapper.FromTransformation(RasterMapMode.ValueIsArea, 300, 1000, 0, 10, 3, 0);

            Assert.AreEqual(new Coordinate(300, 1000), mapper.Translation);
            Assert.AreEqual(RasterMapMode.ValueIsArea, mapper.Mode);
            Assert.AreEqual(10, mapper.ColumnSize);
            Assert.AreEqual(3, mapper.RowSize);
            Assert.AreEqual(new CoordinateVector(10, 0), mapper.ColumnVector);
            Assert.AreEqual(new CoordinateVector(0, 3), mapper.RowVector);


            // exceptions

            Assert.Throws <ArgumentException>(() => RasterMapper.FromTransformation(RasterMapMode.ValueIsArea, Coordinate.Undefined, new CoordinateVector(0, 1, 0)));
            Assert.Throws <ArgumentException>(() => RasterMapper.FromTransformation(RasterMapMode.ValueIsArea, Coordinate.Empty, new CoordinateVector(Double.NaN, Double.NaN, Double.NaN)));
            Assert.Throws <ArgumentException>(() => RasterMapper.FromTransformation(RasterMapMode.ValueIsArea, Coordinate.Empty, new CoordinateVector(0, 1, 0)));
            Assert.Throws <ArgumentException>(() => RasterMapper.FromTransformation(RasterMapMode.ValueIsArea, Coordinate.Empty, new CoordinateVector(1, 0, 0)));
            Assert.Throws <ArgumentException>(() => RasterMapper.FromTransformation(RasterMapMode.ValueIsArea, Double.NaN, 0, 0, 1, 0, 0));
            Assert.Throws <ArgumentException>(() => RasterMapper.FromTransformation(RasterMapMode.ValueIsArea, 0, Double.NaN, 0, 1, 0, 0));
            Assert.Throws <ArgumentException>(() => RasterMapper.FromTransformation(RasterMapMode.ValueIsArea, 0, 0, Double.NaN, 1, 0, 0));
            Assert.Throws <ArgumentException>(() => RasterMapper.FromTransformation(RasterMapMode.ValueIsArea, 0, 0, 0, 1, 0, 0));
            Assert.Throws <ArgumentException>(() => RasterMapper.FromTransformation(RasterMapMode.ValueIsArea, 0, 0, 0, 0, 1, 0));
            Assert.Throws <ArgumentException>(() => RasterMapper.FromTransformation(RasterMapMode.ValueIsArea, 0, 0, 0, Double.NaN, 1, 0));
            Assert.Throws <ArgumentException>(() => RasterMapper.FromTransformation(RasterMapMode.ValueIsArea, 0, 0, 0, 1, Double.NaN, 0));
            Assert.Throws <ArgumentException>(() => RasterMapper.FromTransformation(RasterMapMode.ValueIsArea, 0, 0, 0, 1, 1, Double.NaN));
        }
        public void RasterMapperGetHashCodeTest()
        {
            RasterMapper mapper1 = new RasterMapper(RasterMapMode.ValueIsArea, MatrixFactory.CreateIdentity(4));
            RasterMapper mapper2 = new RasterMapper(RasterMapMode.ValueIsArea, MatrixFactory.CreateIdentity(4));

            Assert.AreEqual(mapper1.GetHashCode(), mapper2.GetHashCode());

            mapper2 = new RasterMapper(RasterMapMode.ValueIsCoordinate, MatrixFactory.CreateIdentity(4));

            Assert.AreNotEqual(mapper1.GetHashCode(), mapper2.GetHashCode());

            mapper2 = RasterMapper.FromTransformation(RasterMapMode.ValueIsArea, new Coordinate(300, 1000, 0), new CoordinateVector(10, 3));

            Assert.AreNotEqual(mapper1.GetHashCode(), mapper2.GetHashCode());
        }
Exemple #8
0
        public void ProxyRasterSetValueTest()
        {
            for (Int32 i = 0; i < 3; i++)
            {
                _service.Setup(entity => entity.WriteValue(It.IsInRange(0, 19, Moq.Range.Inclusive),
                                                           It.IsInRange(0, 14, Moq.Range.Inclusive),
                                                           i, It.IsAny <UInt32>()))
                .Callback(new Action <Int32, Int32, Int32, UInt32>((rowIndex, columnIndex, bandIndex, value) => Assert.AreEqual(bandIndex, value, 0)));
            }

            // test case 1: index based

            ProxyRaster raster = new ProxyRaster(_factory.Object, _service.Object, null);

            for (Int32 i = 0; i < raster.NumberOfBands; i++)
            {
                for (Int32 j = 0; j < raster.NumberOfRows; j += 5)
                {
                    for (Int32 k = 0; k < raster.NumberOfColumns; k += 5)
                    {
                        raster.SetValue(j, k, i, (UInt32)i);
                        raster[j, k, i] = (UInt32)i;
                    }
                }
            }

            // test case 2: coordinate based

            RasterMapper mapper = RasterMapper.FromTransformation(RasterMapMode.ValueIsCoordinate, 1000, 1000, 0, 10, 10, 1);

            raster = new ProxyRaster(_factory.Object, _service.Object, mapper);

            for (Int32 i = 0; i < raster.NumberOfBands; i++)
            {
                for (Int32 x = 1000; x < 1000 + 10 * _service.Object.Dimensions.NumberOfColumns; x += 100)
                {
                    for (Int32 y = 1000; y < 1000 + 10 * _service.Object.Dimensions.NumberOfRows; y += 100)
                    {
                        raster.SetValue(new Coordinate(x, y), i, (UInt32)i);
                        raster[new Coordinate(x, y), i] = (UInt32)i;
                    }
                }
            }
        }
Exemple #9
0
        public void ProxyRasterGetValueTest()
        {
            // index based

            ProxyRaster raster = new ProxyRaster(_factory.Object, _service.Object, null);

            for (Int32 i = -1; i <= raster.NumberOfBands; i++)
            {
                for (Int32 j = -1; j <= raster.NumberOfRows + 5; j += 5)
                {
                    for (Int32 k = -1; k <= raster.NumberOfColumns + 5; k += 5)
                    {
                        try
                        {
                            Assert.AreEqual(_service.Object.ReadValue(j, k, i), raster.GetValue(j, k, i));
                            Assert.AreEqual(_service.Object.ReadValue(j, k, i), raster[j, k, i]);
                        }
                        catch (ArgumentOutOfRangeException)
                        {
                            Assert.IsTrue(i < 0 || i >= raster.NumberOfBands ||
                                          j < 0 || j >= raster.NumberOfRows ||
                                          k < 0 || k >= raster.NumberOfColumns);
                        }
                    }
                }
            }

            // coordinate based

            RasterMapper mapper = RasterMapper.FromTransformation(RasterMapMode.ValueIsCoordinate, 1000, 1000, 0, 10, 10, 1);

            raster = new ProxyRaster(_factory.Object, _service.Object, mapper);

            for (Int32 x = 1000; x < 1000 + 10 * _service.Object.Dimensions.NumberOfColumns; x += 100)
            {
                for (Int32 y = 1000; y < 1000 + 10 * _service.Object.Dimensions.NumberOfRows; y += 100)
                {
                    Assert.AreEqual(_service.Object.ReadValue((x - 1000) / 10, (y - 1000) / 10, 0), raster.GetValue(new Coordinate(x, y), 0));
                    Assert.AreEqual(_service.Object.ReadValue((x - 1000) / 10, (y - 1000) / 10, 0), raster[new Coordinate(x, y), 0]);
                }
            }
        }
        public void RasterMapperEqualsTest()
        {
            RasterMapper mapper1 = new RasterMapper(RasterMapMode.ValueIsArea, MatrixFactory.CreateIdentity(4));
            RasterMapper mapper2 = new RasterMapper(RasterMapMode.ValueIsArea, MatrixFactory.CreateIdentity(4));

            Assert.IsTrue(mapper1.Equals(mapper1));
            Assert.IsFalse(mapper1.Equals(null));
            Assert.IsTrue(mapper1.Equals(mapper2));
            Assert.IsTrue(mapper1.Equals((Object)mapper1));
            Assert.IsFalse(mapper1.Equals((Object)null));
            Assert.IsTrue(mapper1.Equals((Object)mapper2));

            mapper2 = new RasterMapper(RasterMapMode.ValueIsCoordinate, MatrixFactory.CreateIdentity(4));

            Assert.IsFalse(mapper1.Equals(mapper2));
            Assert.IsFalse(mapper1.Equals((Object)mapper2));

            mapper2 = RasterMapper.FromTransformation(RasterMapMode.ValueIsArea, new Coordinate(300, 1000, 0), new CoordinateVector(10, 3));

            Assert.IsFalse(mapper1.Equals(mapper2));
            Assert.IsFalse(mapper1.Equals((Object)mapper2));
        }
Exemple #11
0
        public void ProxyRasterNearestValueTest()
        {
            // test case 1: index based

            ProxyRaster raster = new ProxyRaster(_factory.Object, _service.Object, null);

            for (Int32 i = 0; i < raster.NumberOfBands; i++)
            {
                for (Int32 j = -1; j <= raster.NumberOfRows + 5; j += 5)
                {
                    for (Int32 k = -1; k <= raster.NumberOfColumns + 5; k += 5)
                    {
                        Assert.AreEqual(_service.Object.ReadValue(Math.Max(0, Math.Min(j, _service.Object.Dimensions.NumberOfRows - 1)),
                                                                  Math.Max(0, Math.Min(j, _service.Object.Dimensions.NumberOfColumns - 1)), i),
                                        raster.GetNearestValue(j, k, i));
                    }
                }
            }

            // test case 2: coordinate based

            RasterMapper mapper = RasterMapper.FromTransformation(RasterMapMode.ValueIsCoordinate, 1000, 1000, 0, 10, 10, 1);

            raster = new ProxyRaster(_factory.Object, _service.Object, mapper);

            for (Int32 i = 0; i < raster.NumberOfBands; i++)
            {
                for (Int32 x = 1000; x < 1000 + 10 * _service.Object.Dimensions.NumberOfColumns; x += 100)
                {
                    for (Int32 y = 1000; y < 1000 + 10 * _service.Object.Dimensions.NumberOfRows; y += 100)
                    {
                        Assert.AreEqual(_service.Object.ReadValue(Math.Max(0, Math.Min((x - 1000) / 10, _service.Object.Dimensions.NumberOfRows - 1)),
                                                                  Math.Max(0, Math.Min((y - 1000) / 10, _service.Object.Dimensions.NumberOfColumns - 1)), i),
                                        raster.GetNearestValue(new Coordinate(x, y), i));
                    }
                }
            }
        }
        public void RasterMapperFromCoordinatesTest()
        {
            RasterMapper sourceMapper = RasterMapper.FromTransformation(RasterMapMode.ValueIsCoordinate, new Coordinate(300, 1000, 0), new CoordinateVector(10, 3));

            RasterMapper mapper = RasterMapper.FromCoordinates(RasterMapMode.ValueIsCoordinate,
                                                               new RasterCoordinate(0, 0, sourceMapper.MapCoordinate(0, 0)),
                                                               new RasterCoordinate(100, 0, sourceMapper.MapCoordinate(100, 0)),
                                                               new RasterCoordinate(100, 100, sourceMapper.MapCoordinate(100, 100)),
                                                               new RasterCoordinate(0, 100, sourceMapper.MapCoordinate(0, 100))
                                                               );

            Assert.AreEqual(RasterMapMode.ValueIsCoordinate, mapper.Mode);
            Assert.AreEqual(sourceMapper.ColumnVector, mapper.ColumnVector);
            Assert.AreEqual(sourceMapper.RowVector, mapper.RowVector);
            Assert.AreEqual(sourceMapper.Translation.X, mapper.Translation.X, 0.000001);
            Assert.AreEqual(sourceMapper.Translation.Y, mapper.Translation.Y, 0.000001);
            Assert.AreEqual(sourceMapper.Translation.Z, mapper.Translation.Z, 0.000001);

            // exceptions

            Assert.Throws <ArgumentNullException>(() => RasterMapper.FromCoordinates(RasterMapMode.ValueIsCoordinate, null));
            Assert.Throws <ArgumentException>(() => RasterMapper.FromCoordinates(RasterMapMode.ValueIsCoordinate, new RasterCoordinate(0, 0, sourceMapper.MapCoordinate(0, 0)), new RasterCoordinate(0, 100, sourceMapper.MapCoordinate(0, 100))));
            Assert.Throws <ArgumentException>(() => RasterMapper.FromCoordinates(RasterMapMode.ValueIsCoordinate, new RasterCoordinate(0, 0, sourceMapper.MapCoordinate(0, 0)), new RasterCoordinate(100, 0, sourceMapper.MapCoordinate(100, 0))));
        }
        /// <summary>
        /// Reads the mapping from model space to raster space.
        /// </summary>
        /// <returns>The mapping from model space to raster space.</returns>
        protected RasterMapper ComputeRasterToModelSpaceMapping()
        {
            if (!_currentGeoKeys.ContainsKey(GeoKey.RasterType))
            {
                return(null);
            }

            RasterMapMode mode = Convert.ToInt16(_currentGeoKeys[GeoKey.RasterType]) == 1 ? RasterMapMode.ValueIsArea : RasterMapMode.ValueIsCoordinate;

            Double[] modelTiePointsArray      = null;
            Double[] modelPixelScaleArray     = null;
            Double[] modelTransformationArray = null;

            // gather information from tags
            if (_imageFileDirectories[_currentImageIndex].ContainsKey(TiffTag.ModelTiepointTag))
            {
                modelTiePointsArray = _imageFileDirectories[_currentImageIndex][TiffTag.ModelTiepointTag].Select(value => Convert.ToDouble(value)).ToArray();
            }
            if (_imageFileDirectories[_currentImageIndex].ContainsKey(TiffTag.ModelPixelScaleTag))
            {
                modelPixelScaleArray = _imageFileDirectories[_currentImageIndex][TiffTag.ModelPixelScaleTag].Select(value => Convert.ToDouble(value)).ToArray();
            }
            if (_imageFileDirectories[_currentImageIndex].ContainsKey(TiffTag.ModelTransformationTag))
            {
                modelTransformationArray = _imageFileDirectories[_currentImageIndex][TiffTag.ModelTransformationTag].Select(value => Convert.ToDouble(value)).ToArray();
            }

            // for GeoTIFF 0.2, IntergraphMatrixTag (33920) may contain the transformation values
            if (modelTransformationArray == null && _imageFileDirectories[_currentImageIndex].ContainsKey(TiffTag.IntergraphMatrixTag))
            {
                modelTransformationArray = _imageFileDirectories[_currentImageIndex][TiffTag.IntergraphMatrixTag].Select(value => Convert.ToDouble(value)).ToArray();
            }

            // compute with model tie points
            if (modelTiePointsArray != null && (modelTiePointsArray.Length > 6 || (modelTiePointsArray.Length == 6 && modelPixelScaleArray.Length == 3)))
            {
                if (modelTiePointsArray.Length > 6) // transformation is specified by tie points
                {
                    RasterCoordinate[] coordinates = new RasterCoordinate[modelTiePointsArray.Length / 6];

                    for (Int32 i = 0; i < modelTiePointsArray.Length; i += 6)
                    {
                        coordinates[i / 6] = new RasterCoordinate(Convert.ToInt32(modelTiePointsArray[i]), Convert.ToInt32(modelTiePointsArray[i + 1]), new Coordinate(modelTiePointsArray[i + 3], modelTiePointsArray[i + 4], modelTiePointsArray[i + 5]));
                    }

                    return(RasterMapper.FromCoordinates(mode, coordinates));
                }
                else // transformation is specified by single tie point and scale
                {
                    Coordinate rasterSpaceCoordinate = new Coordinate(modelTiePointsArray[0], modelTiePointsArray[1], modelTiePointsArray[2]);
                    Coordinate modelSpaceCoordinate  = new Coordinate(modelTiePointsArray[3], modelTiePointsArray[4], modelTiePointsArray[5]);
                    Double     scaleX = modelPixelScaleArray[0];
                    Double     scaleY = -modelPixelScaleArray[1];
                    Double     scaleZ = modelPixelScaleArray[2];

                    return(RasterMapper.FromTransformation(mode,
                                                           modelSpaceCoordinate.X - rasterSpaceCoordinate.X * scaleX,
                                                           modelSpaceCoordinate.Y + rasterSpaceCoordinate.Y * scaleY,
                                                           modelSpaceCoordinate.Z - rasterSpaceCoordinate.Z * scaleZ,
                                                           scaleX, scaleY, scaleZ));
                }
            }

            // compute with transformation values
            if (modelTransformationArray != null)
            {
                Matrix transformation = new Matrix(4, 4);
                for (Int32 i = 0; i < 4; i++)
                {
                    for (Int32 j = 0; j < 4; j++)
                    {
                        transformation[i, j] = modelTransformationArray[i * 4 + j];
                    }
                }
                return(RasterMapper.FromTransformation(mode, transformation));
            }

            // nothing is available
            return(null);
        }
        /// <summary>
        /// Reads the header file.
        /// </summary>
        /// <exception cref="System.IO.FileNotFoundException">The header file is not available.</exception>
        private void ReadHeader()
        {
            String headerFileName;

            if (_fileSystem.Exists(_basePath + _fileSystem.DirectorySeparator + _baseFileName + ".hdr"))
            {
                headerFileName = _basePath + _fileSystem.DirectorySeparator + _baseFileName + ".hdr";
            }
            else if (_fileSystem.Exists(_basePath + _fileSystem.DirectorySeparator + _baseFileName + ".HDR"))
            {
                headerFileName = _basePath + _fileSystem.DirectorySeparator + _baseFileName + ".HDR";
            }
            else
            {
                throw new FileNotFoundException("The header file is not available.", _basePath + _fileSystem.DirectorySeparator + _baseFileName + ".hdr");
            }

            using (StreamReader reader = new StreamReader(headerFileName))
            {
                Double upperLeftX = 0, upperLeftY = 0, vectorX = 0, vectorY = 0;
                Regex  regex = new Regex(@"\s");

                while (!reader.EndOfStream)
                {
                    String line = reader.ReadLine();
                    if (String.IsNullOrEmpty(line) || line[0] == '#')
                    {
                        continue;
                    }

                    String[] lineParts = regex.Split(line.ToUpper()).Where(linePart => !String.IsNullOrEmpty(linePart)).ToArray();

                    if (lineParts.Length >= 2)
                    {
                        switch (lineParts[0])
                        {
                        case "BYTEORDER":
                            _byteOrder = (lineParts[1].Equals("I")) ? ByteOrder.LittleEndian : ByteOrder.BigEndian;
                            break;

                        case "LAYOUT":
                            switch (lineParts[1])
                            {
                            case "BIP":
                                _layout = RawImageLayout.BandInterlevedByPixel;
                                break;

                            case "BIL":
                                _layout = RawImageLayout.BandInterlevedByLine;
                                break;

                            case "BSQ":
                                _layout = RawImageLayout.BandSequential;
                                break;
                            }
                            break;

                        case "NROWS":
                            _numberOfRows = Int32.Parse(lineParts[1]);
                            break;

                        case "NCOLS":
                            _numberOfColumns = Int32.Parse(lineParts[1]);
                            break;

                        case "NBANDS":
                            _numberOfBands = Int32.Parse(lineParts[1]);
                            break;

                        case "NBITS":
                            _radiometricResolution = Int32.Parse(lineParts[1]);
                            break;

                        case "ULXMAP":
                            upperLeftX = Double.Parse(lineParts[1], CultureInfo.InvariantCulture);
                            break;

                        case "ULYMAP":
                            upperLeftY = Double.Parse(lineParts[1], CultureInfo.InvariantCulture);
                            break;

                        case "XDIM":
                            vectorX = Double.Parse(lineParts[1], CultureInfo.InvariantCulture);
                            break;

                        case "YDIM":
                            vectorY = Double.Parse(lineParts[1], CultureInfo.InvariantCulture);
                            break;
                        }
                    }
                }

                _bytesGapPerBand = 0;
                _bytesPerBandRow = (Int32)Math.Ceiling(_numberOfColumns * _radiometricResolution / 8.0);
                _bytesPerRow     = (Int32)Math.Ceiling(_numberOfRows * _numberOfColumns * _radiometricResolution / 8.0);
                _bytesSkipped    = 0;

                if (vectorX != 0 && vectorY != 0)
                {
                    _mapper = RasterMapper.FromTransformation(RasterMapMode.ValueIsCoordinate, upperLeftX, upperLeftY, 0, vectorX, vectorY, 0);
                }
            }
        }
        public void RasterMapperMapRasterTest()
        {
            // value is coordinate

            RasterMapper mapper = RasterMapper.FromTransformation(RasterMapMode.ValueIsCoordinate, 300, 1000, 0, 10, 3, 1);

            for (Int32 i = -100; i < 100; i += 3)
            {
                for (Int32 j = -100; j < 100; j += 3)
                {
                    Int32 rowIndex, columnIndex;
                    mapper.MapRaster(new Coordinate(300 + i * 10, 1000 + j * 3), out rowIndex, out columnIndex);

                    Assert.AreEqual(i, columnIndex);
                    Assert.AreEqual(j, rowIndex);

                    Double floatRowIndex, floatColumnIndex;

                    mapper.MapRaster(new Coordinate(300 + i * 10, 1000 + j * 3), out floatRowIndex, out floatColumnIndex);

                    Assert.AreEqual(i, floatColumnIndex);
                    Assert.AreEqual(j, floatRowIndex);

                    mapper.MapRaster(new Coordinate(300 + i * 10, 1000 + j * 3), RasterMapMode.ValueIsCoordinate, out floatRowIndex, out floatColumnIndex);

                    Assert.AreEqual(i, floatColumnIndex);
                    Assert.AreEqual(j, floatRowIndex);

                    mapper.MapRaster(new Coordinate(295 + i * 10, 998.5 + j * 3), RasterMapMode.ValueIsArea, out floatRowIndex, out floatColumnIndex);

                    Assert.AreEqual(i, floatColumnIndex);
                    Assert.AreEqual(j, floatRowIndex);
                }
            }


            // value is area

            mapper = RasterMapper.FromTransformation(RasterMapMode.ValueIsArea, 300, 1000, 0, 10, 3, 1);

            for (Int32 i = -100; i < 100; i += 3)
            {
                for (Int32 j = -100; j < 100; j += 3)
                {
                    Int32 rowIndex, columnIndex;
                    mapper.MapRaster(new Coordinate(300 + i * 10, 1000 + j * 3), out rowIndex, out columnIndex);

                    Assert.AreEqual(i, columnIndex);
                    Assert.AreEqual(j, rowIndex);

                    Double floatRowIndex, floatColumnIndex;

                    mapper.MapRaster(new Coordinate(300 + i * 10, 1000 + j * 3), out floatRowIndex, out floatColumnIndex);

                    Assert.AreEqual(i, floatColumnIndex);
                    Assert.AreEqual(j, floatRowIndex);

                    mapper.MapRaster(new Coordinate(305 + i * 10, 1001.5 + j * 3), RasterMapMode.ValueIsCoordinate, out floatRowIndex, out floatColumnIndex);

                    Assert.AreEqual(i, floatColumnIndex);
                    Assert.AreEqual(j, floatRowIndex);

                    mapper.MapRaster(new Coordinate(300 + i * 10, 1000 + j * 3), RasterMapMode.ValueIsArea, out floatRowIndex, out floatColumnIndex);

                    Assert.AreEqual(i, floatColumnIndex);
                    Assert.AreEqual(j, floatRowIndex);
                }
            }
        }
        public void RasterMapperMapCoordinateTest()
        {
            // value is coordinate

            RasterMapper mapper = RasterMapper.FromTransformation(RasterMapMode.ValueIsCoordinate, 300, 1000, 0, 10, 3, 0);

            for (Int32 i = -100; i < 100; i += 3)
            {
                for (Int32 j = -100; j < 100; j += 3)
                {
                    Coordinate coordinate = mapper.MapCoordinate(j, i);

                    Assert.AreEqual(300 + i * 10, coordinate.X);
                    Assert.AreEqual(1000 + j * 3, coordinate.Y);

                    coordinate = mapper.MapCoordinate((Double)j, (Double)i);

                    Assert.AreEqual(300 + i * 10, coordinate.X);
                    Assert.AreEqual(1000 + j * 3, coordinate.Y);

                    coordinate = mapper.MapCoordinate(j, i, RasterMapMode.ValueIsCoordinate);

                    Assert.AreEqual(300 + i * 10, coordinate.X);
                    Assert.AreEqual(1000 + j * 3, coordinate.Y);

                    coordinate = mapper.MapCoordinate(j, i, RasterMapMode.ValueIsArea);

                    Assert.AreEqual(295 + i * 10, coordinate.X);
                    Assert.AreEqual(998.5 + j * 3, coordinate.Y);
                }
            }


            // value is area

            mapper = RasterMapper.FromTransformation(RasterMapMode.ValueIsArea, 300, 1000, 0, 10, 3, 1);

            for (Int32 i = -100; i < 100; i += 3)
            {
                for (Int32 j = -100; j < 100; j += 3)
                {
                    Coordinate coordinate = mapper.MapCoordinate(j, i);

                    Assert.AreEqual(300 + i * 10, coordinate.X);
                    Assert.AreEqual(1000 + j * 3, coordinate.Y);

                    coordinate = mapper.MapCoordinate((Double)j, (Double)i);

                    Assert.AreEqual(300 + i * 10, coordinate.X);
                    Assert.AreEqual(1000 + j * 3, coordinate.Y);

                    coordinate = mapper.MapCoordinate(j, i, RasterMapMode.ValueIsCoordinate);

                    Assert.AreEqual(305 + i * 10, coordinate.X);
                    Assert.AreEqual(1001.5 + j * 3, coordinate.Y);

                    coordinate = mapper.MapCoordinate(j, i, RasterMapMode.ValueIsArea);

                    Assert.AreEqual(300 + i * 10, coordinate.X);
                    Assert.AreEqual(1000 + j * 3, coordinate.Y);
                }
            }
        }