using ESRI.ArcGIS.Geometry;
using NetTopologySuite.IO;
using System;
using System.Text;
namespace Kingo.Plugin.ProofManager.Common
{
public static class GeometryConvertHelper
{
public static IGeometryFactory3 GeometryFactory3 { get; set; }
///
/// IGeometry转WKT
///
///
///
public static string ConvertIGeoemtryToWKT(IGeometry geometry)
{
return ConvertWKBToWKT(ConvertGeometryToWKB(geometry));
}
///
/// IGeometry转WKB
///
///
///
public static byte[] ConvertGeometryToWKB(IGeometry geometry)
{
//IWkb wkb = geometry as IWkb;
ITopologicalOperator oper = geometry as ITopologicalOperator;
oper.Simplify();
if (GeometryFactory3 == null)
{
GeometryFactory3 = new GeometryEnvironment() as IGeometryFactory3;
}
byte[] b = GeometryFactory3.CreateWkbVariantFromGeometry(geometry) as byte[];
return b;
}
///
/// WKT转IGeometry
///
///
///
public static IGeometry ConvertWKTToIGeometry(string wkt)
{
return ConvertWKBToIGeometry(ConvertWKTToWKB(wkt));
}
///
/// WKB转WKT
///
///
///
public static string ConvertWKBToWKT(byte[] wkb)
{
try
{
WKTWriter writer = new WKTWriter();
WKBReader reader = new WKBReader();
return writer.Write(reader.Read(wkb));
}
catch (Exception ex)
{
return ex.Message;
}
}
public static string GeometryToJsonString(ESRI.ArcGIS.Geometry.IGeometry geometry)
{
ESRI.ArcGIS.esriSystem.IJSONWriter jsonWriter = new ESRI.ArcGIS.esriSystem.JSONWriterClass();
jsonWriter.WriteToString();
ESRI.ArcGIS.Geometry.JSONConverterGeometryClass jsonCon = new ESRI.ArcGIS.Geometry.JSONConverterGeometryClass();
jsonCon.WriteGeometry(jsonWriter, null, geometry, false);
return Encoding.UTF8.GetString(jsonWriter.GetStringBuffer());
}
///
/// WKT转WKB
///
///
///
public static byte[] ConvertWKTToWKB(string wkt)
{
try
{
WKBWriter writer = new WKBWriter();
WKTReader reader = new WKTReader();
return writer.Write(reader.Read(wkt));
}
catch (Exception ex)
{
throw new Exception("WKT格式转WKB异常:" + ex.Message);
}
}
///
/// WKB转IGeometry
///
///
///
public static IGeometry ConvertWKBToIGeometry(byte[] wkb)
{
IGeometry geom;
try
{
int countin = wkb.GetLength(0);
if (GeometryFactory3 == null)
{
GeometryFactory3 = new GeometryEnvironment() as IGeometryFactory3;
}
GeometryFactory3.CreateGeometryFromWkbVariant(wkb, out geom, out countin);
return geom;
}
catch (Exception ex)
{
throw new Exception("WKB格式转IGeometry异常:" + ex.Message);
}
} ///
/// JSON图图形转IGeometry
///
/// 图形串
/// 图形类型:点,线、面
/// 输出图形坐标参考wkid,4490:CGCS2000球面坐标参考,默认输出CGCS2000球面坐标参考的json图形
/// 是地理坐标参考
///
public static IGeometry ConverJsonToIGeoemtry(string jsonGeometry, esriGeometryType geometryType = esriGeometryType.esriGeometryPolygon, int wkid = 4490, bool isGeograhicCoordinateSystem = true)
{
try
{
using (ESRI.ArcGIS.ADF.ComReleaser comReleaser = new ESRI.ArcGIS.ADF.ComReleaser())
{
ESRI.ArcGIS.esriSystem.IJSONReader reader = new ESRI.ArcGIS.esriSystem.JSONReaderClass();
comReleaser.ManageLifetime(reader);
reader.ReadFromString(jsonGeometry);
ESRI.ArcGIS.Geometry.JSONConverterGeometryClass jsonConverter = new JSONConverterGeometryClass();
comReleaser.ManageLifetime(jsonConverter);
IGeometry geometry = jsonConverter.ReadGeometry(reader, geometryType, false, false);
if (geometry == null || geometry.IsEmpty)
{
throw new Exception("转换后图形为空!");
}
ESRI.ArcGIS.Geometry.ITopologicalOperator topological = geometry as ESRI.ArcGIS.Geometry.ITopologicalOperator;
if (!topological.IsSimple)
{
topological.Simplify();
}
ISpatialReference spatialReference = null;
if (isGeograhicCoordinateSystem)
{
spatialReference = CreateGeographicCoordinateSystem(wkid);
}
else
{
spatialReference = CreateProjectedCoordinateSystem(wkid);
}
if (wkid > 0)
{
if (geometry.SpatialReference == null)
{
geometry.SpatialReference = spatialReference;
}
else if (wkid != geometry.SpatialReference.FactoryCode)
{
geometry.Project(spatialReference);
}
}
return geometry;
}
}
catch (Exception ex)
{
throw ex;
}
}
///
/// 根据坐标WKID创建地理坐标参考
///
/// WKID
///
public static IGeographicCoordinateSystem CreateGeographicCoordinateSystem(int WKID)
{
try
{
ISpatialReferenceFactory2 pSpatialRefFac = new SpatialReferenceEnvironmentClass();
return pSpatialRefFac.CreateGeographicCoordinateSystem(WKID);
}
catch (Exception ex)
{
throw ex;
}
}
///
/// 根据WKID创建投影坐标参考
///
///
///
public static IProjectedCoordinateSystem CreateProjectedCoordinateSystem(int wkid)
{
//创建一个要素集创建一个投影
ISpatialReferenceFactory pSpatialRefFac = new SpatialReferenceEnvironmentClass();
return pSpatialRefFac.CreateProjectedCoordinateSystem(wkid);
}
}
}