基于C#的ArcEngine二次开发41:投影坐标系与地理坐标系接口、方法及示例代码(三)

简介: 基于C#的ArcEngine二次开发41:投影坐标系与地理坐标系接口、方法及示例代码

2.3.4 ISpatialReferenceFactory.CreateParameter
public IParameter CreateParameter (int parameterType);


esriSRParameterType, esriSRParameter2Type, esriSRParameter3Type, or esriSRParameter4Type 枚举器指定的参数类型,来创建指定的参数

ISpatialReferenceFactory spatialReferenceFactory = new SpatialReferenceEnvironmentClass(); // Declare an array of IParameters, and for each, create the required Parameter object
// by using ISpatialReferenceFactory::CreateParamater. Then assign the parameter a value. Below is an example used for a Transverse Mercator projection
IParameter[] parameterArray = new IParameter[5];
parameterArray[0] = spatialReferenceFactory.CreateParameter((int)esriSRParameterType.esriSRParameter_FalseEasting);
parameterArray[0].Value = 500000;
parameterArray[1] = spatialReferenceFactory.CreateParameter((int)esriSRParameterType.esriSRParameter_FalseNorthing);
parameterArray[1].Value = 0;
parameterArray[2] = spatialReferenceFactory.CreateParameter((int)esriSRParameterType.esriSRParameter_CentralMeridian);
parameterArray[2].Value = -123;
parameterArray[3] = spatialReferenceFactory.CreateParameter((int)esriSRParameterType.esriSRParameter_LatitudeOfOrigin);
parameterArray[3].Value = 0;
parameterArray[4] = spatialReferenceFactory.CreateParameter((int)esriSRParameterType.esriSRParameter_ScaleFactor);
parameterArray[4].Value = 0.9996;

2.3.5 ISpatialReferenceFactory.CreatePredefinedPrimeMeridians

private void PrintPrimeMeridian()
    {
        // use activator class with SpatialReferenceEnvironment singleton
        Type factoryType = Type.GetTypeFromProgID("esriGeometry.SpatialReferenceEnvironment");
        System.Object obj = Activator.CreateInstance(factoryType);
        ISpatialReferenceFactory spatialReferenceFactory = obj as ISpatialReferenceFactory;
        ISet primeMeridiansSet = spatialReferenceFactory.CreatePredefinedPrimeMeridians();
        System.Windows.Forms.MessageBox.Show("Number of projections = " + primeMeridiansSet.Count);
        for (int i = 0; i < primeMeridiansSet.Count; i++)
        {
            IPrimeMeridian primeMeridian = primeMeridiansSet.Next() as IPrimeMeridian;
            System.Windows.Forms.MessageBox.Show(primeMeridian.Name);
        }
    }

2.3.6 ISpatialReferenceFactory.CreatePredefinedProjections

//This example shows how the CreatePredefinedProjections function returns a 
//set that contains all the available Projection objects. The set is iterated 
//through, and the name of each Projection with the set is obtained. These  
//type of functions are useful for developers who may wish to populate a 
//pulldown selection list of available SpatialReference objects. 
private void PrintProjections()
{
  ISpatialReferenceFactory spatialReferenceFactory = new SpatialReferenceEnvironmentClass();
  ISet projectionSet = spatialReferenceFactory.CreatePredefinedProjections();
  System.Windows.Forms.MessageBox.Show("Number of projections = " + projectionSet.Count);
  for (int i = 0; i < projectionSet.Count; i++)
  {
    IProjection projection = projectionSet.Next() as IProjection;
    System.Windows.Forms.MessageBox.Show(projection.Name);
  }
}

2.3.7 ISpatialReferenceFactory.CreatePrimeMeridian

public IPrimeMeridian CreatePrimeMeridian (int primeMeridianType);

Use an element from the esriSRPrimeMType or esriSRPrimeM2Type enumerations as the primeMeridianType to create a particular predefined prime meridian.

 ISpatialReferenceFactory spatialReferenceFactory = new SpatialReferenceEnvironmentClass();
//Create the prime meridian using the available prime meridians. These can be
//found in the esriGeometry esriSRPrimeMType enumeration.
IPrimeMeridian primeMeridian = spatialReferenceFactory.CreatePrimeMeridian((int)esriSRPrimeMType.esriSRPrimeM_Greenwich);

