示例#1
0
        private ICursor QueryGrid(int x, int y)
        {
            try
            {
                unsafe
                {
                    var floatNodata = (float)_nodata;

                    fixed(float *buf = new float[2])
                    {
                        OSGeo_v1.GDAL.Band band = _gDS.GetRasterBand(1);

                        band.ReadRaster(x, y, 1, 1,
                                        (IntPtr)buf,
                                        1, 1, OSGeo_v1.GDAL.DataType.GDT_CFloat32, 4, 0);

                        if ((_hasNoDataVal != 0 && buf[0] == floatNodata) ||
                            (_useIgnoreValue && buf[0] == _ignoreValue))
                        {
                            return(null);
                        }

                        string[] tags   = { "ImageX", "ImageY", "band1" };
                        object[] values = { x, y, buf[0] };

                        band.Dispose();

                        return(new QueryCursor(tags, values));
                    }
                }
            }
            finally
            {
            }
        }
示例#2
0
 public void InitGridQuery()
 {
     if (_gridQueryBand == null)
     {
         _gridQueryBand = _gDS.GetRasterBand(1);
     }
 }
示例#3
0
 public void ReleaseGridQuery()
 {
     if (_gridQueryBand != null)
     {
         _gridQueryBand.Dispose();
         _gridQueryBand = null;
     }
 }
示例#4
0
        private ICursor QueryImage(int x, int y)
        {
            unsafe
            {
                int              bandCount = _gDS.RasterCount;
                string[]         tags      = new string[bandCount + 2];
                object[]         values    = new object[bandCount + 2];
                List <ArgbColor> colors    = new List <ArgbColor>();

                for (int i = 1; i <= bandCount; ++i)
                {
                    OSGeo_v1.GDAL.Band band = _gDS.GetRasterBand(i);

                    string bandName = "";
                    switch ((ColorInterp)band.GetRasterColorInterpretation())
                    {
                    case ColorInterp.BlueBand:
                        bandName = "(blue)";
                        break;

                    case ColorInterp.GreenBand:
                        bandName = "(green)";
                        break;

                    case ColorInterp.RedBand:
                        bandName = "(red)";
                        break;

                    case ColorInterp.GrayIndex:
                        for (int iColor = 0; iColor < 256; iColor++)
                        {
                            colors.Add(ArgbColor.FromArgb(255, iColor, iColor, iColor));
                        }
                        break;

                    case ColorInterp.PaletteIndex:
                        tags   = new string[tags.Length + 4];
                        values = new object[values.Length + 4];

                        OSGeo_v1.GDAL.ColorTable colTable = band.GetRasterColorTable();
                        if (colTable == null)
                        {
                            break;
                        }

                        int colCount = colTable.GetCount();
                        for (int iColor = 0; iColor < colCount; iColor++)
                        {
                            OSGeo_v1.GDAL.ColorEntry colEntry = colTable.GetColorEntry(iColor);
                            colors.Add(ArgbColor.FromArgb(
                                           colEntry.c4, colEntry.c1, colEntry.c2, colEntry.c3));
                        }

                        break;
                    }

                    int c = 0;

                    int *buf = &c;

                    band.ReadRaster(x, y, 1, 1,
                                    (IntPtr)buf,
                                    1, 1, OSGeo_v1.GDAL.DataType.GDT_Int32, 4, 0);

                    band.Dispose();

                    tags[i + 1]   = "Band " + i.ToString() + " " + bandName;
                    values[i + 1] = c;

                    if (colors.Count > 0 && c >= 0 && c < colors.Count)
                    {
                        ArgbColor col = colors[c];
                        tags[i + 2]   = "Alpha";
                        values[i + 2] = col.A;
                        tags[i + 3]   = "Red";
                        values[i + 3] = col.R;
                        tags[i + 4]   = "Green";
                        values[i + 4] = col.G;
                        tags[i + 5]   = "Blue";
                        values[i + 5] = col.B;
                    }
                }

                tags[0] = "ImageX"; values[0] = x;
                tags[1] = "ImageY"; values[1] = y;

                return(new QueryCursor(tags, values));
            }
        }
