1、ArcEngine 中 Geometry对象浅析本帖最后由 shisanshao 于 2011-4-13 00:12 编辑 ArcEngine Geometry库定义了基本几何图形的矢量表达形式,顶级的几何图形有Points、Multipoints 、Polylines、Polygons、 Multipatches,Geodatabase 和绘图系统使用这些几何图形来定义其他各种形状的特征和图形,提供了编辑图形的操作方法和地图符号系统符号化特征数据的途径。 Geometry 库中几个核心类和接口构成了 Geometry对象的基本框架。 GeometryEnvironment 提供了从不同的输入
2、、设置或获取全局变量来创建几何图形的方法,以便控制 geometry方法的行为。GeometryEnvironment 对象是一个单例对象。以下为引用的内容: 1. public IPolyline TestGeometryEnvironment()2. 3. ISpatialReferenceFactory spatialReferenceFactory = new SpatialReferenceEnvironmentClass();4. /Create a projected coordinate system and define its domain, resolution, and
3、 x,y tolerance.5. IspatialReferenceResolution spatialReferenceResolution = spatialReferenceFactory.CreateProjectedCoordinateSystem(int)esriSRProjCSType.esriSRProjCS_NAD1983UTM_11N) as ISpatialReferenceResolution;6. spatialReferenceResolution.ConstructFromHorizon();7. ISpatialReferenceTolerance spati
4、alReferenceTolerance = spatialReferenceResolution as ISpatialReferenceTolerance;8. spatialReferenceTolerance.SetDefaultXYTolerance();9. ISpatialReference spatialReference = spatialReferenceResolution as ISpatialReference;10. C11. /Create an array of WKSPoint structures starting in the middle of the
5、x,y domain of the 12. /projected coordinate system.13. double xMin;14. double xMax; 15. double yMin;16. double yMax;17. spatialReference.GetDomain(out xMin, out xMax, out yMin, out yMax);18. double xFactor = (xMin xMax) * 0.5;19. double yFactor = (yMin yMax) * 0.5;20. WKSPoint wksPoints = new WKSPoi
6、nt10;21. for ( int i = 0; i wksPoints.Length; i) 22. 23. wksPoints.X = xFactor i;24. wksPoints.Y = yFactor i;25. 26. IPointCollection4 pointCollection = new PolylineClass(); 27. IGeometryBridge2 geometryBridge = new GeometryEnvironmentClass();28. geometryBridge.AddWKSPoints(pointCollection, ref wksP
7、oints);29. IPolyline polyline = pointCollection as IPolyline;30. polyline.SpatialReference = spatialReference;31. return polyline;32. 复制代码new GeometryEnvironmentClass 仅仅是创建了一个指向已存在的GeometryEnvironmentClass 的引用。注意 IGeometryBridge2 接口的使用,addWKSPoints 方法将 WKSPoint 二维点添加到 PointCollection 中,用于构建 path、rin
8、g 、polyline、polygon ,或增加新点到Multipoint、TriangleFan、 TriangleStrip。在 Geometry 库中,除了IGeometryBridge2 还有 IGeometryBridge 接口,后者继承了前者,增加了一些编辑功能(添加点、插入点、重置点、分段等)。 GeometryBag GeometryBag 是支持 IGeometry 接口的几何对象引用的集合,任何几何对象都可以通过 IGeometryCollection 接口添加到 GeometryBag 中,但是在使用拓扑操作的时候,需要注意不同类型的几何类型可能会有相互不兼容的情况。在向
9、 GeometryBag 中添加几何对象的时候,GeometryBag 对象需要指定空间参考,添加到其中的几何对象均拥有和 GeometryBag 对象一样的空间参考。以下为引用的内容:1.2. private IPolygon GeometryBag_Example(IFeatureClass featureClass)3. 4. /Check input objects.5. if (featureClass = null)6. 7. return null;8. 9. IGeoDataset geoDataset = featureClass as IGeoDataset;10. ISp
10、atialFilter queryFilter = new SpatialFilterClass(); 11. /Set the properties of the spatial filter here.12. IGeometry geometryBag = new GeometryBagClass();13. /Define the spatial reference of the bag before adding geometries to it. 14. geometryBag.SpatialReference = geoDataset.SpatialReference;15. /U
11、se a nonrecycling cursor so each returned geometry is a separate object. 16. IFeatureCursor featureCursor = featureClass.Search(queryFilter, false); 17. IGeometryCollection geometryCollection = geometryBag as IGeometryCollection;18. IFeature currentFeature = featureCursor.NextFeature();19. while (cu
12、rrentFeature != null) 20. 21. /Add a reference to this features geometry into the bag.22. /You dont specify the before or after geometry (missing),23. /so the currentFeature.Shape IGeometry is added to the end of the geometryCollection.24. object missing = Type.Missing; 25. geometryCollection.AddGeo
13、metry(currentFeature.Shape , ref missing, ref missing);26. currentFeature = featureCursor.NextFeature();27. 28. / Create the polygon that will be the union of the features returned from the search cursor. 29. / The spatial reference of this feature does not need to be set ahead of time. The 30. / Co
14、nstructUnion method defines the constructed polygons spatial reference to be the same as 31. / the input geometry bag.32. ITopologicalOperator unionedPolygon = new PolygonClass(); 33. unionedPolygon.ConstructUnion(geometryBag as IEnumGeometry);34. return unionedPolygon as IPolygon;35. 复制代码Points 一个点
15、包括 X、Y 坐标,同时可以增加 M、Z 值及 ID 属性来扩展点的功能。Multipoints 点的集合,多点组成 Multipoint 几何类型,使用 multipoint 对象实现了的 IPointCollection 接口可以访问所有的点元素,这些点同样可以拥有M、Z 值及 ID 属性来获得更多的地理空间内涵。下面列举一个例子,通过一个已知的 polyline 来定义一个新的 multipart polyline。以下为引用的内容:1.2. public IPolyline ConstructMultiPartPolyline( IPolyline inputPolyline)3. 4
16、. IGeometry outGeometry = new PolylineClass();5. /Always associate new, toplevel geometries with an appropriate spatial reference.6. outGeometry.SpatialReference = inputPolyline.SpatialReference; 7. IGeometryCollection geometryCollection = outGeometry as IGeometryCollection;8. ISegmentCollection seg
17、mentCollection = inputPolyline as ISegmentCollection;9. /Iterate over existing polyline segments using a segment enumerator. Chinazcom 10. IEnumSegment segments = segmentCollection.EnumSegments;11. ISegment currentSegment;12. int partIndex = 0;13. int segmentIndex = 0;14. segments.Next(out currentSe
18、gment,ref partIndex, ref segmentIndex);15. while(currentSegment != null)16. 17. ILine normal = new LineClass();18. /Geometry methods with _Query_ in their name expect to modify existing geometries. 19. /In this case, the QueryNormal method modifies an existing line20. /segment (normal ) to be the no
19、rmal vector to 21. /currentSegment at the specified location along currentSegment.22. currentSegment.QueryNormal(esriSegmentExtension.esriNoExtension, 0.5, true, currentSegment.Length / 3, normal); 23. /Since each normal vector is not connected to others, create a new path for each one. 24. ISegment
20、Collection newPath = new PathClass();25. object missing = Type.Missing;26. newPath.AddSegment(normal as ISegment, ref missing, ref missing); 27. /The spatial reference associated with geometryCollection will be assigned to all incoming paths and segments.28. geometryCollection.AddGeometry(newPath as
21、 IGeometry, ref missing, ref missing);29. segments.Next(out currentSegment,ref partIndex, ref segmentIndex); 30. 31. /The geometryCollection now contains the new, multipart polyline.32. return geometryCollection as IPolyline;33. 复制代码ISegment 接口的 QueryNormal 方法用来在弧段上的某一点生成该弧段的法线,指定其长度,这样就生成了新的 segmen
22、t,并且多个 path 添加到geometryCollection 中,以 IPolyline 的形式返回。 Polylines Polylines 是有序 path 组成的集合,可以拥有 M、Z 和 ID 属性值。Polyline 对象的 IPointCollection 接口包含了所有节点的复制,IGeometryCollection 接口可以获取 polyline 的 paths,ISegmentCollection 接口可以获取 polyline 的 segments。 Polyline 结构图 Polygons Polygon 是一系列 rings 组成的集合,可以拥有 M、Z 和
23、ID 属性值。每一个 ring 由一个或多个 segment 组成,Polygon 或 ring 对象的 IPointCollection接口包含了所有节点的复制,IGeometryCollection 接口可以获取 polygon 的rings, ISegmentCollection 接口可以获取 polygon 的 segments。 Polygon 结构图 Multipatch Multipatch 用于描述 3D 面状几何类型,由一系列的矢量三角形构成,如果其中的 part 是一个 ring,那么它必须是封闭的,第一个节点和最后一个节点相同,另外每个 part 所包含节点的顺序非常重要
24、,Inner Rings 在 Outer Rings之后,代表单个表面 patch 的一系列 rings 必须由第一个 ring 开始。在 9.0 以后的开发包中,使用 IGeneralMultiPatchCreator 创建新的Multipatch, IGeometryMaterial 进行材质贴图。以下为引用的内容:1.2. public IMultiPatch CreateMultipatch()3. 4. /Prepare the geometry material list.5. IGeometryMaterial texture = new GeometryMaterialClas
25、s();6. texture.TextureImage = “C:TempMyImage.bmp“;7. IGeometryMaterialList materialList = new GeometryMaterialListClass();8. materialList.AddMaterial(texture);9. /Create the multipatch.10. IGeneralMultiPatchCreator multiPatchCreator = new GeneralMultiPatchCreatorClass(); 11. multiPatchCreator.Init(4
26、, 1, false, false, false, 4, materialList);12. /Set up part.13. /Could also use a Ring or a TriangleFan.14. multiPatchCreator.SetPatchType(0, esriPatchType.esriPatchTypeTriangleStrip);15. multiPatchCreator.SetMaterialIndex(0, 0);16. multiPatchCreator.SetPatchPointIndex(0, 0);17. multiPatchCreator.Se
27、tPatchTexturePointIndex(0, 0); 18. /Set realworld points.19. WKSPointZ upperLeft20. = new WKSPointZ();21. WKSPointZ lowerLeft22. = new WKSPointZ();23. WKSPointZ upperRight = new WKSPointZ(); 24. WKSPointZ lowerRight = new WKSPointZ();25. upperLeft.X = 0;26. upperLeft.Y = 0;27. upperLeft.Z = 0; 28. u
28、pperRight.X = 300;29. upperRight.Y = 0;30. upperRight.Z = 0;31. lowerLeft.X = 0; 32. lowerLeft.Y = 0;33. lowerLeft.Z = 100;34. lowerRight.X = 300;35. lowerRight.Y = 1; 36. lowerRight.Z = 100;37. multiPatchCreator.SetWKSPointZ(0, ref upperRight);38. multiPatchCreator.SetWKSPointZ(1, ref lowerRight);3
29、9. multiPatchCreator.SetWKSPointZ(2 , ref upperLeft); 40. multiPatchCreator.SetWKSPointZ(3 , ref lowerLeft);41. /Set texture points.42. /Set the texture coordinates for a panel.43. WKSPoint textureUpperLeft44. = new WKSPoint(); 45. WKSPoint textureLowerLeft46. = new WKSPoint();47. WKSPoint textureUp
30、perRight = new WKSPoint();48. WKSPoint textureLowerRight = new WKSPoint();49. textureUpperLeft.X = 0; 50. textureUpperLeft.Y = 0;51. textureUpperRight.X = 1;52. textureUpperRight.Y = 0;53. textureLowerLeft.X = 0; 54. textureLowerLeft.Y = 1;55. textureLowerRight.X = 1;56. textureLowerRight.Y = 1;57.
31、multiPatchCreator.SetTextureWKSPoint(0, ref textureUpperRight);58. multiPatchCreator.SetTextureWKSPoint(1, ref textureLowerRight);59. multiPatchCreator.SetTextureWKSPoint(2, ref textureUpperLeft);60. multiPatchCreator.SetTextureWKSPoint(3, ref textureLowerLeft);61. IMultiPatch multiPatch = multiPatchCreator.CreateMultiPatch() as IMultiPatch; 62. return multiPatch;63. 复制代码