2.3.8 ISpatialReferenceFactory.CreateProjection

public IProjection CreateProjection (int projectionType);

Use an element from the esriSRProjectionType, esriSRProjection2Type, esriSRProjection3Type, or esriSRProjection4Type enumerations as the projectionType to create a particular predefined map projection.

            ISpatialReferenceFactory spatialReferenceFactory = new SpatialReferenceEnvironmentClass();
            //Create a projections using the available projections. These can be
            //found in the esriGeometry esriSRProjectionType, esriSRProjection2Type,
            //esriSRProjection3Type, and esriSRProjection4Type enumerations.
            IProjection projection = spatialReferenceFactory.CreateProjection((int)esriSRProjectionType.esriSRProjection_LambertConformalConic);

2.3.9 ISpatialReferenceFactory.CreateSpheroid

public ISpheroid CreateSpheroid (int spheroidType);


Use an element from the esriSRSpheroidType or esriSRSpheroid2Type enumerations as the spheroidType to create a particular predefined spheroid.


            ISpatialReferenceFactory spatialReferenceFactory = new SpatialReferenceEnvironmentClass();
            //Create the spheroid using the available spheroids. These can be
            //found in the esriGeometry esriSRSpheroidType enumeration.
            ISpheroid spheroid = spatialReferenceFactory.CreateSpheroid((int)esriSRSpheroidType.esriSRSpheroid_Clarke1866);

2.3.10 ISpatialReferenceFactory.CreateUnit

public IUnit CreateUnit (int unitType);

Use an element from the esriSRUnitType or esriSRUnit2Type enumerations as the unitType to create a particular predefined unit of measure.


Use an element from the esriSRUnitType or esriSRUnit2Type enumerations as the unitType to create a particular predefined unit of measure.
ISpatialReferenceFactory spatialReferenceFactory = new SpatialReferenceEnvironmentClass();
//Create the unit using the available units.  These can be
//found in the esriGeometry esriSRUnitType enumeration.
ILinearUnit linearUnit = spatialReferenceFactory.CreateUnit((int)esriSRUnitType.esriSRUnit_Foot) as ILinearUnit;

2.3.11 ISpatialReferenceFactory.ExportESRISpatialReferenceToPRJFile

public void ExportESRISpatialReferenceToPRJFile (
    stringprjFile,
    ISpatialReferenceSpatialReference);

ExportESRISpatialReferenceToPRJFile converts an ArcGIS style spatial reference into the well-known text string. Here is an example (reformatted):


GEOGCS["GCS_North_American_1983", DATUM["D_North_American_1983", SPHEROID["GRS_1980",6378137,298.257222101]], PRIMEM["Greenwich",0], UNIT["Degree",0.0174532925199433]]

public void ExportESRISpatialReferenceToPRJFileExample()
    {
        //The ISpatialReferenceFactory::ExportESRISpatialReferenceToPRJFile
        //method requires that you specify the path and filename of the output
        //PRJ file you wish to create with the export. A valid spatial reference
        //object containing spatial reference information is also required.
        // use activator class with SpatialReferenceEnvironment singleton
        Type factoryType = Type.GetTypeFromProgID("esriGeometry.SpatialReferenceEnvironment");
        System.Object obj = Activator.CreateInstance(factoryType);
        ISpatialReferenceFactory3 spatialReferenceFactory = obj as ISpatialReferenceFactory3;
        IProjectedCoordinateSystem projectedCoordinateSystem = spatialReferenceFactory.CreateProjectedCoordinateSystem((int)esriSRProjCSType.esriSRProjCS_WGS1984UTM_10N);
        //Export the pcs to a prj file
        String fileName = "c:\\temp\\utm10.prj";
        spatialReferenceFactory.ExportESRISpatialReferenceToPRJFile(fileName, projectedCoordinateSystem);
    }

2.4 ISpatialReferenceFactory.CreateProjectedCoordinateSystem

2.4.1 创建投影坐标系

示例代码1:

