Exemplo n.º 1
0
        //AE+C#修改栅格数据像素值
        //private void ChangeRasterValue(IRasterDataset2 pRasterDatset, double dbScale, double dbOffset)
        //{
        //    IRaster2 pRaster2 = pRasterDatset.CreateFullRaster() as IRaster2;

        //    IPnt pPntBlock = new PntClass();

        //    pPntBlock.X = 128;
        //    pPntBlock.Y = 128;

        //    IRasterCursor pRasterCursor = pRaster2.CreateCursorEx(pPntBlock);
        //    IRasterEdit pRasterEdit = pRaster2 as IRasterEdit;

        //    if (pRasterEdit.CanEdit())
        //    {
        //        IRasterBandCollection pBands = pRasterDatset as IRasterBandCollection;
        //        IPixelBlock3 pPixelblock3 = null;
        //        int pBlockwidth = 0;
        //        int pBlockheight = 0;
        //        System.Array pixels;
        //        IPnt pPnt = null;
        //        object pValue;
        //        long pBandCount = pBands.Count;

        //        //获取Nodata
        //        IRasterProps pRasterPro = pRaster2 as IRasterProps;
        //        object pNodata = pRasterPro.NoDataValue;
        //        //double dbNoData = Convert.ToDouble(((double[])pNodata)[0]);
        //        double dbNoData = getNoDataValue(pNodata);
        //        if (double.IsNaN(dbNoData))
        //            return;

        //        do
        //        {
        //            pPixelblock3 = pRasterCursor.PixelBlock as IPixelBlock3;
        //            pBlockwidth = pPixelblock3.Width;
        //            pBlockheight = pPixelblock3.Height;

        //            for (int k = 0; k < pBandCount; k++)
        //            {
        //                pixels = (System.Array)pPixelblock3.get_PixelData(k);
        //                for (int i = 0; i < pBlockwidth; i++)
        //                {
        //                    for (int j = 0; j < pBlockheight; j++)
        //                    {
        //                        pValue = pixels.GetValue(i, j);
        //                        double ob = Convert.ToDouble(pValue);
        //                        if (ob != dbNoData)
        //                        {
        //                            ob *= dbScale;  //翻转
        //                            ob += dbOffset; //Z方向偏移
        //                        }

        //                        //pixels.SetValue(ob, i, j);
        //                        IRasterProps pRP = pRaster2 as IRasterProps;
        //                        if (pRP.PixelType == rstPixelType.PT_CHAR)
        //                            pixels.SetValue(Convert.ToChar(ob), i, j);
        //                        else if (pRP.PixelType == rstPixelType.PT_UCHAR)
        //                            pixels.SetValue(Convert.ToByte(ob), i, j);
        //                        else if (pRP.PixelType == rstPixelType.PT_FLOAT)
        //                            pixels.SetValue(Convert.ToSingle(ob), i, j);
        //                        else if (pRP.PixelType == rstPixelType.PT_DOUBLE)
        //                            pixels.SetValue(Convert.ToDouble(ob), i, j);
        //                        else if (pRP.PixelType == rstPixelType.PT_ULONG)
        //                            pixels.SetValue(Convert.ToInt32(ob), i, j);
        //                        else
        //                            ;
        //                    }
        //                }
        //                pPixelblock3.set_PixelData(k, pixels);

        //                System.Array textPixel = null;
        //                textPixel = (System.Array)pPixelblock3.get_PixelData(k);
        //            }

        //            pPnt = pRasterCursor.TopLeft;
        //            pRasterEdit.Write(pPnt, (IPixelBlock)pPixelblock3);
        //        }
        //        while (pRasterCursor.Next());

        //        pRasterEdit.Refresh();
        //        System.Runtime.InteropServices.Marshal.ReleaseComObject(pRasterEdit);
        //    }
        //}

        private bool rasterTransfer(IRasterLayer pRasterLayer, string szFilename, double[] dbRotateMatrix, double[] dbOffsetMatrix)
        {
            if (pRasterLayer == null || dbRotateMatrix == null || dbOffsetMatrix == null || dbRotateMatrix.Length != 9 || dbOffsetMatrix.Length != 3)
            {
                return(false);
            }

            //IRasterLayer pResultLayer = new RasterLayerClass();
            //pResultLayer.CreateFromRaster(pRasterLayer.Raster);
            //pResult = new RasterLayer();
            //pResult.co

            IGeoReference pGeoReference = pRasterLayer as IGeoReference;

            //XY平面平移和旋转
            double dbScale = Math.Sqrt(dbRotateMatrix[0] * dbRotateMatrix[0] + dbRotateMatrix[1] * dbRotateMatrix[1]);
            double dbTheta = Math.Acos(dbRotateMatrix[0] / dbScale);

            IPoint pt = new PointClass();

            pt.X = 0; pt.Y = 0;
            pGeoReference.Rotate(pt, -dbTheta / Math.PI * 180);           //旋转
            pGeoReference.Shift(-dbOffsetMatrix[0], -dbOffsetMatrix[1]);  //平移
            //pGeoReference.Rotate(pt, dbTheta / Math.PI * 180);                                   //旋转
            pGeoReference.ReScale(1 / dbScale, 1 / dbScale);              //拉伸

            try
            {
                if (!File.Exists(szFilename))
                {
                    pGeoReference.Rectify(szFilename, "TIFF");

                    IRaster2    pRaster2    = pRasterLayer.Raster as IRaster2;
                    IRasterEdit pRasterEdit = pRaster2 as IRasterEdit;
                    System.Runtime.InteropServices.Marshal.ReleaseComObject(pRasterEdit);
                }
                //MessageBox.Show("转换完成!");
            }
            catch (System.Exception ex)
            {
                //MessageBox.Show("转换出错!");
                return(false);
            }
            finally
            {
                pGeoReference.Reset();
            }


            //Open a raster file workspace.
            IWorkspaceFactory workspaceFactory = new RasterWorkspaceFactoryClass();
            IRasterWorkspace  rasterWorkspace  = (IRasterWorkspace)
                                                 workspaceFactory.OpenFromFile(System.IO.Path.GetDirectoryName(szFilename), 0);

            //Open a file raster dataset.
            IRasterDataset  rasterDataset  = rasterWorkspace.OpenRasterDataset(System.IO.Path.GetFileName(szFilename));
            IRasterDataset2 rasterDataset2 = rasterDataset as IRasterDataset2;

            ClsRasterOp pRasterOp = new ClsRasterOp();

            pRasterOp.ChangeRasterValue(rasterDataset2, dbRotateMatrix[8], dbOffsetMatrix[2]);

            ////Z方向变化

            //System.Array pixels, dstPixels;
            //IPixelBlock3 pixelBlock3 = null;
            //IRaster2 pRaster2 = rasterDataset.Raster as IRaster2;
            //IRaster pRaster = pDstLayer.Raster;
            //IRasterEdit pEdit = pRaster as IRasterEdit;
            //IRasterCursor rasterCursor = pRaster2.CreateCursorEx(null);//null时为128*128

            //do
            //{
            //    IPnt nSize = new PntClass();
            //    nSize.X = rasterCursor.PixelBlock.Width;
            //    nSize.Y = rasterCursor.PixelBlock.Height;
            //    pixelblock4 = rasterCursor.PixelBlock as IPixelBlock3;
            //    pixelBlock3 = pRaster.CreatePixelBlock(nSize) as IPixelBlock3;

            //    int nWidth = pixelBlock3.Width;
            //    int nHeight = pixelBlock3.Height;
            //    pixels = (System.Array)pixelBlock3.get_PixelData(0);
            //    dstPixels = (System.Array)pixelblock4.get_PixelData(0);
            //    for (int i = 0; i < nWidth; i++)
            //    {
            //        for (int j = 0; j < nHeight; j++)
            //        {
            //            object obj = pixels.GetValue(i, j);
            //            double ob = Convert.ToDouble(obj);

            //            ob *= dbRotateMatrix[8];  //翻转
            //            ob += dbOffsetMatrix[2]; //Z方向偏移

            //            dstPixels.SetValue(ob, i, j);
            //        }
            //    }

            //    //写回数据
            //    pEdit.Write(rasterCursor.TopLeft, pixelblock4 as IPixelBlock);
            //} while (rasterCursor.Next() == true);

            return(true);
        }