| pktools 2.6.7 Processing Kernel for geospatial data

Public Member Functions | Protected Member Functions | Protected Attributes | Friends | List of all members
ImgReaderOgr Class Reference
Collaboration diagram for ImgReaderOgr:
[legend]

## Public Member Functions
| ImgReaderOgr (const std::string &filename)
void | open (const std::string &filename)
void | close (void)
template<typename T >
int | readXY (std::vector< T > &xVector, std::vector< T > &yVector, int layer=0, bool verbose=false)
template<typename T >
int | readY (std::vector< T > &yVector, int layer=0, bool verbose=false)
template<typename T >
int | readData (std::vector< T > &data, const OGRFieldType &fieldType, std::vector< std::string > &fields, OGRFeature *poFeature, int layer=0, bool pos=false, bool verbose=false)
template<typename T >
int | readData (std::vector< T > &data, const OGRFieldType &fieldType, const std::string &theField, int layer=0, bool verbose=false)
template<typename T >
int | readData (Vector2d< T > &data, const OGRFieldType &fieldType, std::vector< std::string > &fields, int layer=0, bool pos=false, bool verbose=false)
template<typename T >
int | readData (std::map< int, Vector2d< T > > &data, const OGRFieldType &fieldType, std::vector< std::string > &fields, const std::string &label, int layer=0, bool pos=false, bool verbose=false)
template<typename T >
int | readData (std::map< std::string, Vector2d< T > > &data, const OGRFieldType &fieldType, std::vector< std::string > &fields, const std::string &label, int layer=0, bool pos=false, bool verbose=false)
unsigned int | readDataImageOgr (std::map< std::string, Vector2d< float > > &mapPixels, std::vector< std::string > &fields, const std::vector< unsigned short > &bands, const std::string &label, const std::vector< std::string > &layers, int verbose=false)
unsigned int | readDataImageOgr (std::map< std::string, Vector2d< float > > &mapPixels, std::vector< std::string > &fields, double start, double end, const std::string &label, const std::vector< std::string > &layers, int verbose=false)
unsigned long int | getFeatureCount (int layer=0) const
int | getFieldCount (int layer=0) const
OGRLayer * | getLayer (int layer=0)
std::string | getProjection (int layer=0) const
OGRwkbGeometryType | getGeometryType (int layer=0) const
std::string | getLayerName (int layer=0)
int | getFields (std::vector< std::string > &fields, int layer=0) const
int | getFields (std::vector< OGRFieldDefn * > &fields, int layer=0) const
OGRDataSource * | getDataSource (void)
OGRSFDriver * | getDriver (void) const
int | getLayerCount (void) const
template<typename T >
int | readSql (Vector2d< T > &data, const OGRFieldType &fieldType, std::vector< std::string > &fields, const std::string &sqlStatement, OGRGeometry *spatialFilter=NULL, int layer=0, bool pos=false, bool verbose=false)
template<typename T >
int | readSql (std::map< int, Vector2d< T > > &data, const OGRFieldType &fieldType, std::vector< std::string > &fields, const std::string &label, const std::string &sqlStatement, OGRGeometry *spatialFilter, int layer=0, bool pos=false, bool verbose=false)
bool | getExtent (double &ulx, double &uly, double &lrx, double &lry, int layer)
bool | getExtent (double &ulx, double &uly, double &lrx, double &lry)
void | setFieldSeparator (const char fs)
char | getFieldSeparator () const
template<>
int | readData (std::vector< std::string > &data, const OGRFieldType &fieldType, const std::string &theField, int layer, bool verbose)

## Protected Member Functions
void | setCodec (void)

## Protected Attributes
std::string | m_filename
OGRDataSource * | m_datasource
char | m_fs

## Friends
std::ostream & | operator<< (std::ostream &theOstream, ImgReaderOgr &theImageReader)

## Detailed Description

Definition at line 35 of file ImgReaderOgr.h.

## Constructor & Destructor Documentation

## ◆ ImgReaderOgr() [1/2]

ImgReaderOgr::ImgReaderOgr | ( | void |  | ) |

Definition at line 26 of file ImgReaderOgr.cc.

27 : m_fs(' ')
28{}

## ◆ ImgReaderOgr() [2/2]

ImgReaderOgr::ImgReaderOgr | ( | const std::string & | filename | ) |

Definition at line 30 of file ImgReaderOgr.cc.

31{
32 open(filename);
33}

## ◆ ~ImgReaderOgr()

ImgReaderOgr::~ImgReaderOgr | ( | void |  | ) |

Definition at line 35 of file ImgReaderOgr.cc.

36{
37}

## Member Function Documentation

## ◆ close()

void ImgReaderOgr::close | ( | void |  | ) |

Definition at line 49 of file ImgReaderOgr.cc.

50{
51#if GDAL_VERSION_MAJOR < 2
52 OGRDataSource::DestroyDataSource(m_datasource);
53#else
54 GDALClose(m_datasource);
55#endif
56}

## ◆ getDataSource()

OGRDataSource * ImgReaderOgr::getDataSource | ( | void |  | ) |
| inline

Definition at line 77 of file ImgReaderOgr.h.

77{return m_datasource;};

## ◆ getDriver()

OGRSFDriver * ImgReaderOgr::getDriver | ( | void |  | ) | const
| inline

Definition at line 78 of file ImgReaderOgr.h.

78{return m_datasource->GetDriver();};

## ◆ getExtent() [1/2]

bool ImgReaderOgr::getExtent | ( | double & | ulx,
|  | double & | uly,
|  | double & | lrx,
|  | double & | lry
| ) |  |

Definition at line 100 of file ImgReaderOgr.cc.

101{
102 bool success=false;
103 OGREnvelope oExt;
104 for(int ilayer=0;ilayer<getLayerCount();++ilayer){
105 if(getLayer(ilayer)->GetExtent(&oExt,TRUE)==OGRERR_NONE){
106 if(!ilayer){
107 ulx=oExt.MinX;
108 uly=oExt.MaxY;
109 lrx=oExt.MaxX;
110 lry=oExt.MinY;
111 }
112 else{
113 if(ulx>oExt.MinX)
114 ulx=oExt.MinX;
115 if(uly<oExt.MaxY)
116 uly=oExt.MaxY;
117 if(lrx<oExt.MaxX)
118 lrx=oExt.MaxX;
119 if(lry>oExt.MinY)
120 lry=oExt.MinY;
121 }
122 success=true;
123 }
124 else
125 success=false;
126 }
127 return success;
128}

## ◆ getExtent() [2/2]

bool ImgReaderOgr::getExtent | ( | double & | ulx,
|  | double & | uly,
|  | double & | lrx,
|  | double & | lry,
|  | int | layer
| ) |  |

Definition at line 86 of file ImgReaderOgr.cc.

87{
88 OGREnvelope oExt;
89 if(getLayer(layer)->GetExtent(&oExt,TRUE)==OGRERR_NONE){
90 ulx=oExt.MinX;
91 uly=oExt.MaxY;
92 lrx=oExt.MaxX;
93 lry=oExt.MinY;
94 return true;
95 }
96 else
97 return false;
98}

## ◆ getFeatureCount()

unsigned long int ImgReaderOgr::getFeatureCount | ( | int | layer = `0` | ) | const

Definition at line 130 of file ImgReaderOgr.cc.

131{
132 return(m_datasource->GetLayer(layer)->GetFeatureCount());
133}

## ◆ getFieldCount()

int ImgReaderOgr::getFieldCount | ( | int | layer = `0` | ) | const

Definition at line 135 of file ImgReaderOgr.cc.