private void CreateProjectedCoordinateSystem()
        {
            ISpatialReferenceFactory spatialReferenceFactory = new SpatialReferenceEnvironmentClass();
            //Create a projected coordinate system using the available projected coordinate systems
            IProjectedCoordinateSystem projectedCoordinateSystem1 = spatialReferenceFactory.CreateProjectedCoordinateSystem((int)esriSRProjCSType.esriSRProjCS_World_Mercator);
            //Here is an more detailed example for creating a pre-defined projected coordinate system for a new Shapefile using the 'ISpatialReferenceFactory::CreateProjectedCoordinateSystem method
            //Create the pre-defined projected coordinate system object
            IProjectedCoordinateSystem projectedCoordinateSystem2 = spatialReferenceFactory.CreateProjectedCoordinateSystem((int)esriSRProjCSType.esriSRProjCS_NAD1983SPCS_TXSouthCentFT);
            ISpatialReference spatialReference = projectedCoordinateSystem2 as ISpatialReference;
            //Set the false origin and units for the spatial reference.
            //You can use either the Domain or the FalseOriginAndUnits methods.
            // spatialReference.SetFalseOriginAndUnits(0, 0, 0);
            spatialReference.SetDomain(-1000000, 10000000, -1000000, 10000000);
            // spatialReference.SetMDomain(0, 1);
            spatialReference.SetMFalseOriginAndUnits(1, 1);
            // spatialReference.SetZDomain(0, 1);
            spatialReference.SetZFalseOriginAndUnits(1, 1);
            // Create a new SDE workspace
            IWorkspaceFactory sdeWorkspaceFactory = new ESRI.ArcGIS.DataSourcesGDB.SdeWorkspaceFactoryClass();
            IWorkspace workspace = sdeWorkspaceFactory.OpenFromFile("C:\\Documents and Settings\\bast5010\\Application Data\\ESRI\\ArcCatalog\\Connection to Vampire.sde", 0);
            IFeatureWorkspace sdeFeatureWorkspace = workspace as IFeatureWorkspace;
            //Create the fields for the feature dataset
            IFields fields = new FieldsClass();
            IFieldsEdit fieldsEdit = fields as IFieldsEdit;
            IField field = new FieldClass();
            IFieldEdit fieldEdit = field as IFieldEdit;
            fieldEdit.Type_2 = esriFieldType.esriFieldTypeOID;
            fieldEdit.IsNullable_2 = false;
            fieldEdit.Name_2 = "OID";
            fieldsEdit.AddField(fieldEdit);
            IGeometryDefEdit geometryDefEdit = new GeometryDefClass();
            geometryDefEdit.GeometryType_2 = esriGeometryType.esriGeometryPolygon;
            geometryDefEdit.GridCount_2 = 1;
            geometryDefEdit.set_GridSize(0, 1000);
            geometryDefEdit.SpatialReference_2 = spatialReference;
            fieldEdit = new FieldClass();
            fieldEdit.Name_2 = "Shape";
            fieldEdit.IsNullable_2 = true;
            fieldEdit.Type_2 = esriFieldType.esriFieldTypeGeometry;
            fieldEdit.GeometryDef_2 = geometryDefEdit;
            fieldsEdit.AddField(fieldEdit);
            fieldEdit = new FieldClass();
            fieldEdit.Name_2 = "Test_Field";
            fieldEdit.IsNullable_2 = true;
            fieldEdit.Editable_2 = true;
            fieldEdit.Length_2 = 25;
            fieldEdit.Type_2 = esriFieldType.esriFieldTypeString;
            fieldsEdit.AddField(fieldEdit);
            //Create a UID for the CreateFeatureClass method
            UID uid = new UIDClass();
            uid.Generate();
            //Create ExtCLSID as for CreateFeatureClass method
            UID uidExt = new UIDClass();
            uidExt.Generate();
            //Create the feature class for the feature dataset
            IFeatureClass featureClass = sdeFeatureWorkspace.CreateFeatureClass("PreDef_StateNAD83", fields, null, null, esriFeatureType.esriFTSimple, "Shape", "");
            System.Windows.Forms.MessageBox.Show("Data Creation Complete", "Program Status");
        }

代码示例2:

private void CreateProjectedCoordinateSystem()
{
    // Set up the SpatialReferenceEnvironment.
    // SpatialReferenceEnvironment is a singleton object and needs to use the Activator class.
    Type t = Type.GetTypeFromProgID("esriGeometry.SpatialReferenceEnvironment");
    System.Object obj = Activator.CreateInstance(t);
    ISpatialReferenceFactory srFact = obj as ISpatialReferenceFactory;
    // Use the enumeration to create an instance of the predefined object.
    IProjectedCoordinateSystem projectedCS = srFact.CreateProjectedCoordinateSystem(
        (int)esriSRProjCSType.esriSRProjCS_NAD1983UTM_11N);
}

