22 #include "ImgReaderOgr.h" 23 #include "ImgWriterOgr.h" 24 #include "cpl_string.h" 26 ImgReaderOgr::ImgReaderOgr(
void)
30 ImgReaderOgr::ImgReaderOgr(
const std::string& filename)
35 ImgReaderOgr::~ImgReaderOgr(
void)
41 void ImgReaderOgr::open(
const std::string& filename)
49 void ImgReaderOgr::close(
void)
51 #if GDAL_VERSION_MAJOR < 2 52 OGRDataSource::DestroyDataSource(m_datasource);
54 GDALClose(m_datasource);
59 void ImgReaderOgr::setCodec(
void){
60 #if GDAL_VERSION_MAJOR < 2 64 m_datasource = OGRSFDriverRegistrar::Open(
m_filename.c_str(), FALSE);
69 m_datasource = (GDALDataset*) GDALOpenEx(
m_filename.c_str(), GDAL_OF_VECTOR||GDAL_OF_READONLY, NULL, NULL, NULL);
72 if( m_datasource == NULL ){
73 #if GDAL_VERSION_MAJOR < 2 74 std::string errorString=
"Open failed";
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";
86 bool ImgReaderOgr::getExtent(
double& ulx,
double& uly,
double& lrx,
double& lry,
int layer)
89 if(getLayer(layer)->GetExtent(&oExt,TRUE)==OGRERR_NONE){
100 bool ImgReaderOgr::getExtent(
double& ulx,
double& uly,
double& lrx,
double& lry)
104 for(
int ilayer=0;ilayer<getLayerCount();++ilayer){
105 if(getLayer(ilayer)->GetExtent(&oExt,TRUE)==OGRERR_NONE){
130 unsigned long int ImgReaderOgr::getFeatureCount(
int layer)
const 132 return(m_datasource->GetLayer(layer)->GetFeatureCount());
135 int ImgReaderOgr::getFieldCount(
int layer)
const 138 layer=m_datasource->GetLayerCount()-1;
139 assert(m_datasource->GetLayerCount()>layer);
141 if((poLayer = m_datasource->GetLayer(layer))==NULL){
142 std::string errorstring=
"Could not get layer";
145 OGRFeatureDefn *poFDefn = poLayer->GetLayerDefn();
146 return(poFDefn->GetFieldCount());
149 int ImgReaderOgr::getFields(std::vector<std::string>& fields,
int layer)
const 152 layer=m_datasource->GetLayerCount()-1;
153 assert(m_datasource->GetLayerCount()>layer);
155 if((poLayer = m_datasource->GetLayer(layer))==NULL){
156 std::string errorstring=
"Could not get layer";
159 OGRFeatureDefn *poFDefn = poLayer->GetLayerDefn();
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();
166 return(fields.size());
169 int ImgReaderOgr::getFields(std::vector<OGRFieldDefn*>& fields,
int layer)
const 172 layer=m_datasource->GetLayerCount()-1;
173 assert(m_datasource->GetLayerCount()>layer);
175 if((poLayer = m_datasource->GetLayer(layer))==NULL){
176 std::string errorstring=
"Could not get layer";
179 OGRFeatureDefn *poFDefn = poLayer->GetLayerDefn();
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);
186 assert(fields.size()==getFieldCount(layer));
187 return(fields.size());
190 std::string ImgReaderOgr::getProjection(
int layer)
const 192 if(m_datasource->GetLayer(layer)->GetSpatialRef()){
194 m_datasource->GetLayer(layer)->GetSpatialRef()->exportToWkt(&ppszResult);
201 OGRwkbGeometryType ImgReaderOgr::getGeometryType(
int layer)
const 203 return m_datasource->GetLayer(layer)->GetLayerDefn()->GetGeomType();
206 std::ostream& operator<<(std::ostream& theOstream,
ImgReaderOgr& theImageReader){
211 int nlayerRead=theImageReader.getDataSource()->GetLayerCount();
213 for(
int ilayer=0;ilayer<nlayerRead;++ilayer){
214 OGRLayer *readLayer=theImageReader.getLayer(ilayer);
215 OGRFeatureDefn *poFDefn = readLayer->GetLayerDefn();
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();
224 theOstream << std::endl;
226 readLayer->ResetReading();
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);
236 if(wkbFlatten(poGeometry->getGeometryType()) == wkbPoint){
237 OGRPoint *poPoint = (OGRPoint *) poGeometry;
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);
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;
273 unsigned int ImgReaderOgr::readDataImageOgr(std::map<std::string,
Vector2d<float> > &mapPixels,
274 std::vector<std::string>& fields,
275 const std::vector<unsigned short>& bands,
276 const std::string& label,
277 const std::vector<std::string>& layers,
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();
289 if(find(layers.begin(),layers.end(),currentLayername)==layers.end())
293 getFields(fields,ilayer);
294 std::vector<std::string>::iterator fit=fields.begin();
296 std::cout <<
"reading fields: ";
297 while(fit!=fields.end()){
299 std::cout << *fit <<
" ";
301 if((*fit).substr(0,1)==
"B"||(*fit).substr(0,1)==
"b"){
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")
307 else if(digits!=std::string::npos&&digite==std::string::npos){
308 std::string digitString=(*fit).substr(digits);
310 int theBand=atoi(digitString.c_str());
312 bool validBand=
false;
313 for(
int iband=0;iband<bands.size();++iband){
314 if(theBand==bands[iband])
332 std::cout << std::endl;
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;
341 std::cout <<
"reading data" << std::endl;
342 nband=
readData(mapPixels,OFTReal,fields,label,ilayer,
true,verbose==2);
345 assert(nband==
readData(mapPixels,OFTReal,fields,label,ilayer,
true,
false));
347 nsample=getFeatureCount(ilayer);
348 totalSamples+=nsample;
350 std::cout <<
": " << nsample <<
" samples read with " << nband <<
" bands" << std::endl;
352 catch(std::string e){
353 std::ostringstream estr;
359 std::cout <<
"total number of samples read " << totalSamples << std::endl;
363 unsigned int ImgReaderOgr::readDataImageOgr(std::map<std::string,
Vector2d<float> > &mapPixels,
364 std::vector<std::string>& fields,
367 const std::string& label,
368 const std::vector<std::string>& layers,
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();
380 if(find(layers.begin(),layers.end(),currentLayername)==layers.end())
384 getFields(fields,ilayer);
385 std::vector<std::string>::iterator fit=fields.begin();
387 std::cout <<
"reading fields: ";
388 while(fit!=fields.end()){
390 std::cout << *fit <<
" ";
391 if((*fit).substr(0,1)==
"B"||(*fit).substr(0,1)==
"b"){
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")
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());
401 if((start||end)&&(iband<start||iband>end))
413 std::cout << std::endl;
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;
422 std::cout <<
"reading data" << std::endl;
423 nband=
readData(mapPixels,OFTReal,fields,label,ilayer,
true,verbose==2);
426 assert(nband==
readData(mapPixels,OFTReal,fields,label,ilayer,
true,
false));
428 nsample=getFeatureCount(ilayer);
429 totalSamples+=nsample;
431 std::cout <<
": " << nsample <<
" samples read with " << nband <<
" bands" << std::endl;
433 catch(std::string e){
434 std::ostringstream estr;
439 std::cout <<
": " << nsample <<
" samples read with " << nband <<
" bands" << std::endl;
442 std::cout <<
"total number of samples read " << totalSamples << std::endl;
std::string m_filename
filename of this dataset
void readData(T &value, int col, int row, int band=0)
Read a single pixel cell value at a specific column and row for a specific band (all indices start co...
void setCodec(const GDALAccess &readMode=GA_ReadOnly)
Set GDAL dataset number of columns, rows, bands and geotransform.
void open(const std::string &filename, const GDALAccess &readMode=GA_ReadOnly)
Open an image.