基于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;
相关文章
|
6天前
|
JSON 程序员 C#
使用 C# 比较两个对象是否相等的7个方法总结
比较对象是编程中的一项基本技能,在实际业务中经常碰到,比如在ERP系统中,企业的信息非常重要,每一次更新,都需要比较记录更新前后企业的信息,直接比较通常只能告诉我们它们是否指向同一个内存地址,那我们应该怎么办呢?分享 7 个方法给你!
|
9天前
|
C# UED SEO
C# 异步方法async / await任务超时处理
通过使用 `Task.WhenAny`和 `Task.Delay`方法,您可以在C#中有效地实现异步任务的超时处理机制。这种方法允许您在指定时间内等待任务完成,并在任务超时时采取适当的措施,如抛出异常或执行备用操作。希望本文提供的详细解释和代码示例能帮助您在实际项目中更好地处理异步任务超时问题,提升应用程序的可靠性和用户体验。
27 3
|
1月前
|
存储 C#
【C#】大批量判断文件是否存在的两种方法效率对比
【C#】大批量判断文件是否存在的两种方法效率对比
39 1
|
1月前
|
C#
C#的方法的参数传递
C#的方法的参数传递
15 0
|
1月前
|
数据可视化 程序员 C#
C#中windows应用窗体程序的输入输出方法实例
C#中windows应用窗体程序的输入输出方法实例
46 0
|
6月前
|
开发框架 前端开发 .NET
C#编程与Web开发
【4月更文挑战第21天】本文探讨了C#在Web开发中的应用,包括使用ASP.NET框架、MVC模式、Web API和Entity Framework。C#作为.NET框架的主要语言,结合这些工具,能创建动态、高效的Web应用。实际案例涉及企业级应用、电子商务和社交媒体平台。尽管面临竞争和挑战,但C#在Web开发领域的前景将持续拓展。
192 3
|
6月前
|
SQL 开发框架 安全
C#编程与多线程处理
【4月更文挑战第21天】探索C#多线程处理,提升程序性能与响应性。了解C#中的Thread、Task类及Async/Await关键字,掌握线程同步与安全,实践并发计算、网络服务及UI优化。跟随未来发展趋势,利用C#打造高效应用。
198 3
|
13天前
|
C# 开发者
C# 一分钟浅谈:Code Contracts 与契约编程
【10月更文挑战第26天】本文介绍了 C# 中的 Code Contracts,这是一个强大的工具,用于通过契约编程增强代码的健壮性和可维护性。文章从基本概念入手,详细讲解了前置条件、后置条件和对象不变量的使用方法,并通过具体代码示例进行了说明。同时,文章还探讨了常见的问题和易错点,如忘记启用静态检查、过度依赖契约和性能影响,并提供了相应的解决建议。希望读者能通过本文更好地理解和应用 Code Contracts。
29 3
|
1月前
|
安全 C# 数据安全/隐私保护
实现C#编程文件夹加锁保护
【10月更文挑战第16天】本文介绍了两种用 C# 实现文件夹保护的方法:一是通过设置文件系统权限,阻止普通用户访问;二是使用加密技术,对文件夹中的文件进行加密,防止未授权访问。提供了示例代码和使用方法,适用于不同安全需求的场景。
101 0
|
2月前
|
API C#
C# 一分钟浅谈:文件系统编程
在软件开发中,文件系统操作至关重要。本文将带你快速掌握C#中文件系统编程的基础知识,涵盖基本概念、常见问题及解决方法。文章详细介绍了`System.IO`命名空间下的关键类库,并通过示例代码展示了路径处理、异常处理、并发访问等技巧,还提供了异步API和流压缩等高级技巧,帮助你写出更健壮的代码。
41 2