Esempio n. 1
0
        /// <summary>
        /// Draws selected bounds on map
        /// </summary>
        /// <param name="ext">Bounding box to search CS</param>
        public void DrawSelectedBounds(MapWinGIS.Extents ext)
        {
            this.RemoveLayer(m_handleBounds);

            MapWinGIS.Shapefile sf = new MapWinGIS.Shapefile();
            if (sf.CreateNew("", MapWinGIS.ShpfileType.SHP_POLYGON))
            {
                MapWinGIS.Shape shp = new MapWinGIS.Shape();
                shp.Create(MapWinGIS.ShpfileType.SHP_POLYGON);
                this.InsertPart(shp, ext.xMin, ext.xMax, ext.yMin, ext.yMax);

                int index = 0;
                sf.EditInsertShape(shp, ref index);

                m_handleBounds = this.AddLayer(sf, true);

                MapWinGIS.ShapeDrawingOptions options = sf.DefaultDrawingOptions;
                MapWinGIS.Utils ut = new MapWinGIS.Utils();
                options.FillColor        = ut.ColorByName(MapWinGIS.tkMapColor.Orange);
                options.LineColor        = ut.ColorByName(MapWinGIS.tkMapColor.Orange);
                options.LineWidth        = 3;
                options.LineStipple      = MapWinGIS.tkDashStyle.dsDash;
                options.FillTransparency = 100;
            }
            else
            {
                System.Diagnostics.Debug.Print("Failed to create in-memory shapefile");
            }
        }
Esempio n. 2
0
        // Add the shapes one by one
        private bool AddShapes(MapWinGIS.Shapefile inSF, out string errorMessage)
        {
            errorMessage = "No Error.";
            int outShp = _outSF.NumShapes;

            // Add all the shapes and attributes from the first shapefile
            for (int shp = 0; shp < inSF.NumShapes; shp++)
            {
                MapWinGIS.Shape shape = inSF.get_Shape(shp);
                _outSF.EditInsertShape(shape, ref outShp);
                for (int fld = 0; fld < inSF.NumFields; fld++)
                {
                    string name  = inSF.get_Field(fld).Name;
                    int    index = IndexOf(name);
                    if (index == -1)
                    {
                        errorMessage = "Could not find a field in the output shapefile: [" + name + "]";
                        return(false);
                    }
                    if (_outSF.EditCellValue(index, outShp, inSF.get_CellValue(fld, shp)) == false)
                    {
                        errorMessage = _outSF.get_ErrorMsg(_outSF.LastErrorCode);
                        return(false);
                    }
                }
                outShp++;
            }
            return(true);
        }
        private void AddRectangle(double LeftmostX, double LowestY, double RightmostX, double HighestY)
        {
            MapWinGIS.Shape newPolygon = new MapWinGIS.Shape();

            int partIndex = 0;

            newPolygon.Create(MapWinGIS.ShpfileType.SHP_POLYGON);
            newPolygon.InsertPart(0, ref partIndex);

            int shpIndex = 0;

            MapWinGIS.Point newPt = new MapWinGIS.Point();
            newPt.x = LeftmostX;
            newPt.y = HighestY;
            newPt.Z = 0;
            newPolygon.InsertPoint(newPt, ref shpIndex);

            newPt   = new MapWinGIS.Point();
            newPt.x = RightmostX;
            newPt.y = HighestY;
            newPt.Z = 0;
            newPolygon.InsertPoint(newPt, ref shpIndex);

            newPt   = new MapWinGIS.Point();
            newPt.x = RightmostX;
            newPt.y = LowestY;
            newPt.Z = 0;
            newPolygon.InsertPoint(newPt, ref shpIndex);

            newPt   = new MapWinGIS.Point();
            newPt.x = LeftmostX;
            newPt.y = LowestY;
            newPt.Z = 0;
            newPolygon.InsertPoint(newPt, ref shpIndex);

            // Finalize -- add it.
            MapWinGIS.Shapefile sf = (MapWinGIS.Shapefile)g.MapWin.Layers[g.MapWin.Layers.CurrentLayer].GetObject();
            if (!sf.EditingShapes || !sf.EditingTable)
            {
                sf.StartEditingShapes(true, null);
            }

            int addedShapes = sf.NumShapes;

            sf.EditInsertShape(newPolygon, ref addedShapes);

            g.CreateUndoPoint();

            sf.StopEditingShapes(true, true, null);

            // Release memory used by point reallocations above
            GC.Collect();

            // And show it:
            g.UpdateView();

            g.MapWin.Plugins.BroadcastMessage("ShapefileEditor: Layer " + g.MapWin.Layers.CurrentLayer.ToString() + ": New Shape Added");
        }
Esempio n. 4
0
        /// <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);
            }
        }
        private void AddEllipse(double LeftmostX, double LowestY, double RightmostX, double HighestY)
        {
            MapWinGIS.Shape newPolygon = new MapWinGIS.Shape();

            int partIndex = 0;

            newPolygon.Create(MapWinGIS.ShpfileType.SHP_POLYGON);
            newPolygon.InsertPart(0, ref partIndex);

            int shpIndex = 0;

            double t, a, b, tinc, centx, centy;

            a     = RightmostX - LeftmostX;
            b     = HighestY - LowestY;
            tinc  = Math.PI * 2 / (a + b);
            centx = (LeftmostX + RightmostX) * .5;
            centy = (LowestY + HighestY) * .5;

            MapWinGIS.Point newPt = new MapWinGIS.Point();
            newPt.x = centx + a;
            newPt.y = centy;
            newPt.Z = 0;
            newPolygon.InsertPoint(newPt, ref shpIndex);

            for (t = 0; t < Math.PI * 2; t += tinc)
            {
                MapWinGIS.Point nextPt = new MapWinGIS.Point();
                nextPt.x = centx + a * Math.Cos(t);
                nextPt.y = centy - b * Math.Sin(t);
                nextPt.Z = 0;
                newPolygon.InsertPoint(nextPt, ref shpIndex);
            }

            // Finalize -- add it.
            MapWinGIS.Shapefile sf = (MapWinGIS.Shapefile)g.MapWin.Layers[g.MapWin.Layers.CurrentLayer].GetObject();
            if (!sf.EditingShapes || !sf.EditingTable)
            {
                sf.StartEditingShapes(true, null);
            }

            int addedShapes = sf.NumShapes;

            sf.EditInsertShape(newPolygon, ref addedShapes);

            g.CreateUndoPoint();

            sf.StopEditingShapes(true, true, null);

            // And show it:
            g.UpdateView();

            g.MapWin.Plugins.BroadcastMessage("ShapefileEditor: Layer " + g.MapWin.Layers.CurrentLayer.ToString() + ": New Shape Added");
        }
Esempio n. 6
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);
        }
        private void AddRegularPolygon(int sides, double sidelength, double centroidX, double centroidY)
        {
            double pi      = 3.1415926535897932384626433832795;
            double apothem = sidelength / (2 * Math.Tan(pi / sides));
            double radius  = sidelength / (2 * Math.Sin(pi / sides));

            MapWinGIS.Shape newPolygon = new MapWinGIS.Shape();

            int partIndex = 0;

            newPolygon.Create(MapWinGIS.ShpfileType.SHP_POLYGON);
            newPolygon.InsertPart(0, ref partIndex);

            double i        = 0;
            int    shpIndex = 0;

            // Approximate the polygon with n line segments
            double increment = ((2 * pi) / sides);

            for (; i <= 2 * pi; i += increment, shpIndex++)
            {
                MapWinGIS.Point newPt = new MapWinGIS.Point();

                newPt.x = (radius * Math.Cos(i) + centroidX);
                newPt.y = (radius * Math.Sin(i) + centroidY);
                newPt.Z = 0;

                newPolygon.InsertPoint(newPt, ref shpIndex);
            }

            // Finalize -- add it.
            MapWinGIS.Shapefile sf = (MapWinGIS.Shapefile)g.MapWin.Layers[g.MapWin.Layers.CurrentLayer].GetObject();
            if (!sf.EditingShapes || !sf.EditingTable)
            {
                sf.StartEditingShapes(true, null);
            }

            int addedShapes = sf.NumShapes;

            sf.EditInsertShape(newPolygon, ref addedShapes);

            g.CreateUndoPoint();

            sf.StopEditingShapes(true, true, null);

            // And show it:
            g.UpdateView();

            g.MapWin.Plugins.BroadcastMessage("ShapefileEditor: Layer " + g.MapWin.Layers.CurrentLayer.ToString() + ": New Shape Added");
        }
