Пример #1
0
        /// <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);
        }
        }         // End of 'public void ItemClickedEvent(string ItemName, ref bool Handled)'

        private void RemoveShapes()
        {
            try
            {
                System.Collections.SortedList arr = new System.Collections.SortedList();
                for (int i = 0; i < g.MapWin.View.SelectedShapes.NumSelected; i++)
                {
                    arr.Add(g.MapWin.View.SelectedShapes[i].ShapeIndex, g.MapWin.View.SelectedShapes[i].ShapeIndex);
                }

                if (arr.Count > 0)
                {
                    if (MapWinUtility.Logger.Message("Do you wish to permanently remove the selected shapes?", "Confirm delete", System.Windows.Forms.MessageBoxButtons.YesNo, System.Windows.Forms.MessageBoxIcon.Question, System.Windows.Forms.DialogResult.Yes) == System.Windows.Forms.DialogResult.Yes)
                    {
                        // Deletion has been confirmed.
                        // actually delete the shapes now
                        g.MapWin.View.ClearSelectedShapes();
                        MapWinGIS.Shapefile sf = g.CurrentLayer;

                        g.CreateUndoPoint();

                        if (sf.StartEditingShapes(true, null))
                        {
                            System.Diagnostics.Debug.WriteLine(sf.get_ErrorMsg(sf.LastErrorCode));
                            for (int j = arr.Count - 1; j >= 0; j--)
                            {
                                if (sf.EditDeleteShape((int)arr.GetByIndex(j)) == false)
                                {
                                    System.Diagnostics.Debug.WriteLine(sf.get_ErrorMsg(sf.LastErrorCode));
                                }
                            }

                            if (sf.StopEditingShapes(true, true, null) == false)
                            {
                                MapWinUtility.Logger.Message("Failed to save the changes that were made.", "Error", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Error, System.Windows.Forms.DialogResult.OK);
                            }

                            g.MapWin.Plugins.BroadcastMessage("ShapefileEditor: Layer " + g.MapWin.Layers.CurrentLayer.ToString() + ": Shape Deleted");
                        }

                        //added 1/29/2005
                        g.MapWin.Toolbar.ButtonItem("RemoveShapeButton").Enabled = false;
                        g.MapWin.Toolbar.ButtonItem("RemoveShapeButton").Tooltip = "Remove shapes (a shape must be selected)";

                        g.UpdateView();
                    }
                }
            }
            catch (System.Exception ex)
            {
                System.Diagnostics.Debug.WriteLine(ex.Message.ToString());
            }
        } // end void RemoveShapes()
Пример #3
0
        /// <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);
        }