pktools  2.6.5
Processing Kernel for geospatial data
ImgWriterOgr.cc
1 /**********************************************************************
2 ImgWriterOgr.cc: class to write vector files using OGR API library
3 Copyright (C) 2008-2012 Pieter Kempeneers
4 
5 This file is part of pktools
6 
7 pktools is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
11 
12 pktools is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16 
17 You should have received a copy of the GNU General Public License
18 along with pktools. If not, see <http://www.gnu.org/licenses/>.
19 ***********************************************************************/
20 #include <iostream>
21 #include <fstream>
22 #include <sstream>
23 #include "ImgReaderOgr.h"
24 #include "ImgWriterOgr.h"
25 #include "ImgReaderGdal.h"
26 #include "cpl_string.h"
27 //---------------------------------------------------------------------------
28 ImgWriterOgr::ImgWriterOgr(void)
29 {}
30 
31 ImgWriterOgr::~ImgWriterOgr(void)
32 {
33 }
34 
35 ImgWriterOgr::ImgWriterOgr(const std::string& filename, const std::string& imageType)
36 {
37  open(filename,imageType);
38 }
39 
40 ImgWriterOgr::ImgWriterOgr(const std::string& filename, ImgReaderOgr& imgReaderOgr)
41 {
42  m_filename=filename;
43  setCodec(imgReaderOgr.getDriver());
44  int nlayer=imgReaderOgr.getDataSource()->GetLayerCount();
45  for(int ilayer=0;ilayer<nlayer;++ilayer){
46  std::string layername = imgReaderOgr.getLayer(ilayer)->GetName();
47  createLayer(layername,imgReaderOgr.getProjection(),imgReaderOgr.getGeometryType(),NULL);
48  copyFields(imgReaderOgr,ilayer,ilayer);
49  }
50 }
51 
52 ImgWriterOgr::ImgWriterOgr(const std::string& filename, ImgReaderOgr& imgReaderOgr, bool copyData)
53 {
54  CPLErrorReset();
55  m_filename=filename;
56  setCodec(imgReaderOgr.getDriver());
57  int nlayer=imgReaderOgr.getDataSource()->GetLayerCount();
58  for(int ilayer=0;ilayer<nlayer;++ilayer){
59  std::string layername = imgReaderOgr.getLayer(ilayer)->GetName();
60  createLayer(layername,imgReaderOgr.getProjection(),imgReaderOgr.getGeometryType(),NULL);
61  copyFields(imgReaderOgr,ilayer,ilayer);
62  if(copyData){
63  OGRFeature *poFeature;
64  while(true){// (poFeature = imgReaderOgr.getLayer()->GetNextFeature()) != NULL ){
65  poFeature = imgReaderOgr.getLayer(ilayer)->GetNextFeature();
66  if( poFeature == NULL )
67  break;
68  OGRFeature *poDstFeature = NULL;
69 
70  poDstFeature=createFeature(ilayer);
71  //todo: check here if SetFrom works (experienced segmentation fault)
72  if( poDstFeature->SetFrom( poFeature, TRUE ) != OGRERR_NONE ){
73  const char* fmt;
74  std::string errorString="Unable to translate feature %d from layer %s.\n";
75  fmt=errorString.c_str();
76  CPLError( CE_Failure, CPLE_AppDefined,
77  fmt,
78  poFeature->GetFID(), getLayerName().c_str() );
79  // CPLError( CE_Failure, CPLE_AppDefined,
80  // "Unable to translate feature %d from layer %s.\n",
81  // poFeature->GetFID(), getLayerName().c_str() );
82 
83  OGRFeature::DestroyFeature( poFeature );
84  OGRFeature::DestroyFeature( poDstFeature );
85  }
86  poDstFeature->SetFID( poFeature->GetFID() );
87  OGRFeature::DestroyFeature( poFeature );
88 
89  CPLErrorReset();
90  if(createFeature( poDstFeature,ilayer ) != OGRERR_NONE){
91  const char* fmt;
92  std::string errorString="Unable to translate feature %d from layer %s.\n";
93  fmt=errorString.c_str();
94  CPLError( CE_Failure, CPLE_AppDefined,
95  fmt,
96  poFeature->GetFID(), getLayerName().c_str() );
97  OGRFeature::DestroyFeature( poDstFeature );
98  }
99  OGRFeature::DestroyFeature( poDstFeature );
100  }
101  }
102  }
103 }
104 
105 //---------------------------------------------------------------------------
106 
107 void ImgWriterOgr::open(const std::string& filename, ImgReaderOgr& imgReaderOgr)
108 {
109  m_filename=filename;
110  setCodec(imgReaderOgr.getDriver());
111  int nlayer=imgReaderOgr.getDataSource()->GetLayerCount();
112  for(int ilayer=0;ilayer<nlayer;++ilayer){
113  std::string layername = imgReaderOgr.getLayer(ilayer)->GetName();
114  createLayer(layername,imgReaderOgr.getProjection(),imgReaderOgr.getGeometryType(),NULL);
115  copyFields(imgReaderOgr,ilayer,ilayer);
116  }
117 }
118 
119 void ImgWriterOgr::open(const std::string& filename, const std::string& imageType)
120 {
121  m_filename = filename;
122  setCodec(imageType);
123 }
124 
125 //---------------------------------------------------------------------------
126 void ImgWriterOgr::close(void)
127 {
128  OGRDataSource::DestroyDataSource(m_datasource);
129 }
130 
131 //---------------------------------------------------------------------------
132 void ImgWriterOgr::setCodec(const std::string& imageType){
133  //register the drivers
134  OGRRegisterAll();
135  //fetch the OGR file driver
136  OGRSFDriver *poDriver;
137  poDriver = OGRSFDriverRegistrar::GetRegistrar()->GetDriverByName(imageType.c_str());
138  if( poDriver == NULL ){
139  std::string errorString="FileOpenError";
140  throw(errorString);
141  }
142  m_datasource = OGRSFDriverRegistrar::Open( m_filename.c_str(), TRUE );
143  if( m_datasource == NULL ){
144  m_datasource = OGRSFDriverRegistrar::Open( m_filename.c_str(), FALSE );
145  if ( m_datasource != NULL){// we can only open in not update mode
146  std::string errorString="Update mode not supported, delete output dataset first";
147  throw(errorString);
148  OGRDataSource::DestroyDataSource(m_datasource);
149  m_datasource = NULL;
150  }
151  else //create the data source
152  m_datasource=poDriver->CreateDataSource(m_filename.c_str(),NULL);
153  }
154  else{//datasets exists, always overwrite all layers (no update append for now)
155  int nLayerCount = m_datasource->GetLayerCount();
156  for(int iLayer = 0; iLayer < nLayerCount; ++iLayer){
157  if(m_datasource->DeleteLayer(iLayer)!=OGRERR_NONE){
158  std::string errorstring="DeleteLayer() failed when overwrite requested";
159  throw(errorstring);
160  }
161  }
162  }
163  if(m_datasource==NULL){
164  std::string errorString="Creation of output file failed";
165  throw(errorString);
166  }
167 }
168 
169 void ImgWriterOgr::setCodec(OGRSFDriver *poDriver){
170  OGRRegisterAll();
171  if( poDriver == NULL ){
172  std::string errorString="FileOpenError";
173  throw(errorString);
174  }
175  m_datasource = OGRSFDriverRegistrar::Open( m_filename.c_str(), TRUE );
176  if( m_datasource == NULL ){
177  m_datasource = OGRSFDriverRegistrar::Open( m_filename.c_str(), FALSE );
178  if ( m_datasource != NULL){// we can only open in not update mode
179  std::string errorString="Update mode not supported, delete output dataset first";
180  throw(errorString);
181  OGRDataSource::DestroyDataSource(m_datasource);
182  m_datasource = NULL;
183  }
184  else //create the data source
185  m_datasource=poDriver->CreateDataSource(m_filename.c_str(),NULL);
186  }
187  else{//datasets exists, always overwrite all layers (no update append for now)
188  int nLayerCount = m_datasource->GetLayerCount();
189  for(int iLayer = 0; iLayer < nLayerCount; ++iLayer){
190  if(m_datasource->DeleteLayer(iLayer)!=OGRERR_NONE){
191  std::string errorstring="DeleteLayer() failed when overwrite requested";
192  throw(errorstring);
193  }
194  }
195  }
196  if(m_datasource==NULL){
197  std::string errorString="Creation of output file failed";
198  throw(errorString);
199  }
200 }
201 
202 // OGRLayer* ImgWriterOgr::copyLayer(OGRLayer* poSrcLayer, const std::string& layername, char** papszOptions)
203 // {
204 // return(m_datasource->CopyLayer(poSrcLayer, layername.c_str(),papszOptions));
205 // }
206 
207 OGRLayer* ImgWriterOgr::createLayer(const std::string& layername, const std::string& theProjection, const OGRwkbGeometryType& eGType, char** papszOptions)
208 {
209  if( !m_datasource->TestCapability( ODsCCreateLayer ) ){
210  std::string errorString="Test capability to create layer failed";
211  throw(errorString);
212  }
213  //papszOptions = CSLSetNameValue( papszOptions, "DIM", "1" );
214  //if points: use wkbPoint
215  //if no constraints on the types geometry to be written: use wkbUnknown
216  OGRLayer* poLayer;
217 
218  OGRSpatialReference oSRS;
219 
220  if(theProjection!=""){
221  oSRS.SetFromUserInput(theProjection.c_str());
222  poLayer=m_datasource->CreateLayer( layername.c_str(), &oSRS, eGType,papszOptions );
223  // if(theProjection.find("EPSPG:")!=std::string::npos){
224  // int epsg_code=atoi(theProjection.substr(theProjection.find_first_not_of("EPSG:")).c_str());
225  // OGRSpatialReference oSRS;
226  // oSRS.importFromEPSG(epsg_code);
227  // poLayer=m_datasource->CreateLayer( layername.c_str(), &oSRS, eGType,papszOptions );
228  // }
229  // else{
230  // OGRSpatialReference oSRS(theProjection.c_str());
231  // poLayer=m_datasource->CreateLayer( layername.c_str(), &oSRS, eGType,papszOptions );
232  // }
233  // }
234  // oSRS.importFromProj4(theProjection);
235  }
236  else
237  poLayer=m_datasource->CreateLayer( layername.c_str(), NULL, eGType,papszOptions );
238  //check if destroy is needed?!
239  CSLDestroy( papszOptions );
240  if( poLayer == NULL ){
241  std::string errorstring="Layer creation failed";
242  throw(errorstring);
243  }
244  // OGRFeatureDefn *poFDefn = poLayer->GetLayerDefn();
245  return poLayer;
246 }
247 
248 void ImgWriterOgr::createField(const std::string& fieldname, const OGRFieldType& fieldType, int theLayer)
249 {
250  OGRFieldDefn oField( fieldname.c_str(), fieldType );
251  if(fieldType==OFTString)
252  oField.SetWidth(32);
253  if(theLayer<0)
254  theLayer=m_datasource->GetLayerCount()-1;//get back layer
255  if(m_datasource->GetLayer(theLayer)->CreateField( &oField ) != OGRERR_NONE ){
256  std::ostringstream es;
257  es << "Creating field " << fieldname << " failed";
258  std::string errorString=es.str();
259  throw(errorString);
260  }
261 }
262 
263 int ImgWriterOgr::getFields(std::vector<std::string>& fields, int layer) const
264 {
265  if(layer<0)
266  layer=m_datasource->GetLayerCount()-1;
267  assert(m_datasource->GetLayerCount()>layer);
268  OGRLayer *poLayer;
269  if((poLayer = m_datasource->GetLayer(layer))==NULL){
270  std::string errorstring="Could not get layer";
271  throw(errorstring);
272  }
273  OGRFeatureDefn *poFDefn = poLayer->GetLayerDefn();
274  fields.clear();
275  fields.resize(poFDefn->GetFieldCount());
276  for(int iField=0;iField<poFDefn->GetFieldCount();++iField){
277  OGRFieldDefn *poFieldDefn = poFDefn->GetFieldDefn(iField);
278  fields[iField]=poFieldDefn->GetNameRef();
279  }
280  return(fields.size());
281 }
282 
283 int ImgWriterOgr::getFields(std::vector<OGRFieldDefn*>& fields, int layer) const
284 {
285  if(layer<0)
286  layer=m_datasource->GetLayerCount()-1;
287  assert(m_datasource->GetLayerCount()>layer);
288  OGRLayer *poLayer;
289  if((poLayer = m_datasource->GetLayer(layer))==NULL){
290  std::string errorstring="Could not get layer";
291  throw(errorstring);
292  }
293  OGRFeatureDefn *poFDefn = poLayer->GetLayerDefn();
294  fields.clear();
295  fields.resize(poFDefn->GetFieldCount());
296  for(int iField=0;iField<poFDefn->GetFieldCount();++iField){
297  OGRFieldDefn *poFieldDefn = poFDefn->GetFieldDefn(iField);
298  fields[iField]=poFDefn->GetFieldDefn(iField);
299  }
300  assert(fields.size()==getFieldCount(layer));
301  return(fields.size());
302 }
303 
304 void ImgWriterOgr::copyFields(const ImgReaderOgr& imgReaderOgr, int srcLayer, int targetLayer){
305  if(targetLayer<0)
306  targetLayer=m_datasource->GetLayerCount()-1;//get back layer
307  //get fields from imgReaderOgr
308  std::vector<OGRFieldDefn*> fields;
309 
310  imgReaderOgr.getFields(fields,srcLayer);
311  for(int iField=0;iField<fields.size();++iField){
312  if(m_datasource->GetLayer(targetLayer)->CreateField(fields[iField]) != OGRERR_NONE ){
313  std::ostringstream es;
314  es << "Creating field " << fields[iField]->GetNameRef() << " failed";
315  std::string errorString=es.str();
316  throw(errorString);
317  }
318  }
319 }
320 
321 void ImgWriterOgr::addPoint(double x, double y, const std::map<std::string,double>& pointAttributes, std::string fieldName, const std::string& theId, int layer){
322  OGRFeature *poFeature;
323  poFeature=createFeature(layer);
324  OGRPoint pt;
325  poFeature->SetField( fieldName.c_str(), theId.c_str());
326  for(std::map<std::string,double>::const_iterator mit=pointAttributes.begin();mit!=pointAttributes.end();++mit){
327  poFeature->SetField((mit->first).c_str(), mit->second);
328  }
329  pt.setX(x);
330  pt.setY(y);
331  poFeature->SetGeometry( &pt );
332  if(createFeature(poFeature,layer)!=OGRERR_NONE){
333  std::string errorString="Failed to create feature";
334  throw(errorString);
335  }
336  OGRFeature::DestroyFeature( poFeature );
337 }
338 
339 void ImgWriterOgr::addPoint(double x, double y, const std::map<std::string,double>& pointAttributes, std::string fieldName, int theId, int layer){
340  OGRFeature *poFeature;
341  poFeature = createFeature(layer);
342  OGRPoint pt;
343  if(pointAttributes.size()+1!=poFeature->GetFieldCount()){
344  std::ostringstream ess;
345  ess << "Failed to add feature: " << pointAttributes.size() << " !=" << poFeature->GetFieldCount() << std::endl;
346  throw(ess.str());
347  }
348  assert(pointAttributes.size()+1==poFeature->GetFieldCount());
349  poFeature->SetField( fieldName.c_str(), theId);
350  int fid=0;
351  for(std::map<std::string,double>::const_iterator mit=pointAttributes.begin();mit!=pointAttributes.end();++mit){
352  poFeature->SetField((mit->first).c_str(),mit->second);
353  }
354  pt.setX(x);
355  pt.setY(y);
356  poFeature->SetGeometry( &pt );
357  if(createFeature(poFeature,layer)!=OGRERR_NONE){
358  std::string errorString="Failed to create feature";
359  throw(errorString);
360  }
361  OGRFeature::DestroyFeature( poFeature );
362 }
363 
364 //add a line std::string (polygon), caller is responsible to close the line (end point=start point)
365 void ImgWriterOgr::addLineString(std::vector<OGRPoint*>& points, const std::string& fieldName, int theId, int layer){
366  OGRFeature *poFeature;
367  poFeature = createFeature(layer);
368  poFeature->SetStyleString("PEN(c:#FF0000,w:5px)");//see also http://www.gdal.org/ogr/ogr_feature_style.html
369  poFeature->SetField( fieldName.c_str(), theId);
370  OGRLineString theLineString;
371  theLineString.setNumPoints(points.size());
372  for(int ip=0;ip<points.size();++ip)
373  theLineString.setPoint(ip,points[ip]);
374  if(poFeature->SetGeometry( &theLineString )!=OGRERR_NONE){
375  std::string errorString="Failed to set line OGRLineString as feature geometry";
376  throw(errorString);
377  }
378  if(createFeature(poFeature,layer)!=OGRERR_NONE){
379  std::string errorString="Failed to create feature";
380  throw(errorString);
381  }
382  OGRFeature::DestroyFeature( poFeature );
383 }
384 
385 //add a ring (polygon), caller is responsible to close the line (end point=start point)?
386 void ImgWriterOgr::addRing(std::vector<OGRPoint*>& points, const std::string& fieldName, int theId, int layer){
387  OGRFeature *poFeature;
388  poFeature = createFeature(layer);
389  poFeature->SetStyleString("PEN(c:#FF0000,w:5px)");//see also http://www.gdal.org/ogr/ogr_feature_style.html
390  poFeature->SetField( fieldName.c_str(), theId);
391  // OGRLineString theLineString;
392  // theLineString.setNumPoints(points.size());
393  OGRPolygon thePolygon;
394  OGRLinearRing theRing;
395  for(int ip=0;ip<points.size();++ip)
396  theRing.addPoint(points[ip]);
397  // theRing.addPoint(points[0]);//close the ring
398  theRing.closeRings();//redundent with previous line?
399  thePolygon.addRing(&theRing);
400  // SetSpatialFilter(&thePolygon)
401  poFeature->SetGeometry( &thePolygon );
402  if(createFeature(poFeature,layer)!=OGRERR_NONE){
403  std::string errorString="Failed to create feature";
404  throw(errorString);
405  OGRFeature::DestroyFeature( poFeature );
406  }
407  if(poFeature->SetGeometry( &thePolygon )!=OGRERR_NONE){
408  std::string errorString="Failed to set polygon as feature geometry";
409  throw(errorString);
410  }
411  OGRFeature::DestroyFeature( poFeature );
412 }
413 
414 //add a line string (polygon), caller is responsible to close the line (end point=start point)
415 void ImgWriterOgr::addLineString(std::vector<OGRPoint*>& points, const std::string& fieldName, const std::string& theId, int layer){
416  OGRFeature *poFeature;
417  poFeature = createFeature(layer);
418  poFeature->SetField( fieldName.c_str(), theId.c_str());
419  OGRLineString theLineString;
420  theLineString.setNumPoints(points.size());
421  for(int ip=0;ip<points.size();++ip)
422  theLineString.setPoint(ip,points[ip]);
423  if(poFeature->SetGeometry( &theLineString )!=OGRERR_NONE){
424  std::string errorString="Failed to set line OGRLineString as feature geometry";
425  throw(errorString);
426  }
427  if(createFeature(poFeature,layer)!=OGRERR_NONE){
428  std::string errorString="Failed to create feature";
429  throw(errorString);
430  }
431  OGRFeature::DestroyFeature( poFeature );
432 }
433 
434 OGRFeature* ImgWriterOgr::createFeature(int layer){
435  return(OGRFeature::CreateFeature(m_datasource->GetLayer(layer)->GetLayerDefn()));
436 }
437 
438 OGRErr ImgWriterOgr::createFeature(OGRFeature *theFeature,int layer){
439  return m_datasource->GetLayer(layer)->CreateFeature(theFeature);
440 }
441 
442 int ImgWriterOgr::getFieldCount(int layer) const
443 {
444  assert(m_datasource->GetLayerCount()>layer);
445  OGRLayer *poLayer;
446  if((poLayer = m_datasource->GetLayer(layer))==NULL){
447  std::string errorstring="Could not get layer";
448  throw(errorstring);
449  }
450  OGRFeatureDefn *poFDefn = poLayer->GetLayerDefn();
451  return(poFDefn->GetFieldCount());
452 }
453 
454 int ImgWriterOgr::getFeatureCount(int layer) const
455 {
456  return(getLayer(layer)->GetFeatureCount());
457 }
458 
459 int ImgWriterOgr::ascii2ogr(const std::string& filename, const std::string &layername, const std::vector<std::string>& fieldName, const std::vector<OGRFieldType>& fieldType, short colX, short colY, const std::string& theProjection, const OGRwkbGeometryType& eGType, const char fs)
460 {
461  char **papszOptions=NULL;
462  createLayer(layername, theProjection, eGType, papszOptions);
463  //create attribute fields that should appear on the layer. Fields must be added to the layer before any features are written. To create a field we initialize an OGRField object with the information about the field. In the case of Shapefiles, the field width and precision is significant in the creation of the output .dbf file, so we set it specifically, though generally the defaults are OK
464  int ncol=fieldName.size();
465  assert(colX>=0);
466  assert(colY>=0);
467  assert(colX<ncol+2);
468  assert(colY<ncol+2);
469  for(int ifield=0;ifield<ncol;++ifield)
470  createField(fieldName[ifield],fieldType[ifield]);
471  //create a local OGRFeature, set attributes and attach geometry before trying to write it to the layer. It is imperative that this feature be instantiated from the OGRFeatureDefn associated with the layer it will be written to.
472  //todo: try to open and catch if failure...
473  std::ifstream fpoints(filename.c_str(),std::ios::in);
474  std::string line;
475  OGRPolygon thePolygon;
476  OGRLinearRing theRing;
477  OGRPoint firstPoint;
478  OGRFeature *polyFeature;
479  if(eGType!=wkbPoint)
480  polyFeature=createFeature();
481 
482 
483  if(fs>' '&&fs<='~'){//field separator is a regular character (minimum ASCII code is space, maximum ASCII code is tilde)
484  std::string csvRecord;
485  while(getline(fpoints,csvRecord)){//read a line
486  OGRFeature *pointFeature;
487  if(eGType==wkbPoint)
488  pointFeature=createFeature();
489  OGRPoint thePoint;
490  bool skip=false;
491  std::istringstream csvstream(csvRecord);
492  std::string value;
493  int colId=0;
494  int fieldId=0;
495  while(getline(csvstream,value,fs)){//read a column
496  if(colId==colX)
497  thePoint.setX(atof(value.c_str()));
498  else if(colId==colY)
499  thePoint.setY(atof(value.c_str()));
500  else{
501  switch(fieldType[fieldId]){
502  case(OFTReal):
503  if(eGType==wkbPoint)
504  pointFeature->SetField(fieldId,atof(value.c_str()));
505  else if(firstPoint.IsEmpty())
506  polyFeature->SetField(fieldId,atof(value.c_str()));
507  break;
508  case(OFTInteger):
509  if(eGType==wkbPoint)
510  pointFeature->SetField(fieldId,atoi(value.c_str()));
511  else if(firstPoint.IsEmpty())
512  polyFeature->SetField(fieldId,atoi(value.c_str()));
513  break;
514  case(OFTString):
515  if(eGType==wkbPoint)
516  pointFeature->SetField(fieldId,value.c_str());
517  else if(firstPoint.IsEmpty())
518  polyFeature->SetField(fieldId,value.c_str());
519  break;
520  default:
521  break;
522  }
523  ++fieldId;
524  }
525  ++colId;
526  }
527  if(colId!=fieldId+2){
528  std::ostringstream ess;
529  ess << "Error: colId = " << colId << " is different from fieldId+2 = " << fieldId;
530  throw(ess.str());
531  }
532  if(eGType==wkbPoint){
533  pointFeature->SetGeometry( &thePoint );
534  if(createFeature(pointFeature)!=OGRERR_NONE){
535  std::string errorString="Failed to create feature";
536  throw(errorString);
537  OGRFeature::DestroyFeature( pointFeature );
538  }
539  }
540  else{
541  if(firstPoint.IsEmpty()){
542  firstPoint=thePoint;
543  }
544  theRing.addPoint(&thePoint);
545  }
546  }
547  }
548  else{//space or tab delimited fields
549  while(getline(fpoints,line)){
550  OGRFeature *pointFeature;
551  if(eGType==wkbPoint)
552  pointFeature=createFeature();
553  OGRPoint thePoint;
554  bool skip=false;
555  std::istringstream ist(line);
556  std::string value;
557  int colId=0;
558  int fieldId=0;
559  while(ist >> value){
560  if(colId==colX)
561  thePoint.setX(atof(value.c_str()));
562  else if(colId==colY)
563  thePoint.setY(atof(value.c_str()));
564  else{
565  switch(fieldType[fieldId]){
566  case(OFTReal):
567  if(eGType==wkbPoint)
568  pointFeature->SetField(fieldId,atof(value.c_str()));
569  else if(firstPoint.IsEmpty())
570  polyFeature->SetField(fieldId,atof(value.c_str()));
571  break;
572  case(OFTInteger):
573  if(eGType==wkbPoint)
574  pointFeature->SetField(fieldId,atoi(value.c_str()));
575  else if(firstPoint.IsEmpty())
576  polyFeature->SetField(fieldId,atoi(value.c_str()));
577  break;
578  case(OFTString):
579  if(eGType==wkbPoint)
580  pointFeature->SetField(fieldId,value.c_str());
581  else if(firstPoint.IsEmpty())
582  polyFeature->SetField(fieldId,value.c_str());
583  break;
584  default:
585  break;
586  }
587  ++fieldId;
588  }
589  ++colId;
590  }
591  if(colId!=fieldId+2){
592  std::ostringstream ess;
593  ess << "Error: colId = " << colId << " is different from fieldId+2 = " << fieldId;
594  throw(ess.str());
595  }
596  if(eGType==wkbPoint){
597  pointFeature->SetGeometry( &thePoint );
598  if(createFeature(pointFeature)!=OGRERR_NONE){
599  std::string errorString="Failed to create feature";
600  throw(errorString);
601  OGRFeature::DestroyFeature( pointFeature );
602  }
603  }
604  else{
605  if(firstPoint.IsEmpty()){
606  firstPoint=thePoint;
607  }
608  theRing.addPoint(&thePoint);
609  }
610  }
611  }
612  if(eGType!=wkbPoint){
613  theRing.addPoint(&firstPoint);//close the ring
614  thePolygon.addRing(&theRing);
615  // SetSpatialFilter(&thePolygon)
616  polyFeature->SetGeometry( &thePolygon );
617  if(createFeature(polyFeature)!=OGRERR_NONE){
618  std::string errorString="Failed to create feature";
619  throw(errorString);
620  OGRFeature::DestroyFeature( polyFeature );
621  }
622  }
623  return getFeatureCount();
624 }
625 
626 int ImgWriterOgr::addData(const ImgReaderGdal& imgReader, int theLayer, bool verbose)
627 {
628  OGRLayer *poLayer;
629  assert(m_datasource->GetLayerCount()>theLayer);
630  if(verbose)
631  std::cout << "number of layers: " << m_datasource->GetLayerCount() << std::endl;
632  if(verbose)
633  std::cout << "get layer " << theLayer << std::endl;
634  poLayer = m_datasource->GetLayer(theLayer);
635  //start reading features from the layer
636  OGRFeature *poFeature;
637  if(verbose)
638  std::cout << "reset reading" << std::endl;
639  poLayer->ResetReading();
640  for(int iband=0;iband<imgReader.nrOfBand();++iband){
641  std::ostringstream fs;
642  fs << "band" << iband;
643  createField(fs.str(),OFTReal,theLayer);
644  }
645  OGRFeatureDefn *poFDefn = poLayer->GetLayerDefn();
646  int nfield=poFDefn->GetFieldCount();
647  if(verbose)
648  std::cout << "new number of fields: " << nfield << std::endl;
649  while( (poFeature = poLayer->GetNextFeature()) != NULL ){
650  OGRGeometry *poGeometry;
651  poGeometry = poFeature->GetGeometryRef();
652  assert(poGeometry != NULL
653  && wkbFlatten(poGeometry->getGeometryType()) == wkbPoint);
654  OGRPoint *poPoint = (OGRPoint *) poGeometry;
655  double x=poPoint->getX();
656  double y=poPoint->getY();
657  for(int iband=0;iband<imgReader.nrOfBand();++iband){
658  double imgData;
659  imgReader.readData(imgData,GDT_Float64,x,y,iband);
660  //todo: put imgdata in field
661  std::ostringstream fs;
662  fs << "band" << iband;
663  poFeature->SetField(fs.str().c_str(),imgData);
664  }
665  }
666  return(nfield);
667 }
668