0

I have an file of points which are to be converted to a set of polylines. Some of the lines are multipart and I want to convert them to singlepart. The points are processed and the polylines added to an Interim featureclass (for backup purposes). If a line is multipart it is added to this interim featureclass as multipart. The interim featureclass is then read and it's features copied to a base featureclass. This works fine for single part features but I keep getting the 'No support for this geometry type' error when converting the multiparts to singleparts. The problem is that to create the multipart feature from a set of points I have to use a segment collection as a Path. I've tried setting this to a polyline but then adding line segments to it causes an error (wrong geometry type).

When I add the multipart features to the interim featureclass the geometries are Polylines. When I retrieve them later (by putting thte shape into a new GeometryCollection) the geometry collection is a Polyline but the individual geometries are Paths (?).

The code is:

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;

This part of the code all works fine. When processing these features from the interim featureclass to add them to the base featureclass I get an error because the geometry type from the geometry collection is 'Path' not 'Polyline'.

 // 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.
}

How can I ensure that the geometry collection contains geometries with Polylines rather than Paths?

Thanks,

JM

John M
  • 53
  • 7

1 Answers1

0

I have found what I condsider to be a workaround for this problem but it's not very pretty. The solution is to cast the Path geometry into a polyline and then cast it back into a geometry when assigning it to the .Shape attribute.

      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;
 }

}
John M
  • 53
  • 7