otbGeometriesProjectionFilter.cxx 9.34 KB
Newer Older
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
/*=========================================================================

  Program:   ORFEO Toolbox
  Language:  C++
  Date:      $Date$
  Version:   $Revision$


  Copyright (c) Centre National d'Etudes Spatiales. All rights reserved.
  See OTBCopyright.txt for details.


     This software is distributed WITHOUT ANY WARRANTY; without even
     the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
     PURPOSE.  See the above copyright notices for more information.

=========================================================================*/

/*===========================================================================*/
/*===============================[ Includes ]================================*/
/*===========================================================================*/
#include "otbGeometriesProjectionFilter.h"
#include <boost/type_traits/is_same.hpp>
#include <boost/mpl/assert.hpp>
#include "ogr_srs_api.h" // OCTDestroyCoordinateTransformation
#include "ogr_spatialref.h" // OGRCoordinateTransformation
#include "itkMacro.h"
#include "otbGeometriesSet.h"
#include "itkMetaDataObject.h"
#include "otbOGRGeometryWrapper.h"
#include "otbOGRGeometriesVisitor.h"


/*===========================================================================*/
/*===============================[ constants ]===============================*/
/*===========================================================================*/
namespace
{ // Anonymous namespace
const double k_averageElevation = -32768.0;
} // Anonymous namespace

/*===========================================================================*/
/*======================[ Reprojection Transformation ]======================*/
/*===========================================================================*/


template <typename TGeometry>
otb::ogr::UniqueGeometryPtr
otb::ReprojectTransformationFunctor::ByCopy::operator()(TGeometry const* in) const
{
  boost::interprocess::unique_ptr<TGeometry, ogr::internal::GeometryDeleter>
    out(in ? static_cast <TGeometry*>(in->clone()) : 0); // OGR clone doesn't use covariant return ...
  if (out)
    m_Reprojector.do_transform(*out);
  ogr::UniqueGeometryPtr res(out.release());
  return boost::move(res);
}

template <typename TGeometry>
void
otb::ReprojectTransformationFunctor::InPlace::operator()(TGeometry * inout) const
{
  if (inout)
    m_Reprojector.do_transform(*inout);
}

#if 0
void do_transform(OGRLinearRing & g) const
{
  assert(!"don't know how to reproject a ring yet");
}
#endif

void otb::ReprojectTransformationFunctor::do_transform(OGRPoint & g) const
{
  typedef InternalTransformType::InputPointType  InputPointType;
  typedef InternalTransformType::OutputPointType OutputPointType;
  // in our case:
  BOOST_MPL_ASSERT((boost::is_same<InputPointType, OutputPointType>));
  const double rawPoint[] = { g.getX(), g.getY() };
  const InputPointType inPoint(rawPoint);
  const OutputPointType outPoint = m_Transform->TransformPoint(inPoint);
  g.setX(outPoint[0]);
  g.setY(outPoint[1]);
}

void otb::ReprojectTransformationFunctor::do_transform(OGRLineString & g) const
{
  OGRPoint point;
  for (int i=0, N=g.getNumPoints(); i!=N; ++i)
    {
    g.getPoint(i, &point);
    do_transform(point); // no need to go through the redispatch cycle
    g.setPoint(i, &point);
    }
}

#if 0
void otb::ReprojectTransformationFunctor::do_transform(OGRCurve & g) const
{
  assert(!"OGRCurve is just an interface, they can't be reprojected yet");
}
#endif

void otb::ReprojectTransformationFunctor::do_transform(OGRPolygon & g) const
{
  // Note: OGRPolygon interface states that rings obtained through
  // getInteriorRing() and getExteriorRing() shall not be modified.
  // In this context, it seems to make sense as it gives a direct access to the
  // ring (yes this is a de-encapsulation), and the polygon is a fresh one has
  // just been cloned in the () operator.
  // The fact that OGRPolygon::transform() directly works on each sub-ring seems
  // to confirm that hyppothesis.
  for (int i=0, N=g.getNumInteriorRings(); i!=N; ++i)
    {
    OGRLinearRing * ring = g.getInteriorRing(i);
    assert(ring && "Rings in polygons are not expected to be nil");
    if (ring)
      {
      do_transform(*ring); // no need to go through the redispatch cycle
      // how to commit the change ? Are ring modifications automatically commited ?
      }
    }
  OGRLinearRing * ring = g.getExteriorRing();
  assert(ring && "Rings in polygons are not expected to be nil");
  if (ring)
    {
    do_transform(*ring); // no need to go through the redispatch cycle
    // how to commit the change ? Are ring modifications automatically commited ?
    }
}

#if 0
void otb::ReprojectTransformationFunctor::do_transform(OGRSurface & g) const
{
  assert(!"OGRSurface is just an interface, they can't be reprojected yet");
}

void otb::ReprojectTransformationFunctor::do_transform(OGRMultiLineString & g) const
{
}

void otb::ReprojectTransformationFunctor::do_transform(OGRMultiPoint & g) const
{
}

void otb::ReprojectTransformationFunctor::do_transform(OGRMultiPolygon & g) const
{
}
#endif

