<div dir="ltr">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:<br>
<br><br>For my feature class<br><code><br>#include <iostream><br>#include <vector><br> using namespace std;<br>typedef struct featurePoint{<br> double x;<br> double y;<br>};<br>typedef vector<featurePoint *> myFeaturePoints;<br>
class myFeatureClass{<br> public:<br> double ID;<br> string F_CODE;<br> string F_CODE_DES;<br> string NAM;<br> string NA2;<br> string NA2_DESCRI;<br> string NA3;<br> string NA3_DESCRI;<br>
unsigned int TILE_ID;<br> double FAC_ID;<br> myFeaturePoints points;<br>};<br>typedef vector<myFeatureClass *> Features;<br></code><br><br>And main.cpp<br>/* Created by Anjuta version 1.2.4a */<br>
/* This file will not be overwritten */<br><br><br>#include "ogrsf_frmts.h"<br>#include "myFeature.h"<br>#include <iostream><br>#include <string><br>#include <vector><br>using namespace std;<br>
<br>int main()<br><br>{<br> <br>Features featuresVector;<br> <br>myFeatureClass feature;<br> <br>featurePoint *point;<br> <br> <br> OGRRegisterAll();<br><br> OGRDataSource *poDS;<br><br> poDS = OGRSFDriverRegistrar::Open( "bnd-political-boundary-a.shp", FALSE );<br>
if( poDS == NULL )<br> {<br> printf( "Open failed.\n" );<br> exit( -1 );<br> }<br><br> <br> int layerNumber;<br> layerNumber=poDS->GetLayerCount();<br><br> OGRLayer *poLayer;<br>
<br> poLayer = poDS->GetLayer(0);<br> <br><br> <br> OGRFeature *poFeature;<br><br> poLayer->ResetReading();<br> int asdf=poLayer->GetFeatureCount(1);<br> cout<<"Features counted "<<asdf<<endl;<br>
<br> int count=0;<br> while( (poFeature = poLayer->GetNextFeature()) != NULL )<br> {<br> OGRFeatureDefn *poFDefn = poLayer->GetLayerDefn();<br> <br> int iField;<br><br> for( iField = 0; iField < poFDefn->GetFieldCount(); iField++ )<br>
{<br> // OGRFieldDefn *poFieldDefn = poFDefn->GetFieldDefn( iField );<br> switch(iField){<br> case 0:feature.ID=poFeature->GetFieldAsDouble(iField);<br> case 1:feature.F_CODE=poFeature->GetFieldAsString(iField);<br>
case 2:feature.F_CODE_DES=poFeature->GetFieldAsString(iField);<br> case 3:feature.NAM=poFeature->GetFieldAsString(iField);<br> case 4:feature.NA2=poFeature->GetFieldAsString(iField);<br>
case 5:feature.NA2_DESCRI=poFeature->GetFieldAsString(iField);<br> case 6:feature.NA3=poFeature->GetFieldAsString(iField);<br> case 7:feature.NA3_DESCRI=poFeature->GetFieldAsString(iField);<br>
case 8:feature.TILE_ID=poFeature->GetFieldAsInteger(iField);<br> case 9:feature.FAC_ID=poFeature->GetFieldAsDouble(iField);<br> } <br> <br> }<br><br> OGRGeometry *poGeometry;<br>
<br> poGeometry = poFeature->GetGeometryRef();<br> if( poGeometry != NULL <br> && wkbFlatten(poGeometry->getGeometryType()) == wkbPolygon/*wkbPoint*/)<br> {<br> OGRPolygon *poPoint = (OGRPolygon *) poGeometry;<br>
OGRLinearRing *LR=poPoint->getExteriorRing(); <br> if(LR==NULL){<br> cout<<"No Poligono"<<endl;<br> } <br> else{<br> //~ cout<<"Si poligono"<<endl; <br>
int b=LR->getNumPoints();<br> //~ cout<<"Numero de puntos="<<b<<endl;<br> //~ int a=poPoint->getNumInteriorRings();<br> //~ cout<<"Numero de anillos internos="<<a<<endl;<br>
//~ //printf( "\n%.4f,%4.f\n", poPoint->getX(), poPoint->getY() );<br> for (int i=0;i<b;i++){<br> //cout<<"i="<<i<<endl;<br> OGRPoint *p=(OGRPoint *)poGeometry;<br>
LR->getPoint(i,p);<br> //cout<<"X="<<p->getX()<<" Y="<<p->getY()<<endl;<br> point->x=p->getX();<br>
point->y=p->getY();<br> feature.points.push_back(/*(featurePoint *)&*/point); <br> }<br> featuresVector.push_back(&feature);<br> <br>
int size=featuresVector.size();<br> <br> myFeatureClass *c=featuresVector[count];<br> cout<<"El Tamagno del vector de features es:"<<size<<endl; //////////In this chunk of code I can see all the features/////////<br>
cout<<"El ID de la primera feature es:"<<c->NAM<<endl;<br> <br> }<br> }<br> else<br> {<br> printf( "no point geometry\n" );<br>
} <br> //featuresVector.push_back((myFeatureClass *)&feature);<br> //feature=NULL;<br> count++;<br> //cout<<"***************************************************************"<<endl<<count<<endl;<br>
//OGRFeature::DestroyFeature( poFeature );<br> }getchar();<br> int size=featuresVector.size();<br> cout<<"El Tamagno del vector de features es:"<<size<<endl;<br> int j;<br> for (j=0;j<size;j++){ <br>
myFeatureClass *c=featuresVector[j];<br> cout<<"El ID de la "<<j<<"esima feature es:"<<c->NAM<<endl; ////////////////////////but in this nop////////////////////<br>
cout<<"punto x"<<c->points[0]->x<<endl; <br> } <br> OGRDataSource::DestroyDataSource( poDS );<br>}<br><br>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.<br>
<br>Thank you<br><br><br><br><br></div>