Polylines的IGeometryCollection返回Paths

时间:2017-03-09 01:00:21

标签: arcobjects

我有一个要转换为一组折线的点文件。有些行是多部分的,我想将它们转换为单部分。 处理这些点并将折线添加到临时要素类(用于备份目的)。如果一行是多部分,则将其作为multipart添加到此临时要素类中。 然后读取临时要素类,并将其特征复制到基本要素类。这适用于单个零件特征,但在将多个零件转换为单个零件时,我一直收到“不支持此几何类型”错误。问题是要从一组点创建多部分特征,我必须使用一个分段集合作为路径。我已尝试将其设置为折线,但随后向其添加线段会导致错误(错误的几何类型)。

当我将多部分要素添加到临时要素类时,几何是折线。当我稍后检索它们时(通过将形状放入新的GeometryCollection中),几何集合是一条折线,但各个几何形状是路径(?)。

代码是:

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

1 个答案:

答案 0 :(得分:0)

我发现我认为这是一个解决这个问题的方法,但它并不是很漂亮。 解决方案是将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;
 }

}