Data Access → Drivers → OGR

The OGR data access driver allows applications to access raster data stored in several formats and systems. This driver is built on top of OGR multi-access library. This section describes the design and implementation issues of this driver and how it can be used by application developers.

Design

The OGR driver implements all the data access foundation classes.

From Theory to Practice

Using the OGR Dataaccess implementation to access geo datafiles

The above implementation allows one to obtain a OGR data source from the abstract factory using the “OGR” identifier.

The code snippet bellow shows how to open a geo data file as a TerraLib data source using OGR.

{
  std::map<std::string, std::string> connInfo;  
  connInfo["URI"] = filename;
 
  std::auto_ptr<te::da::DataSource> dsptr = te::da::DataSourceFactory::make("OGR");
  dsptr->setConnectionInfo(connInfo);
  dsptr->open();
 
  std::vector<std::string> dsNames = dsptr->getDataSetNames();
 
  std::cout  << std::endl << "Datasets in " << filename << ":" << std::endl;
 
  BOOST_FOREACH(std::string s, dsNames)
  {
    std::cout  << "\t"  << "\t" << s << std::endl;
  }
  dsNames.clear();
  dsptr->close();
}

The specific OGR driver might be detected from the geo data file extension (e.g. “teste.shp” → OGR “Esri Shapefile” driver) or, you can explicitly indicate it in the connection parameters using the keyword “DRIVER”. For example bellow shows how to explicitly define that OGR “GML” driver should be used.

{
  std::map<std::string, std::string> connInfo;  
  connInfo["URI"] = filename;
  connInfo["DRIVER"] = "GML";
 
  std::auto_ptr<te::da::DataSource> dsptr = te::da::DataSourceFactory::make("OGR");
  dsptr->setConnectionInfo(connInfo);
  dsptr->open();
...
}

You can also include any other format specific parameters in the connection string, as long as it is recognized by OGR. Make sure to check OGR documentation regarding driver names, limitations, versions and specific parameters of vector formats supported by OGR.

Using the OGR Dataaccess implementation to create geo datafiles

The code snippet bellow shows how export a TerraLib Dataset to a geofile using OGR implementation.

{
  // generate the dataset to be exported
  std::auto_ptr<te::da::DataSet> dsToExport; 
  dsToExport = ...
 
  std::map<std::string, std::string> connInfo;
  connInfo["URI"] = filename;                           // file path
  connInfo["DRIVER"] = drivername;                 // indicate explicitly the ogr format driver (optional)
  connInfo["OPT"] = ...;                                    // indicate other format driver specific parameters (optional)
 
  std::auto_ptr<te::da::DataSource> dsOGR = te::da::DataSourceFactory::make("OGR");
  dsOGR->setConnectionInfo(connInfo);
  dsOGR->open();
 
  dataset->moveBeforeFirst();
 
  te::da::Create(dsOGR.get(), dt.get(), dataset.get());
 
  dsOGR->close();
  dsOGR.release();
  connInfo.clear();
}

Again, read the OGR vector formats documentation to be aware of creation issues, restrictions and versions before creating you connection parameters.

You should check for exceptions that might be thrown due to errors such as wrong permissions, attempt to create geo data files that already exists, etc.

Module Summary

Final Remarks

References


QR Code
QR Code wiki:designimplementation:dataaccess:drivers:ogr (generated for current page)