Exemplo n.º 1
0
        public RasterClass(IRasterDataset dataset, string filename, IPolygon polygon)
        {
            try
            {
                FileInfo fi = new FileInfo(filename);
                _title    = fi.Name;
                _filename = filename;
                _dataset  = dataset;

                OSGeo.GDAL.Gdal.AllRegister();
                _gDS = OSGeo.GDAL.Gdal.Open(fi.FullName, 0);
                if (_gDS == null && _gDS.RasterCount == 0)
                {
                    _valid = false;
                    return;
                }

                _iWidth  = _gDS.RasterXSize;
                _iHeight = _gDS.RasterYSize;

                switch (fi.Extension.ToLower())
                {
                case ".adf":
                case ".gsd":
                    _type = RasterType.grid;
                    break;
                    //case ".jp2":
                    //    _type = RasterType.wavelet;
                    //    break;
                }

                using (OSGeo.GDAL.Band band = _gDS.GetRasterBand(1))
                {
                    if (_gDS.RasterCount == 1)
                    {
                        if (band.DataType != OSGeo.GDAL.DataType.GDT_Byte)
                        {
                            _type = RasterType.grid;
                        }
                    }
                    band.GetMinimum(out _min, out _hasNoDataVal);
                    band.GetMaximum(out _max, out _hasNoDataVal);
                    band.GetNoDataValue(out _nodata, out _hasNoDataVal);
                }
                OSGeo.GDAL.Driver driver = _gDS.GetDriver();

                double[] tfw = new double[6];
                _gDS.GetGeoTransform(tfw);

                string tfwFilename = fi.FullName.Substring(0, fi.FullName.Length - fi.Extension.Length);
                switch (fi.Extension.ToLower())
                {
                case ".jpg":
                case ".jpeg":
                    tfwFilename += ".jgw";
                    break;

                case ".jp2":
                    tfwFilename += ".j2w";
                    break;

                case ".tif":
                case ".tiff":
                    tfwFilename += ".tfw";
                    break;

                case ".ecw":
                    tfwFilename += ".eww";
                    break;

                default:
                    break;
                }

                FileInfo tfwInfo = new FileInfo(tfwFilename);

                _tfw = new TFWFile(tfw[0], tfw[3], tfw[1], tfw[2], tfw[4], tfw[5]);
                if (tfwInfo.Exists)
                {
                    _tfw.Filename = tfwFilename;
                }

                if (_tfw.X == 0.0 && _tfw.Y == 0.0 &&
                    Math.Abs(_tfw.dx_X) == 1.0 && _tfw.dx_Y == 0.0 &&
                    Math.Abs(_tfw.dy_Y) == 1.0 && _tfw.dy_X == 0.0 && driver != null)
                {
                    if (tfwInfo.Exists)
                    {
                        _tfw = new TFWFile(tfwFilename);
                    }
                    else
                    {
                        _tfw.isValid = false;
                    }
                }
                else
                {
                    // Bei dem Driver schein es nicht Pixelmitte sein, oder ist das bei GDAL generell
                    //if (driver.ShortName.ToLower() == "jp2openjpeg")
                    {
                        _tfw.X += (_tfw.dx_X / 2.0D + _tfw.dx_Y / 2.0D);
                        _tfw.Y += (_tfw.dy_X / 2.0D + _tfw.dy_Y / 2.0D);
                    }
                }


                FileInfo fiPrj = new FileInfo(fi.FullName.Substring(0, fi.FullName.Length - fi.Extension.Length) + ".prj");
                if (fiPrj.Exists)
                {
                    StreamReader sr  = new StreamReader(fiPrj.FullName);
                    string       wkt = sr.ReadToEnd();
                    sr.Close();

                    _sRef = gView.Framework.Geometry.SpatialReference.FromWKT(wkt);
                }
                else
                {
                    fiPrj = new FileInfo(fi.FullName.Substring(0, fi.FullName.Length - fi.Extension.Length) + ".wkt");
                    if (fiPrj.Exists)
                    {
                        StreamReader sr  = new StreamReader(fiPrj.FullName);
                        string       wkt = sr.ReadToEnd();
                        sr.Close();

                        _sRef = gView.Framework.Geometry.SpatialReference.FromWKT(wkt);
                    }
                }
                if (polygon != null)
                {
                    _polygon = polygon;
                }
                else
                {
                    calcPolygon();
                }
            }
            catch (Exception ex)
            {
                string errMsg = ex.Message;
                _valid = false;
            }
        }