vtkGeoProjection.cxx 7.76 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
/*=========================================================================

  Program:   Visualization Toolkit
  Module:    vtkGeoProjection.cxx

  Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
  All rights reserved.
  See Copyright.txt or http://www.kitware.com/Copyright.htm 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 notice for more information.

=========================================================================*/
/*-------------------------------------------------------------------------
  Copyright 2008 Sandia Corporation.
  Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
  the U.S. Government retains certain rights in this software.
-------------------------------------------------------------------------*/

#include "vtkGeoProjection.h"

#include "vtkObjectFactory.h"

25
#include <sstream>
26
27
28
#include <string>
#include <map>
#include <vector>
29
30
31
32
33
34
35

#include "vtk_libproj4.h"

vtkStandardNewMacro(vtkGeoProjection);

static int vtkGeoProjectionNumProj = -1;

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
//-----------------------------------------------------------------------------
class vtkGeoProjection::vtkInternals
{
public:
  const char* GetKeyAt(int index)
  {
    if (static_cast<int>(this->OptionalParameters.size()) > index)
      {
      std::map< std::string, std::string >::iterator iter =
          this->OptionalParameters.begin();
      int nbIter = index;
      while(nbIter > 0)
        {
        nbIter--;
        iter++;
        }
      return iter->first.c_str();
      }
    return NULL;
  }

  const char* GetValueAt(int index)
  {
    if (static_cast<int>(this->OptionalParameters.size()) > index)
      {
      std::map< std::string, std::string >::iterator iter =
          this->OptionalParameters.begin();
      int nbIter = index;
      while(nbIter > 0)
        {
        nbIter--;
        iter++;
        }
      return iter->second.c_str();
      }
    return NULL;
  }

  std::map< std::string, std::string > OptionalParameters;
};

//-----------------------------------------------------------------------------
78
79
80
81
82
int vtkGeoProjection::GetNumberOfProjections()
{
  if ( vtkGeoProjectionNumProj < 0 )
    {
    vtkGeoProjectionNumProj = 0;
83
    for ( const PJ_LIST* pj = pj_get_list_ref(); pj && pj->id; ++ pj )
84
85
86
87
      ++ vtkGeoProjectionNumProj;
    }
  return vtkGeoProjectionNumProj;
}
88
//-----------------------------------------------------------------------------
89
90
91
92
93
const char* vtkGeoProjection::GetProjectionName( int projection )
{
  if ( projection < 0 || projection >= vtkGeoProjection::GetNumberOfProjections() )
    return 0;

94
  return pj_get_list_ref()[projection].id;
95
}
96
//-----------------------------------------------------------------------------
97
98
99
100
101
const char* vtkGeoProjection::GetProjectionDescription( int projection )
{
  if ( projection < 0 || projection >= vtkGeoProjection::GetNumberOfProjections() )
    return 0;

102
  return pj_get_list_ref()[projection].descr[0];
103
}
104
//-----------------------------------------------------------------------------
105
106
vtkGeoProjection::vtkGeoProjection()
{
107
  this->Name = NULL;
108
109
  this->SetName( "latlong" );
  this->CentralMeridian = 0.;
110
111
  this->Projection = NULL;
  this->ProjectionMTime = 0;
112
  this->Internals = new vtkInternals();
113
}
114
//-----------------------------------------------------------------------------
115
116
117
118
119
vtkGeoProjection::~vtkGeoProjection()
{
  this->SetName( 0 );
  if ( this->Projection )
    {
120
    pj_free( this->Projection );
121
    }
122
123
  delete this->Internals;
  this->Internals = NULL;
124
}
125
//-----------------------------------------------------------------------------
126
127
128
129
130
131
void vtkGeoProjection::PrintSelf( ostream& os, vtkIndent indent )
{
  this->Superclass::PrintSelf( os, indent );
  os << indent << "Name: " << this->Name << "\n";
  os << indent << "CentralMeridian: " << this->CentralMeridian << "\n";
  os << indent << "Projection: " << this->Projection << "\n";
132
133
134
135
136
137
  os << indent << "Optional parameters:\n";
  for(int i=0;i<this->GetNumberOfOptionalParameters();i++)
    {
    os << indent << " - " << this->GetOptionalParameterKey(i) << " = "
       << this->GetOptionalParameterValue(i) << "\n";
    }
138
}
139
//-----------------------------------------------------------------------------
140
141
142
int vtkGeoProjection::GetIndex()
{
  int i = 0;
143
  for ( const PJ_LIST* proj = pj_get_list_ref(); proj && proj->id; ++ proj, ++ i )
144
145
146
147
148
149
150
151
    {
    if ( ! strcmp( proj->id, this->Name ) )
      {
      return i;
      }
    }
  return -1;
}
152
//-----------------------------------------------------------------------------
153
154
155
156
157
158
159
160
161
const char* vtkGeoProjection::GetDescription()
{
  this->UpdateProjection();
  if ( ! this->Projection )
    {
    return 0;
    }
  return this->Projection->descr;
}
162
//-----------------------------------------------------------------------------
163
projPJ vtkGeoProjection::GetProjection()
164
165
166
167
168
{
  this->UpdateProjection();
  return this->Projection;
}

