Metadata.cpp
Go to the documentation of this file.
1 /* Copyright (C) 2008 National Institute For Space Research (INPE) - Brazil.
2 
3  This file is part of the TerraLib - a Framework for building GIS enabled applications.
4 
5  TerraLib is free software: you can redistribute it and/or modify
6  it under the terms of the GNU Lesser General Public License as published by
7  the Free Software Foundation, either version 3 of the License,
8  or (at your option) any later version.
9 
10  TerraLib is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU Lesser General Public License for more details.
14 
15  You should have received a copy of the GNU Lesser General Public License
16  along with TerraLib. See COPYING. If not, write to
17  TerraLib Team at <terralib-team@terralib.org>.
18  */
19 
20 /*!
21  \file Metadata.cpp
22  */
23 
24 // TerraLib
25 #include <terralib/metadata.h>
26 #include <terralib/dataaccess.h>
27 #include <terralib/metadata/serialization/MD_Metadata.h>
28 #include <terralib/srs/Config.h>
29 #include <terralib/xml/Writer.h>
30 
31 //Boost
32 #include <boost/format.hpp>
33 #include <boost/lexical_cast.hpp>
34 #include <boost/uuid/random_generator.hpp>
35 #include <boost/uuid/uuid_io.hpp>
36 
37 //STL
38 #include <fstream>
39 #include <iostream>
40 
41 void
43 {
44  // Open a data source
45  std::string ogrInfo("connection_string="TE_DATA_EXAMPLE_DIR"/data/shp");
46  std::map<std::string, std::string> connInfo;
47  connInfo["URI"] = ""TE_DATA_EXAMPLE_DIR"/data/shp";
48  std::auto_ptr<te::da::DataSource> ds = te::da::DataSourceFactory::make("OGR");
49  ds->setConnectionInfo(connInfo);
50  ds->open();
51 
52  std::auto_ptr<te::da::DataSourceTransactor> transactor = ds->getTransactor();
53 
54  std::vector<std::string> datasets = transactor->getDataSetNames();
55 
56  // Create an annotation for each dataset in the datasourc
57  for (unsigned int i=0; i<datasets.size(); ++i)
58  {
59  std::auto_ptr<te::da::DataSetType> dt = transactor->getDataSetType(datasets[i]);
60 
62  anot->setElementId(boost::lexical_cast<std::string>(i));
63  anot->setElementName(dt->getTitle());
64 
65  boost::format fname("%1%/%2%.xml");
66  fname = fname % TE_DATA_EXAMPLE_DIR"/data/shp" % dt->getTitle();
67  anot->setAnnotationURI(fname.str());
69  }
70 
71  // Print manager contents
72  const std::map<std::string,te::md::MetadataAnnotation*>& mm = te::md::MetadataManager::getInstance().getAnnotations();
73 
74  std::map<std::string,te::md::MetadataAnnotation*>::const_iterator it = mm.begin();
75 
76  std::string aid;
77  while (it != mm.end())
78  {
79  std::cout << "Annt id: " << it->second->getAnnotationtId() << std::endl
80  << " Elem id: " << it->second->getElementId() << std::endl
81  << " Elem name: " << it->second->getElementName() << std::endl
82  << " Annt URI: " << it->second->getAnnotationURI() << std::endl << std::endl ;
83  aid = it->second->getAnnotationtId();
84  ++it;
85  }
86 
87  // Find an annotation
89  if (aannot)
90  std::cout << "Annotation " << aid << " refers to element " << aannot->getElementName() << std::endl;
91 
93 
94  ds->close();
95 }
96 
97 
98 // This example shows how to manually annotate a resource with the minimum ISO19115 core metadata elements
99 void
101 {
102  const std::string ibge("Fundação Instituto Brasileiro de Geografia e Estatística - IBGE/ Diretoria de Geociências - DGC/ Coordenação de Cartografia - CCAR");
103 
104  // Create the metadata root element
105 
106  // this basically describes the metadata record
108  md->setFileId("ibge_topo");
109  md->setLanguage("por; BRA");
111  md->setStandardInfo("ISO 19115", "2003");
112  md->setDateStamp(boost::gregorian::date(boost::gregorian::from_undelimited_string("20041210")));
113  md->setMetadataURI(""TE_DATA_EXAMPLE_DIR"/data/ibge_topo.xml");
114  // now comes the identification section, with information about one or more resources
115 
116  // resource citation - part of data identification
117  te::md::CI_Citation* dt_ct = new te::md::CI_Citation("Carta Topográfica 1:25.000 SD24-X-A-VI-1-NO",
118  boost::gregorian::date(boost::gregorian::from_undelimited_string("19931201")),
120 
121  // resource abstract - part of data identification
122  std::string abstract("A série de carta topográfica na escala 1: 25 000 representa parte do território nacional, de forma articulada, por folhas (segundo a convenção internacional da Carta CIM), e que abrange um quadrilátero geográfico de 7,5' de latitude por 7,5' de longitude. O IBGE desenvolve a produção dessa série, em conjunto com a DSG - Diretoria de Serviço Geográfico do Exército. Esta base vetorial está estruturada em categorias de informação: Hidrografia, Hipsografia, Localidades, Limites, Sistemas de Transporte, Pontos de Referência, Vegetação e Obras e Edificações, segundo modelagem da Mapoteca Topográfica Digital do IBGE.");
123 
124  // the dataset identification
126 
127  dt_id->addCharacterSet(te::md::MD_utf8);
128 
130  dt_id->addPointOfContact(ds_poc);
131 
132  dt_id->addTopicCategory(te::md::MD_imageryBaseMapsEarthCover);
133 
134  dt_id->setSpatialRepType(te::md::MD_vector);
135 
136  dt_id->addGeographicBBoxExt(-45, -25, -40, -20);
137 
138  dt_id->setScale(25000);
139 
140  // associates the data identification to the metadata record
141  md->addIdentification(dt_id);
142 
143  // distribution information
145  dist->addDistrOption(new te::md::MD_Format("Microstation DGN","Windows NT"));
146  dist->setURL("http://www.ibge.gov.br/home/geociencias/default_prod.shtm#TOPO");
147 
148  md->setDistribution(dist);
149 
150  // reference system
152 
153  // lineage
154  md->setLineage("folha topográfica obtida a partir de levantamento aerofotogramétrico, na escala de 1:25.000, com apoio de campo por GPS e restituída em ambiente digital, através de equipamento restituidor fotogramétrico convencional (B9) repotencializado com aplicativo dedicado, elaborado em ambiente MicroStation");
155 
156  // encode the metadata record and persist it to its location.
157  std::ofstream ofs(md->getMetadataURI().c_str());
158  te::xml::Writer writer(ofs);
159  te::md::Save(md, writer);
160 
161  delete md;
162 }
163 
165 {
166  std::string ogrInfo("connection_string="TE_DATA_EXAMPLE_DIR"/data/shp");
167  std::map<std::string, std::string> connInfo;
168  connInfo["URI"] = ""TE_DATA_EXAMPLE_DIR"/data/shp";
169  std::auto_ptr<te::da::DataSource> ds = te::da::DataSourceFactory::make("OGR");
170  ds->setConnectionInfo(connInfo);
171  ds->open();
172 
173  std::auto_ptr<te::da::DataSourceTransactor> transactor = ds->getTransactor();
174 
175  std::vector<std::string> datasets = transactor->getDataSetNames();
176 
177  if (datasets.empty())
178  {
179  ds->close();
180  throw te::common::Exception("Couldn't find the datasets in the datasource");
181  }
182 
183  std::auto_ptr<te::da::DataSet> dataset = transactor->getDataSet(datasets[0]);
184  bool isbb = dataset->isBeforeBegin();
185 
186  if (dataset->isBeforeBegin()) dataset->moveFirst();
187 
188  if(!dataset.get())
189  return;
190 
191  te::md::MD_Metadata* md = te::md::Extract(dataset.get());
192 
193  delete md;
194  ds->close();
195 }
196 
198 {
199  // Open a data source
200  std::string ogrInfo("connection_string="TE_DATA_EXAMPLE_DIR"/data/shp");
201  std::map<std::string, std::string> connInfo;
202  connInfo["URI"] = ""TE_DATA_EXAMPLE_DIR"/data/shp";
203  std::auto_ptr<te::da::DataSource> ds = te::da::DataSourceFactory::make("OGR");
204  ds->setConnectionInfo(connInfo);
205  ds->open();
206 
207  std::auto_ptr<te::da::DataSourceTransactor> transactor = ds->getTransactor();
208 
209  std::vector<std::string> datasets = transactor->getDataSetNames();
210 
211 
212  // Create an annotation for each dataset in the datasourc
213  for (unsigned int i=0; i<datasets.size(); ++i)
214  {
215  std::auto_ptr<te::da::DataSet> dataset = transactor->getDataSet(datasets[i]);
216  std::auto_ptr<te::da::DataSetType> dt = transactor->getDataSetType(datasets[i]);
217 
218  if (dataset->isEmpty()) continue; //soilmeasure is empty.
219 
220  if (dataset->isBeforeBegin()) dataset->moveFirst(); //otherwise m_currentFeature is invalid in getWKB()
221 
222  te::md::MD_Metadata* md = te::md::Extract(dataset.get());
223 
224  // include manually other metadata elements
225  // ...
226  boost::format fname("%1%/%2%.xml");
227  fname = fname % TE_DATA_EXAMPLE_DIR"/data/shp" % dt->getTitle();
228  std::string ffname = fname.str();
229  md->setMetadataURI(ffname);
230  // serialize it
231  std::ofstream ofs(md->getMetadataURI().c_str());
232  te::xml::Writer writer(ofs);
233  te::md::Save(md, writer);
234 
235 
236  // feed the manager
238  anot->setElementId(boost::lexical_cast<std::string>(i));
239  anot->setElementName(dt->getTitle());
240 
241  anot->setAnnotationURI(md->getMetadataURI());
242  te::md::MetadataManager::getInstance().insert(anot);
243 
244  delete md;
245  }
246 
247  // Print manager contents
248  const std::map<std::string,te::md::MetadataAnnotation*>& mm = te::md::MetadataManager::getInstance().getAnnotations();
249  std::map<std::string,te::md::MetadataAnnotation*>::const_iterator it = mm.begin();
250  std::string aid;
251  while (it != mm.end())
252  {
253  std::cout << "Annt id: " << it->second->getAnnotationtId() << std::endl
254  << " Elem id: " << it->second->getElementId() << std::endl
255  << " Elem name: " << it->second->getElementName() << std::endl
256  << " Annt URI: " << it->second->getAnnotationURI() << std::endl << std::endl;
257  aid = it->second->getAnnotationtId();
258  ++it;
259  }
260 
261  // manager should be persisted
262  // ...
263  //
264 
266  ds->close();
267 }
void setReferenceSystem(int srid)
Sets the referency system.
static std::unique_ptr< DataSource > make(const std::string &driver, const te::core::URI &connInfo)
void setAnnotationURI(const std::string &uri)
Sets the link to the metadata.
void setLanguage(const std::string &lang)
Sets the language of the metadata.
void ExMetadataExtractor()
Definition: Metadata.cpp:164
void ExMetadataISO19115()
Definition: Metadata.cpp:100
const std::string & getMetadataURI() const
Returns URI to the metadata.
Information about the distributor of and the options for obtaining the resource.
This class models a XML writer object.
Definition: xml/Writer.h:52
void setDistribution(MD_Distribution *d)
Sets a distribution information to the metadata.
This file contains include headers for the metadata module of TerraLib.
void addIdentification(MD_Identification *mdid)
Adds a dataset identification to the metadata.
void setStandardInfo(const std::string &sname, const std::string &sversion)
Sets the name of the metadata standard/profile used.
static te::dt::Date ds(2010, 01, 01)
Provides a standardized method for citing a resource (dataset, feature, source, publication, etc.)
Definition: CI_Citation.h:53
void addDistrOption(MD_Format *opt)
Adds an distribution option.
static MetadataManager & getInstance()
It returns a reference to the singleton instance.
Information required to identify a dataset.
static te::dt::TimeDuration dt(20, 30, 50, 11)
A class to relate a metadata annotation to a TerraLib element (or entity).
TEMDEXPORT MD_Metadata * Extract(const te::da::DataSet *d)
Extract some metadata from a layer.
Root entity which defines metadata for a resource or resources.
TEMDEXPORT void Save(const te::md::MD_Metadata *md, te::xml::AbstractWriter &writer)
This class is designed to declare objects to be thrown as exceptions by TerraLib. ...
void setElementName(const std::string &ename)
Set the element being annotated name.
void setFileId(const std::string &fileid)
Sets a unique phrase or string which uniquely identifies the metadata file.
void setCharset(MD_CharacterSetCode chaset)
Sets the charset code used in the metadata.
void ExMetadataManager()
Definition: Metadata.cpp:42
void ExMetadataCycle()
Definition: Metadata.cpp:197
void setLineage(const std::string &stm)
Sets the lineage statement. General explanation of the data producer’s knowledge of the dataset line...
Contains the identify of person(s), and/or position, and/or organization(s) associated with the resou...
Format of the data.
Definition: MD_Format.h:45
void setURL(const std::string &url)
Sets an online resource.
void setMetadataURI(const std::string &mdURI)
Sets URI to the metadata .
void setDateStamp(const boost::gregorian::date &date)
Set metadata date stamp.
This file contains include headers for the Data Access module of TerraLib.
void setElementId(const std::string &eid)
Sets the TerrsLib element idenfification.
#define TE_SRS_SAD69_UTM_ZONE_24S
const std::string & getElementName() const
Gets the element being annotated name.
This class models a XML writer object.