示例#5
0
        private IRasterPaintContext PaintHillShade(int x, int y, int wWidth, int wHeight, int iWidth, int iHeight, double mag, ICancelTracker cancelTracker)
        {
            if (CancelTracker.Canceled(cancelTracker) || _gDS == null)
            {
                return(null);
            }

            int pixelSpace = 4;

            using (var bitmap = GraphicsEngine.Current.Engine.CreateBitmap(iWidth, iHeight + 100, GraphicsEngine.PixelFormat.Rgba32))
            {
                var bitmapData          = bitmap.LockBitmapPixelData(GraphicsEngine.BitmapLockMode.ReadWrite, GraphicsEngine.PixelFormat.Rgba32);
                OSGeo_v1.GDAL.Band band = null;

                try
                {
                    int    stride = bitmapData.Stride;
                    IntPtr buf    = bitmapData.Scan0;

                    List <ArgbColor> colors = new List <ArgbColor>();
                    using (band = _gDS.GetRasterBand(1))
                    {
                        band.ReadRaster(x, y, wWidth, wHeight,
                                        buf,
                                        iWidth, iHeight, OSGeo_v1.GDAL.DataType.GDT_CFloat32, pixelSpace, stride);

                        band.Dispose();
                    }

                    double cx = _tfw.cellX / mag;
                    double cy = _tfw.cellY / mag;

                    Vector3d sun = new Vector3d(_hillShade);
                    sun.Normalize();
                    int rowStride = stride / pixelSpace;

                    ArgbColor col = ArgbColor.White;
                    unsafe
                    {
                        byte * ptr = (byte *)(bitmapData.Scan0);
                        float *v   = (float *)(bitmapData.Scan0);

                        float floatNodata = (float)_nodata;

                        for (int i = 0; i < iHeight; i++)
                        {
                            if (CancelTracker.Canceled(cancelTracker))
                            {
                                return(null);
                            }

                            for (int j = 0; j < iWidth; j++)
                            {
                                if ((_hasNoDataVal == 1 && *v == floatNodata) ||
                                    (_useIgnoreValue && *v == _ignoreValue))
                                {
                                    ptr[0] = ptr[1] = ptr[2] = ptr[3] = 0;
                                }
                                else
                                {
                                    double c = *v;

                                    col = GridColorClass.FindColor(c, _colorClasses);
                                    if (!_useHillShade)
                                    {
                                        ptr[0] = col.B; ptr[1] = col.G; ptr[2] = col.R;
                                        ptr[3] = col.A; // alpha
                                    }
                                    else
                                    {
                                        double c1 = (j < iWidth - 1) ? (*(v + 1)) : c;
                                        double c2 = (i < iHeight - 1) ? (*(v + rowStride)) : c;
                                        c1 = ((_hasNoDataVal != 0 && c1 == floatNodata) ||
                                              (_useIgnoreValue && c1 == _ignoreValue)) ? c : c1;
                                        c2 = ((_hasNoDataVal != 0 && c2 == floatNodata) ||
                                              (_useIgnoreValue && c2 == _ignoreValue)) ? c : c2;

                                        Vector3d v1 = new Vector3d(cx, 0.0, c1 - c); v1.Normalize();
                                        Vector3d v2 = new Vector3d(0.0, -cy, c2 - c); v2.Normalize();
                                        Vector3d vs = v2 % v1; vs.Normalize();
                                        double   h  = Math.Min(Math.Max(0.0, sun * vs), 1.0);
                                        //double h = Math.Abs(sun * vs);

                                        double a = col.A;
                                        double r = col.R * h;
                                        double g = col.G * h;
                                        double b = col.B * h;
                                        ptr[0] = (byte)b; ptr[1] = (byte)g; ptr[2] = (byte)r;
                                        ptr[3] = (byte)a; // alpha
                                    }
                                }
                                ptr += pixelSpace;
                                v++;
                            }
                            ptr += bitmapData.Stride - (bitmapData.Width) * pixelSpace;
                            v    = (float *)ptr;
                        }
                    }
                    if (bitmap != null)
                    {
                        bitmap.UnlockBitmapPixelData(bitmapData);
                        bitmapData = null;
                    }

                    var contextBitmap = GraphicsEngine.Current.Engine.CreateBitmap(iWidth, iHeight, GraphicsEngine.PixelFormat.Rgba32);
                    using (var gr = contextBitmap.CreateCanvas())
                    {
                        gr.DrawBitmap(bitmap, new GraphicsEngine.CanvasPoint(0, 0));
                    }

                    return(new RasterPaintContext(contextBitmap));
                }
                catch (Exception ex)
                {
                    throw ex;
                }
                finally
                {
                    if (bitmap != null && bitmapData != null)
                    {
                        bitmap.UnlockBitmapPixelData(bitmapData);
                    }
                }
            }
        }
