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