2017-03-09 50 views
0

我有將要被轉換成一組折線點的文件。一些線是多部分,我想將它們轉換爲單部分。 處理點並將多段線添加到臨時要素類(用於備份目的)。如果一行是多部分,則它將作爲多部分添加到此臨時要素類中。 然後讀取臨時要素類,並將其複製到基本要素類中。這適用於單部件特徵,但在將多部件轉換爲單部件時,我始終得到'不支持此幾何類型'的錯誤。問題是,要從一組點創建多部分功能,我必須將一個段集合用作Path。我試着把它設置成多段線,但是向它添加線段會導致錯誤(錯誤的幾何類型)。IGeometryCollection折線的返回路徑

當我到多的功能添加到臨時要素類的幾何形狀折線。當我稍後檢索它們(通過將形狀放入新的GeometryCollection中)時,幾何集合是Polyline,但各個幾何體是Paths(?)。

的代碼是:

1. Add points to interim featureclass by putting them in a pointcollection. 

    pPtColl = (IPointCollection4)new Polyline(); 
    pGeomColl = (IGeometryCollection)new Polyline(); 

    // Fill point collection 
    ....... 

    // Create a path made up of segments from the point collection 
    ISegment pSegment; 
    ISegmentCollection pSegColl = (ISegmentCollection)new ESRI.ArcGIS.Geometry.Path(); // Fails if changed to new Polyline() 

    // M and Z aware 
    if (bHasZ == true) 
    { 
     pZAware = (IZAware)pSegColl; 
     pZAware.ZAware = true; 
    } 
    if (bHasM == true) 
    { 
    pMAware = (IMAware)pSegColl; 
    pMAware.MAware = true; 
    } 

    for (int n = 1; n < pPtColl.PointCount; n++) 
    { 
    pSegment = (ISegment)new Line(); 
    pSegment.SpatialReference = pSpRef; 
    pSegment.FromPoint = pPtColl.Point[n - 1]; 
    pSegment.ToPoint = pPtColl.Point[n]; 
    pSegColl.AddSegment(pSegment, oMissing, oMissing); 
    } 

    pGeomColl.AddGeometry(pSegColl as IGeometry, oMissing, oMissing); 

    pGeom = (IGeometry)pGeomColl; // pGeom has geometry type = Polyline 
    pGeom.SpatialReference = pSpRef; 

    pFeat.Shape = pGeom; 

代碼一切工作正常的這一部分。 處理臨時要素類中的這些要素以將它們添加到基礎要素類時,我收到一個錯誤,因爲幾何集合中的幾何類型是'路徑'而不是'折線'。

// Read the geometry from the interim feature into a geometry collection 
pGeomColl = (IGeometryCollection)new Polyline(); 
pGeomColl = (IGeometryCollection)pFromFeature.ShapeCopy; 

for (int j = 0; j < pGeomColl.GeometryCount; j++) 
{ 
    // Create a new (Polyline) feature pToFeat and populate its attributes 
    pToFeat = pToFC.CreateFeature(); 
    .... 

    // pGeomColl has geometry type = Polyline 
    pGeom = pGeomColl.Geometry[j]; // pGeom has geometry type = Path 
    pToFeat.Shape = pGeom; // Fails. pToFeat is a Polyline. 
} 

如何確保幾何集合包含帶多段線而不是路徑的幾何?

感謝,

JM

回答

0

我已經找到了我condsider成爲這個問題的方法,但它是不是很漂亮。 解決方案是將Path幾何體轉換爲多段線,然後在將其分配給.Shape屬性時將其重新轉換爲幾何體。

 if (pToFeat .ShapeCopy.GeometryType == esriGeometryType.esriGeometryPolyline && pGeom.GeometryType == esriGeometryType.esriGeometryPath) 
    { 
     IPolyline pPoly = (IPolyline)new Polyline(); 
     pPoly = geometryToPolyline(pGeom, bHasZ, bHasM, ref sError); 
     if (sError.Length > 0) 
     { 
      sError = "processAdds; ID = " + sID + " OID = " + iOID.ToString() + sNL + sError; 
      clsMain.write_log(sError, clsMain.m_eLogType.FATAL); 
      iErrorCount++; 
     } 
     else 
     { 
      pToFeat.Shape = (IGeometry)pPoly; 
     } 
    } 


private static IPolyline geometryToPolyline(IGeometry pInputGeom, bool bHasZ, bool bHasM, ref string sError) 
{ 
IPolyline pPoly = null; 
IGeometryCollection pPolyColl = null; 
IZAware pZAware; 
IMAware pMAware; 
double dZ; 
ISpatialReference pSpRef; 
bool bIsMulti; 
esriGeometryType pType; 

try 
{ 
    sError = ""; 

    pSpRef = pInputGeom.SpatialReference; 

    // Create a new polyline 
    pPoly = (IPolyline)new Polyline(); 

    if (bHasZ == true) 
    { 
     pZAware = (IZAware)pPoly; 
     pZAware.ZAware = true; 
    } 
    if (bHasM == true) 
    { 
     pMAware = (IMAware)pPoly; 
     pMAware.MAware = true; 
    } 

    // Create the geometry collection 
    pPolyColl = (IGeometryCollection)new Polyline(); 
    if (bHasZ == true) 
    { 
     pZAware = (IZAware)pPolyColl; 
     pZAware.ZAware = true; 
    } 
    if (bHasM == true) 
    { 
     pMAware = (IMAware)pPolyColl; 
     pMAware.MAware = true; 
    } 

    // Set the polyline as the geometry collection 
    pPoly = (IPolyline)pPolyColl; 
    pPoly.SpatialReference = pSpRef; 
    pPolyColl.AddGeometry(pInputGeom); 

    return pPoly; 
} 
catch (Exception ex) 
{ 
    System.Diagnostics.StackTrace pStack = new System.Diagnostics.StackTrace(ex, true); 
    System.Diagnostics.StackFrame pFrame = pStack.GetFrame(pStack.FrameCount - 1); 
    int iLineNo = pFrame.GetFileLineNumber(); 

    sError = "ERROR: geometryToPolyline; Line: " + iLineNo + "\n" + ex.ToString(); 
    return pPoly; 
} 

}