/// <summary> /// For faster clipping of polygons with lines. Limits the finding of intersections to /// outside->inside or inside->outside 2pt segments. Assumes only one intersections exists /// per segment, that a segment of two inside points or two outside points will not intersect /// the polygon. /// </summary> /// <param name="polygon">The polygon that will be sectioned by the line.</param> /// <param name="line">The line that will clip the polygon into multiple parts.</param> /// <param name="resultFeatureSet">The in-memory shapefile where the polygon sections will be saved.</param> /// <returns>False if errors are encountered, true otherwise.</returns> public static bool Fast_ClipPolygonWithLine( ref IFeature polygon, ref IFeature line, ref IFeatureSet resultFeatureSet) { IFeatureSet resultFile = new FeatureSet(FeatureType.Polygon); if (polygon != null && line != null) { // make sure we are dealing with a valid shapefile type if (polygon.FeatureType == FeatureType.Polygon) { // create the result shapefile if it does not already exist if (!polygon.Overlaps(line)) { resultFeatureSet = resultFile; return false; } // find if all of the line is inside, outside, or part in and out of polygon // line might intersect polygon mutliple times int numPoints = line.NumPoints; bool[] ptsInside = new bool[numPoints]; int numInside = 0; int numOutside = 0; int numParts = polygon.NumGeometries; if (numParts == 0) { numParts = 1; } Coordinate[][] polyVertArray = new Coordinate[numParts][]; ConvertPolyToVertexArray(ref polygon, ref polyVertArray); // check each point in the line to see if the entire line is either // inside of the polygon or outside of it (we know it's inside polygon bounding box). for (int i = 0; i <= numPoints - 1; i++) { Point currPt = new Point(line.Coordinates[i]); if (polygon.Covers(currPt)) { ptsInside[i] = true; numInside += 1; } else { ptsInside[i] = false; numOutside += 1; } } // case: all points are inside polygon - check for possible intersections if (numInside == numPoints) { // assume no intersections exist in fast version } else if (numOutside == numPoints) { // case: all points are outside of the polygon - check for possible intersections // assume no intersections exist in fast version } else { // case: part of line is inside and part is outside - find inside segments. if (Fast_ProcessPartInAndOut(ptsInside, line, polygon, resultFile) == false) { resultFeatureSet = resultFile; return false; } } // resultSF result file, do not save to disk. resultFeatureSet = resultFile; } else { resultFeatureSet = resultFile; return false; } } else { // polygon or line is invalid resultFeatureSet = resultFile; return false; } return true; }