# C#使用GeoJSON点集合创建类型为ArcGIS Geometry的外接正方形

``````        /// <summary>传入一系列的GeoJSON格式的点集，转换得到外包正方形
///
/// </summary>
/// <param name="listPoint">GeoJSON格式的点集</param>
/// <param name="b">可选参数，缓冲距离</param>
/// <returns></returns>
public static ESRI.ArcGIS.Geometry.IPolygon CreateEsriPolygon(List<GeoJSON.Net.Geometry.Point> listPoint, double? b = 0.0)
{
if (0 >= listPoint.Count)
return null;

var firstPoint = listPoint[0].Coordinates;
double dMinX = firstPoint.X; double dMinY = firstPoint.Y; double dMaxX = firstPoint.X; double dMaxY = firstPoint.Y; double dSumX = 0.00; double dSumY = 0.00;
foreach (var aPt in listPoint)
{
dMinX = aPt.Coordinates.X <= dMinX ? aPt.Coordinates.X : dMinX;
dMinY = aPt.Coordinates.Y <= dMinY ? aPt.Coordinates.Y : dMinY;
dMaxX = aPt.Coordinates.X >= dMaxX ? aPt.Coordinates.X : dMaxX;
dMaxY = aPt.Coordinates.Y >= dMaxY ? aPt.Coordinates.Y : dMaxY;
dSumX += aPt.Coordinates.X;
dSumY += aPt.Coordinates.Y;
}
double dCenX = dSumX * 100 / listPoint.Count * 0.01;  //正方形中心点X坐标
double dCenY = dSumY * 100 / listPoint.Count * 0.01;  //正方形中心点Y坐标
double dHalfLength = (((dMaxX - dMinX) > (dMaxY - dMinY)) ? (dMaxX - dMinX) : (dMaxY - dMinY)) * 0.5;
dHalfLength += (b.HasValue && (double)b > 0) ? (double)b : 0;  //正方形边长的一半

ESRI.ArcGIS.Geometry.IPointCollection pPointCol = new ESRI.ArcGIS.Geometry.Polygon();
pPointCol.AddPoint(CreatePoint(dCenX - dHalfLength, dCenY + dHalfLength));  //左上角点
pPointCol.AddPoint(CreatePoint(dCenX + dHalfLength, dCenY + dHalfLength));  //右上角点
pPointCol.AddPoint(CreatePoint(dCenX + dHalfLength, dCenY - dHalfLength));  //右下角点
pPointCol.AddPoint(CreatePoint(dCenX - dHalfLength, dCenY - dHalfLength));   //左下角点
pPointCol.AddPoint(CreatePoint(dCenX - dHalfLength, dCenY + dHalfLength));  //左上角点（用于闭合线段）

return pPointCol as ESRI.ArcGIS.Geometry.IPolygon;
}``````