设为首页 加入收藏

TOP

ArcObjects中的IGeometry转成Json(一)
2014-11-24 00:08:10 来源: 作者: 【 】 浏览:19
Tags:ArcObjects IGeometry 转成 Json

昨天发了将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 + "}");

}
bre

首页 上一页 1 2 下一页 尾页 1/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇为Silverlight 提供将Json解析为G.. 下一篇ArcGIS 10.1 for Server 如何使用..

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: