/*----------------------------------------------------------------------------*/ /* ProgramNodeProcessImageryExtractMrSID */ /*----------------------------------------------------------------------------*/ macro ProgramNodeProcessImageryExtractMrSID() [ Category="Program Commands", Guid="{1F91FB1A-F612-46D4-A453-4B81096121A9}", Image=".\\icons\\generic_script_icon.bmp" ] { for( int i = 0; i < Model.GetSelectCount(); ++i ) { Node node = Model.GetSelectedNode( i ).GetNode(); if( node.IsDerived( ExtractMrSidModifier ) ) { ExtractMrSidModifier esm_modifier = (ExtractMrSidModifier)Model.EditNode( node ); //////////////////////////////////////////////////////////////////////////////// // Initialize a few variables correctly for offsets. // Output Row/Col +1 is sort of a hack but keeps the loop iteration values // comprehensible for a non-programmer... as in starts at 1 instead of 0. :-) // Protected - Do not modify the protected initializations unless you know what you are doing! double start_upper_left_x = esm_modifier.StartUpperLeftX - esm_modifier.OutputTileWidth; double start_upper_left_y = esm_modifier.StartUpperLeftY - esm_modifier.OutputTileHeight; int output_cols = esm_modifier.OutputCols + 1; int output_rows = esm_modifier.OutputRows + 1; string flag_i = "-i "; /* Input file flag. Leave alone. */ string flag_o = "-o "; /* Output file flag. Leave alone. */ string flag_ulxy = " -ulxy "; /* Upper left corner flag. Leave alone. */ string flag_f = "-of tifg "; /* Output file format. Leave alone. */ string flag_wh = " -wh "; /* Width/Height flag. Leave alone. */ string high_output_prefix = "high"; /* Modifier for high-resolution file name. */ string low_output_prefix = "low"; /* Modifier for low-resolution file name. */ string output_file_extension = ".tif"; /* Output file extension. Leave alone. */ string output_scale = " -s 4"; /* Can be 0, 2 or 4. Leave alone. */ string gdal_output_format = "-of GTiff "; /* Name of output format using GDAL command line terminology. Leave alone. */ string gdal_scalar_flag = "-outsize "; /* Name of GDAL scaling flag to use. Leave alone. */ string output_file_path = esm_modifier.OutputFilePath; string batch_file_name = esm_modifier.BatchFileName; string sid_application_name = esm_modifier.MrSidApplicationName; string input_sid_file = esm_modifier.InputSidFile; string output_file_identifier = esm_modifier.OutputFilePlaceName; int width_of_sid_file = esm_modifier.WidthOfSidFile; int height_of_sid_file = esm_modifier.HeightOfSidFile; int output_tile_width_x = esm_modifier.OutputTileWidth; int output_tile_height_y = esm_modifier.OutputTileHeight; string gdal_application_name = esm_modifier.GdalApplicationName; int gdal_output_tile_width = esm_modifier.GdalOutputTileWidth; int gdal_output_tile_height = esm_modifier.GdalOutputTileHeight; int validate_subdivision_coordinates_x = ( esm_modifier.StartUpperLeftX + ( esm_modifier.OutputCols * esm_modifier.OutputTileWidth ) ); int validate_subdivision_coordinates_y = ( esm_modifier.StartUpperLeftY + ( esm_modifier.OutputRows * esm_modifier.OutputTileHeight ) ); //////////////////////////////////////////////////////////////////////////////// // This section outputs values in case you need to debug the script or the parameters. // Variables used to output extraction specifics. To maintain abstraction, the actual command // variables are not used or modified in any way. int startPointX = esm_modifier.StartUpperLeftX; int startPointY = esm_modifier.StartUpperLeftY; int outputR = esm_modifier.OutputRows; int outputC = esm_modifier.OutputCols; Application.Log.LogString ( "Extraction Pixel Range Along X: \n" + "From Pixel " + startPointX + " to " + validate_subdivision_coordinates_x ); Application.Log.LogString ( "---------------------------------" ); Application.Log.LogString ( "Extraction Pixel Range Along Y: \n" + "From Pixel " + startPointY + " to " + validate_subdivision_coordinates_y ); Application.Log.LogString ( "---------------------------------" ); Application.Log.LogString ( "Total Output Tiles: " + ( outputR * outputC ) ); // Validate property editor inputs. If missing, warn and return to property editor. // Protected. Do not modify unless you know what you are doing! if ( esm_modifier.OutputFilePath == "" ) { Application.MessageBox( 0, "You did not specify an output file path!", "Warning" ); return; } if ( esm_modifier.BatchFileName == "" ) { Application.MessageBox( 0, "You did not specify a name for the batch file!", "Warning" ); return; } if ( esm_modifier.MrSidApplicationName == "" ) { Application.MessageBox( 0, "You did not specify the name of the MrSID executable!", "Warning" ); return; } if ( esm_modifier.InputSidFile == "" ) { Application.MessageBox( 0, "You did not specify the name of the input SID file!", "Warning" ); return; } if ( esm_modifier.OutputFilePlaceName == "" ) { Application.MessageBox( 0, "You did not specify an output file place name identifier!", "Warning" ); return; } if ( esm_modifier.WidthOfSidFile == "") { Application.MessageBox( 0, "You did not specify the width of the SID file!", "Warning" ); return; } if ( esm_modifier.HeightOfSidFile == "" ) { Application.MessageBox( 0, "You did not specify the height of the SID file!", "Warning" ); return; } if ( esm_modifier.StartUpperLeftX == "") { Application.MessageBox( 0, "You did not specify the start upper left X value!", "Warning" ); return; } if ( esm_modifier.StartUpperLeftY == "" ) { Application.MessageBox( 0, "You did not specify the start upper left Y value!", "Warning" ); return; } if ( esm_modifier.OutputTileWidth == "") { Application.MessageBox( 0, "You did not specify the output tile width value!", "Warning" ); return; } if ( esm_modifier.OutputTileHeight == "" ) { Application.MessageBox( 0, "You did not specify the output tile height value!", "Warning" ); return; } if ( esm_modifier.OutputRows == "") { Application.MessageBox( 0, "You did not specify the number of output rows!", "Warning" ); return; } if ( esm_modifier.OutputCols == "" ) { Application.MessageBox( 0, "You did not specify the number of output columns!", "Warning" ); return; } if ( esm_modifier.GdalApplicationName == "" ) { Application.MessageBox( 0, "You did not specify the name of the GDAL executable!", "Warning" ); return; } if ( esm_modifier.GdalOutputTileWidth == "") { Application.MessageBox( 0, "You did not specify the GDAL output tile width value!", "Warning" ); return; } if ( esm_modifier.GdalOutputTileHeight == "" ) { Application.MessageBox( 0, "You did not specify the GDAL output tile heigh value!", "Warning" ); return; } if ( validate_subdivision_coordinates_x > esm_modifier.WidthOfSidFile || validate_subdivision_coordinates_x < 0 ) { Application.MessageBox( 0, "Your X subdivision parameters are outside the boundary of the MrSID file!", "Warning" ); return; } if ( validate_subdivision_coordinates_y > esm_modifier.HeightOfSidFile || validate_subdivision_coordinates_y < 0 ) { Application.MessageBox( 0, "Your Y subdivision parameters are outside the boundary of the MrSID file!", "Warning" ); return; } string dir_output_file = output_file_path + "dir_temp.txt"; TextFile imagery_list_file = new TextFile; if ( input_sid_file == "*" ) { string dir_command = "cmd /c \"dir " + output_file_path + "\*.sid /B > " + dir_output_file + "\""; Application.RunProgram( dir_command, "Generating File List...", 1, 1 ); if ( !imagery_list_file.Open( dir_output_file, true ) ) { Application.MessageBox( 0, "Imagery list file could not be opened.", "Warning" ); return; } } else { if ( !imagery_list_file.Open( dir_output_file, false ) ) { Application.MessageBox( 0, "Imagery list file could not be opened.", "Warning" ); return; } imagery_list_file.Write( input_sid_file + "\n" ); imagery_list_file.Close(); if( !imagery_list_file.Open( dir_output_file, true ) ) { Application.MessageBox( 0, "Imagery list file could not be opened.", "Warning" ); return; } } string batch_file_full_path = output_file_path + "\\" + batch_file_name; TextFile batch_file = new TextFile; if ( ! batch_file.Open ( batch_file_full_path, false ) ) { Application.MessageBox( 0, "Batch file could not be written!", "Warning" ); return; } int imagery_file_count = 0; while( 1 ) { if( imagery_list_file.IsEndOfFile() ) { break; } string imagery_file_name = imagery_list_file.ReadLine(); //output_file_identifier ++imagery_file_count; string cur_output_file_delimiter = output_file_identifier + "_" + imagery_file_count; for ( int cols = 1; cols < output_cols; ++cols ) { for ( int rows = 1; rows < output_rows; ++rows ) { // Create the string that creates the image tile by using MRSIDGEODECODE_WIN.EXE and GDAL_TRANSLATE.EXE. Move the files as well. string sid_command_line = output_file_path + sid_application_name + " " + flag_i + output_file_path + imagery_file_name + " " + flag_f + flag_o + output_file_path + high_output_prefix + "_" + cur_output_file_delimiter + "_" + rows + "x" + cols + output_file_extension + flag_ulxy + ( start_upper_left_x + output_tile_width_x * cols ) + " " + ( start_upper_left_y + output_tile_height_y * rows ) + flag_wh + output_tile_width_x + " " + output_tile_height_y + "\n" + output_file_path + gdal_application_name + " " + gdal_output_format + gdal_scalar_flag + gdal_output_tile_width + " " + gdal_output_tile_height + " " + output_file_path + high_output_prefix + "_" + cur_output_file_delimiter + "_" + rows + "x" + cols + output_file_extension + " " + output_file_path + low_output_prefix + "_" + cur_output_file_delimiter + "_" + rows + "x" + cols + output_file_extension + "\n" + "move " + output_file_path + "h*.tif " + output_file_path + "\"..\\high_resolution_processed_imagery\"" + "\n" + "move " + output_file_path + "l*.tif " + output_file_path + "\"..\\low_resolution_processed_imagery\"" + "\n"; batch_file.Write( sid_command_line ); } } } delete batch_file; Application.RunProgram ( batch_file_full_path, "Image: MrSID Extraction underway...", 1, 0 ); } } }