getComponentString.cc [tools/Rusle2SoilsXMLCreator/pg/rusle2_from_ssurgo_build_newbackup] Revision:   Date:
#include <string>
#include <iostream>
#include <boost/lexical_cast.hpp>
#include "rusle2_from_ssurgo.h"

using namespace std;
using boost::lexical_cast;

/*
 *  Query out the [COMPONENT] information
 */
PGresult*
queryComponents(PGconn* conn, string muKEY)
{
	
	string compQuery = "SELECT cokey, compname, localphase, "
						"compkind, majcompflag, taxorder, taxsubgrp, "
						"comppct_l, comppct_r, comppct_h, "
						"slopelenusle_l, slopelenusle_r, slopelenusle_h, "
						"slope_l, slope_r, slope_h, "
						"tfact, hydgrp, geomdesc, "
						"taxorder, taxsubgrp "
						"FROM ssurgo.component "
						"WHERE mukey=$1::varchar";
    const char * paramValues[1]; 
    paramValues[0] = muKEY.c_str();
    int numParams = 1;
    PGresult *compRes;


    compRes = PQexecParams( conn,
        compQuery.c_str(),
        numParams,
        NULL,
        paramValues,
        NULL,
        NULL,
        0);
    if( PQresultStatus(compRes) != PGRES_TUPLES_OK ){
        PQclear(compRes);
        exit_nicely(conn);
    }

	return compRes;
}

/*
 *  Try to get a component.
 *  This is an individual iteration
 *  A false value indicates a failure or end of records
 *  Either way, it ends the iterations for the PGresult
 *
 *  Result is hopefully to build the component part of the soil string
 *  Part of that process is fetching the texture, which happens to be part of the chorizons
 *  So we thus have to iterate though the horizons for each component to find the valid texture
 *
 *  Also, in order to complete the XML, we set a horizKey string, which indicates the chkey used
 *  for the texture. This allows the parent caller to then reference the proper chorizon in order
 *  to complete the XML build.
 */
bool 
getComponentString(string& compString, string& horizKey, PGresult* compRes, int cROW, PGconn* conn)
{

#ifndef NDEBUG
	cout << "getComponentString()" << endl;
#endif

	/* Awe...no more? */
	if( cROW >= PQntuples(compRes) )
		return false;

	string coKEY 	= GetNasisString("cokey", compRes, cROW);
	string coName 	= GetNasisString("compname", compRes, cROW);
	string coLP		= GetNasisString("localphase", compRes, cROW);
	int coPct_l	= atoi ( GetNasisString("comppct_l", compRes, cROW).c_str() );
	int coPct_r	= atoi ( GetNasisString("comppct_r", compRes, cROW).c_str() );
	int coPct_h	= atoi ( GetNasisString("comppct_h", compRes, cROW).c_str() );

	/* Make sure we are an important component (ie: >15%) */
	string init_compString = coName + " " + coLP;
	string dominanceStr = "";
	if( coPct_r > 15 )
		dominanceStr += lexical_cast<string>(coPct_r) + "%";
	else if( coPct_l + coPct_h > 15 )
		dominanceStr += lexical_cast<string>(coPct_l + coPct_h) + "%";
	else
		init_compString.erase(0);

	/* return if we don't have a component */
	/* not false, because we don't know if there are more components */
	if(init_compString.length() == 0)
		return true;

	/* Go through Textures */
	PGresult *chRes = queryHorizons(conn, coKEY);
	int chRow = 0;
	string chTexture;
	while( getHorizonString( chTexture, chRes, chRow, conn) == true ){

		/* If no texture, go to next horizon */
		if( chTexture.length() == 0 ){
			chRow++;
			continue;
		}

		/**
		 * Ok, we got a valid horizon and component!
		 * Set the compString, set the horizKey so we can build XML
		 * And let's get outta here...
		 */
		compString = init_compString + " " + chTexture + " " + dominanceStr;
		horizKey = GetNasisString("chkey", chRes, chRow);
		return true;
	}

#ifndef NDEBUG
	cout << "\tleaving getComponentString()" << endl;
#endif

	return true;
}