/// <summary> /// Fills the fields tab /// </summary> private void InitFields() { // building list of fields listLeft.Items.Clear(); listRight.Items.Clear(); for (int i = 0; i < _shapefile.NumFields; i++) { if (_shapefile.get_Field(i).Type != FieldType.STRING_FIELD) { listLeft.Items.Add(_shapefile.get_Field(i).Name); } } // in case some fields have been chosen we must show them if (_shapefile.Charts.NumFields > 0) { for (int i = 0; i < _shapefile.Charts.NumFields; i++) { string name = _shapefile.Charts.get_Field(i).Name.ToLower(); for (int j = 0; j < listLeft.Items.Count; j++) { if (listLeft.Items[j].ToString().ToLower() == name) { listRight.Items.Add(listLeft.Items[j]); listLeft.Items.Remove(listLeft.Items[j]); break; } } } } if (listLeft.Items.Count > 0) { listLeft.SelectedIndex = 0; } if (listRight.Items.Count > 0) { listRight.SelectedIndex = 0; } // Filling size and normalization fields cboChartSizeField.Items.Clear(); cboChartNormalizationField.Items.Clear(); cboChartSizeField.Items.Add("<None>"); // default cboChartNormalizationField.Items.Add("<None>"); for (int i = 0; i < _shapefile.NumFields; i++) { MapWinGIS.Field fld = _shapefile.get_Field(i); if (fld.Type != FieldType.STRING_FIELD) { cboChartSizeField.Items.Add(fld.Name); cboChartNormalizationField.Items.Add(fld.Name); } } if (cboChartSizeField.Items.Count >= 0) { cboChartSizeField.SelectedIndex = 0; } if (cboChartNormalizationField.Items.Count >= 0) { cboChartNormalizationField.SelectedIndex = 0; } // size field MapWinGIS.Charts charts = _shapefile.Charts; if (charts.SizeField >= 0 && charts.SizeField < cboChartSizeField.Items.Count - 1) // first item is <none> { Field fld = _shapefile.get_Field(charts.SizeField); if (fld != null) { for (int i = 2; i < cboChartSizeField.Items.Count; i++) // 2 = <none> and <sum of fields> { if (fld.Name == cboChartSizeField.Items[i].ToString()) { cboChartSizeField.SelectedIndex = i; break; } } } } else { cboChartSizeField.SelectedIndex = 0; } // normalization field if (charts.NormalizationField >= 0 && charts.NormalizationField < cboChartNormalizationField.Items.Count - 1) // first item is <none> { Field fld = _shapefile.get_Field(charts.NormalizationField); if (fld != null) { for (int i = 2; i < cboChartNormalizationField.Items.Count; i++) // 2 = <none> and <sum of fields> { if (fld.Name == cboChartNormalizationField.Items[i].ToString()) { cboChartNormalizationField.SelectedIndex = i; break; } } } } else { cboChartNormalizationField.SelectedIndex = 0; } }
/// <summary> /// Converts grid to a shapefile, removing any existing shapefile. /// Assumed to be an integer grid. Adds attribute headed id with the grid values, and /// attribute headed "Area" with the area for each polygon. /// </summary> /// <param name="gridName">Path of input grid</param> /// <param name="shapeName">Path of output shapefile</param> /// <param name="id">String to use for name of grid values attribute</param> /// <param name="cb">Callback for reporting errors</param> /// <returns>null if any errors, else shapefile</returns> public static MapWinGIS.Shapefile GridToShapeManhattan(string gridName, string shapeName, string id, MapWinGIS.ICallback cb) { if (!System.IO.File.Exists(gridName)) { if (cb != null) { cb.Error("GridToShapeManhattan", clsUtils.ManhattanShapes.nogrid + gridName); } return(null); } MapWinGIS.Grid grid = new MapWinGIS.GridClass(); if (!grid.Open(gridName, GridDataType.UnknownDataType, true, GridFileType.UseExtension, null)) { if (cb != null) { cb.Error("GridToShapeManhattan", clsUtils.ManhattanShapes.noopengrid + gridName); } return(null); } MapWinGIS.GridHeader header = grid.Header; int numRows = header.NumberRows; int numCols = header.NumberCols; MapWinGeoProc.DataManagement.DeleteShapefile(ref shapeName); string oldProj = System.IO.Path.ChangeExtension(gridName, ".prj"); string newProj = System.IO.Path.ChangeExtension(shapeName, ".prj"); if (System.IO.File.Exists(oldProj) && !oldProj.Equals(newProj)) { System.IO.File.Copy(oldProj, newProj); } MapWinGIS.Shapefile shapeFile = new MapWinGIS.Shapefile(); bool success = shapeFile.CreateNew(shapeName, ShpfileType.SHP_POLYGON); if (!success) { if (cb != null) { cb.Error("GridToShapeManhattan", clsUtils.ManhattanShapes.nocreateshapefile + shapeName); } return(null); } success = shapeFile.StartEditingShapes(true, null); if (!success) { if (cb != null) { cb.Error("GridToShapeManhattan", clsUtils.ManhattanShapes.noeditshapefile + shapeName); } return(null); } ManhattanShapes shapes = new ManhattanShapes(header); for (int i = 0; i < numRows; i++) { shapes.addGridRow(grid, i, numCols); } if (!shapes.lastError.Equals("")) { if (cb != null) { cb.Error("GridToShapeManhattan", shapes.lastError); } return(null); } grid.Close(); shapes.FinishShapes(); MapWinGIS.Field idField = new MapWinGIS.Field(); idField.Name = id; idField.Type = FieldType.INTEGER_FIELD; int idIndex = 0; success = shapeFile.EditInsertField(idField, ref idIndex, null); if (!success) { if (cb != null) { cb.Error("GridToShapeManhattan", clsUtils.ManhattanShapes.noaddfield + shapeName); } shapeFile.Close(); return(null); } MapWinGIS.Field areaField = new MapWinGIS.Field(); areaField.Name = "Area"; areaField.Type = FieldType.DOUBLE_FIELD; int areaIndex = 1; success = shapeFile.EditInsertField(areaField, ref areaIndex, null); if (!success) { if (cb != null) { cb.Error("GridToShapeManhattan", clsUtils.ManhattanShapes.noaddfield + shapeName); } shapeFile.Close(); return(null); } int shapeIndex = 0; foreach (int k in shapes.ShapesTable.Keys) { success = shapes.InsertShape(shapeFile, k, ref shapeIndex); if (!success) { if (cb != null) { cb.Error("GridToShapeManhattan", clsUtils.ManhattanShapes.noaddshape + shapeName); } shapeFile.Close(); return(null); } success = shapeFile.EditCellValue(idIndex, shapeIndex, k); if (!success) { if (cb != null) { cb.Error("GridToShapeManhattan", clsUtils.ManhattanShapes.noeditcell + shapeName); } shapeFile.Close(); return(null); } double area = shapes.Area(k); success = shapeFile.EditCellValue(areaIndex, shapeIndex, area); if (!success) { if (cb != null) { cb.Error("GridToShapeManhattan", clsUtils.ManhattanShapes.noeditcell + shapeName); } shapeFile.Close(); return(null); } } success = shapeFile.StopEditingShapes(true, true, null); if (!success) { if (cb != null) { cb.Error("GridToShapeManhattan", clsUtils.ManhattanShapes.noeditshapefile + shapeName); } shapeFile.Close(); return(null); } if (!shapeFile.Close()) { if (cb != null) { cb.Error("GridToShapeManhattan", clsUtils.ManhattanShapes.nocloseshapefile + shapeName); } return(null); } return(shapeFile); }