136{
137 if(layer<0)
138 layer=m_datasource->GetLayerCount()-1;
139 assert(m_datasource->GetLayerCount()>layer);
140 OGRLayer *poLayer;
141 if((poLayer = m_datasource->GetLayer(layer))==NULL){
142 std::string errorstring="Could not get layer";
143 throw(errorstring);
144 }
145 OGRFeatureDefn *poFDefn = poLayer->GetLayerDefn();
146 return(poFDefn->GetFieldCount());
147}

## ◆ getFields() [1/2]

int ImgReaderOgr::getFields | ( | std::vector< OGRFieldDefn * > & | fields,
|  | int | layer = `0`
| ) |  | const

Definition at line 169 of file ImgReaderOgr.cc.

170{
171 if(layer<0)
172 layer=m_datasource->GetLayerCount()-1;
173 assert(m_datasource->GetLayerCount()>layer);
174 OGRLayer *poLayer;
175 if((poLayer = m_datasource->GetLayer(layer))==NULL){
176 std::string errorstring="Could not get layer";
177 throw(errorstring);
178 }
179 OGRFeatureDefn *poFDefn = poLayer->GetLayerDefn();
180 fields.clear();
181 fields.resize(poFDefn->GetFieldCount());
182 for(int iField=0;iField<poFDefn->GetFieldCount();++iField){
183 OGRFieldDefn *poFieldDefn = poFDefn->GetFieldDefn(iField);
184 fields[iField]=poFDefn->GetFieldDefn(iField);
185 }
186 assert(fields.size()==getFieldCount(layer));
187 return(fields.size());
188}

## ◆ getFields() [2/2]

int ImgReaderOgr::getFields | ( | std::vector< std::string > & | fields,
|  | int | layer = `0`
| ) |  | const

Definition at line 149 of file ImgReaderOgr.cc.

150{
151 if(layer<0)
152 layer=m_datasource->GetLayerCount()-1;
153 assert(m_datasource->GetLayerCount()>layer);
154 OGRLayer *poLayer;
155 if((poLayer = m_datasource->GetLayer(layer))==NULL){
156 std::string errorstring="Could not get layer";
157 throw(errorstring);
158 }
159 OGRFeatureDefn *poFDefn = poLayer->GetLayerDefn();
160 fields.clear();
161 fields.resize(poFDefn->GetFieldCount());
162 for(int iField=0;iField<poFDefn->GetFieldCount();++iField){
163 OGRFieldDefn *poFieldDefn = poFDefn->GetFieldDefn(iField);
164 fields[iField]=poFieldDefn->GetNameRef();
165 }
166 return(fields.size());
167}

## ◆ getFieldSeparator()

char ImgReaderOgr::getFieldSeparator | ( |  | ) | const
| inline

Definition at line 91 of file ImgReaderOgr.h.

91{ return m_fs;};

## ◆ getGeometryType()

OGRwkbGeometryType ImgReaderOgr::getGeometryType | ( | int | layer = `0` | ) | const

Definition at line 201 of file ImgReaderOgr.cc.

202{
203 return m_datasource->GetLayer(layer)->GetLayerDefn()->GetGeomType();
204}

## ◆ getLayer()

OGRLayer * ImgReaderOgr::getLayer | ( | int | layer = `0` | ) |
| inline

Definition at line 69 of file ImgReaderOgr.h.

69{return m_datasource->GetLayer(layer);};

## ◆ getLayerCount()

int ImgReaderOgr::getLayerCount | ( | void |  | ) | const
| inline

Definition at line 83 of file ImgReaderOgr.h.

83{return m_datasource->GetLayerCount();};

## ◆ getLayerName()

std::string ImgReaderOgr::getLayerName | ( | int | layer = `0` | ) |
| inline

Definition at line 72 of file ImgReaderOgr.h.

72{return m_datasource->GetLayer(layer)->GetLayerDefn()->GetName();};

## ◆ getProjection()

std::string ImgReaderOgr::getProjection | ( | int | layer = `0` | ) | const

Definition at line 190 of file ImgReaderOgr.cc.

191{
192 if(m_datasource->GetLayer(layer)->GetSpatialRef()){
193 char* ppszResult;
194 m_datasource->GetLayer(layer)->GetSpatialRef()->exportToWkt(&ppszResult);
195 return(ppszResult);
196 }
197 else
198 return "";
199}

## ◆ open()

void ImgReaderOgr::open | ( | const std::string & | filename | ) |

Definition at line 41 of file ImgReaderOgr.cc.

42{
43 m_fs=' ';
44 m_filename = filename;
45 setCodec();
46}

## ◆ readData() [1/6]

template<typename T >

int ImgReaderOgr::readData | ( | std::map< int, Vector2d< T > > & | data,
|  | const OGRFieldType & | fieldType,
|  | std::vector< std::string > & | fields,
|  | const std::string & | label,
|  | int | layer = `0`,
|  | bool | pos = `false`,
|  | bool | verbose = `false`
| ) |  |

Definition at line 107 of file ImgReaderOgr.h.

108{
109 assert(m_datasource->GetLayerCount()>layer);
110 OGRLayer *poLayer;
111 if(verbose)
112 std::cout << "number of layers: " << m_datasource->GetLayerCount() << std::endl;
113 poLayer = m_datasource->GetLayer(layer);
114 if(poLayer!=NULL){
115 OGRFeatureDefn *poFDefn = poLayer->GetLayerDefn();
116 if(fields.empty()){
117 fields.resize(poFDefn->GetFieldCount());
118 if(verbose)
119 std::cout << "resized fields to " << fields.size() << std::endl;
120 }
121 //start reading features from the layer
122 OGRFeature *poFeature;
123 if(verbose)
124 std::cout << "reset reading" << std::endl;
125 poLayer->ResetReading();
126 unsigned long int ifeature=0;
127 int posOffset=(pos)?2:0;
128 if(verbose)
129 std::cout << "going through features" << std::endl << std::flush;
130 int theClass=0;
131 while( (poFeature = poLayer->GetNextFeature()) != NULL ){
132 std::vector<T> theFeature;//(fields.size()+posOffset);//x,y+selectedfields
133 if(verbose)
134 std::cout << "reading feature " << ifeature << std::endl << std::flush;
135 OGRGeometry *poGeometry;
136 poGeometry = poFeature->GetGeometryRef();
137 if(verbose){
138 if(poGeometry == NULL)
139 std::cerr << "no geometry defined" << std::endl << std::flush;
140 else if(wkbFlatten(poGeometry->getGeometryType()) != wkbPoint)
141 std::cerr << "Warning: poGeometry type: " << wkbFlatten(poGeometry->getGeometryType()) << std::endl << std::flush;
142 }
143 assert(poGeometry != NULL );
144 // && wkbFlatten(poGeometry->getGeometryType()) == wkbPoint);
145 if(pos){
146 if(wkbFlatten(poGeometry->getGeometryType()) == wkbPoint){
147 OGRPoint *poPoint;
148 poPoint = (OGRPoint *) poGeometry;
149 theFeature.push_back(poPoint->getX());
150 theFeature.push_back(poPoint->getY());
151 }
152 else if(wkbFlatten(poGeometry->getGeometryType()) == wkbPolygon){
153 OGRPoint thePoint;
154 OGRPolygon * poPolygon = (OGRPolygon *) poGeometry;
155 poPolygon->Centroid(&thePoint);
156 theFeature.push_back(thePoint.getX());
157 theFeature.push_back(thePoint.getY());
158 }
159 else{
160 //Centroid for non polygon geometry not supported until OGR 1.8.0, comment out if version < 1.8.0 is installed...";
161 OGRPoint thePoint;
162 poGeometry->Centroid(&thePoint);
163 theFeature.push_back(thePoint.getX());
164 theFeature.push_back(thePoint.getY());
165 }
166 }
167 // OGRFeatureDefn *poFDefn = poLayer->GetLayerDefn();
168 std::string featurename;
169 for(int iField=0;iField<poFDefn->GetFieldCount();++iField){
170 OGRFieldDefn *poFieldDefn = poFDefn->GetFieldDefn(iField);
171 std::string fieldname=poFieldDefn->GetNameRef();
172 if(fieldname==label)
173 theClass=poFeature->GetFieldAsInteger(iField);
174 else{
175 switch(fieldType){
176 case(OFTReal):
177 if(fields.size()<poFDefn->GetFieldCount()){
178 if(find(fields.begin(),fields.end(),fieldname)!=fields.end())
179 theFeature.push_back(poFeature->GetFieldAsDouble(iField));
180 }
181 else{
182 fields[iField]=fieldname;
183 theFeature.push_back(poFeature->GetFieldAsDouble(iField));
184 }
185 break;
186 case(OFTInteger):
187 if(fields.size()<poFDefn->GetFieldCount()){
188 if(find(fields.begin(),fields.end(),fieldname)!=fields.end())
189 theFeature.push_back(poFeature->GetFieldAsDouble(iField));
190 }
191 else{
192 fields[iField]=fieldname;
193 theFeature.push_back(poFeature->GetFieldAsDouble(iField));
194 }
195 break;
196 default:
197 {
198 std::string errorstring="field type not supported in ImgReaderOgr::ReadData";
199 throw(errorstring);
200 }
201 break;
202 }
203 }
204 }
205 data[theClass].push_back(theFeature);
206 ++ifeature;
207 ++ifeature;
208 }
209 if(verbose)
210 std::cout << "number of features read: " << ifeature << std::endl << std::flush;
211 typename std::map<int,Vector2d<T> >::const_iterator mit=data.begin();
212 int nband=0;
213 if(verbose)
214 std::cout << "read classes: " << std::flush;
215 while(mit!=data.end()){
216 if(verbose)
217 std::cout << mit->first << " " << std::flush;
218 if(!nband)
219 nband=fields.size();
220 if(pos)
221 assert((mit->second)[0].size()==nband+2);
222 else
223 assert((mit->second)[0].size()==nband);
224 ++mit;
225 }
226 if(verbose)
227 std::cout << std::endl << std::flush;
228 return(nband);
229 }
230 else{
231 std::ostringstream ess;
232 ess << "no layer in " << m_filename;
233 throw(ess.str());
234 }
235}

