HI, I WANT TO BUILD A TREE STRUCTURE BY TOKENIZING THE WKT STRING STREAM ( BELOW ).
ALSO I WANT TO ACCESS AND STORE THE VALUES OF THE CHILD NODES IN THE TREE. EX : VARIABLE PROCS_NAME : "OSGB 1936 / British National Grid" .

I AM ATTACHING THE REQUIRED TREE STRUCTURE FIGURE , PLEASE HELP ME OUT

THANKS


WKT STRING STREAM :

COMPD_CS["OSGB36 / British National Grid + ODN",
PROJCS["OSGB 1936 / British National Grid",
GEOGCS["OSGB 1936",
DATUM["OSGB_1936",
SPHEROID["Airy 1830",6377563.396,299.3249646,AUTHORITY["EPSG","7001"]],
TOWGS84[375,-111,431,0,0,0,0],
AUTHORITY["EPSG","6277"]],
PRIMEM["Greenwich",0,AUTHORITY["EPSG","8901"]],
UNIT["DMSH",0.0174532925199433,AUTHORITY["EPSG","9108"]],
AXIS["Lat",NORTH],
AXIS["Long",EAST],
AUTHORITY["EPSG","4277"]],
PROJECTION["Transverse_Mercator"],
PARAMETER["latitude_of_origin",49],
PARAMETER["central_meridian",-2],
PARAMETER["scale_factor",0.999601272],
PARAMETER["false_easting",400000],
PARAMETER["false_northing",-100000],
UNIT["metre",1,AUTHORITY["EPSG","9001"]],
AXIS["E",EAST],
AXIS["N",NORTH],
AUTHORITY["EPSG","27700"]],
VERT_CS["Newlyn",
VERT_DATUM["Ordnance Datum Newlyn",2005,AUTHORITY["EPSG","5101"]],
UNIT["metre",1,AUTHORITY["EPSG","9001"]],
AXIS["Up",UP],
AUTHORITY["EPSG","5701"]],
AUTHORITY["EPSG","

Attachments TREESTRUCT.jpg 39.67 KB

Its finally done,

i'm passing the string from the main file ( attached ).

the importfrmwkt function scans the string and tokenize the string, called from treebuild function.

i am intially building the tree with the root node with the value " first node " .
later the algorithm loop builds the tree successively without any hindrance.

second part of the code has the methods to access the values from the tree.

plz find the attached cpp and its associated header file with main application file.

the code is coded in Qt.

u can easily understand the code.

any further queries plz email me @

[email-snipped]

Edited 5 Years Ago by WaltP: Removed email

Attachments
#include "importfrmwkt5.h"
/************************************************************************/
/*                            OGR_SRSNode()                             */
/************************************************************************/

/**
 * Constructor.
 *
 * @param pszValueIn this optional parameter can be used to initialize
 * the value of the node upon creation.  If omitted the node will be created
 * with a value of "".  Newly created OGR_SRSNodes have no children.
 */

OGR_SRSNode::OGR_SRSNode()

{
    central_meridian = 25;
    dostandard_parallel_1 = 25;
    standard_parallel_2 = 35;
    scale_factor = 25;
    latitude_of_origin = 30;
    false_easting = 0;
    false_northing = 0;
    longitudeOfOrigin = 25;
    utm_zone = 43;
    *hemisphere = "north";
}

/************************************************************************/
/*                            ~OGR_SRSNode()                            */
/************************************************************************/

OGR_SRSNode::~OGR_SRSNode()

{
    //VSIFree( pszValue );

    //    ClearChildren();
}

void OGR_SRSNode :: importFromWkt()

{
    int         bInQuotedString = FALSE;

    //OGR_SRSNode *tree_list_arr[50];

    /* -------------------------------------------------------------------- */
    /*      Read the ``value'' for this node.                               */
    /* -------------------------------------------------------------------- */
    char        szToken[512];
    int         nTokenLen = 0;

    while( *pszInput != '\0' && nTokenLen < (int) sizeof(szToken)-1 )
    {

        if( *pszInput == '"' )
        {
            bInQuotedString = !bInQuotedString;
        }
        else if( !bInQuotedString
                 && (*pszInput == '[' || *pszInput == ']' || *pszInput == ','
                     || *pszInput == '(' || *pszInput == ')' ) )
        {
            break;
        }
        else if( !bInQuotedString
                 && (*pszInput == ' ' || *pszInput == '\t'
                     || *pszInput == 10 || *pszInput == 13) )
        {
            /* just skip over whitespace */
        }
        else
        {
            szToken[nTokenLen++] = *pszInput;
        }

        pszInput++;
    }

    szToken[nTokenLen++] = '\0';
    SetValue( szToken );
}

OGRErr OGR_SRSNode :: Treebuild( char *ppszInput)

{


    char str_root[2000] = "WKT_ROOT_NODE[\"first_node\",";

    pszInput = strcat(str_root,ppszInput);

    pszInput = strcat(pszInput,"]");

    OGR_SRSNode *poNewChild;

    //poNewChild = new OGR_SRSNode;

    OGR_SRSNode *Temp_parent;

    OGR_SRSNode *Temp_parent2;

    poNewChild->importFromWkt();

    static int temp_index = 10;

    static int nchildren = 0;

    int TEMP1=0;


    //pszInput++;

    if( *pszInput == '[' || *pszInput == '(' )
    {

        inde=0;

        do
        {

            if(X==1)
            {

                poNewChild = new OGR_SRSNode;
                poNewChild->pszValue = New_token;
                tree_list_arr[++j] = (double*) poNewChild;
                Temp_parent2 = poNewChild;
                Temp_parent2->sub_treelistarr[++sj] = (double*) poNewChild;
                Temp_parent2->openbrce = 1;
                X=2;

            }
            else
            {

                inde=0;
                //free((OGR_SRSNode*)poNewChild);
                poNewChild = Temp_parent;

            }

            pszInput++; // Skip bracket or comma.

            poNewChild->importFromWkt();
            //qDebug() << tree_list_arr[j];

            AddChild( &poNewChild);

            inde++;

            nchildren++;

            poNewChild->child_tracer = nchildren;

            //int x=0;
            int testvar=0;

            do
            {

                testvar = Testcomma(pszInput+1);

                if(*pszInput == ']' || *pszInput == ')')
                {
                    testvar = 3;
                }

                if(testvar == 1)
                {
                    pszInput++;
                    poNewChild->importFromWkt();

                    AddChild( &poNewChild);

                    inde++;

                    nchildren++;

                    poNewChild->child_tracer = nchildren;

                }
                else if(testvar == 2)
                {
                    pszInput++;

                    nchildren = 0;

                    poNewChild->index_tracer = inde;

                    nchildren++;

                    poNewChild->child_tracer = poNewChild->child_tracer + nchildren;

                    poNewChild->importFromWkt();

                    Temp_parent2->openbrce=Temp_parent2->openbrce + 1;

                    OGR_SRSNode *temp_child;

                    temp_child = new OGR_SRSNode();

                    if((!qstrcmp(New_token,"PROJCS")) || (!qstrcmp(New_token,"GEOGCS")))
                    {
                        sj=0;
                        tree_list_arr[++j]= (double*) temp_child;
                        Temp_parent2 = temp_child;
                        Temp_parent2->sub_treelistarr[++sj] = (double*) temp_child;
                        Temp_parent2->openbrce = 1;
                        //Temp_parent2->openbrce = Temp_parent2->openbrce +1;

                    }

                    else if((!qstrcmp(New_token,"VERT_CS")) || (!qstrcmp(New_token,"COMPD_CS")))
                    {
                        sj=0;
                        tree_list_arr[++j]= (double*) temp_child;
                        Temp_parent2 = temp_child;
                        Temp_parent2->sub_treelistarr[++sj] = (double*) temp_child;
                        Temp_parent2->openbrce = 1;
                       //Temp_parent2->openbrce = Temp_parent2->openbrce +1;
                    }
                    else
                    {
                        Temp_parent2->sub_treelistarr[++sj] = (double*) temp_child;
                    }

                    temp_child->pszValue=New_token;
                    qDebug()<<"sub_parent node" <<temp_child->pszValue;

                    poNewChild->papoChildNodes[inde] = temp_child;
                    //poNewChild->papoChildNodes[inde+1] = NULL;

                    Temp_parent = temp_child;

                    nchildren = 0;
                    break;
                }

                else if( *pszInput == ']' || *pszInput == ')' || testvar == 3 )
                {

                    do
                    {

                        //closed_bracket_count++;

                        //poNewChild->closebrce = poNewChild->closebrce +1;

                        Temp_parent2->openbrce = Temp_parent2->openbrce - 1;

                        temp_index = Temp_parent2->openbrce;

//                        if(temp_index<0)
//                        {
//                            break;
//                        }
                        if(temp_index != 0)
                        {
                            poNewChild = (OGR_SRSNode*) Temp_parent2->sub_treelistarr[temp_index];
                        }
                        if(temp_index == 1)
                        {
                            sj=1;
                        }

                        if(Temp_parent2->openbrce == 0)
                        {
                            TEMP1 = j;
                            while(TEMP1>=1)
                            {
                                Temp_parent2 = (OGR_SRSNode*)tree_list_arr[TEMP1--];

                                if(Temp_parent2->openbrce != 0 )
                                {
                                    poNewChild = Temp_parent2;
                                    Temp_parent2->openbrce=Temp_parent2->openbrce-1;
                                    sj=1;
//                                    inde = poNewChild->index_tracer + 1;
//                                    pszInput++;
                                    break;
                                }
                            }

                        }

                        inde = poNewChild->index_tracer + 1;
                        pszInput++;

                    }while(*pszInput !=',' && *pszInput == ']');


                }

                testvar = 0;

            }while(*pszInput == ',' || *pszInput == ']');


        }while(*pszInput!='\0');


    }
    else
    {
        qDebug()<<"CORRUPT WKT STRING";
    }

    /************************************ COMPD_CS SCAN ***********************************************/

    int count = j;

    for(int i=2;i<=count;i++)
    {
        poParent = (OGR_SRSNode*)tree_list_arr[i];

        if(!qstrcmp("COMPD_CS", poParent->pszValue))
        {
            compd_cs_name = poParent->papoChildNodes[0]->pszValue;

            for(int n=1;n<poParent->child_tracer;n++)
            {
                if(!qstrcmp("AUTHORITY", poParent->papoChildNodes[n]->pszValue))
                {
                    compdcs_autho_name = poParent->papoChildNodes[n]->papoChildNodes[0]->pszValue;
                    compdcs_autho_code = poParent->papoChildNodes[n]->papoChildNodes[1]->pszValue;
                }
            }

        }
    }

    /*********************************** GEOGCS SCAN ***************************************************/

    for(int i=2;i<=count;i++)
    {
        poParent = (OGR_SRSNode*)tree_list_arr[i];

        if(!qstrcmp("GEOGCS",poParent->pszValue))
        {
            geogcs_name = poParent->papoChildNodes[0]->pszValue;

            for(int k=1;k<poParent->child_tracer;k++)
            {
                if(!qstrcmp("DATUM",poParent->papoChildNodes[k]->pszValue))
                {
                    datum_name = poParent->papoChildNodes[k]->papoChildNodes[0]->pszValue;

                    /******************** *DATUM NODE ************************************/

                    for(int m=1;m<poParent->papoChildNodes[k]->child_tracer;m++)
                    {
                        if(!qstrcmp("SPHEROID",poParent->papoChildNodes[k]->papoChildNodes[m]->pszValue))//
                        {
                            int c=poParent->papoChildNodes[k]->papoChildNodes[m]->child_tracer;

                            int l=0;
                            while(l<c)
#ifndef IMPORTFRMWKT5_H
#define IMPORTFRMWKT5_H

#include <QtGui/QApplication>
#include <QMessageBox>
#include<QString>
#include "QDebug"
#include <iostream>
#include "coordinatesystemmgr.h"
#include "datum.h"

#define OGRERR_NONE 0
#define OGRERR_CORRUPT_DATA 5

typedef int OGRErr;

static char *pszInput;

static char *New_token;

static int j=0;

static int sj=0;

static double *tree_list_arr[20];

//static double *sub_treelistarr[80];

static int inde=0;

static int X=1;

class OGR_SRSNode
{
    char   *pszValue;

    int    index_tracer;

    int    child_tracer;

    int    openbrce,closebrce;

    double *sub_treelistarr[80];

    char *projection_name,*projcs_name,*geogcs_name,*geogcs_autho_name,*geogcs_autho_code,*datum_name,*datum_autho_name,*datum_autho_code,*projcs_axisE_name,*projcs_axisN_name,*compd_cs_name,*compdcs_autho_name,*compdcs_autho_code;

    char *sphd_name,*sphd_autho_name,*sphd_autho_code,*projcs_unit_name,*projcs_unit_autho_name,*projcs_unit_autho_code,*vertcs_name,*vert_datum_name;

    char *vert_dat_autho_name,*vert_dat_autho_code,*vert_unit_name,*vert_unit_autho_name,*vert_unit_autho_code,*vert_autho_name,*vert_autho_code,*vert_axisUP_name,*vert_axisDOWN_name;

    char *prim_name,*prim_long,*unit_name,*prim_autho_code,*prim_autho_name,*unit_autho_name,*unit_autho_code,*geogcs_axisLAT_name,*geogcs_axisLONG_name,*projcs_autho_name,*projcs_autho_code,*hemisphere;

    double central_meridian,standard_parallel_1,standard_parallel_2,scale_factor,latitude_of_origin,false_easting,false_northing,longitudeOfOrigin,vert_datum_type,vert_unit_conf;

    double unit_convf,towgs_shift_X,towgs_shift_Y,towgs_shift_Z,towgs_rot_X,towgs_rot_Y,towgs_rot_Z,towgs_scalefactor,projcs_axisE_dirn,projcs_axisN_dirn,geogcs_axisLONG_dirn,geogcs_axisLAT_dirn;

    double vert_axisUP_dirn,vert_axisDOWN_dirn,projcs_unit_conf,utm_zone,sphd_semimajor_axis,sphd_inv_flat;
    //double *tree_list_arr[50];

    OGR_SRSNode     *papoChildNodes[50];
    OGR_SRSNode     *poParent;
    OGR_SRSNode     *poParent1;
    CGDatum *cgdat;
    CGCoordinateSystemMgr *cgmgr;

    CGProjectionID _proj_id;

public:

    OGR_SRSNode();
    ~OGR_SRSNode();

    OGR_SRSNode     *GetNode( const char * );

    const OGR_SRSNode *GetNode( const char * ) const;


    void            AddChild( OGR_SRSNode **);
    int             FindChild( const char * ) const;



    const char      *GetValue() const { return pszValue; }

    void            SetValue( char * );

    void            importFromWkt();

    OGRErr          Treebuild(char* );

    char            *CPLStrdup( const char *);

    void            *CPLRealloc( void *, size_t );

    char            *VSIStrdup( const char * );

    void            *VSIMalloc( size_t );

    void            *VSIRealloc( void * , size_t );

    void            VSIFree( void * );

    int             Testcomma ( const char *);

};

#endif // IMPORTFRMWKT5_H
#include <QtGui/QApplication>
#include "importfrmwkt5.h"

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    OGR_SRSNode sr;
    int res;
    char  *str= "COMPD_CS[\"OSGB36 / British National Grid + ODN\",PROJCS[\"OSGB 1936 / British National Grid\",GEOGCS[\"OSGB 1936\",DATUM[\"OSGB_1936\",SPHEROID[\"Airy 1830\",6377563.396,299.3249646,AUTHORITY[\"EPSG\",\"7001\"]],TOWGS84[375,-111,431,0,0,0,0],AUTHORITY[\"EPSG\",\"6277\"]],PRIMEM[\"Greenwich\",0,AUTHORITY[\"EPSG\",\"8901\"]],UNIT[\"DMSH\",0.0174532925199433,AUTHORITY[\"EPSG\",\"9108\"]],AXIS[\"Lat\",NORTH],AXIS[\"Long\",EAST],AUTHORITY[\"EPSG\",\"4277\"]],PROJECTION[\"Transverse_Mercator\"],PARAMETER[\"latitude_of_origin\",49],PARAMETER[\"central_meridian\",-2],PARAMETER[\"scale_factor\",0.999601272],PARAMETER[\"false_easting\",400000],PARAMETER[\"false_northing\",-100000],UNIT[\"metre\",1,AUTHORITY[\"EPSG\",\"9001\"]],AXIS[\"E\",EAST],AXIS[\"N\",NORTH],AUTHORITY[\"EPSG\",\"27700\"]],VERT_CS[\"Newlyn\",VERT_DATUM[\"Ordnance Datum Newlyn\",2005,AUTHORITY[\"EPSG\",\"5101\"]],UNIT[\"metre\",1,AUTHORITY[\"EPSG\",\"9001\"]],AXIS[\"Up\",UP],AUTHORITY[\"EPSG\",\"5701\"]],AUTHORITY[\"EPSG\",\"7405\"]]";
    //char *tree=str;
    res = sr.Treebuild(str);
    return a.exec();
}
This question has already been answered. Start a new discussion instead.