IAIS Client Sample.cpp

//-----------------------------------------------------------------------------

//

// Copyright (c) 2006 Zanja Technologies, Inc

//

// TRADE SECRETS: PROPRIETARY AND CONFIDENTIAL

// Unpublished material - all rights reserved under the

// Copyright Laws of the United States.

//

// Distributed under license by ESRI, Inc.

// For additional information, contact:

// Environmental Systems Research Institute, Inc.

// 380 New York Street

// Redlands, California, USA 92373

 

//-----------------------------------------------------------------------------/*

This sample code in C++ creates and initializes the Client connection object, loads an image service, which is published on a server, and requests an image given the extents and the number of rows and columns.

For this sample to work, open a C++ standard EXE project. Add in references:

Image Server Client 1.0 Type Library [ISClient.Dll] & [ISGlobal.Dll]

 

// sampleAISClient.cpp : Defines the entry point for the console application.

 

#include "stdafx.h"

#include <WINDOWS.h>

#include <atlbase.h>

#include <stdlib.h>

#include <ctype.h>

#include <limits.h>

#include<string>

#include<sstream>

#include<iostream>

 

using namespace std;

 

string MakeExportPropsXML(double xMin, double xMax, double yMin, double yMax, LONG nRows, LONG nCols, string   outputFile);

 

int _tmain(int argc, const char* argv[])

{        

   /// Initializing COM

  HRESULT hRes = CoInitialize(NULL);

 

  ///Creating the Client Object

  CLSID test;

  CLSIDFromProgID(L"esriImageServer.AISClientObject", &test);

  CComQIPtr<IAISClientObject> clientObject;

  HRESULT Result;

  Result = clientObject.CoCreateInstance(test);

 

       

  ///Initializing the Client Object

       VARIANT_BOOL retVal;

       clientObject->Init(CComBSTR("").Detach(), &retVal);

 

  /// Insufficient number of arguements

       if (argc < 10)

  {

      cout<<"\nSample Client Application";

      cout<<"\nUsage: ";

      cout<<"\n\t outputType <Image Service name> <XMin> <YMin> <XMax> <YMax> <Cols> <Rows> <Output File>";

               cout<<"\n outputType can take value 'buffer' or 'file' ";

      cout<<"\nPress any key to exit...";

      getchar();

 

   return -1;

  }

 

  /// Reading the command line arguments

 

  /// outputType accepted as command line argument  

  const char* outputType = argv[1];

 

  /// image service name accepted as command line argument

       const char* imageServiceName = argv[2];

 

  /// Area of Interest accepted as command line argument

       double xMin = atof(argv[3]);

  double yMin = atof(argv[4]);

  double xMax = atof(argv[5]);

  double yMax = atof(argv[6]);

  unsigned long nCols = atol(argv[7]);

  unsigned long nRows = atol(argv[8]);

 

     /// The output file where the image is to be exported

       const char* outputFile = argv[9];

       ///Creating and Initializing the Image Service Object

       CLSID test1;

 

  CLSIDFromProgID(L"esriImageServer.AISImageService", &test1);

  CComQIPtr<IAISImageService> imageServiceObject;

  Result = imageServiceObject.CoCreateInstance(test1);

       

       ///Opening the specified Image Service

  clientObject->OpenImageService(CComBSTR(imageServiceName).Detach(), L"", &imageServiceObject);

 

       /// Processing done based on the output type

       if(strcmp(outputType, "file")==0)

       {

      if (strcmp(outputFile, "") == 0)

               {

          printf("\n Please specify the output file");

                       return -1;

               }

 

      /// Creating the xml to write out images.

      string exportPropsXML = MakeExportPropsXML(xMin, xMax, yMin, yMax,nRows,nCols,outputFile);

 

               ///Creating and initializing the Image Writer Object

      CLSID test2;

               CLSIDFromProgID(L"esriImageServer.AISImageWriter", &test2);

               CComQIPtr< IAISImageWriter> imageWriter;

               Result = imageWriter.CoCreateInstance(test2);                        

               if (FAILED(imageWriter->Init(imageServiceObject, &retVal)))

               {

          cout<<"Failed to initialize Image Writer Object";

                       return -1;

               }

               ///Saving the Image to File

               if (FAILED(imageWriter->Save(CComBSTR(exportPropsXML.c_str()).Detach(), &retVal)))

               {

          cout<<"Failed to save Image on file";

                       return -1;

               }

               cout<<"\n Image has been saved on file: "<<outputFile<<"\n";

 

  }

       else

       {

      if(strcmp(outputType, "buffer")==0)

      {          

          /// Extract the required area of interest

          imageServiceObject->ExtractAoi(xMin,yMin,xMax,yMax,nCols,nRows, &retVal);

 

          /// Get the buffer size.

          LONG size;

          Result = imageServiceObject->GetBufferSize(&size);

 

          /// Create the buffer

          unsigned char *buffer= new unsigned char[size];

 

          ///Getting Image data into the buffer        

                       imageServiceObject->GetImageInBuffer((long)(buffer), &retVal);

                       

                       BSTR pVal;

          if (retVal == VARIANT_FALSE)

                       {

              imageServiceObject->get_Status(&pVal);

              delete[] buffer;

              return -1;

          }

                       else

              cout<< "\nImage has been saved in buffer\n";

 

          delete[] buffer;

               }                                

               else

      {

                       cout<<"\n Please specify valid output type";

          return -1;

               }

  }

 

 

       ///Closing the Client connection object and the Image Service object

       imageServiceObject->Close();

       clientObject->Close(&retVal);

       

       

       CoUninitialize();

 

       return 0;

}

 

///Function that forms the Export properties XML

string MakeExportPropsXML(double xMin, double xMax, double yMin, double yMax, LONG nRows, LONG nCols, string outputFile)

{

  std::ostringstream oss;

  oss<< "<ImageServer><Client><GetImage>";

       oss<< "<Format>TIFF</Format>";

  oss<< "<FileName>"<<outputFile<<"</FileName>";

  oss<< "<OutputType>File</OutputType>";

  oss<< "<CompressionMethod>jpeg</CompressionMethod>";

  oss<< "<CompressionQuality>75</CompressionQuality>";

  oss<< "<TileSize>512</TileSize>";

 

  /// Set the write geo keys parameter to true

  /// so as to enable writing out of geo-tiffs

 

  oss<< "<WriteGeoKeys>True</WriteGeoKeys>";

 

  /// Set the extents

 

  oss<< "<Left>"<< xMin <<"</Left>";

 oss<<"<Bottom>"<< yMin << "</Bottom>";

       oss<< "<Right>"<< xMax << "</Right>";

       oss<<"<Top>" << yMax << "</Top>";

  oss<< "<Cols>"<< nCols << "</Cols>";

       oss<<"<Rows>"<< nRows <<"</Rows>";

 

  oss<< "</GetImage></Client></ImageServer>";        

       return oss.str();

 

}