示例#6
0
        private IRasterPaintContext PaintGrid(int x, int y, int wWidth, int wHeight, int iWidth, int iHeight)
        {
            if (_gDS == null)
            {
                return(null);
            }

            int pixelSpace = 4;

            using (var bitmap = GraphicsEngine.Current.Engine.CreateBitmap(iWidth, iHeight + 100, GraphicsEngine.PixelFormat.Rgba32))
            {
                var bitmapData          = bitmap.LockBitmapPixelData(GraphicsEngine.BitmapLockMode.ReadWrite, GraphicsEngine.PixelFormat.Rgba32);
                OSGeo_v1.GDAL.Band band = null;

                try
                {
                    int    stride = bitmapData.Stride;
                    IntPtr buf    = bitmapData.Scan0;

                    List <ArgbColor> colors = new List <ArgbColor>();
                    using (band = _gDS.GetRasterBand(1))
                    {
                        band.ReadRaster(x, y, wWidth, wHeight,
                                        buf,
                                        iWidth, iHeight, OSGeo_v1.GDAL.DataType.GDT_CFloat32, pixelSpace, stride);

                        band.Dispose();
                    }

                    unsafe
                    {
                        byte * ptr = (byte *)(bitmapData.Scan0);
                        float *v   = (float *)(bitmapData.Scan0);

                        var floatNodata = (float)_nodata;
                        for (int i = 0; i < iHeight; i++)
                        {
                            for (int j = 0; j < iWidth; j++)
                            {
                                if (_renderRawGridValues)
                                {
                                    if (_hasNoDataVal == 1 && *v == floatNodata)
                                    {
                                        ptr[0] = ptr[1] = ptr[2] = ptr[3] = 0;
                                    }
                                    else
                                    {
                                        var int24Bytes = new Int24(*v * 100f).GetBytes();
                                        ptr[0] = int24Bytes[0];
                                        ptr[1] = int24Bytes[1];
                                        ptr[2] = int24Bytes[2];
                                        ptr[3] = 255;
                                    }
                                    //byte* vb = (byte*)v;
                                    //ptr[0] = *vb; vb++;
                                    //ptr[1] = *vb; vb++;
                                    //ptr[2] = *vb; vb++;
                                    //ptr[3] = *vb;
                                }
                                else
                                {
                                    if (_hasNoDataVal == 1 && *v == floatNodata)
                                    {
                                        ptr[0] = ptr[1] = ptr[2] = ptr[3] = 0;
                                    }
                                    else
                                    {
                                        double c = (*v - _min) / (_max - _min);
                                        double a = _minColor.A + c * (_maxColor.A - _minColor.A);
                                        double r = _minColor.R + c * (_maxColor.R - _minColor.R);
                                        double g = _minColor.G + c * (_maxColor.G - _minColor.G);
                                        double b = _minColor.B + c * (_maxColor.B - _minColor.B);
                                        ptr[0] = (byte)b; ptr[1] = (byte)g; ptr[2] = (byte)r;
                                        ptr[3] = (byte)a; // alpha
                                    }
                                }
                                ptr += pixelSpace;
                                v++;
                            }
                            ptr += bitmapData.Stride - bitmapData.Width * pixelSpace;
                            v    = (float *)ptr;
                        }
                    }

                    if (bitmap != null)
                    {
                        bitmap.UnlockBitmapPixelData(bitmapData);
                        bitmapData = null;
                    }

                    var contextBitmap = GraphicsEngine.Current.Engine.CreateBitmap(iWidth, iHeight, GraphicsEngine.PixelFormat.Rgba32);
                    using (var canvas = contextBitmap.CreateCanvas())
                    {
                        canvas.DrawBitmap(bitmap, new GraphicsEngine.CanvasPoint(0, 0));
                    }

                    return(new RasterPaintContext(contextBitmap));
                }
                catch (Exception ex)
                {
                    throw ex;
                }
                finally
                {
                    if (bitmap != null && bitmapData != null)
                    {
                        bitmap.UnlockBitmapPixelData(bitmapData);
                    }
                }
            }
        }