## ◆ readData() [2/6]

template<typename T >

int ImgReaderOgr::readData | ( | std::map< std::string, Vector2d< T > > & | data,
|  | const OGRFieldType & | fieldType,
|  | std::vector< std::string > & | fields,
|  | const std::string & | label,
|  | int | layer = `0`,
|  | bool | pos = `false`,
|  | bool | verbose = `false`
| ) |  |

Definition at line 238 of file ImgReaderOgr.h.

239{
240 assert(m_datasource->GetLayerCount()>layer);
241 OGRLayer *poLayer;
242 if(verbose)
243 std::cout << "number of layers: " << m_datasource->GetLayerCount() << std::endl;
244 poLayer = m_datasource->GetLayer(layer);
245 if(poLayer!=NULL){
246 OGRFeatureDefn *poFDefn = poLayer->GetLayerDefn();
247 assert(poFDefn!=NULL);
248
249 if(fields.empty()){
250 fields.resize(poFDefn->GetFieldCount());
251 if(verbose)
252 std::cout << "resized fields to " << fields.size() << std::endl;
253 }
254
255 //start reading features from the layer
256 OGRFeature *poFeature;
257 if(verbose)
258 std::cout << "reset reading" << std::endl;
259 poLayer->ResetReading();
260 unsigned long int ifeature=0;
261 int posOffset=(pos)?2:0;
262 if(verbose)
263 std::cout << "going through features to fill in string map" << std::endl << std::flush;
264 std::string theClass;
265 while( (poFeature = poLayer->GetNextFeature()) != NULL ){
266 std::vector<T> theFeature;//(fields.size()+posOffset);//x,y+selectedfields
267 if(verbose)
268 std::cout << "reading feature " << ifeature << std::endl << std::flush;
269 OGRGeometry *poGeometry;
270 poGeometry = poFeature->GetGeometryRef();
271 if(verbose){
272 if(poGeometry == NULL)
273 std::cerr << "no geometry defined" << std::endl << std::flush;
274 else if(wkbFlatten(poGeometry->getGeometryType()) != wkbPoint)
275 std::cerr << "Warning: poGeometry type: " << wkbFlatten(poGeometry->getGeometryType()) << std::endl << std::flush;
276 }
277 assert(poGeometry != NULL );
278 // && wkbFlatten(poGeometry->getGeometryType()) == wkbPoint);
279 if(pos){
280 if(wkbFlatten(poGeometry->getGeometryType()) == wkbPoint){
281 OGRPoint *poPoint;
282 poPoint = (OGRPoint *) poGeometry;
283 theFeature.push_back(poPoint->getX());
284 theFeature.push_back(poPoint->getY());
285 }
286 else if(wkbFlatten(poGeometry->getGeometryType()) == wkbPolygon){
287 OGRPoint thePoint;
288 poGeometry->Centroid(&thePoint);
289 theFeature.push_back(thePoint.getX());
290 theFeature.push_back(thePoint.getY());
291 }
292 else{
293 //Centroid for non polygon geometry not supported until OGR 1.8.0, comment out if version < 1.8.0 is installed...";
294 OGRPoint thePoint;
295 poGeometry->Centroid(&thePoint);
296 theFeature.push_back(thePoint.getX());
297 theFeature.push_back(thePoint.getY());
298 }
299 }
300 // OGRFeatureDefn *poFDefn = poLayer->GetLayerDefn();//got LayerDefn already...
301 std::string featurename;
302 for(int iField=0;iField<poFDefn->GetFieldCount();++iField){
303 OGRFieldDefn *poFieldDefn = poFDefn->GetFieldDefn(iField);
304 std::string fieldname=poFieldDefn->GetNameRef();
305 if(fieldname==label){
306 theClass=poFeature->GetFieldAsString(iField);
307 if(verbose)
308 std::cout << "read feature for " << theClass << std::endl;
309 }
310 else{
311 switch(fieldType){
312 case(OFTReal):
313 if(fields.size()<poFDefn->GetFieldCount()){
314 if(find(fields.begin(),fields.end(),fieldname)!=fields.end())
315 theFeature.push_back(poFeature->GetFieldAsDouble(iField));
316 }
317 else{
318 fields[iField]=fieldname;
319 theFeature.push_back(poFeature->GetFieldAsDouble(iField));
320 }
321 break;
322 case(OFTInteger):
323 if(fields.size()<poFDefn->GetFieldCount()){
324 if(find(fields.begin(),fields.end(),fieldname)!=fields.end())
325 theFeature.push_back(poFeature->GetFieldAsDouble(iField));
326 }
327 else{
328 fields[iField]=fieldname;
329 theFeature.push_back(poFeature->GetFieldAsDouble(iField));
330 }
331 break;
332 default:
333 {
334 std::string errorstring="field type not supported in ImgReaderOgr::ReadData";
335 throw(errorstring);
336 }
337 break;
338 }
339 }
340 assert(poFDefn!=NULL);
341 }
342 data[theClass].push_back(theFeature);
343 ++ifeature;
344 }
345 if(verbose)
346 std::cout << "number of features read: " << ifeature << std::endl << std::flush;
347 typename std::map<std::string,Vector2d<T> >::const_iterator mit=data.begin();
348 int nband=0;
349 if(verbose)
350 std::cout << "read classes: " << std::flush;
351 while(mit!=data.end()){
352 if(verbose)
353 std::cout << mit->first << " " << std::flush;
354 if(!nband)
355 nband=fields.size();
356 if(pos)
357 assert((mit->second)[0].size()==nband+2);
358 else
359 assert((mit->second)[0].size()==nband);
360 ++mit;
361 }
362 if(verbose)
363 std::cout << std::endl << std::flush;
364 return(nband);
365 }
366 else{
367 std::ostringstream ess;
368 ess << "no layer in " << m_filename;
369 throw(ess.str());
370 }
371}

