20 #ifndef _IMGREADEROGR_H_ 21 #define _IMGREADEROGR_H_ 30 #include "ogrsf_frmts.h" 31 #include "base/Vector2d.h" 32 #include "ImgReaderGdal.h" 41 void open(
const std::string& filename);
44 template <
typename T>
int readXY(std::vector<T>& xVector, std::vector<T>& yVector,
int layer=0,
bool verbose=
false);
45 template <
typename T>
int readY(std::vector<T>& yVector,
int layer=0,
bool verbose=
false);
46 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);
47 template <
typename T>
int readData(std::vector<T>& data,
const OGRFieldType& fieldType,
const std::string& theField,
int layer=0,
bool verbose=
false);
48 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);
49 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);
50 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);
51 unsigned int readDataImageOgr(std::map<std::string,
Vector2d<float> > &mapPixels,
52 std::vector<std::string>& fields,
53 const std::vector<unsigned short>& bands,
54 const std::string& label,
55 const std::vector<std::string>& layers,
58 unsigned int readDataImageOgr(std::map<std::string,
Vector2d<float> > &mapPixels,
59 std::vector<std::string>& fields,
62 const std::string& label,
63 const std::vector<std::string>& layers,
67 unsigned long int getFeatureCount(
int layer=0)
const;
68 int getFieldCount(
int layer=0)
const;
69 OGRLayer* getLayer(
int layer=0){
return m_datasource->GetLayer(layer);};
70 std::string getProjection(
int layer=0)
const;
71 OGRwkbGeometryType getGeometryType(
int layer=0)
const;
72 std::string getLayerName(
int layer=0){
return m_datasource->GetLayer(layer)->GetLayerDefn()->GetName();};
74 int getFields(std::vector<std::string>& fields,
int layer=0)
const;
75 int getFields(std::vector<OGRFieldDefn*>& fields,
int layer=0)
const;
76 #if GDAL_VERSION_MAJOR < 2 77 OGRDataSource* getDataSource(
void) {
return m_datasource;};
78 OGRSFDriver* getDriver(
void)
const {
return m_datasource->GetDriver();};
80 GDALDataset* getDataSource(
void) {
return m_datasource;};
81 GDALDriver* getDriver(
void)
const {
return m_datasource->GetDriver();};
83 int getLayerCount(
void)
const {
return m_datasource->GetLayerCount();};
85 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);
86 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);
87 bool getExtent(
double& ulx,
double& uly,
double& lrx,
double& lry,
int layer);
88 bool getExtent(
double& ulx,
double& uly,
double& lrx,
double& lry);
90 void setFieldSeparator(
const char fs){ m_fs=fs;};
91 char getFieldSeparator()
const {
return m_fs;};
92 friend std::ostream& operator<<(std::ostream& theOstream,
ImgReaderOgr& theImageReader);
97 std::string m_filename;
98 #if GDAL_VERSION_MAJOR < 2 99 OGRDataSource *m_datasource;
101 GDALDataset *m_datasource;
107 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,
bool pos,
bool verbose)
109 assert(m_datasource->GetLayerCount()>layer);
112 std::cout <<
"number of layers: " << m_datasource->GetLayerCount() << std::endl;
113 poLayer = m_datasource->GetLayer(layer);
115 OGRFeatureDefn *poFDefn = poLayer->GetLayerDefn();
117 fields.resize(poFDefn->GetFieldCount());
119 std::cout <<
"resized fields to " << fields.size() << std::endl;
122 OGRFeature *poFeature;
124 std::cout <<
"reset reading" << std::endl;
125 poLayer->ResetReading();
126 unsigned long int ifeature=0;
127 int posOffset=(pos)?2:0;
129 std::cout <<
"going through features" << std::endl << std::flush;
131 while( (poFeature = poLayer->GetNextFeature()) != NULL ){
132 std::vector<T> theFeature;
134 std::cout <<
"reading feature " << ifeature << std::endl << std::flush;
135 OGRGeometry *poGeometry;
136 poGeometry = poFeature->GetGeometryRef();
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;
143 assert(poGeometry != NULL );
146 if(wkbFlatten(poGeometry->getGeometryType()) == wkbPoint){
148 poPoint = (OGRPoint *) poGeometry;
149 theFeature.push_back(poPoint->getX());
150 theFeature.push_back(poPoint->getY());
152 else if(wkbFlatten(poGeometry->getGeometryType()) == wkbPolygon){
154 OGRPolygon * poPolygon = (OGRPolygon *) poGeometry;
155 poPolygon->Centroid(&thePoint);
156 theFeature.push_back(thePoint.getX());
157 theFeature.push_back(thePoint.getY());
162 poGeometry->Centroid(&thePoint);
163 theFeature.push_back(thePoint.getX());
164 theFeature.push_back(thePoint.getY());
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();
173 theClass=poFeature->GetFieldAsInteger(iField);
177 if(fields.size()<poFDefn->GetFieldCount()){
178 if(find(fields.begin(),fields.end(),fieldname)!=fields.end())
179 theFeature.push_back(poFeature->GetFieldAsDouble(iField));
182 fields[iField]=fieldname;
183 theFeature.push_back(poFeature->GetFieldAsDouble(iField));
187 if(fields.size()<poFDefn->GetFieldCount()){
188 if(find(fields.begin(),fields.end(),fieldname)!=fields.end())
189 theFeature.push_back(poFeature->GetFieldAsDouble(iField));
192 fields[iField]=fieldname;
193 theFeature.push_back(poFeature->GetFieldAsDouble(iField));
198 std::string errorstring=
"field type not supported in ImgReaderOgr::ReadData";
205 data[theClass].push_back(theFeature);
210 std::cout <<
"number of features read: " << ifeature << std::endl << std::flush;
211 typename std::map<int,Vector2d<T> >::const_iterator mit=data.begin();
214 std::cout <<
"read classes: " << std::flush;
215 while(mit!=data.end()){
217 std::cout << mit->first <<
" " << std::flush;
221 assert((mit->second)[0].size()==nband+2);
223 assert((mit->second)[0].size()==nband);
227 std::cout << std::endl << std::flush;
231 std::ostringstream ess;
232 ess <<
"no layer in " << m_filename;
238 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,
bool pos,
bool verbose)
240 assert(m_datasource->GetLayerCount()>layer);
243 std::cout <<
"number of layers: " << m_datasource->GetLayerCount() << std::endl;
244 poLayer = m_datasource->GetLayer(layer);
246 OGRFeatureDefn *poFDefn = poLayer->GetLayerDefn();
247 assert(poFDefn!=NULL);
250 fields.resize(poFDefn->GetFieldCount());
252 std::cout <<
"resized fields to " << fields.size() << std::endl;
256 OGRFeature *poFeature;
258 std::cout <<
"reset reading" << std::endl;
259 poLayer->ResetReading();
260 unsigned long int ifeature=0;
261 int posOffset=(pos)?2:0;
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;
268 std::cout <<
"reading feature " << ifeature << std::endl << std::flush;
269 OGRGeometry *poGeometry;
270 poGeometry = poFeature->GetGeometryRef();
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;
277 assert(poGeometry != NULL );
280 if(wkbFlatten(poGeometry->getGeometryType()) == wkbPoint){
282 poPoint = (OGRPoint *) poGeometry;
283 theFeature.push_back(poPoint->getX());
284 theFeature.push_back(poPoint->getY());
286 else if(wkbFlatten(poGeometry->getGeometryType()) == wkbPolygon){
288 poGeometry->Centroid(&thePoint);
289 theFeature.push_back(thePoint.getX());
290 theFeature.push_back(thePoint.getY());
295 poGeometry->Centroid(&thePoint);
296 theFeature.push_back(thePoint.getX());
297 theFeature.push_back(thePoint.getY());
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);
308 std::cout <<
"read feature for " << theClass << std::endl;
313 if(fields.size()<poFDefn->GetFieldCount()){
314 if(find(fields.begin(),fields.end(),fieldname)!=fields.end())
315 theFeature.push_back(poFeature->GetFieldAsDouble(iField));
318 fields[iField]=fieldname;
319 theFeature.push_back(poFeature->GetFieldAsDouble(iField));
323 if(fields.size()<poFDefn->GetFieldCount()){
324 if(find(fields.begin(),fields.end(),fieldname)!=fields.end())
325 theFeature.push_back(poFeature->GetFieldAsDouble(iField));
328 fields[iField]=fieldname;
329 theFeature.push_back(poFeature->GetFieldAsDouble(iField));
334 std::string errorstring=
"field type not supported in ImgReaderOgr::ReadData";
340 assert(poFDefn!=NULL);
342 data[theClass].push_back(theFeature);
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();
350 std::cout <<
"read classes: " << std::flush;
351 while(mit!=data.end()){
353 std::cout << mit->first <<
" " << std::flush;
357 assert((mit->second)[0].size()==nband+2);
359 assert((mit->second)[0].size()==nband);
363 std::cout << std::endl << std::flush;
367 std::ostringstream ess;
368 ess <<
"no layer in " << m_filename;
374 template <
typename T>
int ImgReaderOgr::readXY(std::vector<T>& xVector, std::vector<T>& yVector,
int layer,
bool verbose){
375 assert(m_datasource->GetLayerCount()>layer);
378 std::cout <<
"number of layers: " << m_datasource->GetLayerCount() << std::endl;
379 poLayer = m_datasource->GetLayer(layer);
380 OGRFeatureDefn *poFDefn = poLayer->GetLayerDefn();
382 OGRFeature *poFeature;
384 std::cout <<
"reset reading" << std::endl;
385 poLayer->ResetReading();
386 unsigned long int ifeature=0;
388 std::cout <<
"going through features" << std::endl << std::flush;
389 while( (poFeature = poLayer->GetNextFeature()) != NULL ){
391 std::cout <<
"reading feature " << ifeature << std::endl << std::flush;
392 OGRGeometry *poGeometry;
393 poGeometry = poFeature->GetGeometryRef();
395 if(poGeometry == NULL)
396 std::cerr <<
"no geometry defined" << std::endl << std::flush;
398 std::cout <<
"poGeometry type: " << wkbFlatten(poGeometry->getGeometryType()) << std::endl;
402 OGRPoint *poPoint = (OGRPoint *) poGeometry;
403 xVector.push_back(poPoint->getX());
404 yVector.push_back(poPoint->getY());
407 assert(xVector.size()==yVector.size());
409 return xVector.size();
412 std::ostringstream ess;
413 ess <<
"no layer in " << m_filename;
419 template <
typename T>
int ImgReaderOgr::readData(std::vector<T>& data,
const OGRFieldType& fieldType, std::vector<std::string>& fields, OGRFeature *poFeature,
int layer,
bool pos,
bool verbose)
421 assert(m_datasource->GetLayerCount()>layer);
424 std::cout <<
"number of layers: " << m_datasource->GetLayerCount() << std::endl;
425 poLayer = m_datasource->GetLayer(layer);
426 OGRFeatureDefn *poFDefn = poLayer->GetLayerDefn();
428 fields.resize(poFDefn->GetFieldCount());
430 std::cout <<
"resized fields to " << fields.size() << std::endl;
432 OGRGeometry *poGeometry;
433 poGeometry = poFeature->GetGeometryRef();
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;
440 assert(poGeometry != NULL);
442 OGRPoint *poPoint = (OGRPoint *) poGeometry;
444 if(wkbFlatten(poGeometry->getGeometryType()) == wkbPoint){
446 poPoint = (OGRPoint *) poGeometry;
447 data.push_back(poPoint->getX());
448 data.push_back(poPoint->getY());
450 else if(wkbFlatten(poGeometry->getGeometryType()) == wkbPolygon){
452 poGeometry->Centroid(&thePoint);
453 data.push_back(thePoint.getX());
454 data.push_back(thePoint.getY());
459 poGeometry->Centroid(&thePoint);
460 data.push_back(thePoint.getX());
461 data.push_back(thePoint.getY());
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();
471 if(fields.size()<poFDefn->GetFieldCount()){
472 if(find(fields.begin(),fields.end(),fieldname)!=fields.end())
473 data.push_back(poFeature->GetFieldAsDouble(iField));
476 fields[iField]=fieldname;
477 data.push_back(poFeature->GetFieldAsDouble(iField));
481 if(fields.size()<poFDefn->GetFieldCount()){
482 if(find(fields.begin(),fields.end(),fieldname)!=fields.end())
483 data.push_back(poFeature->GetFieldAsDouble(iField));
486 fields[iField]=fieldname;
487 data.push_back(poFeature->GetFieldAsDouble(iField));
492 std::string errorstring=
"field type not supported in ImgReaderOgr::ReadData";
501 assert(data.size()==fields.size()+2);
503 assert(data.size()==fields.size());
504 return fields.size();
507 std::ostringstream ess;
508 ess <<
"no layer in " << m_filename;
514 template <
typename T>
inline int ImgReaderOgr::readData(std::vector<T>& data,
const OGRFieldType& fieldType,
const std::string& theField,
int layer,
bool verbose)
516 assert(m_datasource->GetLayerCount()>layer);
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;
526 std::cout <<
"read first field from total of " << nfield << std::endl;
530 OGRFeature *poFeature;
532 std::cout <<
"reset reading" << std::endl;
533 poLayer->ResetReading();
534 unsigned long int ifeature=0;
536 std::cout <<
"going through features" << std::endl << std::flush;
537 while( (poFeature = poLayer->GetNextFeature()) != NULL ){
541 std::cout <<
"reading feature " << ifeature << std::endl << std::flush;
542 OGRGeometry *poGeometry;
543 poGeometry = poFeature->GetGeometryRef();
545 if(poGeometry == NULL)
546 std::cerr <<
"no geometry defined" << std::endl << std::flush;
548 std::cout <<
"poGeometry type: " << wkbFlatten(poGeometry->getGeometryType()) << std::endl;
552 OGRPoint *poPoint = (OGRPoint *) poGeometry;
554 for(
int iField=0;iField<nfield;++iField){
555 OGRFieldDefn *poFieldDefn = poFDefn->GetFieldDefn(iField);
556 std::string fieldname=poFieldDefn->GetNameRef();
557 if(fieldname!=theField)
562 theFeature=poFeature->GetFieldAsDouble(iField);
566 std::string errorstring=
"field type not supported in ImgReaderOgr::ReadData";
572 data.push_back(theFeature);
574 std::cout <<
"feature is: " << theFeature << std::endl;
581 std::ostringstream ess;
582 ess <<
"no layer in " << m_filename;
588 template <>
inline int ImgReaderOgr::readData(std::vector<std::string>& data,
const OGRFieldType& fieldType,
const std::string& theField,
int layer,
bool verbose)
590 assert(m_datasource->GetLayerCount()>layer);
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;
600 std::cout <<
"read first field from total of " << nfield << std::endl;
604 OGRFeature *poFeature;
606 std::cout <<
"reset reading" << std::endl;
607 poLayer->ResetReading();
608 unsigned long int ifeature=0;
610 std::cout <<
"going through features" << std::endl << std::flush;
611 while( (poFeature = poLayer->GetNextFeature()) != NULL ){
612 std::string theFeature;
614 std::cout <<
"reading feature " << ifeature << std::endl << std::flush;
615 OGRGeometry *poGeometry;
616 poGeometry = poFeature->GetGeometryRef();
618 if(poGeometry == NULL)
619 std::cerr <<
"no geometry defined" << std::endl << std::flush;
621 std::cout <<
"poGeometry type: " << wkbFlatten(poGeometry->getGeometryType()) << std::endl;
625 OGRPoint *poPoint = (OGRPoint *) poGeometry;
627 for(
int iField=0;iField<nfield;++iField){
628 OGRFieldDefn *poFieldDefn = poFDefn->GetFieldDefn(iField);
629 std::string fieldname=poFieldDefn->GetNameRef();
630 if(fieldname!=theField)
636 theFeature=poFeature->GetFieldAsString(iField);
640 std::string errorstring=
"field type not supported in ImgReaderOgr::ReadData";
646 data.push_back(theFeature);
648 std::cout <<
"feature is: " << theFeature << std::endl;
655 std::ostringstream ess;
656 ess <<
"no layer in " << m_filename;
662 template <
typename T>
int ImgReaderOgr::readData(
Vector2d<T>& data,
const OGRFieldType& fieldType, std::vector<std::string>& fields,
int layer,
bool pos,
bool verbose)
664 assert(m_datasource->GetLayerCount()>layer);
667 std::cout <<
"number of layers: " << m_datasource->GetLayerCount() << std::endl;
668 poLayer = m_datasource->GetLayer(layer);
669 OGRFeatureDefn *poFDefn = poLayer->GetLayerDefn();
671 fields.resize(poFDefn->GetFieldCount());
673 std::cout <<
"resized fields to " << fields.size() << std::endl;
676 OGRFeature *poFeature;
678 std::cout <<
"reset reading" << std::endl;
679 poLayer->ResetReading();
680 unsigned long int ifeature=0;
681 int posOffset=(pos)?2:0;
683 std::cout <<
"going through features" << std::endl << std::flush;
684 while( (poFeature = poLayer->GetNextFeature()) != NULL ){
685 std::vector<T> theFeature;
687 std::cout <<
"reading feature " << ifeature << std::endl << std::flush;
688 OGRGeometry *poGeometry;
689 poGeometry = poFeature->GetGeometryRef();
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;
696 assert(poGeometry != NULL
697 && wkbFlatten(poGeometry->getGeometryType()) == wkbPoint);
698 OGRPoint *poPoint = (OGRPoint *) poGeometry;
700 theFeature.push_back(poPoint->getX());
701 theFeature.push_back(poPoint->getY());
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();
710 if(fields.size()<poFDefn->GetFieldCount()){
711 if(find(fields.begin(),fields.end(),fieldname)!=fields.end())
712 theFeature.push_back(poFeature->GetFieldAsDouble(iField));
715 fields[iField]=fieldname;
716 theFeature.push_back(poFeature->GetFieldAsDouble(iField));
720 if(fields.size()<poFDefn->GetFieldCount()){
721 if(find(fields.begin(),fields.end(),fieldname)!=fields.end())
722 theFeature.push_back(poFeature->GetFieldAsDouble(iField));
725 fields[iField]=fieldname;
726 theFeature.push_back(poFeature->GetFieldAsDouble(iField));
731 std::string errorstring=
"field type not supported in ImgReaderOgr::ReadData";
737 data.push_back(theFeature);
743 assert(data[0].size()==fields.size()+2);
745 assert(data[0].size()==fields.size());
746 return fields.size();
749 std::ostringstream ess;
750 ess <<
"no layer in " << m_filename;
755 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,
bool pos,
bool verbose)
757 assert(m_datasource->GetLayerCount()>layer);
759 poLayer = m_datasource->ExecuteSQL(sqlStatement.c_str(), spatialFilter,NULL );
761 OGRFeatureDefn *poFDefn = poLayer->GetLayerDefn();
763 fields.resize(poFDefn->GetFieldCount());
765 std::cout <<
"resized fields to " << fields.size() << std::endl;
768 OGRFeature *poFeature;
770 std::cout <<
"reset reading" << std::endl;
771 poLayer->ResetReading();
772 unsigned long int ifeature=0;
773 int posOffset=(pos)?2:0;
775 std::cout <<
"going through features" << std::endl << std::flush;
777 while( (poFeature = poLayer->GetNextFeature()) != NULL ){
778 std::vector<T> theFeature;
780 std::cout <<
"reading feature " << ifeature << std::endl << std::flush;
781 OGRGeometry *poGeometry;
782 poGeometry = poFeature->GetGeometryRef();
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;
789 assert(poGeometry != NULL
790 && wkbFlatten(poGeometry->getGeometryType()) == wkbPoint);
791 OGRPoint *poPoint = (OGRPoint *) poGeometry;
793 theFeature.push_back(poPoint->getX());
794 theFeature.push_back(poPoint->getY());
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();
802 theClass=poFeature->GetFieldAsInteger(iField);
806 if(fields.size()<poFDefn->GetFieldCount()){
807 if(find(fields.begin(),fields.end(),fieldname)!=fields.end())
808 theFeature.push_back(poFeature->GetFieldAsDouble(iField));
811 fields[iField]=fieldname;
812 theFeature.push_back(poFeature->GetFieldAsDouble(iField));
816 if(fields.size()<poFDefn->GetFieldCount()){
817 if(find(fields.begin(),fields.end(),fieldname)!=fields.end())
818 theFeature.push_back(poFeature->GetFieldAsDouble(iField));
821 fields[iField]=fieldname;
822 theFeature.push_back(poFeature->GetFieldAsDouble(iField));
827 std::string errorstring=
"field type not supported in ImgReaderOgr::ReadData";
834 data[theClass].push_back(theFeature);
838 std::cout <<
"number of features read: " << ifeature << std::endl << std::flush;
839 typename std::map<int,Vector2d<T> >::const_iterator mit=data.begin();
842 std::cout <<
"read classes: " << std::flush;
843 while(mit!=data.end()){
845 std::cout << mit->first <<
" " << std::flush;
849 assert((mit->second)[0].size()==nband+2);
851 assert((mit->second)[0].size()==nband);
855 std::cout << std::endl << std::flush;
859 std::ostringstream ess;
860 ess <<
"no layer in " << m_filename;
865 template<
typename T>
int ImgReaderOgr::readSql(
Vector2d<T>& data,
const OGRFieldType& fieldType, std::vector<std::string>& fields,
const std::string& sqlStatement, OGRGeometry* spatialFilter,
int layer,
bool pos,
bool verbose)
867 assert(m_datasource->GetLayerCount()>layer);
869 poLayer = m_datasource->ExecuteSQL(sqlStatement.c_str(), spatialFilter,NULL );
871 OGRFeatureDefn *poFDefn = poLayer->GetLayerDefn();
873 fields.resize(poFDefn->GetFieldCount());
875 std::cout <<
"resized fields to " << fields.size() << std::endl;
878 OGRFeature *poFeature;
880 std::cout <<
"reset reading" << std::endl;
881 poLayer->ResetReading();
882 unsigned long int ifeature=0;
883 int posOffset=(pos)?2:0;
885 std::cout <<
"going through features" << std::endl << std::flush;
886 while( (poFeature = poLayer->GetNextFeature()) != NULL ){
887 std::vector<T> theFeature;
889 std::cout <<
"reading feature " << ifeature << std::endl << std::flush;
890 OGRGeometry *poGeometry;
891 poGeometry = poFeature->GetGeometryRef();
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;
898 assert(poGeometry != NULL
899 && wkbFlatten(poGeometry->getGeometryType()) == wkbPoint);
900 OGRPoint *poPoint = (OGRPoint *) poGeometry;
902 theFeature.push_back(poPoint->getX());
903 theFeature.push_back(poPoint->getY());
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();
912 if(fields.size()<poFDefn->GetFieldCount()){
913 if(find(fields.begin(),fields.end(),fieldname)!=fields.end())
914 theFeature.push_back(poFeature->GetFieldAsDouble(iField));
917 fields[iField]=fieldname;
918 theFeature.push_back(poFeature->GetFieldAsDouble(iField));
922 if(fields.size()<poFDefn->GetFieldCount()){
923 if(find(fields.begin(),fields.end(),fieldname)!=fields.end())
924 theFeature.push_back(poFeature->GetFieldAsDouble(iField));
927 fields[iField]=fieldname;
928 theFeature.push_back(poFeature->GetFieldAsDouble(iField));
933 std::string errorstring=
"field type not supported in ImgReaderOgr::ReadData";
939 data.push_back(theFeature);
942 m_datasource->ReleaseResultSet( poLayer );
946 assert(data[0].size()==fields.size()+2);
948 assert(data[0].size()==fields.size());
949 return fields.size();
955 std::ostringstream ess;
956 ess <<
"no layer in " << m_filename;
961 #endif // _IMGREADEROGR_H_