AE解析WKT生成图形C#

 public IGeometry CreatePolygon(string wkt)
       {
           //多面
           if (wkt.Contains("MULTIPOLYGON"))
           {
              wkt = wkt.Replace(")),", "@");
              string[] wkts = wkt.Split('@');
              return CreateMultiPolygon(wkts);
           }
           //环
           if (wkt.Contains("),"))
           {
              wkt = wkt.Replace("),", "@");
              string[] wkts = wkt.Split('@');

              return CreateMultiPolygon(wkts);
           }
           string RegExp = @"(\d+\.\d+|\d+)\s(\d+\.\d+|\d+)";
           Regex regex = new Regex(RegExp, RegexOptions.Singleline | RegexOptions.IgnoreCase);
           Match m = regex.Match(wkt);

           //单面
          IPointCollection pPcl = new PolygonClass();
           while (m.Success)
           {
              string[] arr = m.Value.Trim().Split(new Char[] { ' ' });
              IPoint pPoint = new PointClass();
              pPoint.X = Convert.ToDouble(arr[0]);
              pPoint.Y = Convert.ToDouble(arr[1]);
              object a = Type.Missing;
              object b = Type.Missing;
              pPcl.AddPoint(pPoint, ref a, ref b);
              m = m.NextMatch();
           }
           IPolygon polygon = pPcl as IPolygon;
           if (polygon != null)
             polygon.SimplifyPreserveFromTo();
           return (IGeometry)polygon;
       }

 public IGeometry CreateMultiPolygon(string[] wkts)
       {
          IGeometryCollection geometryColl = new PolygonClass();
           IGeometry pGeometrRings = null;
           object missing = Type.Missing;
           foreach (string wkt in wkts)
           {
            //多面中含有环图形
              if (wkt.Contains("),"))
              {
                 string wkt_ring = wkt.Replace("),", "@");
                 string[] wkts_ring = wkt_ring.Split('@');
                 if (pGeometrRings == null || pGeometrRings.IsEmpty)
                    pGeometrRings = CreateMultiPolygon(wkts_ring);
                 else
                    pGeometrRings= MapUtility.GeometryOperation.UnionTwoPolygon(pGeometrRings as IPolygon, CreateMultiPolygon(wkts_ring) as IPolygon);
                 
              }
              else
              {
                 string RegExp = @"(\d+\.\d+|\d+)\s(\d+\.\d+|\d+)";
                 Regex regex = new Regex(RegExp, RegexOptions.Singleline | RegexOptions.IgnoreCase);
                 Match m = regex.Match(wkt.Trim());
                 IPointCollection pPcl = new RingClass();
                 while (m.Success)
                 {
                     string[] arr = m.Value.Trim().Split(new Char[] { ' ' });
                     IPoint pPoint = new PointClass();
                     pPoint.X = Convert.ToDouble(arr[0]);
                     pPoint.Y = Convert.ToDouble(arr[1]);
                    pPcl.AddPoint(pPoint, ref missing, ref missing);
                     m = m.NextMatch();
                 }
                 IRing ring = pPcl as IRing;
                 ring.Close();
                 geometryColl.AddGeometry(ring as IGeometry, ref missing, ref missing);
                 geometryColl.GeometriesChanged();
              }
           }
           IPolygon polygon = geometryColl as IPolygon;
           if (polygon != null)
             polygon.SimplifyPreserveFromTo();
           if (pGeometrRings != null )
              polygon = MapUtility.GeometryOperation.UnionTwoPolygon(polygon, pGeometrRings as IPolygon);
           return (IGeometry)polygon;
       }
using System;
using System.Collections.Generic;
using System.Text;
using GisSharpBlog.NetTopologySuite.IO;
using ESRI.ArcGIS.Geometry;

namespace Utils
{
    /// <summary>
    /// This class is used to convert a GeoAPI Geometry to ESRI and vice-versa.
    /// It can also convert a ESRI Geometry to WKB/WKT and vice-versa.
    /// </summary>
    public static class Converters
    {

        public static byte[] ConvertGeometryToWKB(IGeometry geometry)
        {
            IWkb wkb = geometry as IWkb;
            ITopologicalOperator oper = geometry as ITopologicalOperator;
            oper.Simplify();

            IGeometryFactory3 factory = new GeometryEnvironment() as IGeometryFactory3;
            byte[] b = factory.CreateWkbVariantFromGeometry(geometry) as byte[];
            return b;
        }


        public static byte[] ConvertWKTToWKB(string wkt)
        {
            WKBWriter writer = new WKBWriter();
            WKTReader reader = new WKTReader();
            return writer.Write(reader.Read(wkt));
        }

        public static string ConvertWKBToWKT(byte[] wkb)
        {
            WKTWriter writer = new WKTWriter();
            WKBReader reader = new WKBReader();
            return writer.Write(reader.Read(wkb));
        }

        public static string ConvertGeometryToWKT(IGeometry geometry)
        {
            byte[] b = ConvertGeometryToWKB(geometry);
            WKBReader reader = new WKBReader();
            GeoAPI.Geometries.IGeometry g = reader.Read(b);
            WKTWriter writer = new WKTWriter();
            return writer.Write(g);
        }

        public static IGeometry ConvertWKTToGeometry(string wkt)
        {
            byte[] wkb = ConvertWKTToWKB(wkt);
            return ConvertWKBToGeometry(wkb);
        }

        public static IGeometry ConvertWKBToGeometry(byte[] wkb)
        {
            IGeometry geom;
            int countin = wkb.GetLength(0);
            IGeometryFactory3 factory = new GeometryEnvironment() as IGeometryFactory3;
            factory.CreateGeometryFromWkbVariant(wkb, out geom, out countin);
            return geom;
        }


        public static IGeometry ConvertGeoAPIToESRI(GeoAPI.Geometries.IGeometry geometry)
        {
            WKBWriter writer = new WKBWriter();
            byte[] bytes = writer.Write(geometry);
            return ConvertWKBToGeometry(bytes);
        }

        public static GeoAPI.Geometries.IGeometry ConvertESRIToGeoAPI(IGeometry geometry)
        {
            byte[] wkb = ConvertGeometryToWKB(geometry);
            WKBReader reader = new WKBReader();
            return reader.Read(wkb);
        }
    }
}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

QQ359677345

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值