/// <summary> /// Er... Changes a grid format? /// </summary> /// <param name="origFilename">Original Grid Filename</param> /// <param name="newFilename">Output Grid Filename</param> /// <param name="newFileType">Specifies the original file format of the grid</param> /// <param name="newFileFormat">Specifies the new file format</param> /// <param name="multFactor">Like Extrusion, this multiplies the Z value</param> /// <returns>Boolean, false if there was an error</returns> public static bool ChangeGridFormat(string origFilename, string newFilename, MapWinGIS.GridFileType newFileType, MapWinGIS.GridDataType newFileFormat, float multFactor) { bool Errors = false; MapWinGIS.Grid tGrd = new MapWinGIS.Grid(); tGrd.Open(origFilename, MapWinGIS.GridDataType.UnknownDataType, true, MapWinGIS.GridFileType.UseExtension, null); Logger.Status("Writing Grid to New Format"); //If we're multiplying by a factor, must //create the new grid and actually do it ourselves. //Otherwise, can save directly //Jiri Kadlec 1-28-2009 we still neet to create a new grid when the data or file type is different. if (multFactor == 1 && newFileFormat == tGrd.DataType) { Logger.Dbg("Saving directly to new format"); tGrd.Save(newFilename, newFileType, null); // ProgressForm) } else { Logger.Dbg("Saving to new format with mult. factor: " + multFactor.ToString()); MapWinGIS.GridHeader hdr = new MapWinGIS.GridHeader(); hdr.CopyFrom(tGrd.Header); MapWinGIS.Grid newgrid = new MapWinGIS.Grid(); if (!newgrid.CreateNew(newFilename, hdr, newFileFormat, hdr.NodataValue, true, newFileType, null)) { Logger.Message("Unable to create new grid!", "Error", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Error, System.Windows.Forms.DialogResult.OK); Errors = true; } else { int ncols = tGrd.Header.NumberCols; int nrows = tGrd.Header.NumberRows; float[] oneRow = new float[ncols + 1]; for (int i = 0; i <= nrows - 1; i++) { tGrd.GetFloatWindow(i, i, 0, ncols, ref oneRow[0]); for (int z = 0; z <= ncols - 1; z++) { oneRow[z] *= multFactor; } newgrid.PutFloatWindow(i, i, 0, ncols, ref oneRow[0]); } newgrid.Save(newFilename, newFileType, null); newgrid.Close(); } } return(!Errors); }
private static void CreateGridFromExtents(MapWinGIS.Extents InExtents, double CellSize, String Projection, double NoDataValue, string OutGridPath, out MapWinGIS.Grid OutGrid) { double height = Math.Abs(InExtents.yMax - InExtents.yMin); double width = Math.Abs(InExtents.xMax - InExtents.xMin); OutGrid = new MapWinGIS.Grid(); MapWinGIS.GridHeader hdr = new MapWinGIS.GridHeader(); hdr.dX = CellSize; hdr.dY = CellSize; hdr.NodataValue = NoDataValue; hdr.NumberRows = int.Parse(Math.Ceiling(height / CellSize).ToString()); hdr.NumberCols = int.Parse(Math.Ceiling(width / CellSize).ToString()); hdr.Projection = Projection; hdr.XllCenter = InExtents.xMin + 0.5 * CellSize; hdr.YllCenter = InExtents.yMin + 0.5 * CellSize; OutGrid.CreateNew(OutGridPath, hdr, MapWinGIS.GridDataType.DoubleDataType, NoDataValue, true, MapWinGIS.GridFileType.UseExtension, null); }
/// <summary> /// Creates a new instance of a MapWinGIS Grid according to the parameters required by IRaster /// </summary> /// <param name="Filename">The string full path filename of the grid</param> /// <param name="newFileType">A MapWindow.Interfaces.Type.GridFileType specifying a file format</param> /// <param name="dX">Double, specifies the cell width of a single "pixel" for the grid</param> /// <param name="dY">Double, specifies the cell height of a single pixel of the grid</param> /// <param name="xllCenter">Double, The longitude/X-coordinate of the lower left pixel in the grid</param> /// <param name="yllCenter">Double, The latitude/Y-coordinate of the lower left pixel of the grid</param> /// <param name="noDataVal">Double, The value to use as a no-data value in the grid</param> /// <param name="projection">String: the proj-4 string to use to describe the grid projection</param> /// <param name="nCols">Int, the number of columns in the grid</param> /// <param name="nrows">Int, the number of rows in the grid</param> /// <param name="DataType">A MapWindow.Interfaces.Type.GridDataType that specifies the numeric data format</param> /// <param name="CreateINRam">Boolean, if true, the entire element will be created in ram</param> /// <param name="initialValue">The intial value for the grid</param> /// <param name="applyinitialValue">I'm not sure this is an option in the old grid</param> public void CreateNew(string Filename, GridFileType newFileType, double dX, double dY, double xllCenter, double yllCenter, double noDataVal, string projection, int nCols, int nrows, GridDataType DataType, bool CreateINRam, double initialValue, bool applyinitialValue) { MapWinGIS.GridHeader gh = new MapWinGIS.GridHeader(); gh.dX = dX; gh.dY = dY; gh.XllCenter = xllCenter; gh.YllCenter = yllCenter; gh.Projection = projection; gh.NumberCols = nCols; gh.NumberRows = nrows; gh.NodataValue = noDataVal; if (m_Grid.CreateNew(Filename, gh, MapWinGeoProc.Compatibility.Convert.mwGridDataType(DataType), initialValue, CreateINRam, MapWinGeoProc.Compatibility.Convert.mwGridFileType(newFileType), null) == false) { MapWinUtility.Logger.Dbg("Error calling CreateNew in MapWinGIS.Grid"); throw new MapWinException(m_Grid.LastErrorCode); } }
/// <summary> /// This overload calculates the difference between files. THe number of rows and columns should be the same. /// </summary> /// <param name="SourceFile1">String filename of one grid to compare</param> /// <param name="SourceFile2">String filename of another grid to compare</param> /// <param name="DestFile">String filename of the output difference file</param> /// <param name="Overwrite">Boolean, true if you wish to overwrite an existing output /// file and delete the associated .bmp file. False raises a messagebox if the files exist.</param> /// <param name="ICallBack">A MapWinGIS.ICallBack for status messages</param> public static void Difference(string SourceFile1, string SourceFile2, string DestFile, bool Overwrite, MapWinGIS.ICallback ICallBack) { MapWinGIS.Grid Source1 = new MapWinGIS.Grid(); MapWinGIS.Grid Source2 = new MapWinGIS.Grid(); MapWinGIS.Grid Dest = new MapWinGIS.Grid(); bool res; // Open the source grids if (ICallBack != null) { ICallBack.Progress("Status", 0, "Opening Files..."); } res = Source1.Open(SourceFile1, MapWinGIS.GridDataType.UnknownDataType, true, MapWinGIS.GridFileType.UseExtension, ICallBack); if (res == false) { MapWinUtility.Logger.Dbg("Argument Exception: " + Source1.get_ErrorMsg(Source1.LastErrorCode)); throw new ArgumentException(Source1.get_ErrorMsg(Source1.LastErrorCode)); } res = Source2.Open(SourceFile2, MapWinGIS.GridDataType.UnknownDataType, true, MapWinGIS.GridFileType.UseExtension, ICallBack); if (res == false) { throw new ArgumentException(Source2.get_ErrorMsg(Source2.LastErrorCode)); } // Delete any existing files for our output grid if (System.IO.File.Exists(DestFile)) { string bmp = System.IO.Path.ChangeExtension(DestFile, "bmp"); string bpw = System.IO.Path.ChangeExtension(DestFile, "bpw"); string prj = System.IO.Path.ChangeExtension(DestFile, "prj"); string mwleg = System.IO.Path.ChangeExtension(DestFile, "mwleg"); if (Overwrite == false) { if (System.IO.File.Exists(bmp) || System.IO.File.Exists(bpw) || System.IO.File.Exists(prj) || System.IO.File.Exists(mwleg)) { if (MapWinUtility.Logger.Message("The output file exists, or associated files of the same name exist. Do you wish to delete the existing files?\n", "Output Files Exist", System.Windows.Forms.MessageBoxButtons.YesNo, System.Windows.Forms.MessageBoxIcon.Warning, System.Windows.Forms.DialogResult.No) == System.Windows.Forms.DialogResult.No) { return; } // This ensures mapwindow will recognize the new image as a new file. if (System.IO.File.Exists(bmp)) { System.IO.File.Delete(bmp); } if (System.IO.File.Exists(bpw)) { System.IO.File.Delete(bpw); } if (System.IO.File.Exists(prj)) { System.IO.File.Delete(prj); } if (System.IO.File.Exists(mwleg)) { System.IO.File.Delete(mwleg); } } else { if (MapWinUtility.Logger.Message("The output file already exists. Do you wish to delete it?", "Destination File Already Exists", System.Windows.Forms.MessageBoxButtons.YesNo, System.Windows.Forms.MessageBoxIcon.Warning, System.Windows.Forms.DialogResult.No) == System.Windows.Forms.DialogResult.No) { return; } } } else { if (System.IO.File.Exists(bmp)) { System.IO.File.Delete(bmp); } if (System.IO.File.Exists(bpw)) { System.IO.File.Delete(bpw); } if (System.IO.File.Exists(prj)) { System.IO.File.Delete(prj); } if (System.IO.File.Exists(mwleg)) { System.IO.File.Delete(mwleg); } } System.IO.File.Delete(DestFile); } // Create a new output grid MapWinGIS.GridHeader newHeader = new MapWinGIS.GridHeader(); newHeader.CopyFrom(Source1.Header); if (ICallBack != null) { MapWinUtility.Logger.Dbg("Creating Output File..."); ICallBack.Progress("Status", 0, "Creating Output File..."); } res = Dest.CreateNew(DestFile, newHeader, Source1.DataType, 0, true, MapWinGIS.GridFileType.UseExtension, ICallBack); if (res == false) { MapWinUtility.Logger.Dbg("Argument Exception: " + Dest.get_ErrorMsg(Dest.LastErrorCode)); throw new ArgumentException(Dest.get_ErrorMsg(Dest.LastErrorCode)); } // Calculate the differences Difference(Source1, Source2, Dest, ICallBack); // Close Source grids Source1.Close(); Source2.Close(); // Save and close the output grid res = Dest.Save(DestFile, MapWinGIS.GridFileType.UseExtension, ICallBack); if (res == false) { MapWinUtility.Logger.Dbg("Application Exception: " + Dest.get_ErrorMsg(Dest.LastErrorCode)); throw new ArgumentException(Dest.get_ErrorMsg(Dest.LastErrorCode)); } Dest.Close(); MapWinUtility.Logger.Dbg("Finished Difference"); }