Differences
This shows you the differences between two versions of the page.
| Both sides previous revision Previous revision | |||
|
wiki:documentation:mini_curso:dataaccess [2016/01/20 14:24] gribeiro |
wiki:documentation:mini_curso:dataaccess [2016/01/20 14:56] (current) gribeiro |
||
|---|---|---|---|
| Line 40: | Line 40: | ||
| // TerraLib | // TerraLib | ||
| #include <terralib/common.h> | #include <terralib/common.h> | ||
| + | #include <terralib/dataaccess.h> | ||
| #include <terralib/plugin.h> | #include <terralib/plugin.h> | ||
| Line 69: | Line 70: | ||
| } | } | ||
| + | void PrintDataSet(std::string datasetName, te::da::DataSet* dataset) | ||
| + | { | ||
| + | if(dataset == 0) | ||
| + | { | ||
| + | std::cout << "The informed dataset is NULL!" << std::endl; | ||
| + | return; | ||
| + | } | ||
| + | |||
| + | // this will be used just to count the items in the dataset | ||
| + | int item = 0; | ||
| + | |||
| + | // traverse the dataset and print each dataset item | ||
| + | std::cout << "DATASET: " << datasetName; | ||
| + | |||
| + | while(dataset->moveNext()) | ||
| + | { | ||
| + | std::cout << std::endl << "ITEM NUMBER: " << item++ << " =======================" << std::endl; | ||
| + | |||
| + | for(std::size_t i = 0; i < dataset->getNumProperties(); ++i) | ||
| + | { | ||
| + | std::cout << dataset->getPropertyName(i) << ": " ; | ||
| + | |||
| + | if(dataset->isNull(i)) | ||
| + | { | ||
| + | std::cout << std::endl; | ||
| + | continue; | ||
| + | } | ||
| + | |||
| + | std::cout << dataset->getAsString(i) << std::endl; | ||
| + | } | ||
| + | } | ||
| + | } | ||
| + | |||
| + | void ReadShapefile() | ||
| + | { | ||
| + | std::unique_ptr<te::da::DataSource> ds = te::da::DataSourceFactory::make("OGR"); | ||
| + | |||
| + | std::map<std::string, std::string> connInfo; | ||
| + | connInfo["URI"] = "/home/terralib5/curso/data/shp/munic_2001.shp"; | ||
| + | |||
| + | ds->setConnectionInfo(connInfo); | ||
| + | |||
| + | ds->open(); | ||
| + | |||
| + | std::cout << "Datasource is opened? " | ||
| + | << std::boolalpha | ||
| + | << ds->isOpened() << std::endl; | ||
| + | |||
| + | // check point: what can be done with this datasource | ||
| + | //PrintDataSourceCapabilities(ds.get()); | ||
| + | |||
| + | // check point: retrieving data from the datasource | ||
| + | std::cout << "\nDatasource has " | ||
| + | << ds->getNumberOfDataSets() | ||
| + | << " datasources" << std::endl; | ||
| + | |||
| + | std::vector<std::string> dsets = ds->getDataSetNames(); | ||
| + | |||
| + | for (size_t i=0; i<ds->getNumberOfDataSets(); ++i) | ||
| + | std::cout << '[' << (i + 1) | ||
| + | << "]: " | ||
| + | << dsets[i] | ||
| + | << std::endl; | ||
| + | |||
| + | // check point: retrieving the data from a dataset of the datasource | ||
| + | if(ds->getNumberOfDataSets() == 0) | ||
| + | return; | ||
| + | |||
| + | std::unique_ptr<te::da::DataSet> dataset = ds->getDataSet(dsets[0]); | ||
| + | |||
| + | PrintDataSet(dsets[0], dataset.get()); | ||
| + | } | ||
| int main(int argc, char* argv[]) | int main(int argc, char* argv[]) | ||
| Line 75: | Line 148: | ||
| | | ||
| LoadModules(); | LoadModules(); | ||
| + | |||
| + | ReadShapefile(); | ||