Esempio n. 8
0
        /// <summary>
        /// Draws coordinate system at map
        /// </summary>
        /// <param name="cs">The territory (coordinate system or country) to draw</param>
        public void DrawCoordinateSystem(Territory cs)
        {
            this.RemoveLayer(m_handleCS);

            MapWinGIS.Shapefile sf = new MapWinGIS.Shapefile();
            sf.CreateNew("", MapWinGIS.ShpfileType.SHP_POLYGON);
            MapWinGIS.Shape shp = new MapWinGIS.Shape();
            shp.Create(MapWinGIS.ShpfileType.SHP_POLYGON);

            double xMax = cs.Right;
            double xMin = cs.Left;
            double yMin = cs.Bottom;
            double yMax = cs.Top;

            if (xMax < xMin)
            {
                InsertPart(shp, -180, xMax, yMin, yMax);
                InsertPart(shp, xMin, 180, yMin, yMax);
                System.Diagnostics.Debug.Print(shp.NumParts.ToString());
            }
            else
            {
                InsertPart(shp, xMin, xMax, yMin, yMax);
            }

            int shpIndex = sf.NumShapes;

            sf.EditInsertShape(shp, ref shpIndex);

            m_handleCS = this.AddLayer(sf, true);

            MapWinGIS.Utils utils = new MapWinGIS.Utils();
            sf.DefaultDrawingOptions.FillColor        = utils.ColorByName(MapWinGIS.tkMapColor.LightBlue);
            sf.DefaultDrawingOptions.FillTransparency = 120;
            sf.DefaultDrawingOptions.LineColor        = utils.ColorByName(MapWinGIS.tkMapColor.Blue);
            sf.DefaultDrawingOptions.LineStipple      = MapWinGIS.tkDashStyle.dsDash;
            sf.DefaultDrawingOptions.LineWidth        = 2;

            this.Redraw();
        }
Esempio n. 9
0
        /// <summary>
        /// Removes portions of the lineSF that fall within the erase polygon
        /// </summary>
        /// <param name="lineSF">The shapefile of lines to be erased.</param>
        /// <param name="erasePoly">The polygon to be used for erasing portion of the line shapefile.</param>
        /// <param name="resultSF">The resulting line shapefile with portions removed.</param>
        /// <param name="CopyAttributes">Indicates whether to copy attributes</param>
        /// <returns>False if an error was encountered, true otherwise.</returns>
        public static bool EraseLineSFWithPoly(ref MapWinGIS.Shapefile lineSF, ref MapWinGIS.Shape erasePoly, ref MapWinGIS.Shapefile resultSF, bool CopyAttributes)
        {
            MapWinUtility.Logger.Dbg("EraseLineSFWithPoly(lineSF: " + Macro.ParamName(lineSF) + ",\n" +
                                     "                    erasePoly: " + Macro.ParamName(erasePoly) + ",\n" +
                                     "                    resultSF: " + Macro.ParamName(resultSF) + ",\n" +
                                     "                    CopyAttributes: " + CopyAttributes.ToString() + ")");
            if (lineSF == null || erasePoly == 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 <= lineSF.NumFields - 1; f++)
                {
                    tmpField      = new MapWinGIS.Field();
                    currField     = lineSF.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 shpIndex = 0;
            int numLines = lineSF.NumShapes;

            for (int i = 0; i <= numLines - 1; i++)
            {
                MapWinGIS.Shape currLine = new MapWinGIS.ShapeClass();
                currLine.Create(lineSF.ShapefileType);
                currLine = lineSF.get_Shape(i);

                MapWinGIS.Shape lineEnvelope = new MapWinGIS.ShapeClass();
                lineEnvelope.Create(MapWinGIS.ShpfileType.SHP_POLYGON);
                //create lineExtents' points out of the line extent points
                MapWinGIS.Point lTop, rTop, rBottom, lBottom;
                lTop      = new MapWinGIS.PointClass();
                lTop.x    = currLine.Extents.xMin;
                lTop.y    = currLine.Extents.yMax;
                rTop      = new MapWinGIS.PointClass();
                rTop.x    = currLine.Extents.xMax;
                rTop.y    = currLine.Extents.yMax;
                rBottom   = new MapWinGIS.PointClass();
                rBottom.x = currLine.Extents.xMax;
                rBottom.y = currLine.Extents.yMin;
                lBottom   = new MapWinGIS.PointClass();
                lBottom.x = currLine.Extents.xMin;
                lBottom.y = currLine.Extents.yMin;
                //now add the extent points to the new polygon shape: lineEnvelope
                int ptIndex = 0;
                lineEnvelope.InsertPoint(lTop, ref ptIndex);
                ptIndex++;
                lineEnvelope.InsertPoint(rTop, ref ptIndex);
                ptIndex++;
                lineEnvelope.InsertPoint(rBottom, ref ptIndex);
                ptIndex++;
                lineEnvelope.InsertPoint(lBottom, ref ptIndex);
                ptIndex++;
                lineEnvelope.InsertPoint(lTop, ref ptIndex);
                //remove COM points from memory
                while (Marshal.ReleaseComObject(lTop) != 0)
                {
                    ;
                }
                while (Marshal.ReleaseComObject(rTop) != 0)
                {
                    ;
                }
                while (Marshal.ReleaseComObject(rBottom) != 0)
                {
                    ;
                }
                while (Marshal.ReleaseComObject(lBottom) != 0)
                {
                    ;
                }

                //Check if line extents and polygon extents overlap
                if (Globals.CheckBounds(ref lineEnvelope, ref erasePoly))
                {
                    //make the envelope polygon slightly larger
                    MapWinGIS.Shape lgEnvelope = new MapWinGIS.ShapeClass();
                    lgEnvelope.Create(MapWinGIS.ShpfileType.SHP_POLYGON);
                    SpatialOperations.BufferPolygon(ref lineEnvelope, 0.5, Enumerations.Buffer_HoleTreatment.Ignore, Enumerations.Buffer_CapStyle.Pointed, out lgEnvelope);
                    //take the difference of the envelope polygon with the erase polygon.
                    MapWinGIS.Shape diff = new MapWinGIS.ShapeClass();
                    diff.Create(MapWinGIS.ShpfileType.SHP_POLYGON);
                    diff = SpatialOperations.Difference(lgEnvelope, erasePoly);
                    if (diff.numPoints > 0)
                    {
                        //the difference shape represents the line envelope
                        //minus the area of the erase polygon.
                        MapWinGIS.Shapefile inputLine = new MapWinGIS.ShapefileClass();
                        string tempPath = System.IO.Path.GetTempPath() + "tempInputLine.shp";
                        //CDM 8/4/2006 inputLine.CreateNew(tempPath, lineSF.ShapefileType);
                        Globals.PrepareResultSF(ref tempPath, ref inputLine, lineSF.ShapefileType);
                        shpIndex = 0;
                        inputLine.EditInsertShape(currLine, ref shpIndex);

                        int numParts = diff.NumParts;
                        if (numParts == 0)
                        {
                            numParts = 1;
                        }

                        if (numParts > 1)
                        {
                            //separate and test each part individually
                            MapWinGIS.Shape[] diffParts = new MapWinGIS.Shape[numParts];
                            Globals.SeparateParts(ref diff, out diffParts);
                            for (int j = 0; j <= numParts - 1; j++)
                            {
                                //don't check inside of holes
                                if (Globals.IsClockwise(ref diffParts[j]))
                                {
                                    MapWinGIS.Shapefile tempLineResult = new MapWinGIS.ShapefileClass();
                                    string tempLineFile = System.IO.Path.GetTempPath() + "tempLines.shp";
                                    DataManagement.DeleteShapefile(ref tempLineFile);
                                    //CDM 8/4/2006 tempLineResult.CreateNew(tempLineFile, lineSF.ShapefileType);
                                    Globals.PrepareResultSF(ref tempLineFile, ref tempLineResult, lineSF.ShapefileType);
                                    tempLineResult.StartEditingShapes(true, null);

                                    SpatialOperations.ClipShapesWithPolygon(ref inputLine, ref diffParts[j], out tempLineResult, false);

                                    int numResults = tempLineResult.NumShapes;
                                    if (numResults > 0)
                                    {
                                        //add results to the final result file.
                                        for (int k = 0; k <= numResults - 1; k++)
                                        {
                                            shpIndex = resultSF.NumShapes;
                                            resultSF.EditInsertShape(tempLineResult.get_Shape(k), ref shpIndex);
                                            if (CopyAttributes)
                                            {
                                                for (int f = 0; f <= lineSF.NumFields - 1; f++)
                                                {
                                                    bool tmpbool = resultSF.EditCellValue(f, shpIndex, lineSF.get_CellValue(f, i));
                                                }
                                            }
                                        }
                                    }                    //clipping successful
                                }                        //done checking islands
                            }                            //done looping through parts of the difference shape
                        }
                        else
                        {
                            MapWinGIS.Shapefile tempLineResult = new MapWinGIS.ShapefileClass();
                            string tempLineFile = System.IO.Path.GetTempPath() + "tempLines.shp";
                            DataManagement.DeleteShapefile(ref tempLineFile);
                            //CDM 8/4/2006 tempLineResult.CreateNew(tempLineFile, lineSF.ShapefileType);
                            Globals.PrepareResultSF(ref tempLineFile, ref tempLineResult, lineSF.ShapefileType);

                            tempLineResult.StartEditingShapes(true, null);

                            SpatialOperations.ClipShapesWithPolygon(ref inputLine, ref diff, out tempLineResult, false);

                            int numResults = tempLineResult.NumShapes;
                            if (numResults > 0)
                            {
                                //add results to the final result file.
                                for (int k = 0; k <= numResults - 1; k++)
                                {
                                    shpIndex = resultSF.NumShapes;
                                    resultSF.EditInsertShape(tempLineResult.get_Shape(k), ref shpIndex);
                                    if (CopyAttributes)
                                    {
                                        for (int f = 0; f <= lineSF.NumFields - 1; f++)
                                        {
                                            bool tmpbool = resultSF.EditCellValue(f, shpIndex, lineSF.get_CellValue(f, i));
                                        }
                                    }
                                }
                            }    //clipping successful
                        }
                    }            //difference operation successful
                }                //bounds overlapped
                else
                {
                    shpIndex = resultSF.NumShapes;
                    resultSF.EditInsertShape(currLine, ref shpIndex);
                    if (CopyAttributes)
                    {
                        for (int f = 0; f <= lineSF.NumFields - 1; f++)
                        {
                            bool tmpbool = resultSF.EditCellValue(f, shpIndex, lineSF.get_CellValue(f, i));
                        }
                    }
                }
            }            //end of looping through lines in the input shapefile
            MapWinUtility.Logger.Dbg("Finished EraseLineSFWithPoly");
            return(true);
        }
Esempio n. 10
0
        /// <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);
        }
