当前位置: 首页 > 工具软件 > WKT > 使用案例 >

AE解析WKT生成图形C#

仉英朗
2023-12-01
 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);
        }
    }
}

 类似资料: