All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
DataSet2Layer.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 terralib/qt/widgets/layer/utils/DataSet2Layer.cpp
22 
23  \brief ....
24 */
25 
26 // TerraLib
27 #include "../../../../common/Translator.h"
28 #include "../../../../dataaccess/dataset/DataSetType.h"
29 #include "../../../../dataaccess/datasource/DataSourceManager.h"
30 #include "../../../../dataaccess/datasource/DataSourceTransactor.h"
31 #include "../../../../dataaccess/utils/Utils.h"
32 #include "../../../../geometry/Envelope.h"
33 #include "../../../../geometry/GeometryProperty.h"
34 #include "../../../../maptools/AbstractLayer.h"
35 #include "../../../../maptools/DataSetLayer.h"
36 #include "../../../../raster/Grid.h"
37 #include "../../../../raster/RasterProperty.h"
38 #include "../../../../se/Utils.h"
39 #include "../../../../srs/Config.h"
40 #include "../../Exception.h"
41 #include "DataSet2Layer.h"
42 
43 // Boost
44 #include <boost/uuid/random_generator.hpp>
45 #include <boost/uuid/uuid_io.hpp>
46 
47 te::qt::widgets::DataSet2Layer::DataSet2Layer(const std::string& datasourceId)
48  : m_datasourceId(datasourceId)
49 {
50 }
51 
53 {
54  static boost::uuids::basic_random_generator<boost::mt19937> gen;
55 
56  if(dataset.get() == 0)
57  throw Exception(TE_TR("Can not convert a NULL dataset to a layer!"));
58 
59  boost::uuids::uuid u = gen();
60  std::string id = boost::uuids::to_string(u);
61 
62  std::string title = dataset->getTitle().empty() ? dataset->getName() : dataset->getTitle();
63 
64  te::map::DataSetLayerPtr layer(new te::map::DataSetLayer(id, title));
65  layer->setDataSetName(dataset->getName());
66  layer->setDataSourceId(m_datasourceId);
67  layer->setVisibility(te::map::NOT_VISIBLE);
68  layer->setRendererType("ABSTRACT_LAYER_RENDERER");
69 
70  if(dataset->size() == 0)
71  {
73  te::da::LoadProperties(dataset.get(), m_datasourceId);
74  }
75 
76  if(dataset->hasGeom())
77  {
79  std::auto_ptr<te::gm::Envelope> mbr(te::da::GetExtent(dataset->getName(), gp->getName(), m_datasourceId));
80  layer->setSRID(gp->getSRID());
81  if (mbr.get()!=0)
82  layer->setExtent(*mbr);
83  layer->setStyle(te::se::CreateFeatureTypeStyle(gp->getGeometryType()));
84  }
85  else if(dataset->hasRaster())
86  {
87  te::rst::Grid* grid = te::da::GetFirstRasterProperty(dataset.get())->getGrid();
88 
89  layer->setSRID(grid->getSRID());
90  layer->setExtent(*(grid->getExtent()));
91  layer->setStyle(te::se::CreateCoverageStyle(te::da::GetFirstRasterProperty(dataset.get())->getBandProperties()));
92  }
93  else
94  {
95  layer->setSRID(TE_UNKNOWN_SRS);
96  //layer->setExtent(te::gm::Envelope());
97  }
98 
99  return layer;
100 }
101 
102 te::map::DataSetLayerPtr te::qt::widgets::DataSet2Layer::operator()(const te::da::DataSetTypePtr& dataset, const std::string& geomPropertyName) const
103 {
104  static boost::uuids::basic_random_generator<boost::mt19937> gen;
105 
106  if(dataset.get() == 0)
107  throw Exception(TE_TR("Can not convert a NULL dataset to a layer!"));
108 
109  boost::uuids::uuid u = gen();
110  std::string id = boost::uuids::to_string(u);
111 
112  std::string title = dataset->getTitle().empty() ? dataset->getName() : dataset->getTitle();
113 
114  te::map::DataSetLayerPtr layer(new te::map::DataSetLayer(id, title));
115  layer->setDataSetName(dataset->getName());
116  layer->setDataSourceId(m_datasourceId);
117  layer->setVisibility(te::map::NOT_VISIBLE);
118  layer->setRendererType("ABSTRACT_LAYER_RENDERER");
119 
120  if(dataset->size() == 0)
121  {
123  te::da::LoadProperties(dataset.get(), m_datasourceId);
124  }
125 
126  if(dataset->hasGeom())
127  {
128  te::gm::GeometryProperty* gp = 0;
129  geomPropertyName.empty() ? gp = te::da::GetFirstGeomProperty(dataset.get()) : gp = dynamic_cast<te::gm::GeometryProperty*>(dataset->getProperty(geomPropertyName));
130 
131  assert(gp);
132 
133  std::auto_ptr<te::gm::Envelope> mbr(te::da::GetExtent(dataset->getName(), gp->getName(), m_datasourceId));
134  layer->setSRID(gp->getSRID());
135  if (mbr.get()!=0)
136  layer->setExtent(*mbr);
137  layer->setStyle(te::se::CreateFeatureTypeStyle(gp->getGeometryType()));
138  layer->setGeomPropertytName(geomPropertyName);
139  }
140  else if(dataset->hasRaster())
141  {
142  te::rst::RasterProperty* rp = 0;
143  geomPropertyName.empty() ? rp = te::da::GetFirstRasterProperty(dataset.get()) : rp = dynamic_cast<te::rst::RasterProperty*>(dataset->getProperty(geomPropertyName));
144 
145  assert(rp);
146 
147  te::rst::Grid* grid = rp->getGrid();
148 
149  layer->setSRID(grid->getSRID());
150  layer->setExtent(*(grid->getExtent()));
151  layer->setStyle(te::se::CreateCoverageStyle(te::da::GetFirstRasterProperty(dataset.get())->getBandProperties()));
152  layer->setGeomPropertytName(geomPropertyName);
153  }
154  else
155  {
156  layer->setSRID(TE_UNKNOWN_SRS);
157  //layer->setExtent(te::gm::Envelope());
158  }
159 
160  return layer;
161 }
TEDATAACCESSEXPORT te::rst::RasterProperty * GetFirstRasterProperty(const DataSetType *dt)
Definition: Utils.cpp:571
Geometric property.
TEDATAACCESSEXPORT te::gm::Envelope * GetExtent(const std::string &datasetName, const std::string &propertyName, const std::string &datasourceId)
Definition: Utils.cpp:136
TEDATAACCESSEXPORT void LoadProperties(te::da::DataSetType *dataset, const std::string &datasourceId)
Definition: Utils.cpp:120
boost::shared_ptr< DataSetType > DataSetTypePtr
Definition: DataSetType.h:653
boost::shared_ptr< DataSource > DataSourcePtr
Definition: DataSource.h:1435
int getSRID() const
Returns the grid spatial reference system identifier.
Definition: Grid.cpp:265
TESEEXPORT Style * CreateCoverageStyle(const std::vector< te::rst::BandProperty * > &properties)
Try creates an appropriate coverage style based on given band properties.
Definition: Utils.cpp:299
te::map::DataSetLayerPtr operator()(const te::da::DataSetTypePtr &dataset) const
#define TE_TR(message)
It marks a string in order to get translated.
Definition: Translator.h:347
Raster property.
TESEEXPORT Style * CreateFeatureTypeStyle(const te::gm::GeomType &geomType)
Try creates an appropriate feature type style based on given geometry type.
Definition: Utils.cpp:284
int getSRID() const
It returns the spatial reference system identifier associated to this property.
static DataSourceManager & getInstance()
It returns a reference to the singleton instance.
#define TE_UNKNOWN_SRS
A numeric value to represent a unknown SRS identification in TerraLib.
Definition: Config.h:44
GeomType getGeometryType() const
It returns the geometry subtype allowed for the property.
DataSet2Layer(const std::string &datasourceId)
te::rst::Grid * getGrid()
Returns the definition of the raster grid support.
boost::intrusive_ptr< DataSetLayer > DataSetLayerPtr
Definition: DataSetLayer.h:171
te::gm::Envelope * getExtent()
Returns the geographic extension of the grid.
Definition: Grid.cpp:275
A layer with reference to a dataset.
Definition: DataSetLayer.h:47
void setSRID(int srid)
Just sets the grid spatial reference system identifier.
Definition: Grid.cpp:270
TEDATAACCESSEXPORT te::gm::GeometryProperty * GetFirstGeomProperty(const DataSetType *dt)
Definition: Utils.cpp:557
A rectified grid is the spatial support for raster data.
Definition: Grid.h:68
const std::string & getName() const
It returns the property name.
Definition: Property.h:127