Esempio n. 11
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);
        }
Esempio n. 12
0
        /// <summary>
        /// Creates shapefile from specified database table.
        /// Geometry or GeometryText columns are expected
        /// </summary>
        private MapWinGIS.Shapefile LoadShapefile(string sql)
        {
            this.CheckConnection();

            MapWinGIS.Shapefile sf = null;

            if (m_callback != null)
            {
                m_callback.Progress("", 0, "Extracting data...");
            }

            System.Diagnostics.Stopwatch watch = new System.Diagnostics.Stopwatch();
            watch.Start();

            DbDataAdapter adapter = this.CreateAdapter();

            adapter.SelectCommand             = this.CreateCommand();
            adapter.SelectCommand.Connection  = m_connection;
            adapter.SelectCommand.CommandText = sql;

            DataTable dt = new DataTable();

            adapter.Fill(dt);

            int index = dt.Columns.IndexOf("Geometry");

            if (index >= 0 && dt.Rows.Count > 0)
            {
                // get shapefile type by the first shape
                MapWinGIS.ShpfileType shpType;
                MapWinGIS.Shape       shape = new MapWinGIS.Shape();
                if (shape.ImportFromBinary(dt.Rows[0][index]))
                {
                    shpType = shape.ShapeType;
                }
                else
                {
                    return(null);
                }

                sf = new MapWinGIS.Shapefile();
                sf.CreateNew("", shpType);

                foreach (DataColumn column in dt.Columns)
                {
                    MapWinGIS.Field field = new MapWinGIS.Field();
                    field.Name = column.ColumnName;
                    switch (column.DataType.Name.ToLower())
                    {
                    case "int32":
                    case "int64":
                        field.Type = MapWinGIS.FieldType.INTEGER_FIELD;
                        break;

                    case "double":
                        field.Type = MapWinGIS.FieldType.DOUBLE_FIELD;
                        break;

                    case "string":
                        field.Type = MapWinGIS.FieldType.STRING_FIELD;
                        break;

                    case "byte[]":
                        continue;

                    default:
                        continue;
                    }
                    int  fieldIndex = sf.NumFields;
                    bool result     = sf.EditInsertField(field, ref fieldIndex, null);
                    System.Diagnostics.Debug.Print(result.ToString());
                }

                int shapeIndex = dt.Rows.Count;
                shape = null;
                int percent = 0;
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    if (m_callback != null && dt.Rows.Count > 1)
                    {
                        int newPercent = Convert.ToInt32((double)i / (double)(dt.Rows.Count - 1) * 100.0);
                        if (newPercent > percent)
                        {
                            m_callback.Progress("", newPercent, "Creating shapefile...");
                            percent = newPercent;
                        }
                    }

                    DataRow row = dt.Rows[i];

                    shape = new MapWinGIS.Shape();
                    if (shape.ImportFromBinary(row[index]))
                    {
                        shapeIndex = sf.NumShapes;
                        if (sf.EditInsertShape(shape, ref shapeIndex))
                        {
                            for (int j = 0; j < sf.NumFields; j++)
                            {
                                sf.EditCellValue(j, shapeIndex, row[j + 1]);
                            }
                        }
                    }
                }

                if (m_callback != null)
                {
                    m_callback.Progress("", 100, "");
                }
            }

            watch.Stop();
            Debug.Print("Finished: " + watch.Elapsed.ToString());

            return(sf);
        }
