/// <summary>Delete fields from shapefile</summary> /// <param name = "dataTable">The datatable with data.</param> private void DeleteField(DataTable dataTable) { for (var col = dataTable.Columns.Count - 1; col > 0; col--) { if ((bool)dataTable.Columns[col].ExtendedProperties["removed"]) { _shapefile.EditDeleteField(col - 1, null); } } }
/// <summary> /// Clips the rectangular shaped minor grid lines to the selected major grids whose shape may not be a 4 sided polygon /// </summary> /// <param name="clippingShapefile"></param> /// <returns></returns> public bool ClipMinorGrid(Shapefile clippingShapefile) { _shapefileMinorGridLines = clippingShapefile.GetIntersection(false, _shapefileMinorGridLines, false, ShpfileType.SHP_POLYLINE); //When we clip a shapefile and the result is the same shapefile, the shapeID field is duplicated. //The original ID field is renamed to MWShapeI_1. //We delete the duplicate and rename MWShapeI_1 to MWShapeID. _shapefileMinorGridLines.EditDeleteField(0, null); _shapefileMinorGridLines.Field[0].Name = "MWShapeID"; return(_shapefileMinorGridLines.NumShapes > 0); }
// <summary> // This code calculates an area of polygons, writes it to the attribute table, and displays as labels. // </summary> public void CalculateArea(AxMap axMap1, string dataPath) { axMap1.Projection = tkMapProjection.PROJECTION_GOOGLE_MERCATOR; string filename = dataPath + "buildings.shp"; if (!File.Exists(filename)) { MessageBox.Show("Couldn't file the file: " + filename); return; } var sf = new Shapefile(); if (!sf.Open(filename, null)) { return; } if (sf.ShapefileType != ShpfileType.SHP_POLYGON) { MessageBox.Show("Polygon shapefile is expected." + Environment.NewLine + "Received: " + sf.ShapefileType); } else { int layerHandle = axMap1.AddLayer(sf, true); sf = axMap1.get_Shapefile(layerHandle); // in case a copy of shapefile was created by GlobalSettings.ReprojectLayersOnAdding int fldIndex = sf.Table.FieldIndexByName["CalcArea"]; if (fldIndex != -1) { if (MessageBox.Show("The area field exists. Do you want to overwrite it?", "", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No) { sf.Close(); return; } } if (!sf.StartEditingTable(null)) { MessageBox.Show("Failed to start editing mode: " + sf.ErrorMsg[sf.LastErrorCode]); sf.Close(); } else { // removing the field in case it is already present if (fldIndex != -1) { if (!sf.EditDeleteField(fldIndex, null)) { MessageBox.Show("Failed to delete field: " + sf.ErrorMsg[sf.LastErrorCode]); sf.Close(); return; } } //adding the new field in the end of the table fldIndex = sf.EditAddField("CalcArea", FieldType.DOUBLE_FIELD, 12, 12); if (fldIndex == -1) { MessageBox.Show("Failed to insert field: " + sf.ErrorMsg[sf.LastErrorCode]); sf.Close(); return; } for (int i = 0; i < sf.NumShapes; i++) { Shape shp = sf.Shape[i]; sf.EditCellValue(fldIndex, i, shp.Area); } sf.Labels.Generate("[CalcArea] + \" sqr.m\"", tkLabelPositioning.lpCentroid, true); sf.Labels.FrameVisible = true; } } }