/// <summary> /// Removes points from the point shapefile that lie within any shapes in the polygon shapefile. /// </summary> /// <param name="pointSF">The point shapefile.</param> /// <param name="polygonSF">The shapefile containing the erase polygons.</param> /// <param name="resultSF">The resulting file with points removed.</param> /// <returns>False if an error was encountered, true otherwise.</returns> public static bool ErasePointSFWithPolySF(ref MapWinGIS.Shapefile pointSF, ref MapWinGIS.Shapefile polygonSF, ref MapWinGIS.Shapefile resultSF) { MapWinUtility.Logger.Dbg("ErasePointSFWithPolySF(pointSF : " + Macro.ParamName(pointSF) + ",\n" + " polygonSF: " + Macro.ParamName(polygonSF) + ",\n" + " resultSF: " + resultSF.ToString()); if (pointSF == null || polygonSF == null || resultSF == null) { gErrorMsg = "One of the input parameters is null."; Error.SetErrorMsg(gErrorMsg); Debug.WriteLine(gErrorMsg); MapWinUtility.Logger.Dbg(gErrorMsg); return(false); } int shpIndex = 0; int numPts = pointSF.NumShapes; for (int i = 0; i <= numPts - 1; i++) { shpIndex = resultSF.NumShapes; resultSF.EditInsertShape(pointSF.get_Shape(i), ref shpIndex); } int numPolygons = polygonSF.NumShapes; for (int i = 0; i <= numPolygons - 1; i++) { MapWinGIS.Shape currPoly = new MapWinGIS.ShapeClass(); currPoly.Create(polygonSF.ShapefileType); currPoly = polygonSF.get_Shape(i); int numParts = currPoly.NumParts; if (numParts == 0) { numParts = 1; } Globals.Vertex[][] polyVertArray = new Globals.Vertex[numParts][]; Globals.ConvertPolyToVertexArray(ref currPoly, out polyVertArray); numPts = resultSF.NumShapes; for (int j = 0; j <= numPts - 1; j++) { double x = resultSF.QuickPoint(j, 0).x; double y = resultSF.QuickPoint(j, 0).y; if (Utils.PointInPoly(ref polyVertArray, x, y) == true) { //remove the point. resultSF.EditDeleteShape(j); numPts--; j--; } } } MapWinUtility.Logger.Dbg("Finsihed ErasePointSFWithPolySF"); return(true); }
/// <summary> /// Returns a shapefile of points from the input shapefile that fall within the polygon. /// </summary> /// <param name="pointSFPath">Full path to the point shapefile.</param> /// <param name="polygon">The polygon used for clipping the point shapefile.</param> /// <param name="resultSFPath">Full path to where the resulting point shapefile should be saved.</param> /// <param name="copyAttributes">True if copying attributes over</param> /// <returns>False if an error was encountered, true otherwise.</returns> public static bool ClipPointSFWithPolygon(ref string pointSFPath, ref MapWinGIS.Shape polygon, ref string resultSFPath, bool copyAttributes) { MapWinUtility.Logger.Dbg("ClipPointSFWithPolygon(pointSFPath: " + pointSFPath + ",\n" + " polygon: " + Macro.ParamName(polygon) + ",\n" + " resultSFPath: " + resultSFPath.ToString() + ",\n" + " copyAttributes: " + copyAttributes.ToString() + ")"); MapWinGIS.Shapefile resultSF = new MapWinGIS.ShapefileClass(); MapWinGIS.Shapefile pointSF = new MapWinGIS.ShapefileClass(); int shpIndex = 0; //all new shapes will be placed at the beginning of the shapefile string tmpName; if (pointSF.Open(pointSFPath, null) == false) { gErrorMsg = "Failure to open input shapefile: " + pointSF.get_ErrorMsg(pointSF.LastErrorCode); MapWinUtility.Logger.Dbg(gErrorMsg); return(false); } MapWinGIS.ShpfileType sfType = pointSF.ShapefileType; //make sure we are dealing with a valid shapefile type if (sfType == MapWinGIS.ShpfileType.SHP_POINT || sfType == MapWinGIS.ShpfileType.SHP_POINTM || sfType == MapWinGIS.ShpfileType.SHP_POINTZ) { if (Globals.PrepareResultSF(ref resultSFPath, ref resultSF, sfType, copyAttributes) == false) { return(false); } if (copyAttributes) { MapWinGIS.Field tmpField, pointField; for (int f = 0; f <= pointSF.NumFields - 1; f++) { tmpField = new MapWinGIS.Field(); pointField = pointSF.get_Field(f); tmpName = pointField.Name; if (tmpName.Contains("MWShapeID")) { tmpField.Name = "Last_" + tmpName; } else { tmpField.Name = tmpName; } tmpField.Width = pointField.Width; tmpField.Type = pointField.Type; tmpField.Precision = pointField.Precision; tmpField.Key = pointField.Key; if (!resultSF.EditInsertField(tmpField, ref f, null)) { return(false); } } } int numTargetPoints = pointSF.NumShapes; MapWinGIS.Point targetPoint = new MapWinGIS.PointClass(); //MapWinGIS.Utils utils = new MapWinGIS.UtilsClass(); int numParts = polygon.NumParts; if (numParts == 0) { numParts = 1; } Globals.Vertex[][] polyVertArray = new Globals.Vertex[numParts][]; Globals.ConvertPolyToVertexArray(ref polygon, out polyVertArray); for (int i = 0; i <= numTargetPoints - 1; i++) { targetPoint = pointSF.QuickPoint(i, 0); if (Utils.PointInPoly(ref polyVertArray, ref targetPoint) == true) { resultSF.EditInsertShape(pointSF.get_Shape(i), ref shpIndex); if (copyAttributes) { for (int f = 0; f <= pointSF.NumFields - 1; f++) { bool tmpbool = resultSF.EditCellValue(f, shpIndex, pointSF.get_CellValue(f, i)); } } } } MapWinGIS.Field ID = new MapWinGIS.FieldClass(); ID.Name = "MWShapeID"; ID.Type = MapWinGIS.FieldType.INTEGER_FIELD; int fieldIndex = 0; if (resultSF.EditInsertField(ID, ref fieldIndex, null) == false) { gErrorMsg = "Problem inserting field into .dbf table: " + resultSF.get_ErrorMsg(resultSF.LastErrorCode); Debug.WriteLine(gErrorMsg); Error.SetErrorMsg(gErrorMsg); MapWinUtility.Logger.Dbg(gErrorMsg); return(false); } int numIDs = resultSF.NumShapes; for (int i = 0; i <= numIDs - 1; i++) { if (resultSF.EditCellValue(fieldIndex, i, i) == false) { gErrorMsg = "Problem inserting value into .dbf table for shape " + i + ": " + resultSF.get_ErrorMsg(resultSF.LastErrorCode); Debug.WriteLine(gErrorMsg); Error.SetErrorMsg(gErrorMsg); MapWinUtility.Logger.Dbg(gErrorMsg); return(false); } } if (resultSF.StopEditingShapes(true, true, null) == false) { gErrorMsg = "Problem with StopEditingShapes: " + resultSF.get_ErrorMsg(resultSF.LastErrorCode); Debug.WriteLine(gErrorMsg); Error.SetErrorMsg(gErrorMsg); MapWinUtility.Logger.Dbg(gErrorMsg); return(false); } resultSF.Close(); pointSF.Close(); MapWinUtility.Logger.Dbg("Finished ClipPointSFWithPolygon"); } else { pointSF.Close(); gErrorMsg = "Shapefile type is incorrect. Must be of type Point."; Debug.WriteLine(gErrorMsg); MapWinGeoProc.Error.SetErrorMsg(gErrorMsg); MapWinUtility.Logger.Dbg(gErrorMsg); return(false); } return(true); }
/// <summary> /// Returns an in-memory shapefile of points from the input shapefile that fall within the polygon. /// </summary> /// <param name="pointSF">Full path to the point shapefile.</param> /// <param name="polygon">The polygon used for clipping the point shapefile.</param> /// <param name="result">Full path to where the resulting point shapefile should be saved.</param> /// <param name="copyAttributes">True if copying attributes over</param> /// <returns>False if an error was encountered, true otherwise.</returns> public static bool ClipPointSFWithPolygon(ref MapWinGIS.Shapefile pointSF, ref MapWinGIS.Shape polygon, out MapWinGIS.Shapefile result, bool copyAttributes) { MapWinUtility.Logger.Dbg("ClipPointSFWithPolygon(pointSF: " + Macro.ParamName(pointSF) + ",\n" + " polygon: " + Macro.ParamName(polygon) + ",\n" + " result: out, \n" + " copyAttributes: " + copyAttributes.ToString() + ",\n"); MapWinGIS.Shapefile resultSF = new MapWinGIS.ShapefileClass(); int shpIndex = 0; //all new shapes will be placed at the beginning of the shapefile MapWinGIS.ShpfileType sfType = pointSF.ShapefileType; //make sure we are dealing with a valid shapefile type if (sfType == MapWinGIS.ShpfileType.SHP_POINT || sfType == MapWinGIS.ShpfileType.SHP_POINTM || sfType == MapWinGIS.ShpfileType.SHP_POINTZ) { string tempPath = System.IO.Path.GetTempPath() + "resultSF.shp"; DataManagement.DeleteShapefile(ref tempPath); string tmpName; //create the result shapeFile if it does not already exist //if(resultSF.CreateNew(tempPath, sfType) == false) //{ // gErrorMsg = "Problem creating the result shapeFile: " + resultSF.get_ErrorMsg(resultSF.LastErrorCode); // Debug.WriteLine(gErrorMsg); // MapWinGeoProc.Error.SetErrorMsg(gErrorMsg); // result = resultSF; // return false; //} //CDM 8/4/2006 resultSF.CreateNew(resultSFPath, sfType); Globals.PrepareResultSF(ref tempPath, ref resultSF, sfType); if (copyAttributes) { MapWinGIS.Field tmpField, pointField; for (int f = 0; f <= pointSF.NumFields - 1; f++) { tmpField = new MapWinGIS.Field(); pointField = pointSF.get_Field(f); tmpName = pointField.Name; if (tmpName.Contains("MWShapeID")) { tmpField.Name = "Last_" + tmpName; } else { tmpField.Name = tmpName; } tmpField.Width = pointField.Width; tmpField.Type = pointField.Type; tmpField.Precision = pointField.Precision; tmpField.Key = pointField.Key; resultSF.EditInsertField(tmpField, ref f, null); } } int numTargetPoints = pointSF.NumShapes; MapWinGIS.Point targetPoint = new MapWinGIS.PointClass(); //MapWinGIS.Utils utils = new MapWinGIS.UtilsClass(); int numParts = polygon.NumParts; if (numParts == 0) { numParts = 1; } Globals.Vertex[][] polyVertArray = new Globals.Vertex[numParts][]; Globals.ConvertPolyToVertexArray(ref polygon, out polyVertArray); for (int i = 0; i <= numTargetPoints - 1; i++) { targetPoint = pointSF.QuickPoint(i, 0); if (Utils.PointInPoly(ref polyVertArray, ref targetPoint) == true) { resultSF.EditInsertShape(pointSF.get_Shape(i), ref shpIndex); if (copyAttributes) { for (int f = 0; f <= pointSF.NumFields - 1; f++) { bool tmpbool = resultSF.EditCellValue(f, shpIndex, pointSF.get_CellValue(f, i)); } } } } } else { gErrorMsg = "The shapefile is of the wrong type. Should be of type Point."; Debug.WriteLine(gErrorMsg); MapWinGeoProc.Error.SetErrorMsg(gErrorMsg); result = resultSF; MapWinUtility.Logger.Dbg(gErrorMsg); return(false); } result = resultSF; MapWinUtility.Logger.Dbg("Finished ClipPointSFWithPolygon"); return(true); }
/// <summary> /// Removes points from the point shapefile that lie within the polygon. /// </summary> /// <param name="pointSF">The point shapefile.</param> /// <param name="polygon">The erase polygon.</param> /// <param name="resultSF">The resulting file with points removed.</param> /// <param name="CopyAttributes">Indicates whether to copy attributes</param> /// <returns>False if an error was encountered, true otherwise.</returns> public static bool ErasePointSFWithPoly(ref MapWinGIS.Shapefile pointSF, ref MapWinGIS.Shape polygon, ref MapWinGIS.Shapefile resultSF, bool CopyAttributes) { MapWinUtility.Logger.Dbg("ErasePointSFWithPoly(pointSF: " + Macro.ParamName(pointSF) + "\n, " + " polygon: " + Macro.ParamName(polygon) + "\n, " + " resultSF: " + Macro.ParamName(resultSF) + "\n, " + " CopyAttributes: " + CopyAttributes.ToString() + ")"); if (pointSF == null || polygon == null || resultSF == null) { gErrorMsg = "One of the input parameters is null."; Error.SetErrorMsg(gErrorMsg); Debug.WriteLine(gErrorMsg); MapWinUtility.Logger.Dbg(gErrorMsg); return(false); } if (CopyAttributes) { string tmpName; MapWinGIS.Field tmpField, currField; for (int f = 0; f <= pointSF.NumFields - 1; f++) { tmpField = new MapWinGIS.Field(); currField = pointSF.get_Field(f); tmpName = currField.Name; tmpField.Name = tmpName; tmpField.Width = currField.Width; tmpField.Type = currField.Type; tmpField.Precision = currField.Precision; tmpField.Key = currField.Key; resultSF.EditInsertField(tmpField, ref f, null); } } int numPts = pointSF.NumShapes; int numParts = polygon.NumParts; if (numParts == 0) { numParts = 1; } int shpIndex = 0; Globals.Vertex[][] vertArray = new Globals.Vertex[numParts][]; Globals.ConvertPolyToVertexArray(ref polygon, out vertArray); for (int i = 0; i <= numPts - 1; i++) { MapWinGIS.Point currPt = new MapWinGIS.PointClass(); currPt = pointSF.QuickPoint(i, 0); double currX = currPt.x; double currY = currPt.y; if (Utils.PointInPoly(ref vertArray, currX, currY) == false) { shpIndex = resultSF.NumShapes; if (resultSF.EditInsertShape(pointSF.get_Shape(i), ref shpIndex) == false) { gErrorMsg = "ErasePointSF: problem inserting shape into result file: " + resultSF.get_ErrorMsg(resultSF.LastErrorCode); Debug.WriteLine(gErrorMsg); Error.SetErrorMsg(gErrorMsg); MapWinUtility.Logger.Dbg(gErrorMsg); return(false); } if (CopyAttributes) { for (int f = 0; f <= pointSF.NumFields - 1; f++) { bool tmpbool = resultSF.EditCellValue(f, shpIndex, pointSF.get_CellValue(f, i)); } } } } MapWinUtility.Logger.Dbg("Finished ErasePointSFWithPoly"); return(true); }
/// <summary> /// Removes portions of the input polygon shapefile that are within the erase polygons. /// </summary> /// <param name="inputSF">The input polygon shapefile.</param> /// <param name="eraseSF">The erase polygon shapefile.</param> /// <param name="resultSF">The resulting shapefile, with portions removed.</param> /// <returns>False if an error was encountered, true otherwise.</returns> public static bool ErasePolySFWithPolySF(ref MapWinGIS.Shapefile inputSF, ref MapWinGIS.Shapefile eraseSF, ref MapWinGIS.Shapefile resultSF) { MapWinUtility.Logger.Dbg("ErasePolySFWithPolySF(inputSF: " + Macro.ParamName(inputSF) + ",\n" + " eraseSF: " + Macro.ParamName(eraseSF) + ",\n" + " resultSF: " + Macro.ParamName(resultSF) + ",\n"); if (inputSF == null || eraseSF == null || resultSF == null) { gErrorMsg = "One of the input parameters is null."; Error.SetErrorMsg(gErrorMsg); Debug.WriteLine(gErrorMsg); MapWinUtility.Logger.Dbg(gErrorMsg); return(false); } int numInputs = inputSF.NumShapes; int shpIndex = 0; //create the result shapefile out of the original inputSF for (int i = 0; i <= numInputs - 1; i++) { shpIndex = resultSF.NumShapes; resultSF.EditInsertShape(inputSF.get_Shape(i), ref shpIndex); } int numErase = eraseSF.NumShapes; for (int i = 0; i <= numErase - 1; i++) { MapWinGIS.Shape eraseShape = new MapWinGIS.ShapeClass(); eraseShape = eraseSF.get_Shape(i); MapWinGIS.Shape resultShp = new MapWinGIS.ShapeClass(); for (int j = 0; j <= numInputs - 1; j++) { MapWinGIS.Shape currShape = new MapWinGIS.ShapeClass(); currShape = resultSF.get_Shape(j); //if bounds intersect, then check if all polygon points are inside the currShape if (Globals.CheckBounds(ref currShape, ref eraseShape)) { int numPts = eraseShape.numPoints; bool allInside = true; int numParts = eraseShape.NumParts; if (numParts == 0) { numParts = 1; } Globals.Vertex[][] vertArray = new Globals.Vertex[numParts][]; Globals.ConvertPolyToVertexArray(ref currShape, out vertArray); for (int k = 0; k <= numPts - 1; k++) { double x = eraseShape.get_Point(k).x; double y = eraseShape.get_Point(k).y; if (Utils.PointInPoly(ref vertArray, x, y) == false) { allInside = false; break; } } if (allInside == true) { resultShp = new MapWinGIS.ShapeClass(); resultShp.Create(inputSF.ShapefileType); //we want the symmetric difference of these two shapes //which should leave us with a hole where the erase polygon was in the currShape resultShp = SpatialOperations.SymmetricDifference(eraseShape, currShape); } else { //erase overlapping section and add result to the file. MapWinGIS.Shape intersect = new MapWinGIS.ShapeClass(); intersect.ShapeType = inputSF.ShapefileType; intersect = SpatialOperations.Intersection(eraseShape, currShape); if (intersect.numPoints > 0) { MapWinGIS.Shape diff = new MapWinGIS.ShapeClass(); diff.ShapeType = eraseShape.ShapeType; diff = SpatialOperations.Difference(currShape, eraseShape); int numPoints = diff.numPoints; if (numPoints > 0) { resultShp = new MapWinGIS.ShapeClass(); resultShp.Create(inputSF.ShapefileType); resultShp = diff; } //difference operation successful } //intersect operation successful else { //no intersection, shapes do not collide resultShp = currShape; } } //all points of erase polygon are not inside currShape if (resultShp.numPoints > 0) { shpIndex = j; resultSF.EditDeleteShape(shpIndex); resultSF.EditInsertShape(resultShp, ref shpIndex); } } //end of bounds intersect } //end of looping through input polygons } //end of looping through erase polygons MapWinUtility.Logger.Dbg("Finsihed ErasePolySFWithPolySF"); return(true); }
/// <summary> /// Erases the portions of the polygon shapefile that are within the polygon shape. /// </summary> /// <param name="polySF">The polygon shapefile.</param> /// <param name="polygon">The erase polygon.</param> /// <param name="resultSF">The resulting shapefile, with portions removed.</param> /// <param name="CopyAttributes">Indicates whether to copy attributes or not.</param> /// <returns>False if an error was encountered, true otherwise.</returns> public static bool ErasePolySFWithPoly(ref MapWinGIS.Shapefile polySF, ref MapWinGIS.Shape polygon, ref MapWinGIS.Shapefile resultSF, bool CopyAttributes) { MapWinUtility.Logger.Dbg("ErasePolySFWithPoly(polySF: " + Macro.ParamName(polySF) + ",\n" + " polygon: " + Macro.ParamName(polygon) + ",\n" + " resultSF: " + Macro.ParamName(resultSF) + "\n" + " CopyAttributes: " + CopyAttributes.ToString()); if (polySF == null || polygon == null || resultSF == null) { gErrorMsg = "One of the input parameters is null."; Error.SetErrorMsg(gErrorMsg); Debug.WriteLine(gErrorMsg); MapWinUtility.Logger.Dbg(gErrorMsg); return(false); } if (CopyAttributes) { string tmpName; MapWinGIS.Field tmpField, currField; for (int f = 0; f <= polySF.NumFields - 1; f++) { tmpField = new MapWinGIS.Field(); currField = polySF.get_Field(f); tmpName = currField.Name; tmpField.Name = tmpName; tmpField.Width = currField.Width; tmpField.Type = currField.Type; tmpField.Precision = currField.Precision; tmpField.Key = currField.Key; resultSF.EditInsertField(tmpField, ref f, null); } } int numShapes = polySF.NumShapes; int shpIndex = 0; for (int i = 0; i <= numShapes - 1; i++) { MapWinGIS.Shape currShape = new MapWinGIS.Shape(); MapWinGIS.Shape resultShp = new MapWinGIS.Shape(); currShape = polySF.get_Shape(i); //if bounds intersect, then check if all polygon points are inside the currShape if (Globals.CheckBounds(ref currShape, ref polygon)) { int numPts = polygon.numPoints; bool allInside = true; int numParts = currShape.NumParts; if (numParts == 0) { numParts = 1; } Globals.Vertex[][] vertArray = new Globals.Vertex[numParts][]; Globals.ConvertPolyToVertexArray(ref currShape, out vertArray); for (int j = 0; j <= numPts - 1; j++) { double x = polygon.get_Point(j).x; double y = polygon.get_Point(j).y; if (Utils.PointInPoly(ref vertArray, x, y) == false) { allInside = false; break; } } if (allInside == true) { resultShp = new MapWinGIS.ShapeClass(); resultShp.Create(polygon.ShapeType); //we want the symmetric difference of these two shapes //which should leave us with a hole where the erase polygon was in the currShape resultShp = SpatialOperations.SymmetricDifference(polygon, currShape); } else { //erase overlapping section and add result to the file. MapWinGIS.Shape intersect = new MapWinGIS.ShapeClass(); intersect.ShapeType = polygon.ShapeType; intersect = SpatialOperations.Intersection(polygon, currShape); if (intersect.numPoints > 0) { //there might be parts in the difference result that do not belong, //perform an intersection operation with currShape to remove them. MapWinGIS.Shape diff = new MapWinGIS.ShapeClass(); diff.ShapeType = polygon.ShapeType; //diff = SpatialOperations.SymmetricDifference(intersect, currShape); diff = SpatialOperations.Difference(currShape, polygon); int numPoints = diff.numPoints; if (numPoints > 0) { resultShp = diff; } //difference operation successful } //intersect operation successful else { //no intersection, shapes do not collide resultShp = currShape; } } //all points of erase polygon are not inside currShape if (resultShp.numPoints > 0) { shpIndex = resultSF.NumShapes; if (resultSF.EditInsertShape(resultShp, ref shpIndex) == false) { gErrorMsg = "ErasePolySF: problem inserting shape into result file: " + resultSF.get_ErrorMsg(resultSF.LastErrorCode); Debug.WriteLine(gErrorMsg); Error.SetErrorMsg(gErrorMsg); MapWinUtility.Logger.Dbg(gErrorMsg); return(false); } if (CopyAttributes) { for (int f = 0; f <= polySF.NumFields - 1; f++) { bool tmpbool = resultSF.EditCellValue(f, shpIndex, polySF.get_CellValue(f, i)); } } } } //end of if bounds intersect else { //the erase object does not intersect with the current polygon, //add current polygon to resultSF in unchanged form shpIndex = resultSF.NumShapes; if (resultSF.EditInsertShape(currShape, ref shpIndex) == false) { gErrorMsg = "ErasePolySF: problem inserting shape into result file: " + resultSF.get_ErrorMsg(resultSF.LastErrorCode); Debug.WriteLine(gErrorMsg); Error.SetErrorMsg(gErrorMsg); MapWinUtility.Logger.Dbg(gErrorMsg); return(false); } if (CopyAttributes) { for (int f = 0; f <= polySF.NumFields - 1; f++) { bool tmpbool = resultSF.EditCellValue(f, shpIndex, polySF.get_CellValue(f, i)); } } } } //end of looping through shapes in shapefile MapWinUtility.Logger.Dbg("Finished ErasePolySFWithPoly"); return(true); }
/// <summary> /// Fills the result grid with values from the input grid. /// </summary> /// <param name="inputGF">The path to the input grid.</param> /// <param name="resultGF">The path to the result grid.</param> /// <param name="poly">The polygon used in clipping.</param> /// <param name="newNumRows">The number of rows in the result grid.</param> /// <param name="newNumCols">The number of cols in the result grid.</param> /// <param name="rowClearCount">The number of rows that should be filled before /// the unmanaged resources are disposed of.</param> /// <param name="firstCol">The first column of the original grid that corresponds to the first column of the result grid.</param> /// <param name="firstRow">The first row of the original grid that corresponds to the first row of the result grid.</param> /// <param name="firstXPt">The X value (minX) of the first point in the result grid.</param> /// <param name="firstYPt">The Y value (maxY) of the first point in the result grid.(</param> /// <param name="clipToExtents">True if clipping to extents rather than polygon shape.</param> /// <returns>False if an error was encountered, true otherwise.</returns> private static bool FillGrid(ref string inputGF, ref string resultGF, ref MapWinGIS.Shape poly, int newNumRows, int newNumCols, int rowClearCount, int firstCol, int firstRow, double firstXPt, double firstYPt, bool clipToExtents) { // System.Diagnostics.PerformanceCounter ramCounter; // ramCounter = new PerformanceCounter("Memory", "Available Bytes"); // float availableRAM = ramCounter.NextValue(); bool inRAM = true; bool status = true; int startRow = 0; int lastRow = newNumRows - 1; GridWrapper inputGW = new GridWrapper(); GridWrapper resultGW = new GridWrapper(); //variables associated with input grid: int oldNumCols = 0; double cellWidth = 0; double cellHeight = 0; double yPt = 0; int numParts = poly.NumParts; if (numParts == 0) { numParts = 1; } Globals.Vertex[][] polyVertArray = new Globals.Vertex[numParts][]; Globals.ConvertPolyToVertexArray(ref poly, out polyVertArray); for (int row = startRow; row <= lastRow; row++) { if (row == startRow) { inputGW = new GridWrapper(); if (inputGW.Open(inputGF, inRAM) == false) { status = false; break; } resultGW = new GridWrapper(); if (resultGW.Open(resultGF, inRAM) == false) { status = false; break; } if (row == 0) { //initialize variables cellWidth = inputGW.GetCellWidth(); cellHeight = inputGW.GetCellHeight(); oldNumCols = inputGW.GetNumCols(); } yPt = firstYPt - (cellHeight * row); if (FillRow(inputGW, resultGW, ref polyVertArray, row, firstXPt, yPt, firstCol, firstRow, cellWidth, newNumCols, oldNumCols, clipToExtents) == false) { status = false; break; } } //dispose of COM objects every "rowClearCount" iterations to give memory a chance to be released by GC //TODO: Calculate how often we should dispose of resources (expensive) based on //amount of RAM available and size of grid being created. else if (row == startRow + rowClearCount) { if (resultGW.Save(resultGF) == false) { status = false; break; } startRow += rowClearCount; inputGW.Dispose(); inputGW = null; resultGW.Dispose(); resultGW = null; row = startRow - 1; continue; } else { yPt = firstYPt - (cellHeight * row); //adjust y value according to current row if (FillRow(inputGW, resultGW, ref polyVertArray, row, firstXPt, yPt, firstCol, firstRow, cellWidth, newNumCols, oldNumCols, clipToExtents) == false) { status = false; break; } } } //end of looping through rows if (inputGW != null) { inputGW.Dispose(); inputGW = null; } if (resultGW != null) { if (resultGW.Save(resultGF) == false) { status = false; } resultGW.Dispose(); resultGW = null; } return(status); }