Esempio n. 13
0
        /// <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 SelectLinesWithPolygon(ref MapWinGIS.Shapefile inputSF, ref MapWinGIS.Shape polygon, ref MapWinGIS.Shapefile resultSF, bool SkipMWShapeID)
        {
            int numLines = inputSF.NumShapes;
            int shpIndex = 0;

            // Boundary intersection test variables
            double xMin1, xMax1, yMin1, yMax1, zMin1, zMax1, xMin2, xMax2, yMin2, yMax2, zMin2, zMax2;

            // Get the masking polygon's boundaries only once:
            polygon.Extents.GetBounds(out xMin2, out yMin2, out zMin2, out xMax2, out yMax2, out zMax2);

            if (Globals.CopyFields(ref inputSF, ref resultSF) == false)
            {
                return(false);
            }

            MapWinGIS.Shape currLine;
            MapWinGIS.Point currPt;
            for (int i = 0; i <= numLines - 1; i++)
            {
                currLine = inputSF.get_Shape(i);
                currLine.Extents.GetBounds(out xMin1, out yMin1, out zMin1, out xMax1, out yMax1, out zMax1);

                // Are the boundaries intersecting?
                if (!(xMin1 > xMax2 || xMax1 < xMin2 || yMin1 > yMax2 || yMax1 < yMin2))
                {
                    //lines are nasty, just because the boundaries intersect it
                    //doesn't mean the line enters the polygon
                    //do a quick point check before doing a more thorough investigation

                    int  numPoints = currLine.numPoints;
                    bool ptInside  = false;
                    for (int j = 0; j <= numPoints - 1; j++)
                    {
                        currPt = currLine.get_Point(j);
                        if (polygon.PointInThisPoly(currPt))
                        {
                            ptInside = true;
                            break;
                        }
                    }
                    if (ptInside)
                    {
                        //we know part of the line is inside the polygon so add line to result file
                        shpIndex = resultSF.NumShapes;
                        if (resultSF.EditInsertShape(currLine, 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 ptInside check
                    else
                    {
                        // Avoid using a temp file to test each individual file;
                        // instead, just see if the line crosses the polygon
                        if (LineCrossesPoly(ref currLine, ref polygon))
                        {
                            //part of the line lies within the polygon, add to result file
                            shpIndex = resultSF.NumShapes;
                            if (resultSF.EditInsertShape(currLine, 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 successful cross
                    }         //end of else no points were found inside polygon
                }             //end of checking bounds
            }                 //end of looping through lines

            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);
            }
        }
Esempio n. 14
0
        private void AddShape(ShapeClass s)
        {
            if (m_SFType == MapWindow.Interfaces.eLayerType.LineShapefile)
            {
                if (s.NumPoints < 2)
                {
                    ResetForNew();
                    MapWinUtility.Logger.Message("You must add at least two points for a line.", "Not Enough Points", MessageBoxButtons.OK, MessageBoxIcon.Exclamation, DialogResult.OK);
                    return;
                }
            }
            else if (m_SFType == MapWindow.Interfaces.eLayerType.PolygonShapefile)
            {
                // 3 + 1 for termination point - first must equal last, meaning we have to have 4 for a polygon
                if (s.NumPoints < 4)
                {
                    ResetForNew();
                    MapWinUtility.Logger.Message("You must add at least three points for a polygon.", "Not Enough Points", MessageBoxButtons.OK, MessageBoxIcon.Exclamation, DialogResult.OK);
                    return;
                }
            }

            if (m_sf.EditingShapes == false)
            {
                if (m_sf.StartEditingShapes(true, null) == false)
                {
                    MapWinUtility.Logger.Msg("Could not edit the shapefile.", "Error");
                    if (!GlobalFunctions.m_StayInAddMode)
                    {
                        this.Close();
                    }
                    else
                    {
                        ResetForNew();
                    }
                    return;
                }
            }

            int newIndex = m_sf.NumShapes;

            MapWinGIS.Shape shp = s.ToMWShape(m_sf.ShapefileType);
            //MapWinGIS.ShapefileColorScheme cs = m_globals.MapWin.Layers[m_globals.MapWin.Layers.CurrentLayer].ColoringScheme;
            if (m_sf.EditInsertShape(shp, ref newIndex) == false)
            {
                MapWinUtility.Logger.Msg("Failed to add the new shape to the shapefile.", "Error");
            }
            else
            {
                if (m_snapper != null)
                {
                    m_snapper.AddShapeData(newIndex, m_globals.MapWin.Layers.CurrentLayer);
                }

                // Save
                if (m_sf.EditingShapes == true)
                {
                    m_globals.CreateUndoPoint();
                    m_sf.StopEditingShapes(true, true, null);
                }

                m_globals.MapWin.Plugins.BroadcastMessage("ShapefileEditor: Layer " + m_globals.MapWin.Layers.CurrentLayer.ToString() + ": New Shape Added");
            }
            m_globals.MapWin.View.Draw.ClearDrawing(m_drawHandle);
            if (!GlobalFunctions.m_StayInAddMode)
            {
                this.Close();
            }
            else
            {
                ResetForNew();
            }
        }
Esempio n. 15
0
        /// <summary>
        /// This is used by the Identity process to export all shapes intesecting all polygons passed in.
        /// Each identity polygon is used to select and clip input shapes. The new clipped shape is written
        /// to the result shapefile. The attribute fields from both input and identity shapefiles are copied
        /// to the result shapefile.
        /// This process uses a QuadTree index to speed up the selection of overlapping geometries.
        /// </summary>
        /// <param name="inputSF">The shapefile, of any geometry type, to be clipped and exported.</param>
        /// <param name="identitySF">The polygon shapefile used to clip the inputSF.</param>
        /// <param name="resultSF">The result shapefile that will contain the results.</param>
        /// <returns>False if an error occurs, true otherwise.</returns>
        public static bool ExportShapesWithPolygons(ref MapWinGIS.Shapefile inputSF, ref MapWinGIS.Shapefile identitySF, ref MapWinGIS.Shapefile resultSF)
        {
            try
            {
                // Boundary intersection test variables
                double xMin1, xMax1, yMin1, yMax1, zMin1, zMax1, xMin2, xMax2, yMin2, yMax2, zMin2, zMax2;

                // Build Quadtree index for inputSF
                MapWinGeoProc.NTS.Topology.Index.Quadtree.Quadtree myQuadTree = new MapWinGeoProc.NTS.Topology.Index.Quadtree.Quadtree();

                MapWinGIS.Shape currGeom;
                for (int i = 0; i < inputSF.NumShapes; i++)
                {
                    currGeom = inputSF.get_Shape(i);
                    currGeom.Extents.GetBounds(out xMin1, out yMin1, out zMin1, out xMax1, out yMax1, out zMax1);
                    Envelope myItemEnv = new Envelope(xMin1, xMax1, yMin1, yMax1);
                    myQuadTree.Insert(myItemEnv, i);
                }//end of looping through lines

                // Copy inputSf and identitySF fields to resultSF, renaming duplicate fields
                if (Globals.CopyFields(ref inputSF, ref resultSF) == false)
                {
                    return(false);
                }
                if (Globals.CopyFields(ref identitySF, ref resultSF, true) == false)
                {
                    return(false);
                }

                int             resultNumFields             = resultSF.NumFields;
                int             inputNumFields              = inputSF.NumFields;
                int             identityNumFields           = identitySF.NumFields;
                int             shpIndex                    = 0;
                Envelope        myQueryEnv                  = null;
                Geometry        queryPoly                   = null;
                Geometry        inputGeom                   = null;
                IGeometry       intersectGeom               = null;
                MapWinGIS.Shape identityShape               = null;
                MapWinGIS.Shape intersectShape              = null;
                IList           results                     = null;
                string          progressmessage             = "";
                int             inputShapesForIdentityShape = 0;

                // Loop through identitySF and get inputSF geometries that intersect
                for (int identityIndex = 0; identityIndex < identitySF.NumShapes; identityIndex++)
                {
                    identityShape = identitySF.get_Shape(identityIndex);
                    queryPoly     = NTS_Adapter.ShapeToGeometry(identityShape);
                    identityShape.Extents.GetBounds(out xMin2, out yMin2, out zMin2, out xMax2, out yMax2, out zMax2);
                    myQueryEnv = new Envelope(xMin2, xMax2, yMin2, yMax2);

                    //use quadtree index to find geometries that may intersect
                    results = myQuadTree.Query(myQueryEnv);
                    int intersectIndex = 0;
                    for (int i = 0; i < results.Count; i++)
                    {
                        intersectIndex = Convert.ToInt32(results[i]);                                                                                                                                                                          // Get input id from quadtree results
                        currGeom       = inputSF.get_Shape(intersectIndex);                                                                                                                                                                    // Get input geometry
                        inputGeom      = NTS_Adapter.ShapeToGeometry(currGeom);                                                                                                                                                                // Convert to NTS Geometry
                        for (int inputGoemIndex = 0; inputGoemIndex < inputGeom.NumGeometries; inputGoemIndex++)                                                                                                                               //use each part of the geometry
                        {
                            if (inputGeom.GetGeometryN(inputGoemIndex).Intersects(queryPoly))                                                                                                                                                  // check for intersection
                            {
                                intersectGeom = MapWinGeoProc.NTS.Topology.Operation.Overlay.OverlayOp.Overlay(queryPoly, inputGeom.GetGeometryN(inputGoemIndex), MapWinGeoProc.NTS.Topology.Operation.Overlay.SpatialFunctions.Intersection); // create intersect geometry
                                if (!intersectGeom.IsEmpty)
                                {
                                    for (int geomIndex = 0; geomIndex < intersectGeom.NumGeometries; geomIndex++)                                          // process each part of intersect result
                                    {
                                        if (inputGeom.GetGeometryN(inputGoemIndex).GetType().Name == intersectGeom.GetGeometryN(geomIndex).GetType().Name) // only used geometries of the same type as the input.
                                        {
                                            // Write shape geometry
                                            intersectShape = NTS_Adapter.GeometryToShape(intersectGeom.GetGeometryN(geomIndex));
                                            shpIndex       = resultSF.NumShapes;
                                            if (resultSF.EditInsertShape(intersectShape, ref shpIndex) == false)
                                            {
                                                gErrorMsg = string.Format("Problem inserting shape into result file: {0}, Input Id: {1}, IdentityId: {2}", resultSF.get_ErrorMsg(resultSF.LastErrorCode), intersectIndex, identityIndex);
                                                Debug.WriteLine(gErrorMsg);
                                                Error.SetErrorMsg(gErrorMsg);
                                                return(false);
                                            }
                                            inputShapesForIdentityShape++;
                                            intersectShape = null;
                                            //add the table values from input SF
                                            for (int j = 0; j <= inputNumFields - 1; j++)
                                            {
                                                if (resultSF.EditCellValue(j, shpIndex, inputSF.get_CellValue(j, intersectIndex)) == 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
                                            //add the table values from identity SF
                                            for (int j = 0; j <= identityNumFields - 1; j++)
                                            {
                                                if (resultSF.EditCellValue(j + inputNumFields, shpIndex, identitySF.get_CellValue(j, identityIndex)) == 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
                                        }
                                    }
                                }
                            }
                        }
                        inputGeom = null;
                    }
                    progressmessage = string.Format("{3}: Identity Index:{0}, Quadtree Results:{1}, Shapes added:{2}", identityIndex, results.Count, inputShapesForIdentityShape, DateTime.Now.ToShortTimeString());
                    results.Clear();
                    Debug.WriteLine(progressmessage);
                    inputShapesForIdentityShape = 0;
                    resultSF.StopEditingShapes(true, true, null);
                    resultSF.StartEditingShapes(true, null);
                }
                return(resultSF.NumShapes > 0);
            }
            catch (Exception E)
            {
                System.Windows.Forms.MessageBox.Show(E.ToString());
                return(false);
            }
        }
Esempio n. 16
0
        /// <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 SelectPolygonsWithPolygon(ref MapWinGIS.Shapefile inputSF, ref MapWinGIS.Shape polygon, ref MapWinGIS.Shapefile resultSF, bool SkipMWShapeID)
        {
            MapWinUtility.Logger.Dbg("SelectPolygonsWithPolygon(inputSF: " + Macro.ParamName(inputSF) + ",\n" +
                                     "                          polygon: " + Macro.ParamName(polygon) + ",\n" +
                                     "                          resultSF: ref " + Macro.ParamName(resultSF) + ",\n" +
                                     "                          SkipMWShapeID: " + SkipMWShapeID.ToString() + ")");
            int numShapes = inputSF.NumShapes;
            int shpIndex  = 0;

            if (Globals.CopyFields(ref inputSF, ref resultSF) == false)
            {
                return(false);
            }

            for (int i = 0; i <= numShapes - 1; i++)
            {
                MapWinGIS.Shape currShape = new MapWinGIS.ShapeClass();
                currShape = inputSF.get_Shape(i);
                bool boundsIntersect = Globals.CheckBounds(ref currShape, ref polygon);
                if (boundsIntersect)
                {
                    //check that actual intersection occurs
                    MapWinGIS.Shape intersectShp = new MapWinGIS.ShapeClass();
                    intersectShp = SpatialOperations.Intersection(currShape, polygon);
                    if (intersectShp != null && intersectShp.numPoints > 0)
                    {
                        //shape has at least a small portion inside polygon
                        shpIndex = resultSF.NumShapes;
                        if (resultSF.EditInsertShape(currShape, ref shpIndex) == false)
                        {
                            gErrorMsg = "Problem inserting shape into result file: " + resultSF.get_ErrorMsg(resultSF.LastErrorCode);
                            Debug.WriteLine(gErrorMsg);
                            Error.SetErrorMsg(gErrorMsg);
                            MapWinUtility.Logger.Dbg(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);
                                MapWinUtility.Logger.Dbg(gErrorMsg);
                                return(false);
                            }
                        } //end of looping through table
                    }     //end of checking for intersection
                }         //end of checking bounds
            }             //end of looping through shapes
            if (resultSF.NumShapes > 0)
            {
                if (resultSF.NumFields == 0 || !SkipMWShapeID)
                {
                    //add the ID field and values
                    if (Globals.DoInsertIDs(ref resultSF) == false)
                    {
                        return(false);
                    }
                }
                MapWinUtility.Logger.Dbg("Finished SelectPolygonsWithPolygon");
                return(true);
            }
            else
            {
                MapWinUtility.Logger.Dbg("Finished SelectPolygonsWithPolygon");
                return(false);
            }
        }
Esempio n. 17
0
        /// <summary>
        /// This creates a new shapefile that has Z values and follows along the same line segments.
        /// The boundaries for grid cells are marked with vertices and the segment is given a Z value
        /// that corresponds to the grid elevation it intersects.
        /// </summary>
        /// <param name="mwElevGrid">A MapWinGIS Grid that contains the elevations.</param>
        /// <param name="mwPolyLine">A MapWinGIS Shapefile that shows the pathways of the cross sections in the X-Y direction.</param>
        /// <param name="OutFileName">A string containing the full path of the desired output shapefile.  The extension should be *.shp</param>
        /// <param name="CrossSectionType">Clarifies the type of output.  default = PolyLineWithZ</param>
        /// <param name="ICallBack">A MapWinGIS.ICallback for progress messages. [Optional]</param>
        /// <remarks>This function throws Argument or Application exceptions on errors, so it's recommended that coders enclose it in a try catch block.</remarks>
        public static void GetCrossSection(MapWinGIS.Grid mwElevGrid, MapWinGIS.Shapefile mwPolyLine, string OutFileName, CrossSectionTypes CrossSectionType, MapWinGIS.ICallback ICallBack)
        {
            MapWinUtility.Logger.Dbg("GetCrossSection(mwElevGrid: " + Macro.ParamName(mwElevGrid) + ",\n" +
                                     "                mwPolyLine: " + Macro.ParamName(mwPolyLine) + ",\n" +
                                     "                OutFileName: " + OutFileName + ",\n" +
                                     "                CrossSectionType: " + CrossSectionType.ToString() + ",\n" +
                                     "                ICallback)");
            bool   res;
            int    Prog = 0;
            int    OldProg = 0;
            double dS, dX, dY, XllCenter, YllCenter;
            int    NumRows, NumCols, ElevField, IDField;

            ElevField = 1;
            IDField   = 0;
            // Test to be sure that the elevation grid and shapefile are not null
            if (mwElevGrid == null)
            {
                MapWinUtility.Logger.Dbg("Argument Exception: Elevation grid mwElevGrid can't be null.");
                throw new ArgumentException("Elevation grid mwElevGrid can't be null.");
            }
            if (mwPolyLine == null)
            {
                MapWinUtility.Logger.Dbg("Argument Exception: The shapefile of input cross sections mwPolyLine can't be null.");
                throw new ArgumentException("The shapefile of input cross sections mwPolyLine can't be null.");
            }

            // Clear any existing shapefile output filenames that might cause problems if they exist.
            string fn;

            if (System.IO.Directory.Exists(System.IO.Path.GetDirectoryName(OutFileName)) == false)
            {
                System.IO.Directory.CreateDirectory(System.IO.Path.GetDirectoryName(OutFileName));
            }
            if (System.IO.File.Exists(OutFileName))
            {
                System.IO.File.Delete(OutFileName);
            }
            fn = System.IO.Path.ChangeExtension(OutFileName, ".dbf");
            if (System.IO.File.Exists(fn))
            {
                System.IO.File.Delete(fn);
            }
            fn = System.IO.Path.ChangeExtension(OutFileName, ".shx");
            if (System.IO.File.Exists(fn))
            {
                System.IO.File.Delete(fn);
            }
            fn = System.IO.Path.ChangeExtension(OutFileName, ".prj");
            if (System.IO.File.Exists(fn))
            {
                System.IO.File.Delete(fn);
            }
            // Since we are given the lower left coordinate, just make sure dX and dY are positive
            dX = Math.Abs(mwElevGrid.Header.dX);
            if (dX == 0)
            {
                throw new ApplicationException("mwElevGrid.Header.dX cannot be 0.");
            }
            dY = Math.Abs(mwElevGrid.Header.dY);
            if (dY == 0)
            {
                throw new ApplicationException("mwElevGrid.Header.dY cannot be 0.");
            }

            // Determine the stepping distance from the grid coordintes
            dS = dX / 2;
            if (dY < dX)
            {
                dS = dY / 2;
            }


            XllCenter = mwElevGrid.Header.XllCenter;
            YllCenter = mwElevGrid.Header.YllCenter;
            NumRows   = mwElevGrid.Header.NumberRows;
            NumCols   = mwElevGrid.Header.NumberCols;

            // Test for intersection between the entire shapefile and the grid
            double left, right, top, bottom;

            left   = XllCenter - dX / 2;
            right  = XllCenter + NumCols * dX - dX / 2;
            bottom = YllCenter - dY / 2;
            top    = YllCenter + NumRows * dY - dY / 2;
            MapWinGeoProc.Topology2D.Envelope gExt = new MapWinGeoProc.Topology2D.Envelope(left, right, bottom, top);
            MapWinGeoProc.Topology2D.Envelope pExt = new MapWinGeoProc.Topology2D.Envelope(mwPolyLine.Extents);
            if (gExt.Intersects(pExt) == false)
            {
                MapWinUtility.Logger.Dbg("Application Exception: The shapefile doesn't overlap the grid, so no cross sections were found.");
                throw new ApplicationException("The shapefile doesn't overlap the grid, so no cross sections were found.");
            }


            // Setup the output shapefile and the basic shape objects
            MapWinGIS.Shape     mwShape;
            MapWinGIS.Shapefile sfOut = new MapWinGIS.Shapefile();
            sfOut.Projection = mwPolyLine.Projection;

            if (CrossSectionType == CrossSectionTypes.PointsWithZAndElevField)
            {
                res = sfOut.CreateNew(OutFileName, MapWinGIS.ShpfileType.SHP_POINTZ);
                if (res != true)
                {
                    MapWinUtility.Logger.Dbg("Application Exception: " + sfOut.get_ErrorMsg(sfOut.LastErrorCode));
                    throw new ApplicationException(sfOut.get_ErrorMsg(sfOut.LastErrorCode));
                }
                res = sfOut.StartEditingShapes(true, ICallBack);
                if (res != true)
                {
                    MapWinUtility.Logger.Dbg("Application Exception: " + sfOut.get_ErrorMsg(sfOut.LastErrorCode));
                    throw new ApplicationException(sfOut.get_ErrorMsg(sfOut.LastErrorCode));
                }
                MapWinGIS.Field ID = new MapWinGIS.Field();
                ID.Name = "ID";
                ID.Type = MapWinGIS.FieldType.INTEGER_FIELD;
                res     = sfOut.EditInsertField(ID, ref IDField, ICallBack);
                if (res != true)
                {
                    MapWinUtility.Logger.Dbg("Application Exception: " + sfOut.get_ErrorMsg(sfOut.LastErrorCode));
                    throw new ApplicationException(sfOut.get_ErrorMsg(sfOut.LastErrorCode));
                }
                MapWinGIS.Field Elev = new MapWinGIS.Field();
                Elev.Name = "Elevation";
                Elev.Type = MapWinGIS.FieldType.DOUBLE_FIELD;
                res       = sfOut.EditInsertField(Elev, ref ElevField, ICallBack);
                if (res != true)
                {
                    MapWinUtility.Logger.Dbg("Application Exception: " + sfOut.get_ErrorMsg(sfOut.LastErrorCode));
                    throw new ApplicationException(sfOut.get_ErrorMsg(sfOut.LastErrorCode));
                }
            }
            else
            {
                res = sfOut.CreateNew(OutFileName, MapWinGIS.ShpfileType.SHP_POLYLINEZ);
                if (res != true)
                {
                    MapWinUtility.Logger.Dbg("Application Exception: " + sfOut.get_ErrorMsg(sfOut.LastErrorCode));
                    throw new ApplicationException(sfOut.get_ErrorMsg(sfOut.LastErrorCode));
                }
                res = sfOut.StartEditingShapes(true, ICallBack);
                if (res != true)
                {
                    MapWinUtility.Logger.Dbg("Application Exception: " + sfOut.get_ErrorMsg(sfOut.LastErrorCode));
                    throw new ApplicationException(sfOut.get_ErrorMsg(sfOut.LastErrorCode));
                }
                MapWinGIS.Field ID = new MapWinGIS.Field();
                ID.Name = "ID";
                ID.Type = MapWinGIS.FieldType.INTEGER_FIELD;
                res     = sfOut.EditInsertField(ID, ref IDField, ICallBack);
                if (res != true)
                {
                    MapWinUtility.Logger.Dbg("Application Exception: " + sfOut.get_ErrorMsg(sfOut.LastErrorCode));
                    throw new ApplicationException(sfOut.get_ErrorMsg(sfOut.LastErrorCode));
                }
            }



            MapWinGIS.Shape mwOutShape;
            // Loop through all the shapes in the polyline shapefile
            int shIndx = -1;

            for (int shp = 0; shp < mwPolyLine.NumShapes; shp++)
            {
                mwShape = mwPolyLine.get_Shape(shp);

                // By turning multi-part polylines into multiple linestrings, we avoid the annoying multi-part logic
                MultiLineString MLS = GeometryFactory.CreateMultiLineString(mwShape);

                for (int ls = 0; ls < MLS.NumGeometries; ls++)
                {
                    LineString lsSource = MLS.GeometryN[ls] as LineString;
                    for (int pt = 0; pt < lsSource.Coordinates.Count - 1; pt++)
                    {
                        Coordinate start = lsSource.Coordinates[pt];
                        Coordinate end   = lsSource.Coordinates[pt + 1];

                        // Crop the parts of each segment that do not overlap with the grid.
                        if (start.X < left)
                        {
                            if (end.X < left)
                            {
                                // this segment is outside the grid
                                continue;
                            }
                            // crop this segment to only the portion on the grid
                            start.X = left;
                        }
                        if (end.X < left)
                        {
                            // crop this segment to only the portion on the grid
                            end.X = left;
                        }
                        if (start.X > right)
                        {
                            if (end.X > right)
                            {
                                // this segment is outside the grid
                                continue;
                            }
                            // crop to grid
                            start.X = right;
                        }
                        if (end.X > right)
                        {
                            // crop to the grid
                            end.X = right;
                        }


                        double length   = Math.Sqrt((end.X - start.X) * (end.X - start.X) + (end.Y - start.Y) * (end.Y - start.Y));
                        int    NumSteps = (int)Math.Floor(length / dS);
                        double segDx    = (end.X - start.X) / NumSteps;
                        double segDy    = (end.Y - start.Y) / NumSteps;
                        mwOutShape = new MapWinGIS.Shape();
                        if (CrossSectionType == CrossSectionTypes.PolyLineWithZ)
                        {
                            mwOutShape.Create(MapWinGIS.ShpfileType.SHP_POLYLINEZ);
                        }

                        // step by dS and get the grid value at that point at each step
                        int p = 0;
                        for (int I = 0; I < NumSteps; I++)
                        {
                            int    row, col;
                            object Elev;
                            double X = start.X + segDx * I;
                            double Y = start.Y + segDy * I;
                            mwElevGrid.ProjToCell(X, Y, out col, out row);
                            Elev = mwElevGrid.get_Value(col, row);
                            MapWinGIS.Point pnt = new MapWinGIS.Point();
                            pnt.x = X;
                            pnt.y = Y;
                            pnt.Z = (double)Elev;
                            if (CrossSectionType == CrossSectionTypes.PointsWithZAndElevField)
                            {
                                p          = 0;
                                mwOutShape = new MapWinGIS.Shape();
                                mwOutShape.Create(MapWinGIS.ShpfileType.SHP_POINTZ);
                                res = mwOutShape.InsertPoint(pnt, ref p);
                                if (res == false)
                                {
                                    throw new ApplicationException(mwOutShape.get_ErrorMsg(mwOutShape.LastErrorCode));
                                }
                                res = sfOut.EditInsertShape(mwOutShape, ref shIndx);
                                if (res != true)
                                {
                                    throw new ApplicationException(sfOut.get_ErrorMsg(sfOut.LastErrorCode));
                                }
                                res = sfOut.EditCellValue(IDField, shIndx, shIndx);
                                if (res != true)
                                {
                                    throw new ApplicationException(sfOut.get_ErrorMsg(sfOut.LastErrorCode));
                                }
                                res = sfOut.EditCellValue(ElevField, shIndx, Elev);
                                if (res != true)
                                {
                                    throw new ApplicationException(sfOut.get_ErrorMsg(sfOut.LastErrorCode));
                                }
                                shIndx++;
                            }
                            else
                            {
                                res = mwOutShape.InsertPoint(pnt, ref p);
                                p++;
                                if (res == false)
                                {
                                    throw new ApplicationException(mwOutShape.get_ErrorMsg(mwOutShape.LastErrorCode));
                                }
                            }
                        }
                        if (CrossSectionType == CrossSectionTypes.PolyLineWithZ)
                        {
                            if (mwOutShape.numPoints > 0)
                            {
                                res = sfOut.EditInsertShape(mwOutShape, ref shIndx);
                                if (res != true)
                                {
                                    throw new ApplicationException(sfOut.get_ErrorMsg(sfOut.LastErrorCode));
                                }
                                res = sfOut.EditCellValue(IDField, shIndx, shIndx);
                                if (res != true)
                                {
                                    throw new ApplicationException(sfOut.get_ErrorMsg(sfOut.LastErrorCode));
                                }
                                shIndx++;
                            }
                        }
                    }
                }
                Prog = Convert.ToInt32(shp * 100 / mwPolyLine.NumShapes);
                if (Prog > OldProg)
                {
                    MapWinUtility.Logger.Progress("Evaluating Cross Section..." + Prog.ToString() + "% Complete.", Prog, OldProg);
                    OldProg = Prog;
                }
            }
            res = sfOut.StopEditingShapes(true, true, ICallBack);
            if (res != true)
            {
                MapWinUtility.Logger.Dbg("Application Exception: " + sfOut.get_ErrorMsg(sfOut.LastErrorCode));
                throw new ApplicationException(sfOut.get_ErrorMsg(sfOut.LastErrorCode));
            }
        }
Esempio n. 18
0
        /// <summary>
        /// Exports the shapes that are selected in the MapWindow view to a new shapefile.
        /// </summary>
        /// <param name="MapWin">A reference to the running MapWindow.</param>
        /// <param name="ExportToSFPath">The full path to where the result shapefile should be saved.</param>
        /// <param name="AddToMap">Indicates that the output should be added to the map view immediately.</param>
        /// <returns>False if an error occurs, true otherwise.</returns>
        public static bool ExportSelectedMWViewShapes(MapWindow.Interfaces.IMapWin MapWin, string ExportToSFPath, bool AddToMap)
        {
            MapWinUtility.Logger.Dbg("ExportSelectedMWViewShapes(MapWin: IMapWin,\n" +
                                     "                           ExportToSFPath: " + ExportToSFPath + ",\n" +
                                     "                           AddToMap: " + AddToMap.ToString() + ")");

            if (MapWin.Layers.NumLayers == 0)
            {
                gErrorMsg = "Please select a layer first.";
                Error.SetErrorMsg(gErrorMsg);
                MapWinUtility.Logger.Dbg(gErrorMsg);
                return(false);
            }
            if (MapWin.View.SelectedShapes.NumSelected == 0)
            {
                gErrorMsg = "There are no selected features to export. Please select a feature first.";
                Error.SetErrorMsg(gErrorMsg);
                MapWinUtility.Logger.Dbg(gErrorMsg);
                return(false);
            }
            MapWinGIS.Shapefile sf     = new MapWinGIS.Shapefile();
            MapWinGIS.Shapefile tollSF = new MapWinGIS.Shapefile();
            MapWinGIS.Field     fld    = new MapWinGIS.Field();
            MapWinGIS.Shape     seg    = new MapWinGIS.Shape();
            int  Segments;
            bool Status;

            Status = sf.Open(MapWin.Layers[MapWin.Layers.CurrentLayer].FileName, null);
            if (Status == false)
            {
                gErrorMsg = sf.get_ErrorMsg(sf.LastErrorCode);
                Error.SetErrorMsg(gErrorMsg);
                MapWinUtility.Logger.Dbg(gErrorMsg);
                return(false);
            }

            if (System.IO.File.Exists(ExportToSFPath))
            {
                try
                {
                    DataManagement.DeleteShapefile(ref ExportToSFPath);
                }
                catch
                {
                    gErrorMsg = "The destination file already exists, but could not be deleted. Please check to make sure the file isn't in use.";
                    Error.SetErrorMsg(gErrorMsg);
                    MapWinUtility.Logger.Dbg(gErrorMsg);
                    return(false);
                }
            }

            Status = tollSF.CreateNew(ExportToSFPath, sf.ShapefileType);

            if (Status == false)
            {
                gErrorMsg = tollSF.get_ErrorMsg(tollSF.LastErrorCode);
                Error.SetErrorMsg(gErrorMsg);
                MapWinUtility.Logger.Dbg(gErrorMsg);
                return(false);
            }

            try
            {
                tollSF.Projection = sf.Projection;
            }
            catch (Exception e)
            {
                System.Diagnostics.Debug.WriteLine(e.ToString());
            }

            Status = tollSF.StartEditingShapes(true, null);
            if (Status == false)
            {
                gErrorMsg = tollSF.get_ErrorMsg(tollSF.LastErrorCode);
                Error.SetErrorMsg(gErrorMsg);
                MapWinUtility.Logger.Dbg(gErrorMsg);
                return(false);
            }
            fld.Name  = "MWShapeID";
            fld.Type  = MapWinGIS.FieldType.INTEGER_FIELD;
            fld.Width = 12;
            Segments  = 0;

            // Chris M -- This is already opened above, why open
            // it again here?
            // sf.Open(MapWin.Layers[MapWin.Layers.CurrentLayer].FileName, null);

            for (int j = 0; j <= sf.NumFields - 1; j++)
            {
                tollSF.EditInsertField(sf.get_Field(j), ref j, null);
            }
            MapWin.View.MapCursor = MapWinGIS.tkCursor.crsrWait;
            try
            {
                for (int i = 0; i <= MapWin.View.SelectedShapes.NumSelected - 1; i++)
                {
                    seg    = sf.get_Shape(MapWin.View.SelectedShapes[i].ShapeIndex);
                    Status = tollSF.EditInsertShape(seg, ref Segments);
                    if (Status == false)
                    {
                        gErrorMsg = tollSF.get_ErrorMsg(tollSF.LastErrorCode);
                        Error.SetErrorMsg(gErrorMsg);
                        MapWinUtility.Logger.Dbg(gErrorMsg);
                        return(false);
                    }
                    for (int h = 0; h <= sf.NumFields - 1; h++)
                    {
                        tollSF.EditCellValue(h, i, sf.get_CellValue(h, MapWin.View.SelectedShapes[i].ShapeIndex));
                    }
                    Segments = Segments + 1;
                }
                sf.Close();
                tollSF.StopEditingShapes(true, true, null);
            }
            catch (Exception ex)
            {
                gErrorMsg = ex.Message;
                Error.SetErrorMsg(gErrorMsg);
                MapWinUtility.Logger.Dbg(gErrorMsg);
            }
            MapWin.View.MapCursor = MapWinGIS.tkCursor.crsrArrow;
            tollSF.Close();
            if (AddToMap)
            {
                MapWin.View.LockMap();
                MapWindow.Interfaces.Layer thelayer;
                thelayer = MapWin.Layers.Add(ExportToSFPath, System.IO.Path.GetFileNameWithoutExtension(ExportToSFPath), true);
                thelayer.ClearLabels();
                MapWin.View.UnlockMap();
            }
            MapWinUtility.Logger.Dbg("Finished ExportSelectedMWViewShapes");
            return(true);
        }
Esempio n. 19
0
        /// <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);
        }
Esempio n. 20
0
        /// <summary>
        /// Converts a list of 3d-points to a point shapefile with z-value field.
        /// This function creates a new shapefile. The shapefile has two fields:
        /// a 'MWShapeId' field and a field which contains the z-value.
        /// </summary>
        /// <param name="ShpFileName">Name of the resulting point shapefile</param>
        /// <param name="ZFieldName">Name of the z-field in the shapefile</param>
        public void ToShapefile(string ShpFileName, string ZFieldName)
        {
            MapWinGIS.Shapefile newSF = new MapWinGIS.Shapefile();
            try
            {
                Hashtable FieldIndices = new Hashtable();

                MapWinGIS.ShpfileType sftype;
                sftype = MapWinGIS.ShpfileType.SHP_POINT;
                int fldIdx = 0;

                // if shapefile exists - open it and clear all shapes
                if (System.IO.File.Exists(ShpFileName))
                {
                    newSF.Open(ShpFileName, null);
                    newSF.StartEditingShapes(true, null);
                    newSF.EditClear();
                }
                else //else, create a new shapefile
                {
                    if (!newSF.CreateNew(ShpFileName, sftype))
                    {
                        throw new InvalidOperationException
                                  ("Error creating shapefile " + newSF.get_ErrorMsg(newSF.LastErrorCode));
                    }
                    newSF.StartEditingShapes(true, null);
                }

                //check existing fields:
                for (int i = 0; i < newSF.NumFields; ++i)
                {
                    MapWinGIS.Field fl = newSF.get_Field(i);
                    if (fl.Name == "MWShapeID")
                    {
                        FieldIndices.Add("MWShapeID", i);
                    }
                    if (fl.Name == ZFieldName)
                    {
                        FieldIndices.Add(ZFieldName, i);
                    }
                }

                //Add the fields:
                if (!FieldIndices.ContainsKey("MWShapeID"))
                {
                    //First an ID field
                    MapWinGIS.Field idFld = new MapWinGIS.Field();
                    idFld.Name = "MWShapeID";
                    idFld.Type = MapWinGIS.FieldType.INTEGER_FIELD;
                    fldIdx     = newSF.NumFields;

                    if (newSF.EditInsertField(idFld, ref fldIdx, null) == false)
                    {
                        throw new InvalidOperationException("error inserting field " +
                                                            newSF.get_ErrorMsg(newSF.LastErrorCode));
                    }
                    FieldIndices.Add("MWShapeID", fldIdx);
                }

                if (!FieldIndices.ContainsKey(ZFieldName))
                {
                    //Second add a Z-field
                    MapWinGIS.Field zFld = new MapWinGIS.Field();
                    zFld.Name = "Z";
                    zFld.Type = MapWinGIS.FieldType.DOUBLE_FIELD;
                    fldIdx    = newSF.NumFields;

                    if (newSF.EditInsertField(zFld, ref fldIdx, null) == false)
                    {
                        throw new InvalidOperationException("error inserting field " +
                                                            newSF.get_ErrorMsg(newSF.LastErrorCode));
                    }
                    FieldIndices.Add("Z", fldIdx);
                }

                foreach (ICoordinate pt in _points)
                {
                    //first, add a point shape (geometry)
                    MapWinGIS.Shape newShp = new MapWinGIS.Shape();
                    newShp.Create(MapWinGIS.ShpfileType.SHP_POINT);
                    MapWinGIS.Point newPt = new MapWinGIS.Point();
                    newPt.x = pt.X;
                    newPt.y = pt.Y;
                    int ptIdx = 0;
                    newShp.InsertPoint(newPt, ref ptIdx);
                    int shpIdx = newSF.NumShapes;
                    newSF.EditInsertShape(newShp, ref shpIdx);

                    //second add the z-value
                    newSF.EditCellValue(fldIdx, shpIdx, pt.Z);
                }
            }
            finally
            {
                //finally stop editing and close the shapefile
                newSF.StopEditingShapes(true, true, null);
                if (newSF.Close() == false)
                {
                    throw new InvalidOperationException("error closing shapefile " +
                                                        newSF.get_ErrorMsg(newSF.LastErrorCode));
                }
            }
        }
Esempio n. 21
0
        /// <summary>
        /// Generalization of polyline
        /// shapefiles using the Douglas-Peucker line simplification
        /// algorithm. This method will output a line shapefile.
        /// </summary>
        /// <param name="inFileName">Input shapefile</param>
        /// <param name="outFileName">Output shapefile</param>
        /// <param name="tolerance">tolerance parameter -
        /// specfies the maximum allowed distance between original polyline
        /// and simplified polyline</param>
        /// <param name="cback">Use this parameter for reporting progress. Set to null if not needed</param>
        public static void Generalize(string inFileName, string outFileName, double tolerance, MapWinGIS.ICallback cback)
        {
            MapWinGIS.Shapefile oldSF = new MapWinGIS.Shapefile();
            if (!oldSF.Open(inFileName, null))
            {
                throw new ArgumentException(string.Format("Shapefile {0} could not be opened. Error: {1}",
                                                          inFileName, oldSF.get_ErrorMsg(oldSF.LastErrorCode)));
            }

            //Check if it's a line shapefile
            if (!(oldSF.ShapefileType == MapWinGIS.ShpfileType.SHP_POLYLINE ||
                  oldSF.ShapefileType == MapWinGIS.ShpfileType.SHP_POLYLINEM ||
                  oldSF.ShapefileType == MapWinGIS.ShpfileType.SHP_POLYLINEZ))
            {
                throw new ArgumentException(string.Format("Shapefile {0} must be a polyline shapefile.", inFileName));
            }

            int numShapes = oldSF.NumShapes;
            int numFields = oldSF.NumFields;

            //create a new output shapefile
            MapWinGIS.Shapefile   newSF  = new MapWinGIS.Shapefile();
            MapWinGIS.ShpfileType sftype = MapWinGIS.ShpfileType.SHP_POLYLINE;

            // if shapefile exists - open it and clear all shapes
            if (System.IO.File.Exists(outFileName))
            {
                try
                {
                    //TODO: ask for overwriting..
                    bool deleted = MapWinGeoProc.DataManagement.DeleteShapefile(ref outFileName);
                }
                finally
                {
                }
            }

            if (!newSF.CreateNew(outFileName, sftype))
            {
                throw new InvalidOperationException
                          ("Error creating shapefile " + outFileName + " " + newSF.get_ErrorMsg(newSF.LastErrorCode));
            }
            newSF.StartEditingShapes(true, cback);

            //Copy all fields
            if (!Globals.CopyFields(ref oldSF, ref newSF))
            {
                throw new InvalidOperationException(string.Format("Error copying fields from {0} to {1}",
                                                                  oldSF.Filename, newSF.Filename));
            }

            int newShapeIndex = 0;

            for (int shpIdx = 0; shpIdx < numShapes; ++shpIdx)
            {
                MapWinGIS.Shape shp = oldSF.get_Shape(shpIdx);

                // convert each part of the polyline shape to a 'geometry' object
                Geometry geom = MapWinGeoProc.NTS_Adapter.ShapeToGeometry(shp);
                for (int partIdx = 0; partIdx < geom.NumGeometries; ++partIdx)
                {
                    Geometry geomPart = (Geometry)geom.GetGeometryN(partIdx);

                    //do the simplification
                    ICoordinate[] oldCoords = geomPart.Coordinates;
                    DouglasPeuckerLineSimplifier simplifier = new DouglasPeuckerLineSimplifier(oldCoords);
                    simplifier.DistanceTolerance = tolerance;
                    ICoordinate[] newCoords = simplifier.Simplify();

                    //convert the coordinates back to a geometry
                    Geometry newGeom = new LineString(newCoords);

                    //convert the geometry back to a shape
                    MapWinGIS.Shape newShape = MapWinGeoProc.NTS_Adapter.GeometryToShape(newGeom);

                    //add the shape to the new shapefile
                    newShapeIndex = newSF.NumShapes;
                    if (newSF.EditInsertShape(newShape, ref newShapeIndex) == false)
                    {
                        throw new InvalidOperationException("Error inserting shape: " +
                                                            newSF.get_ErrorMsg(newSF.LastErrorCode));
                    }
                    //add attribute values
                    for (int fldIdx = 0; fldIdx < numFields; ++fldIdx)
                    {
                        object val = oldSF.get_CellValue(fldIdx, shpIdx);
                        if (newSF.EditCellValue(fldIdx, newSF.NumShapes - 1, val) == false)
                        {
                            throw new InvalidOperationException("Error editing cell value: " +
                                                                newSF.get_ErrorMsg(newSF.LastErrorCode));
                        }
                    }
                }
            }
            //close the old shapefile
            oldSF.Close();

            //stop editing and close the new shapefile
            newSF.StopEditingShapes(true, true, cback);
            newSF.Close();
        }