otbRCC8GraphFileWriter.hxx 5.56 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
/*
 * Copyright (C) 2005-2017 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.
 */
20

21 22
#ifndef otbRCC8GraphFileWriter_hxx
#define otbRCC8GraphFileWriter_hxx
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38

#include "otbRCC8GraphFileWriter.h"
#include "otbRCC8VertexIterator.h"
#include "otbRCC8EdgeIterator.h"
#include "otbMacro.h"
#include <fstream>

namespace otb
{
/**
 * Constructor
 */
template <class TInputGraph>
RCC8GraphFileWriter<TInputGraph>
::RCC8GraphFileWriter()
{
39
  this->SetNumberOfRequiredInputs(1);
40 41 42 43 44 45 46 47 48 49 50 51
  m_FileName = "";
}
/**
 * Destructor
 */
template <class TInputGraph>
RCC8GraphFileWriter<TInputGraph>
::~RCC8GraphFileWriter()
{
}
/**
 * Set the input graph.
52
 * \param graph The graph to write.
53 54
 */
template <class TInputGraph>
55
void
56 57 58
RCC8GraphFileWriter<TInputGraph>
::SetInput(const InputGraphType * graph)
{
OTB Bot's avatar
OTB Bot committed
59
  this->itk::ProcessObject::SetNthInput(0, const_cast<TInputGraph *>(graph));
60 61 62 63 64 65 66
}
/**
 * Get the input graph.
 * \return The input graph pointer.
 */
template <class TInputGraph>
typename RCC8GraphFileWriter<TInputGraph>
67
::InputGraphPointerType
68 69 70 71 72
RCC8GraphFileWriter<TInputGraph>
::GetInput(void)
{
  return static_cast<TInputGraph*>(this->itk::ProcessObject::GetInput(0));
}
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
/**
 * Update method.
 * (Call the Write() method).
 */
template <class TInputGraph>
void
RCC8GraphFileWriter<TInputGraph>
::Update(void)
{
  this->Write();
}
/**
 * Write Method.
 * Performs checkings and invoke GenerateData().
 */
template <class TInputGraph>
void
RCC8GraphFileWriter<TInputGraph>
::Write(void)
{
  InputGraphType * input = this->GetInput();

OTB Bot's avatar
OTB Bot committed
95
  itkDebugMacro(<< "Writing a RCC8Graph file");
96 97

  // Make sure input is available
98
  if (input == nullptr)
OTB Bot's avatar
OTB Bot committed
99
    {
100
    itkExceptionMacro(<< "No input to writer!");
OTB Bot's avatar
OTB Bot committed
101
    }
102 103 104

  // Make sure that we can write the file given the name
  //
OTB Bot's avatar
OTB Bot committed
105 106 107 108
  if (m_FileName == "")
    {
    itkExceptionMacro(<< "No filename was specified");
    }
109

110 111 112 113 114
  // Pipeline updating sequence
  input->UpdateOutputInformation();
  input->PropagateRequestedRegion();
  input->UpdateOutputData();
  // GenerateData (actually write file)
115 116
  this->GenerateData();
}
117 118 119 120
/**
 * Main computation method.
 */
template <class TInputGraph>
121
void
122 123
RCC8GraphFileWriter<TInputGraph>
::GenerateData()
124
{
OTB Bot's avatar
OTB Bot committed
125
  otbMsgDevMacro(<< "RCC8GraphFileWriter: GenerateData call");
126 127
  // input graph pointer
  InputGraphPointerType input = this->GetInput();
128

129 130
  // iterators typedefs
  typedef otb::RCC8VertexIterator<InputGraphType> VertexIteratorType;
OTB Bot's avatar
OTB Bot committed
131
  typedef otb::RCC8EdgeIterator<InputGraphType>   EdgeIteratorType;
132 133 134

  // Output file stream
  std::ofstream out;
135

136
  // open the outputfile
137
  out.open(m_FileName, std::ios::out);
138 139

  // Test if the file has been opened correctly
140
  if (!out)
OTB Bot's avatar
OTB Bot committed
141
    {
142
    RCC8GraphFileWriterException e(__FILE__, __LINE__);
143
    std::ostringstream msg;
144
    msg << " Could not create IO object for file ";
OTB Bot's avatar
OTB Bot committed
145
    msg << m_FileName << "." << std::endl;
146
    e.SetDescription(msg.str());
147 148
    throw e;
    return;
OTB Bot's avatar
OTB Bot committed
149
    }
150

151
  // Start writing the graph to file
OTB Bot's avatar
OTB Bot committed
152
  out << "digraph G {" << std::endl;
153 154 155

  // For each vertex in the graph
  VertexIteratorType vIt(input);
OTB Bot's avatar
OTB Bot committed
156 157 158 159
  for (vIt.GoToBegin(); !vIt.IsAtEnd(); ++vIt)
    {
    this->WriteVertex(out, vIt.GetIndex(), vIt.Get());
    }
160 161 162

  // For each edge in the graph
  EdgeIteratorType eIt(input);
OTB Bot's avatar
OTB Bot committed
163 164
  for (eIt.GoToBegin(); !eIt.IsAtEnd(); ++eIt)
    {
165 166 167
    this->WriteEdge(out, eIt.GetSourceIndex(),
                    eIt.GetTargetIndex(),
                    eIt.GetValue());
OTB Bot's avatar
OTB Bot committed
168
    }
169 170

  // Ends the graph writing
OTB Bot's avatar
OTB Bot committed
171
  out << "}" << std::endl;
172 173 174 175 176 177 178 179 180 181 182

  // Close the file
  out.close();
}
/**
 * Write an edge to file.
 * \param of The output file stream.
 * \param source The index of the source vertex.
 * \param target The index of the target vertex.
 * \param value  The value of the edge.
 */
183
template <class TInputGraph>
184 185
void
RCC8GraphFileWriter<TInputGraph>
OTB Bot's avatar
OTB Bot committed
186
::WriteEdge(std::ofstream& of, VertexDescriptorType source,
187
            VertexDescriptorType target, RCC8ValueType value)
188
{
OTB Bot's avatar
OTB Bot committed
189 190 191 192
  otbMsgDevMacro(<< "RCC8GraphFileWriter: WriteEdge call: " << source << " " << target << " " << value);
  of << source << " -> " << target << " ";
  of << "[Value=\"" << value << "\"];";
  of << std::endl;
193 194 195 196 197 198 199 200 201 202 203
}
/**
 * Write a vertex to file.
 * \param of The output file stream.
 * \param index The index of the edge to write.
 * \param vertex The pointer to the vertex object.
 */
template <class TInputGraph>
void
RCC8GraphFileWriter<TInputGraph>
::WriteVertex(std::ofstream& of, VertexDescriptorType index,
204
              VertexPointerType vertex)
205
{
206
  AttributesMapType attr = vertex->GetAttributesMap();
OTB Bot's avatar
OTB Bot committed
207 208
  otbMsgDevMacro(<< "RCC8GraphFileWriter: WriteVertex call: " << index);
  of << index << " [";
209
  IteratorType it = attr.begin();
OTB Bot's avatar
OTB Bot committed
210
  while (it != attr.end())
211
    {
OTB Bot's avatar
OTB Bot committed
212 213 214 215 216 217 218
    of << (*it).first << "=\"";
    of << (*it).second << "\"";
    ++it;
    if (it == attr.end())
      {
      of << "];" << std::endl;
      }
219
    else
OTB Bot's avatar
OTB Bot committed
220 221 222
      {
      of << ",";
      }
223
    }
224 225 226 227 228 229 230
}
/**
 * PrintSelf method
 */
template <class TInputGraph>
void
RCC8GraphFileWriter<TInputGraph>
OTB Bot's avatar
OTB Bot committed
231
::PrintSelf(std::ostream& os, itk::Indent indent) const
232
{
OTB Bot's avatar
OTB Bot committed
233
  Superclass::PrintSelf(os, indent);
234 235 236
}
} // namespace otb
#endif