예제 #1
0
        //}

        /// <summary>
        /// 矢量转换函数
        /// </summary>
        private void exprotMatrix()
        {
            ClsGDBDataCommon comm = new ClsGDBDataCommon();

            if (!textOutData.Text.EndsWith("shp"))
            {
                //timerShow.Start();
                MessageBox.Show("输出文件名不是shp文件!");
                return;
            }
            object featureOID;

            String strFullName = textOutData.Text;
            string strPath     = System.IO.Path.GetDirectoryName(strFullName);
            string strName     = System.IO.Path.GetFileName(strFullName);

            ILayer player = null;

            for (int i = 0; i < pMapControl.Map.LayerCount; i++)
            {
                if (pMapControl.Map.Layer[i].Name == cmbLayer.Text)
                {
                    player = pMapControl.Map.Layer[i];
                    break;
                }
            }
            IFeatureLayer pFlayer = player as IFeatureLayer;
            IFeatureClass pFclass = pFlayer.FeatureClass;
            IFields       pFields = pFclass.Fields;
            //设置空间参考
            ISpatialReference pSpatialRef;
            IGeoDataset       pGeo = (IGeoDataset)pFlayer;

            pSpatialRef = pGeo.SpatialReference;

            IFeatureClass pFtClassNew = comm.CreateShapefile(strPath, strName, pFields, pSpatialRef);
            //for (int i = 0; i < pFtClassNew.Fields.FieldCount;i++ )
            //{
            //    if (pFtClassNew.Fields.get_Field(i).Type != esriFieldType.esriFieldTypeGeometry &&
            //      pFtClassNew.Fields.get_Field(i).Type != esriFieldType.esriFieldTypeOID)
            //    {
            //        pFtClassNew.Fields.get_Field(i).IsNullable = true;
            //    }
            //}


            IFeatureCursor pFC = pFlayer.FeatureClass.Search(null, false);
            IFeature       pF  = pFC.NextFeature();

            while (pF != null)
            {
                if (pFclass.ShapeType == esriGeometryType.esriGeometryMultipoint || pFclass.ShapeType == esriGeometryType.esriGeometryPoint)
                {
                    double[] oldarray = new double[3];
                    double[] newarray = new double[3];
                    Matrix   matrix   = new Matrix();

                    IPoint pPointOld = pF.Shape as IPoint;
                    IPoint pPointNew = new PointClass();
                    oldarray[0] = pPointOld.X;
                    oldarray[1] = pPointOld.Y;
                    if (pPointOld.Z.ToString() == "NaN" || pPointOld.Z.ToString() == "非数字")
                    {
                        oldarray[2] = 0;//pPointOld.Z;
                    }
                    else
                    {
                        oldarray[2] = pPointOld.Z;
                    }
                    matrix.coord_Trans(oldarray, rotateMat, tranVec, newarray);

                    pPointNew.X = newarray[0];
                    pPointNew.Y = newarray[1];
                    if (pPointOld.Z.ToString() == "NaN" || pPointOld.Z.ToString() == "非数字")
                    {
                        pPointNew.Z = 0;
                    }
                    else
                    {
                        pPointNew.Z = newarray[2];
                    }


                    IFeature pFeatureTemp = pFtClassNew.CreateFeature();
                    pFeatureTemp.Shape = pPointNew as IGeometry;
                    ClsGDBDataCommon.CopyFeatureFieldValue(pF, pFeatureTemp);
                    pFeatureTemp.Store();
                }
                else if (pFclass.ShapeType == esriGeometryType.esriGeometryLine || pFclass.ShapeType == esriGeometryType.esriGeometryPolyline)
                {
                    double[] oldarrayS = new double[3];
                    double[] oldarrayT = new double[3];
                    double[] newarrayS = new double[3];
                    double[] newarrayT = new double[3];
                    Matrix   matrix    = new Matrix();

                    ISegmentCollection pSegCOld = pF.Shape as ISegmentCollection;
                    ISegmentCollection pSegCNew = new PolylineClass();
                    // ISegment pSegmentNEW = new LineClass();
                    //ILine pLine = new LineClass();
                    IPoint pPointF = new PointClass();
                    IPoint pPointT = new PointClass();

                    for (int i = 0; i < pSegCOld.SegmentCount; i++)
                    {
                        ISegment pSegmentNEW = new LineClass();
                        ISegment pseg        = pSegCOld.Segment[i];
                        oldarrayS[0] = pseg.FromPoint.X;
                        oldarrayS[1] = pseg.FromPoint.Y;
                        if (pseg.FromPoint.Z.ToString() == "NaN" || pseg.FromPoint.Z.ToString() == "非数字")
                        {
                            oldarrayS[2] = 0;
                        }
                        else
                        {
                            oldarrayS[2] = pseg.FromPoint.Z;
                        }
                        matrix.coord_Trans(oldarrayS, rotateMat, tranVec, newarrayS);
                        pPointF.X = newarrayS[0];
                        pPointF.Y = newarrayS[1];
                        if (pseg.FromPoint.Z.ToString() == "NaN" || pseg.FromPoint.Z.ToString() == "非数字")
                        {
                            pPointF.Z = 0;
                        }
                        else
                        {
                            pPointF.Z = newarrayS[2];
                        }
                        pSegmentNEW.FromPoint = pPointF;
                        oldarrayT[0]          = pseg.ToPoint.X;
                        oldarrayT[1]          = pseg.ToPoint.Y;
                        if (pseg.ToPoint.Z.ToString() == "NaN" || pseg.ToPoint.Z.ToString() == "非数字")
                        {
                            oldarrayT[2] = 0;
                        }
                        else
                        {
                            oldarrayT[2] = pseg.ToPoint.Z;
                        }
                        matrix.coord_Trans(oldarrayT, rotateMat, tranVec, newarrayT);
                        pPointT.X = newarrayT[0];
                        pPointT.Y = newarrayT[1];
                        if (pseg.ToPoint.Z.ToString() == "NaN" || pseg.ToPoint.Z.ToString() == "非数字")
                        {
                            pPointT.Z = 0;
                        }
                        else
                        {
                            pPointT.Z = newarrayT[2];
                        }
                        pSegmentNEW.ToPoint = pPointT;

                        pSegCNew.AddSegment(pSegmentNEW);
                    }
                    IFeature pFeatureTemp = pFtClassNew.CreateFeature();
                    pFeatureTemp.Shape = pSegCNew as IGeometry;
                    ClsGDBDataCommon.CopyFeatureFieldValue(pF, pFeatureTemp);
                    pFeatureTemp.Store();
                }

                else if (pFclass.ShapeType == esriGeometryType.esriGeometryPolygon)
                {
                    double[] oldarrayS = new double[3];
                    double[] oldarrayT = new double[3];
                    double[] newarrayS = new double[3];
                    double[] newarrayT = new double[3];
                    Matrix   matrix    = new Matrix();

                    IGeometryCollection pGCollectionOld = pF.Shape as IGeometryCollection;
                    IGeometryCollection pGCollectionNew = new PolygonClass();
                    ISegmentCollection  psegCOld        = new RingClass();


                    //ILine pLine = new LineClass();
                    IPoint pPointF = new PointClass();
                    IPoint pPointT = new PointClass();

                    //IPointCollection pPointCOld = new PolygonClass();
                    //IPointCollection pPointCNew = new PolygonClass();
                    //pPointCOld=pF.Shape as IPointCollection;
                    //for (int i = 0; i < pPointCOld.PointCount;i++ )
                    //{
                    //    pPointF = pPointCOld.Point[i];
                    //    oldarrayS[0] = pPointF.X;
                    //    oldarrayS[0] = pPointF.Y;
                    //    if (pPointF.Z.ToString() == "NaN" || pPointF.Z.ToString() == "非数字")
                    //    {
                    //        oldarrayS[2] = 0;//pPointOld.Z;
                    //    }
                    //    else
                    //    {
                    //        oldarrayS[2] = pPointF.Z;
                    //    }
                    //    matrix.coord_Trans(oldarrayS, rotateMat, tranVec, newarrayT);
                    //    pPointT.X = newarrayT[0];
                    //    pPointT.Y = newarrayT[1];
                    //    if (pPointF.Z.ToString() == "NaN" || pPointF.Z.ToString() == "非数字")
                    //    {
                    //        pPointT.Z = 0;
                    //    }
                    //    else
                    //    {
                    //        pPointT.Z = newarrayT[2];
                    //    }
                    //    pPointCNew.AddPoint(pPointT);
                    //}
                    //IPolygon pPolygon = pPointCNew as IPolygon;
                    //pPolygon.Close();
                    //IFeature pFeatureTemp = pFtClassNew.CreateFeature();
                    //pFeatureTemp.Shape = pPolygon as IGeometry;
                    //CopyFeatureField(pF, pFeatureTemp);
                    //pFeatureTemp.Store();

                    ///方法一
                    //IPolygon pPolygon=pF.Shape as IPolygon;
                    //IRing pExterRingOld = new RingClass();
                    //IRing pExterRingNew = new RingClass();

                    //for (int i = 0; i<pPolygon.ExteriorRingCount;i++ )
                    //{
                    //    pExterRingOld = pPolygon.FindExteriorRing(null);
                    //}


                    //方法二
                    for (int i = 0; i < pGCollectionOld.GeometryCount; i++)
                    {
                        psegCOld = pGCollectionOld.Geometry[i] as ISegmentCollection;
                        ISegmentCollection pSegCNew = new RingClass();
                        for (int j = 0; j < psegCOld.SegmentCount - 1; j++)
                        {
                            ISegment pSegmentNEW = new LineClass();
                            ISegment pseg        = psegCOld.Segment[j];
                            oldarrayS[0] = pseg.FromPoint.X;
                            oldarrayS[1] = pseg.FromPoint.Y;
                            if (pseg.FromPoint.Z.ToString() == "NaN" || pseg.FromPoint.Z.ToString() == "非数字")
                            {
                                oldarrayS[2] = 0;
                            }
                            else
                            {
                                oldarrayS[2] = pseg.FromPoint.Z;
                            }
                            matrix.coord_Trans(oldarrayS, rotateMat, tranVec, newarrayS);
                            pPointF.X = newarrayS[0];
                            pPointF.Y = newarrayS[1];
                            if (pseg.FromPoint.Z.ToString() == "NaN" || pseg.FromPoint.Z.ToString() == "非数字")
                            {
                                pPointF.Z = 0;
                            }
                            else
                            {
                                pPointF.Z = newarrayS[2];
                            }
                            pSegmentNEW.FromPoint = pPointF;
                            oldarrayT[0]          = pseg.ToPoint.X;
                            oldarrayT[1]          = pseg.ToPoint.Y;
                            if (pseg.ToPoint.Z.ToString() == "NaN" || pseg.ToPoint.Z.ToString() == "非数字")
                            {
                                oldarrayT[2] = 0;
                            }
                            else
                            {
                                oldarrayT[2] = pseg.ToPoint.Z;
                            }
                            matrix.coord_Trans(oldarrayT, rotateMat, tranVec, newarrayT);
                            pPointT.X = newarrayT[0];
                            pPointT.Y = newarrayT[1];
                            if (pseg.ToPoint.Z.ToString() == "NaN" || pseg.ToPoint.Z.ToString() == "非数字")
                            {
                                pPointT.Z = 0;
                            }
                            else
                            {
                                pPointT.Z = newarrayT[2];
                            }
                            pSegmentNEW.ToPoint = pPointT;

                            pSegCNew.AddSegment(pSegmentNEW);
                        }
                        IRing pRing = pSegCNew as IRing;
                        pRing.Close();
                        object ob = Type.Missing;
                        pGCollectionNew.AddGeometry(pRing as IGeometry, ref ob, ref ob);
                    }
                    IFeature pFeatureTemp = pFtClassNew.CreateFeature();
                    pFeatureTemp.Shape = pGCollectionNew as IGeometry;
                    ClsGDBDataCommon.CopyFeatureFieldValue(pF, pFeatureTemp);
                    pFeatureTemp.Store();
                }


                pF = pFC.NextFeature();
            }

            IFeatureLayer ppFeatureLayer = new FeatureLayerClass();

            ppFeatureLayer.FeatureClass = pFtClassNew;
            ppFeatureLayer.Name         = strName;
            pMapControl.AddLayer(ppFeatureLayer as ILayer);
            //pMapControl.ActiveView.PartialRefresh(esriViewDrawPhase.esriViewGeography, null, null);
            this.Close();
        }