示例#7
0
        private IRasterPaintContext PaintWavelet(int x, int y, int wWidth, int wHeight, int iWidth, int iHeight, ICancelTracker cancelTracker)
        {
            if (CancelTracker.Canceled(cancelTracker) || _gDS == null)
            {
                return(null);
            }

            int pixelSpace = 4;
            var bitmap     = Current.Engine.CreateBitmap(iWidth, iHeight, GraphicsEngine.PixelFormat.Rgba32);
            var bitmapData = bitmap.LockBitmapPixelData(BitmapLockMode.ReadWrite, GraphicsEngine.PixelFormat.Rgba32);

            try
            {
                int    stride = bitmapData.Stride;
                IntPtr buf    = bitmapData.Scan0;

                for (int i = 1; i <= (_gDS.RasterCount > 3 ? 3 : _gDS.RasterCount); ++i)
                {
                    using (OSGeo_v1.GDAL.Band band = _gDS.GetRasterBand(i))
                    {
                        int ch = 0;
                        switch ((ColorInterp)band.GetRasterColorInterpretation())
                        {
                        case ColorInterp.BlueBand:
                            ch = 0;
                            break;

                        case ColorInterp.GreenBand:
                            ch = 1;
                            break;

                        case ColorInterp.RedBand:
                            ch = 2;
                            break;
                        }
                        band.ReadRaster(x, y, wWidth, wHeight,
                                        new IntPtr(buf.ToInt64() + ch),
                                        iWidth, iHeight, OSGeo_v1.GDAL.DataType.GDT_Byte, pixelSpace, stride);
                    }
                }

                return(new RasterPaintContext(bitmap));
            }
            catch (Exception ex)
            {
                if (bitmap != null && bitmapData != null)
                {
                    bitmap.UnlockBitmapPixelData(bitmapData);
                    bitmapData = null;
                    bitmap.Dispose();
                    bitmap = null;
                }

                throw ex;
            }
            finally
            {
                if (bitmap != null && bitmapData != null)
                {
                    bitmap.UnlockBitmapPixelData(bitmapData);
                }
            }
        }
