[FWTools] help

Oscar De Leon oleongomez at gmail.com
Thu Sep 18 14:52:17 EDT 2008


This is a chunk of code that iḿ writing based in  OGR tutorial but I don't
know what's going on with this. OGR is working well but I've a trouble with
stl vector. I'm using a vector to save the polygon feature points extracter
from shapefile obtained from vmap0 noamer ,for draw them later. The trouble
is: I'm saving a feature in a feature vector, but when i want to use it in
other code it only have the last feature repeated in  the whole vector.
There is the code:


For my feature class
<code>
#include <iostream>
#include <vector>
 using namespace std;
typedef    struct featurePoint{
    double x;
    double y;
};
typedef vector<featurePoint *> myFeaturePoints;
 class myFeatureClass{
     public:
      double  ID;
      string  F_CODE;
     string  F_CODE_DES;
     string  NAM;
     string  NA2;
     string  NA2_DESCRI;
     string  NA3;
     string  NA3_DESCRI;
     unsigned int TILE_ID;
     double  FAC_ID;
     myFeaturePoints points;
};
typedef vector<myFeatureClass *> Features;
</code>

And main.cpp
/* Created by Anjuta version 1.2.4a */
/*    This file will not be overwritten */


#include "ogrsf_frmts.h"
#include "myFeature.h"
#include <iostream>
#include <string>
#include <vector>
using namespace std;

int main()

{

Features featuresVector;

myFeatureClass feature;

featurePoint *point;


    OGRRegisterAll();

    OGRDataSource       *poDS;

        poDS = OGRSFDriverRegistrar::Open( "bnd-political-boundary-a.shp",
FALSE );
    if( poDS == NULL )
    {
        printf( "Open failed.\n" );
        exit( -1 );
    }


    int layerNumber;
    layerNumber=poDS->GetLayerCount();

    OGRLayer  *poLayer;

    poLayer = poDS->GetLayer(0);



    OGRFeature *poFeature;

    poLayer->ResetReading();
   int asdf=poLayer->GetFeatureCount(1);
    cout<<"Features counted "<<asdf<<endl;

    int count=0;
    while( (poFeature = poLayer->GetNextFeature()) != NULL )
    {
        OGRFeatureDefn *poFDefn = poLayer->GetLayerDefn();

        int iField;

        for( iField = 0; iField < poFDefn->GetFieldCount(); iField++ )
        {
           // OGRFieldDefn *poFieldDefn = poFDefn->GetFieldDefn( iField );
            switch(iField){
                 case 0:feature.ID=poFeature->GetFieldAsDouble(iField);
                case 1:feature.F_CODE=poFeature->GetFieldAsString(iField);
                case
2:feature.F_CODE_DES=poFeature->GetFieldAsString(iField);
                case 3:feature.NAM=poFeature->GetFieldAsString(iField);
                case 4:feature.NA2=poFeature->GetFieldAsString(iField);
                case
5:feature.NA2_DESCRI=poFeature->GetFieldAsString(iField);
                case 6:feature.NA3=poFeature->GetFieldAsString(iField);
                case
7:feature.NA3_DESCRI=poFeature->GetFieldAsString(iField);
                case 8:feature.TILE_ID=poFeature->GetFieldAsInteger(iField);
                case 9:feature.FAC_ID=poFeature->GetFieldAsDouble(iField);
            }

        }

        OGRGeometry *poGeometry;

        poGeometry = poFeature->GetGeometryRef();
         if( poGeometry != NULL
            && wkbFlatten(poGeometry->getGeometryType()) ==
wkbPolygon/*wkbPoint*/)
        {
            OGRPolygon *poPoint = (OGRPolygon *) poGeometry;
            OGRLinearRing *LR=poPoint->getExteriorRing();
            if(LR==NULL){
                cout<<"No Poligono"<<endl;
            }
            else{
            //~ cout<<"Si poligono"<<endl;
             int b=LR->getNumPoints();
            //~ cout<<"Numero de puntos="<<b<<endl;
            //~ int a=poPoint->getNumInteriorRings();
            //~ cout<<"Numero de anillos internos="<<a<<endl;
            //~ //printf( "\n%.4f,%4.f\n", poPoint->getX(), poPoint->getY()
);
                for (int i=0;i<b;i++){
                     //cout<<"i="<<i<<endl;
                     OGRPoint *p=(OGRPoint *)poGeometry;
                     LR->getPoint(i,p);
                     //cout<<"X="<<p->getX()<<" Y="<<p->getY()<<endl;
                    point->x=p->getX();
                    point->y=p->getY();
                    feature.points.push_back(/*(featurePoint *)&*/point);

                }
                featuresVector.push_back(&feature);

                int size=featuresVector.size();

    myFeatureClass *c=featuresVector[count];
    cout<<"El Tamagno del vector de features es:"<<size<<endl; //////////In
this chunk of code I can see all the features/////////
    cout<<"El ID de la primera feature es:"<<c->NAM<<endl;

            }
        }
        else
        {
            printf( "no point geometry\n" );
        }
        //featuresVector.push_back((myFeatureClass *)&feature);
        //feature=NULL;
        count++;

//cout<<"***************************************************************"<<endl<<count<<endl;
       //OGRFeature::DestroyFeature( poFeature );
    }getchar();
    int size=featuresVector.size();
    cout<<"El Tamagno del vector de features es:"<<size<<endl;
    int j;
    for (j=0;j<size;j++){
    myFeatureClass *c=featuresVector[j];
    cout<<"El ID de la "<<j<<"esima feature es:"<<c->NAM<<endl;
////////////////////////but in this nop////////////////////
    cout<<"punto x"<<c->points[0]->x<<endl;
    }
    OGRDataSource::DestroyDataSource( poDS );
}

This is a newbie error and not a GDAL/OGR issue and this isn't the rigth
place to ask for an answer but nobody help me before. I know but I don't
know what I'm doing wrong.

Thank you
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.maptools.org/pipermail/fwtools/attachments/20080918/dd00b480/attachment.html


More information about the FWTools mailing list