private void buttonX_ok_Click(object sender, EventArgs e)
        {
            if (Convert.ToInt32(textBoxX4.Text) > 10)
            {
                MessageBox.Show("对齐参考点距离超过10米,精度低于预期,不能用于对齐!");
                return;
            }
            Thread.Sleep(4000);
            ClsGDBDataCommon processDataCommon = new ClsGDBDataCommon();
            string           strInputPath      = System.IO.Path.GetDirectoryName(textBoxX2.Text);
            string           strInputName      = System.IO.Path.GetFileName(textBoxX2.Text);

            #region 读取SHP文件
            IWorkspace        pWorkspace        = processDataCommon.OpenFromShapefile(strInputPath);
            IFeatureWorkspace pFeatureWorkspace = pWorkspace as IFeatureWorkspace;
            IFeatureClass     pInputFC          = pFeatureWorkspace.OpenFeatureClass(strInputName);
            #endregion

            IFeatureLayer pFeatureLayer = new FeatureLayerClass();
            pFeatureLayer.FeatureClass = pInputFC;
            pFeatureLayer.Name         = strInputName;
            m_pMapCtl.AddLayer(pFeatureLayer as ILayer, 0);
            m_pMapCtl.ActiveView.PartialRefresh(esriViewDrawPhase.esriViewGeography, null, null);

            this.Close();
        }
        public IFeatureClass CreateShapeFile(DataSet ds, string strOutShpName, ISpatialReference pSRF)
        {
            try
            {
                DirectoryInfo    di         = new DirectoryInfo(strOutShpName);
                string           filefolder = di.Parent.FullName;
                ClsGDBDataCommon cdc        = new ClsGDBDataCommon();
                //cdc.OpenFromShapefile(filefolder);
                IFields     pFields     = new FieldsClass();
                IFieldsEdit pFieldsEdit = (IFieldsEdit)pFields;

                //设置字段
                IField     pField     = new FieldClass();
                IFieldEdit pFieldEdit = (IFieldEdit)pField;

                //创建类型为几何类型的字段
                IGeometryDef     pGeoDef     = new GeometryDefClass();
                IGeometryDefEdit pGeoDefEdit = (IGeometryDefEdit)pGeoDef;
                pGeoDefEdit.GeometryType_2 = esriGeometryType.esriGeometryPoint;

                pGeoDefEdit.HasM_2             = false;
                pGeoDefEdit.HasZ_2             = false;
                pGeoDefEdit.SpatialReference_2 = pSRF;

                pFieldEdit.Name_2        = "SHAPE";
                pFieldEdit.Type_2        = esriFieldType.esriFieldTypeGeometry;
                pFieldEdit.GeometryDef_2 = pGeoDef;
                //pFieldEdit.IsNullable_2 = true;
                //pFieldEdit.Required_2 = true;
                pFieldsEdit.AddField(pField);

                for (int i = 0; i < ds.Tables[0].Columns.Count; i++)
                {
                    pField                  = new FieldClass();
                    pFieldEdit              = (IFieldEdit)pField;
                    pFieldEdit.Name_2       = ds.Tables[0].Columns[i].ColumnName;
                    pFieldEdit.Type_2       = esriFieldType.esriFieldTypeString;
                    pFieldEdit.IsNullable_2 = true;
                    pFieldsEdit.AddField(pField);
                }

                ClsGDBDataCommon comm     = new ClsGDBDataCommon();
                IWorkspace       inmemWor = comm.OpenFromShapefile(filefolder);
                // ifeatureworkspacee
                IFeatureWorkspace pFeatureWorkspace = inmemWor as IFeatureWorkspace;
                IFeatureClass     pFeatureClass     = pFeatureWorkspace.CreateFeatureClass(di.Name, pFields, null, null, esriFeatureType.esriFTSimple, "SHAPE", "");

                return(pFeatureClass);
                //IFeatureLayer pFeatureLayer = new FeatureLayerClass();
                //pFeatureLayer.FeatureClass = pFeatureClass;
                //pFeatureLayer.Name = System.IO.Path.GetFileNameWithoutExtension(di.Name);
                //m_mapControl.AddLayer(pFeatureLayer as ILayer, 0);
                //m_mapControl.ActiveView.PartialRefresh(esriViewDrawPhase.esriViewGeography, null, null);
            }
            catch (SystemException ee)
            {
                MessageBox.Show(ee.Message);
                return(null);
            }
        }
        private void buttonX_ok_Click(object sender, EventArgs e)
        {
            string strInputPath1 = System.IO.Path.GetDirectoryName(textBoxX1.Text);
            string strInputName1 = System.IO.Path.GetFileName(textBoxX2.Text);
            string strInputPath2 = System.IO.Path.GetDirectoryName(textBoxX3.Text);
            string strInputName2 = System.IO.Path.GetFileName(textBoxX3.Text);

            if (checkBoxX1.Checked)
            {
                MessageBox.Show("做矫正对齐,同时生成数据报告");
            }
            else
            {
                MessageBox.Show("不做矫正对齐只生成数据报告");

                ClsGDBDataCommon processDataCommon = new ClsGDBDataCommon();

                #region 读取SHP文件
                IWorkspace        pWorkspace        = processDataCommon.OpenFromShapefile(strInputPath1);
                IFeatureWorkspace pFeatureWorkspace = pWorkspace as IFeatureWorkspace;
                IFeatureClass     pInputFC          = pFeatureWorkspace.OpenFeatureClass(strInputName1);
                #endregion

                IFeatureLayer pFeatureLayer = new FeatureLayerClass();
                pFeatureLayer.FeatureClass = pInputFC;
                pFeatureLayer.Name         = strInputName1;
                m_pMapCtl.AddLayer(pFeatureLayer as ILayer, 0);
                m_pMapCtl.ActiveView.PartialRefresh(esriViewDrawPhase.esriViewGeography, null, null);

                #region 读取SHP文件
                IWorkspace        pWorkspace2        = processDataCommon.OpenFromShapefile(strInputPath2);
                IFeatureWorkspace pFeatureWorkspace2 = pWorkspace2 as IFeatureWorkspace;
                IFeatureClass     pInputFC2          = pFeatureWorkspace2.OpenFeatureClass(strInputName2);
                #endregion

                IFeatureLayer pFeatureLayer2 = new FeatureLayerClass();
                pFeatureLayer2.FeatureClass = pInputFC2;
                pFeatureLayer2.Name         = strInputName2;
                m_pMapCtl.AddLayer(pFeatureLayer2 as ILayer, 0);
                m_pMapCtl.ActiveView.PartialRefresh(esriViewDrawPhase.esriViewGeography, null, null);
            }



            this.Close();
        }
        private void buttonXOK_Click(object sender, EventArgs e)
        {
            try
            {
                DirectoryInfo    di         = new DirectoryInfo(textBoxXPath.Text);
                string           filefolder = di.Parent.FullName;
                ClsGDBDataCommon cdc        = new ClsGDBDataCommon();
                //cdc.OpenFromShapefile(filefolder);
                IFields     pFields     = new FieldsClass();
                IFieldsEdit pFieldsEdit = (IFieldsEdit)pFields;

                //设置字段
                IField     pField     = new FieldClass();
                IFieldEdit pFieldEdit = (IFieldEdit)pField;

                //创建类型为几何类型的字段
                pFieldEdit.Name_2 = "shape";
                pFieldEdit.Type_2 = esriFieldType.esriFieldTypeGeometry;

                /*
                 * //不定义空间参考会报异常
                 * //为esriFieldTypeGeometry类型的字段创建几何定义,包括类型和空间参照
                 * IGeometryDef pGeoDef = new GeometryDefClass();     //The geometry definition for the field if IsGeometry is TRUE.
                 * IGeometryDefEdit pGeoDefEdit = (IGeometryDefEdit)pGeoDef;
                 * pGeoDefEdit.GeometryType_2 = esriGeometryType.esriGeometryPolygon;
                 * pGeoDefEdit.SpatialReference_2 = new UnknownCoordinateSystemClass();
                 * pGeoDefEdit.SpatialReference.SetDomain(-200, 200, -200, 200);//没有这句就抛异常来自HRESULT:0x8004120E。
                 * // pGeoDefEdit.SpatialReference_2 = pSPF;
                 * pFieldEdit.GeometryDef_2 = pGeoDef;
                 * pFieldsEdit.AddField(pField);
                 */



                IGeometryDef     pGeoDef     = new GeometryDefClass();
                IGeometryDefEdit pGeoDefEdit = (IGeometryDefEdit)pGeoDef;
                //pGeoDefEdit.AvgNumPoints_2 = 5;
                switch (comboBoxExShapeType.SelectedIndex)
                {
                case 0:
                    pGeoDefEdit.GeometryType_2 = esriGeometryType.esriGeometryPoint;
                    break;

                case 1:
                    pGeoDefEdit.GeometryType_2 = esriGeometryType.esriGeometryPolyline;
                    break;

                case 2:
                    pGeoDefEdit.GeometryType_2 = esriGeometryType.esriGeometryPolygon;
                    break;
                }
                // pGeoDefEdit.GridCount_2 = 1;
                pGeoDefEdit.HasM_2             = true;
                pGeoDefEdit.HasZ_2             = true;
                pGeoDefEdit.SpatialReference_2 = pSpaReference;
                //  pGeoDefEdit.SpatialReference_2 = new UnknownCoordinateSystemClass();//没有这一句就报错,说尝试读取或写入受保护的内存。



                // pGeoDefEdit.SpatialReference_2 = CreateProjectedCoordinateSystem();

                // pGeoDefEdit.SpatialReference.SetDomain(-8000000, 8000000, -800000, 8000000);//没有这句就抛异常来自HRESULT:0x8004120E。

                pFieldEdit.Name_2        = "SHAPE";
                pFieldEdit.Type_2        = esriFieldType.esriFieldTypeGeometry;
                pFieldEdit.GeometryDef_2 = pGeoDef;
                pFieldEdit.IsNullable_2  = true;
                pFieldEdit.Required_2    = true;
                pFieldsEdit.AddField(pField);


                //IFeatureDataset pFdataset = pFeatureWorkspace.CreateFeatureDataset("mem", null);
                //pFeatureClass = pFdataset.CreateFeatureClass("", pFields, null, null, esriFeatureType.esriFTSimple, "shape", "");
                //  pFeatureClass = pFeatureWorkspace.CreateFeatureClass("", pFields, null, null, esriFeatureType.esriFTSimple, "shape", "");


                ClsGDBDataCommon comm     = new ClsGDBDataCommon();
                IWorkspace       inmemWor = comm.OpenFromShapefile(filefolder);
                // ifeatureworkspacee
                IFeatureWorkspace pFeatureWorkspace = inmemWor as IFeatureWorkspace;
                IFeatureClass     pFeatureClass     = pFeatureWorkspace.CreateFeatureClass(di.Name, pFields, null, null, esriFeatureType.esriFTSimple, "shape", "");


                IFeatureLayer pFeatureLayer = new FeatureLayerClass();
                pFeatureLayer.FeatureClass = pFeatureClass;
                pFeatureLayer.Name         = System.IO.Path.GetFileNameWithoutExtension(di.Name);
                m_mapControl.AddLayer(pFeatureLayer as ILayer, 0);
                m_mapControl.ActiveView.PartialRefresh(esriViewDrawPhase.esriViewGeography, null, null);
            }
            catch (SystemException ee)
            {
                MessageBox.Show(ee.Message);
            }
        }
        public bool projectBackward(string szInputShpFilename, IRasterLayer pDemLayer, string szXmlFilename, string szOutShpFilename)
        {
            if (string.IsNullOrEmpty(szInputShpFilename) || pDemLayer == null || string.IsNullOrEmpty(szXmlFilename) || string.IsNullOrEmpty(szOutShpFilename))
            {
                return(false);
            }

            ClsGDBDataCommon processDataCommon = new ClsGDBDataCommon();
            string           strInputPath      = System.IO.Path.GetDirectoryName(szInputShpFilename);
            string           strInputName      = System.IO.Path.GetFileName(szInputShpFilename);

            #region 读取SHP文件
            IWorkspace        pWorkspace        = processDataCommon.OpenFromShapefile(strInputPath);
            IFeatureWorkspace pFeatureWorkspace = pWorkspace as IFeatureWorkspace;
            IFeatureClass     pInputFC          = pFeatureWorkspace.OpenFeatureClass(strInputName);
            #endregion

            #region 读取DEM文件
            IRasterLayer pRasterLayer = pDemLayer;// new RasterLayerClass();
            //pRasterLayer.CreateFromFilePath(szDemFilename);
            IRaster      pDemRaster      = pRasterLayer.Raster;
            IRasterProps pDemRasterProps = pDemRaster as IRasterProps;
            #endregion

            #region 得到曲线信息
            List <ISegmentCollection> pSegmentCollection = new List <ISegmentCollection>();
            List <IFeature>           pFeatureList       = new List <IFeature>();
            IFeatureCursor            pFCursor           = pInputFC.Search(null, false);
            IFeature pFeature = null;// pFCursor.NextFeature();
            while ((pFeature = pFCursor.NextFeature()) != null)
            {
                IPolyline pPolyline = pFeature.Shape as IPolyline;
                if (pPolyline == null)
                {
                    //pFeature = pFCursor.NextFeature();
                    continue;
                }

                IFeature pTmpFeature = pFeature;
                pFeatureList.Add(pTmpFeature);

                ISegmentCollection pCurrentSegmentCollection = pPolyline as ISegmentCollection;
                pSegmentCollection.Add(pCurrentSegmentCollection);
            }
            #endregion

            #region 读取相机姿态参数
            ClsCameraPara cameraPara = ParseXmlFileToGetPara(szXmlFilename);
            if (cameraPara == null)
            {
                return(false);
            }

            //根据姿态参数获得旋转矩阵
            OriAngle oriAngle      = new OriAngle();
            Matrix   pRotateMatrix = new Matrix(3, 3);

            oriAngle.kap = cameraPara.dKappa;
            oriAngle.omg = cameraPara.dOmg;
            oriAngle.phi = cameraPara.dPhi;
            if (!ClsGetCameraView.OPK2RMat(oriAngle, ref pRotateMatrix))
            {
                return(false);
            }
            #endregion

            #region 创建输出SHP文件
            string strOutputPath      = System.IO.Path.GetDirectoryName(szOutShpFilename);
            string strOutputName      = System.IO.Path.GetFileName(szOutShpFilename);
            string strPointOutputName = System.IO.Path.GetFileNameWithoutExtension(szOutShpFilename) + "_point" + System.IO.Path.GetExtension(szOutShpFilename);

            //设置空间参考
            ISpatialReference pSpatialRef = new UnknownCoordinateSystemClass(); //没有这一句就报错,说尝试读取或写入受保护的内存。
            pSpatialRef.SetDomain(MIN_INT, MAX_INT, MIN_INT, MAX_INT);          //没有这句就抛异常来自HRESULT:0x8004120E。
            pSpatialRef.SetZDomain(MIN_INT, MAX_INT);

            //创建POLYLINE文件的字段
            IFields pFields = pInputFC.Fields;

            //创建点SHP文件的字段
            IFields     pPointFields = new FieldsClass();
            IFieldsEdit pFieldsEdit  = (IFieldsEdit)pPointFields;

            //设置字段
            IField     pField     = new FieldClass();
            IFieldEdit pFieldEdit = (IFieldEdit)pField;

            //创建类型为几何类型的字段
            pFieldEdit.Name_2 = "shape";
            pFieldEdit.Type_2 = esriFieldType.esriFieldTypeGeometry;

            IGeometryDef     pGeoDef     = new GeometryDefClass();
            IGeometryDefEdit pGeoDefEdit = (IGeometryDefEdit)pGeoDef;
            pGeoDefEdit.GeometryType_2     = esriGeometryType.esriGeometryPoint;
            pGeoDefEdit.HasM_2             = false;
            pGeoDefEdit.HasZ_2             = false;
            pGeoDefEdit.SpatialReference_2 = pSpatialRef;
            //pFieldEdit.Name_2 = "SHAPE";
            //pFieldEdit.Type_2 = esriFieldType.esriFieldTypeGeometry;
            pFieldEdit.GeometryDef_2 = pGeoDef;
            pFieldEdit.IsNullable_2  = true;
            pFieldEdit.Required_2    = true;
            pFieldsEdit.AddField(pField);

            IFeatureClass pOutputFC      = processDataCommon.CreateShapefile(strOutputPath, strOutputName, pFields, pSpatialRef);
            IFeatureClass pOutputPointFC = processDataCommon.CreateShapefile(strOutputPath, strPointOutputName, pPointFields, pSpatialRef);
            if (pOutputFC == null || pOutputPointFC == null)
            {
                return(false);
            }
            #endregion

            #region 节点位置反投影
            List <List <Pt2d> > ptImageFeaturePoints            = new List <List <Pt2d> >();
            List <List <Pt2d> > ptImageFeaturePointsForPointShp = new List <List <Pt2d> >();
            ClsGetCameraView    pGetCameraView = new ClsGetCameraView();

            for (int i = 0; i < pSegmentCollection.Count; i++)
            {
                List <Pt2d> ptSingleImageFeaturePoints            = new List <Pt2d>();
                List <Pt2d> ptSingleImageFeaturePointsForPointShp = new List <Pt2d>();
                for (int j = 0; j < pSegmentCollection[i].SegmentCount; j++)
                {
                    ISegment pCurrentSegment = pSegmentCollection[i].get_Segment(j);
                    if (pCurrentSegment == null)
                    {
                        continue;
                    }

                    //直线
                    if (pCurrentSegment.GeometryType == esriGeometryType.esriGeometryLine)
                    {
                        ILine pLine = pCurrentSegment as ILine;

                        //起始点
                        IPoint ptFrom   = pLine.FromPoint;
                        Pt2d   ptFrom2d = getProjectBackwardPoint(ptFrom, pDemRaster, pGetCameraView, pRotateMatrix, cameraPara);
                        if (ptFrom2d != null)
                        {
                            ptSingleImageFeaturePoints.Add(ptFrom2d);
                            ptSingleImageFeaturePointsForPointShp.Add(ptFrom2d);
                        }

                        //终止点
                        IPoint ptTo   = pLine.ToPoint;
                        Pt2d   ptTo2d = getProjectBackwardPoint(ptTo, pDemRaster, pGetCameraView, pRotateMatrix, cameraPara);
                        if (ptTo2d != null)
                        {
                            ptSingleImageFeaturePoints.Add(ptTo2d);
                            if (j == pSegmentCollection[i].SegmentCount - 1)   //最后一条直线段
                            {
                                ptSingleImageFeaturePointsForPointShp.Add(ptTo2d);
                            }
                        }
                    }
                    else if (pCurrentSegment.GeometryType == esriGeometryType.esriGeometryCircularArc) //圆弧
                    {
                        ICircularArc pCircularArc  = pCurrentSegment as ICircularArc;
                        double       dbCurveLength = pCircularArc.Length;
                        int          nCount        = 15;
                        double       dbOffset      = dbCurveLength / nCount;
                        for (int nCurrent = 0; nCurrent < nCount; nCurrent++)
                        {
                            IPoint ptCurrent = new Point();
                            pCircularArc.QueryPoint(esriSegmentExtension.esriNoExtension, nCurrent * dbOffset, false, ptCurrent);
                            Pt2d ptCurrentImagePoint = getProjectBackwardPoint(ptCurrent, pDemRaster, pGetCameraView, pRotateMatrix, cameraPara);
                            if (ptCurrentImagePoint != null)
                            {
                                ptSingleImageFeaturePoints.Add(ptCurrentImagePoint);

                                //只保留端点的信息,用于创建点SHP图层
                                if (nCurrent == 0 || nCurrent == nCount - 1)
                                {
                                    ptSingleImageFeaturePointsForPointShp.Add(ptCurrentImagePoint);
                                }
                            }
                        }
                    }
                    else
                    {
                        continue;
                    }
                }

                ptImageFeaturePoints.Add(ptSingleImageFeaturePoints);
                ptImageFeaturePointsForPointShp.Add(ptSingleImageFeaturePointsForPointShp);
            }
            #endregion

            #region 写入POLYLINE节点信息
            IDataset   dataset   = (IDataset)pOutputFC;
            IWorkspace workspace = dataset.Workspace;

            //Cast for an IWorkspaceEdit
            IWorkspaceEdit workspaceEdit = workspace as IWorkspaceEdit;
            workspaceEdit.StartEditing(true);
            workspaceEdit.StartEditOperation();

            //////////////////////////////////////////////////////////////////////////
            //整条路线的添加
            IFeatureCursor pOutFCursor = null;
            for (int i = 0; i < ptImageFeaturePoints.Count; i++)
            {
                int            nPtsCount      = ptImageFeaturePoints[i].Count;
                IFeatureBuffer pFeatureBuffer = pOutputFC.CreateFeatureBuffer();
                pOutFCursor = pOutputFC.Insert(true);

                IPolyline        pPolyline        = new PolylineClass();
                IPointCollection pPointCollection = pPolyline as IPointCollection;

                for (int j = 0; j < nPtsCount; j++)
                {
                    IPoint pt = new PointClass();
                    pt.PutCoords(ptImageFeaturePoints[i][j].X, ptImageFeaturePoints[i][j].Y);
                    pPointCollection.AddPoint(pt);
                }

                if (!ClsGDBDataCommon.CopyFeatureFieldValue(pFeatureList[i], pFeatureBuffer as IFeature))
                {
                    continue;
                }

                pFeatureBuffer.Shape = pPolyline as IGeometry;
                object featureOID = pOutFCursor.InsertFeature(pFeatureBuffer);
            }
            pOutFCursor.Flush();

            //分段路线的添加
            for (int i = 0; i < ptImageFeaturePoints.Count; i++)
            {
                int nPtsCount = ptImageFeaturePoints[i].Count;
                for (int j = 0; j < nPtsCount - 1; j++)
                {
                    IFeatureBuffer pFeatureBuffer = pOutputFC.CreateFeatureBuffer();
                    pOutFCursor = pOutputFC.Insert(true);
                    IPolyline        pPolyline        = new PolylineClass();
                    IPointCollection pPointCollection = pPolyline as IPointCollection;

                    IPoint pt = new PointClass();
                    pt.PutCoords(ptImageFeaturePoints[i][j].X, ptImageFeaturePoints[i][j].Y);
                    pPointCollection.AddPoint(pt);
                    pt.PutCoords(ptImageFeaturePoints[i][j + 1].X, ptImageFeaturePoints[i][j + 1].Y);
                    pPointCollection.AddPoint(pt);

                    if (!ClsGDBDataCommon.CopyFeatureFieldValue(pFeatureList[i], pFeatureBuffer as IFeature))
                    {
                        continue;
                    }
                    pFeatureBuffer.Shape = pPolyline as IGeometry;
                    object featureOID = pOutFCursor.InsertFeature(pFeatureBuffer);
                }
            }
            pOutFCursor.Flush();
            //featureCursor.Flush();
            workspaceEdit.StopEditOperation();
            workspaceEdit.StopEditing(true);
            #endregion

            #region 点SHP文件的添加
            IDataset   pPointDataset   = (IDataset)pOutputPointFC;
            IWorkspace pPointWorkspace = pPointDataset.Workspace;

            //Cast for an IWorkspaceEdit
            IWorkspaceEdit pPointWorkspaceEdit = pPointWorkspace as IWorkspaceEdit;
            pPointWorkspaceEdit.StartEditing(true);
            pPointWorkspaceEdit.StartEditOperation();

            IFeatureCursor pOutPointFCursor = null;
            for (int i = 0; i < ptImageFeaturePointsForPointShp.Count; i++)
            {
                //int nSegmentCount = pSegmentCollection[i].SegmentCount;

                //IMultipoint pMultiPoint = new MultipointClass();
                for (int j = 0; j < ptImageFeaturePointsForPointShp[i].Count; j++)
                {
                    IFeatureBuffer pFeatureBuffer = pOutputPointFC.CreateFeatureBuffer();
                    pOutPointFCursor = pOutputPointFC.Insert(true);
                    IPoint pt   = new PointClass();
                    Pt2d   pt2d = ptImageFeaturePointsForPointShp[i][j];
                    pt.PutCoords(pt2d.X, pt2d.Y);

                    //CopyFeatureField(pFeatureList[i], pFeatureBuffer);
                    pFeatureBuffer.Shape = pt as IGeometry;
                    object featureOID = pOutPointFCursor.InsertFeature(pFeatureBuffer);
                }
            }
            pOutPointFCursor.Flush();

            //featureCursor.Flush();
            pPointWorkspaceEdit.StopEditOperation();
            pPointWorkspaceEdit.StopEditing(true);
            #endregion

            return(true);
        }
