/// <summary> /// IGeometry转成JSON字符串 /// </summary> public static string GeometryToJsonString(IGeometry geometry) { IJSONWriter jsonWriter = new JSONWriterClass(); jsonWriter.WriteToString(); JSONConverterGeometryClass jsonCon = new JSONConverterGeometryClass(); jsonCon.WriteGeometry(jsonWriter, null, geometry, false); return(Encoding.UTF8.GetString(jsonWriter.GetStringBuffer())); }
public static string ConvertGeometryToJson(IGeometry geometry, bool isGeneralize = false) { string geomJsonStr = null; try { ITopologicalOperator topoGeom = geometry as ITopologicalOperator; topoGeom.Simplify(); //IPolygon polygon = topoGeom as IPolygon; //if (polygon != null) //{ // polygon.Generalize(1); //} //if (geometry.SpatialReference == null || geometry.SpatialReference.Name == "Unknown") //{ // geometry.SpatialReference = SpatialRefOpt.GetProjectedCoordinate(esriSRProjCS4Type.esriSRProjCS_Xian1980_3_Degree_GK_CM_120E); //} if (isGeneralize) { ISpatialReference spatialReference = geometry.SpatialReference; enumSpatialRelType spatialRelType = GetSpatialRelType(spatialReference); double offset = 0.000001; if (spatialRelType == enumSpatialRelType.GCS) { offset = 0.00000001; } IPolycurve polycurve = geometry as IPolycurve; polycurve.Generalize(offset); } IJSONWriter jsonWriter = new JSONWriterClass(); jsonWriter.WriteToString(); JSONConverterGeometryClass jsonCon = new JSONConverterGeometryClass(); jsonCon.WriteGeometry(jsonWriter, null, geometry, false); geomJsonStr = Encoding.UTF8.GetString(jsonWriter.GetStringBuffer()); } catch (Exception ex) { System.Diagnostics.Trace.WriteLine("\nConvertGeometryToJson::error::" + ex.Source + ".\n" + ex.ToString()); } return(geomJsonStr); }