## ◆ readData() [3/6]

template<>

int ImgReaderOgr::readData | ( | std::vector< std::string > & | data,
|  | const OGRFieldType & | fieldType,
|  | const std::string & | theField,
|  | int | layer,
|  | bool | verbose
| ) |  |
| inline

Definition at line 588 of file ImgReaderOgr.h.

589{
590 assert(m_datasource->GetLayerCount()>layer);
591 OGRLayer *poLayer;
592 if(verbose)
593 std::cout << "number of layers: " << m_datasource->GetLayerCount() << std::endl;
594 poLayer = m_datasource->GetLayer(layer);
595 OGRFeatureDefn *poFDefn = poLayer->GetLayerDefn();
596 int nfield=(theField!="")? poFDefn->GetFieldCount() : 1;
597 if(theField==""){
598 //read first field available
599 if(verbose)
600 std::cout << "read first field from total of " << nfield << std::endl;
601 }
602
603 //start reading features from the layer
604 OGRFeature *poFeature;
605 if(verbose)
606 std::cout << "reset reading" << std::endl;
607 poLayer->ResetReading();
608 unsigned long int ifeature=0;
609 if(verbose)
610 std::cout << "going through features" << std::endl << std::flush;
611 while( (poFeature = poLayer->GetNextFeature()) != NULL ){
612 std::string theFeature;
613 if(verbose)
614 std::cout << "reading feature " << ifeature << std::endl << std::flush;
615 OGRGeometry *poGeometry;
616 poGeometry = poFeature->GetGeometryRef();
617 if(verbose){
618 if(poGeometry == NULL)
619 std::cerr << "no geometry defined" << std::endl << std::flush;
620 else// if(wkbFlatten(poGeometry->getGeometryType()) != wkbPoint)
621 std::cout << "poGeometry type: " << wkbFlatten(poGeometry->getGeometryType()) << std::endl;
622 }
623 // assert(poGeometry != NULL
624 // && wkbFlatten(poGeometry->getGeometryType()) == wkbPoint);
625 OGRPoint *poPoint = (OGRPoint *) poGeometry;
626
627 for(int iField=0;iField<nfield;++iField){
628 OGRFieldDefn *poFieldDefn = poFDefn->GetFieldDefn(iField);
629 std::string fieldname=poFieldDefn->GetNameRef();
630 if(fieldname!=theField)
631 continue;
632 switch(fieldType){
633 case(OFTInteger):
634 case(OFTReal):
635 case(OFTString):
636 theFeature=poFeature->GetFieldAsString(iField);
637 break;
638 default:
639 {
640 std::string errorstring="field type not supported in ImgReaderOgr::ReadData";
641 throw(errorstring);
642 }
643 break;
644 }
645 }
646 data.push_back(theFeature);
647 if(verbose)
648 std::cout << "feature is: " << theFeature << std::endl;
649 ++ifeature;
650 }
651 if(data.size()){
652 return data.size();
653 }
654 else{
655 std::ostringstream ess;
656 ess << "no layer in " << m_filename;
657 throw(ess.str());
658 }
659}

## ◆ readData() [4/6]

template<typename T >

int ImgReaderOgr::readData | ( | std::vector< T > & | data,
|  | const OGRFieldType & | fieldType,
|  | const std::string & | theField,
|  | int | layer = `0`,
|  | bool | verbose = `false`
| ) |  |
| inline

Definition at line 514 of file ImgReaderOgr.h.

515{
516 assert(m_datasource->GetLayerCount()>layer);
517 OGRLayer *poLayer;
518 if(verbose)
519 std::cout << "number of layers: " << m_datasource->GetLayerCount() << std::endl;
520 poLayer = m_datasource->GetLayer(layer);
521 OGRFeatureDefn *poFDefn = poLayer->GetLayerDefn();
522 int nfield=(theField!="")? poFDefn->GetFieldCount() : 1;
523 if(theField==""){
524 //read first field available
525 if(verbose)
526 std::cout << "read first field from total of " << nfield << std::endl;
527 }
528
529 //start reading features from the layer
530 OGRFeature *poFeature;
531 if(verbose)
532 std::cout << "reset reading" << std::endl;
533 poLayer->ResetReading();
534 unsigned long int ifeature=0;
535 if(verbose)
536 std::cout << "going through features" << std::endl << std::flush;
537 while( (poFeature = poLayer->GetNextFeature()) != NULL ){
538 // std::vector<T> theFeature;//(fields.size()+posOffset);//x,y+selectedfields
539 T theFeature;
540 if(verbose)
541 std::cout << "reading feature " << ifeature << std::endl << std::flush;
542 OGRGeometry *poGeometry;
543 poGeometry = poFeature->GetGeometryRef();
544 if(verbose){
545 if(poGeometry == NULL)
546 std::cerr << "no geometry defined" << std::endl << std::flush;
547 else// if(wkbFlatten(poGeometry->getGeometryType()) != wkbPoint)
548 std::cout << "poGeometry type: " << wkbFlatten(poGeometry->getGeometryType()) << std::endl;
549 }
550 // assert(poGeometry != NULL
551 // && wkbFlatten(poGeometry->getGeometryType()) == wkbPoint);
552 OGRPoint *poPoint = (OGRPoint *) poGeometry;
553
554 for(int iField=0;iField<nfield;++iField){
555 OGRFieldDefn *poFieldDefn = poFDefn->GetFieldDefn(iField);
556 std::string fieldname=poFieldDefn->GetNameRef();
557 if(fieldname!=theField)
558 continue;
559 switch(fieldType){
560 case(OFTInteger):
561 case(OFTReal):
562 theFeature=poFeature->GetFieldAsDouble(iField);
563 break;
564 default:
565 {
566 std::string errorstring="field type not supported in ImgReaderOgr::ReadData";
567 throw(errorstring);
568 }
569 break;
570 }
571 }
572 data.push_back(theFeature);
573 if(verbose)
574 std::cout << "feature is: " << theFeature << std::endl;
575 ++ifeature;
576 }
577 if(data.size()){
578 return data.size();
579 }
580 else{
581 std::ostringstream ess;
582 ess << "no layer in " << m_filename;
583 throw(ess.str());
584 }
585}

## ◆ readData() [5/6]

template<typename T >

int ImgReaderOgr::readData | ( | std::vector< T > & | data,
|  | const OGRFieldType & | fieldType,
|  | std::vector< std::string > & | fields,
|  | OGRFeature * | poFeature,
|  | int | layer = `0`,
|  | bool | pos = `false`,
|  | bool | verbose = `false`
| ) |  |

Definition at line 419 of file ImgReaderOgr.h.

