C# NetTopologySuite+ProjNet 任意图形类型坐标转换

添加引用:NetTopologySuite、ProjNet、ProjNet.SRID

Program.cs文件:

using ProjNet.CoordinateSystems;
using ProjNet.CoordinateSystems.Transformations;
using ProjNet.SRID;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Xsl;
using NetTopologySuite.IO;
using NetTopologySuite.Geometries;

namespace Reproject2
{
    internal class Program
    {
        static void Main(string[] args)
        {
            CoordinateSystem projCs = SRIDReader.GetCSbyID(4527);
            CoordinateSystem geoCs = SRIDReader.GetCSbyID(4490);

            CoordinateTransformationFactory ctFactory = new CoordinateTransformationFactory();
            ICoordinateTransformation transformation = ctFactory.CreateFromCoordinateSystems(projCs, geoCs);

            Polygon original = new Polygon(
                new LinearRing(
                    new Coordinate[] {
                        new Coordinate(39498340.1151, 4807100.9600),
                        new Coordinate(39499340.1151, 4809100.9600),
                        new Coordinate(39497340.1151, 4806100.9600),
                        new Coordinate(39498340.1151, 4807100.9600)
            }));

            foreach (var coor in original.Coordinates)
            {
                Console.WriteLine("原始坐标: " + coor.X + " ; " + coor.Y);
            }

            Geometry after = Transform(original, transformation.MathTransform);

            foreach (var coor in after.Coordinates)
            {
                Console.WriteLine("转换后坐标: " + coor.X + " ; " + coor.Y);
            }
            Console.ReadKey();
        }

        public static Geometry Transform(Geometry geometry, MathTransform mathTransform)
        {
            geometry = geometry.Copy();
            geometry.Apply(new MathTransformFilter(mathTransform));
            return geometry;
        }
    }
}

新增文件:MathTransformFilter.cs,用于完成任意geometry的序列化转坐标。

using NetTopologySuite.Geometries;
using ProjNet.CoordinateSystems.Transformations;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Reproject2
{
    public class MathTransformFilter : ICoordinateSequenceFilter
    {
        private readonly MathTransform _mathTransform;

        public MathTransformFilter(MathTransform mathTransform)
            => _mathTransform = mathTransform;

        public bool Done => false;
        public bool GeometryChanged => true;

        public void Filter(CoordinateSequence seq, int i)
        {
            var (x, y, z) = _mathTransform.Transform(seq.GetX(i), seq.GetY(i), seq.GetZ(i));
            seq.SetX(i, x);
            seq.SetY(i, y);
            seq.SetZ(i, z);
        }
    }
}

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,以下是一个简单的C#代码示例,用于将经纬度坐标系转换为平面坐标系(UTM坐标系): ```csharp using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using ProjNet.CoordinateSystems; using ProjNet.CoordinateSystems.Transformations; using ProjNet.Converters.WellKnownText; namespace CoordinateConverter { class Program { static void Main(string[] args) { // 输入待转换的经纬度坐标 Console.WriteLine("请输入待转换的经纬度坐标(格式:经度,纬度):"); string input = Console.ReadLine(); string[] coordinates = input.Split(','); // 定义源坐标系 ICoordinateSystem sourceCS = GeographicCoordinateSystem.WGS84; // 定义目标坐标系(这里使用UTM Zone 51N) ICoordinateSystem targetCS = ProjectedCoordinateSystem.WGS84_UTM(51, true); // 创建转换器 CoordinateTransformationFactory ctfac = new CoordinateTransformationFactory(); ICoordinateTransformation transformation = ctfac.CreateFromCoordinateSystems(sourceCS, targetCS); // 转换坐标 double[] sourcePoint = new double[] { double.Parse(coordinates[0]), double.Parse(coordinates[1]) }; double[] targetPoint = transformation.MathTransform.Transform(sourcePoint); // 输出转换后的坐标 Console.WriteLine($"转换后的UTM坐标为:{targetPoint[0]}, {targetPoint[1]}"); Console.ReadLine(); } } } ``` 这个示例使用ProjNet库中的坐标转换功能,需要先在项目中添加对ProjNet的引用。当然,具体实现可能因为实际需求而有所不同,以上代码仅供参考。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值