예제 #2
0
//         private IProjectedCoordinateSystem CreateProjectedCoordinateSystem()
//         {
//
//             // Set up the SpatialReferenceEnvironment.
//             // SpatialReferenceEnvironment is a singleton object and needs to use the Activator class.
//             Type factoryType = Type.GetTypeFromProgID(
//                 "esriGeometry.SpatialReferenceEnvironment");
//             System.Object obj = Activator.CreateInstance(factoryType);
//             ISpatialReferenceFactory3 spatialReferenceFactory = obj as
//                 ISpatialReferenceFactory3;
//
//             // Create a projection, GeographicCoordinateSystem, and unit using the factory.
//             IProjectionGEN projection = spatialReferenceFactory.CreateProjection((int)
//                 esriSRProjectionType.esriSRProjection_Sinusoidal) as IProjectionGEN;
//             IGeographicCoordinateSystem geographicCoordinateSystem =
//                 spatialReferenceFactory.CreateGeographicCoordinateSystem((int)
//                 esriSRGeoCS3Type.esriSRGeoCS_TheMoon);
//             ILinearUnit unit = spatialReferenceFactory.CreateUnit((int)
//                 esriSRUnitType.esriSRUnit_Meter) as ILinearUnit;
//
//             // Get the default parameters from the projection.
//             IParameter[] parameters = projection.GetDefaultParameters();
//
//             // Create a PCS using the Define method.
//             IProjectedCoordinateSystemEdit projectedCoordinateSystemEdit = new
//                 ProjectedCoordinateSystemClass();
//             object name = "Coustom";
//             object alias = "Coustom";
//             object abbreviation = "Coustom";
//             object remarks = "Coustom";
//             object usage = "Coustom";
//             object geographicCoordinateSystemObject = geographicCoordinateSystem as object;
//             object unitObject = unit as object;
//             object projectionObject = projection as object;
//             object parametersObject = parameters as object;
//
//
//             projectedCoordinateSystemEdit.Define(ref name, ref alias, ref abbreviation, ref
//         remarks, ref usage, ref geographicCoordinateSystemObject, ref unitObject,
//                 ref projectionObject, ref parametersObject);
//             ;
//             IProjectedCoordinateSystem userDefinedProjectCoordinateSystem =
//             projectedCoordinateSystemEdit as IProjectedCoordinateSystem;
//             return userDefinedProjectCoordinateSystem;
//         }

        private void btnOK_Click(object sender, EventArgs e)
        {
            //string szInputFilename = ;
            string       szDemFilename = cmbDemLayer.Text;
            IRasterLayer pDemLayer     = ClsGDBDataCommon.GetLayerFromName(m_pMapControl.Map, szDemFilename) as IRasterLayer;

            if (pDemLayer == null)
            {
                return;
            }

            string szXmlFilename = txtXmlFilename.Text;

            //得到FeatureLayer
            IFeatureLayer pInputFeatureLayer = ClsGDBDataCommon.GetLayerFromName(m_pMapControl.Map, cmbPathFeatureLayer.Text) as IFeatureLayer;

            if (pInputFeatureLayer == null)
            {
                return;
            }

            try
            {
                //获得inputFeatureClass
                IFeatureClass pInputFeatureClass = pInputFeatureLayer.FeatureClass;

                #region 注释代码
                //IFeatureDataset pInputDataset = pInputFeatureClass.FeatureDataset;
                //IWorkspace pInputWorkspace = pInputDataset.Workspace;
                //IWorkspace2 pInputWorkspace2 = pInputWorkspace as IWorkspace2;

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


                #region 创建点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);

                //添加字段
                pField            = new FieldClass();
                pFieldEdit        = (IFieldEdit)pField;
                pFieldEdit.Name_2 = "Name";             //名字
                pFieldEdit.Type_2 = esriFieldType.esriFieldTypeString;
                pFieldsEdit.AddField(pField);

                pField            = new FieldClass();
                pFieldEdit        = (IFieldEdit)pField;
                pFieldEdit.Name_2 = "Type";             //类型
                pFieldEdit.Type_2 = esriFieldType.esriFieldTypeString;
                pFieldsEdit.AddField(pField);
                #endregion

                #region 创建polyline字段
                IFields pPolylineFields = new FieldsClass(); //pInputFeatureClass.Fields;
                pFieldsEdit = (IFieldsEdit)pPolylineFields;

                //创建类型为几何类型的字段
                //pField = new FieldClass();
                //pFieldEdit = (IFieldEdit)pField;

                //pFieldEdit.Name_2 = "shape";
                //pFieldEdit.Type_2 = esriFieldType.esriFieldTypeGeometry;
                //pGeoDef = new GeometryDefClass();
                //pGeoDefEdit = (IGeometryDefEdit)pGeoDef;
                //pGeoDefEdit.GeometryType_2 = esriGeometryType.esriGeometryPolyline;
                //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);

                for (int i = 0; i < pInputFeatureClass.Fields.FieldCount; i++)
                {
                    //pField = new FieldClass();
                    //pFieldEdit = (IFieldEdit)pField;

                    IField pTmpField = pInputFeatureClass.Fields.get_Field(i);
                    IClone pSrcClone = pTmpField as IClone;
                    IClone pDstClone = pSrcClone.Clone();
                    IField pDstField = pDstClone as IField;
                    //IFieldEdit pTmpFieldEdit = pTmpField as IFieldEdit;

                    //pFieldEdit.Name_2 = pTmpFieldEdit.Name;
                    //pFieldEdit.Type_2 = pTmpFieldEdit.Type;
                    pFieldsEdit.AddField(pDstField);
                }

                ////添加字段
                //pField = new FieldClass();
                //pFieldEdit = (IFieldEdit)pField;
                //pFieldEdit.Name_2 = "Name";             //名字
                //pFieldEdit.Type_2 = esriFieldType.esriFieldTypeString;
                //pFieldsEdit.AddField(pField);

                //pField = new FieldClass();
                //pFieldEdit = (IFieldEdit)pField;
                //pFieldEdit.Name_2 = "Type";             //类型
                //pFieldEdit.Type_2 = esriFieldType.esriFieldTypeString;
                //pFieldsEdit.AddField(pField);
                #endregion

                //创建outputPolylineFeatureClass, outputPointFeatureClass
                IFeatureClass    pOutputPolylineFeatureClass = null, pOutputPointFeatureClass = null;
                ClsGDBDataCommon pDataCommon  = new ClsGDBDataCommon();


                string szPolylineFilename     = txtOutputPolyline.Text; // "D:\\test\\testPolyline.shp";
                string szPointFilename        = txtOutputPoint.Text;    // "D:\\test\\testPoint.shp";
                //IWorkspaceFactory pWorkspaceFactory = new ShapefileWorkspaceFactoryClass();
                //IWorkspace pWorkspace = pDataCommon.OpenFromShapefile(System.IO.Path.GetDirectoryName(szPolylineFilename));
                //IFeatureWorkspace pFW = pWorkspace as IFeatureWorkspace;

                string szPath = System.IO.Path.GetDirectoryName(szPointFilename);
                pOutputPointFeatureClass    = pDataCommon.CreateShapefile(szPath, System.IO.Path.GetFileName(szPointFilename), pPointFields, null);
                pOutputPolylineFeatureClass = pDataCommon.CreateShapefile(szPath, System.IO.Path.GetFileName(szPolylineFilename), pPolylineFields, null);

                //判断类型是否一致
                if (pOutputPointFeatureClass.ShapeType != esriGeometryType.esriGeometryPoint)
                {
                    MessageBox.Show("输出点文件已存在,但类型不一致,请更改或删除!");
                    return;
                }

                if (pOutputPolylineFeatureClass.ShapeType != esriGeometryType.esriGeometryPolyline)
                {
                    MessageBox.Show("输出路径文件已存在,但类型不一致,请更改或删除!");
                    return;
                }
                //pOutputPolylineFeatureClass = pDataCommon.CreateFeatureClass(pInputWorkspace as IWorkspace2, pInputDataset, "Polyline", pPolylineFields, null, null, "");
                //pOutputPointFeatureClass = pDataCommon.CreateFeatureClass(pInputWorkspace as IWorkspace2, pInputDataset, "Point", pPointFields, null, null, "");
                if (pOutputPointFeatureClass == null || pOutputPolylineFeatureClass == null)
                {
                    return;
                }

                //先清空所有对象
                IFeatureCursor pFeatureCursor = pOutputPolylineFeatureClass.Search(null, false);
                IFeature       pFeature = pFeatureCursor.NextFeature();
                while (pFeature != null)
                {
                    pFeature.Delete();
                    pFeature = pFeatureCursor.NextFeature();
                }
                pFeatureCursor = pOutputPointFeatureClass.Search(null, false);
                pFeature       = pFeatureCursor.NextFeature();
                while (pFeature != null)
                {
                    pFeature.Delete();
                    pFeature = pFeatureCursor.NextFeature();
                }

                //反投影
                ClsProjectBackward pProjectBackward = new ClsProjectBackward();
                if (pProjectBackward.projectBackward(pInputFeatureClass, pDemLayer, szXmlFilename,
                                                     ref pOutputPolylineFeatureClass, ref pOutputPointFeatureClass))
                {
                    MessageBox.Show("反投影操作已经完成!");
                }
                else
                {
                    MessageBox.Show("反投影操作失败!");
                }

                //写入GDB
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.ToString());
                return;
            }
        }
