private Grid FindAssociatedGrid(string fileName) { MapWinGIS.ESRIGridManager esriManager = new ESRIGridManager(); string bgdGrid = System.IO.Path.ChangeExtension(fileName, ".bgd"); string ascGrid = System.IO.Path.ChangeExtension(fileName, ".asc"); Grid grid = new MapWinGIS.GridClass(); //check for all grid types to see if one exists if (System.IO.File.Exists(bgdGrid)) { grid.Open(bgdGrid, MapWinGIS.GridDataType.UnknownDataType, true, MapWinGIS.GridFileType.UseExtension, null); return(grid); } else if (System.IO.File.Exists(ascGrid)) { grid.Open(ascGrid, MapWinGIS.GridDataType.UnknownDataType, true, MapWinGIS.GridFileType.UseExtension, null); return(grid); } else if (esriManager.CanUseESRIGrids()) { if (esriManager.IsESRIGrid(fileName)) { grid.Open(fileName, MapWinGIS.GridDataType.UnknownDataType, true, MapWinGIS.GridFileType.UseExtension, null); return(grid); } } grid.Close(); //could not find any info on this image // No need to warn -- we'll show pixel values -> MapWinUtility.Logger.Message("Could not find any information about this image", "Identifier", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Information); return(null); }
/// <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); }