using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ArcGIS.Core.CIM;
using ArcGIS.Core.Data;
using ArcGIS.Core.Geometry;
using ArcGIS.Desktop.Catalog;
using ArcGIS.Desktop.Core;
using ArcGIS.Desktop.Editing;
using ArcGIS.Desktop.Extensions;
using ArcGIS.Desktop.Framework;
using ArcGIS.Desktop.Framework.Contracts;
using ArcGIS.Desktop.Framework.Dialogs;
using ArcGIS.Desktop.Framework.Threading.Tasks;
using ArcGIS.Desktop.Mapping;
using ArcGIS.Desktop.Layouts;
using ArcGIS.Desktop.Core.Geoprocessing;
using ArcGIS.Desktop.Editing.Attributes;
namespace YLPub
{
public class YLGEOPub
{
// 从几何对象中获取插入中心点
public static MapPoint getInsertCenter(Geometry pGeometry)
{
// 获取几何对象的中心点
MapPoint CenterPoint = getCenterPoint(pGeometry);
// 如果几何对象包含中心点,返回中心点
if (Contains(pGeometry, CenterPoint))
{
return CenterPoint;
}
else
{
// 获取几何对象的标签点
CenterPoint = getLabelPoint(pGeometry);
if (Contains(pGeometry, CenterPoint))
{
return CenterPoint;
}
else
{
return null;
}
}
}
//交换节点,反转
public static List<MapPoint> Swap(List<MapPoint> mapPoints)
{
List<MapPoint> pList = new List<MapPoint>();
int num = mapPoints.Count;
for (int i = num - 1; i > -1; i--)
{
pList.Add(mapPoints[i]);
}
return pList;
}
// 获取几何对象的面积
public static double getArea(Geometry geometry1)
{
return GeometryEngine.Instance.Area(geometry1);
}
// 获取几何对象的椭球面积
public static double GeodesicArea(Geometry geometry1)
{
return GeometryEngine.Instance.GeodesicArea(geometry1);
}
// 将矩形转为多边形
public static Polygon RectToPoly(Envelope envelope)
{
var newPoly = PolygonBuilderEx.CreatePolygon(envelope);
return newPoly;
}
//多个边形生成一个多边形,可以中间带孔
public static Polygon PolygonbyList(List<Polygon> PolygonList)
{
var newpoly = PolygonBuilderEx.CreatePolygon(PolygonList);
return newpoly;
}
public static Polygon PolygonbyList(Polygon polygon1, Polygon polygon2)
{
List<Polygon> PolygonList = new List<Polygon>();
PolygonList.Add(polygon1);
PolygonList.Add(polygon2);
return PolygonbyList(PolygonList);
}
//有线生成面
public static Polygon PolylineToPolygon(Polyline polyline)
{
var newPoly = PolygonBuilderEx.CreatePolygon(polyline);
return newPoly;
}
//线中点分割
public static Polyline SplitPolyline(Polyline polyline)
{
var polylineBuilder = new PolylineBuilderEx(polyline);
polylineBuilder.SplitAtDistance(0.5, true);
var reversedPolyline = polylineBuilder.ToGeometry();
return reversedPolyline;
}
// 将点列表转为多边形
public static Polygon PointsToPolygon(List<MapPoint> mapPoints)
{
var newPoly = PolygonBuilderEx.CreatePolygon(mapPoints.AsEnumerable<MapPoint>());
return newPoly;
}
// 将有空间参考的点列表转为多边形
public static Polygon PointsToPolygon(List<MapPoint> mapPoints, SpatialReference spatialReference)
{
//int num = mapPoints.Count;
//for (int i = 0; i < num; i++)
//{
// // MessageBox.Show(mapPoints[i].X.ToString() + ":" + mapPoints[i].Y.ToString() + ":" + i.ToString());
//}
var newPoly = PolygonBuilderEx.CreatePolygon(mapPoints.AsEnumerable<MapPoint>(), spatialReference);
return newPoly;
}
//翻转线
public static Polyline FlipLine(Polyline polyline)
{
var polylineBuilder = new PolylineBuilderEx(polyline);
polylineBuilder.ReverseOrientation();
var reversedPolyline = polylineBuilder.ToGeometry();
return reversedPolyline;
//using (PolylineBuilder polylineBuilder = new PolylineBuilder(polyline))
//{
// polylineBuilder.ReverseOrientation();
// Polyline reversedPolyline = polylineBuilder.ToGeometry();
// return reversedPolyline;
//}
}
// 延长一条线以满足另一条线
public static Polyline LineExtent(Polyline polyline, Polyline otherLine)
{
return GeometryEngine.Instance.Extend(polyline, otherLine, ExtendFlags.Default);
}
// 获取两线延长后的交点
public static MapPoint LineExtentPoint(double x1, double y1, double x2, double y2, double ax1, double ay1, double ax2, double ay2, SpatialReference spatialReference)
{
Polyline line1 = gettwoLine(x1, y1, x2, y2, spatialReference);
Polyline line2 = gettwoLine(ax1, ay1, ax2, ay2, spatialReference);
return LineExtentPoint(line1, line2);
}
//线延长后的交点
public static MapPoint LineExtentPoint(Polyline polyline, Polyline otherLine)
{
Polyline line1 = GeometryEngine.Instance.Extend(polyline, otherLine, ExtendFlags.Default);
Geometry geo = Difference(line1, polyline);
if (geo == null) return null;
Polyline line2 = geo as Polyline;
MapPoint p1 = line2.Points[0];
MapPoint p2 = line2.Points[1];
if (Distance(p1, polyline) < 0.001)
{
return p2;
}
else
{
return p1;
}
}
// 将多边形转为线
public static Polyline PolygonToPolyline(Polygon polygon)
{
var newline = PolylineBuilderEx.CreatePolyline(polygon);
return newline;
}
// 联合两个几何对象
public static Geometry Union(Geometry geometry1, Geometry geometry2)
{
return GeometryEngine.Instance.Union(geometry1, geometry2);
}
// 获取几何对象的标注点
public static MapPoint LabelPoint(Geometry geometry)
{
return GeometryEngine.Instance.LabelPoint(geometry);
}
// 获取几何对象的中心点
public static MapPoint getCenterPoint(Geometry geometry)
{
return GeometryEngine.Instance.Centroid(geometry);
}
/// 获取几何对象的标注点或中心点
public static MapPoint getLabelPoint(Geometry geometry)
{
MapPoint mapPoint = getCenterPoint(geometry);
if (Disjoint(mapPoint, geometry))
{
return GeometryEngine.Instance.LabelPoint(geometry);
}
else
{
return mapPoint;
}
}
// 获取两个几何对象间的距离
public static double getdis(Geometry geometry1, Geometry geometry2)
{
return GeometryEngine.Instance.Distance(geometry1, geometry2);
}
// 获取线的中心点
public static MapPoint getLineCenterPoint(Polyline polyline)
{
Polyline polyline1 = GeometryEngine.Instance.GetSubCurve(polyline, 0, 0.5, AsRatioOrLength.AsRatio);
var segments = new List<Segment>() as ICollection<Segment>;
polyline1.GetAllSegments(ref segments);
return segments.First().EndPoint;
}
// 移动几何对象
public static Geometry Move(Geometry geometry, double dx, double dy)
{
return GeometryEngine.Instance.Move(geometry, dx, dy);
}
// 旋转几何对象
public static Geometry Rotate(Geometry geometry, MapPoint mapPoint, double angle)
{
return GeometryEngine.Instance.Rotate(geometry, mapPoint, angle);
}
// 缩放几何对象
public static Geometry Scale(Geometry geometry, MapPoint mapPoint, double sx, double sy)
{
return GeometryEngine.Instance.Scale(geometry, mapPoint, sx, sy);
}
// 简化几何对象
public static Geometry SimplifyAsFeature(Geometry geometry)
{
return GeometryEngine.Instance.SimplifyAsFeature(geometry);
}
// 检查两个几何对象是否接触
public static bool Touches(Geometry geometry1, Geometry geometry2)
{
return GeometryEngine.Instance.Touches(geometry1, geometry2);
}
// 返回给定点最接近的几何对象的顶点
public static ProximityResult hitTest(Geometry geometry, MapPoint point)
{
return GeometryEngine.Instance.NearestVertex(geometry, point); // 注意:有问题
}
// 投影几何对象
public static Geometry Project(Geometry geometry, SpatialReference outSpatialReference)
{
return GeometryEngine.Instance.Project(geometry, outSpatialReference); // 注意:有问题
}
// 获取两个点之间的距离
public static double getdistance(MapPoint p1, MapPoint p2)
{
return Math.Sqrt((p1.X - p2.X) * (p1.X - p2.X) + (p1.Y - p2.Y) * (p1.Y - p2.Y));
}
// 返回给定点最接近的多边形顶点的索引
public static int hitTest(Polygon polygon, MapPoint point)
{
int num = polygon.Points.Count();
double min = double.MaxValue;
int idx = -1;
for (int i = 0; i < num; i++)
{
double d = getdistance(polygon.Points[i], point);
if (d < min)
{
min = d;
idx = i;
}
}
return idx;
}
// 返回两个几何对象的交集
public static Geometry Intersect(Geometry geometry1, Geometry geometry2, GeometryDimensionType resultDimension)
{
return GeometryEngine.Instance.Intersection(geometry1, geometry2, resultDimension);
}
// 检查两个几何对象是否重叠
public static bool Overlap(Geometry geometry1, Geometry geometry2)
{
return GeometryEngine.Instance.Overlaps(geometry1, geometry2);
}
// 检查两个几何对象是否相交
public static bool Intersects(Geometry geometry1, Geometry geometry2)
{
return GeometryEngine.Instance.Intersects(geometry1, geometry2);
}
// 检查两个几何对象是否相等
public static bool Equals(Geometry geometry1, Geometry geometry2)
{
return GeometryEngine.Instance.Equals(geometry1, geometry2);
}
// 检查一个几何对象是否位于另一个几何对象内部
public static bool Within(Geometry geometry1, Geometry geometry2)
{
return GeometryEngine.Instance.Within(geometry1, geometry2);
}
// 获取两个几何对象之间的距离
public static double Distance(Geometry geometry1, Geometry geometry2)
{
return GeometryEngine.Instance.Distance(geometry1, geometry2);
}
// 检查一个几何对象是否包含另一个几何对象
public static bool Contains(Geometry geometry1, Geometry geometry2)
{
return GeometryEngine.Instance.Contains(geometry1, geometry2);
}
// 扩展包围盒
public static Envelope Expand有问题(Envelope envelope, double wd)
{
if (Math.Abs(wd) < 0.0001) // 判断wd是否接近于0
{
return envelope;
}
else
return envelope.Expand(wd, wd, false); // 扩展包围盒的宽度和高度
}
// 扩展包围盒的另一种方式
public static Envelope Expand(Envelope envelope, double wd)
{
if (Math.Abs(wd) < 0.0001)
{
return envelope;
}
else
{
// 获取空间参考
SpatialReference spatialReference = envelope.SpatialReference;
// 计算新的包围盒坐标
double MinX = envelope.XMin - wd;
double MinY = envelope.YMin - wd;
double MaxX = envelope.XMax + wd;
double MaxY = envelope.YMax + wd;
// 创建新的包围盒
Envelope envelope1 = EnvelopeBuilderEx.CreateEnvelope(MinX, MinY, MaxX, MaxY, spatialReference);
return envelope1;
}
}
// 判断两个几何体是否不相交
public static bool Disjoint(Geometry geometry1, Geometry geometry2)
{
return GeometryEngine.Instance.Disjoint(geometry1, geometry2);
}
// 计算几何体的差异
public static Geometry Difference(Geometry geometry1, Geometry geometry2)
{
try
{
// 检查两个几何体的空间参考是否相同
if (geometry1.SpatialReference.Wkid != geometry2.SpatialReference.Wkid)
{
MessageBox.Show("两个坐标系不一样" + geometry1.SpatialReference.Name + ":" + geometry2.SpatialReference.Name);
return null;
}
geometry2 = GeometryEngine.Instance.SimplifyAsFeature(geometry2, true);
return GeometryEngine.Instance.Difference(geometry1, geometry2);
}
catch (Exception ex)
{
MessageBox.Show("异常:" + ex.Message.ToString());
if (geometry1.GeometryType == GeometryType.Polygon && geometry2.GeometryType == GeometryType.Polygon)
{
return DifferenceNew(geometry1, geometry2);
}
else
{
return null;
}
}
}
// 从段集合中获取所有起点
public static List<MapPoint> getPoints(ReadOnlySegmentCollection segments)
{
List<MapPoint> pList = new List<MapPoint>();
int num = segments.Count;
for (int i = 0; i < num; i++)
{
pList.Add(segments[i].StartPoint);
if (i == num - 1)
{
pList.Add(segments[i].EndPoint);
}
}
return pList;
}
// 反转点列表的顺序
public static List<MapPoint> reversePoints(List<MapPoint> mapPoints)
{
List<MapPoint> pList = new List<MapPoint>();
int num = mapPoints.Count;
for (int i = num - 1; i > -1; i--)
{
pList.Add(mapPoints[i]);
}
return pList;
}
//原来的擦除有问题,自己做一个,自定义的差异计算方法
public static Geometry DifferenceNew(Geometry geometry1, Geometry geometry2)
{
List<Polygon> PolygonList = new List<Polygon>();
PolygonList.Add(geometry1 as Polygon);
Polygon polygon = geometry2 as Polygon;
int pnum = polygon.PartCount;//只要外多边形
List<MapPoint> mapPoints = getPoints(polygon.Parts[0]); //只要外多边形
mapPoints = reversePoints(mapPoints);
IEnumerable<MapPoint> EMapPoint = mapPoints.AsEnumerable<MapPoint>();
Polygon polygon1 = PolygonBuilderEx.CreatePolygon(EMapPoint);
PolygonList.Add(polygon1);
Polygon polygon2 = PolygonBuilderEx.CreatePolygon(PolygonList.AsEnumerable<Polygon>());
return polygon2;
}
/// <summary>
/// 创建缓冲区
/// </summary>
/// <param name="geometry1"></param>
/// <param name="geometry2"></param>
/// <returns></returns>
public static Geometry Buffer(Geometry geometry, double distance)
{
return GeometryEngine.Instance.Buffer(geometry, distance);
}
//把一个图层选择的对象合并一起
public static async Task<Geometry> UnionSelect(FeatureLayer featureLayer)
{
List<Geometry> pList = new List<Geometry>();
Geometry geometry1 = null;
await QueuedTask.Run(() =>
{
RowCursor rowCursor = featureLayer.GetSelection().Search(null);
while (rowCursor.MoveNext())
{
Feature feature = rowCursor.Current as Feature;
Geometry geometry = feature.GetShape();
pList.Add(geometry);
}
});
int num = pList.Count;
if (num > 1)
{
await QueuedTask.Run(() =>
{
IEnumerable<Geometry> Engeometry = pList.AsEnumerable<Geometry>();
geometry1 = GeometryEngine.Instance.Union(Engeometry);
});
}
else if (num == 1)
{
geometry1 = pList[0];
}
return geometry1;
}
//一个图层所有的对象合并在一起
public static async Task<Geometry> UnionLayerGeo(FeatureLayer featureLayer)
{
List<Geometry> pList = new List<Geometry>();
Geometry geometry1 = null;
await QueuedTask.Run(() =>
{
RowCursor rowCursor = featureLayer.Search(null);
while (rowCursor.MoveNext())
{
Feature feature = rowCursor.Current as Feature;
Geometry geometry = feature.GetShape();
pList.Add(geometry);
}
});
int num = pList.Count;
if (num > 1)
{
await QueuedTask.Run(() =>
{
IEnumerable<Geometry> Engeometry = pList.AsEnumerable<Geometry>();
geometry1 = GeometryEngine.Instance.Union(Engeometry);
});
}
else if (num == 1)
{
geometry1 = pList[0];
}
return geometry1;
}
//更快速的方法合并图层中的所有对象
public static async Task<Envelope> UnionLayerGeo2(FeatureLayer featureLayer)
{
return await QueuedTask.Run(() => featureLayer.QueryExtent());
}
//获取指定坐标的矩形多边形
public static Polygon gettRectPoly(double x1, double y1, double x2, double y2, SpatialReference spatialReference)
{
Envelope envelope = EnvelopeBuilderEx.CreateEnvelope(x1, y1, x2, y2, spatialReference);
return RectToPoly(envelope);
}
/// <summary>
/// 4点多边形
/// </summary>
/// <param name="p1"></param>
/// <param name="p2"></param>
/// <param name="p3"></param>
/// <param name="p4"></param>
/// <param name="spatialReference"></param>
/// <returns></returns>
public static Polygon gettPoly4Point(MapPoint p1, MapPoint p2, MapPoint p3, MapPoint p4, SpatialReference spatialReference)
{
List<MapPoint> mapPoints = new List<MapPoint>();
mapPoints.Add(p1);
mapPoints.Add(p2);
mapPoints.Add(p3);
mapPoints.Add(p4);
Polygon polygon = PolygonBuilderEx.CreatePolygon(mapPoints.AsEnumerable<MapPoint>(), spatialReference);
return polygon;
}
//获取二维地图点
public static MapPoint getMapPoint(double x1, double y1, SpatialReference spatialReference)
{
MapPoint p1 = MapPointBuilderEx.CreateMapPoint(x1, y1, spatialReference);
return p1;
}
//获取三维地图点
public static MapPoint getMapPoint(double x1, double y1, double z, SpatialReference spatialReference)
{
MapPoint p1 = MapPointBuilderEx.CreateMapPoint(x1, y1, z, spatialReference);
return p1;
}
//获取二维坐标
public static Coordinate2D gePoint(double x1, double y1)
{
Coordinate2D p1 = new Coordinate2D(x1, y1);
return p1;
}
//二维转三维点
public static MapPoint To3DdPoint(MapPoint mapPoint)
{
if (!mapPoint.HasZ)
{
double x = mapPoint.X;
double y = mapPoint.Y;
SpatialReference spatialReference = mapPoint.SpatialReference;
return getMapPoint(x, y, 0, spatialReference);
}
else
{
return mapPoint;
}
}
// 创建并返回与给定点具有相同X和Y坐标的新MapPoint
public static MapPoint get2MapPoint(MapPoint p)
{
MapPoint p1 = MapPointBuilderEx.CreateMapPoint(p.X, p.Y, p.SpatialReference);
return p1;
}
// 返回两个几何形状的交点
public static MapPoint getIntersectPoint(Geometry geometry1, Geometry geometry2)
{
Geometry geometry = Intersect(geometry1, geometry2, GeometryDimensionType.EsriGeometry0Dimension);
if (geometry == null) return null;
if (geometry.PointCount < 1) return null;
return geometry.Extent.Center;
}
// 使用两个给定的MapPoints创建并返回一条线
public static Polyline gettwoLine(MapPoint p1, MapPoint p2, SpatialReference spatialReference = null)
{
var lineMapPoints = new List<MapPoint>(2);
lineMapPoints.Add(p1);
lineMapPoints.Add(p2);
var newPolyline = PolylineBuilderEx.CreatePolyline(lineMapPoints, spatialReference);
return newPolyline;
}
// 使用提供的坐标创建并返回一条线
public static Polyline gettwoLine(double x1, double y1, double x2, double y2, SpatialReference spatialReference)
{
MapPoint p1 = MapPointBuilderEx.CreateMapPoint(x1, y1, spatialReference);
MapPoint p2 = MapPointBuilderEx.CreateMapPoint(x2, y2, spatialReference);
return gettwoLine(p1, p2, spatialReference);
}
// 使用提供的坐标(不指定空间参考)创建并返回一条线
public static Polyline gettwoLine(double x1, double y1, double x2, double y2)
{
Coordinate2D p1 = new Coordinate2D(x1, y1);
Coordinate2D p2 = new Coordinate2D(x2, y2);
List<Coordinate2D> pList = new List<Coordinate2D>();
pList.Add(p1);
pList.Add(p2);
IEnumerable<Coordinate2D> coordinates = pList.AsEnumerable<Coordinate2D>();
var newPolyline = PolylineBuilderEx.CreatePolyline(coordinates);
return newPolyline;
}
// 在指定的FeatureClass中插入新行
public static void Insert(FeatureClass featureLayer, Geometry geometry, int geoidx, int IDidx, object ID)
{
QueuedTask.Run(() =>
{
RowBuffer rowBuffer = featureLayer.CreateRowBuffer();
if (geoidx > -1)
{
rowBuffer[geoidx] = geometry;
}
if (IDidx > -1)
{
rowBuffer[IDidx] = ID;
}
featureLayer.CreateRow(rowBuffer);
rowBuffer.Dispose();
});
}
// 允许插入额外字段和值的重载插入方法
public async static void Insert(FeatureClass featureLayer, Geometry geometry, int geoidx, int IDidx, object ID,
int oldtfhidx, object oldtfh)
{
await QueuedTask.Run(() =>
{
RowBuffer rowBuffer = featureLayer.CreateRowBuffer();
if (geoidx > -1)
{
rowBuffer[geoidx] = geometry;
}
if (IDidx > -1)
{
rowBuffer[IDidx] = ID;
}
if (oldtfhidx > -1)
{
rowBuffer[oldtfhidx] = oldtfh;
}
featureLayer.CreateRow(rowBuffer);
rowBuffer.Dispose();
});
}
// 使用字符串字段插入新功能
public static void Insertstr(FeatureClass featureLayer, Geometry geometry, int geoidx, int tfhidx, string tfh)
{
QueuedTask.Run(() =>
{
RowBuffer rowBuffer = featureLayer.CreateRowBuffer();
if (geoidx > -1)
{
rowBuffer[geoidx] = geometry;
}
if (tfhidx > -1)
{
rowBuffer[tfhidx] = tfh;
}
featureLayer.CreateRow(rowBuffer);
});
}
// 根据提供的SQL where子句删除指定FeatureLayer中的功能
public static void Delete(FeatureLayer featureLayer, string wheresql)
{
QueryFilter queryFilter = new QueryFilter();
queryFilter.WhereClause = wheresql;
QueuedTask.Run(() => featureLayer.GetFeatureClass().DeleteRows(queryFilter));
}
// 使用EditOperation在FeatureLayer中插入新功能
public static void Insertop(FeatureLayer featureLayer, Geometry geometry)
{
QueuedTask.Run(() =>
{
var insp = new Inspector();
insp.LoadSchema(featureLayer);
var op = new EditOperation();
op.Create(featureLayer, geometry);
op.Execute();
});
}
// 使用提供的MapPoint在指定的FeatureLayer中构造点功能
public Task<bool> ConstructSamplePoints(FeatureLayer pointFeatureLayer, MapPoint newMapPoint)
{
return QueuedTask.Run(() =>
{
var createOperation = new EditOperation()
{
Name = "Generate points",
SelectNewFeatures = false
};
var featureClass = pointFeatureLayer.GetTable() as FeatureClass;
var classDefinition = featureClass.GetDefinition() as FeatureClassDefinition;
var spatialReference = classDefinition.GetSpatialReference();
createOperation.Create(pointFeatureLayer, newMapPoint);
return createOperation.ExecuteAsync();
});
}
}
}
ArcGIS Pro C#图形操作
最新推荐文章于 2024-09-02 19:43:41 发布