420{
421 assert(m_datasource->GetLayerCount()>layer);
422 OGRLayer *poLayer;
423 if(verbose)
424 std::cout << "number of layers: " << m_datasource->GetLayerCount() << std::endl;
425 poLayer = m_datasource->GetLayer(layer);
426 OGRFeatureDefn *poFDefn = poLayer->GetLayerDefn();
427 if(fields.empty()){
428 fields.resize(poFDefn->GetFieldCount());
429 if(verbose)
430 std::cout << "resized fields to " << fields.size() << std::endl;
431 }
432 OGRGeometry *poGeometry;
433 poGeometry = poFeature->GetGeometryRef();
434 if(verbose){
435 if(poGeometry == NULL)
436 std::cerr << "no geometry defined" << std::endl << std::flush;
437 else if(wkbFlatten(poGeometry->getGeometryType()) != wkbPoint)
438 std::cerr << "poGeometry type: " << wkbFlatten(poGeometry->getGeometryType()) << std::endl << std::flush;
439 }
440 assert(poGeometry != NULL);
441 // && wkbFlatten(poGeometry->getGeometryType()) == wkbPoint);
442 OGRPoint *poPoint = (OGRPoint *) poGeometry;
443 if(pos){
444 if(wkbFlatten(poGeometry->getGeometryType()) == wkbPoint){
445 OGRPoint *poPoint;
446 poPoint = (OGRPoint *) poGeometry;
447 data.push_back(poPoint->getX());
448 data.push_back(poPoint->getY());
449 }
450 else if(wkbFlatten(poGeometry->getGeometryType()) == wkbPolygon){
451 OGRPoint thePoint;
452 poGeometry->Centroid(&thePoint);
453 data.push_back(thePoint.getX());
454 data.push_back(thePoint.getY());
455 }
456 else{
457 //Centroid for non polygon geometry not supported until OGR 1.8.0, comment out if version < 1.8.0 is installed...";
458 OGRPoint thePoint;
459 poGeometry->Centroid(&thePoint);
460 data.push_back(thePoint.getX());
461 data.push_back(thePoint.getY());
462 }
463
464 }
465 std::string featurename;
466 for(int iField=0;iField<poFDefn->GetFieldCount();++iField){
467 OGRFieldDefn *poFieldDefn = poFDefn->GetFieldDefn(iField);
468 std::string fieldname=poFieldDefn->GetNameRef();
469 switch(fieldType){
470 case(OFTReal):
471 if(fields.size()<poFDefn->GetFieldCount()){
472 if(find(fields.begin(),fields.end(),fieldname)!=fields.end())
473 data.push_back(poFeature->GetFieldAsDouble(iField));
474 }
475 else{
476 fields[iField]=fieldname;
477 data.push_back(poFeature->GetFieldAsDouble(iField));
478 }
479 break;
480 case(OFTInteger):
481 if(fields.size()<poFDefn->GetFieldCount()){
482 if(find(fields.begin(),fields.end(),fieldname)!=fields.end())
483 data.push_back(poFeature->GetFieldAsDouble(iField));
484 }
485 else{
486 fields[iField]=fieldname;
487 data.push_back(poFeature->GetFieldAsDouble(iField));
488 }
489 break;
490 default:
491 {
492 std::string errorstring="field type not supported in ImgReaderOgr::ReadData";
493 throw(errorstring);
494 }
495 break;
496 }
497 }
498 // assert(data.size()==ifeature);
499 if(data.size()){
500 if(pos)
501 assert(data.size()==fields.size()+2);
502 else
503 assert(data.size()==fields.size());
504 return fields.size();
505 }
506 else{
507 std::ostringstream ess;
508 ess << "no layer in " << m_filename;
509 throw(ess.str());
510 }
511}

## ◆ readData() [6/6]

template<typename T >

int ImgReaderOgr::readData | ( | Vector2d< T > & | data,
|  | const OGRFieldType & | fieldType,
|  | std::vector< std::string > & | fields,
|  | int | layer = `0`,
|  | bool | pos = `false`,
|  | bool | verbose = `false`
| ) |  |

Definition at line 662 of file ImgReaderOgr.h.

663{
664 assert(m_datasource->GetLayerCount()>layer);
665 OGRLayer *poLayer;
666 if(verbose)
667 std::cout << "number of layers: " << m_datasource->GetLayerCount() << std::endl;
668 poLayer = m_datasource->GetLayer(layer);
669 OGRFeatureDefn *poFDefn = poLayer->GetLayerDefn();
670 if(fields.empty()){
671 fields.resize(poFDefn->GetFieldCount());
672 if(verbose)
673 std::cout << "resized fields to " << fields.size() << std::endl;
674 }
675 //start reading features from the layer
676 OGRFeature *poFeature;
677 if(verbose)
678 std::cout << "reset reading" << std::endl;
679 poLayer->ResetReading();
680 unsigned long int ifeature=0;
681 int posOffset=(pos)?2:0;
682 if(verbose)
683 std::cout << "going through features" << std::endl << std::flush;
684 while( (poFeature = poLayer->GetNextFeature()) != NULL ){
685 std::vector<T> theFeature;//(fields.size()+posOffset);//x,y+selectedfields
686 if(verbose)
687 std::cout << "reading feature " << ifeature << std::endl << std::flush;
688 OGRGeometry *poGeometry;
689 poGeometry = poFeature->GetGeometryRef();
690 if(verbose){
691 if(poGeometry == NULL)
692 std::cerr << "no geometry defined" << std::endl << std::flush;
693 else if(wkbFlatten(poGeometry->getGeometryType()) != wkbPoint)
694 std::cerr << "poGeometry type: " << wkbFlatten(poGeometry->getGeometryType()) << std::endl << std::flush;
695 }
696 assert(poGeometry != NULL
697 && wkbFlatten(poGeometry->getGeometryType()) == wkbPoint);
698 OGRPoint *poPoint = (OGRPoint *) poGeometry;
699 if(pos){
700 theFeature.push_back(poPoint->getX());
701 theFeature.push_back(poPoint->getY());
702 }
703 OGRFeatureDefn *poFDefn = poLayer->GetLayerDefn();
704 std::string featurename;
705 for(int iField=0;iField<poFDefn->GetFieldCount();++iField){
706 OGRFieldDefn *poFieldDefn = poFDefn->GetFieldDefn(iField);
707 std::string fieldname=poFieldDefn->GetNameRef();
708 switch(fieldType){
709 case(OFTReal):
710 if(fields.size()<poFDefn->GetFieldCount()){
711 if(find(fields.begin(),fields.end(),fieldname)!=fields.end())
712 theFeature.push_back(poFeature->GetFieldAsDouble(iField));
713 }
714 else{
715 fields[iField]=fieldname;
716 theFeature.push_back(poFeature->GetFieldAsDouble(iField));
717 }
718 break;
719 case(OFTInteger):
720 if(fields.size()<poFDefn->GetFieldCount()){
721 if(find(fields.begin(),fields.end(),fieldname)!=fields.end())
722 theFeature.push_back(poFeature->GetFieldAsDouble(iField));
723 }
724 else{
725 fields[iField]=fieldname;
726 theFeature.push_back(poFeature->GetFieldAsDouble(iField));
727 }
728 break;
729 default:
730 {
731 std::string errorstring="field type not supported in ImgReaderOgr::ReadData";
732 throw(errorstring);
733 }
734 break;
735 }
736 }
737 data.push_back(theFeature);
738 ++ifeature;
739 }
740// assert(data.size()==ifeature);
741 if(data.size()){
742 if(pos)
743 assert(data[0].size()==fields.size()+2);
744 else
745 assert(data[0].size()==fields.size());
746 return fields.size();
747 }
748 else{
749 std::ostringstream ess;
750 ess << "no layer in " << m_filename;
751 throw(ess.str());
752 }
753}

## ◆ readDataImageOgr() [1/2]

unsigned int ImgReaderOgr::readDataImageOgr | ( | std::map< std::string, Vector2d< float > > & | mapPixels,
|  | std::vector< std::string > & | fields,
|  | const std::vector< unsigned short > & | bands,
|  | const std::string & | label,
|  | const std::vector< std::string > & | layers,
|  | int | verbose = `false`
| ) |  |

Definition at line 273 of file ImgReaderOgr.cc.

