/*******************************************************************************
*                                                                       
*N  {rebuildrasterpyramid.c}  --  Example of how to change the pyramid of an existing raster. 
*To remove a pyramid, set the max_level argument of the SE_rasterattr_set_pyramid_info function to 0.
*To allow ArcSDE to determine the optimum pyramid maximum level for the raster, 
*set the max_level argument to SE_PYRAMID_AUTO_LEVEL. 
*                                                                   
*:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
* How to compile:: 
*       This program provides programming examples of 
*
*
*     1. Add {ibm ICU installation directory}/lib to your library paths
*	  2. Add {ibm ICU installation directory}/include to your include paths
*     3. Add {$SDEHOME}/lib to your library paths;
*     4. Compile and link with icuuc.lib and other needed SDE libraries
*E
*::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
*::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
*X  Legalese:
*
* Copyright � 2006-2007 ESRI
*
* All rights reserved under the copyright laws of the United States and 
* applicable international laws, treaties, and conventions.
*
* You may freely redistribute and use this sample code, with or without 
* modification, provided you include the original copyright notice and use 
* restrictions.  
*
* Disclaimer:  THE SAMPLE CODE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED 
* WARRANTIES, INCLUDING THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL ESRI OR 
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 
* OR BUSINESS INTERRUPTION) SUSTAINED BY YOU OR A THIRD PARTY, HOWEVER CAUSED 
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR 
* TORT ARISING IN ANY WAY OUT OF THE USE OF THIS SAMPLE CODE, EVEN IF ADVISED 
* OF THE POSSIBILITY OF SUCH DAMAGE.
* 
* For additional information, contact:
* Environmental Systems Research Institute, Inc.
* Attn: Contracts and Legal Services Department
* 380 New York Street
* Redlands, California, 92373
* USA
* 
* email: contracts@esri.com
*
*E
****************************************************************************/

#include <stdlib.h>
#include <string.h>
#include "sdetype.h"
#include "sderaster.h"
#include "raster_source.h"


void main (void) {

	SE_CONNECTION connection;
	SE_ERROR error;
	SE_STREAM stream;
	CHAR **columns,
	table[SE_MAX_TABLE_LEN],
	name_value[] = "ESRI_SDERASTERDATASET",
	*server,
	*instance,
	*database,
	*username,
	*password; 
	LONG rc,
	row_id = 1;
	SHORT name_ind = SE_IS_NOT_NULL_VALUE,
	raster_ind = SE_IS_NOT_NULL_VALUE,
	num_columns = 2;
	SE_RASTERATTR raster_attrib;

	/* set the business table */

	strcpy(table,"myrastertable");


	/*****************/
	/* Set the ArcSDE Server connection parameters */
	/*****************/

	server = "jolex";
	instance = "9000";
	database = NULL;
	username = "mark";
	password = "mark";

	/* Connect to the ArcSDE server */
		rc = SE_connection_create (server,instance,database,username,password,&error,&connection);
	check_error (rc,connection,NULL,"SE_connection_create");

	/* Create and initialize the stream. */

	rc = SE_stream_create (connection,&stream);
	check_error (rc, connection, NULL, "SE_connection_create");

	/* Populate the column names */

	columns = (CHAR **) malloc (num_columns * sizeof(CHAR *));
	columns[0] = "name";
	columns[1] = "raster";

	/* Setup the stream update */

	rc = SE_stream_update_row (stream, table, &row_id, num_columns, (const CHAR **)columns);
	check_error (rc, NULL, stream, "SE_stream_update_row");

	/* Bind the NAME column */

	rc = SE_stream_bind_input_column (stream, 1, name_value, &name_ind);
	check_error (rc, NULL, stream, "SE_stream_bind_input_column");

	/* Initialize the raster attribute structure */

	rc = SE_rasterattr_create (&raster_attrib,TRUE);
	check_error (rc, NULL, NULL, "SE_rasterattr_create");

	/* To remove an existing pyramid set the level 0 and */
	/* the interpolation to SE_INTERPOLATION_NONE */

	rc = SE_rasterattr_set_pyramid_info (raster_attrib, 0, FALSE, SE_INTERPOLATION_NONE);
	check_error (rc, NULL, NULL, "SE_rasterattr_set_pyramid_info");

	/* Bind the raster attribute structure to the RASTER column */

	rc = SE_stream_bind_input_column (stream,2,(void *) raster_attrib,&raster_ind);
	check_error (rc, NULL, stream, "SE_stream_bind_input_column");

	/* Execute the pyramid update */

	rc = SE_stream_execute (stream);
	check_error (rc, NULL, stream, "SE_stream_execute");

	/*********/
	/* Cleanup */
	/*********/

	SE_rasterattr_free (raster_attrib);
	SE_stream_free (stream);
	SE_connection_free (connection);


}