169
//-----------------------------------------------------------------------------
170
171
172
173
174
175
176
177
178
int vtkGeoProjection::UpdateProjection()
{
  if ( this->GetMTime() <= this->ProjectionMTime )
    {
    return 0;
    }

  if ( this->Projection )
    {
179
    pj_free( this->Projection );
180
181
182
183
184
185
186
187
188
189
190
    this->Projection = 0;
    }

  if ( ! this->Name || ! strlen( this->Name ) )
    {
    return 1;
    }

  if ( ! strcmp ( this->Name, "latlong" ) )
    {
    // latlong is "null" projection.
191
    return 0;
192
193
    }

194
195
  int argSize = 3 + this->GetNumberOfOptionalParameters();
  const char** pjArgs = new const char*[argSize];
196
  std::string projSpec( "+proj=" );
197
  projSpec += this->Name;
198
199
  std::string ellpsSpec( "+ellps=clrk66" );
  std::string meridSpec;
200
  std::ostringstream os;
201
202
203
204
205
206
  os << "+lon_0=" << this->CentralMeridian;
  meridSpec = os.str();
  pjArgs[0] = projSpec.c_str();
  pjArgs[1] = ellpsSpec.c_str();
  pjArgs[2] = meridSpec.c_str();

207
  // Add optional parameters
208
209
  std::vector<std::string> stringHolder(
    this->GetNumberOfOptionalParameters()); // Keep string ref in memory
210
211
  for(int i=0; i < this->GetNumberOfOptionalParameters(); i++)
    {
212
    std::ostringstream param;
213
214
    param << "+" << this->GetOptionalParameterKey(i);
    param << "=" << this->GetOptionalParameterValue(i);
215
    stringHolder[i] = param.str();
216
217
218
    pjArgs[3+i] = stringHolder[i].c_str();
    }

219
  this->Projection = pj_init( argSize, const_cast<char**>( pjArgs ) );
220
  delete[] pjArgs;
221
  this->ProjectionMTime = this->GetMTime();
222
223
  if ( this->Projection )
    {
224
    return 0;
225
    }
226
  return 1;
227
228
}

229
230
231
232
233
234
//-----------------------------------------------------------------------------
void vtkGeoProjection::SetOptionalParameter(const char* key, const char* value)
{
  if(key != NULL && value != NULL)
    {
    this->Internals->OptionalParameters[key] = value;
235
    this->Modified();
236
237
238
239
240
241
    }
  else
    {
    vtkErrorMacro("Invalid Optional Parameter Key/Value pair. None can be NULL");
    }
}
242

243
244
245
246
//-----------------------------------------------------------------------------
void vtkGeoProjection::RemoveOptionalParameter(const char* key)
{
  this->Internals->OptionalParameters.erase(key);
247
  this->Modified();
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
}
//-----------------------------------------------------------------------------
int vtkGeoProjection::GetNumberOfOptionalParameters()
{
  return static_cast<int>(this->Internals->OptionalParameters.size());
}
//-----------------------------------------------------------------------------
const char* vtkGeoProjection::GetOptionalParameterKey(int index)
{
  return this->Internals->GetKeyAt(index);
}
//-----------------------------------------------------------------------------
const char* vtkGeoProjection::GetOptionalParameterValue(int index)
{
  return this->Internals->GetValueAt(index);
}
//-----------------------------------------------------------------------------
void vtkGeoProjection::ClearOptionalParameters()
{
  this->Internals->OptionalParameters.clear();
268
  this->Modified();
269
}