ArcGIS Engine 几何对象和WKB的转换

时间:2021-09-24 23:14:51
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();
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);
}
}
}