Пример #1
0
        public void BeginPaint(gView.Framework.Carto.IDisplay display, ICancelTracker cancelTracker)
        {
            IntPtr     bufferData = (IntPtr)0;
            BitmapData bitmapData = null;
            double     mag        = 1f; // mag immer als float, läuft stabiler!!!

            int x       = 0;
            int y       = 0;
            int iWidth  = 0;
            int iHeight = 0;

            try
            {
                if (_reader == (IntPtr)0)
                {
                    if (!InitReader())
                    {
                        return;
                    }
                }

                if (!(_polygon is ITopologicalOperation) || _reader == (IntPtr)0)
                {
                    return;
                }

                TFWFile tfw = this.GeoCoord as TFWFile;
                if (tfw == null)
                {
                    return;
                }

                IEnvelope dispEnvelope = display.DisplayTransformation.TransformedBounds(display); //display.Envelope;
                if (display.GeometricTransformer != null)
                {
                    dispEnvelope = (IEnvelope)((IGeometry)display.GeometricTransformer.InvTransform2D(dispEnvelope)).Envelope;
                }

                IGeometry clipped;
                ((ITopologicalOperation)_polygon).Clip(dispEnvelope, out clipped);
                if (!(clipped is IPolygon))
                {
                    return;
                }

                IPolygon cPolygon = (IPolygon)clipped;

                // geclipptes Polygon transformieren -> Bild
                vector2[] vecs = new vector2[cPolygon[0].PointCount];
                for (int i = 0; i < cPolygon[0].PointCount; i++)
                {
                    vecs[i] = new vector2(cPolygon[0][i].X, cPolygon[0][i].Y);
                }
                if (!tfw.ProjectInv(vecs))
                {
                    return;
                }
                IEnvelope picEnv = vector2.IntegerEnvelope(vecs);
                picEnv.minx = Math.Max(0, picEnv.minx);
                picEnv.miny = Math.Max(0, picEnv.miny);
                picEnv.maxx = Math.Min(picEnv.maxx, _geoCoord.iWidth);
                picEnv.maxy = Math.Min(picEnv.maxy, _geoCoord.iHeight);

                // Ecken zurücktransformieren -> Welt
                vecs    = new vector2[3];
                vecs[0] = new vector2(picEnv.minx, picEnv.miny);
                vecs[1] = new vector2(picEnv.maxx, picEnv.miny);
                vecs[2] = new vector2(picEnv.minx, picEnv.maxy);
                tfw.Project(vecs);
                _p1 = new gView.Framework.Geometry.Point(vecs[0].x, vecs[0].y);
                _p2 = new gView.Framework.Geometry.Point(vecs[1].x, vecs[1].y);
                _p3 = new gView.Framework.Geometry.Point(vecs[2].x, vecs[2].y);

                double pix = display.mapScale / (display.dpi / 0.0254);  // [m]
                double c1  = Math.Sqrt(_geoCoord.xRes * _geoCoord.xRes + _geoCoord.xRot * _geoCoord.xRot);
                double c2  = Math.Sqrt(_geoCoord.yRes * _geoCoord.yRes + _geoCoord.yRot * _geoCoord.yRot);
                mag = Math.Round((Math.Min(c1, c2) / pix), 8);

                // Immer in auf float runden! Läuft stabiler!!!
                //mag = (float)mag; //1.03;
                if (mag > 1f)
                {
                    mag = 1f;
                }
                if (mag < _geoCoord.MinMagnification)
                {
                    mag = (float)_geoCoord.MinMagnification;
                }

                x       = (int)(picEnv.minx * mag);
                y       = (int)(picEnv.miny * mag);
                iWidth  = (int)((picEnv.Width - 1) * mag);
                iHeight = (int)((picEnv.Height - 1) * mag);

                bufferData = MrSidWrapper.Read(_reader, x, y, iWidth, iHeight, (double)mag);
                if (bufferData == (IntPtr)0)
                {
                    return;
                }

                int totalWidth  = MrSidWrapper.GetTotalCols(bufferData);
                int totalHeight = MrSidWrapper.GetTotalRows(bufferData);

                if (_bm != null)
                {
                    _bm.Dispose();
                }
                _bm        = new Bitmap(totalWidth, totalHeight, PixelFormat.Format24bppRgb);
                bitmapData = _bm.LockBits(new Rectangle(0, 0, totalWidth, totalHeight), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);

                Console.Write(bitmapData.Scan0);

                MrSidWrapper.ReadBandData(bufferData, bitmapData.Scan0, (uint)3, (uint)bitmapData.Stride);

                //_bm.Save(@"C:\temp\pic\" + Guid.NewGuid() + ".jpg", ImageFormat.Jpeg);
            }
            catch (Exception ex)
            {
                //string errMsg = ex.Message;
                EndPaint(cancelTracker);

                if (display is IServiceMap && ((IServiceMap)display).MapServer != null)
                {
                    IMapServer mapServer = ((IServiceMap)display).MapServer;
                    mapServer.Log(
                        "RenderRasterLayerThread", loggingMethod.error,
                        ex.Message + "\n" + ex.Source + "\n" + ex.StackTrace + "\n" +
                        "filename=" + _filename + "\n" +
                        "x=" + x.ToString() + "\n" +
                        "y=" + y.ToString() + "\n" +
                        "iWidth=" + iWidth.ToString() + "\n" +
                        "iHeight=" + iHeight.ToString() + "\n" +
                        "mag=" + mag.ToString() + "\n");
                }
                else
                {
                    throw ex;
                }
            }
            finally
            {
                if (bitmapData != null)
                {
                    _bm.UnlockBits(bitmapData);
                }
                MrSidWrapper.ReleaseBandData(bufferData);
                ReleaseReader();
            }
        }