2.4.2  创建一个自定义的投影坐标系

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)
        esriSRGeoCSType.esriSRGeoCS_WGS1984);
    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 = "Newfoundland";
    object alias = "NF_LAB";
    object abbreviation = "NF";
    object remarks = "Most Eastern Province in Canada";
    object usage = "When making maps of Newfoundland";
    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);
    return projectedCoordinateSystemEdit as IProjectedCoordinateSystem;
}

3 其它接口

3.1 IPRJSpatialReference

image.png

A sample represetation (reformatted for display) is below.

Projection    UTM   
Zone          10  
Datum         NAD83     
Zunits        NO     
Units         METERS     
Spheroid      GRS1980     
Xshift        0.0000000000     
Yshift        0.0000000000  
Parameters  


3.2 ISpatialReferenceInfo


image.png



相关文章
|
1月前
|
开发框架 监控 .NET
C#进阶-ASP.NET WebForms调用ASMX的WebService接口
通过本文的介绍,希望您能深入理解并掌握ASP.NET WebForms中调用ASMX WebService接口的方法和技巧,并在实际项目中灵活运用这些技术,提高开发效率和应用性能。
44 5
|
3月前
|
编译器 C#
C#多态概述:通过继承实现的不同对象调用相同的方法,表现出不同的行为
C#多态概述:通过继承实现的不同对象调用相同的方法,表现出不同的行为
138 65
|
2月前
|
JSON 程序员 C#
使用 C# 比较两个对象是否相等的7个方法总结
比较对象是编程中的一项基本技能,在实际业务中经常碰到,比如在ERP系统中,企业的信息非常重要,每一次更新,都需要比较记录更新前后企业的信息,直接比较通常只能告诉我们它们是否指向同一个内存地址,那我们应该怎么办呢?分享 7 个方法给你!
|
2月前
|
C# UED SEO
C# 异步方法async / await任务超时处理
通过使用 `Task.WhenAny`和 `Task.Delay`方法,您可以在C#中有效地实现异步任务的超时处理机制。这种方法允许您在指定时间内等待任务完成,并在任务超时时采取适当的措施,如抛出异常或执行备用操作。希望本文提供的详细解释和代码示例能帮助您在实际项目中更好地处理异步任务超时问题,提升应用程序的可靠性和用户体验。
100 3
|
3月前
|
存储 C#
【C#】大批量判断文件是否存在的两种方法效率对比
【C#】大批量判断文件是否存在的两种方法效率对比
62 1
|
3月前
|
C#
C#的方法的参数传递
C#的方法的参数传递
37 0
|
3月前
|
数据可视化 程序员 C#
C#中windows应用窗体程序的输入输出方法实例
C#中windows应用窗体程序的输入输出方法实例
66 0
|
2月前
|
C# 开发者
C# 一分钟浅谈:Code Contracts 与契约编程
【10月更文挑战第26天】本文介绍了 C# 中的 Code Contracts,这是一个强大的工具,用于通过契约编程增强代码的健壮性和可维护性。文章从基本概念入手,详细讲解了前置条件、后置条件和对象不变量的使用方法,并通过具体代码示例进行了说明。同时,文章还探讨了常见的问题和易错点,如忘记启用静态检查、过度依赖契约和性能影响,并提供了相应的解决建议。希望读者能通过本文更好地理解和应用 Code Contracts。
45 3
|
27天前
|
存储 安全 编译器
学懂C#编程:属性(Property)的概念定义及使用详解
通过深入理解和使用C#的属性,可以编写更清晰、简洁和高效的代码,为开发高质量的应用程序奠定基础。
89 12
|
2月前
|
设计模式 C# 图形学
Unity 游戏引擎 C# 编程:一分钟浅谈
本文介绍了在 Unity 游戏开发中使用 C# 的基础知识和常见问题。从 `MonoBehavior` 类的基础用法,到变量和属性的管理,再到空引用异常、资源管理和性能优化等常见问题的解决方法。文章还探讨了单例模式、事件系统和数据持久化等高级话题,旨在帮助开发者避免常见错误,提升游戏开发效率。
83 4