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

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

2. ISpatialReferenceFactory

此接口已被ISpatialReferenceFactory3所代替,

2.1 接口方法概览

image.png

image.png


2.2 ISpatialReferenceFactory.CreateGeographicCoordinateSystem

public IGeographicCoordinateSystem CreateGeographicCoordinateSystem (int gcsType);

其参数由esriSRGeoCSType, esriSRGeoCS2Type, or esriSRGeoCS3Type枚举器提供

使用代码示例

 ISpatialReferenceFactory spatialReferenceFactory = new SpatialReferenceEnvironmentClass();
//Create a geographic coordinate system using the available geographic
//coordinate systems. These can be found in the esriGeometry esriSRGeoCSType
//enumeration.
IGeographicCoordinateSystem geographicCoordinateSystem = spatialReferenceFactory.CreateGeographicCoordinateSystem((int)esriSRGeoCSType.esriSRGeoCS_WGS1984);

2.2.1 Creating a predefined geographic coordinate system

ISpatialReferenceFactory 可以创建一个被 esriSRGeoCSType, esriSRGeoCS2Type, and esriSRGeoCS3Type枚举列表中包含的地理坐标系统

private void CreateGeographicCoordinateSystem()
{
    // 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.
    IGeographicCoordinateSystem geographicCS =
        srFact.CreateGeographicCoordinateSystem((int)
        esriSRGeoCSType.esriSRGeoCS_NAD1983);
}

2.2.2 Creating a custom geographic coordinate system


一个基本的地理坐标系统包括名称、角度单位、基准基本子午线,他是一个描述三维地球模型的坐标系统;我们可以通过IGeographicCoordinateSystem 接口获取主要的属性和方法,以及IGeographicCoordinateSystem2定义的额外属性;虽然我们不需要创建地理坐标系,但是IGeographicCoordinateSystemEdit 接口包含Define 和DefineEx 方法。


A GCS includes a name, angular unit of measure, datum (which includes a spheroid), and a prime meridian. It is a model of the earth in a three-dimensional (3D) coordinate system. Latitude-longitude or lat/lon data is in a GCS. You can access the majority of the properties and methods through the IGeographicCoordinateSystem interface with additional properties that are available in IGeographicCoordinateSystem2. Although most developers will not need to create a custom GCS, IGeographicCoordinateSystemEdit contains the Define and DefineEx methods.


下面是如何使用Define 方法创建用户定义GCS的示例,ISpatialReferenceFactory 允许创建基准、本初子午线、角度三维等要素


The following code example shows how to use the Define method to create a user-defined GCS. The ISpatialReferenceFactory interface allows you to create the Datum, PrimeMeridian, and AngularUnit component parts. These components can also be created using a similar Define method available on their classes.

private IGeographicCoordinateSystem CreateGeographicCoordinateSystem()
{
    // 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 the datum, prime meridian, and angular unit from existing definitions.
    IDatum datum = spatialReferenceFactory.CreateDatum((int)
        esriSRDatumType.esriSRDatum_OSGB1936);
    IPrimeMeridian primeMeridian = spatialReferenceFactory.CreatePrimeMeridian((int)
        esriSRPrimeMType.esriSRPrimeM_Greenwich);
    IUnit unit = spatialReferenceFactory.CreateUnit((int)
        esriSRUnitType.esriSRUnit_Degree);
    IGeographicCoordinateSystemEdit geographicCoordinateSystemEdit = new
        GeographicCoordinateSystemClass();
    object name = "UserDefined Geographic Coordinate System";
    object alias = "UserDefined GCS";
    object abbreviation = "UserDefined";
    object remarks = "User Defined Geographic Coordinate System based on OSGB1936";
    object usage = "Suitable for the UK";
    object datumObject = datum as object;
    object primeMeridianObject = primeMeridian as object;
    object unitObject = unit as object;
    geographicCoordinateSystemEdit.Define(ref name, ref alias, ref abbreviation, ref
        remarks, ref usage, ref datumObject, ref primeMeridianObject, ref unitObject)
        ;
    IGeographicCoordinateSystem userDefinedGeographicCoordinateSystem =
        geographicCoordinateSystemEdit as IGeographicCoordinateSystem;
    return userDefinedGeographicCoordinateSystem;
}

