Engine中如何对要素类进行投影变换?

Engine中如何对要素类进行投影变换?例如Desktop中的Project工具。以要素类从WGS84地理坐标系转为西安80投影坐标系为例
已邀请:

朱新颖

赞同来自:

【解决办法】:
主要分三步:
1,由于WGS84地理坐标系和西安80投影坐标系的椭球体不同,需要先定义一个地理变换,参考AO帮助链接:
http://resources.arcgis.com/en ... 0.htm
2,获取要素类,遍历每个要素,获取IGeometry,使用IGeometry5.ProjectEx()方法进行投影,将投影后的Geometry重新赋值给Feature,并且报存。
http://resources.arcgis.com/en ... 00000
3,将处理过的要素类的空间参考改为新的空间参考,使用IGeoDatasetSchemaEdit.AlterSpatialReference方法。
http://resources.arcgis.com/en ... 00000

注:如果两个坐标系的椭球体相同的话,直接使用IGeometry5.Project()进行投影即可,也不需要定义地理变换

参考代码(以WGS84到西安80为例):



IGeographicCoordinateSystem wgs84GCS = createGeographicCoordinateSystem((int)esriSRGeoCSType.esriSRGeoCS_WGS1984);
            IProjectedCoordinateSystem xian80PCS = createProjectedCoordinateSystem((int)esriSRProjCS4Type.esriSRProjCS_Xian1980_3_Degree_GK_CM_102E);
            IGeoTransformation geoTrans = createCustomGeoTrans((ISpatialReference)wgs84GCS, (ISpatialReference)xian80PCS);
            IWorkspace workspace = getWorkspace(esriDataSourcesGDB.FileGDBWorkspaceFactory, @E:\ZhuXinying\testData\Data.gdb);
            IFeatureClass featureClass = openFeatureClass(workspace, WGS841);
            IFeatureCursor featureCur = featureClass.Search(null, false);
            IFeature feature = featureCur.NextFeature();
            while (feature != null)
            {
                IGeometry5 geometry = feature.ShapeCopy as IGeometry5;
                geometry.ProjectEx(xian80PCS as ISpatialReference, esriTransformDirection.esriTransformForward,geoTrans, false, 0, 0);
                feature.Shape = geometry;
                feature.Store();
                feature = featureCur.NextFeature();
            }
            // 定义投影
            defineSpatialReference(featureClass as IDataset, xian80PCS);

​ private void defineSpatialReference(IDataset dataset,ISpatialReference spatialReference)
        {
            IGeoDatasetSchemaEdit geoSchemaEdit = dataset as IGeoDatasetSchemaEdit;
            if (geoSchemaEdit.CanAlterSpatialReference)
            {
                geoSchemaEdit.AlterSpatialReference(spatialReference);
            }
       }
        private IProjectedCoordinateSystem createProjectedCoordinateSystem(int WKID)
        {
            Type factoryType = Type.GetTypeFromProgID(esriGeometry.SpatialReferenceEnvironment);
            System.Object obj = Activator.CreateInstance(factoryType);
            ISpatialReferenceFactory2 pSRF = obj as ISpatialReferenceFactory2;

            // Initialize and create the input and output coordinate systems.
            IProjectedCoordinateSystem pPCS = new ProjectedCoordinateSystemClass();
            pPCS = pSRF.CreateProjectedCoordinateSystem(WKID);
            return pPCS;
        }

        private IGeographicCoordinateSystem createGeographicCoordinateSystem(int WKID)
        {
            Type factoryType = Type.GetTypeFromProgID(esriGeometry.SpatialReferenceEnvironment);
            System.Object obj = Activator.CreateInstance(factoryType);
            ISpatialReferenceFactory2 pSRF = obj as ISpatialReferenceFactory2;
           
            IGeographicCoordinateSystem pGCS = new GeographicCoordinateSystemClass();
            // Initialize and create the input and output coordinate systems.
            pGCS = pSRF.CreateGeographicCoordinateSystem(WKID);
            return pGCS;
        }
        private IGeoTransformation createCustomGeoTrans(ISpatialReference pInputSR, ISpatialReference pOutputSR)
        {
            IGeographicCoordinateSystem pGCSfrom = null;
            IGeographicCoordinateSystem pGCSto = null;
            if (pInputSR is IGeographicCoordinateSystem)
                pGCSfrom = pInputSR as IGeographicCoordinateSystem;
            else if (pInputSR is IProjectedCoordinateSystem)
                pGCSfrom = (pInputSR as IProjectedCoordinateSystem).GeographicCoordinateSystem;
            if (pOutputSR is IGeographicCoordinateSystem)
                pGCSto = pOutputSR as IGeographicCoordinateSystem;
            else if (pOutputSR is IProjectedCoordinateSystem)
                pGCSto = (pOutputSR as IProjectedCoordinateSystem).GeographicCoordinateSystem;
            
            ICoordinateFrameTransformation pCFT = new CoordinateFrameTransformationClass();
            pCFT.PutParameters(p1, p2, p3, p4, p5, p6, p7); //七参数
            pCFT.PutSpatialReferences(pGCSfrom, pGCSto);
            pCFT.Name = Custom GeoTran;
            IGeoTransformation pGeoTrans = pCFT as IGeoTransformation;
            return pGeoTrans;
        }

要回复问题请先登录注册