昨天發了將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);
}