279{
280 mapPixels.clear();
281 int nsample=0;
282 int totalSamples=0;
283 int nband=0;
284 if(verbose)
285 std::cout << "reading OGR dataset " << m_filename << std::endl;
286 for(int ilayer=0;ilayer<getLayerCount();++ilayer){
287 std::string currentLayername=getLayer(ilayer)->GetName();
288 if(layers.size())
289 if(find(layers.begin(),layers.end(),currentLayername)==layers.end())
290 continue;
291 try{
292 //only retain bands in fields
293 getFields(fields,ilayer);
294 std::vector<std::string>::iterator fit=fields.begin();
295 if(verbose>1)
296 std::cout << "reading fields: ";
297 while(fit!=fields.end()){
298 if(verbose)
299 std::cout << *fit << " ";
300 // size_t pos=(*fit).find_first_not_of("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_ ");
301 if((*fit).substr(0,1)=="B"||(*fit).substr(0,1)=="b"){
302 // if((*fit).substr(1).find_first_not_of("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_ ")!=std::string::npos){
303 std::size_t digits=(*fit).substr(1,1).find_first_of("0123456789");
304 std::size_t digite=(*fit).substr(1).find_first_not_of("0123456789");
305 if((*fit)=="B" || (*fit)=="b" || (*fit)=="Band")//B is only band
306 ++fit;
307 else if(digits!=std::string::npos&&digite==std::string::npos){
308 std::string digitString=(*fit).substr(digits);
309 // int theBand=atoi((*fit).substr(1).c_str());
310 int theBand=atoi(digitString.c_str());
311 if(bands.size()){
312 bool validBand=false;
313 for(int iband=0;iband<bands.size();++iband){
314 if(theBand==bands[iband])
315 validBand=true;
316 }
317 if(validBand)
318 ++fit;
319 else
320 fields.erase(fit);
321 }
322 else
323 ++fit;
324 }
325 else
326 fields.erase(fit);
327 }
328 else
329 fields.erase(fit);
330 }
331 if(verbose)
332 std::cout << std::endl;
333 if(verbose){
334 std::cout << "fields:";
335 for(std::vector<std::string>::iterator fit=fields.begin();fit!=fields.end();++fit)
336 std::cout << " " << *fit;
337 std::cout << std::endl;
338 }
339 if(!nband){
340 if(verbose)
341 std::cout << "reading data" << std::endl;
342 nband=readData(mapPixels,OFTReal,fields,label,ilayer,true,verbose==2);
343 }
344 else{
345 assert(nband==readData(mapPixels,OFTReal,fields,label,ilayer,true,false));
346 }
347 nsample=getFeatureCount(ilayer);
348 totalSamples+=nsample;
349 if(verbose)
350 std::cout << ": " << nsample << " samples read with " << nband << " bands" << std::endl;
351 }
352 catch(std::string e){
353 std::ostringstream estr;
354 estr << e << " " << m_filename;
355 throw(estr.str());
356 }
357 }
358 if(verbose)
359 std::cout << "total number of samples read " << totalSamples << std::endl;
360 return totalSamples;
361}

## ◆ readDataImageOgr() [2/2]

unsigned int ImgReaderOgr::readDataImageOgr | ( | std::map< std::string, Vector2d< float > > & | mapPixels,
|  | std::vector< std::string > & | fields,
|  | double | start,
|  | double | end,
|  | const std::string & | label,
|  | const std::vector< std::string > & | layers,
|  | int | verbose = `false`
| ) |  |

Definition at line 363 of file ImgReaderOgr.cc.

370{
371 mapPixels.clear();
372 int nsample=0;
373 int totalSamples=0;
374 int nband=0;
375 if(verbose)
376 std::cout << "reading OGR dataset file " << m_filename << std::endl;
377 for(int ilayer=0;ilayer<getLayerCount();++ilayer){
378 std::string currentLayername=getLayer(ilayer)->GetName();
379 if(layers.size())
380 if(find(layers.begin(),layers.end(),currentLayername)==layers.end())
381 continue;
382 try{
383 //only retain bands in fields
384 getFields(fields,ilayer);
385 std::vector<std::string>::iterator fit=fields.begin();
386 if(verbose)
387 std::cout << "reading fields: ";
388 while(fit!=fields.end()){
389 if(verbose)
390 std::cout << *fit << " ";
391 if((*fit).substr(0,1)=="B"||(*fit).substr(0,1)=="b"){
392 // if((*fit).substr(1).find_first_not_of("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_ ")!=std::string::npos){
393 std::size_t digits=(*fit).substr(1,1).find_first_of("0123456789");
394 std::size_t digite=(*fit).substr(1).find_first_not_of("0123456789");
395 if(*fit=="B" || *fit=="b"|| *fit=="Band")
396 ++fit;
397 else if(digits!=std::string::npos&&digite==std::string::npos){
398 std::string digitString=(*fit).substr(digits);
399 int iband=atoi(digitString.c_str());
400 // int iband=atoi((*fit).substr(1).c_str());
401 if((start||end)&&(iband<start||iband>end))
402 fields.erase(fit);
403 else
404 ++fit;
405 }
406 else
407 fields.erase(fit);
408 }
409 else
410 fields.erase(fit);
411 }
412 if(verbose)
413 std::cout << std::endl;
414 if(verbose){
415 std::cout << "fields:";
416 for(std::vector<std::string>::iterator fit=fields.begin();fit!=fields.end();++fit)
417 std::cout << " " << *fit;
418 std::cout << std::endl;
419 }
420 if(!nband){
421 if(verbose)
422 std::cout << "reading data" << std::endl;
423 nband=readData(mapPixels,OFTReal,fields,label,ilayer,true,verbose==2);
424 }
425 else{
426 assert(nband==readData(mapPixels,OFTReal,fields,label,ilayer,true,false));
427 }
428 nsample=getFeatureCount(ilayer);
429 totalSamples+=nsample;
430 if(verbose)
431 std::cout << ": " << nsample << " samples read with " << nband << " bands" << std::endl;
432 }
433 catch(std::string e){
434 std::ostringstream estr;
435 estr << e << " " << m_filename;
436 throw(estr.str());
437 }
438 if(verbose)
439 std::cout << ": " << nsample << " samples read with " << nband << " bands" << std::endl;
440 }
441 if(verbose)
442 std::cout << "total number of samples read " << totalSamples << std::endl;
443 return totalSamples;
444}

## ◆ readSql() [1/2]

template<typename T >

int ImgReaderOgr::readSql | ( | std::map< int, Vector2d< T > > & | data,
|  | const OGRFieldType & | fieldType,
|  | std::vector< std::string > & | fields,
|  | const std::string & | label,
|  | const std::string & | sqlStatement,
|  | OGRGeometry * | spatialFilter,
|  | int | layer = `0`,
|  | bool | pos = `false`,
|  | bool | verbose = `false`
| ) |  |

Definition at line 755 of file ImgReaderOgr.h.