예제 #3
0
        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);
        }
예제 #4
0
        public void ExportDatatoShape()
        {
            ClsGDBDataCommon comm = new ClsGDBDataCommon();

            if (!txtOutData.Text.EndsWith("shp"))
            {
                //timerShow.Start();
                MessageBox.Show("输出文件名不是shp文件!");
                return;
            }
            //
            String strFullName = txtOutData.Text;
            string strPath     = System.IO.Path.GetDirectoryName(strFullName);
            string strName     = System.IO.Path.GetFileName(strFullName);

            //导出数据
            IFeatureLayer pFtlayer = (IFeatureLayer)m_pLayer;
            IFeatureClass pFtClass = pFtlayer.FeatureClass;
            //IFields pFields = pFtClass.Fields;
            //设置空间参考
            ISpatialReference pSpatialRef;

            if (rdoLayer.Checked)
            {
                IGeoDataset pGeo = (IGeoDataset)pFtlayer;
                pSpatialRef = pGeo.SpatialReference;
            }
            else
            {
                pSpatialRef = m_pMap.SpatialReference;
            }

            //创建fields
            IFields pFields = ClsGDBDataCommon.CopyFeatureField(pFtClass, pSpatialRef);

            if (pFields == null)
            {
                MessageBox.Show("拷贝图形字段失败!");
                return;
            }

            #region

            //IFieldsEdit pFieldsEdit = (IFieldsEdit)pFields;

            ////设置字段
            //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 = pFtClass.ShapeType;
            //pGeoDefEdit.SpatialReference_2 = pSpatialRef;
            //pGeoDefEdit.HasM_2 = false;
            //pGeoDefEdit.HasZ_2 = false;
            //pFieldEdit.GeometryDef_2 = pGeoDef;
            //pFieldEdit.IsNullable_2 = true;
            //pFieldEdit.Required_2 = true;
            //pFieldsEdit.AddField(pField);
            //复制pFtClass的字段给fields


            //IFieldsEdit pFieldsEdit = (IFieldsEdit)pFields;
            //for (int i = 0; i < pFtClass.Fields.FieldCount; i++)
            //{
            //    if (pFtClass.Fields.get_Field(i).Type != esriFieldType.esriFieldTypeGeometry &&
            //       pFtClass.Fields.get_Field(i).Type != esriFieldType.esriFieldTypeOID)
            //    {
            //        IField pfieldcopy = new FieldClass();
            //        pfieldcopy = pFtClass.Fields.get_Field(i);
            //        pFieldsEdit.AddField(pfieldcopy);
            //    }
            //}
            #endregion
            //创建新的FeatureClass
            IFeatureClass pFtClassNew = comm.CreateShapefile(strPath, strName, pFields, pSpatialRef);
            if (pFtClassNew == null)
            {
                MessageBox.Show("创建shp文件失败!");
                return;
            }
            bool bSel = false;
            if (cmbData.SelectedIndex == 0)
            {
                bSel = true;
            }
            if (ClsGDBDataCommon.CopyFeatures(pFtlayer, pFtClassNew, bSel) == false)
            {
                MessageBox.Show("拷贝失败!");
            }
            #region 拷贝Feature

            ////
            ////////////////////////////////////////////////////////////////////////////
            //IDataset dataset = (IDataset)pFtClassNew;
            //IWorkspace workspace = dataset.Workspace;

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

            //IFeatureBuffer featureBuffer;
            //IFeatureCursor featureCursor = null;
            //object featureOID;

            ////////////////////////////////////////////////////////////////////////////

            //IFeature pFeature;
            //long nCount;
            //long nProcess = 0;
            //if (cmbData.SelectedIndex == 0)//选中要素
            //{
            //    IFeatureSelection pFtSel = (IFeatureSelection)pFtlayer;
            //    ISelectionSet pSelSet = pFtSel.SelectionSet;
            //    nCount = pSelSet.Count;
            //    if (nCount < 1)
            //    {
            //        //timerShow.Start();
            //        MessageBox.Show("没有选中对象!");
            //        return;
            //    }
            //    ICursor pCursor;
            //    pSelSet.Search(null, false, out pCursor);
            //    IFeatureCursor pFtCur = (IFeatureCursor)pCursor;
            //    pFeature = pFtCur.NextFeature();

            //    while (pFeature != null)
            //    {
            //        //////////////////////////////////////////////////////////////////////////
            //        featureBuffer = pFtClassNew.CreateFeatureBuffer();
            //        featureCursor = pFtClassNew.Insert(true);
            //        featureBuffer.Shape = pFeature.ShapeCopy;
            //        ClsGDBDataCommon.CopyFeatureFieldValue(pFeature, (IFeature)featureBuffer);
            //        featureOID = featureCursor.InsertFeature(featureBuffer);
            //        //featureCursor.Flush();

            //        nProcess++;
            //        this.Text = string.Format("共有:{0}条数据,已处理:{1}条", nCount, nProcess);
            //        pFeature = pFtCur.NextFeature();
            //    }

            //}
            //else//全部要素
            //{
            //    ITable pTable = (ITable)m_pLayer;
            //    nCount = pTable.RowCount(null);

            //    IFeatureCursor pFtCursor = pFtlayer.Search(null, true);
            //    if (pFtCursor == null) return;
            //    pFeature = pFtCursor.NextFeature();
            //    while (pFeature != null)
            //    {
            //        //////////////////////////////////////////////////////////////////////////
            //        featureBuffer = pFtClassNew.CreateFeatureBuffer();
            //        featureCursor = pFtClassNew.Insert(true);

            //        featureBuffer.Shape = pFeature.ShapeCopy;
            //        ClsGDBDataCommon.CopyFeatureFieldValue(pFeature, (IFeature)featureBuffer);
            //        featureOID = featureCursor.InsertFeature(featureBuffer);

            //        nProcess++;
            //        this.Text = string.Format("共有:{0}条数据,已处理:{1}条", nCount, nProcess);
            //        pFeature = pFtCursor.NextFeature();
            //    }
            //}
            //featureCursor.Flush();
            //workspaceEdit.StopEditOperation();
            //workspaceEdit.StopEditing(true);
            #endregion
            //将导出数据添加到地图
            IFeatureLayer pOutFL = new FeatureLayerClass();
            pOutFL.FeatureClass = pFtClassNew;
            pOutFL.Name         = pFtClassNew.AliasName;
            m_pMap.AddLayer(pOutFL);

            this.Close();
        }
