示例#1
0
        /// <summary>
        /// Occurs when this command is clicked
        /// </summary>
        public override void OnClick()
        {
            // TODO: Add CmdRasterRegister.OnClick implementation
            IMapControl3 pMapCtrl = null;

            if (m_hookHelper.Hook is IToolbarControl)
            {
                if (((IToolbarControl)m_hookHelper.Hook).Buddy is IMapControl3)
                {
                    pMapCtrl = (IMapControl3)((IToolbarControl)m_hookHelper.Hook).Buddy;
                }
            }
            //In case the container is MapControl
            else if (m_hookHelper.Hook is IMapControl3)
            {
                pMapCtrl = (IMapControl3)m_hookHelper.Hook;
            }
            else
            {
                return;
            }

            System.GC.Collect();
            FrmSiftMatching frmSiftMatching = new FrmSiftMatching(pMapCtrl, pRasterLayer);

            if (frmSiftMatching.ShowDialog() == DialogResult.OK)
            {
                ClsSiftMatching pSiftMatching = new ClsSiftMatching();
                double[]        dbMatchPts;
                int             nCount = 0;
                if (pSiftMatching.siftMatching(frmSiftMatching.m_pSiftMatchPara, out dbMatchPts, out nCount))
                {
                    IRaster2     pLeftRaster    = frmSiftMatching.m_pRasterLeft;
                    IRasterProps pLeftProps     = pLeftRaster as IRasterProps;
                    IPoint       pLeftLowerLeft = pLeftProps.Extent.LowerLeft;

                    IRaster2     pRightRaster    = frmSiftMatching.m_pRasterRight;
                    IRasterProps pRightProps     = pRightRaster as IRasterProps;
                    IPoint       pRightLowerLeft = pRightProps.Extent.LowerLeft;

                    //SIFT匹配出来的都是影像坐标,应该先转换成MAPCTRL单位为准
                    for (int i = 0; i < nCount; i++)
                    {
                        int x = (int)dbMatchPts[4 * i];
                        int y = (int)(dbMatchPts[4 * i + 1]);
                        //y = (int)(-1*dbMatchPts[4 * i + 1]);
                        pLeftRaster.PixelToMap(x, y, out dbMatchPts[4 * i], out dbMatchPts[4 * i + 1]);

                        x = (int)dbMatchPts[4 * i + 2];
                        y = (int)(dbMatchPts[4 * i + 3]);
                        //y = (int)(-1*dbMatchPts[4 * i + 3]);
                        pRightRaster.PixelToMap(x, y, out dbMatchPts[4 * i + 2], out dbMatchPts[4 * i + 3]);

                        #region 废弃代码
                        //int x = (int)dbMatchPts[4 * i];
                        //int y = (int)(dbMatchPts[4 * i + 1]);
                        //dbMatchPts[4 * i] = x * pLeftProps.MeanCellSize().X + pLeftLowerLeft.X;
                        //dbMatchPts[4 * i + 1] = y * pLeftProps.MeanCellSize().Y + pLeftLowerLeft.Y;

                        //x = (int)dbMatchPts[4 * i + 2];
                        //y = (int)(dbMatchPts[4 * i + 3]);
                        //dbMatchPts[4 * i + 2] = x * pRightProps.MeanCellSize().X + pRightLowerLeft.X;
                        //dbMatchPts[4 * i + 3] = y * pRightProps.MeanCellSize().Y + pRightLowerLeft.Y;
                        //IPoint ptFrom = pMapCtrl.ToMapPoint(Convert.ToInt32(dbMatchPts[4 * i + 0]), Convert.ToInt32(dbMatchPts[4 * i + 1]));
                        //IPoint ptTo = pMapCtrl.ToMapPoint(Convert.ToInt32(dbMatchPts[4 * i + 2]), Convert.ToInt32(dbMatchPts[4 * i + 3]));

                        //dbMatchPts[4 * i + 0] = ptFrom.X;
                        //dbMatchPts[4 * i + 1] = ptFrom.Y;
                        //dbMatchPts[4 * i + 2] = ptTo.X;
                        //dbMatchPts[4 * i + 3] = ptTo.Y;
                        #endregion
                    }
                    pSiftMatching.outputMatchPointsToFile("d:\\b.txt", dbMatchPts, nCount);

                    //添加到控制点中
                    m_FrmLink.DelAllPoints();
                    for (int i = 0; i < nCount; i++)
                    {
                        IPoint ptOrg = new ESRI.ArcGIS.Geometry.PointClass();
                        ptOrg.PutCoords(dbMatchPts[4 * i], dbMatchPts[4 * i + 1]);
                        m_FrmLink.OriginPoints.AddPoint(ptOrg);

                        IPoint ptTarget = new ESRI.ArcGIS.Geometry.PointClass();
                        ptTarget.PutCoords(dbMatchPts[4 * i + 2], dbMatchPts[4 * i + 3]);
                        m_FrmLink.TargetPoints.AddPoint(ptTarget);
                    }
                    m_FrmLink.RefreshControlAllPoints();
                    m_FrmLink.Show();
                    pMapCtrl.ActiveView.PartialRefresh(esriViewDrawPhase.esriViewGeography, null, null);
                    //MessageBox.Show("匹配成功!");
                }
                else
                {
                    MessageBox.Show("匹配失败!");
                }

                //把临时创建的文件删除
                try
                {
                    string szLeftFilename  = frmSiftMatching.m_pSiftMatchPara.szLeftFilename;
                    string szRightFilename = frmSiftMatching.m_pSiftMatchPara.szRightFilename;
                    if (System.IO.File.Exists(szLeftFilename))
                    {
                        System.IO.File.Delete(szLeftFilename);
                    }

                    if (System.IO.File.Exists(szRightFilename))
                    {
                        System.IO.File.Delete(szRightFilename);
                    }
                }
                catch (System.Exception ex)
                {
                    return;
                }
            }
        }