756{
757 assert(m_datasource->GetLayerCount()>layer);
758 OGRLayer *poLayer;
759 poLayer = m_datasource->ExecuteSQL(sqlStatement.c_str(), spatialFilter,NULL );
760 if(poLayer!=NULL){
761 OGRFeatureDefn *poFDefn = poLayer->GetLayerDefn();
762 if(fields.empty()){
763 fields.resize(poFDefn->GetFieldCount());
764 if(verbose)
765 std::cout << "resized fields to " << fields.size() << std::endl;
766 }
767 //start reading features from the layer
768 OGRFeature *poFeature;
769 if(verbose)
770 std::cout << "reset reading" << std::endl;
771 poLayer->ResetReading();
772 unsigned long int ifeature=0;
773 int posOffset=(pos)?2:0;
774 if(verbose)
775 std::cout << "going through features" << std::endl << std::flush;
776 int theClass=0;
777 while( (poFeature = poLayer->GetNextFeature()) != NULL ){
778 std::vector<T> theFeature;//(fields.size()+posOffset);//x,y+selectedfields
779 if(verbose)
780 std::cout << "reading feature " << ifeature << std::endl << std::flush;
781 OGRGeometry *poGeometry;
782 poGeometry = poFeature->GetGeometryRef();
783 if(verbose){
784 if(poGeometry == NULL)
785 std::cerr << "no geometry defined" << std::endl << std::flush;
786 else if(wkbFlatten(poGeometry->getGeometryType()) != wkbPoint)
787 std::cerr << "poGeometry type: " << wkbFlatten(poGeometry->getGeometryType()) << std::endl << std::flush;
788 }
789 assert(poGeometry != NULL
790 && wkbFlatten(poGeometry->getGeometryType()) == wkbPoint);
791 OGRPoint *poPoint = (OGRPoint *) poGeometry;
792 if(pos){
793 theFeature.push_back(poPoint->getX());
794 theFeature.push_back(poPoint->getY());
795 }
796 OGRFeatureDefn *poFDefn = poLayer->GetLayerDefn();
797 std::string featurename;
798 for(int iField=0;iField<poFDefn->GetFieldCount();++iField){
799 OGRFieldDefn *poFieldDefn = poFDefn->GetFieldDefn(iField);
800 std::string fieldname=poFieldDefn->GetNameRef();
801 if(fieldname==label)
802 theClass=poFeature->GetFieldAsInteger(iField);
803 else{
804 switch(fieldType){
805 case(OFTReal):
806 if(fields.size()<poFDefn->GetFieldCount()){
807 if(find(fields.begin(),fields.end(),fieldname)!=fields.end())
808 theFeature.push_back(poFeature->GetFieldAsDouble(iField));
809 }
810 else{
811 fields[iField]=fieldname;
812 theFeature.push_back(poFeature->GetFieldAsDouble(iField));
813 }
814 break;
815 case(OFTInteger):
816 if(fields.size()<poFDefn->GetFieldCount()){
817 if(find(fields.begin(),fields.end(),fieldname)!=fields.end())
818 theFeature.push_back(poFeature->GetFieldAsDouble(iField));
819 }
820 else{
821 fields[iField]=fieldname;
822 theFeature.push_back(poFeature->GetFieldAsDouble(iField));
823 }
824 break;
825 default:
826 {
827 std::string errorstring="field type not supported in ImgReaderOgr::ReadData";
828 throw(errorstring);
829 }
830 break;
831 }
832 }
833 }
834 data[theClass].push_back(theFeature);
835 ++ifeature;
836 }
837 if(verbose)
838 std::cout << "number of features read: " << ifeature << std::endl << std::flush;
839 typename std::map<int,Vector2d<T> >::const_iterator mit=data.begin();
840 int nband=0;
841 if(verbose)
842 std::cout << "read classes: " << std::flush;
843 while(mit!=data.end()){
844 if(verbose)
845 std::cout << mit->first << " " << std::flush;
846 if(!nband)
847 nband=fields.size();
848 if(pos)
849 assert((mit->second)[0].size()==nband+2);
850 else
851 assert((mit->second)[0].size()==nband);
852 ++mit;
853 }
854 if(verbose)
855 std::cout << std::endl << std::flush;
856 return(nband);
857 }
858 else{
859 std::ostringstream ess;
860 ess << "no layer in " << m_filename;
861 throw(ess.str());
862 }
863}

## ◆ readSql() [2/2]

template<typename T >

int ImgReaderOgr::readSql | ( | Vector2d< T > & | data,
|  | const OGRFieldType & | fieldType,
|  | std::vector< std::string > & | fields,
|  | const std::string & | sqlStatement,
|  | OGRGeometry * | spatialFilter = `NULL`,
|  | int | layer = `0`,
|  | bool | pos = `false`,
|  | bool | verbose = `false`
| ) |  |

Definition at line 865 of file ImgReaderOgr.h.

866{
867 assert(m_datasource->GetLayerCount()>layer);
868 OGRLayer *poLayer;
869 poLayer = m_datasource->ExecuteSQL(sqlStatement.c_str(), spatialFilter,NULL );
870 if(poLayer!=NULL){
871 OGRFeatureDefn *poFDefn = poLayer->GetLayerDefn();
872 if(fields.empty()){
873 fields.resize(poFDefn->GetFieldCount());
874 if(verbose)
875 std::cout << "resized fields to " << fields.size() << std::endl;
876 }
877 //start reading features from the layer
878 OGRFeature *poFeature;
879 if(verbose)
880 std::cout << "reset reading" << std::endl;
881 poLayer->ResetReading();
882 unsigned long int ifeature=0;
883 int posOffset=(pos)?2:0;
884 if(verbose)
885 std::cout << "going through features" << std::endl << std::flush;
886 while( (poFeature = poLayer->GetNextFeature()) != NULL ){
887 std::vector<T> theFeature;//(fields.size()+posOffset);//x,y+selectedfields
888 if(verbose)
889 std::cout << "reading feature " << ifeature << std::endl << std::flush;
890 OGRGeometry *poGeometry;
891 poGeometry = poFeature->GetGeometryRef();
892 if(verbose){
893 if(poGeometry == NULL)
894 std::cerr << "no geometry defined" << std::endl << std::flush;
895 else if(wkbFlatten(poGeometry->getGeometryType()) != wkbPoint)
896 std::cerr << "poGeometry type: " << wkbFlatten(poGeometry->getGeometryType()) << std::endl << std::flush;
897 }
898 assert(poGeometry != NULL
899 && wkbFlatten(poGeometry->getGeometryType()) == wkbPoint);
900 OGRPoint *poPoint = (OGRPoint *) poGeometry;
901 if(pos){
902 theFeature.push_back(poPoint->getX());
903 theFeature.push_back(poPoint->getY());
904 }
905 OGRFeatureDefn *poFDefn = poLayer->GetLayerDefn();
906 std::string featurename;
907 for(int iField=0;iField<poFDefn->GetFieldCount();++iField){
908 OGRFieldDefn *poFieldDefn = poFDefn->GetFieldDefn(iField);
909 std::string fieldname=poFieldDefn->GetNameRef();
910 switch(fieldType){
911 case(OFTReal):
912 if(fields.size()<poFDefn->GetFieldCount()){
913 if(find(fields.begin(),fields.end(),fieldname)!=fields.end())
914 theFeature.push_back(poFeature->GetFieldAsDouble(iField));
915 }
916 else{
917 fields[iField]=fieldname;
918 theFeature.push_back(poFeature->GetFieldAsDouble(iField));
919 }
920 break;
921 case(OFTInteger):
922 if(fields.size()<poFDefn->GetFieldCount()){
923 if(find(fields.begin(),fields.end(),fieldname)!=fields.end())
924 theFeature.push_back(poFeature->GetFieldAsDouble(iField));
925 }
926 else{
927 fields[iField]=fieldname;
928 theFeature.push_back(poFeature->GetFieldAsDouble(iField));
929 }
930 break;
931 default:
932 {
933 std::string errorstring="field type not supported in ImgReaderOgr::ReadData";
934 throw(errorstring);
935 }
936 break;
937 }
938 }
939 data.push_back(theFeature);
940 ++ifeature;
941 }
942 m_datasource->ReleaseResultSet( poLayer );
943 // assert(data.size()==ifeature);
944 if(data.size()){
945 if(pos)
946 assert(data[0].size()==fields.size()+2);
947 else
948 assert(data[0].size()==fields.size());
949 return fields.size();
950 }
951 else
952 return(0);
953 }
954 else{
955 std::ostringstream ess;
956 ess << "no layer in " << m_filename;
957 throw(ess.str());
958 }
959}