Exemple #6
0
        private void btnOK_Click(object sender, EventArgs e)
        {
            //首先进行坐标系x,y变换
            //string fileExpTran;//进行x,y做表转换后输出的tiff文件存储路径,用这一文件在进行后期的Z转换
            //fileExpTran = System.IO.Path.GetDirectoryName(LayerExpName) +"\\"+ System.IO.Path.GetFileNameWithoutExtension(LayerExpName)+"XY.tif";
            //try
            //{
            //    if (NorthEastToEastNorth(pRasterLayer, LayerExpName))
            //    {
            //        RasterLayerClass rasterlayer = new RasterLayerClass();
            //        rasterlayer.CreateFromFilePath(LayerExpName);
            //        IRaster2 pRaster2 = rasterlayer.Raster as IRaster2;
            //        IRasterDataset2 pRasterDataset = pRaster2.RasterDataset as IRasterDataset2;
            //        ChangeRasterValue(pRasterDataset, -1, 0);
            //        pMapControl.AddLayer(rasterlayer as ILayer);
            //        this.Close();
            //    }
            //}
            //catch (System.Exception ex)
            //{
            //    MessageBox.Show(ex.Message);
            //}

            try
            {
                IFeatureClass    pFC        = pFeatureLayer.FeatureClass;
                IDataset         pDS        = pFC as IDataset;
                IWorkspace       pWS        = pDS.Workspace;
                string           filedir    = pWS.PathName;
                string           fdestname  = System.IO.Path.GetFileNameWithoutExtension(txtFeatureName.Text);
                ClsGDBDataCommon CGD        = new ClsGDBDataCommon();
                IWorkspace       pTargetWS  = CGD.OpenFromShapefile(txtLayerExp.Text);
                IWorkspace2      workspace2 = pTargetWS as IWorkspace2;

                if (workspace2.get_NameExists(ESRI.ArcGIS.Geodatabase.esriDatasetType.esriDTFeatureClass, fdestname))
                {
                    MessageBox.Show("目标文件已存在!");
                    return;
                }
                //shape文件直接拷贝后再修改
                if (pDS.CanCopy() == true)
                {
                    pDS.Copy(txtFeatureName.Text, pTargetWS);
                    IFeatureWorkspace pFW = pTargetWS as IFeatureWorkspace;
                    pTargetFeatureClass = pFW.OpenFeatureClass(txtFeatureName.Text);
                    TransCoordiante(pFW as IWorkspace, pTargetFeatureClass);
                }
                //表示为gdb的feature,只能往gdb拷贝
                else
                {
                    // Create workspace name objects.
                    IWorkspaceName sourceWorkspaceName = new WorkspaceNameClass();
                    IWorkspaceName targetWorkspaceName = new WorkspaceNameClass();
                    IName          targetName          = (IName)targetWorkspaceName;

                    // Set the workspace name properties.
                    sourceWorkspaceName.PathName = pWS.PathName;
                    sourceWorkspaceName.WorkspaceFactoryProgID =
                        "esriDataSourcesGDB.FileGDBWorkspaceFactory";
                    // targetWorkspaceName.PathName = @"PartialMontgomery.gdb";
                    targetWorkspaceName.PathName = txtLayerExp.Text;
                    targetWorkspaceName.WorkspaceFactoryProgID =
                        "esriDataSourcesGDB.FileGDBWorkspaceFactory";

                    //if (txtLayerExp.Text.Length>4 && txtLayerExp.Text.Substring(txtLayerExp.Text.Length-4,4) == ".gdb")
                    //{
                    //    targetWorkspaceName.WorkspaceFactoryProgID =
                    //      "esriDataSourcesGDB.FileGDBWorkspaceFactory";
                    //}
                    //else
                    //{
                    //    targetWorkspaceName.WorkspaceFactoryProgID =
                    //     "esriDataSourcesFile.ShapefileWorkspaceFactory";
                    //}

                    // Create a name object for the source feature class.
                    IFeatureClassName featureClassName = new FeatureClassNameClass();

                    // Set the featureClassName properties.
                    IDatasetName sourceDatasetName = (IDatasetName)featureClassName;
                    sourceDatasetName.WorkspaceName = sourceWorkspaceName;
                    sourceDatasetName.Name          = pDS.BrowseName;
                    IName sourceName = (IName)sourceDatasetName;


                    // Create an enumerator for source datasets.
                    IEnumName     sourceEnumName     = new NamesEnumeratorClass();
                    IEnumNameEdit sourceEnumNameEdit = (IEnumNameEdit)sourceEnumName;

                    // Add the name object for the source class to the enumerator.
                    sourceEnumNameEdit.Add(sourceName);

                    // Create a GeoDBDataTransfer object and a null name mapping enumerator.
                    IGeoDBDataTransfer geoDBDataTransfer = new GeoDBDataTransferClass();
                    IEnumNameMapping   enumNameMapping   = null;

                    // Use the data transfer object to create a name mapping enumerator.
                    Boolean conflictsFound = geoDBDataTransfer.GenerateNameMapping(sourceEnumName,
                                                                                   targetName, out enumNameMapping);
                    enumNameMapping.Reset();
                    //修改拷贝的文件名
                    INameMapping nameMapping = enumNameMapping.Next();
                    if ((nameMapping != null))
                    {
                        nameMapping.TargetName = txtFeatureName.Text;
                    }

                    // Check for conflicts.
                    //if (conflictsFound)
                    //{
                    //    // Iterate through each name mapping.
                    //    INameMapping nameMapping = null;
                    //    while ((nameMapping = enumNameMapping.Next()) != null)
                    //    {
                    //        // Resolve the mapping's conflict (if there is one).
                    //        if (nameMapping.NameConflicts)
                    //        {
                    //            nameMapping.TargetName = nameMapping.GetSuggestedName(targetName);
                    //        }

                    //        // See if the mapping's children have conflicts.
                    //        IEnumNameMapping childEnumNameMapping = nameMapping.Children;
                    //        if (childEnumNameMapping != null)
                    //        {
                    //            childEnumNameMapping.Reset();

                    //            // Iterate through each child mapping.
                    //            INameMapping childNameMapping = null;
                    //            while ((childNameMapping = childEnumNameMapping.Next()) != null)
                    //            {
                    //                if (childNameMapping.NameConflicts)
                    //                {
                    //                    childNameMapping.TargetName = childNameMapping.GetSuggestedName
                    //                        (targetName);
                    //                }
                    //            }
                    //        }
                    //    }
                    //}

                    // Start the transfer.
                    geoDBDataTransfer.Transfer(enumNameMapping, targetName);
                    IFeatureWorkspace pFW = CGD.OpenFromFileGDB(txtLayerExp.Text) as IFeatureWorkspace;
                    pTargetFeatureClass = pFW.OpenFeatureClass(txtFeatureName.Text);
                    TransCoordiante(pFW as IWorkspace, pTargetFeatureClass);
                }

                if (pTargetFeatureClass != null)
                {
                    //添加到图层中
                    IFeatureLayer featureLayer = new FeatureLayerClass();
                    featureLayer.FeatureClass = pTargetFeatureClass;
                    featureLayer.Name         = fdestname;
                    pMapControl.AddLayer(featureLayer as ILayer);
                    pMapControl.ActiveView.PartialRefresh(esriViewDrawPhase.esriViewGeography, null, null);
                }

                this.DialogResult = DialogResult.OK;
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
Exemple #7
0
 private void CreatePolyline()
 {
     try
     {
         //建立shape字段
         IFields     pFields     = new FieldsClass();
         IFieldsEdit pFieldsEdit = (IFieldsEdit)pFields;
         IField      pField      = new FieldClass();
         IFieldEdit  pFieldEdit  = (IFieldEdit)pField;
         pFieldEdit.Name_2 = "Shape";
         pFieldEdit.Type_2 = esriFieldType.esriFieldTypeGeometry;
         //设置Geometry definition
         IGeometryDef     pGeometryDef     = new GeometryDefClass();
         IGeometryDefEdit pGeometryDefEdit = (IGeometryDefEdit)pGeometryDef;
         pGeometryDefEdit.GeometryType_2     = esriGeometryType.esriGeometryPolyline; //点、线、面
         pGeometryDefEdit.SpatialReference_2 = new UnknownCoordinateSystemClass();
         pGeometryDefEdit.SpatialReference.SetDomain(-8000000, 8000000, -800000, 8000000);
         pFieldEdit.GeometryDef_2 = pGeometryDef;
         pFieldsEdit.AddField(pField);
         IFeatureClass    pFeatureClass = null;
         DirectoryInfo    di            = new DirectoryInfo(textBoxX1.Text);
         string           filefolder    = di.Parent.FullName;
         ClsGDBDataCommon comm          = new ClsGDBDataCommon();
         IWorkspace       inmemWor      = comm.OpenFromShapefile(filefolder);
         // ifeatureworkspacee
         IFeatureWorkspace pFeatureWorkspace = inmemWor as IFeatureWorkspace;
         try
         {
             pFeatureClass = pFeatureWorkspace.CreateFeatureClass(di.Name, pFields, null, null, esriFeatureType.esriFTSimple, "shape", "");
         }
         catch (SystemException eee)
         {
             if (eee.Message == "The table already exists.")
             {
                 pFeatureClass = pFeatureWorkspace.OpenFeatureClass(textBoxX1.Text);
                 ITable ptable = pFeatureClass as ITable;
                 ptable.DeleteSearchedRows(null);
             }
             //MessageBox.Show(eee.Message);
         }
         List <Pt2d>    ImagePoint = new List <Pt2d>();
         IQueryFilter   pQF        = new QueryFilterClass();
         IFeatureCursor pFC        = pPointFeatureclass.Search(null, false);
         IFeature       pF         = pFC.NextFeature();
         while (pF != null)
         {
             IPoint pPoint   = pF.Shape as IPoint;
             Pt2d   imgpoint = new Pt2d();
             imgpoint.X = pPoint.X;
             imgpoint.Y = pPoint.Y;
             ImagePoint.Add(imgpoint);
             pF = pFC.NextFeature();
         }
         Pt2d[] originPt = new Pt2d[ImagePoint.Count];
         for (int i = 0; i < ImagePoint.Count; i++)
         {
             Pt2d pttemp = new Pt2d();
             pttemp.X    = ImagePoint[i].X;
             pttemp.Y    = ImagePoint[i].Y;
             originPt[i] = pttemp;
         }
         // Pt2d[] ptImagePos = new Pt2d[ImagePoint.Count];
         Pt2d[] ptImagePos = null;
         calImagePos(pRaster, originPt, textBoxX2.Text, out ptImagePos);
         IPointCollection pPointC = new PolylineClass();
         for (int i = 0; i < ptImagePos.Length; i++)
         {
             if (ptImagePos[i] == null)
             {
                 continue;
             }
             IPoint pPoint = new PointClass();
             pPoint.X = ptImagePos[i].X;
             pPoint.Y = ptImagePos[i].Y;
             pPointC.AddPoint(pPoint);
         }
         ISegmentCollection pSegC = new PolylineClass();
         pSegC.AddSegment(pPointC as ISegment);
         IFeature pFeatureTemp = pFeatureClass.CreateFeature();
         pFeatureTemp.Shape = pPointC as IGeometry;
         pFeatureTemp.Store();
     }
     catch (Exception ee)
     {
         MessageBox.Show(ee.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
     }
 }