/// <summary> /// Adds a point to the current shape. If a shape is not yet initialized, it will initialize it. /// </summary> /// <param name="x">X coordinate in projected coordinates.</param> /// <param name="y">Y coordinate in projected coordinates.</param> internal void AddPoint(double x, double y) { if (m_Shape == null) { m_Shape = new ShapeClass(); } if ((m_Shape.NumPoints > 0) && (m_SFType == lt.LineShapefile) && (m_Shape[m_Shape.NumPoints - 1].x == x) && (m_Shape[m_Shape.NumPoints - 1].y == y)) { this.Close(); // a line tried to add two points to the end, meaning that the line is finished. } PointD newPoint = new PointD(x, y); m_Shape.AddPoint(newPoint); if (m_SFType == lt.PointShapefile) // point shapefiles can only have a single point. { this.Close(); } if ((m_SFType == lt.PolygonShapefile) && (m_Shape.NumPoints > 2) && (m_Shape[0].x == newPoint.x) && (m_Shape[0].y == newPoint.y)) { this.Close(); // a polygon or line has doubled back on itself. } }
/// <summary> /// Constructor. /// The AddShapeForm is used when creating a new shape. /// The user can either click on the map to generate new points, lines or polygons, or input coordinates in the text boxes provided on the form. /// </summary> public AddShapeForm(GlobalFunctions g, ref SnapClass snapper) { // // Required for Windows Form Designer support // InitializeComponent(); m_globals = g; m_cursor = new System.Windows.Forms.Cursor(this.GetType(), "InsertPoint.cur"); view = g.MapWin.View; m_oldCursorMode = view.CursorMode; m_oldCursor = view.MapCursor; m_oldCursorHandle = view.UserCursorHandle; view.CursorMode = MapWinGIS.tkCursorMode.cmNone; view.MapCursor = MapWinGIS.tkCursor.crsrWait; m_SFType = g.MapWin.Layers[g.MapWin.Layers.CurrentLayer].LayerType; m_Shape = new ShapeClass(); // Sign me up for these events! m_MapMouseMoveDelegate = new MapWindow.Events.MapMouseMoveEvent(MouseMoveEvent); g.Events.AddHandler(m_MapMouseMoveDelegate); m_MapMouseUpDelegate = new MapWindow.Events.MapMouseUpEvent(MouseUpEvent); g.Events.AddHandler(m_MapMouseUpDelegate); m_LayerSelectedDelegate = new MapWindow.Events.LayerSelectedEvent(LayerSelectedEvent); g.Events.AddHandler(m_LayerSelectedDelegate); MapWindow.Interfaces.eLayerType t = g.MapWin.Layers[g.MapWin.Layers.CurrentLayer].LayerType; m_sf = g.CurrentLayer; if ((m_snapper == null) && ((t == lt.LineShapefile) || (t == lt.PointShapefile) || (t == lt.PolygonShapefile))) { m_snapper = new SnapClass(m_globals.MapWin); } view.MapCursor = MapWinGIS.tkCursor.crsrUserDefined; view.UserCursorHandle = m_cursor.Handle.ToInt32(); m_PointSize = 6; }
private void ResetForNew() { m_snapper = new SnapClass(m_globals.MapWin); m_Shape = new ShapefileEditor.ShapeClass(); m_globals.UpdateButtons(); m_globals.DoEnables(); m_globals.UpdateView(); if (m_cursor == null) { m_cursor = new System.Windows.Forms.Cursor(this.GetType(), "InsertPoint.cur"); } m_oldCursorMode = view.CursorMode; m_oldCursor = view.MapCursor; m_oldCursorHandle = view.UserCursorHandle; view.CursorMode = MapWinGIS.tkCursorMode.cmNone; view.MapCursor = MapWinGIS.tkCursor.crsrUserDefined; view.UserCursorHandle = m_cursor.Handle.ToInt32(); }
/// <summary> /// Finds the nearest point to the requested location that is within the tolerance radius. /// </summary> /// <param name="ProjectedRadius">The tolerance radius in projected units used to search for the nearest point that can be snapped to.</param> /// <param name="x">x coordinate in projected map units</param> /// <param name="y">y coordinate in projected map units</param> /// <param name="curShape">The shape that is currently being created. Points from this shape are also checked.</param> /// <param name="BestPoint">A PointD class with the location of the nearest point to snap to if there is are any within the tolerance, null if no points are found.</param> /// <returns>Returns true if there is a point to snap to.</returns> public bool CanSnap(double ProjectedRadius, double x, double y, ShapeClass curShape, ref System.Collections.ArrayList BestPoints) { System.Collections.IDictionaryEnumerator ie = m_lists.GetEnumerator(); PointD myBest = null; System.Collections.ArrayList myBestPoints = null; while (ie.MoveNext()) { int snaplayeridx = (int)ie.Key; // find any points that are within the tolerance in curShape. for (int i = 0; i < curShape.NumPoints; i++) { PointD pt = curShape[i]; double d = pt.Dist(x, y); if (d < ProjectedRadius) { if (myBest != null) { if (d < myBest.Dist(x, y)) { myBest = pt; } } else { myBest = pt; } } } if (myBest != null) { SnapData d = new SnapData(-1, -1, myBest); myBestPoints = new System.Collections.ArrayList(); myBestPoints.Add(d); } if (CanSnap(ProjectedRadius, x, y, ref BestPoints)) { // work with that best point. if (myBest != null) { PointD tPoint = ((SnapData)BestPoints[0]).point; if (myBest.Dist(x, y) < tPoint.Dist(x, y)) { if (BestPoints == null) { BestPoints = new System.Collections.ArrayList(); } BestPoints.AddRange(myBestPoints); } } } else { if (myBestPoints != null) { if (BestPoints == null) { BestPoints = new System.Collections.ArrayList(); } BestPoints.AddRange(myBestPoints); } } } if (BestPoints == null) { return(false); } else { return(true); } }
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(); } }