コード例 #1
0
        static public bool NorthEastToEastNorth(IRasterLayer pRasterLayer, string szFilename)//坐标系x,y变换
        {
            if (pRasterLayer == null || szFilename == null)
            {
                return(false);
            }

            IGeoReference pGeoReference = pRasterLayer as IGeoReference;
            IPoint        pt            = new PointClass();

            pt.X = 0;
            pt.Y = 0;
            pGeoReference.Rotate(pt, -90); //顺时针旋转90

            //水平旋转
            IRaster2     pRaster2  = pRasterLayer.Raster as IRaster2;
            IRasterProps pProps    = pRaster2 as IRasterProps;
            int          nWidth    = pProps.Width;
            int          nHeight   = pProps.Height;
            double       dbCenterY = (pProps.Extent.UpperLeft.Y + pProps.Extent.LowerRight.Y) / 2;
            double       dbDeltaX  = 0;
            double       dbDeltaY  = -dbCenterY * 2;

            pGeoReference.Flip();
            pGeoReference.Shift(dbDeltaX, dbDeltaY);

            pGeoReference.Rectify(szFilename, "TIFF");
            IRasterEdit pRasterEdit = pRaster2 as IRasterEdit;

            System.Runtime.InteropServices.Marshal.ReleaseComObject(pRasterEdit);

            return(true);
        }
コード例 #2
0
ファイル: frmAdjustPoint.cs プロジェクト: VB6Hobbyst7/minegis
        private void Save()
        {
            string format = System.IO.Path.GetExtension(m_fileName);

            m_geoRef.Rectify(m_selectPath + "\\" + m_fileName + "_Rectify.tif", "TIFF");

            //frmW.Close();
            MessageBox.Show("保存成功!");
        }
コード例 #3
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);
        }
コード例 #4
0
        private void buttonXSaveRaster_Click(object sender, EventArgs e)
        {
            SaveFileDialog dlg = new SaveFileDialog();

            dlg.Filter          = "(*.tif)|*.tif";
            dlg.OverwritePrompt = false;
            if (dlg.ShowDialog() == DialogResult.OK)
            {
                IGeoReference pGR = pRasterLayer as IGeoReference;
                if (pGR != null)
                {
                    try
                    {
                        pGR.Rectify(dlg.FileName, "TIFF");
                        MessageBox.Show("\"" + dlg.FileName + "\"保存成功!");
                    }
                    catch (SystemException ee)
                    {
                        MessageBox.Show(ee.Message);
                    }
                }
            }

            /*
             * if (pRasterLayer == null)
             * {
             *  return;
             * }
             * FrmSaveRasterTransformation dlg = new FrmSaveRasterTransformation();
             *
             * double dxcellsize = ((IRasterProps)pRasterLayer.Raster).MeanCellSize().X;
             * dlg.SetCellSize(dxcellsize);
             * double y = ((IRasterProps)pRasterLayer.Raster).MeanCellSize().Y;
             * dlg.SetCellSize(((IRasterProps)pRasterLayer.Raster).Extent.Height / ((IRasterProps)pRasterLayer.Raster).Height);
             * if (dlg.ShowDialog() == DialogResult.OK)
             * {
             *
             *  try
             *  {
             *      //IGeoReference pGR = pRasterLayer as IGeoReference;
             *      //if (pGR != null)
             *      //{
             *      //    pGR.Rectify(dlg.FileName, "TIFF");
             *      //}
             *     ITransformationOp TO = new RasterTransformationOpClass();
             *      ESRI.ArcGIS.Geodatabase.IGeoDataset ds= TO.Warp(pRasterLayer.Raster as ESRI.ArcGIS.Geodatabase.IGeoDataset,
             *          OriginPoints, TargetPoints,
             *          (ESRI.ArcGIS.DataSourcesRaster.esriGeoTransTypeEnum)(WarpType + 1),
             *          (esriGeoAnalysisResampleEnum)dlg.InterationType);
             *      ClsGDBDataCommon cgc = new ClsGDBDataCommon();
             *      DirectoryInfo di = new DirectoryInfo(dlg.savefilepath);
             *      string strName = di.Name;
             *      string parentpath = di.Parent.FullName;
             *      IWorkspaceFactory workspaceFactory = new RasterWorkspaceFactoryClass ();
             *      ESRI.ArcGIS.esriSystem.IPropertySet propertySet = new ESRI.ArcGIS.esriSystem.PropertySetClass();
             *      propertySet.SetProperty("DATABASE", parentpath);
             *      IWorkspace inmemWor = workspaceFactory.OpenFromFile(parentpath, 0);
             *
             *      IRasterLayer player = new RasterLayerClass();
             *      player.CreateFromDataset(ds as IRasterDataset);
             *      pMapCtr.Map.AddLayer(player as ILayer);
             *      pMapCtr.Refresh();
             *
             *      //ISaveAs2 sa = ds as ISaveAs2;
             *      ISaveAs2 sa = (ds) as ISaveAs2;
             *      if (sa != null)
             *      {
             *         sa.SaveAs(strName,   inmemWor, "TIFF");
             *         ((IRasterDataset3)ds).Refresh();
             *      }
             *
             *      MessageBox.Show("保存\"" + dlg.savefilepath + "\"成功!");
             *  }
             *  catch (SystemException ee)
             *  {
             *      MessageBox.Show(ee.Message);
             *  }
             * }
             */
        }