C# 地图开发:几何要素操作示例 (添加、查询、修改)
这是一系列的 C# 方法和事件处理程序,用于在地图上添加、查询和修改几何要素。
'private void addGeometryCollectionToolStripMenuItem_Click(object sender, EventArgs e)':当单击 addGeometryCollection 菜单项时触发。
- 'IGeometryCollection geometryCollection1 = new MultipointClass();':创建一个 IGeometryCollection 对象 geometryCollection1,用于存储几何要素集合。
- 'IGeometryCollection geometryCollection2 = new MultipointClass();':创建一个 IGeometryCollection 对象 geometryCollection2,用于存储几何要素集合。
- 'object missing = Type.Missing;':创建一个 missing 对象,用于传递缺失的参数。
- 'IPoint point;':定义一个 IPoint 对象。
- 'for (int i = 0; i < 10; i++)':循环 10 次。
- 'point = new PointClass();':创建一个新的 IPoint 对象。
- 'point.PutCoords(i * 2, i * 2);':设置 IPoint 对象的坐标。
- 'geometryCollection1.AddGeometry(point as IGeometry, ref missing, ref missing);':将 IPoint 对象添加到 geometryCollection1 中。
- 'geometryCollection2.AddGeometryCollection(geometryCollection1);':将 geometryCollection1 添加到 geometryCollection2 中。
- 'IMultipoint multipoint;':定义一个 IMultipoint 对象。
- 'multipoint = geometryCollection2 as IMultipoint;':将 geometryCollection2 转换为 IMultipoint 对象。
- 'addFeature("multipoint", multipoint as IGeometry);':调用 addFeature 方法向指定图层添加几何要素。
- 'this.axMapControl1.Extent = multipoint.Envelope;':设置地图控件的显示范围为 multipoint 的外包络。
- 'this.axMapControl1.Refresh();':刷新地图控件,以显示新添加的要素。
'private void insertGeometriesCollectionToolStripMenuItem_Click(object sender, EventArgs e)':当单击 insertGeometriesCollection 菜单项时触发。
- 'IGeometryCollection geometryCollection1 = new MultipointClass();':创建一个 IGeometryCollection 对象 geometryCollection1,用于存储几何要素集合。
- 'IGeometryCollection geometryCollection2 = new MultipointClass();':创建一个 IGeometryCollection 对象 geometryCollection2,用于存储几何要素集合。
- 'IGeometryCollection geometryCollection3 = new MultipointClass();':创建一个 IGeometryCollection 对象 geometryCollection3,用于存储几何要素集合。
- 'object missing = Type.Missing;':创建一个 missing 对象,用于传递缺失的参数。
- 'IPoint point;':定义一个 IPoint 对象。
- 'for (int i = 0; i < 10; i++)':循环 10 次。
- 'point = new PointClass();':创建一个新的 IPoint 对象。
- 'point.PutCoords(i * 2, i);':设置 IPoint 对象的坐标。
- 'geometryCollection1.AddGeometry(point as IGeometry, ref missing, ref missing);':将 IPoint 对象添加到 geometryCollection1 中。
- 'for (int i = 0; i < 10; i++)':循环 10 次。
- 'point = new PointClass();':创建一个新的 IPoint 对象。
- 'point.PutCoords(i, i);':设置 IPoint 对象的坐标。
- 'geometryCollection2.AddGeometry(point as IGeometry, ref missing, ref missing);':将 IPoint 对象添加到 geometryCollection2 中。
- 'for (int i = 0; i < 10; i++)':循环 10 次。
- 'point = new PointClass();':创建一个新的 IPoint 对象。
- 'point.PutCoords(i, i * 2);':设置 IPoint 对象的坐标。
- 'geometryCollection3.AddGeometry(point as IGeometry, ref missing, ref missing);':将 IPoint 对象添加到 geometryCollection3 中。
- 'geometryCollection1.InsertGeometryCollection(1, geometryCollection2);':在 geometryCollection1 中的索引 1 处插入 geometryCollection2。
- 'geometryCollection1.InsertGeometryCollection(1, geometryCollection3);':在 geometryCollection1 中的索引 1 处插入 geometryCollection3。
- 'IMultipoint multipoint;':定义一个 IMultipoint 对象。
- 'multipoint = geometryCollection1 as IMultipoint;':将 geometryCollection1 转换为 IMultipoint 对象。
- 'addFeature("multipoint", multipoint as IGeometry);':调用 addFeature 方法向指定图层添加几何要素。
- 'this.axMapControl1.Extent = multipoint.Envelope;':设置地图控件的显示范围为 multipoint 的外包络。
- 'this.axMapControl1.Refresh();':刷新地图控件,以显示新添加的要素。
'private void setGeometriesToolStripMenuItem_Click(object sender, EventArgs e)':当单击 setGeometriesToolStripMenuItem 菜单项时触发。
- 'IGeometryCollection geometryCollection1 = new MultipointClass();':创建一个 IGeometryCollection 对象 geometryCollection1,用于存储几何要素集合。
- 'IGeometry[] geometryArray = new IGeometry[10];':创建一个 IGeometry 数组 geometryArray,用于存储几何要素。
- 'object missing = Type.Missing;':创建一个 missing 对象,用于传递缺失的参数。
- 'IPoint point;':定义一个 IPoint 对象。
- 'for (int i = 0; i < 10; i++)':循环 10 次。
- 'point = new PointClass();':创建一个新的 IPoint 对象。
- 'point.PutCoords(i * 2, i * 2);':设置 IPoint 对象的坐标。
- 'geometryArray[i] = point as IGeometry;':将 IPoint 对象添加到 geometryArray 中。
- 'IGeometryBridge geometryBridge = new GeometryEnvironmentClass();':创建一个 IGeometryBridge 对象。
- 'geometryBridge.SetGeometries(geometryCollection1, ref geometryArray);':将 geometryArray 中的几何要素设置到 geometryCollection1 中。
- 'IMultipoint multipoint;':定义一个 IMultipoint 对象。
- 'multipoint = geometryCollection1 as IMultipoint;':将 geometryCollection1 转换为 IMultipoint 对象。
- 'addFeature("multipoint", multipoint as IGeometry);':调用 addFeature 方法向指定图层添加几何要素。
- 'this.axMapControl1.Extent = multipoint.Envelope;':设置地图控件的显示范围为 multipoint 的外包络。
- 'this.axMapControl1.Refresh();':刷新地图控件,以显示新添加的要素。
'private void addSegmentsToolStripMenuItem_Click(object sender, EventArgs e)':当单击 addSegmentsToolStripMenuItem 菜单项时触发。
- 'IPoint fromPoint = new PointClass();':创建一个起点 IPoint 对象。
- 'fromPoint.PutCoords(0, 0);':设置起点的坐标。
- 'IPoint toPoint = new PointClass();':创建一个终点 IPoint 对象。
- 'toPoint.PutCoords(10, 20);':设置终点的坐标。
- 'ILine line1 = new LineClass();':创建一个 ILine 对象。
- 'line1.PutCoords(fromPoint, toPoint);':设置 ILine 对象的起点和终点。
- 'ISegmentCollection segmentCollection_path = new PathClass();':创建一个 ISegmentCollection 对象 segmentCollection_path,用于存储路径。
- 'segmentCollection_path.AddSegment(line1 as ISegment, Type.Missing, Type.Missing);':将 ILine 对象添加到 segmentCollection_path 中。
- 'fromPoint.PutCoords(10, 20);':更新起点的坐标。
- 'toPoint.PutCoords(20, 10);':更新终点的坐标。
- 'ILine line2 = new LineClass();':创建一个 ILine 对象。
- 'line2.PutCoords(fromPoint, toPoint);':设置 ILine 对象的起点和终点。
- 'segmentCollection_path.AddSegment(line2 as ISegment, Type.Missing, Type.Missing);':将 ILine 对象添加到 segmentCollection_path 中。
- 'fromPoint.PutCoords(20, 10);':更新起点的坐标。
- 'toPoint.PutCoords(50, 20);':更新终点的坐标。
- 'ILine line3 = new LineClass();':创建一个 ILine 对象。
- 'line3.PutCoords(fromPoint, toPoint);':设置 ILine 对象的起点和终点。
- 'segmentCollection_path.AddSegment(line3 as ISegment, Type.Missing, Type.Missing);':将 ILine 对象添加到 segmentCollection_path 中。
- 'IGeometryCollection polyline = new PolylineClass();':创建一个 IGeometryCollection 对象 polyline,用于存储折线几何要素。
- 'polyline.AddGeometry(segmentCollection_path as IGeometry, Type.Missing, Type.Missing);':将 segmentCollection_path 添加到 polyline 中。
- 'addFeature("polyline", polyline as IGeometry);':调用 addFeature 方法向指定图层添加几何要素。
- 'IGeometry pl = polyline as IGeometry;':将 polyline 转换为 IGeometry 对象。
- 'this.axMapControl1.Extent = pl.Envelope;':设置地图控件的显示范围为 pl 的外包络。
- 'this.axMapControl1.Refresh();':刷新地图控件,以显示新添加的要素。
'private void querSegmentsToolStripMenuItem_Click(object sender, EventArgs e)':当单击 querSegmentsToolStripMenuItem 菜单项时触发。
- 'ISegment[] segmentArray = new ISegment[10];':创建一个 ISegment 数组 segmentArray,用于存储线段要素。
- 'for (int i = 0; i < 10; i++)':循环 10 次。
- 'ILine line = new LineClass();':创建一个 ILine 对象。
- 'IPoint fromPoint = new PointClass();':创建一个起点 IPoint 对象。
- 'fromPoint.PutCoords(i * 10, i * 10);':设置起点的坐标。
- 'IPoint toPoint = new PointClass();':创建一个终点 IPoint 对象。
- 'toPoint.PutCoords(i * 15, i * 15);':设置终点的坐标。
- 'line.PutCoords(fromPoint, toPoint);':设置 ILine 对象的起点和终点。
- 'segmentArray[i] = line as ISegment;':将 ILine 对象添加到 segmentArray 中。
- 'ISegmentCollection segmentCollection = new PolylineClass();':创建一个 ISegmentCollection 对象 segmentCollection,用于存储线段要素集合。
- 'IGeometryBridge geometryBridge = new GeometryEnvironmentClass();':创建一个 IGeometryBridge 对象。
- 'geometryBridge.AddSegments(segmentCollection, ref segmentArray);':将 segmentArray 中的线段要素添加到 segmentCollection 中。
- 'int index = 0;':定义一个整型变量 index,并初始化为 0。
- 'ISegment[] outputSegmentArray = new ISegment[segmentCollection.SegmentCount - index];':创建一个 ISegment 数组 outputSegmentArray,用于存储输出的线段要素。
- 'for (int i = 0; i < outputSegmentArray.Length; i++)':循环遍历 outputSegmentArray 的长度。
- 'outputSegmentArray[i] = new LineClass();':创建一个新的 ILine 对象,并将其赋值给 outputSegmentArray[i]。
- 'geometryBridge.QuerySegments(segmentCollection, index, ref outputSegmentArray);':查询 segmentCollection 中的线段要素并将结果存储在 outputSegmentArray 中。
- 'String report = "";':创建一个字符串变量 report,用于存储查询结果的报告。
- 'for (int i = 0; i < outputSegmentArray.Length; i++)':循环遍历 outputSegmentArray。
- 'ISegment currentSegment = outputSegmentArray[i];':获取 outputSegmentArray 中的当前线段要素。
- 'ILine currentLine = currentSegment as ILine;':将 currentSegment 转换为 ILine 对象。
- 'report = report + "index = " + i + " , FromPoint X = " + currentLine.FromPoint.X + " , FromPoint Y = " + currentLine.FromPoint.Y + " , ToPoint X = " + currentLine.ToPoint.X + " , ToPoint Y = " + currentLine.ToPoint.Y + "\n";':将查询结果拼接到 report 字符串中。
- 'System.Windows.Forms.MessageBox.Show(report);':显示一个消息框,展示查询结果。
'private void setSegmentsToolStripMenuItem_Click(object sender, EventArgs e)':当单击 setSegmentsToolStripMenuItem 菜单项时触发。
- 'IPolyline polyline = new PolylineClass();':创建一个 IPolyline 对象 polyline,用于存储折线几何要素。
- 'ISegment[] insertSegmentArray = new ISegment[5];':创建一个 ISegment 数组 insertSegmentArray,用于存储要插入的线段要素。
- 'for (int i = 0; i < 5; i++)':循环 5 次。
- 'ILine insertLine = new LineClass();':创建一个 ILine 对象。
- 'IPoint insertFromPoint = new PointClass();':创建一个起点 IPoint 对象。
- 'insertFromPoint.PutCoords(i, 1);':设置起点的坐标。
- 'IPoint insertToPoint = new PointClass();':创建一个终点 IPoint 对象。
- 'insertToPoint.PutCoords(i * 10, 1);':设置终点的坐标。
- 'insertLine.PutCoords(insertFromPoint, insertToPoint);':设置 ILine 对象的起点和终点。
- 'insertSegmentArray[i] = insertLine as ISegment;':将 ILine 对象添加到 insertSegmentArray 中。
- 'ISegmentCollection segmentCollection = new PolylineClass();':创建一个 ISegmentCollection 对象 segmentCollection,用于存储线段要素集合。
- 'IGeometryBridge geometryBridge = new GeometryEnvironmentClass();':创建一个 IGeometryBridge 对象。
- 'geometryBridge.SetSegments(segmentCollection, ref insertSegmentArray);':将 insertSegmentArray 中的线段要素设置到 segmentCollection 中。
- 'polyline = segmentCollection as IPolyline;':将 segmentCollection 转换为 IPolyline 对象。
- 'addFeature("polyline", polyline as IGeometry);':调用 addFeature 方法向指定图层添加几何要素。
- 'this.axMapControl1.Extent = polyline.Envelope;':设置地图控件的显示范围为 polyline 的外包络。
- 'this.axMapControl1.Refresh();':刷新地图控件,以显示新添加的要素。
'private void addPointCollectionToolStripMenuItem_Click(object sender, EventArgs e)':当单击 addPointCollectionToolStripMenuItem 菜单项时触发。
- 'IPointCollection5 pointCollection1 = new MultipointClass();':创建一个 IPointCollection5 对象 pointCollection1,用于存储点要素集合。
- 'IPoint[] points = new PointClass[10];':创建一个 IPoint 数组 points,用于存储点要素。
- 'IMultipoint multipoint;':定义一个 IMultipoint 对象。
- 'object missing = Type.Missing;':创建一个 missing 对象,用于传递缺失的参数。
- 'IPoint point;':定义一个 IPoint 对象。
- 'for (int i = 0; i < 10; i++)':循环 10 次。
- 'point = new PointClass();':创建一个新的 IPoint 对象。
- 'point.PutCoords(i * 5, i);':设置 IPoint 对象的坐标。
- 'points[i] = point;':将 IPoint 对象添加到 points 中。
- 'IGeometryBridge geometryBridge = new GeometryEnvironmentClass();':创建一个 IGeometryBridge 对象。
- 'geometryBridge.SetPoints(pointCollection1, ref points);':将 points 中的点要素设置到 pointCollection1 中。
- 'IPointCollection5 pointCollection2 = new MultipointClass();':创建一个 IPointCollection5 对象 pointCollection2,用于存储点要素集合。
- 'pointCollection2.AddPointCollection(pointCollection1);':将 pointCollection1 中的点要素添加到 pointCollection2 中。
- 'multipoint = pointCollection1 as IMultipoint;':将 pointCollection1 转换为 IMultipoint 对象。
- 'addFeature("multipoint", multipoint as IGeometry);':调用 addFeature 方法向指定图层添加几何要素。
- 'this.axMapControl1.Extent = multipoint.Envelope;':设置地图控件的显示范围为 multipoint 的外包络。
- 'this.axMapControl1.Refresh();':刷新地图控件,以显示新添加的要素。
'private void queryPointsToolStripMenuItem_Click(object sender, EventArgs e)':当单击 queryPointsToolStripMenuItem 菜单项时触发。
- 'IPoint point1 = new PointClass();':创建一个 IPoint 对象 point1。
- 'point1.PutCoords(10, 10);':设置 point1 的坐标。
- 'IPoint point2 = new PointClass();':创建一个 IPoint 对象 point2。
- 'point2.PutCoords(20, 20);':设置 point2 的坐标。
- 'IPoint[] inputPointArray = new IPoint[2];':创建一个 IPoint 数组 inputPointArray,用于存储输入的点要素。
- 'inputPointArray[0] = point1;':将 point1 添加到 inputPointArray 中。
- 'inputPointArray[1] = point2;':将 point2 添加到 inputPointArray 中。
- 'IPointCollection4 pointCollection = new MultipointClass();':创建一个 IPointCollection4 对象 pointCollection,用于存储点要素集合。
- 'IGeometryBridge geometryBridge = new GeometryEnvironmentClass();':创建一个 IGeometryBridge 对象。
- 'geometryBridge.AddPoints(pointCollection, ref inputPointArray);':将 inputPointArray 中的点要素添加到 pointCollection 中。
- 'IPoint[] outputPointArray = new IPoint[2];':创建一个 IPoint 数组 outputPointArray,用于存储输出的点要素。
- 'for (int i = 0; i < outputPointArray.Length; i++)':循环遍历 outputPointArray 的长度。
- 'outputPointArray[i] = new PointClass();':创建一个新的 IPoint 对象,并将其赋值给 outputPointArray[i]。
- 'pointCollection.QueryPoint(0, outputPointArray[0]);':查询 pointCollection 中的点要素,并将结果存储在 outputPointArray 中。
- 'for (int i = 0; i < outputPointArray.Length; i++)':循环遍历 outputPointArray。
- 'IPoint currentPoint = outputPointArray[i];':获取 outputPointArray 中的当前点要素。
- 'if (currentPoint.IsEmpty == true)':如果当前点为空。
- 'System.Windows.Forms.MessageBox.Show("Current point = null");':显示一个消息框,提示当前点为空。
- 'else':否则。
- 'System.Windows.Forms.MessageBox.Show("X = " + currentPoint.X + ", Y = " + currentPoint.Y);':显示一个消息框,展示当前点的坐标。
'private void updatePointToolStripMenuItem_Click(object sender, EventArgs e)':当单击 updatePointToolStripMenuItem 菜单项时触发。
- 'IPoint point1 = new PointClass();':创建一个 IPoint 对象 point1。
- 'point1.PutCoords(10, 10);':设置 point1 的坐标。
- 'IPoint point2 = new PointClass();':创建一个 IPoint 对象 point2。
- 'point2.PutCoords(20, 20);':设置 point2 的坐标。
- 'IPointCollection pointCollection = new MultipointClass();':创建一个 IPointCollection 对象 pointCollection,用于存储点要素集合。
- 'object missing = Type.Missing;':创建一个 missing 对象,用于传递缺失的参数。
- 'pointCollection.AddPoint(point1, ref missing, ref missing);':向 pointCollection 中添加 point1。
- 'pointCollection.AddPoint(point2, ref missing, ref missing);':向 pointCollection 中添加 point2。
- 'point1 = new PointClass();':重新创建一个 IPoint 对象 point1。
- 'point1.PutCoords(40, 10);':更新 point1 的坐标。
- 'pointCollection.UpdatePoint(1, point1);':更新 pointCollection 中索引为 1 的点为 point1。
- 'System.Windows.Forms.MessageBox.Show("X = " + pointCollection.get_Point(0).X + ", Y = " + pointCollection.get_Point(0).Y);':显示一个消息框,展示 pointCollection 中第一个点的坐标。
'private void createPolygonToolStripMenuItem_Click(object sender, EventArgs e)':当单击 createPolygonToolStripMenuItem 菜单项时触发。
- 'IPoint point1 = new PointClass();':创建一个 IPoint 对象 point1。
- 'point1.PutCoords(10, 20);':设置 point1 的坐标。
- 'IPoint point2 = new PointClass();':创建一个 IPoint 对象 point2。
- 'point2.PutCoords(40, 50);':设置 point2 的坐标。
- 'IPoint point3 = new PointClass();':创建一个 IPoint 对象 point3。
- 'point3.PutCoords(60, 40);':设置 point3 的坐标。
- 'IPoint point4 = new PointClass();':创建一个 IPoint 对象 point4。
- 'point4.PutCoords(50, 10);':设置 point4 的坐标。
- 'ILine pLine = new LineClass();':创建一个 ILine 对象 pLine。
- 'ISegmentCollection pPath = new PathClass();':创建一个 ISegmentCollection 对象 pPath,用于存储路径。
- 'object o = Type.Missing;':创建一个 missing 对象,用于传递缺失的参数。
- 'pLine.PutCoords(point1, point2);':设置 pLine 的起点和终点。
- 'pPath.AddSegment(pLine as ISegment, ref o, ref o);':将 pLine 添加到 pPath 中。
- 'pLine = new LineClass();':重新创建一个 ILine 对象 pLine。
- 'pLine.PutCoords(point2, point3);':设置 pLine 的起点和终点。
- 'pPath.AddSegment(pLine as ISegment, ref o, ref o);':将 pLine 添加到 pPath 中。
- 'pLine = new LineClass();':重新创建一个 ILine 对象 pLine。
- 'pLine.PutCoords(point3, point4);':设置 pLine 的起点和终点。
- 'pPath.AddSegment(pLine as ISegment, ref o, ref o);':将 pLine 添加到 pPath 中。
- 'ISegmentCollection pRing = new RingClass();':创建一个 ISegmentCollection 对象 pRing,用于存储环。
- 'for (int i = 0; i < pPath.SegmentCount; i++)':循环遍历 pPath 的线段要素数量。
- 'pRing.AddSegment(pPath.get_Segment(i) as ISegment);':将 pPath 中的线段要素添加到 pRing 中。
- 'IRing pRing2 = pRing as IRing;':将 pRing 转换为 IRing 对象。
- 'pRing2.Close();':关闭 pRing2,使其成为封闭环。
- 'IGeometryCollection pPolygon = new PolygonClass();':创建一个 IGeometryCollection 对象 pPolygon,用于存储多边形几何要素。
- 'pPolygon.AddGeometry(pRing2 as IGeometry, ref o, ref o);':将 pRing2 添加到 pPolygon 中。
- 'IGeometry polygon = new PolygonClass();':创建一个 IGeometry 对象 polygon。
- 'polygon = pPolygon as IGeometry;':将 pPolygon 转换为 IGeometry 对象。
- 'addFeature("polygon", polygon);':调用 addFeature 方法向指定图层添加几何要素。
- 'this.axMapControl1.Extent = polygon.Envelope;':设置地图控件的显示范围为 polygon 的外包络。
- 'this.axMapControl1.Refresh();':刷新地图控件,以显示新添加的要素。
原文地址: https://www.cveoy.top/t/topic/bezM 著作权归作者所有。请勿转载和采集!