ArcObjects中的IGeometry转成Json
来源:互联网 发布:fdm和idm 知乎 编辑:程序博客网 时间:2024/04/30 22:18
昨天发了将Json格式的描述转换为Web API中的Geometry,今天发一个将ArcObjects中的IGeometry转成Json。
private string Geometry2Json(IGeometry pGeo) { int wkid = pGeo.SpatialReference.FactoryCode; ESRI.ArcGIS.Geometry.IPoint pPoint = null; ESRI.ArcGIS.Geometry.IPointCollection pPoints = null; double x, y; StringBuilder sb = new StringBuilder("{"); sb.Append(@"""geometries""" + ":{"); 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; IPolyline pPolyline = pGeo as IPolyline; IGeometryCollection pGeoetryCollection = pPolyline as IGeometryCollection; if (pGeoetryCollection.GeometryCount >= 1) { sb.Append(@"""paths"":["); for (int i = 0; i < pGeoetryCollection.GeometryCount; i++) { //paths可能有多个path,而每一个path是多个点,用两个for循环 if (pGeoetryCollection.get_Geometry(i) is IPath) { sb.Append("["); pPoints = pGeoetryCollection.get_Geometry(i) as IPointCollection; for (int j = 0; j < pPoints.PointCount;j++ ) { pPoint = pPoints.get_Point(j); pPoint.QueryCoords(out x, out y); sb.Append("[" + x + "," + y + "],"); } sb.Remove(sb.Length - 1, 1); sb.Append("]"); } } sb.Append("]" + @",""spatialReference"":" + @"{""wkid"":" + wkid + "}"); } break; #endregion #region Polygon2Json case ESRI.ArcGIS.Geometry.esriGeometryType.esriGeometryPolygon: pPoints = pGeo as ESRI.ArcGIS.Geometry.IPointCollection; IPolygon pPolygon = pGeo as IPolygon; //外环和内环?面的构造比较复杂,在AO中我们可以获取内环和外环,如果注意过在客户端API中的Rings数组,似乎看不出,所以这里我没采用外环和内环的构造方式,但是代码在后面附上。 IGeometryCollection pGeoetryCollection1 = pPolygon as IGeometryCollection; if (pGeoetryCollection1.GeometryCount >=1) { sb.Append(@"""rings"":["); for (int i = 0; i < pGeoetryCollection1.GeometryCount; i++) { if (pGeoetryCollection1.get_Geometry(i) is IRing) { sb.Append("["); pPoints = pGeoetryCollection1.get_Geometry(i) as IPointCollection; for (int j = 0; j < pPoints.PointCount;j++ ) { pPoint = pPoints.get_Point(j); pPoint.QueryCoords(out x, out y); sb.Append("[" + x + "," + y + "],"); } sb.Remove(sb.Length - 1, 1); sb.Append("]"); } } sb.Append("]" + @",""spatialReference"":" + @"{""wkid"":" + wkid + "}"); } break; #endregion } sb.Append("}"); //添加Geometry sb.Append("}"); return sb.ToString(); }
下面是获取面的内环和外环的坐标
public void PolygonToString(IPolygon4 polygon) { IGeometryBag exteriorRingGeometryBag = polygon.ExteriorRingBag; IGeometryCollection exteriorRingGeometryCollection = exteriorRingGeometryBag as IGeometryCollection; Trace.WriteLine("polygon.ExteriorRingCount = "+ exteriorRingGeometryCollection.GeometryCount); for(int i = 0; i < exteriorRingGeometryCollection.GeometryCount; i++) { Trace.WriteLine("polygon.ExteriorRing["+ i +"]"); IGeometry exteriorRingGeometry = exteriorRingGeometryCollection.get_Geometry(i); IPointCollection exteriorRingPointCollection = exteriorRingGeometry as IPointCollection; for(int j = 0; j < exteriorRingPointCollection.PointCount; j++) { Trace.WriteLine("Point["+ j +"] = "+ PointToString(exteriorRingPointCollection.get_Point(j))); } IGeometryBag interiorRingGeometryBag = polygon.get_InteriorRingBag(exteriorRingGeometry as IRing); IGeometryCollection interiorRingGeometryCollection = interiorRingGeometryBag as IGeometryCollection; Trace.WriteLine("polygon.InteriorRingCount[exteriorRing"+ i +"] = "+ interiorRingGeometryCollection.GeometryCount); for(int k = 0; k < interiorRingGeometryCollection.GeometryCount; k++) { Trace.WriteLine("polygon.InteriorRing["+ k +"]"); IGeometry interiorRingGeometry = interiorRingGeometryCollection.get_Geometry(k); IPointCollection interiorRingPointCollection = interiorRingGeometry as IPointCollection; for(int m = 0; m < interiorRingPointCollection.PointCount; m++) { Trace.WriteLine("Point["+ m +"] = "+ PointToString(interiorRingPointCollection.get_Point(m))); } } } } private string PointToString(IPoint point) { return(point.X +", "+ point.Y +", "+ point.Z); }
- ArcObjects中的IGeometry转成Json
- ArcObjects中的IGeometry转成Json
- ArcObjects中的IGeometry转成Json
- ArcGIS Engine中 IGeometry和Json字符串的相互转换
- ArcGIS Engine中 IGeometry和Json字符串的相互转换
- ArcGIS Engine中 IGeometry和Json字符串的相互转换
- ArcGIS Engine中 IGeometry和Json字符串的相互转换
- ArcObjects
- IGeometry接口
- ArcObjects中的类结构关系图
- ArcObjects中的GraphicsLayer---------元素的容器
- List 转成 Json
- 将RSS转成json
- json 转成对象
- dt转成JSON
- list转成json对象
- list转成json对象
- DataSet转成JSON对象
- C语言字符数组的初始化
- 不吃早饭后期易得很多的病?胃炎,胆结石。。。
- 缺钙吃这些蔬菜能补钙
- Esri美国用户大会Q&A 之 ArcGIS Server未来发展
- sql操作数据库修改字段中的某些字串
- ArcObjects中的IGeometry转成Json
- Dive Into Python 学习记录3-对获取某文件夹下MP3文件信息的代码构成分析
- Spring Security oAuth学习之Hello World
- Redis复制流程概述
- libevent源码深度剖析
- Re-installation failed due to different application signatures.
- 每日一读---补充正能量
- 大数据与信息社会的意义结构【转自阿里巴巴研究院】
- Redis内存使用优化与存储