All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
Utils.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/maptools/Utils.cpp
22 
23  \brief Utility functions for MapTools module.
24 */
25 
26 // TerraLib
27 #include "../common/progress/TaskProgress.h"
28 #include "../common/Translator.h"
29 #include "../common/STLUtils.h"
30 #include "../common/StringUtils.h"
31 #include "../dataaccess/dataset/DataSetType.h"
32 #include "../dataaccess/query/And.h"
33 #include "../dataaccess/query/DataSetName.h"
34 #include "../dataaccess/query/Field.h"
35 #include "../dataaccess/query/LiteralEnvelope.h"
36 #include "../dataaccess/query/PropertyName.h"
37 #include "../dataaccess/query/Select.h"
38 #include "../dataaccess/query/ST_Intersects.h"
39 #include "../dataaccess/query/Where.h"
40 #include "../dataaccess/utils/Utils.h"
41 #include "../fe/Literal.h"
42 #include "../geometry/GeometryProperty.h"
43 #include "../geometry/Utils.h"
44 #include "../memory/DataSet.h"
45 #include "../raster/Grid.h"
46 #include "../raster/Raster.h"
47 #include "../raster/RasterFactory.h"
48 #include "../raster/RasterProperty.h"
49 #include "../raster/RasterSummary.h"
50 #include "../raster/RasterSummaryManager.h"
51 #include "../raster/Utils.h"
52 #include "../se/ChannelSelection.h"
53 #include "../se/CoverageStyle.h"
54 #include "../se/FeatureTypeStyle.h"
55 #include "../se/ImageOutline.h"
56 #include "../se/RasterSymbolizer.h"
57 #include "../se/Rule.h"
58 #include "../se/Utils.h"
59 #include "../srs/Config.h"
60 #include "../srs/Converter.h"
61 #include "Canvas.h"
62 #include "CanvasConfigurer.h"
63 #include "DataSetLayer.h"
64 #include "Exception.h"
65 #include "QueryEncoder.h"
66 #include "RasterTransform.h"
68 #include "Utils.h"
69 
70 // Boost
71 #include <boost/format.hpp>
72 #include <boost/lexical_cast.hpp>
73 
74 // STL
75 #include <cassert>
76 #include <cstdlib>
77 #include <memory>
78 
79 te::gm::Envelope te::map::GetSelectedExtent(const std::list<te::map::AbstractLayerPtr> layers, int srid, bool onlyVisibles)
80 {
81  std::list<te::map::AbstractLayerPtr>::const_iterator it = layers.begin();
82 
83  te::gm::Envelope finalEnv;
84 
85  while(it != layers.end())
86  {
87  if(it == layers.begin())
88  finalEnv = GetSelectedExtent((*it), srid, onlyVisibles);
89  else
90  finalEnv.Union(GetSelectedExtent((*it), srid, onlyVisibles));
91 
92  ++it;
93  }
94 
95  return finalEnv;
96 }
97 
99 {
100  if(onlyVisibles && layer->getVisibility() == te::map::NOT_VISIBLE)
101  return te::gm::Envelope();
102 
103  te::gm::Envelope finalEnv;
104 
105  bool isFirst = true;
106 
107  if(!layer->hasChildren())
108  {
109  std::auto_ptr<te::da::DataSetType> dt(layer->getSchema());
110 
111  const te::da::ObjectIdSet* objSet = layer->getSelected();
112 
113  if(!objSet)
114  return te::gm::Envelope();
115 
116  std::auto_ptr<te::da::DataSet> ds(layer->getData(objSet));
117 
119 
120  while(ds->moveNext())
121  {
122  std::auto_ptr<te::gm::Geometry> geom = ds->getGeometry(geomProp->getName());
123 
124  te::gm::Envelope auxEnv(*geom->getMBR());
125 
126  if(layer->getSRID() != srid)
127  auxEnv.transform(layer->getSRID(), srid);
128 
129  if(isFirst)
130  {
131  finalEnv = auxEnv;
132  isFirst = false;
133  continue;
134  }
135 
136  finalEnv.Union(auxEnv);
137  }
138  }
139  else
140  {
141  for(std::size_t i = 0; i < layer->getChildrenCount(); ++i)
142  {
143  te::map::AbstractLayerPtr child(boost::dynamic_pointer_cast<AbstractLayer>(layer->getChild(i)));
144 
145  if(i == 0)
146  finalEnv = GetSelectedExtent(child, srid, onlyVisibles);
147  else
148  finalEnv.Union(GetSelectedExtent(child, srid, onlyVisibles));
149  }
150  }
151 
152  return finalEnv;
153 }
154 
155 void te::map::GetDashStyle(const std::string& dasharray, std::vector<double>& style)
156 {
157  std::vector<std::string> values;
158  te::common::Tokenize(dasharray, values);
159  for(std::size_t i = 0; i < values.size(); ++i)
160  style.push_back(atof(values[i].c_str()));
161 }
162 
164 {
165  if(layer == 0)
166  throw Exception(TE_TR("The layer is invalid!"));
167 
168 // name of referenced data set
169  std::string dsname = layer->getDataSetName();
170 
171  if(dsname.empty())
172  throw Exception(TE_TR("The data set name referenced by the layer is empty!"));
173 
174 // retrieve the associated data source
176 
177 // gets the data set type
178  std::auto_ptr<te::da::DataSetType> dstype(ds->getDataSetType(dsname));
179 
180  if(dstype.get() == 0)
181  throw Exception(TE_TR("Could not get the data set type!"));
182 
183 // gets the raster property
184  std::auto_ptr<te::rst::RasterProperty> rasterProperty(dynamic_cast<te::rst::RasterProperty*>(dstype->getProperties()[0]->clone()));
185 
186  if(rasterProperty.get() == 0)
187  throw Exception(TE_TR("Could not get the raster property!"));
188 
189  return rasterProperty.release();
190 }
191 
193 {
194  if(layer == 0)
195  throw Exception(TE_TR("The layer is invalid!"));
196 
197 // name of referenced data set
198  std::string dsname = layer->getDataSetName();
199 
200  if(dsname.empty())
201  throw Exception(TE_TR("The data set name referenced by the layer is empty!"));
202 
203 // retrieve the associated data source
205 
206 // gets the data set type
207  std::auto_ptr<te::da::DataSetType> dstype(ds->getDataSetType(dsname));
208 
209  if(dstype.get() == 0)
210  throw Exception(TE_TR("Could not get the data set type!"));
211 
212  if(!dstype->hasRaster())
213  throw Exception(TE_TR("The data set referenced by the layer not contains raster data!"));
214 
215 // get the referenced data set
216  std::auto_ptr<te::da::DataSet> dataset(ds->getDataSet(dsname));
217  if(dataset.get() == 0)
218  throw Exception(TE_TR("Could not get the data set reference by the layer!"));
219 
220 // gets the raster
221  std::size_t rpos = te::da::GetFirstPropertyPos(dataset.get(), te::dt::RASTER_TYPE);
222 
223  std::auto_ptr<te::rst::Raster> raster(dataset->getRaster(rpos));
224  if(raster.get() == 0)
225  throw Exception(TE_TR("Could not get the raster referenced by the layer!"));
226 
227  return raster.release();
228 }
229 
230 void te::map::GetVisibleLayers(const te::map::AbstractLayerPtr& layer, std::list<te::map::AbstractLayerPtr>& visibleLayers)
231 {
232  te::map::Visibility visibility = layer->getVisibility();
233 
234  if(visibility == te::map::NOT_VISIBLE)
235  return;
236 
237  if(visibility == te::map::VISIBLE && layer->isValid())
238  visibleLayers.push_back(layer);
239 
240  for(std::size_t i = 0; i < layer->getChildrenCount(); ++i)
241  {
242  te::map::AbstractLayerPtr child(boost::dynamic_pointer_cast<AbstractLayer>(layer->getChild(i)));
243 
244  GetVisibleLayers(child, visibleLayers);
245  }
246 }
247 
248 void te::map::GetVisibleLayers(const std::list<te::map::AbstractLayerPtr>& layers, std::list<te::map::AbstractLayerPtr>& visibleLayers)
249 {
250  for(std::list<te::map::AbstractLayerPtr>::const_iterator it = layers.begin(); it != layers.end(); ++it)
251  GetVisibleLayers(*it, visibleLayers);
252 }
253 
254 te::gm::Envelope te::map::GetExtent(const std::list<te::map::AbstractLayerPtr>& layers, int srid, bool onlyVisibles)
255 {
257 
258  for(std::list<te::map::AbstractLayerPtr>::const_iterator it = layers.begin(); it != layers.end(); ++it)
259  e.Union(GetExtent(*it, srid, onlyVisibles));
260 
261  return e;
262 }
263 
264 te::gm::Envelope te::map::GetExtent(const te::map::AbstractLayerPtr& layer, int srid, bool onlyVisibles)
265 {
266  if(onlyVisibles && layer->getVisibility() == te::map::NOT_VISIBLE)
267  return te::gm::Envelope();
268 
269  te::gm::Envelope e = layer->getExtent();
270 
271  if((layer->getSRID() != TE_UNKNOWN_SRS) && (srid != TE_UNKNOWN_SRS))
272  e.transform(layer->getSRID(), srid);
273 
274  for(std::size_t i = 0; i < layer->getChildrenCount(); ++i)
275  {
276  te::map::AbstractLayerPtr child(boost::dynamic_pointer_cast<AbstractLayer>(layer->getChild(i)));
277  e.Union(GetExtent(child, srid, onlyVisibles));
278  }
279 
280  return e;
281 }
282 
284 {
285  assert(dataset);
286 
287  if(!dataset->moveNext())
288  throw Exception(TE_TR("Could not copy the data set to memory!"));
289 
290  return new te::mem::DataSet(*dataset);
291 }
292 
294  Canvas* canvas, const te::gm::Envelope& bbox, int bboxSRID,
295  int srid, te::se::FeatureTypeStyle* style)
296 {
297  assert(type);
298  assert(type->hasGeom());
299  assert(canvas);
300  assert(bbox.isValid());
301  assert(style);
302 
303  const std::string& datasetName = type->getName();
304  assert(!datasetName.empty());
305 
306 // for while, default geometry. TODO: need a visitor to get which properties the style references
307  te::gm::GeometryProperty* geometryProperty = te::da::GetFirstGeomProperty(type);
308 
309 // create a canvas configurer
310  te::map::CanvasConfigurer cc(canvas);
311 
312 // number of rules defined on feature type style
313  std::size_t nRules = style->getRules().size();
314 
315  for(std::size_t i = 0; i < nRules; ++i) // for each <Rule>
316  {
317 // the current rule
318  const te::se::Rule* rule = style->getRule(i);
319  assert(rule);
320 
321 // TODO: should be verified the MinScaleDenominator and MaxScaleDenominator. Where will we put the current scale information? Method parameter?
322 
323 // gets the rule filter
324  const te::fe::Filter* filter = rule->getFilter();
325 
326 // let's retrieve the correct dataset
327  std::auto_ptr<te::da::DataSet> dataset(0);
328  if(!filter)
329  {
330 // there isn't a Filter expression. Gets the dataset using only box restriction...
331  dataset = ds->getDataSet(datasetName, geometryProperty->getName(), &bbox, te::gm::INTERSECTS);
332  }
333  else
334  {
335 // get an enconder
336  te::map::QueryEncoder queryConverter;
337 
338 // convert the Filter expression to a TerraLib Expression!
339  te::da::Expression* exp = queryConverter.getExpression(filter);
340  if(exp == 0)
341  throw Exception(TE_TR("Could not convert the OGC Filter expression to TerraLib expression!"));
342 
343 /* 1) Creating te::da::Where object with this expression + box restriction */
344 
345 // the box restriction
346  te::da::LiteralEnvelope* lenv = new te::da::LiteralEnvelope(bbox, srid);
347  te::da::PropertyName* geometryPropertyName = new te::da::PropertyName(geometryProperty->getName());
348  te::da::ST_Intersects* intersects = new te::da::ST_Intersects(geometryPropertyName, lenv);
349 
350 // combining the expressions (Filter expression + box restriction)
351  te::da::And* finalRestriction = new te::da::And(exp, intersects);
352 
353  te::da::Where* wh = new te::da::Where(exp);
354  wh->setExp(finalRestriction);
355 
356 // fields
357  te::da::Fields* all = new te::da::Fields;
358  all->push_back(new te::da::Field("*"));
359 
360 // from
361  te::da::FromItem* fi = new te::da::DataSetName(datasetName);
362  te::da::From* from = new te::da::From;
363  from->push_back(fi);
364 
365 // build the Select
366  te::da::Select select(all, from, wh);
367 
368 /* 2) Calling the datasource query method to get the correct restricted dataset. */
369  dataset = ds->query(select);
370  }
371 
372  if(dataset.get() == 0)
373  throw Exception((boost::format(TE_TR("Could not retrieve the data set %1%.")) % datasetName).str());
374 
375  if(dataset->moveNext() == false)
376  continue;
377 
378 // get the set of symbolizers defined on current rule
379  const std::vector<te::se::Symbolizer*>& symbolizers = rule->getSymbolizers();
380  std::size_t nSymbolizers = symbolizers.size();
381 
382 // build task message; e.g. ("Drawing the dataset Countries. Rule 1 of 3.")
383  std::string message = TE_TR("Drawing the dataset");
384  message += " " + datasetName + ". ";
385  message += TE_TR("Rule");
386  message += " " + boost::lexical_cast<std::string>(i + 1) + " " + TE_TR("of") + " ";
387  message += boost::lexical_cast<std::string>(nRules) + ".";
388 
389 // create a draw task
391  //task.setTotalSteps(nSymbolizers * dataset->size()); // Removed! The te::da::DataSet size() method would be too costly to compute.
392 
393  for(std::size_t j = 0; j < nSymbolizers; ++j) // for each <Symbolizer>
394  {
395 // the current symbolizer
396  te::se::Symbolizer* symb = symbolizers[j];
397 
398 // let's config de canvas based on the current symbolizer
399  cc.config(symb);
400 
401 // for while, first geometry. TODO: get which property the symbolizer references
402  std::size_t gpos = te::da::GetFirstPropertyPos(dataset.get(), te::dt::GEOMETRY_TYPE);
403 
404 // let's draw! for each data set geometry...
405  DrawGeometries(dataset.get(), gpos, canvas, bboxSRID, srid, &task);
406 
407 // prepare to draw the other symbolizer
408  dataset->moveFirst();
409 
410  } // end for each <Symbolizer>
411 
412  } // end for each <Rule>
413 }
414 
415 void te::map::DrawGeometries(te::da::DataSet* dataset, const std::size_t& gpos, Canvas* canvas, int fromSRID, int toSRID, te::common::TaskProgress* task)
416 {
417  assert(dataset);
418  assert(canvas);
419 
420 // verify if is necessary convert the data set geometries to the given srid
421  bool needRemap = false;
422  if((fromSRID != TE_UNKNOWN_SRS) && (toSRID != TE_UNKNOWN_SRS) && (fromSRID != toSRID))
423  needRemap = true;
424 
425  do
426  {
427  if(task)
428  {
429  if(!task->isActive())
430  return;
431 
432  // update the draw task
433  task->pulse();
434  }
435 
436  std::auto_ptr<te::gm::Geometry> geom(0);
437  try
438  {
439  geom = dataset->getGeometry(gpos);
440  if(geom.get() == 0)
441  continue;
442  }
443  catch(std::exception& /*e*/)
444  {
445  continue;
446  }
447 
448 // if necessary, geometry remap
449  if(needRemap)
450  {
451  geom->setSRID(fromSRID);
452  geom->transform(toSRID);
453  }
454 
455  canvas->draw(geom.get());
456 
457  }while(dataset->moveNext()); // next geometry!
458 }
459 
461  const te::gm::Envelope& bbox, int bboxSRID, const te::gm::Envelope& visibleArea, int srid, te::se::CoverageStyle* style)
462 {
463  assert(type);
464  assert(type->hasRaster());
465  assert(canvas);
466  assert(bbox.isValid());
467  assert(visibleArea.isValid());
468  assert(style);
469 
470  const std::string& datasetName = type->getName();
471  assert(!datasetName.empty());
472 
473 // for while, first raster property
475 
476 // retrieve the data set
477  std::auto_ptr<te::da::DataSet> dataset(ds->getDataSet(datasetName, rasterProperty->getName(), &bbox, te::gm::INTERSECTS));
478  if(dataset.get() == 0)
479  throw Exception((boost::format(TE_TR("Could not retrieve the data set %1%.")) % datasetName).str());
480 
481 // retrieve the raster
482  std::size_t rpos = te::da::GetFirstPropertyPos(dataset.get(), te::dt::RASTER_TYPE);
483  std::auto_ptr<te::rst::Raster> raster(dataset->getRaster(rpos));
484  if(dataset.get() == 0)
485  throw Exception((boost::format(TE_TR("Could not retrieve the raster from the data set %1%.")) % datasetName).str());
486 
487  DrawRaster(raster.get(), canvas, bbox, bboxSRID, visibleArea, srid, style);
488 }
489 
490 void te::map::DrawRaster(te::rst::Raster* raster, Canvas* canvas, const te::gm::Envelope& bbox, int bboxSRID,
491  const te::gm::Envelope& visibleArea, int srid, te::se::CoverageStyle* style)
492 {
493  assert(raster);
494  assert(canvas);
495  assert(bbox.isValid());
496  assert(visibleArea.isValid());
497  assert(style);
498 
499 // build the grid canvas. i.e. a grid with canvas dimensions and requested mbr
500  te::gm::Envelope* gmbr = new te::gm::Envelope(visibleArea);
501  std::auto_ptr<te::rst::Grid> gridCanvas(new te::rst::Grid(static_cast<unsigned int>(canvas->getWidth()), static_cast<unsigned int>(canvas->getHeight()), gmbr, srid));
502 
503 // compute the best level
504  double resx = visibleArea.getWidth() / static_cast<double>(canvas->getWidth());
505  double resy = visibleArea.getHeight() / static_cast<double>(canvas->getHeight());
506 
507  double rx = resx / raster->getResolutionX();
508  double ry = resy / raster->getResolutionY();
509 
510  double factor = std::min(rx, ry);
511  factor = std::max(factor, 1.0);
512 
513  std::size_t level = static_cast<std::size_t>(std::log(factor) / std::log(2.0));
514 
515 // get the raster overview
516  te::rst::Raster* overview = raster;
517  bool needDelete = false;
518  std::size_t numberOfLevels = raster->getMultiResLevelsCount();
519  if(level != 0 && numberOfLevels != 0) // need overview? has overview?
520  {
521  if(numberOfLevels >= level)
522  overview = raster->getMultiResLevel(level - 1);
523  else
524  overview = raster->getMultiResLevel(numberOfLevels - 1);
525 
526  assert(overview);
527 
528  needDelete = true;
529  }
530 
531 // create a raster transform
532  RasterTransform rasterTransform(overview, 0);
533 
534 //check band data type
535  if(raster->getBandDataType(0) != te::dt::UCHAR_TYPE)
536  {
537  // raster min / max values
540  const std::complex<double>* cmin = rsMin->at(0).m_minVal;
541  const std::complex<double>* cmax = rsMax->at(0).m_maxVal;
542  double min = cmin->real();
543  double max = cmax->real();
544 
545  // *** aqui temos a questão da variável global que diz se é para normalizar ou não os valores do raster ***
546  rasterTransform.setLinearTransfParameters(min, max, 0, 255);
547  }
548  else
549  {
550  rasterTransform.setLinearTransfParameters(0, 255, 0, 255);
551  }
552 
553 // get the raster symbolizer
554  std::size_t nRules = style->getRules().size();
555  assert(nRules >= 1);
556 
557 // for while, consider one rule
558  const te::se::Rule* rule = style->getRule(0);
559 
560  const std::vector<te::se::Symbolizer*>& symbolizers = rule->getSymbolizers();
561  assert(!symbolizers.empty());
562 
563 // for while, consider one raster symbolizer
564  te::se::RasterSymbolizer* rasterSymbolizer = dynamic_cast<te::se::RasterSymbolizer*>(symbolizers[0]);
565  assert(rasterSymbolizer);
566 
567 // configure the raster transformation based on the raster symbolizer
568  RasterTransformConfigurer rtc(rasterSymbolizer, &rasterTransform);
569  rtc.configure();
570 
571 // verify if is necessary convert the raster to the given srid
572  bool needRemap = false;
573  if((bboxSRID != TE_UNKNOWN_SRS) && (srid != TE_UNKNOWN_SRS) && (bboxSRID != srid))
574  needRemap = true;
575 
576 // build task message; e.g. ("Drawing the raster cbers_sao_jose_dos_campos.")
577  std::string message = TE_TR("Drawing raster");
578  const std::string& rasterName = raster->getName();
579  !rasterName.empty() ? message += " " + raster->getName() + ". " : message += ".";
580 
581 // create the draw task
582  te::common::TaskProgress task(message, te::common::TaskProgress::DRAW, gridCanvas->getNumberOfRows());
583 
584 // create a RGBA array that will be drawn in the canvas. i.e. This array is the result of the render process.
585  //te::color::RGBAColor** rgba = new te::color::RGBAColor*[gridCanvas->getNumberOfRows()];
586 
587 // create a RGBA array that will be drawn in the canvas. i.e. This array represents a row of the render process.
589  te::color::RGBAColor* columns = new te::color::RGBAColor[gridCanvas->getNumberOfColumns()];
590  row[0] = columns;
591 
592 // create a SRS converter
593  std::auto_ptr<te::srs::Converter> converter(new te::srs::Converter());
594 
595  if(needRemap)
596  {
597  converter->setSourceSRID(srid);
598  converter->setTargetSRID(bboxSRID);
599  }
600 
601 // fill the result RGBA array
602  for(unsigned int r = 0; r < gridCanvas->getNumberOfRows(); ++r)
603  {
604  for(unsigned int c = 0; c < gridCanvas->getNumberOfColumns(); ++c)
605  {
606  te::gm::Coord2D inputGeo = gridCanvas->gridToGeo(c, r);
607 
608  if(needRemap)
609  converter->convert(inputGeo.x, inputGeo.y, inputGeo.x, inputGeo.y);
610 
611  te::gm::Coord2D outputGrid = overview->getGrid()->geoToGrid(inputGeo.x, inputGeo.y);
612 
613 // TODO: round or truncate?
614  int x = te::rst::Round(outputGrid.x);
615  int y = te::rst::Round(outputGrid.y);
616 
617  te::color::RGBAColor color(0, 0, 0, 0);
618 
619  if((x >= 0 && x < (int)(overview->getNumberOfColumns())) &&
620  (y >= 0 && y < (int)(overview->getNumberOfRows())))
621  color = rasterTransform.apply(x, y);
622 
623  columns[c] = color;
624  }
625 
626  //rgba[r] = columns;
627 
628  if(!task.isActive())
629  {
630 // draw the part of result
631  //canvas->drawImage(0, 0, rgba, canvas->getWidth(), r + 1);
632  canvas->drawImage(0, r, row, canvas->getWidth(), 1);
633 
634 // free memory
635  //te::common::Free(rgba, r + 1);
636  te::common::Free(row, 1);
637 
638  return;
639  }
640 
641  canvas->drawImage(0, r, row, canvas->getWidth(), 1);
642 
643  task.pulse();
644  }
645 
646  if(needDelete)
647  delete overview;
648 
649 // put the result in the canvas
650  //canvas->drawImage(0, 0, rgba, canvas->getWidth(), canvas->getHeight());
651 
652 // free memory
653  //te::common::Free(rgba, gridCanvas->getNumberOfRows());
654  te::common::Free(row, 1);
655 
656 // image outline
657  if(rasterSymbolizer->getImageOutline() == 0)
658  return;
659 
660 // get the symbolizer that will be used to draw the image outline
661  te::se::Symbolizer* outlineSymbolizer = rasterSymbolizer->getImageOutline()->getSymbolizer();
662  if(outlineSymbolizer == 0)
663  return;
664 
665 // create a canvas configurer
666  te::map::CanvasConfigurer cc(canvas);
667  cc.config(outlineSymbolizer);
668 
669 // creates the image outline
670  std::auto_ptr<te::gm::Geometry> geom(te::gm::GetGeomFromEnvelope(raster->getExtent(), bboxSRID));
671  if(needRemap)
672  {
673  geom->setSRID(bboxSRID);
674  geom->transform(srid);
675  }
676 
677  canvas->draw(geom.get());
678 }
679 
680 te::rst::Raster* te::map::GetExtentRaster(te::rst::Raster* raster, int w, int h, const te::gm::Envelope& bbox, int bboxSRID,
681  const te::gm::Envelope& visibleArea, int srid)
682 {
683  assert(raster);
684  assert(bbox.isValid());
685  assert(visibleArea.isValid());
686 
687  std::vector<te::rst::BandProperty*> bprops;
688 
689  for(size_t t = 0; t < raster->getNumberOfBands(); ++t)
690  {
691  te::rst::Band* band = raster->getBand(t);
692 
693  te::rst::BandProperty* bp = new te::rst::BandProperty(t, band->getProperty()->getType(), "");
694 
695  bprops.push_back(bp);
696  }
697 
698 // build the grid canvas. i.e. a grid with canvas dimensions and requested mbr
699  te::gm::Envelope* gmbr = new te::gm::Envelope(visibleArea);
700  te::rst::Grid* gridCanvas = new te::rst::Grid(static_cast<unsigned int>(w), static_cast<unsigned int>(h), gmbr, srid);
701 
702 //build output raster
703  te::rst::Raster* rasterOut = te::rst::RasterFactory::make("MEM", gridCanvas, bprops, std::map<std::string, std::string>());
704 
705 // verify if is necessary convert the raster to the given srid
706  bool needRemap = false;
707  if((bboxSRID != TE_UNKNOWN_SRS) && (srid != TE_UNKNOWN_SRS) && (bboxSRID != srid))
708  needRemap = true;
709 
710 // create a SRS converter
711  std::auto_ptr<te::srs::Converter> converter(new te::srs::Converter());
712 
713  if(needRemap)
714  {
715  converter->setSourceSRID(srid);
716  converter->setTargetSRID(bboxSRID);
717  }
718 
719 // fill the result RGBA array
720  for(unsigned int r = 0; r < gridCanvas->getNumberOfRows(); ++r)
721  {
722  for(unsigned int c = 0; c < gridCanvas->getNumberOfColumns(); ++c)
723  {
724  te::gm::Coord2D inputGeo = gridCanvas->gridToGeo(c, r);
725 
726  if(needRemap)
727  converter->convert(inputGeo.x, inputGeo.y, inputGeo.x, inputGeo.y);
728 
729  te::gm::Coord2D outputGrid = raster->getGrid()->geoToGrid(inputGeo.x, inputGeo.y);
730 
731 // TODO: round or truncate?
732  int x = te::rst::Round(outputGrid.x);
733  int y = te::rst::Round(outputGrid.y);
734 
735  if((x >= 0 && x < (int)(raster->getNumberOfColumns())) &&
736  (y >= 0 && y < (int)(raster->getNumberOfRows())))
737  {
738  std::vector<double> values;
739  raster->getValues(x, y, values);
740  rasterOut->setValues(c, r, values);
741  }
742  }
743  }
744 
745  return rasterOut;
746 }
747 
749 {
750  assert(layer.get());
751 
752  std::auto_ptr<te::map::LayerSchema> schema(layer->getSchema());
753 
754  te::gm::GeometryProperty* geometryProperty = te::da::GetFirstGeomProperty(schema.get());
755  assert(geometryProperty);
756 
757  te::gm::GeomType gtype = geometryProperty->getGeometryType();
758 
759  switch(gtype)
760  {
766  break;
767 
768  default:
769  return gtype;
770  }
771 
772  // Here, tries fetch a geometry and retrieve its type
773 
774  std::auto_ptr<te::da::DataSet> dataset(layer->getData());
775  assert(dataset.get());
776 
777  dataset->moveNext();
778 
779  std::size_t gpos = te::da::GetFirstPropertyPos(dataset.get(), te::dt::GEOMETRY_TYPE);
780 
781  std::auto_ptr<te::gm::Geometry> g(dataset->getGeometry(gpos));
782  assert(g.get());
783 
784  return g->getGeomTypeId();
785 }
TEDATAACCESSEXPORT DataSourcePtr GetDataSource(const std::string &datasourceId, const bool opened=true)
Search for a data source with the informed id in the DataSourceManager.
Definition: Utils.cpp:262
unsigned int getNumberOfRows() const
Returns the grid number of rows.
Definition: Grid.cpp:209
virtual void setValues(unsigned int c, unsigned int r, const std::vector< double > &values)
Sets the imaginary attribute values in all complex bands of a cell.
Definition: Raster.cpp:286
te::gm::Envelope * getExtent()
Returns the geographic extension of the raster data.
Definition: Raster.cpp:104
TEDATAACCESSEXPORT te::rst::RasterProperty * GetFirstRasterProperty(const DataSetType *dt)
Definition: Utils.cpp:571
Geometric property.
GeomType
Each enumerated type is compatible with a Well-known Binary (WKB) type code.
Definition: Enums.h:41
A Raster Transform is a class that defines functions to transform a styled raster.
Utility functions for the data access module.
const std::string & getDataSetName() const
An abstract class that models a source of data in a query.
Definition: FromItem.h:50
The Field class can be used to model an expression that takes part of the output items of a SELECT...
Definition: Field.h:50
double y
y-coordinate.
Definition: Coord2D.h:114
Spatial intersects operator.
Definition: ST_Intersects.h:46
A class that models the name of a dataset used in a From clause.
Definition: DataSetName.h:43
boost::shared_ptr< DataSource > DataSourcePtr
Definition: DataSource.h:1435
bool hasGeom() const
It returns true if the DataSetType has at least one geometry property; otherwise, it returns false...
Definition: DataSetType.h:655
A raster band description.
Definition: BandProperty.h:61
double x
x-coordinate.
Definition: Coord2D.h:113
TEMAPEXPORT te::gm::Envelope GetExtent(const std::list< te::map::AbstractLayerPtr > &layers, int srid, bool onlyVisibles)
It calculates the extent of the given layers in the given SRID.
Definition: Utils.cpp:254
A class that models the name of any property of an object.
Definition: PropertyName.h:50
A class that models the description of a dataset.
Definition: DataSetType.h:72
unsigned int getNumberOfColumns() const
Returns the raster number of columns.
Definition: Raster.cpp:213
A Symbolizer describes how a feature is to appear on a map.
Definition: Symbolizer.h:80
virtual const Band * getBand(std::size_t i) const =0
Returns the raster i-th band.
TEMAPEXPORT te::rst::RasterProperty * GetRasterProperty(DataSetLayer *layer)
It gets the raster property referenced by the given data set layer.
Definition: Utils.cpp:163
A visitor that converts a OGC Filter Expression to TerraLib Expression.
virtual int getHeight() const =0
It returns the canvas height.
The CoverageStyle defines the styling that is to be applied to a subset of Coverage data...
Definition: CoverageStyle.h:45
void configure()
Configure Transformation.
virtual void getValues(unsigned int c, unsigned int r, std::vector< double > &values) const
Returns the imaginary attribute values in all complex bands of a cell.
Definition: Raster.cpp:258
This class can be used to inform the progress of a task.
Definition: TaskProgress.h:53
TEMAPEXPORT te::gm::GeomType GetGeomType(const te::map::AbstractLayerPtr &layer)
It gets the geometry type of the given layer.
Definition: Utils.cpp:748
double getWidth() const
It returns the envelope width.
Definition: Envelope.h:443
te::da::Expression * getExpression(const te::fe::Filter *f)
It converts the OGC Filter Expression to a TerraLib Expression.
virtual int getWidth() const =0
It returns the canvas width.
An utility struct for representing 2D coordinates.
Definition: Coord2D.h:40
A layer with reference to a dataset.
const te::fe::Filter * getFilter() const
Definition: Rule.cpp:97
#define TE_TR(message)
It marks a string in order to get translated.
Definition: Translator.h:347
TEMAPEXPORT void DrawGeometries(te::da::DataSetType *type, te::da::DataSourcePtr ds, Canvas *canvas, const te::gm::Envelope &bbox, int bboxSRID, int srid, te::se::FeatureTypeStyle *style)
It draws the data set geometries in the given canvas using the informed SRID and style.
Definition: Utils.cpp:293
bool isActive() const
Verify if the task is active.
Raster property.
Boolean logic operator: AND.
Definition: And.h:46
void Union(const Envelope &rhs)
It updates the envelop with coordinates of another envelope.
Definition: Envelope.h:555
void geoToGrid(const double &x, const double &y, double &col, double &row) const
Get the grid point associated to a spatial location.
Definition: Grid.cpp:307
This is an abstract class that models a query expression.
Definition: Expression.h:47
An exception class for the MapTools module.
void setLinearTransfParameters(double vmin, double vmax, double rmin, double rmax)
Set parameters of linear transformation.
Rule * getRule(std::size_t i) const
Definition: Style.cpp:105
const std::vector< Rule * > & getRules() const
Definition: Style.cpp:94
TEMAPEXPORT te::gm::Envelope GetSelectedExtent(const std::list< te::map::AbstractLayerPtr > layers, int srid, bool onlyVisibles)
It calculates the extent of selected objects of the given layers in the given SRID.
Definition: Utils.cpp:79
virtual bool moveNext()=0
It moves the internal pointer to the next item of the collection.
TEMAPEXPORT te::da::DataSet * DataSet2Memory(te::da::DataSet *dataset)
It creates a new In-Memory dataset with the items from the given dataset.
Definition: Utils.cpp:283
void Tokenize(const std::string &str, std::vector< std::string > &tokens, const std::string &delimiters=" ")
It tokenizes a given string with a delimiter of your own choice.
Definition: StringUtils.h:216
Implementation of a random-access dataset class for the TerraLib In-Memory Data Access driver...
Definition: DataSet.h:65
void Free(std::vector< T * > *v)
This function can be applied to a pointer to a vector of pointers.
Definition: STLUtils.h:131
TEMAPEXPORT void GetDashStyle(const std::string &dasharray, std::vector< double > &style)
Converts a dasharray pattern coded by a string to a vector of double.
Definition: Utils.cpp:155
static RasterSummaryManager & getInstance()
It returns a reference to the singleton instance.
TERASTEREXPORT int Round(double val)
Round a double value to a integer value.
Definition: Utils.cpp:298
An Envelope defines a 2D rectangular region.
Definition: Envelope.h:51
const std::string & getDataSourceId() const
The FeatureTypeStyle defines the styling that is to be applied to a dataset that can be viewed as a f...
#define TE_UNKNOWN_SRS
A numeric value to represent a unknown SRS identification in TerraLib.
Definition: Config.h:44
This class represents a set of unique ids created in the same context. i.e. from the same data set...
Definition: ObjectIdSet.h:55
An abstract class for raster data strucutures.
Definition: Raster.h:71
unsigned int getNumberOfRows() const
Returns the raster number of rows.
Definition: Raster.cpp:208
bool hasRaster() const
It returns true if the DataSetType has at least one raster property; otherwise, it returns false...
Definition: DataSetType.h:660
BandProperty * getProperty()
Returns the band property.
Definition: Band.cpp:428
virtual std::size_t getNumberOfBands() const =0
Returns the number of bands (dimension of cells attribute values) in the raster.
unsigned int getNumberOfColumns() const
Returns the grid number of columns.
Definition: Grid.cpp:193
double getResolutionX() const
Returns the raster horizontal (x-axis) resolution.
Definition: Raster.cpp:218
boost::ptr_vector< Field > Fields
Fields is just a boost::ptr_vector of Field pointers.
Definition: Fields.h:37
A visitor that converts a OGC Filter Expression to TerraLib Expression.
Definition: QueryEncoder.h:53
boost::ptr_vector< BandSummary > RasterSummary
RasterSummary is just a typedef of a boost::ptr_vector.
Definition: RasterSummary.h:44
A class that can be used to model a filter expression that can be applied to a query.
Definition: Where.h:47
void setExp(Expression *exp)
Sets the expression.
Definition: Where.cpp:65
void pulse()
Calls setCurrentStep() function using getCurrentStep() + 1.
The RasterSymbolizer describes how to render raster/matrix-coverage data (e.g., satellite photos...
virtual void drawImage(char *src, std::size_t size, ImageType t)=0
It draws the src image over the canvas.
A raster band description.
Definition: Band.h:63
A filter is any valid predicate expression.
Definition: Filter.h:52
Grid * getGrid()
It returns the raster grid.
Definition: Raster.cpp:94
TEMAPEXPORT te::rst::Raster * GetRaster(DataSetLayer *layer)
It gets the raster referenced by the given data set layer.
Definition: Utils.cpp:192
A Select models a query to be used when retrieving data from a DataSource.
Definition: Select.h:65
virtual std::auto_ptr< te::gm::Geometry > getGeometry(std::size_t i) const =0
Method for retrieving a geometric attribute value.
const std::vector< Symbolizer * > & getSymbolizers() const
Definition: Rule.cpp:158
boost::ptr_vector< FromItem > From
It models the FROM clause for a query.
Definition: From.h:37
A Converter is responsible for the conversion of coordinates between different Coordinate Systems (CS...
Definition: Converter.h:53
void apply(double icol, double ilin, double ocol, double olin)
A dataset is the unit of information manipulated by the data access module of TerraLib.
Definition: DataSet.h:112
A canvas is an abstraction of a drawing area.
Definition: Canvas.h:91
virtual Raster * getMultiResLevel(const unsigned int level) const =0
Returns the required level of a multi-resolution pyramid or NULL if that level does not exists...
const std::string & getName() const
Returns the raster name.
Definition: Raster.cpp:79
A Rule is used to attach property/scale conditions to and group the individual symbols used for rende...
Definition: Rule.h:78
A Raster Transform configurer generates a Raster Transform given a RasterSymbolzier.
std::auto_ptr< te::gm::Envelope > GetExtent(te::da::DataSet *dset, te::qt::widgets::Promoter *p, const int &rowPosition)
static Raster * make()
It creates and returns an empty raster with default raster driver.
A helper class for 32-bit RGBA (Red-Green-Blue-Alpha channel) color.
Definition: RGBAColor.h:57
TEMAPEXPORT te::rst::Raster * GetExtentRaster(te::rst::Raster *raster, int w, int h, const te::gm::Envelope &bbox, int bboxSRID, const te::gm::Envelope &visibleArea, int srid)
Definition: Utils.cpp:680
A class that models a literal for Envelope values.
void gridToGeo(const double &col, const double &row, double &x, double &y) const
Get the spatial location of a grid point.
Definition: Grid.cpp:301
double getResolutionY() const
Returns the raster vertical (y-axis) resolution.
Definition: Raster.cpp:223
A Raster Transform configurer generates a Raster Transform given a RasterSymbolzier.
int getType() const
It returns the data type of the elements in the band.
Definition: BandProperty.h:113
A layer with reference to a dataset.
Definition: DataSetLayer.h:47
A Raster Transform is a class that defines functions to transform a styled raster.
TEDATAACCESSEXPORT std::size_t GetFirstPropertyPos(const te::da::DataSet *dataset, int datatype)
Definition: Utils.cpp:481
void config(const te::se::Symbolizer *symbolizer)
It configs the canvas based on given symbolizer.
Visibility
Each layer can have three states of visibility.
Definition: Enums.h:138
Calculate the min value.
Definition: Enums.h:40
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
Calculate the max value.
Definition: Enums.h:41
boost::intrusive_ptr< AbstractLayer > AbstractLayerPtr
void transform(int oldsrid, int newsrid)
It will transform the coordinates of the Envelope from the old SRS to the new one.
Definition: Envelope.cpp:92
virtual int getBandDataType(std::size_t i) const =0
Returns the data type in a particular band (or dimension).
virtual void draw(const te::gm::Geometry *geom)=0
It draws the geometry on canvas.
double getHeight() const
It returns the envelope height.
Definition: Envelope.h:448
virtual unsigned int getMultiResLevelsCount() const =0
Returns the current number of multi-resolution pyramid levels.
bool isValid() const
It tells if the rectangle is valid or not.
Definition: Envelope.h:438
TEMAPEXPORT void DrawRaster(te::da::DataSetType *type, te::da::DataSourcePtr ds, Canvas *canvas, const te::gm::Envelope &bbox, int bboxSRID, const te::gm::Envelope &visibleArea, int srid, te::se::CoverageStyle *style)
Definition: Utils.cpp:460
TEGEOMEXPORT Geometry * GetGeomFromEnvelope(const Envelope *const e, int srid)
It creates a Geometry (a polygon) from the given envelope.
Definition: Utils.cpp:38
A Symbology Enconding visitor that configures a given canvas based on symbolizers elements...
TEMAPEXPORT void GetVisibleLayers(const std::list< te::map::AbstractLayerPtr > &layers, std::list< te::map::AbstractLayerPtr > &visibleLayers)
It gets the visible layers of the given layer list.
Definition: Utils.cpp:248
const std::string & getName() const
It returns the property name.
Definition: Property.h:127
A Symbology Enconding visitor that configures a given canvas based on symbolizers elements...