下面是使用DefineEx 创建定义投影的例子,它使用 SpatialReferenceEnvironment  接口创建基准、本初子午线和单位


The following code example shows how the DefineEx method can be used. It uses SpatialReferenceEnvironment to create the Datum, PrimeMeridian, and Unit components:

private IGeographicCoordinateSystem CreateGeographicCoordinateSystemEx()
{
    // 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);
    ISpatialReferenceFactory3 spatialReferenceFactory = obj as
        ISpatialReferenceFactory3;
    // Create the datum, prime meridian, and angular unit from existing definitions.
    IDatum datum = spatialReferenceFactory.CreateDatum((int)
        esriSRDatumType.esriSRDatum_OSGB1936);
    IPrimeMeridian primeMeridian = spatialReferenceFactory.CreatePrimeMeridian((int)
        esriSRPrimeMType.esriSRPrimeM_Greenwich);
    IUnit unit = spatialReferenceFactory.CreateUnit((int)
        esriSRUnitType.esriSRUnit_Degree);
    IGeographicCoordinateSystemEdit geographicCoordinateSystemEdit = new
        GeographicCoordinateSystemClass();
    String name = "UserDefined Geographic Coordinate System";
    String alias = "UserDefined GCS";
    String abbreviation = "UserDefined";
    String remarks = "User Defined Geographic Coordinate System based on OSGB1936";
    String usage = "Suitable for the UK";
    geographicCoordinateSystemEdit.DefineEx(name, alias, abbreviation, remarks,
        usage, datum, primeMeridian, unit as IAngularUnit);
    IGeographicCoordinateSystem userDefinedGeographicCoordinateSystem =
        geographicCoordinateSystemEdit as IGeographicCoordinateSystem;
    return userDefinedGeographicCoordinateSystem;
}

2.2.3 esriSRGeoCS3Type

几个常见的椭球编码:WGS-84(4326), Xian-80(4610), GCS-2000(4490)

更为详细的编码,参见文章:https://www.cnblogs.com/liweis/p/5951032.html

2.3 其他方法

2.3.1 ISpatialReferenceFactory.CreateDatum

public IDatum CreateDatum (int datumType);

参数由 esriSRDatumType, esriSRDatum2Type, or esriSRDatum3Type 枚举器提供

ISpatialReferenceFactory spatialReferenceFactory = new SpatialReferenceEnvironmentClass(); 
//Create the datum using the available datums. These can be 
//found in the esriGeometry esriSRDatumType enumeration. 
IDatum datum = spatialReferenceFactory.CreateDatum((int)esriSRDatumType.esriSRDatum_NAD1983);

2.3.2 ISpatialReferenceFactory.CreateESRISpatialReferenceFromPRJFile

public ISpatialReference CreateESRISpatialReferenceFromPRJFile (stringprjFile);
    public void CreateESRISpatialReferenceFromPRJFileExample()
    {
        //The ISpatialReferenceFactory::CreateESRISpatialReferenceFromPRJFile
        //method requires that you specify the path and filename of the PRJ
        //file you wish to import to create a spatial reference from.
        // use activator class with SpatialReferenceEnvironment singleton
        Type factoryType = Type.GetTypeFromProgID("esriGeometry.SpatialReferenceEnvironment");
        System.Object obj = Activator.CreateInstance(factoryType);
        ISpatialReferenceFactory3 spatialReferenceFactory = obj as ISpatialReferenceFactory3;
        ISpatialReference spatialReference = spatialReferenceFactory.
        CreateESRISpatialReferenceFromPRJFile("C:\\Program Files\\ArcGIS\\Coordinate Systems\\Geographic Coordinate Systems\\World\\WGS 1984.prj");
    }

2.3.3 ISpatialReferenceFactory.CreateGeoTransformation

public ITransformation CreateGeoTransformation (
    intgTransformationType);

参数由esriSRGeoTransformationType, esriSRGeoTransformation2Type, or esriSRGeoTransformation3Type枚举器指定的变换类型,以创建特定的地理变换

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