下面是将Feature对象的Geometry对象和属性分别转换为两个Json文本,代码很简单:
/// <summary> /// 提取feature的geometry,并将其转换为json对象 /// </summary> /// <param name="pFeature">要素对象</param> /// <returns></returns> private string feature2JsonGeometry(ESRI.ArcGIS.Geodatabase.IFeature pFeature) { ESRI.ArcGIS.Geometry.IGeometry pGeo = pFeature.Shape; int wkid = pGeo.SpatialReference.FactoryCode; ESRI.ArcGIS.Geometry.IPoint pPoint = null; ESRI.ArcGIS.Geometry.IPointCollection pPoints = null; double x, y; StringBuilder sb = new StringBuilder("{"); switch (pGeo.GeometryType) { #region Point2Json case ESRI.ArcGIS.Geometry.esriGeometryType.esriGeometryPoint: pPoint = pGeo as ESRI.ArcGIS.Geometry.IPoint; pPoint.QueryCoords(out x, out y); string json = @"{""x"":" + x + @",""y"":" + y + @",""spatialReference"":" + @"{""wkid"":" + wkid + "}"; sb.Append(@"""point"":" + json); break; #endregion #region Polyline2Json case ESRI.ArcGIS.Geometry.esriGeometryType.esriGeometryPolyline: pPoints = pGeo as ESRI.ArcGIS.Geometry.IPointCollection; sb.Append(@"""paths"":[["); for (int i = 0; i < pPoints.PointCount; i++) { pPoint = pPoints.get_Point(i); pPoint.QueryCoords(out x, out y); sb.Append("[" + x + "," + y + "],"); } sb.Remove(sb.Length - 1, 1); sb.Append("]]" + @",""spatialReference"":" + @"{""wkid"":" + wkid + "}"); break; #endregion #region Polygon2Json case ESRI.ArcGIS.Geometry.esriGeometryType.esriGeometryPolygon: pPoints = pGeo as ESRI.ArcGIS.Geometry.IPointCollection; sb.Append(@"""rings"":[["); for (int i = 0; i < pPoints.PointCount; i++) { pPoint = pPoints.get_Point(i); pPoint.QueryCoords(out x, out y); sb.Append("[" + x + "," + y + "],"); } sb.Remove(sb.Length - 1, 1); sb.Append("]]" + @",""spatialReference"":" + @"{""wkid"":" + wkid + "}"); break; #endregion } sb.Append("}"); return sb.ToString(); } /// <summary> /// 创建feature要素非空间属性的json对象 /// </summary> /// <param name="pFeature">要素对象</param> /// <returns></returns> private string feature2JsonGeoAttribute(ESRI.ArcGIS.Geodatabase.IFeature pFeature) { StringBuilder sb = new StringBuilder("{"); for (int i = 0; i < pFeature.Fields.FieldCount; i++) { if (pFeature.Fields.get_Field(i).Name.ToLower() != "shape") { string name = pFeature.Fields.get_Field(i).Name; sb.Append(@",""" + name + @""":"); if (pFeature.get_Value(i).ToString() == "") sb.Append(@""""""); else sb.Append(@"""" + pFeature.get_Value(i) + @""""); } } sb.Remove(1, 1); sb.Append("}"); return sb.ToString(); }