## ◆ readXY()

template<typename T >

int ImgReaderOgr::readXY | ( | std::vector< T > & | xVector,
|  | std::vector< T > & | yVector,
|  | int | layer = `0`,
|  | bool | verbose = `false`
| ) |  |

Definition at line 374 of file ImgReaderOgr.h.

374 {
375 assert(m_datasource->GetLayerCount()>layer);
376 OGRLayer *poLayer;
377 if(verbose)
378 std::cout << "number of layers: " << m_datasource->GetLayerCount() << std::endl;
379 poLayer = m_datasource->GetLayer(layer);
380 OGRFeatureDefn *poFDefn = poLayer->GetLayerDefn();
381 //start reading features from the layer
382 OGRFeature *poFeature;
383 if(verbose)
384 std::cout << "reset reading" << std::endl;
385 poLayer->ResetReading();
386 unsigned long int ifeature=0;
387 if(verbose)
388 std::cout << "going through features" << std::endl << std::flush;
389 while( (poFeature = poLayer->GetNextFeature()) != NULL ){
390 if(verbose)
391 std::cout << "reading feature " << ifeature << std::endl << std::flush;
392 OGRGeometry *poGeometry;
393 poGeometry = poFeature->GetGeometryRef();
394 if(verbose){
395 if(poGeometry == NULL)
396 std::cerr << "no geometry defined" << std::endl << std::flush;
397 else// if(wkbFlatten(poGeometry->getGeometryType()) != wkbPoint)
398 std::cout << "poGeometry type: " << wkbFlatten(poGeometry->getGeometryType()) << std::endl;
399 }
400 // assert(poGeometry != NULL
401 // && wkbFlatten(poGeometry->getGeometryType()) == wkbPoint);
402 OGRPoint *poPoint = (OGRPoint *) poGeometry;
403 xVector.push_back(poPoint->getX());
404 yVector.push_back(poPoint->getY());
405 ++ifeature;
406 }
407 assert(xVector.size()==yVector.size());
408 if(xVector.size()){
409 return xVector.size();
410 }
411 else{
412 std::ostringstream ess;
413 ess << "no layer in " << m_filename;
414 throw(ess.str());
415 }
416}

## ◆ setCodec()

void ImgReaderOgr::setCodec | ( | void |  | ) |
| protected

Definition at line 59 of file ImgReaderOgr.cc.

59 {
60#if GDAL_VERSION_MAJOR < 2
61 //register the drivers
62 OGRRegisterAll();
63 //open the input OGR datasource. Datasources can be files, RDBMSes, directories full of files, or even remote web services depending on the driver being used. However, the datasource name is always a single string.
64 m_datasource = OGRSFDriverRegistrar::Open(m_filename.c_str(), FALSE);//FAlSE: do not update
65#else
66 //register the drivers
67 GDALAllRegister();
68 //open the input OGR datasource. Datasources can be files, RDBMSes, directories full of files, or even remote web services depending on the driver being used. However, the datasource name is always a single string.
69 m_datasource = (GDALDataset*) GDALOpenEx(m_filename.c_str(), GDAL_OF_VECTOR||GDAL_OF_READONLY, NULL, NULL, NULL);
70 // m_datasource = (GDALDataset*) GDALOpenEx(m_filename.c_str(), GDAL_OF_READONLY||GDAL_OF_VECTOR, NULL, NULL, NULL);
71#endif
72 if( m_datasource == NULL ){
73#if GDAL_VERSION_MAJOR < 2
74 std::string errorString="Open failed";
75 throw(errorString);
76#else
77 m_datasource = (GDALDataset*) GDALOpenEx(m_filename.c_str(), GDAL_OF_VECTOR, NULL, NULL, NULL);
78 if( m_datasource == NULL ){
79 std::string errorString="Open failed";
80 throw(errorString);
81 }
82#endif
83 }
84}

## ◆ setFieldSeparator()

void ImgReaderOgr::setFieldSeparator | ( | const char | fs | ) |
| inline

Definition at line 90 of file ImgReaderOgr.h.

90{ m_fs=fs;};

## Friends And Related Function Documentation

## ◆ operator<<

std::ostream & operator<< | ( | std::ostream & | theOstream,
|  | ImgReaderOgr & | theImageReader
| ) |  |
| friend

Definition at line 206 of file ImgReaderOgr.cc.

206 {
207 //An OGRDataSource can potentially have many layers associated with it. The number of layers available can be queried with OGRDataSource::GetLayerCount() and individual layers fetched by index using OGRDataSource::GetLayer(). However, we wil just fetch the layer by name.
208 //todo: try to open and catch if failure...
209 // ofstream fpoints(filename.c_str(),ios::out);
210
211 int nlayerRead=theImageReader.getDataSource()->GetLayerCount();
212
213 for(int ilayer=0;ilayer<nlayerRead;++ilayer){
214 OGRLayer *readLayer=theImageReader.getLayer(ilayer);
215 OGRFeatureDefn *poFDefn = readLayer->GetLayerDefn();
216
217 theOstream << "#";
218 int iField=0;
219 for(int iField=0;iField<poFDefn->GetFieldCount();++iField){
220 OGRFieldDefn *poFieldDefn = poFDefn->GetFieldDefn(iField);
221 std::string fieldname=poFieldDefn->GetNameRef();
222 theOstream << fieldname << theImageReader.getFieldSeparator();
223 }
224 theOstream << std::endl;
225
226 readLayer->ResetReading();
227
228 //start reading features from the layer
229 OGRFeature *poFeature;
230 unsigned long int ifeature=0;
231 while( (poFeature = readLayer->GetNextFeature()) != NULL ){
232 OGRGeometry *poGeometry;
233 poGeometry = poFeature->GetGeometryRef();
234 assert(poGeometry != NULL);
235 double x,y;
236 if(wkbFlatten(poGeometry->getGeometryType()) == wkbPoint){
237 OGRPoint *poPoint = (OGRPoint *) poGeometry;
238 x=poPoint->getX();
239 y=poPoint->getY();
240 }
241 std::vector<std::string> vfields(poFDefn->GetFieldCount());
242 std::string featurename;
243 std::vector<std::string>::iterator fit=vfields.begin();
244 for(int iField=0;iField<poFDefn->GetFieldCount();++iField){
245 *(fit++)=poFeature->GetFieldAsString(iField);
246 }
247 theOstream.precision(12);
248 if(wkbFlatten(poGeometry->getGeometryType()) == wkbPoint)
249 theOstream << x << theImageReader.getFieldSeparator() << y;
250 for(fit=vfields.begin();fit!=vfields.end();++fit)
251 theOstream << theImageReader.getFieldSeparator() << *fit;
252 theOstream << std::endl;
253 ++ifeature;
254 }
255 }
256 return(theOstream);
257}

## Member Data Documentation

## ◆ m_datasource

OGRDataSource* ImgReaderOgr::m_datasource
| protected

Definition at line 99 of file ImgReaderOgr.h.

## ◆ m_filename

std::string ImgReaderOgr::m_filename
| protected

Definition at line 97 of file ImgReaderOgr.h.

## ◆ m_fs

char ImgReaderOgr::m_fs
| protected

Definition at line 103 of file ImgReaderOgr.h.

The documentation for this class was generated from the following files:

- /tmp/slackdce/slackrepo.NvCr9x/build_pktools/PKTOOLS-2.6.7.6/src/imageclasses/ImgReaderOgr.h
- /tmp/slackdce/slackrepo.NvCr9x/build_pktools/PKTOOLS-2.6.7.6/src/imageclasses/ImgReaderOgr.cc