示例#8
0
        public RasterClassV1(IRasterDataset dataset, string filename, IPolygon polygon)
        {
            try
            {
                FileInfo fi = new FileInfo(filename);
                _title    = fi.Name;
                _filename = filename;
                _dataset  = dataset;

                if (!OSGeo.Initializer.RegisterAll())
                {
                    throw new Exception("Can't register GDAL");
                }

                _gDS = OSGeo_v1.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_v1.GDAL.Band band = _gDS.GetRasterBand(1))
                {
                    if (_gDS.RasterCount == 1)
                    {
                        if (band.DataType != OSGeo_v1.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_v1.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;

                Console.WriteLine($"GDAL Excepiton: { ex.Message }");
                while (ex.InnerException != null)
                {
                    ex = ex.InnerException;
                    Console.WriteLine($"  Inner Exception: { ex.Message }");
                }

                _dataset.LastErrorMessage = ex.Message;
                _valid = false;
            }
        }
示例#9
0
        private IRasterPaintContext PaintImage(int x, int y, int wWidth, int wHeight, int iWidth, int iHeight, ICancelTracker cancelTracker)
        {
            if (CancelTracker.Canceled(cancelTracker) || _gDS == null)
            {
                return(null);
            }

            int pixelSpace = 3;
            var bitmap     = Current.Engine.CreateBitmap(iWidth, iHeight, GraphicsEngine.PixelFormat.Rgb24);
            var bitmapData = bitmap.LockBitmapPixelData(BitmapLockMode.WriteOnly, GraphicsEngine.PixelFormat.Rgb24);

            try
            {
                int    stride = bitmapData.Stride;
                IntPtr buf    = bitmapData.Scan0;

                List <ArgbColor> colors = new List <ArgbColor>();
                for (int i = 1; i <= (_gDS.RasterCount > 3 ? 3 : _gDS.RasterCount); ++i)
                {
                    using (OSGeo_v1.GDAL.Band band = _gDS.GetRasterBand(i))
                    {
                        int ch = 0;
                        switch ((ColorInterp)band.GetRasterColorInterpretation())
                        {
                        case ColorInterp.BlueBand:
                            ch = 0;
                            break;

                        case ColorInterp.GreenBand:
                            ch = 1;
                            break;

                        case ColorInterp.RedBand:
                            ch = 2;
                            break;

                        case ColorInterp.GrayIndex:
                            for (int iColor = 0; iColor < 256; iColor++)
                            {
                                colors.Add(ArgbColor.FromArgb(255, iColor, iColor, iColor));
                            }
                            break;

                        case ColorInterp.PaletteIndex:
                            OSGeo_v1.GDAL.ColorTable colTable = band.GetRasterColorTable();
                            if (colTable == null)
                            {
                                break;
                            }

                            int colCount = colTable.GetCount();
                            for (int iColor = 0; iColor < colCount; iColor++)
                            {
                                OSGeo_v1.GDAL.ColorEntry colEntry = colTable.GetColorEntry(iColor);
                                colors.Add(ArgbColor.FromArgb(
                                               colEntry.c4, colEntry.c1, colEntry.c2, colEntry.c3));
                            }

                            break;
                        }
                        band.ReadRaster(x, y, wWidth, wHeight,
                                        new IntPtr(buf.ToInt64() + ch),
                                        iWidth, iHeight, OSGeo_v1.GDAL.DataType.GDT_Byte, pixelSpace, stride);
                    }
                }
                if (colors.Count > 0)
                {
                    unsafe
                    {
                        byte *ptr = (byte *)(bitmapData.Scan0);
                        for (int i = 0; i < bitmapData.Height; i++)
                        {
                            if (CancelTracker.Canceled(cancelTracker))
                            {
                                return(null);
                            }

                            for (int j = 0; j < bitmapData.Width; j++)
                            {
                                // write the logic implementation here
                                byte      c   = ptr[0];
                                ArgbColor col = colors[c];
                                ptr[0] = col.B;
                                ptr[1] = col.G;
                                ptr[2] = col.R;
                                ptr   += pixelSpace;
                            }
                            ptr += bitmapData.Stride - bitmapData.Width * pixelSpace;
                        }
                    }
                }

                return(new RasterPaintContext(bitmap));
            }
            catch (Exception ex)
            {
                if (bitmap != null && bitmapData != null)
                {
                    bitmap.UnlockBitmapPixelData(bitmapData);
                    bitmapData = null;
                    bitmap.Dispose();
                    bitmap = null;
                }

                throw ex;
            }
            finally
            {
                if (bitmap != null && bitmapData != null)
                {
                    bitmap.UnlockBitmapPixelData(bitmapData);
                }
            }
        }