/// <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> /// Exports the shapes from the inputSF which fall within the given polygon, saving to the resultSF provided. /// </summary> /// <returns>False if an error occurs, true otherwise.</returns> public static bool SelectPointsWithPolygon(ref MapWinGIS.Shapefile inputSF, ref MapWinGIS.Shape polygon, ref MapWinGIS.Shapefile resultSF, bool SkipMWShapeID) { MapWinGIS.Utils utils = new MapWinGIS.UtilsClass(); int numPoints = inputSF.NumShapes; int shpIndex = 0; if (Globals.CopyFields(ref inputSF, ref resultSF) == false) { return(false); } for (int i = 0; i <= numPoints - 1; i++) { MapWinGIS.Point currPt = new MapWinGIS.PointClass(); currPt = inputSF.QuickPoint(i, 0); if (utils.PointInPolygon(polygon, currPt)) { shpIndex = resultSF.NumShapes; if (resultSF.EditInsertShape(inputSF.get_Shape(i), ref shpIndex) == false) { gErrorMsg = "Problem inserting shape into result file: " + resultSF.get_ErrorMsg(resultSF.LastErrorCode); Debug.WriteLine(gErrorMsg); Error.SetErrorMsg(gErrorMsg); return(false); } //add the table values int numFields = resultSF.NumFields; for (int j = 0; j <= numFields - 1; j++) { if (resultSF.EditCellValue(j, shpIndex, inputSF.get_CellValue(j, i)) == false) { gErrorMsg = "Problem inserting value into DBF table: " + resultSF.get_ErrorMsg(resultSF.LastErrorCode); Debug.WriteLine(gErrorMsg); Error.SetErrorMsg(gErrorMsg); return(false); } } //end of looping through table } //end of checking if point is inside polygon } //end of looping through points if (resultSF.NumShapes > 0) { if (resultSF.NumFields == 0 || !SkipMWShapeID) { //add the ID field and values if (Globals.DoInsertIDs(ref resultSF) == false) { return(false); } } return(true); } else { return(false); } }
/// <summary> /// Returns the shape indexes of the shapes falling within the specified polygon. /// </summary> /// <returns>False if an error occurs, true otherwise.</returns> public static bool SelectPointsWithPolygon(ref MapWinGIS.Shapefile inputSF, ref MapWinGIS.Shape polygon, ref System.Collections.ArrayList results) { results = new System.Collections.ArrayList(); MapWinGIS.Utils utils = new MapWinGIS.UtilsClass(); int numPoints = inputSF.NumShapes; for (int i = 0; i <= numPoints - 1; i++) { MapWinGIS.Point currPt = new MapWinGIS.PointClass(); currPt = inputSF.QuickPoint(i, 0); if (utils.PointInPolygon(polygon, currPt)) { results.Add(i); } //end of checking if point is inside polygon } //end of looping through points return(results.Count > 0); }
/// <summary> /// Merges two lines (at matching end points) or two polygons /// (by dissolving the common border or by combining into a multi-part polygon) /// to make one result shape. /// This version requires that both shapes be located in the same shapefile. /// </summary> /// <param name="shapes">The shapefile containing the two shapes to be merged.</param> /// <param name="indexOne">The index of the first shape.</param> /// <param name="indexTwo">The index of the second shape.</param> /// <param name="resultShp">The result of merging the shapes at indexOne and indexTwo together.</param> /// <returns>True if the shapes were merged, false otherwise.</returns> public static bool MergeShapes(ref MapWinGIS.Shapefile shapes, int indexOne, int indexTwo, out MapWinGIS.Shape resultShp) { MapWinUtility.Logger.Dbg("MergeShapes(shapes: " + Macro.ParamName(shapes) + ",\n" + " indexOne: " + indexOne.ToString() + ",\n" + " indexTwo: " + indexTwo.ToString() + ",\n" + " resultShp: resultShp)"); Error.ClearErrorLog(); MapWinGIS.ShpfileType sfType = shapes.ShapefileType; MapWinGIS.Shape mShp = new MapWinGIS.ShapeClass(); mShp.Create(sfType); bool status = false; //having a point merge function is quite pointless...but, just in case it's needed! if (sfType == MapWinGIS.ShpfileType.SHP_POINT || sfType == MapWinGIS.ShpfileType.SHP_POLYGONM || sfType == MapWinGIS.ShpfileType.SHP_POLYGONZ) { MapWinGIS.Point pt1 = new MapWinGIS.PointClass(); pt1 = shapes.QuickPoint(indexOne, 0); MapWinGIS.Point pt2 = new MapWinGIS.PointClass(); pt2 = shapes.QuickPoint(indexTwo, 0); status = MergePoints(ref pt1, ref pt2, sfType, out mShp); while (Marshal.ReleaseComObject(pt1) != 0) { ; } pt1 = null; while (Marshal.ReleaseComObject(pt2) != 0) { ; } pt2 = null; resultShp = mShp; MapWinUtility.Logger.Dbg("Finished MergeShapes"); return(status); } //end of merging points //merge polygons else if (sfType == MapWinGIS.ShpfileType.SHP_POLYGON || sfType == MapWinGIS.ShpfileType.SHP_POLYGONM || sfType == MapWinGIS.ShpfileType.SHP_POLYGONZ) { MapWinGIS.Shape poly1 = new MapWinGIS.ShapeClass(); poly1 = shapes.get_Shape(indexOne); MapWinGIS.Shape poly2 = new MapWinGIS.ShapeClass(); poly2 = shapes.get_Shape(indexTwo); status = MergePolygons(ref poly1, ref poly2, sfType, out mShp); while (Marshal.ReleaseComObject(poly1) != 0) { ; } poly1 = null; while (Marshal.ReleaseComObject(poly2) != 0) { ; } poly2 = null; resultShp = mShp; MapWinUtility.Logger.Dbg("Finished MergeShapes"); return(status); } //end of merging polygons //Merge lines by joining them at common endpoints else if (sfType == MapWinGIS.ShpfileType.SHP_POLYLINE || sfType == MapWinGIS.ShpfileType.SHP_POLYLINEM || sfType == MapWinGIS.ShpfileType.SHP_POLYLINEZ) { MapWinGIS.Shape line1 = new MapWinGIS.ShapeClass(); line1 = shapes.get_Shape(indexOne); MapWinGIS.Shape line2 = new MapWinGIS.ShapeClass(); line2 = shapes.get_Shape(indexTwo); status = MergeLines(ref line1, ref line2, sfType, out mShp); if (line1 != null) { while (Marshal.ReleaseComObject(line1) != 0) { ; } } line1 = null; if (line2 != null) { while (Marshal.ReleaseComObject(line2) != 0) { ; } } line2 = null; resultShp = mShp; MapWinUtility.Logger.Dbg("Finished MergeShapes"); return(status); } //end of merging lines else { gErrorMsg = "Unknown shapefile type, aborting call to ShapeMerge."; Debug.WriteLine(gErrorMsg); Error.SetErrorMsg(gErrorMsg); resultShp = mShp; MapWinUtility.Logger.Dbg(gErrorMsg); return(false); } }
/// <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); }