void otb::ReprojectTransformationFunctor::do_transform(OGRGeometryCollection & col) const
{
  for (int i=0, N=col.getNumGeometries(); i!=N; ++i)
    {
    OGRGeometry * g = col.getGeometryRef(i);
    assert(g && "geometries to transform shall not be null");
    apply_inplace(g);
    }
}

otb::ogr::UniqueGeometryPtr
otb::ReprojectTransformationFunctor::operator()(OGRGeometry const* in) const
// otb::ReprojectTransformationFunctor::apply(OGRGeometry const* in) const
{
  otb::ogr::UniqueGeometryPtr res
    = ogr::apply<otb::ogr::UniqueGeometryPtr>(in, ByCopy(*this));
  return boost::move(res);
}

void otb::ReprojectTransformationFunctor::apply_inplace(OGRGeometry * inout) const
{
  if (inout)
    ogr::apply<void>(inout, InPlace(*this));
}

/*===========================================================================*/
/*======================[ GeometriesProjectionFilter ]=======================*/
/*===========================================================================*/

otb::GeometriesProjectionFilter::GeometriesProjectionFilter()
: InputImageReference(*this)
, OutputImageReference(*this)
{
}

/*virtual*/
otb::GeometriesProjectionFilter::~GeometriesProjectionFilter()
{
}

/*virtual*/
void otb::GeometriesProjectionFilter::GenerateOutputInformation(void)
{
  Superclass::GenerateOutputInformation();

  OutputGeometriesType::Pointer  output = this->GetOutput();
  itk::MetaDataDictionary& dict = output->GetMetaDataDictionary();

  itk::EncapsulateMetaData<std::string>(dict, MetaDataKey::ProjectionRefKey, m_OutputProjectionRef);
}

/*virtual*/
void otb::GeometriesProjectionFilter::FinalizeInitialisation()
{
  m_Transform = InternalTransformType::New();

  InputGeometriesType::ConstPointer   input      = this->GetInput();
  const itk::MetaDataDictionary&      inputDict  = input->GetMetaDataDictionary();
  OutputGeometriesType::Pointer       output     = this->GetOutput();
  itk::MetaDataDictionary&            outputDict = output->GetMetaDataDictionary();

  // m_Transform->SetInputDictionary(input->GetMetaDataDictionary());
  m_Transform->SetInputDictionary(inputDict);
  m_Transform->SetOutputDictionary(output->GetMetaDataDictionary());

  m_Transform->SetInputProjectionRef(m_InputProjectionRef);
  m_Transform->SetOutputProjectionRef(m_OutputProjectionRef);
  m_Transform->SetInputKeywordList(m_InputKeywordList);
  m_Transform->SetOutputKeywordList(m_OutputKeywordList);
  m_Transform->SetDEMDirectory(m_DEMDirectory);
  m_Transform->SetGeoidFile(m_GeoidFile);
  m_Transform->SetAverageElevation(k_averageElevation);

  m_Transform->SetInputSpacing(InputImageReference.GetSpacing());
  m_Transform->SetInputOrigin(InputImageReference.GetOrigin());
  m_Transform->SetOutputSpacing(OutputImageReference.GetSpacing());
  m_Transform->SetOutputOrigin(OutputImageReference.GetOrigin());

  m_Transform->InstanciateTransform();

  m_TransformationFunctor->SetOnePointTransformation(m_Transform);

  // retrieve the output projection ref
  // if it is not specified and end up being geographic,
  // only the m_Transform will know
  m_OutputProjectionRef = m_Transform->GetOutputProjectionRef();

  //If the projection information for the output is provided, propagate it

  if (m_OutputKeywordList.GetSize() != 0)
    {
    itk::EncapsulateMetaData<ImageKeywordlist>(outputDict, MetaDataKey::OSSIMKeywordlistKey, m_OutputKeywordList);
    }
  if (!m_OutputProjectionRef.empty())
    {
    itk::EncapsulateMetaData<std::string>(outputDict, MetaDataKey::ProjectionRefKey, m_OutputProjectionRef);
    }
  output->SetImageReference(OutputImageReference);
}

/*virtual*/
OGRSpatialReference* otb::GeometriesProjectionFilter::DoDefineNewLayerSpatialReference(
  ogr::Layer const& source) const
{
  if (!m_OutputProjectionRef.empty())
    {
    char const* wkt_string = m_OutputProjectionRef.c_str();
    OGRSpatialReference * res = static_cast <OGRSpatialReference*>(OSRNewSpatialReference(wkt_string));
    return res;
    }
  else
    {
    return 0;
    }
}

/*virtual*/
void otb::GeometriesProjectionFilter::DoProcessLayer(ogr::Layer const& source, ogr::Layer & destination) const
{
  // std::cout << "GPF::DoProcessLayer: L("<<source.GetName()<<") -> L("<<destination.GetName()<<") ...\n";
  if (source != destination)
    {
    m_TransformationFunctor(source, destination); // if TransformedElementType == layer
    }
  else
    {
    m_TransformationFunctor(destination); // if TransformedElementType == layer
    }
}