目錄
1 點要素連接配接成線要素
1.1 需求說明
1.2 LineClass示例代碼
2 線要素反向
3 由點集建立多段線要素
4 擷取兩條線要素的交點
4.1 建立交集代碼
4.2 注意事項
4.3 根據polygon的拓撲關系建立線要素
1 點要素連接配接成線要素
1.1 需求說明
将兩個點連接配接起來構成為直線要素

1.2 LineClass示例代碼
A 2D straight line between a pair of 2D endpoints; can optionally have height, measure and ID attributes at each endpoint.
從二維點對生成二維直線,高程坐标、測度、ID等屬性可選
用到的API:
類 | 屬性/方法 | 功能 |
LineClass | FromPoint | 線段起點 |
ToPoint | 線段的終點 |
/// <summary>
/// 建立線要素
/// </summary>
/// <returns>線幾何圖形</returns>
public static IGeometry GetLineGeometry()
{
ILine line = new LineClass();
line.FromPoint = GetPoint();
line.ToPoint = GetPoint();
return line as IGeometry;
}
/// <summary>
/// 随機生成三維資料點
/// </summary>
/// <returns>三維資料點</returns>
private static IPoint GetPoint()
{
const double Min = -10;
const double Max = 10;
Random random = new Random();
double x = Min + (Max - Min) * random.NextDouble();
double y = Min + (Max - Min) * random.NextDouble();
double z = Min + (Max - Min) * random.NextDouble();
return ConstructPoint3D(x, y, z);
}
/// <summary>
/// 根據輸入的x,y,z坐标,生成三維資料點
/// </summary>
/// <param name="x">X坐标</param>
/// <param name="y">Y坐标</param>
/// <param name="z">Z坐标</param>
/// <returns>三維坐标點</returns>
private static IPoint ConstructPoint3D(double x, double y, double z)
{
IPoint point = ConstructPoint2D(x, y);
point.Z = z;
return point;
}
/// <summary>
/// 建構二維坐标點
/// </summary>
/// <param name="x">X坐标</param>
/// <param name="y">Y坐标</param>
/// <returns>二維點集合圖形</returns>
private static IPoint ConstructPoint2D(double x, double y)
{
IPoint point = new PointClass();
point.PutCoords(x, y);
return point;
}
2 線要素反向
線要素在arcgis中的存儲方式為一條有向的路徑path,如果想要得到線要素反向,隻需要擷取線要素節點集合,然後将其逆序得到新的線要素即為反向線要素
/// <summary>
/// 線要素反向
/// </summary>
/// <param name="polyline">線要素</param>
/// <returns>反向之後的線要素</returns>
private IPolyline ChangeDirection(IPolyline polyline)
{
IPointCollection pointCollection = polyline as IPointCollection;
IPointCollection newPC = new PolylineClass();
for (int i = pointCollection.PointCount - 1; i >= 0; i--)
{
newPC.AddPoint(pointCollection.get_Point(i), mis, mis);
}
return newPC as IPolyline;
}
3 由點集建立多段線要素
接口跳轉的前提是有類實作了該接口,比如使用PolylineClass實作了IPointCollection 接口,然後添加點之後在跳轉到IPolyline;也就是之是以能夠實作接口跳轉,是因為有一個類同時實作了這兩個接口,這樣以這個類為跳闆,實作接口之間的跳轉。
注意:使用點集接口建立PolylineClass類對象,之後添加點集之後,跳轉為IPolyline
/// <summary>
/// 由點集建立多段線
/// </summary>
/// <param name="pts">點集</param>
/// <returns>多段線</returns>
public IPolyline AddLineByWrite(List<IPoint> pts)
{
IPointCollection ptColl = new PolylineClass();
for (int i = 0; i < pts.Count; i++)
{
ptColl.AddPoint(pts[i]);
}
return ptColl as IPolyline;
}
4 擷取兩條線要素的交點
4.1 建立交集代碼
ArcEngine中相交分析用到接口為 ITopologicalOperator 這裡需要用到它裡面的 Intersect方法,功能很強大,能根據兩個交的幾何圖形得到對應的各種交集(點,線,面等)現在我要做兩個線段的交集,代碼如下:
IPolyline tmpLine1 = new PolylineClass();
tmpLine1.FromPoint = 起點;
tmpLine1.ToPoint = 終點;
IPolyline tmpLine2 = new PolylineClass();
tmpLine2.FromPoint = 起點;
tmpLine2.ToPoint = 終點;
ITopologicalOperator topoOperator = tmpLine1 as ITopologicalOperator;
IGeometry geo = topoOperator.Intersect(tmpLine2 , esriGeometryDimension.esriGeometry0Dimension);
if (!geo.IsEmpty)
{
IPointCollection Pc = geo as IPointCollection;
IPoint Pt = Pc.get_Point(0);
}
Intersect(tmpLine2 , esriGeometryDimension.esriGeometry0Dimension)方法的第二個參數,決定的是輸出的要素的類型,如果要輸出交點就是0D
3D IPolyline 要打開Z屬性。上面的代碼需要加上這個:
IZAware zaware = tmpLine1 as IZAware;
zaware.ZAware = true;
兩條線都要加,這樣再對其做相交分析就能正常得到Z屬性了!
4.2 注意事項
以下是官方文檔的說明:
Intersection is basically an AND between input geometries.
ITopologicalOperator methods must be applied on high-level geometries only. High-Level geometries are point, multipoint, polyline and polygon. To use this method with low-level geometries such as segments (Line, Circular Arc, Elliptic Arc, Bézier Curve), paths or rings, they must be wrapped into high-level geometries types.
This method does not support GeometryBags.
Since ArcGIS 9.2, Intersect has a larger cost - it takes longer to run the method. Therefore, it is a better approach to test if the two geometries are disjoint before calling Intersect.
ntersection基本上是輸入幾何圖形之間的AND。ItopologicalOperator方法隻能應用于進階幾何圖形。進階幾何圖形包括點、多點、多段線和多邊形。要将此方法用于低層幾何圖形,如線段(直線、圓弧、橢圓弧、Bézier曲線)、路徑或環,必須将它們包裝成高層幾何圖形類型。此方法不支援GeometryBags。因為ArcGis9.2,Intersect有更大的成本-運作該方法需要更長的時間。是以,在調用Intersect之前測試兩個幾何圖形是否不相交是一種更好的方法。
也就是相交不能針對簡單圖形,簡單圖形要做一層進階包裝
//The following code shows to wrap a line segment into a polyline in C#
//Assume a line (line1 as ILine) is already created
object obj = Type.Missing;
ISegmentCollection segCollection = new PolylineClass() as ISegmentCollection;
segCollection.AddSegment((ISegment)line1, ref obj, ref obj);
//Set the spatial reference on the new polyline
//The spatial reference is not transfered automatically from the segments
IGeometry geom = segCollection as IGeometry;
geom.SpatialReference = spatialRef;
//Can now be used with ITopologicalOperator methods
4.3 根據polygon的拓撲關系建立線要素
void CreatePolylineFromExistingGeometries(ESRI.ArcGIS.Geometry.IPolygon pPolygon1,
ESRI.ArcGIS.Geometry.IPolygon pPolygon2)
{
//Build a new polyline by intersecting two existing polygons.
ESRI.ArcGIS.Geometry.ITopologicalOperator2 pTopoOp2 = pPolygon1 as
ITopologicalOperator2;
//Simplify.
pTopoOp2.IsKnownSimple_2 = false;
pTopoOp2.Simplify();
ESRI.ArcGIS.Geometry.IPolyline pPoly = pTopoOp2.Intersect(pPolygon2,
ESRI.ArcGIS.Geometry.esriGeometryDimension.esriGeometry1Dimension)as
IPolyline;
//pPoly is the new generated polyline.
}
5 IRelationalOperator
Relational Operators compare two geometries and return a boolean indicating whether or not the desired relationship exists. Some relationships require that the input geometries be of the same dimension while other have more flexible dimensional constraints. Most of the predefined Relational Operators are mutually exclusive Clementini operators. Please see the "Shape Comparison Language" topic in the Technical Documents section in the help.
關系運算符比較兩個幾何圖形并傳回一個布爾值,訓示所需關系是否存在。有些關系要求輸入幾何圖形具有相同的尺寸,而另一些關系則具有更靈活的尺寸限制。大多數預定義的關系運算符都是互斥的Clementini運算符。請參閱幫助中技術文檔部分的“形狀比較語言”主題。
AllPropertiesMethods | Description |
---|---|
Contains | Indicates if this geometry contains the other geometry. 【前者是否包含後者】 |
Crosses | Indicates if the two geometries intersect in a geometry of lesser dimension. 【是否在更低的次元相交】 |
Disjoint | Indicates if the two geometries share no points in common. Negate this result to compute the Intersect relation. [是否有共同點,求反可驗證相交] |
Equals | Indicates if the two geometries are of the same type and define the same set of points in the plane. 【訓示兩個幾何圖形是否屬于同一類型并在平面中定義相同的點集。】 |
Overlaps | Indicates if the intersection of the two geometries has the same dimension as one of the input geometries. 【訓示兩個幾何圖形的交集是否與一個輸入幾何圖形具有相同的尺寸】 |
Relation | Indicates if the defined relationship exists. 【訓示定義的關系是否存在】 |
Touches | Indicates if the boundaries of the geometries intersect. 【訓示幾何圖形的邊界是否相交】 |
Within | Indicates if this geometry is contained (is within) another geometry. 【訓示此幾何圖形是否包含在另一個幾何圖形中。】 |
目錄