예제 #5
0
        public void ExportDatatoShape(string strFullName, ILayer m_pLayer)
        {
            ClsGDBDataCommon comm = new ClsGDBDataCommon();


            string strPath = System.IO.Path.GetDirectoryName(strFullName);
            string strName = System.IO.Path.GetFileName(strFullName);

            //导出数据
            IFeatureLayer pFtlayer = (IFeatureLayer)m_pLayer;
            IFeatureClass pFtClass = pFtlayer.FeatureClass;
            IFields       pFields  = pFtClass.Fields;
            //设置空间参考
            ISpatialReference pSpatialRef;

            IGeoDataset pGeo = (IGeoDataset)pFtlayer;

            pSpatialRef = pGeo.SpatialReference;

            IFeatureClass pFtClassNew = comm.CreateShapefile(strPath, strName, pFields, pSpatialRef);

            if (pFtClassNew == null)
            {
                MessageBox.Show("创建shp文件失败!");
                return;
            }
            //////////////////////////////////////////////////////////////////////////
            IDataset   dataset   = (IDataset)pFtClassNew;
            IWorkspace workspace = dataset.Workspace;

            //Cast for an IWorkspaceEdit
            IWorkspaceEdit workspaceEdit = (IWorkspaceEdit)workspace;

            workspaceEdit.StartEditing(true);
            workspaceEdit.StartEditOperation();

            IFeatureBuffer featureBuffer;
            IFeatureCursor featureCursor = null;
            object         featureOID;

            //////////////////////////////////////////////////////////////////////////

            IFeature pFeature;
            long     nCount;
            long     nProcess = 0;

            ITable pTable = (ITable)m_pLayer;

            nCount = pTable.RowCount(null);

            IFeatureCursor pFtCursor = pFtlayer.Search(null, true);

            if (pFtCursor == null)
            {
                return;
            }
            pFeature = pFtCursor.NextFeature();
            while (pFeature != null)
            {
                //////////////////////////////////////////////////////////////////////////
                featureBuffer = pFtClassNew.CreateFeatureBuffer();
                featureCursor = pFtClassNew.Insert(true);

                featureBuffer.Shape = pFeature.ShapeCopy;
                CopyFeatureField(pFeature, featureBuffer);
                featureOID = featureCursor.InsertFeature(featureBuffer);

                nProcess++;
                this.Text = string.Format("共有:{0}条数据,已处理:{1}条", nCount, nProcess);
                pFeature  = pFtCursor.NextFeature();
            }

            featureCursor.Flush();
            workspaceEdit.StopEditOperation();
            workspaceEdit.StopEditing(true);

            IFeatureLayer pOutFL = new FeatureLayerClass();

            pOutFL.FeatureClass = pFtClassNew;
            pOutFL.Name         = pFtClassNew.AliasName;
        }