实验指导书图元编辑1.docx
- 文档编号:26403024
- 上传时间:2023-06-19
- 格式:DOCX
- 页数:28
- 大小:33.25KB
实验指导书图元编辑1.docx
《实验指导书图元编辑1.docx》由会员分享,可在线阅读,更多相关《实验指导书图元编辑1.docx(28页珍藏版)》请在冰豆网上搜索。
实验指导书图元编辑1
1图元编辑
1.1如何得到图形的基本属性
本例要实现的功能是得到一个FeatureLayer中被选择的Feature的基本图形属性,如,图形的维数,类型,范围,空间坐标系统等。
●要点
接口IGeometry的主要属性有Dimension(维数),GeometryType(图形类型),Envelope(范围),IsEmpty(是否为空),SpatialReference(空间坐标系)等。
●程序说明
该过程在开始处使用IEnumFeature接口来得到所选择的Features,用Next方法取得每个Feature。
然后利用IFeature接口的Shape属性得到Geometry。
最后弹出消息框显示图形的属性信息。
●代码
PublicSubGetGeometryProperty()
DimpMxDocumentAsIMxDocument
DimpEnumFeatureAsIEnumFeature
DimpFeatureAsIFeature
DimpGeometryAsIGeometry
OnErrorGoToErrorHandler
SetpMxDocument=Application.Document
'得到图形集
SetpEnumFeature=pMxDocument.FocusMap.FeatureSelection
'重新设置图形集
pEnumFeature.Reset
'得到第一个图形
SetpFeature=pEnumFeature.Next
'判断是否有图形被选上
IfpFeatureIsNothingThen
MsgBox"noselection,pleaseselectaFeature"
Else
‘循环图形,直到最后
WhileNotpFeatureIsNothing
SetpGeometry=pFeature.Shape
'得到图形的基本属性
MsgBox"+++Polygon:
:
IGeometryproperties..."&vbCrLf_
&"Dimension="&pGeometry.Dimension&vbCrLf_
&"Geometrytype="&pGeometry.GeometryType&vbCrLf_
&"Envelope="&pGeometry.Envelope.XMin&","&pGeometry.Envelope.YMin&","_
&pGeometry.Envelope.XMax&","&pGeometry.Envelope.YMin&vbCrLf_
&"IsEmpty="&pGeometry.IsEmpty&vbCrLf_
&"SpatialReference="&pGeometry.SpatialReference.Name
‘指向下一个图形
SetpFeature=pEnumFeature.Next
Wend
EndIf
ExitSub
ErrorHandler:
MsgBoxErr.Description
EndSub
1.2如何将选中的点集转换成Polygon
本例要实现的功能是根据选中的Points创建一个Polygon,并且保存到Polygon类型的FeatureLayer中,要求被选择的Points最少为3个。
●要点
根据选择的点创建一个Polygon,首先要判断生成的Polygon是否是Simple,这里用到接口ITopologicalOperator2的属性IsSimple。
如果不是,则要对做Polygon排序等处理。
此外还用到了接口IPointCollection的方法ReplacePoints,进行点的交换。
将排好序的点,按顺序创建Segment,运用实例化为Ring的ISegmentCollection接口方法AddSegment增加Segment。
实例化为Polygon的IGeometryCollection接口方法AddGeometry增加Ring。
这样,通过上面的方法便可以创建Polygon。
●程序说明
根据接口ITopologicalOperator2.IsSimple属性判断Polygon是否Simple。
如果返回为False,就对Polygon上的点进行排序等处理,排好序后,找出X方向上值最大和最小的点,由这两点创建一条直线,将所有点分成在直线左边和右边两部分。
●代码
PublicSubConvertPointToPolygon()
DimpMxDocAsIMxDocument
DimpMapAsIMap
DimpEnumFeatureAsIEnumFeature
DimpMultiPointAsIPointCollection
DimpMultiPointSortedAsIPointCollection
DimpFeatureAsIFeature
DimpPointiAsIPoint
DimpTopoOpAsITopologicalOperator2
DimpLineAsILine
DimpGonCollAsIPointCollection
DimpCloneiAsIClone
DimptMinAsIPoint
DimptMaxAsIPoint
DimpBaseLineAsILine
DimpBaseCurveAsICurve
DimpOutpointAsIPoint
DimpMultiRightAsIPointCollection
DimpMultiLeftAsIPointCollection
DimpGonColl2AsIGeometryCollection
DimpPolygonAsIPolygon
DimpRingAsIRing
DimpFeatureClassAsIFeatureClass
DimpFeatureLayerAsIfeatureLayer
DimpFeature1AsIFeature
DimpFeatureClass1AsIFeatureClass
DimpFeatureLayer1AsIFeatureLayer
DimpDataSetAsIDataset
DimpWorkspaceFactoryAsIWorkspaceFactory
DimpWorkspaceEditAsIWorkspaceEdit
DimpRingCollAsISegmentCollection
DimdDistAlongAsDouble
DimdDistFromAsDouble
DimbIsRightAsBoolean
DimiAsLong
DimjAsLong
DimlFlagAsLong
OnErrorGoToerrorHander
SetpMxDoc=ThisDocument
SetpMap=pMxDoc.FocusMap
SetpActiveView=pMap
SetpFeatureLayer=pMap.Layer(0)
SetpFeatureClass=pFeatureLayer.FeatureClass
'创建一个工作区,开始编辑
SetpDataSet=pFeatureClass
SetpWorkspaceFactory=NewShapefileWorkspaceFactory
SetpWorkspaceEdit=pWorkspaceFactory.OpenFromFile(pDataSet.Workspace.PathName,0)
pWorkspaceEdit.StartEditOperation
pWorkspaceEdit.StartEditingTrue
SetpMultiLeft=NewMultipoint
SetpMultiRight=NewMultipoint
SetpGonColl=NewPolygon
SetpMultiPoint=NewMultipoint
SetpMultiPointSorted=NewMultipoint
'得到所选择的图形集
SetpEnumFeature=pMxDoc.FocusMap.FeatureSelection
SetpFeature=pEnumFeature.Next
'增加点到MultiPoint
WhileNotpFeatureIsNothing
IfpFeature.ShapeCopy.GeometryType=esriGeometryPointThen
pMultiPoint.AddPointpFeature.ShapeCopy
ElseIfpFeature.ShapeCopy.GeometryType=esriGeometryMultipointThen
pMultiPoint.AddPointCollectionpFeature.ShapeCopy
EndIf
SetpFeature=pEnumFeature.Next
Wend
IfpMultiPoint.PointCount<3Then
MsgBox"Selectaleast3points!
"
ExitSub
EndIf
'创建第一个Polygon
pGonColl.AddPointCollectionpMultiPoint
SetpTopoOp=pGonColl
'将Polygon是否是Simple设置成未知
pTopoOp.IsKnownSimple=False
'经判断,如果不是Simple,则经过以下处理,将其转换为Simple
IfpTopoOp.IsSimple=FalseandpMultiPoint.PointCount>3Then
lFlag=1
SetpTopoOp=pMultiPoint
pTopoOp.IsKnownSimple=False
pTopoOp.Simplify
'将Multipoint进行排序
Fori=0TopMultiPoint.PointCount-1
Forj=i+1TopMultiPoint.PointCount-1
IfpMultiPoint.Point(j).x SetpClonei=pMultiPoint.Point(i) SetpPointi=pClonei.Clone '交换两点 pMultiPoint.ReplacePointsi,1,1,pMultiPoint.Point(j) pMultiPoint.ReplacePointsj,1,1,pPointi EndIf Next Next SetptMin=NewPoint SetptMax=NewPoint '找出MultiPoint中的最大和最小点 pMultiPoint.QueryPoint0,ptMin pMultiPoint.QueryPointpMultiPoint.PointCount-1,ptMax '创建一条线段 SetpBaseLine=NewLine pBaseLine.PutCoordsptMin,ptMax SetpBaseCurve=pBaseLine Fori=0TopMultiPoint.PointCount-1 SetpOutpoint=NewPoint pBaseCurve.QueryPointAndDistanceesriNoExtension,pMultiPoint.Point(i),False,pOutpoint,_dDistAlong,dDistFrom,bIsRight IfbIsRightThen pMultiRight.AddPointpMultiPoint.Point(i) Else pMultiLeft.AddPointpMultiPoint.Point(i) EndIf Next SetpRingColl=NewRing '将左边的线添加到Ring Fori=0TopMultiLeft.PointCount-2 SetpLine=NewLine pLine.PutCoordspMultiLeft.Point(i),pMultiLeft.Point(i+1) pRingColl.AddSegmentpLine Next '第一条线 SetpLine=NewLine pLine.PutCoordspMultiLeft.Point(pMultiLeft.PointCount-1),pMultiRight.Point(0) pRingColl.AddSegmentpLine '将右边的先添加到Ring Fori=(pMultiRight.PointCount-1)To1Step-1 SetpLine=NewLine pLine.PutCoordspMultiRight.Point(i),pMultiRight.Point(i-1) pRingColl.AddSegmentpLine Next '最后一条线 SetpLine=NewLine pLine.PutCoordspMultiRight.Point(0),pMultiLeft.Point(0) pRingColl.AddSegmentpLine SetpRing=pRingColl pRing.Close SetpGonColl2=NewPolygon pGonColl2.AddGeometrypRing EndIf IflFlag=0Then SetpPolygon=pGonColl Else SetpPolygon=pGonColl2'QI EndIf '画出Polygon SetpFeatureLayer1=pMap.Layer (1) SetpFeatureClass1=pFeatureLayer1.FeatureClass SetpFeature1=pFeatureClass1.CreateFeature '把画的Polygon加到新建的Feature上 SetpFeature1.Shape=pPolygon '保存Feature pFeature1.Store pMxDoc.ActiveView.Refresh '停止编辑 pWorkspaceEdit.StopEditOperation pWorkspaceEdit.StopEditingTrue ExitSub ErrorHander: pWorkspaceEdit.AbortEditOperation MsgBoxErr.Description EndSub 1.3如何将Multipoint转换成Points 本例要实现的功能是根据一个FeatureLayer中被选择一个或多个MultiPoint,生成多个Point并把这些新生成的Point保存在一个Point类型的FeatureLayer上。 ●要点 本例将选择的Multipoints上的每个点都生成一个对应得Point,并用一个接口IPointCollection的变量来接收。 利用IPointCollection的方法point(index),取出新生成的每个点,用来创建Point类型的Feature。 ●程序说明 本例要求在ArcMap中添加两个层,最上面的是层Multipoint,下面是层wind。 根据循环得到选择的每个Multipoint的每个点,为wind层生成新的Feature并保存 ●代码 SubconvertMultipointToPoints() DimpMxDocumentAsIMxDocument DimpMapAsIMap DimpActiveViewAsIActiveView DimpEnumFeatureAsIEnumFeature DimpFeature0AsIFeature DimpFeatureLayer0AsIFeatureLayer DimpFeatureClass0AsIFeatureClass DimpFeature1AsIFeature DimpFeatureLayer1AsIFeatureLayer DimpFeatureClass1AsIFeatureClass DimpPointCollectionAsIPointCollection DimpDataSetAsIDataset DimpWorkspaceFactoryAsIWorkspaceFactory DimpWorkspaceEditAsIWorkspaceEdit DimlPointIndexAsLong DimlPointFieldIndexAsLong OnErrorGoToErrorHanlder '得到当前层 SetpMxDocument=ThisDocument SetpMap=pMxDocument.FocusMap SetpActiveView=pMap '得到0层和1层的FeatureClass SetpFeatureLayer0=pMxDocument.FocusMap.Layer(0) SetpFeatureClass0=pFeatureLayer0.FeatureClass SetpFeatureLayer1=pMxDocument.FocusMap.Layer (1) SetpFeatureClass1=pFeatureLayer1.FeatureClass '建立编辑工作区 SetpDataSet=pFeatureClass1 SetpWorkspaceFactory=NewShapefileWorkspaceFactory SetpWorkspaceEdit=pWorkspaceFactory.OpenFromFile(pDataSet.Workspace.PathName,0) pWorkspaceEdit.StartEditOperation pWorkspaceEdit.StartEditingTrue '得到Feature SetpEnumFeature=pMxDocument.FocusMap.FeatureSelection SetpFeature0=pEnumFeature.Next IfpFeature0IsNothingThen MsgBox"MusthaveSelectinPosition0" ExitSub EndIf '循环,通过每个MultiPoint,在1图层上,生成以每个点为特征的Points WhileNotpFeature0IsNothing IfpFeature0.ShapeCopy.GeometryType=esriGeometryMultipointThen SetpPointCollection=pFeature0.ShapeCopy FornPointIndex=0TopPointCollection.PointCount-1 SetpFeature1=pFeatureClass1.CreateFeature '在pFeature1上生成Point SetpFeature1.Shape=pPointCollection.Point(nPointIndex) '如果两Feature的FieldCount相同,赋每个Field的值,ID, 'TypeGeometry的Field除外 IfpFeature1.Fields.FieldCount=pFeature0.Fields.FieldCountThen ForlPointFieldIndex=0TopFeature1.Fields.FieldCount-1 IfNotpFeature1.Fields.Field(lPointFieldIndex).Type=_ esriFieldTypeGeometryAndNotpFeature1.Fields._ Field(lPointFieldIndex).Type=esriFieldTypeOIDThen pFeature1.Value(lPointFieldIndex)=_ pFeature0.Value(lPointFieldIndex) EndIf Next EndIf '保存Feature pFeature1.Store Next Else MsgBox"MusthaveMultipointinposition0" ExitSub EndIf SetpFeature0=pEnumFeature.Next Wend '停止编辑 pWorkspaceEdit.StopEditOperation pWorkspaceEdit.StopEditingTrue ExitSub ErrorHanlder: pWorkspaceEdit.AbortEditOperation MsgBoxErr.Description EndSub 1.4如何通过Polygon中的多个Ring创建多个Polygon 本例要实现的是如何在一个FeatureLayer中,选择Polygon(Feature)的Shape,如果它有多个Ring
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 实验 指导书 编辑
![提示](https://static.bdocx.com/images/bang_tan.gif)