/// <summary> /// Converts a list of polylines into a single multi-part shape /// </summary> /// <param name="Polylines"></param> /// <returns></returns> public static MapWinGIS.Shape LineStrings_To_mwShape(List <LineString> Polylines) { MapWinGIS.Shape mwShape = new MapWinGIS.Shape(); bool res; res = mwShape.Create(MapWinGIS.ShpfileType.SHP_POLYLINEZ); if (res != true) { throw new ApplicationException(mwShape.get_ErrorMsg(mwShape.LastErrorCode)); } int idx = 0; for (int iPart = 0; iPart < Polylines.Count; iPart++) { LineString Part = Polylines[iPart]; // we only cal set_part if we have multiple polylines if (Polylines.Count > 0) { mwShape.set_Part(iPart, idx); } for (int iPoint = 0; iPoint < Part.Points.Count; iPoint++) { mwShape.InsertPoint(Part.Points[iPoint].To_mwPoint(), ref idx); idx++; } } return(mwShape); } // End LineStrings_To_mwShape
private bool AddPointToShapeFile(double x, double y, MapWinGIS.Shapefile shpFile, int shpIndex, int insertIndex) { try { MapWinGIS.Point p; //start editing shapes shpFile.StartEditingShapes(true, null); MapWinGIS.Shape shp = shpFile.get_Shape(shpIndex); p = new MapWinGIS.Point(); p.x = x; p.y = y; if (!shp.InsertPoint(p, ref insertIndex)) { return(false); } else if (shp.NumParts > 1) { // Shift up part indices if needed (bugzilla 798) bool shifting = false; for (int i = 0; i < shp.NumParts; i++) { if (shp.get_Part(i) >= insertIndex) { // Shift up this one and all remaining shifting = true; } if (shifting) { shp.set_Part(i, shp.get_Part(i) + 1); } } } m_Globals.CreateUndoPoint(); //stop editing and save changes shpFile.StopEditingShapes(true, true, null); return(true); } catch (System.Exception ex) { m_MapWin.ShowErrorDialog(ex); } return(false); }
private void ShiftDownVertices(ref MapWinGIS.Shape shp, int deleteIndex) { if (shp.NumParts > 1) { // Shift down part indices if needed (bugzilla 798) bool shifting = false; for (int i = 0; i < shp.NumParts; i++) { if (shp.get_Part(i) > deleteIndex) { // Shift up this one and all remaining shifting = true; } if (shifting) { shp.set_Part(i, Math.Max(0, shp.get_Part(i) - 1)); } } } }
private void btnOK_Click(object sender, EventArgs e) { double tolerance = 0; if (!double.TryParse(txtTolerance.Text, out tolerance)) { MapWinUtility.Logger.Message("Please enter only numbers in the distance field.", "Enter Only Numbers", MessageBoxButtons.OK, MessageBoxIcon.Exclamation, DialogResult.OK); return; } this.Cursor = Cursors.WaitCursor; prgShape.Visible = true; prgPoint.Visible = true; sf.StartEditingShapes(true, null); long totalRemoved = 0; long fromShapes = 0; int numPts; System.Collections.ArrayList removeList = new System.Collections.ArrayList(); if (sf.NumShapes > 0) { prgShape.Maximum = sf.NumShapes; for (int z = 0; z < sf.NumShapes; z++) { MapWinGIS.Shape shp = sf.get_Shape(z); if (z < 100 || z % 5 == 0) { prgShape.Value = z; this.Refresh(); } Application.DoEvents(); numPts = shp.numPoints; if (numPts > 0) { prgPoint.Maximum = numPts; for (int i = numPts - 1; i > 0; i--) // 0 never needs to actually be hit, inner loop will compare against all zeros { if (i % 5 == 0) { prgPoint.Value = numPts - i; this.Refresh(); } for (int j = i - 1; j >= 0; j--) { if (Math.Sqrt(Math.Pow(shp.get_Point(i).x - shp.get_Point(j).x, 2) + Math.Pow(shp.get_Point(i).y - shp.get_Point(j).y, 2)) < tolerance) { // Make sure that !(i == numPts - 1 && j == 0) -- polygon completion point if (!removeList.Contains(i) && !(i == numPts - 1 && j == 0)) { removeList.Add(i); } } } } } if (removeList.Count > 0) { if (removeList.Count >= shp.numPoints - 1) { // Probably not a good thing..... MapWinUtility.Logger.Message("Aborting: Proceeding will remove all points from one or more shapes. The distance may need to be smaller, particularly for unprojected (latitute and longitude) coordinate systems.", "Aborting -- All Points Would Be Removed", MessageBoxButtons.OK, MessageBoxIcon.Error, DialogResult.OK); sf.StopEditingShapes(false, true, null); this.Cursor = Cursors.Default; prgPoint.Value = 0; prgShape.Value = 0; prgPoint.Visible = false; prgShape.Visible = false; this.Cursor = Cursors.Default; return; } totalRemoved += removeList.Count; fromShapes++; while (removeList.Count > 0) { for (int part = 0; part < shp.NumParts; part++) { if (shp.get_Part(part) >= (int)removeList[0]) { shp.set_Part(part, shp.get_Part(part) - 1); } } shp.DeletePoint((int)removeList[0]); removeList.RemoveAt(0); } // If this is a polygon and there are now less than 3 shapes, panic if ((shp.ShapeType == MapWinGIS.ShpfileType.SHP_POLYGON || shp.ShapeType == MapWinGIS.ShpfileType.SHP_POLYGONZ || shp.ShapeType == MapWinGIS.ShpfileType.SHP_POLYGONM) && shp.numPoints < 3) { // Probably not a good thing..... MapWinUtility.Logger.Message("Aborting: Proceeding will leave less than 3 points in a polygon. The distance may need to be smaller, particularly for unprojected (latitute and longitude) coordinate systems.", "Aborting -- Polygons Would Be Destroyed", MessageBoxButtons.OK, MessageBoxIcon.Error, DialogResult.OK); sf.StopEditingShapes(false, true, null); this.Cursor = Cursors.Default; prgPoint.Value = 0; prgShape.Value = 0; prgPoint.Visible = false; prgShape.Visible = false; this.Cursor = Cursors.Default; return; } // If the first and last points are not the same now, reclose it if (shp.get_Point(0).x != shp.get_Point(shp.numPoints - 1).x || shp.get_Point(0).y != shp.get_Point(shp.numPoints - 1).y) { MapWinGIS.Point pnt = new MapWinGIS.Point(); pnt.x = shp.get_Point(0).x; pnt.y = shp.get_Point(0).y; pnt.Z = shp.get_Point(0).Z; int ptidx = shp.numPoints; shp.InsertPoint(pnt, ref ptidx); } } } } prgPoint.Value = prgPoint.Maximum; prgShape.Value = prgShape.Maximum; g.CreateUndoPoint(); sf.StopEditingShapes(true, true, null); this.Cursor = Cursors.Default; if (totalRemoved > 0) { MapWinUtility.Logger.Message("There were " + totalRemoved.ToString() + " points removed from " + fromShapes.ToString() + " shapes.", "Finished", MessageBoxButtons.OK, MessageBoxIcon.Information, DialogResult.OK); } else { MapWinUtility.Logger.Message("Finished -- no extra points needed to be removed.", "Finished", MessageBoxButtons.OK, MessageBoxIcon.Information, DialogResult.OK); } this.DialogResult = DialogResult.OK; this.Close(); }