Пример #2
0
        async public Task <IRasterPaintContext> BeginPaint(gView.Framework.Carto.IDisplay display, ICancelTracker cancelTracker)
        {
            IntPtr bufferData = (IntPtr)0;

            GraphicsEngine.BitmapPixelData bitmapData = null;
            double mag = 1f; // mag immer als float, läuft stabiler!!!

            int x       = 0;
            int y       = 0;
            int iWidth  = 0;
            int iHeight = 0;

            GraphicsEngine.Abstraction.IBitmap bitmap = null;

            try
            {
                if (_reader == (IntPtr)0)
                {
                    if (!InitReader())
                    {
                        return(null);
                    }
                }

                if (!(_polygon is ITopologicalOperation) || _reader == (IntPtr)0)
                {
                    return(null);
                }

                TFWFile tfw = this.GeoCoord as TFWFile;
                if (tfw == null)
                {
                    return(null);
                }

                IEnvelope dispEnvelope = display.DisplayTransformation.TransformedBounds(display); //display.Envelope;
                if (display.GeometricTransformer != null)
                {
                    dispEnvelope = ((IGeometry)display.GeometricTransformer.InvTransform2D(dispEnvelope)).Envelope;
                }

                IGeometry clipped;
                ((ITopologicalOperation)_polygon).Clip(dispEnvelope, out clipped);
                if (!(clipped is IPolygon))
                {
                    return(null);
                }

                IPolygon cPolygon = (IPolygon)clipped;

                if (cPolygon.RingCount == 0 || cPolygon[0].Area == 0D)
                {
                    return(null);
                }

                // geclipptes Polygon transformieren -> Bild
                vector2[] vecs = new vector2[cPolygon[0].PointCount];
                for (int i = 0; i < cPolygon[0].PointCount; i++)
                {
                    vecs[i] = new vector2(cPolygon[0][i].X, cPolygon[0][i].Y);
                }
                if (!tfw.ProjectInv(vecs))
                {
                    return(null);
                }

                IEnvelope picEnv = vector2.IntegerEnvelope(vecs);
                picEnv.minx = Math.Max(0, picEnv.minx);
                picEnv.miny = Math.Max(0, picEnv.miny);
                picEnv.maxx = Math.Min(picEnv.maxx, _geoCoord.iWidth);
                picEnv.maxy = Math.Min(picEnv.maxy, _geoCoord.iHeight);

                // Ecken zurücktransformieren -> Welt
                vecs    = new vector2[3];
                vecs[0] = new vector2(picEnv.minx, picEnv.miny);
                vecs[1] = new vector2(picEnv.maxx, picEnv.miny);
                vecs[2] = new vector2(picEnv.minx, picEnv.maxy);
                tfw.Project(vecs);
                _p1 = new gView.Framework.Geometry.Point(vecs[0].x, vecs[0].y);
                _p2 = new gView.Framework.Geometry.Point(vecs[1].x, vecs[1].y);
                _p3 = new gView.Framework.Geometry.Point(vecs[2].x, vecs[2].y);

                double pix = display.mapScale / (display.dpi / 0.0254);  // [m]
                double c1  = Math.Sqrt(_geoCoord.xRes * _geoCoord.xRes + _geoCoord.xRot * _geoCoord.xRot);
                double c2  = Math.Sqrt(_geoCoord.yRes * _geoCoord.yRes + _geoCoord.yRot * _geoCoord.yRot);
                mag = Math.Round((Math.Min(c1, c2) / pix), 8);

                // Immer in auf float runden! Läuft stabiler!!!
                //mag = (float)mag; //1.03;
                if (mag > 1f)
                {
                    mag = 1f;
                }

                if (mag < _geoCoord.MinMagnification)
                {
                    mag = (float)_geoCoord.MinMagnification;
                }

                x       = (int)(picEnv.minx * mag);
                y       = (int)(picEnv.miny * mag);
                iWidth  = (int)((picEnv.Width - 1) * mag);
                iHeight = (int)((picEnv.Height - 1) * mag);

                bufferData = MrSidWrapper.Read(_reader, x, y, iWidth, iHeight, mag);
                if (bufferData == (IntPtr)0)
                {
                    return(null);
                }

                int totalWidth  = MrSidWrapper.GetTotalCols(bufferData);
                int totalHeight = MrSidWrapper.GetTotalRows(bufferData);

                bitmap     = GraphicsEngine.Current.Engine.CreateBitmap(totalWidth, totalHeight, GraphicsEngine.PixelFormat.Rgb24);
                bitmapData = bitmap.LockBitmapPixelData(GraphicsEngine.BitmapLockMode.WriteOnly, GraphicsEngine.PixelFormat.Rgb24);

                MrSidWrapper.ReadBandData(bufferData, bitmapData.Scan0, 3, (uint)bitmapData.Stride);

                return(new RasterPaintContext(bitmap));
            }
            catch (Exception ex)
            {
                //string errMsg = ex.Message;

                if (display is IServiceMap && ((IServiceMap)display).MapServer != null)
                {
                    IMapServer mapServer = ((IServiceMap)display).MapServer;
                    await mapServer.LogAsync(
                        ((IServiceMap)display).Name,
                        "RenderRasterLayerThread", loggingMethod.error,
                        ex.Message + "\n" + ex.Source + "\n" + ex.StackTrace + "\n" +
                        "filename=" + _filename + "\n" +
                        "x=" + x.ToString() + "\n" +
                        "y=" + y.ToString() + "\n" +
                        "iWidth=" + iWidth.ToString() + "\n" +
                        "iHeight=" + iHeight.ToString() + "\n" +
                        "mag=" + mag.ToString() + "\n");
                }
                else
                {
                    throw ex;
                }

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

                MrSidWrapper.ReleaseBandData(bufferData);
                ReleaseReader();
            }
        }