Example #1
0
        private void MarkAllVertices(double curX, double curY)
        {
            try
            {
                int handle;

                if (m_globals.CurrentLayer == null)
                {
                    return;
                }
                handle = m_MapWin.Layers.CurrentLayer;
                MapWinGIS.Shapefile shpFile = m_globals.CurrentLayer;
                int numShp = shpFile.NumShapes;
                int shpIndex;

                if (m_prevShape != -1)
                {
                    if (!m_MapWin.Layers[m_MapWin.Layers.CurrentLayer].VerticesVisible)
                    {
                        m_MapWin.Layers[m_MapWin.Layers.CurrentLayer].HideVertices();
                    }
                }

                if (m_MapWin.Layers[m_MapWin.Layers.CurrentLayer].LayerType == MapWindow.Interfaces.eLayerType.PolygonShapefile)
                {
                    shpFile.BeginPointInShapefile();
                    shpIndex = shpFile.PointInShapefile(curX, curY);
                    shpFile.EndPointInShapefile();
                }
                else
                {
                    MapWinGIS.Extents bounds = new MapWinGIS.ExtentsClass();
                    bounds.SetBounds(curX, curY, 0, curX, curY, 0);
                    object resArray = null;
                    if (shpFile.SelectShapes(bounds, m_globals.CurrentTolerance * 2, MapWinGIS.SelectMode.INTERSECTION, ref resArray))
                    {
                        shpIndex = (int)((System.Array)resArray).GetValue(0);
                    }
                    else
                    {
                        shpIndex = -1;
                    }
                }

                if (shpIndex >= 0)
                {
                    m_MapWin.Layers[handle].Shapes[shpIndex].ShowVertices(System.Drawing.Color.Red, m_globals.VertexSize);
                    m_prevShape = shpIndex;
                }
                else
                {
                    m_prevShape = -1;
                }
            }
            catch (System.Exception ex)
            {
                m_MapWin.ShowErrorDialog(ex);
            }
        }
        /// <summary>
        /// Returns all portions of the shapefile polygons that fall within the clipper polygon.
        /// </summary>
        /// <param name="polySFPath">The full path to the shapefile of polygons to be clipped.</param>
        /// <param name="polygon">The polygon used for clipping the shapefile.</param>
        /// <param name="resultSFPath">The full path to the result file for where the clipped polygons should be saved.</param>
        /// <param name="copyAttributes">True if copying attrs</param>
        /// <returns>False if an error was encountered, true otherwise.</returns>
        public static bool ClipPolygonSFWithPolygon(ref string polySFPath, ref MapWinGIS.Shape polygon, ref string resultSFPath, bool copyAttributes)
        {
            MapWinUtility.Logger.Dbg("ClipPolygonSFWithPolygon(polySFPath: " + polySFPath + ",\n " +
                                     "                         polygon: + " + Macro.ParamName(polygon) + ",\n" +
                                     "                         resultsSFPath: " + resultSFPath + ",\n" +
                                     "                         copyAttributes: " + copyAttributes + ",\n");

            MapWinGIS.Shapefile resultSF = new MapWinGIS.ShapefileClass();
            MapWinGIS.Shapefile polySF   = new MapWinGIS.ShapefileClass();
            polySF.Open(polySFPath, null);
            MapWinGIS.ShpfileType sfType = polySF.ShapefileType;
            int    shapeIndex            = 0;//insert new shapes at the beginning of the shapefile
            string tmpName;

            if (Globals.PrepareResultSF(ref resultSFPath, ref resultSF, sfType, copyAttributes) == false)
            {
                polySF.Close();
                return(false);
            }

            if (copyAttributes)
            {
                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;
                    if (tmpName.Contains("MWShapeID"))
                    {
                        tmpField.Name = "Last_" + tmpName;
                    }
                    else
                    {
                        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);
                }
            }

            if (sfType == MapWinGIS.ShpfileType.SHP_POLYGON || sfType == MapWinGIS.ShpfileType.SHP_POLYGONM || sfType == MapWinGIS.ShpfileType.SHP_POLYGONZ)
            {
                MapWinGIS.Shape   resultShape = new MapWinGIS.ShapeClass();
                MapWinGIS.Extents shpExtents  = new MapWinGIS.ExtentsClass();
                int  numShapes       = polySF.NumShapes;
                bool boundsIntersect = false;

                for (int i = 0; i <= numShapes - 1; i++)
                {
                    MapWinGIS.Shape currPoly = new MapWinGIS.ShapeClass();
                    currPoly = polySF.get_Shape(i);
                    //check to see if bounds intersect before sending shape to GPC clip function
                    boundsIntersect = Globals.CheckBounds(ref currPoly, ref polygon);

                    if (boundsIntersect == true)
                    {
                        //find the shape resulting from intersection
                        resultShape = SpatialOperations.Intersection(currPoly, polygon);
                        if (resultShape.numPoints != 0)
                        {
                            //save the new shape to the result shapefile
                            if (resultSF.EditInsertShape(resultShape, ref shapeIndex) == false)
                            {
                                gErrorMsg = "Problem inserting shape: " + 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, shapeIndex, polySF.get_CellValue(f, i));
                                }
                            }
                        }
                    }
                }
            }
            if (copyAttributes)
            {
                MapWinGIS.Field ID = new MapWinGIS.FieldClass();
                ID.Name = "MWShapeID";
                ID.Type = MapWinGIS.FieldType.INTEGER_FIELD;
                int fieldIndex = 0;
                if (resultSF.EditInsertField(ID, ref fieldIndex, null) == false)
                {
                    gErrorMsg = "Problem inserting field into .dbf table: " + resultSF.get_ErrorMsg(resultSF.LastErrorCode);
                    Debug.WriteLine(gErrorMsg);
                    Error.SetErrorMsg(gErrorMsg);
                    MapWinUtility.Logger.Dbg(gErrorMsg);
                    return(false);
                }
            }
            int numIDs = resultSF.NumShapes;

            for (int i = 0; i <= numIDs - 1; i++)
            {
                if (resultSF.EditCellValue(0, i, i) == false)
                {
                    gErrorMsg = "Problem inserting value into .dbf table for shape " + i + ": " + resultSF.get_ErrorMsg(resultSF.LastErrorCode);
                    Debug.WriteLine(gErrorMsg);
                    Error.SetErrorMsg(gErrorMsg);
                    MapWinUtility.Logger.Dbg(gErrorMsg);
                    return(false);
                }
            }
            if (resultSF.StopEditingShapes(true, true, null) == false)
            {
                gErrorMsg = "Problem with StopEditingShapes: " + resultSF.get_ErrorMsg(resultSF.LastErrorCode);
                Debug.WriteLine(gErrorMsg);
                Error.SetErrorMsg(gErrorMsg);
                MapWinUtility.Logger.Dbg(gErrorMsg);
                return(false);
            }
            resultSF.Close();
            polySF.Close();
            MapWinUtility.Logger.Dbg("Finished ClipPolygonSFWithPolygon");
            return(true);
        }
        private void MapMouseMove(int ScreenX, int ScreenY, ref bool Handled)
        {
            try
            {
                double projX = 0, projY = 0, newX = 0, newY = 0;
                int    insertIndex = -1, shpIndex = -1;
                MapWinGIS.Shapefile shpFile;

                if (m_Globals.CurrentMode == GlobalFunctions.Modes.AddVertex)
                {
                    m_MapWin.View.PixelToProj((double)ScreenX, (double)ScreenY, ref projX, ref projY);
                    if (m_Globals.CurrentLayer == null)
                    {
                        return;
                    }
                    shpFile = m_Globals.CurrentLayer;

                    //clear all previous drawings and create a new drawing suface
                    ClearDrawings();

                    //draw all the vertices that this point is in
                    if (m_Globals.ShowVertices)
                    {
                        MarkAllVertices(projX, projY);
                    }

                    for (int i = 0; i < shpFile.NumShapes; i++)
                    {
                        if (m_MapWin.Layers[m_MapWin.Layers.CurrentLayer].LayerType == MapWindow.Interfaces.eLayerType.PolygonShapefile)
                        {
                            shpFile.BeginPointInShapefile();
                            shpIndex = shpFile.PointInShapefile(projX, projY);
                            shpFile.EndPointInShapefile();
                        }
                        else
                        {
                            MapWinGIS.Extents bounds = new MapWinGIS.ExtentsClass();
                            bounds.SetBounds(projX, projY, 0, projX, projY, 0);
                            object resArray = null;
                            if (shpFile.SelectShapes(bounds, m_Globals.CurrentTolerance, MapWinGIS.SelectMode.INTERSECTION, ref resArray))
                            {
                                shpIndex = (int)((System.Array)resArray).GetValue(0);
                            }
                            else
                            {
                                shpIndex = -1;
                            }
                        }

                        if (shpIndex != -1)
                        {
                            //draw the location of the point if it is within the tolerance
                            if (WithinTolerance(projX, projY, shpIndex, m_Globals.CurrentTolerance, ref newX, ref newY, ref insertIndex))
                            {
                                m_MapWin.View.Draw.DrawPoint(newX, newY, m_Globals.VertexSize, System.Drawing.Color.Blue);
                                break;
                            }
                        }
                    }

                    //handled this event
                    Handled = true;
                }
            }
            catch (System.Exception ex)
            {
                m_MapWin.ShowErrorDialog(ex);
            }
        }