19 #ifndef __otbVectorDataToImageFilter_txx
20 #define __otbVectorDataToImageFilter_txx
29 #include <mapnik/datasource_cache.hpp>
30 #include <mapnik/font_engine_freetype.hpp>
31 #include <mapnik/agg_renderer.hpp>
32 #include <mapnik/filter_factory.hpp>
33 #include <mapnik/color_factory.hpp>
34 #include <mapnik/image_util.hpp>
35 #include <mapnik/config_error.hpp>
36 #include <mapnik/memory_datasource.hpp>
37 #include <mapnik/layer.hpp>
38 #include <mapnik/map.hpp>
39 #include <mapnik/value.hpp>
41 #include "ogr_spatialref.h"
43 #ifdef OTB_MAPNIK_COMPATIBILITY_API07
46 typedef Image32 image_32;
56 #ifdef OTB_MAPNIK_COMPATIBILITY_API07
57 static size_t get_num_layer(
const mapnik::Map& map) {
return map.layerCount(); }
58 static unsigned get_height(
const mapnik::Map& map) {
return map.getHeight(); }
59 static unsigned get_width(
const mapnik::Map& map) {
return map.getWidth(); }
60 typedef mapnik::Envelope<double>
box2d;
61 static void zoom_to_box(mapnik::Map* map,
const mapnik::Envelope<double>& envelope)
63 map->zoomToBox(envelope);
65 typedef mapnik::geometry2d
geom;
72 g =
new mapnik::point<mapnik::vertex<double, 2> >;
73 case mapnik::LineString:
74 g =
new mapnik::line_string<mapnik::vertex<double, 2> , mapnik::vertex_vector2>;
76 g =
new mapnik::polygon<mapnik::vertex<double, 2>, mapnik::vertex_vector2>;
79 <<
"Please fix otb::mapnik_otb::create_geom for mapnik 0.7" << std::endl;
84 static size_t get_num_layer(
const mapnik::Map& map) {
return map.layer_count(); }
85 static unsigned get_height(
const mapnik::Map& map) {
return map.height(); }
86 static unsigned get_width(
const mapnik::Map& map) {
return map.width(); }
87 typedef mapnik::box2d<double>
box2d;
88 static void zoom_to_box(mapnik::Map* map,
const mapnik::box2d<double>& envelope)
90 map->zoom_to_box(envelope);
93 typedef mapnik::geometry<vertex2d>
geom;
101 template <
class TVectorData,
class TImage>
105 m_UseAsOverlay(true),
106 m_RenderingStyleType(
OSM)
108 this->SetNumberOfRequiredInputs(1);
111 m_Direction.SetIdentity();
113 m_StartIndex.Fill(0);
114 m_SensorModelFlip = 1;
116 m_VectorDataProjectionProj4 =
"";
117 m_VectorDataProjectionWKT =
"";
120 template <
class TVectorData,
class TImage>
127 const_cast<VectorDataType *>(input));
130 template <
class TVectorData,
class TImage>
137 const_cast<VectorDataType *>(input));
140 template <
class TVectorData,
class TImage>
145 if (this->GetNumberOfInputs() < 1)
150 return static_cast<const TVectorData *
>
154 template <
class TVectorData,
class TImage>
159 return static_cast<const TVectorData *
>
164 template <
class TVectorData,
class TImage>
169 if (this->m_Spacing != spacing)
171 this->m_Spacing = spacing;
177 template <
class TVectorData,
class TImage>
187 template <
class TVectorData,
class TImage>
199 template <
class TVectorData,
class TImage>
209 template <
class TVectorData,
class TImage>
223 template <
class TVectorData,
class TImage>
238 typename TImage::RegionType outputLargestPossibleRegion;
239 outputLargestPossibleRegion.SetSize(m_Size);
240 outputLargestPossibleRegion.SetIndex(m_StartIndex);
241 outputPtr->SetLargestPossibleRegion(outputLargestPossibleRegion);
244 outputPtr->SetSpacing(m_Spacing);
245 outputPtr->SetOrigin(m_Origin);
246 outputPtr->SetDirection(m_Direction);
250 static_cast<std::string
>(m_VectorDataProjectionWKT));
260 template <
class TVectorData,
class TImage>
266 Superclass::BeforeThreadedGenerateData();
269 if(!m_FontFileName.empty())
270 mapnik::freetype_engine::register_font(m_FontFileName);
274 styleLoader->SetScaleFactor(m_ScaleFactor);
279 itk::ExposeMetaData<std::string>(
283 m_SensorModelFlip = 1;
285 if (m_VectorDataProjectionWKT ==
"")
290 m_VectorDataProjectionProj4 =
"+proj=utm +zone=31 +ellps=WGS84";
291 m_SensorModelFlip = -1;
296 OGRSpatialReferenceH oSRS = OSRNewSpatialReference(m_VectorDataProjectionWKT.c_str());
298 OSRExportToProj4(oSRS, &pszProj4);
299 m_VectorDataProjectionProj4 = pszProj4;
302 m_SensorModelFlip = 1;
310 RegionType requestedRegion = output->GetRequestedRegion();
313 m_NbTile = (vcl_pow(std::max(vcl_floor((
double)requestedRegion.GetSize()[0] / 16000),
314 vcl_floor((
double)requestedRegion.GetSize()[1] / 16000))+1, 2));
319 m_TilingRegions.resize(m_NbTile);
320 m_Maps.resize(m_NbTile);
321 m_VectorDataExtractors.resize(m_NbTile);
323 unsigned int tilingRegionsIdx = 0;
324 unsigned int stdXOffset;
325 unsigned int stdYOffset;
327 stdXOffset = vcl_floor((
double)requestedRegion.GetSize()[0]/ (m_NbTile/2))+1;
328 stdYOffset = vcl_floor((
double)requestedRegion.GetSize()[1]/ (m_NbTile/2))+1;
330 for(
unsigned int i=0; i < vcl_floor((
double)(m_NbTile)/2 + 0.5); ++i)
332 for(
unsigned int j=0; j < vcl_floor((
double)(m_NbTile)/2 + 0.5); ++j)
339 index = requestedRegion.GetIndex();
340 size = requestedRegion.
GetSize();
344 index[0] = requestedRegion.GetIndex()[0] + i * stdXOffset;
345 index[1] = requestedRegion.GetIndex()[1] + j * stdYOffset;
347 size[0] = std::min((
unsigned int)(requestedRegion.GetSize()[0] - index[0]), stdXOffset);
348 size[1] = std::min((
unsigned int)(requestedRegion.GetSize()[1] - index[1]), stdYOffset);
350 m_TilingRegions[tilingRegionsIdx].SetIndex(index);
351 m_TilingRegions[tilingRegionsIdx].SetSize(size);
357 m_Maps[tilingRegionsIdx] = mapnik::Map();
360 switch (m_RenderingStyleType)
364 styleLoader->LoadOSMStyle(m_Maps[tilingRegionsIdx]);
368 m_Maps[tilingRegionsIdx].set_background(mapnik::color(255, 255, 255, 0));
372 m_Maps[tilingRegionsIdx].set_background(mapnik::color(
"#b5d0d0"));
378 styleLoader->LoadBinaryRasterizationStyle(m_Maps[tilingRegionsIdx]);
380 m_Maps[tilingRegionsIdx].set_background(mapnik::color(
"#ffffff"));
385 itkExceptionMacro(<<
"Style Type Not Supported!");
391 m_Maps[tilingRegionsIdx].set_srs(m_VectorDataProjectionProj4);
394 m_VectorDataExtractors[tilingRegionsIdx].resize(this->GetNumberOfInputs());
395 for (
unsigned int idx = 0; idx < this->GetNumberOfInputs(); ++idx)
397 if (this->GetInput(idx))
401 sizePhy[0] = size[0] * m_Spacing[0];
402 sizePhy[1] = size[1] * m_Spacing[1];
405 origin[0] = m_Origin[0] + index[0] * m_Spacing[0];
406 origin[1] = m_Origin[1] + index[1] * m_Spacing[1];
414 m_VectorDataExtractors[tilingRegionsIdx][idx] = VectorDataExtractROIType::New();
415 m_VectorDataExtractors[tilingRegionsIdx][idx]->SetRegion(rsRegion);
416 m_VectorDataExtractors[tilingRegionsIdx][idx]->SetInput(this->GetInput(idx));
428 template <
class TVectorData,
class TImage>
434 this->AllocateOutputs();
436 this->BeforeThreadedGenerateData();
438 if (m_StyleList.size() == 0)
440 switch (m_RenderingStyleType)
445 itkExceptionMacro(<<
"No style is provided for the vector data");
451 this->AddStyle(
"binary-rasterization");
456 itkExceptionMacro(<<
"No style is provided for the vector data");
464 for (
unsigned int tileIdx = 0; tileIdx < m_NbTile; ++tileIdx)
468 for (
int i = numberLayer - 1; i >= 0; i--)
470 m_Maps[tileIdx].removeLayer(i);
472 m_Maps[tileIdx].resize(m_TilingRegions[tileIdx].GetSize()[0], m_TilingRegions[tileIdx].GetSize()[1]);
474 for (
unsigned int vdIdx = 0; vdIdx < this->GetNumberOfInputs(); ++vdIdx)
476 if (this->GetInput(vdIdx))
480 m_VectorDataExtractors[tileIdx][vdIdx]->Update();
484 ProcessNode(inputRoot, mDatasource);
487 std::stringstream layerName;
488 layerName <<
"layer-" << tileIdx;
489 mapnik::layer lyr(layerName.str());
490 lyr.set_srs(m_VectorDataProjectionProj4);
491 lyr.set_datasource(mDatasource);
493 for (
unsigned int i = 0; i < m_StyleList.size(); ++i)
495 lyr.add_style(m_StyleList[i]);
498 m_Maps[tileIdx].addLayer(lyr);
501 assert((m_SensorModelFlip == 1) || (m_SensorModelFlip == -1));
504 m_Origin[0] + m_TilingRegions[tileIdx].GetIndex()[0]*m_Spacing[0],
505 m_SensorModelFlip*(m_Origin[1] + m_TilingRegions[tileIdx].GetIndex()[1] * m_Spacing[1]
506 + m_TilingRegions[tileIdx].GetSize()[1] * m_Spacing[1]),
507 m_Origin[0] + m_TilingRegions[tileIdx].GetIndex()[0] * m_Spacing[0]
508 + m_TilingRegions[tileIdx].GetSize()[0] * m_Spacing[0],
509 m_SensorModelFlip*(m_Origin[1] + m_TilingRegions[tileIdx].GetIndex()[1] * m_Spacing[1])
518 mapnik::agg_renderer<mapnik::image_32> ren(m_Maps[tileIdx], buf);
521 const unsigned char * src = buf.raw_data();
534 it.
Set(m_RGBAConverter->Convert(pix));
539 template <
class TVectorData,
class TImage>
544 typedef typename VectorDataType::DataNodeType DataNodeType;
545 typedef typename DataNodeType::Pointer DataNodePointerType;
551 for (
typename ChildrenListType::iterator it = children.begin(); it != children.end(); ++it)
554 DataNodePointerType dataNode = (*it)->Get();
556 switch (dataNode->GetNodeType())
560 ProcessNode((*it), mDatasource);
565 ProcessNode((*it), mDatasource);
570 ProcessNode((*it), mDatasource);
577 point->move_to(dataNode->GetPoint()[0], m_SensorModelFlip * dataNode->GetPoint()[1]);
580 typedef boost::shared_ptr<mapnik::raster> raster_ptr;
581 typedef mapnik::feature<mapnik_otb::geom, raster_ptr> Feature;
582 typedef boost::shared_ptr<Feature> feature_ptr;
584 feature_ptr mfeature = feature_ptr(
new Feature(1));
585 mfeature->add_geometry(point);
587 mapnik::transcoder tr(
"ISO-8859-15");
589 if (dataNode->HasField(
"place_name"))
590 boost::put(*mfeature,
"name",
591 tr.transcode((dataNode->GetFieldAsString(
"place_name")).c_str()));
593 boost::put(*mfeature,
"place", tr.transcode(
"city"));
594 boost::put(*mfeature,
"capital", tr.transcode(
"yes"));
596 boost::put(*mfeature,
"geometry", tr.transcode(
"point"));
598 mDatasource->push(mfeature);
606 typedef typename DataNodeType::LineType::VertexListConstIteratorType VertexIterator;
607 VertexIterator itVertex = dataNode->GetLine()->GetVertexList()->Begin();
608 while (itVertex != dataNode->GetLine()->GetVertexList()->End())
611 line->line_to(itVertex.Value()[0], m_SensorModelFlip * itVertex.Value()[1]);
617 typedef boost::shared_ptr<mapnik::raster> raster_ptr;
618 typedef mapnik::feature<mapnik_otb::geom, raster_ptr> Feature;
619 typedef boost::shared_ptr<Feature> feature_ptr;
621 feature_ptr mfeature = feature_ptr(
new Feature(1));
622 mfeature->add_geometry(line);
624 mapnik::transcoder tr(
"ISO-8859-15");
626 if (dataNode->HasField(
"name"))
627 boost::put(*mfeature,
"name",
628 tr.transcode((dataNode->GetFieldAsString(
"name")).c_str()));
629 if (dataNode->HasField(
"NAME"))
630 boost::put(*mfeature,
"name",
631 tr.transcode((dataNode->GetFieldAsString(
"NAME")).c_str()));
640 if (dataNode->HasField(
"type"))
641 boost::put(*mfeature,
"highway",
642 tr.transcode((dataNode->GetFieldAsString(
"type")).c_str()));
643 if (dataNode->HasField(
"TYPE"))
644 boost::put(*mfeature,
"highway",
645 tr.transcode((dataNode->GetFieldAsString(
"TYPE")).c_str()));
647 boost::put(*mfeature,
"geometry", tr.transcode(
"line"));
649 mDatasource->push(mfeature);
657 typedef typename DataNodeType::PolygonType::VertexListConstIteratorType VertexIterator;
658 VertexIterator itVertex = dataNode->GetPolygonExteriorRing()->GetVertexList()->Begin();
659 while (itVertex != dataNode->GetPolygonExteriorRing()->GetVertexList()->End())
661 polygon->line_to(itVertex.Value()[0], m_SensorModelFlip * itVertex.Value()[1]);
665 typedef boost::shared_ptr<mapnik::raster> raster_ptr;
666 typedef mapnik::feature<mapnik_otb::geom, raster_ptr> Feature;
667 typedef boost::shared_ptr<Feature> feature_ptr;
669 feature_ptr mfeature = feature_ptr(
new Feature(1));
670 mfeature->add_geometry(polygon);
672 mapnik::transcoder tr(
"ISO-8859-15");
674 boost::put(*mfeature,
"geometry", tr.transcode(
"polygon"));
676 mDatasource->push(mfeature);
683 <<
"This type (FEATURE_MULTIPOINT) is not handle (yet) by VectorDataToImageFilter(), please request for it");
689 <<
"This type (FEATURE_MULTILINE) is not handle (yet) by VectorDataToImageFilter(), please request for it");
695 <<
"This type (FEATURE_MULTIPOLYGON) is not handle (yet) by VectorDataToImageFilter(), please request for it");
701 <<
"This type (FEATURE_COLLECTION) is not handle (yet) by VectorDataToImageFilter(), please request for it");
711 template <
class TVectorData,
class TImage>
716 Superclass::PrintSelf(os, indent);