1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560
|
/*
* Copyright (C) 2005-2020 Centre National d'Etudes Spatiales (CNES)
*
* This file is part of Orfeo Toolbox
*
* https://www.orfeo-toolbox.org/
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef otbMapFileProductWriter_hxx
#define otbMapFileProductWriter_hxx
#include "otbMapFileProductWriter.h"
#include "itksys/SystemTools.hxx"
#include "otbSpatialReference.h"
namespace otb
{
/**
* Constructor
*/
template <class TInputImage>
MapFileProductWriter<TInputImage>::MapFileProductWriter()
{
m_GenericRSResampler = GenericRSResamplerType::New();
m_TileSize = 256;
m_CurrentDepth = 0;
m_SRID = 26918;
// Modify superclass default values, can be overridden by subclasses
this->SetNumberOfRequiredInputs(1);
}
/**
* Desctructor
*/
template <class TInputImage>
MapFileProductWriter<TInputImage>::~MapFileProductWriter()
{
}
/**
*
*/
template <class TInputImage>
void MapFileProductWriter<TInputImage>::SetInput(const InputImageType* input)
{
// Process object is not const-correct so the const_cast is required here
this->ProcessObject::SetNthInput(0, const_cast<InputImageType*>(input));
}
template <class TInputImage>
void MapFileProductWriter<TInputImage>::SetInput(unsigned int index, const TInputImage* image)
{
// Process object is not const-correct so the const_cast is required here
this->ProcessObject::SetNthInput(index, const_cast<TInputImage*>(image));
}
/**
*
*/
template <class TInputImage>
const typename MapFileProductWriter<TInputImage>::InputImageType* MapFileProductWriter<TInputImage>::GetInput(void)
{
if (this->GetNumberOfInputs() < 1)
{
return nullptr;
}
return static_cast<const TInputImage*>(this->ProcessObject::GetInput(0));
}
/**
* Get the idx input
*/
template <class TInputImage>
const typename MapFileProductWriter<TInputImage>::InputImageType* MapFileProductWriter<TInputImage>::GetInput(unsigned int idx)
{
return static_cast<const TInputImage*>(this->ProcessObject::GetInput(idx));
}
/**
* Write launch the tiling and the mapFile generation and write on the
* disk the indexfile as a shapefile
*/
template <class TInputImage>
void MapFileProductWriter<TInputImage>::Write()
{
// Get the input Image
m_VectorImage = const_cast<TInputImage*>(this->GetInput());
m_VectorImage->UpdateOutputInformation();
if (m_VectorImage->GetProjectionRef().empty() && m_VectorImage->GetImageKeywordlist().GetSize() > 0)
{
otbMsgDevMacro("Sensor Model detected : Reprojecting in the targer SRID");
m_GenericRSResampler->SetInput(this->GetInput());
m_GenericRSResampler->SetOutputParametersFromMap(otb::SpatialReference::FromEPSG(m_SRID).ToWkt());
m_VectorImage = m_GenericRSResampler->GetOutput();
m_VectorImage->UpdateOutputInformation();
}
// Initialize the filename, the vectordatas
this->Initialize();
// Generate the mapFile
this->GenerateMapFile();
// Do the tiling
this->Tiling();
}
/**
* Initialize the path, the filename, the vectordata used to store
* each bounding box of a tile as a feature
*/
template <class TInputImage>
void MapFileProductWriter<TInputImage>::Initialize()
{
// check that the right extension is given : expected .map
if (itksys::SystemTools::GetFilenameLastExtension(m_FileName) != ".map")
{
itkExceptionMacro(<< itksys::SystemTools::GetFilenameLastExtension(m_FileName) << " is a wrong Extension FileName : Expected .map");
}
// Initialize the index vectordata
this->InitializeVectorData();
}
/**
* Initialize the vectordata to write
*
*/
template <class TInputImage>
void MapFileProductWriter<TInputImage>::InitializeVectorData()
{
// Intitialize the vectordata to build the indexTile
m_VectorDataIndexTile = VectorDataType::New();
m_VectorDataIndexTile->SetProjectionRef(otb::SpatialReference::FromEPSG(m_SRID).ToWkt());
DataNodeType::Pointer root = m_VectorDataIndexTile->GetDataTree()->GetRoot()->Get();
DataNodeType::Pointer document = DataNodeType::New();
m_Folder = DataNodeType::New();
document->SetNodeType(otb::DOCUMENT);
m_Folder->SetNodeType(otb::FOLDER);
document->SetNodeId("DOCUMENT");
m_Folder->SetNodeId("FOLDER");
m_VectorDataIndexTile->GetDataTree()->Add(document, root);
m_VectorDataIndexTile->GetDataTree()->Add(m_Folder, document);
}
/**
* Do the tiling
*/
template <class TInputImage>
void MapFileProductWriter<TInputImage>::Tiling()
{
unsigned int numberOfChannel = m_VectorImage->GetNumberOfComponentsPerPixel();
/** Image statistics*/
typename InputImageType::PixelType inMin(numberOfChannel), inMax(numberOfChannel), outMin(numberOfChannel), outMax(numberOfChannel);
outMin.Fill(0);
outMax.Fill(255);
// Update image base information
m_VectorImage->UpdateOutputInformation();
// Get the image size
SizeType size;
size = m_VectorImage->GetLargestPossibleRegion().GetSize();
unsigned int sizeX = size[0];
unsigned int sizeY = size[1];
// Compute max depth
unsigned int maxDepth = static_cast<unsigned int>(std::max(std::ceil(std::log(static_cast<float>(sizeX) / static_cast<float>(m_TileSize)) / std::log(2.0)),
std::ceil(std::log(static_cast<float>(sizeY) / static_cast<float>(m_TileSize)) / std::log(2.0))));
// Extract size & index
SizeType extractSize;
IndexType extractIndex;
for (unsigned int depth = 0; depth <= maxDepth; depth++)
{
// update the attribute value Current Depth
m_CurrentDepth = depth;
// Compose the name of the vectordata
// the index shapefile filename
std::ostringstream tempIndexShapeName;
tempIndexShapeName << m_ShapeIndexPath << "/";
tempIndexShapeName << itksys::SystemTools::GetFilenameWithoutExtension(m_FileName) << "_" << m_CurrentDepth;
tempIndexShapeName << "_index.shp";
m_IndexShapeFileName = tempIndexShapeName.str();
// Resample image to the max Depth
int sampleRatioValue = 1 << (maxDepth - depth); // 2^(maxDepth - depth)
if (sampleRatioValue > 1)
{
m_StreamingShrinkImageFilter = StreamingShrinkImageFilterType::New();
m_StreamingShrinkImageFilter->SetShrinkFactor(sampleRatioValue);
m_StreamingShrinkImageFilter->SetInput(m_VectorImage);
m_StreamingShrinkImageFilter->GetStreamer()->SetAutomaticStrippedStreaming(0);
m_StreamingShrinkImageFilter->Update();
m_VectorRescaleIntensityImageFilter = VectorRescaleIntensityImageFilterType::New();
m_VectorRescaleIntensityImageFilter->SetInput(m_StreamingShrinkImageFilter->GetOutput());
m_VectorRescaleIntensityImageFilter->SetOutputMinimum(outMin);
m_VectorRescaleIntensityImageFilter->SetOutputMaximum(outMax);
if (depth == 0)
{
m_VectorRescaleIntensityImageFilter->Update();
inMin = m_VectorRescaleIntensityImageFilter->GetInputMinimum();
inMax = m_VectorRescaleIntensityImageFilter->GetInputMaximum();
}
else
{
m_VectorRescaleIntensityImageFilter->SetInputMinimum(inMin);
m_VectorRescaleIntensityImageFilter->SetInputMaximum(inMax);
m_VectorRescaleIntensityImageFilter->SetAutomaticInputMinMaxComputation(false);
}
// New resample vector image
m_ResampleVectorImage = m_VectorRescaleIntensityImageFilter->GetOutput();
}
else
{
m_VectorRescaleIntensityImageFilter = VectorRescaleIntensityImageFilterType::New();
m_VectorRescaleIntensityImageFilter->SetInput(m_VectorImage);
m_VectorRescaleIntensityImageFilter->SetOutputMinimum(outMin);
m_VectorRescaleIntensityImageFilter->SetOutputMaximum(outMax);
m_VectorRescaleIntensityImageFilter->SetInputMinimum(inMin);
m_VectorRescaleIntensityImageFilter->SetInputMaximum(inMax);
m_VectorRescaleIntensityImageFilter->SetAutomaticInputMinMaxComputation(false);
m_ResampleVectorImage = m_VectorRescaleIntensityImageFilter->GetOutput();
}
m_ResampleVectorImage->UpdateOutputInformation();
// Get the image size
size = m_ResampleVectorImage->GetLargestPossibleRegion().GetSize();
sizeX = size[0];
sizeY = size[1];
unsigned int x = 0;
unsigned int y = 0;
// Create directory where to store generated tiles
// Do it once here outside of the loop
std::ostringstream path;
path << m_ShapeIndexPath << "/tiles";
if (!itksys::SystemTools::MakeDirectory(path.str()))
{
itkExceptionMacro(<< "Error while creating cache directory" << path.str());
}
// Tiling resample image
for (unsigned int tx = 0; tx < sizeX; tx += m_TileSize)
{
for (unsigned int ty = 0; ty < sizeY; ty += m_TileSize)
{
if ((tx + m_TileSize) >= sizeX)
{
extractIndex[0] = tx;
extractSize[0] = sizeX - tx;
}
else
{
extractIndex[0] = tx;
extractSize[0] = m_TileSize;
}
if ((ty + m_TileSize) >= sizeY)
{
extractIndex[1] = ty;
extractSize[1] = sizeY - ty;
}
else
{
extractIndex[1] = ty;
extractSize[1] = m_TileSize;
}
// Generate Tile filename
std::ostringstream ossFileName;
ossFileName << path.str() << "/";
ossFileName << "tile_" << m_CurrentDepth << "_";
ossFileName << x << "_" << y << ".tif";
// Extract ROI
m_VectorImageExtractROIFilter = VectorImageExtractROIFilterType::New();
// Set extract roi parameters
m_VectorImageExtractROIFilter->SetStartX(extractIndex[0]);
m_VectorImageExtractROIFilter->SetStartY(extractIndex[1]);
m_VectorImageExtractROIFilter->SetSizeX(extractSize[0]);
m_VectorImageExtractROIFilter->SetSizeY(extractSize[1]);
// Set Channel to extract
m_VectorImageExtractROIFilter->SetChannel(1);
m_VectorImageExtractROIFilter->SetChannel(2);
m_VectorImageExtractROIFilter->SetChannel(3);
// Set extract roi input
m_VectorImageExtractROIFilter->SetInput(m_ResampleVectorImage);
// Configure writer
m_VectorWriter = VectorWriterType::New();
m_VectorWriter->SetFileName(ossFileName.str());
m_VectorWriter->SetInput(m_VectorImageExtractROIFilter->GetOutput());
m_VectorWriter->Update();
/** TODO : Generate KML for this tile */
// Search Lat/Lon box
// Initialize the transform to be used
// typename TransformType::Pointer transform =
// TransformType::New();
m_Transform = TransformType::New();
m_Transform->SetInputProjectionRef(m_GenericRSResampler->GetOutputProjectionRef());
m_Transform->SetOutputProjectionRef(otb::SpatialReference::FromEPSG(m_SRID).ToWkt());
m_Transform->InstantiateTransform();
InputPointType inputPoint;
SizeType sizeTile;
sizeTile = extractSize;
/** GX LAT LON **/
// Compute upper left corner
itk::ContinuousIndex<double, 2> indexTile(extractIndex);
indexTile[0] += -0.5;
indexTile[1] += -0.5;
m_ResampleVectorImage->TransformContinuousIndexToPhysicalPoint(indexTile, inputPoint);
OutputPointType upperLeftCorner = m_Transform->TransformPoint(inputPoint);
// std::cout <<"indexTile "<< indexTile <<" --> input Point "<< inputPoint << " upperLeftCorner "<< upperLeftCorner << std::endl;
// Compute lower left corner
indexTile[1] += static_cast<double>(sizeTile[1]);
m_ResampleVectorImage->TransformContinuousIndexToPhysicalPoint(indexTile, inputPoint);
OutputPointType lowerLeftCorner = m_Transform->TransformPoint(inputPoint);
// std::cout <<"indexTile "<< indexTile <<" --> input Point "<< inputPoint << " lowerLeftCorner "<< lowerLeftCorner << std::endl;
// Compute lower right corner
indexTile[0] += static_cast<double>(sizeTile[0]);
m_ResampleVectorImage->TransformContinuousIndexToPhysicalPoint(indexTile, inputPoint);
OutputPointType lowerRightCorner = m_Transform->TransformPoint(inputPoint);
// std::cout <<"indexTile "<< indexTile <<" --> input Point "<< inputPoint << " lowerRightCorner "<< lowerRightCorner << std::endl;
// Compute upper right corner
indexTile[1] -= static_cast<double>(sizeTile[1]);
m_ResampleVectorImage->TransformContinuousIndexToPhysicalPoint(indexTile, inputPoint);
OutputPointType upperRightCorner = m_Transform->TransformPoint(inputPoint);
// std::cout <<"indexTile "<< indexTile <<" --> input Point "<< inputPoint << " upperRightCorner "<< upperRightCorner << std::endl;
// Build The indexTile
this->AddBBoxToIndexTile(lowerLeftCorner, lowerRightCorner, upperRightCorner, upperLeftCorner, x, y);
/** END GX LAT LON */
y++;
}
x++;
y = 0;
}
// Add the layer in the mapfile
this->AddLayer();
// Write the IndexTile
typename VectorDataFileWriterType::Pointer writer = VectorDataFileWriterType::New();
writer->SetFileName(m_IndexShapeFileName);
writer->SetInput(m_VectorDataIndexTile);
writer->Update();
this->InitializeVectorData();
}
// Close the file
m_File << "END" << std::endl;
m_File.close();
}
/**
* Add the bounding box and the location of the generated tile as
* field of the vectordata
*/
template <class TInputImage>
void MapFileProductWriter<TInputImage>::AddBBoxToIndexTile(OutputPointType lowerLeftCorner, OutputPointType lowerRightCorner, OutputPointType upperRightCorner,
OutputPointType upperLeftCorner, unsigned int x, unsigned int y)
{
// From PointType to VertexType
VertexType pLL, pLR, pUR, pUL;
pLL[0] = lowerLeftCorner[0];
pLL[1] = lowerLeftCorner[1];
pLR[0] = lowerRightCorner[0];
pLR[1] = lowerRightCorner[1];
pUR[0] = upperRightCorner[0];
pUR[1] = upperRightCorner[1];
pUL[0] = upperLeftCorner[0];
pUL[1] = upperLeftCorner[1];
// Form a polygon with the vertices
PolygonType::Pointer poly = PolygonType::New();
poly->AddVertex(pLL);
poly->AddVertex(pLR);
poly->AddVertex(pUR);
poly->AddVertex(pUL);
// Add the polygon to the datanode
m_Polygon = DataNodeType::New();
m_Polygon->SetNodeId("FEATURE_POLYGON");
m_Polygon->SetNodeType(otb::FEATURE_POLYGON);
m_Polygon->SetPolygonExteriorRing(poly);
std::ostringstream oss;
oss << "tiles/tile_";
oss << m_CurrentDepth << "_" << x << "_" << y << ".tif";
// Add the field "LOCATION" used in mapserver clients
// to get the path to tiles
m_Polygon->SetFieldAsString("LOCATION", oss.str());
// Add the to vectordata
m_VectorDataIndexTile->GetDataTree()->Add(m_Polygon, m_Folder);
}
/**
* Write the mapFile on the disk
*/
template <class TInputImage>
void MapFileProductWriter<TInputImage>::GenerateMapFile()
{
// create the directory where to store the filemap
// if it does not exists
std::ostringstream path;
path << itksys::SystemTools::GetFilenamePath(m_FileName);
if (!itksys::SystemTools::MakeDirectory(path.str()))
{
itkExceptionMacro(<< "Error while creating cache directory" << path.str());
}
// Create a mapfile
m_File.open(m_FileName);
m_File << std::fixed << std::setprecision(6);
// Get the name of the layer
std::ostringstream tempIndexShapeName;
tempIndexShapeName << itksys::SystemTools::GetFilenameWithoutExtension(m_FileName);
m_File << "MAP" << std::endl;
m_File << "\tNAME " << tempIndexShapeName.str() << std::endl;
m_File << "\t# Map image size" << std::endl;
m_File << "\tSIZE " << m_TileSize << " " << m_TileSize << std::endl;
m_File << "\tUNITS dd" << std::endl;
m_File << "\tSHAPEPATH '" << m_ShapeIndexPath << "'" << std::endl;
m_File << "\tEXTENT -180 -90 180 90" << std::endl; // TODO : get the
// extent from the
// vector data
m_File << "\tPROJECTION" << std::endl;
m_File << "\t \"init=epsg:" << m_SRID << "\"" << std::endl;
m_File << "\tEND" << std::endl;
m_File << "\t# Background color for the map canvas -- change as desired" << std::endl;
m_File << "\tIMAGECOLOR 192 192 192" << std::endl;
m_File << "\tIMAGEQUALITY 95" << std::endl;
m_File << "\tIMAGETYPE PNG" << std::endl;
m_File << "\tOUTPUTFORMAT" << std::endl;
m_File << "\t\tNAME PNG" << std::endl;
m_File << "\t\tDRIVER 'GD/PNG'" << std::endl;
m_File << "\t\tMIMETYPE 'image/png'" << std::endl;
m_File << "\t\tIMAGEMODE RGB" << std::endl;
m_File << "\t\tFORMATOPTION INTERLACE=OFF" << std::endl;
m_File << "\t\tEXTENSION 'png'" << std::endl;
m_File << "\tEND" << std::endl;
m_File << "\t# Web interface definition. Only the template parameter" << std::endl;
m_File << "\t# is required to display a map. See MapServer documentation" << std::endl;
m_File << "\tWEB" << std::endl;
m_File << "\t\t# Set IMAGEPATH to the path where MapServer should" << std::endl;
m_File << "\t\t# write its output." << std::endl;
// m_File <<"#IMAGEPATH \'D:\OSGeo4W_bis2/tmp/ms_tmp/\'" << std::endl;
m_File << "\t\t# Set IMAGEURL to the url that points to IMAGEPATH" << std::endl;
m_File << "\t\t# as defined in your web server configuration" << std::endl;
m_File << "\t\t#IMAGEURL '/ms_tmp/'" << std::endl;
m_File << "\t\t# WMS server settings" << std::endl;
m_File << "\t\t# NOTE : the user must change the path to the mapserver executable in the " << std::endl;
m_File << "\t\t# wms_onlineresource field" << std::endl;
m_File << "\t\tMETADATA" << std::endl;
m_File << "\t\t 'wms_title' 'Level0'" << std::endl;
m_File << "\t\t \'wms_onlineresource\' \'" << m_CGIPath << "?map=" << m_FileName << "&\'" << std::endl;
m_File << "\t\t \'wms_srs\' \'EPSG:" << m_SRID << " EPSG:900913\'" << std::endl;
m_File << "\t\tEND" << std::endl;
m_File << "\tEND" << std::endl;
}
template <class TInputImage>
void MapFileProductWriter<TInputImage>::AddLayer()
{
m_File << "\tLAYER" << std::endl;
m_File << "\t\tNAME '" << itksys::SystemTools::GetFilenameWithoutExtension(m_IndexShapeFileName) /*tempIndexShapeName.str()*/ << "'" << std::endl;
m_File << "\t\t\tOFFSITE 0 0 0" << std::endl;
m_File << "\t\t\tTYPE RASTER" << std::endl;
m_File << "\t\t\tTILEITEM 'LOCATION'" << std::endl;
m_File << "\t\t\tTILEINDEX \'" << itksys::SystemTools::GetFilenameName(m_IndexShapeFileName) << "\'" << std::endl;
// file <<"\t\t\tMETADATA" << std::endl;
// file <<"\t\t\t 'wms_title' 'earthsat'" << std::endl;
// file <<"\t\t\t 'wms_name' 'earthsat'" << std::endl;
// file <<"\t\t\tEND" << std::endl;
m_File << "\t\t\tPROCESSING \"RESAMPLE=AVERAGE\"" << std::endl;
m_File << "\t\t\tSTATUS ON" << std::endl;
m_File << "\t\t\tTRANSPARENCY 100" << std::endl;
m_File << "\t\t\tPROJECTION" << std::endl;
m_File << "\t\t\t \"init=epsg:" << m_SRID << "\"" << std::endl;
m_File << "\t\t\tEND" << std::endl;
m_File << "\t\t#MINSCALE 250000" << std::endl; // TODO : problem with
// the MINSCALE AND
// MAXSCALE ..
m_File << "\tEND" << std::endl;
}
template <class TInputImage>
void MapFileProductWriter<TInputImage>::PrintSelf(std::ostream& os, itk::Indent indent) const
{
// Call the superclass implementation
Superclass::PrintSelf(os, indent);
}
}
#endif
|