OSSIM - Open Source Software Image Map  Version 1.9.0 (20180803)
ossimOgrVectorTileSource.cpp
Go to the documentation of this file.
1 //*******************************************************************
2 //
3 // License: See top level LICENSE.txt file.
4 //
5 // Author: Mingjie Su
6 //
7 // Description:
8 //
9 // Contains class implementaiton for the class "ossimOgrVectorTileSource".
10 //
11 //*******************************************************************
12 // $Id: ossimOgrVectorTileSource.cpp 1347 2010-08-23 17:03:06Z oscar.kramer $
13 
15 #include <ossimOgcWktTranslator.h>
16 #include <ossimGdalType.h>
17 
18 #include <ossim/base/ossimTrace.h>
19 #include <ossim/base/ossimNotify.h>
20 #include <ossim/base/ossimIpt.h>
21 #include <ossim/base/ossimDpt.h>
28 
29 #include <ogr_api.h>
30 
32  "ossimOgrVectorTileSource",
34 
35 static ossimOgcWktTranslator wktTranslator;
36 static ossimTrace traceDebug("ossimOgrVectorTileSource:debug");
37 
39 {
40 public:
42  : theBoundingRect(bounds)
43  {
44  }
46  {
48  theGeoImage = 0;
49  }
50 
51  bool intersects(const ossimDrect& rect)const
52  {
53  return theBoundingRect.intersects(rect);
54  }
55 
56  bool intersects(double minX, double minY,
57  double maxX, double maxY)const
58  {
59  return theBoundingRect.intersects(ossimDrect(minX, minY, maxX, maxY));
60  }
61 
63  {
64  theGeoImage = image;
65  }
66 
69 };
70 
71 //*******************************************************************
72 // Public Constructor:
73 //*******************************************************************
76  theDataSource(0),
77  theImageBound(),
78  theBoundingExtent()
79 {
80 }
81 
82 //*******************************************************************
83 // Destructor:
84 //*******************************************************************
86 {
87  close();
88 }
89 
90 //*******************************************************************
91 // Public Method:
92 //*******************************************************************
94 {
95  const char* MODULE = "ossimOgrVectorTileSource::open";
96 
97  if(traceDebug())
98  {
100  << MODULE << " entered...\nFile: " << theImageFile.c_str() << std::endl;
101  }
102 
103  if(isOpen())
104  {
105  close();
106  }
107 
108  if ( isOgrVectorDataSource() )
109  {
110  //---
111  // Old interface removed in gdal.
112  // theDataSource = OGRSFDriverRegistrar::Open(theImageFile,
113  // false);
114  //---
115  theDataSource = (OGRDataSource*) OGROpen( theImageFile.c_str(), false, NULL );
116  if (theDataSource)
117  {
118  int layerCount = theDataSource->GetLayerCount();
119  theLayerVector.resize(layerCount);
120  if(layerCount)
121  {
122  for(int i = 0; i < layerCount; ++i)
123  {
124  OGRLayer* layer = theDataSource->GetLayer(i);
125  if(layer)
126  {
127  OGRSpatialReference* spatialReference = layer->GetSpatialRef();
128 
129  if(!spatialReference)
130  {
131  if(traceDebug())
132  {
134  << MODULE
135  << " No spatial reference given, assuming geographic"
136  << endl;
137  }
138  }
139  }
140  else
141  {
142  if(traceDebug())
143  {
145 
146  << MODULE
147  << " layer " << i << " is null." << endl;
148  }
149  }
150 
151  if (layer)
152  {
153  layer->GetExtent(&theBoundingExtent, true);
154 
155  ossimRefPtr<ossimProjection> proj = createProjFromReference(layer->GetSpatialRef());
156  ossimRefPtr<ossimImageGeometry> imageGeometry = 0;
157  bool isDefaultProjection = false;
158  if(proj.valid())
159  {
160  imageGeometry = new ossimImageGeometry(0, proj.get());
161  }
162 
163  ossimMapProjection* mapProj = 0;
164  if(imageGeometry.valid())
165  {
166  mapProj = PTR_CAST(ossimMapProjection, imageGeometry->getProjection());
167  }
168  else
169  {
170  mapProj = createDefaultProj();
171  imageGeometry = new ossimImageGeometry(0, mapProj);
172  isDefaultProjection = true;
173  }
174 
175  if(mapProj)
176  {
177  ossimDrect rect(theBoundingExtent.MinX,
178  theBoundingExtent.MaxY,
179  theBoundingExtent.MaxX,
180  theBoundingExtent.MinY,
182 
183  std::vector<ossimGpt> points;
184  if (isDefaultProjection || mapProj->isGeographic())
185  {
186  ossimGpt g1(rect.ul().y, rect.ul().x);
187  ossimGpt g2(rect.ur().y, rect.ur().x);
188  ossimGpt g3(rect.lr().y, rect.lr().x);
189  ossimGpt g4(rect.ll().y, rect.ll().x);
190 
191  points.push_back(g1);
192  points.push_back(g2);
193  points.push_back(g3);
194  points.push_back(g4);
195  }
196  else
197  {
198  ossimGpt g1 = mapProj->inverse(rect.ul());
199  ossimGpt g2 = mapProj->inverse(rect.ur());
200  ossimGpt g3 = mapProj->inverse(rect.lr());
201  ossimGpt g4 = mapProj->inverse(rect.ll());
202 
203  points.push_back(g1);
204  points.push_back(g2);
205  points.push_back(g3);
206  points.push_back(g4);
207  }
208 
209  std::vector<ossimDpt> rectTmp;
210  rectTmp.resize(4);
211 
212  for(std::vector<ossimGpt>::size_type index=0; index < 4; ++index)
213  {
214  imageGeometry->worldToLocal(points[(int)index], rectTmp[(int)index]);
215  }
216 
217  ossimDrect rect2 = ossimDrect(rectTmp[0],
218  rectTmp[1],
219  rectTmp[2],
220  rectTmp[3]);
221 
222  theLayerVector[i] = new ossimOgrVectorLayerNode(rect2);
223  theLayerVector[i]->setGeoImage(imageGeometry);
224 
225  // Initialize the image geometry to the first layer's geometry:
226  if (i == 0)
227  theImageGeometry = imageGeometry;
228  }
229  }
230  }
231 
232  } // if(layerCount)
233  else
234  {
235  OGR_DS_Destroy( theDataSource );
236  theDataSource = 0;
237  }
238 
239  } // Matches: if (theDataSource)
240 
241  } // Matches: if ( isOgrVectorDataSource() )
242 
243  return ( theDataSource ? true : false );
244 }
245 
246 //*******************************************************************
247 // Public method:
248 //*******************************************************************
250  const char* prefix) const
251 {
252  for (unsigned int i = 0; i < theLayerVector.size(); i++)
253  {
254  ossimRefPtr<ossimImageGeometry> imageGeometry = theLayerVector[i]->theGeoImage;
255  if(theImageGeometry.valid())
256  {
257  theImageGeometry->saveState(kwl, prefix);
258  }
259  }
260 
261  return ossimImageHandler::saveState(kwl, prefix);
262 }
263 
264 //*******************************************************************
265 // Public method:
266 //*******************************************************************
267 bool ossimOgrVectorTileSource::loadState(const ossimKeywordlist& kwl, const char* prefix)
268 {
269  bool success = ossimImageHandler::loadState(kwl, prefix);
270  if (success)
271  {
273  success = theImageGeometry->loadState(kwl, prefix);
274  }
275  return success;
276 }
277 
279 {
280  return 0;
281 }
282 
284 {
285  return 0;
286 }
287 
289 {
290  return theImageGeometry;
291 }
292 
294 {
295  for(ossim_uint32 i = 0; i < theLayerVector.size(); ++i)
296  {
297  if(theLayerVector[i])
298  {
299  delete theLayerVector[i];
300  }
301  }
302 
303  theLayerVector.clear();
304 
305  if(theDataSource)
306  {
307  delete theDataSource;
308  theDataSource = 0;
309  }
310 }
311 
313  const ossimIrect& /* tileRect */, ossim_uint32 /* resLevel */)
314 {
315  return 0;
316 }
317 
319 {
320  return 0;
321 }
322 
324 {
325  return 0;
326 }
327 
329 {
330  if(theImageBound.hasNans())
331  {
332  return theImageBound.ul().x;
333  }
334 
335  return theImageBound.height();
336 }
337 
339 {
340  if(theImageBound.hasNans())
341  {
342  return theImageBound.ul().y;
343  }
344 
345  return theImageBound.width();
346 }
347 
349 {
350  return 32;
351 }
352 
354 {
355  return theImageBound;
356 }
357 
359 {
360  return theImageGeometry;
361 }
362 
364 {
365  return OSSIM_SCALAR_UNKNOWN;
366 }
367 
369 {
370  return 0;
371 }
372 
374 {
375  return 0;
376 }
377 
379 {
380  return (theDataSource!=0);
381 }
382 
384 {
385  if (!theImageFile.empty())
386  {
387  if (theImageFile.before(":", 3).upcase() != "SDE" &&
388  theImageFile.before(":", 4).upcase() != "GLTP" &&
389  theImageFile.ext().downcase() != "mdb")
390  {
391  return false;
392  }
393  }
394  else
395  {
396  return false;
397  }
398 
399  return true;
400 }
401 
403 {
404  return 0.0;
405 }
406 
408 {
409  return 0.0;
410 }
411 
413 {
414  return 0.0;
415 }
416 
418 {
419  if(theLayerVector.size() > 0)
420  {
421  if (theLayerVector.size() > entryIdx)
422  {
423  theImageGeometry = 0;
425  theImageGeometry = theLayerVector[entryIdx]->theGeoImage;
426  theImageBound = theLayerVector[entryIdx]->theBoundingRect;
427  if (theImageGeometry.valid())
428  {
429  return true;
430  }
431  }
432  }
433  return false;
434 }
435 
437 {
438  return theLayerVector.size();
439 }
440 
441 void ossimOgrVectorTileSource::getEntryList(std::vector<ossim_uint32>& entryList) const
442 {
443  if (theLayerVector.size() > 0)
444  {
445  for (ossim_uint32 i = 0; i < getNumberOfEntries(); i++)
446  {
447  entryList.push_back(i);
448  }
449  }
450  else
451  {
453  }
454 }
455 
457 {
458  if(traceDebug())
459  {
460  ossimNotify(ossimNotifyLevel_DEBUG) << "ossimOgrVectorTileSource::createProjFromReference: entered........" << std::endl;
461  }
462  if(!reference)
463  {
464  if(traceDebug())
465  {
466  ossimNotify(ossimNotifyLevel_DEBUG) << "ossimOgrVectorTileSource::createProjFromReference: leaving........" << std::endl;
467  }
468  return 0;
469  }
470  char* wktString = 0;
471  ossimKeywordlist kwl;
472  reference->exportToWkt(&wktString);
473  wktTranslator.toOssimKwl(wktString,
474  kwl);
475  if(traceDebug())
476  {
477  ossimNotify(ossimNotifyLevel_DEBUG) << "wktString === " << wktString << std::endl;
478  ossimNotify(ossimNotifyLevel_DEBUG) << "KWL === " << kwl << std::endl;
479  }
480  OGRFree(wktString);
481  if(traceDebug())
482  {
483  ossimNotify(ossimNotifyLevel_DEBUG) << "ossimOgrVectorTileSource::createProjFromReference: returning........" << std::endl;
484  }
486 }
487 
489 {
491 
492  ossim_float64 centerLat = (theBoundingExtent.MaxY + theBoundingExtent.MinY ) / 2.0;
493  ossim_float64 centerLon = (theBoundingExtent.MaxX + theBoundingExtent.MinX ) / 2.0;
494  ossim_float64 deltaLat = theBoundingExtent.MaxY - theBoundingExtent.MinY;
495 
496  // Scale that gives 1024 pixel in the latitude direction.
497  ossim_float64 scaleLat = deltaLat / 1024.0;
498  ossim_float64 scaleLon = scaleLat*ossim::cosd(std::fabs(centerLat));
499  ossimGpt origin(centerLat, centerLon, 0.0);
500  ossimDpt scale(scaleLon, scaleLat);
501 
502  // Set the origin.
503  proj->setOrigin(origin);
504 
505  // Set the tie point.
506  proj->setUlGpt( ossimGpt(theBoundingExtent.MaxY,
507  theBoundingExtent.MinX) );
508 
509  // Set the scale. Note this will handle computing meters per pixel.
510  proj->setDecimalDegreesPerPixel(scale);
511 
512  return proj;
513 }
ossimRefPtr< ossimImageGeometry > theGeoImage
virtual ossim_uint32 getNumberOfOutputBands() const
void makeNan()
Definition: ossimDrect.h:388
ossimString before(const ossimString &str, std::string::size_type pos=0) const
METHOD: before(str, pos) Returns string beginning at pos and ending one before the token str If strin...
virtual bool setCurrentEntry(ossim_uint32 entryIdx)
virtual ossimScalarType getOutputScalarType() const
static ossimString upcase(const ossimString &aString)
Definition: ossimString.cpp:34
virtual ossim_uint32 getNumberOfSamples(ossim_uint32 reduced_res_level=0) const
virtual void close()
Deletes the overview and clears the valid image vertices.
virtual double getNullPixelValue(ossim_uint32 band=0) const
Each band has a null pixel associated with it.
ossimFilename theImageFile
ossim_float64 width() const
Definition: ossimDrect.h:522
ossimMapProjection * createDefaultProj()
void setGeoImage(ossimRefPtr< ossimImageGeometry > image)
Represents serializable keyword/value map.
virtual ossim_uint32 getImageTileHeight() const
bool valid() const
Definition: ossimRefPtr.h:75
ossimRefPtr< ossimImageGeometry > theImageGeometry
virtual bool loadState(const ossimKeywordlist &kwl, const char *prefix=0)
Attempts to initialize a transform and a projection given the KWL.
virtual ossim_uint32 getNumberOfEntries() const
virtual bool loadState(const ossimKeywordlist &kwl, const char *prefix=0)
Method to the load (recreate) the state of an object from a keyword list.
const ossimDpt & ul() const
Definition: ossimDrect.h:339
double y
Definition: ossimDpt.h:165
virtual bool isGeographic() const
ossimProjection * createProjFromReference(OGRSpatialReference *reference) const
virtual void setDecimalDegreesPerPixel(const ossimDpt &gsd)
std::vector< ossimOgrVectorLayerNode * > theLayerVector
ossimOgrVectorLayerNode(const ossimDrect &bounds)
virtual ossimGpt inverse(const ossimDpt &projectedPoint) const =0
Will take a point in meters and convert it to ground.
virtual void setUlGpt(const ossimGpt &ulGpt)
double ossim_float64
ossimProjection * createProjection(const ossimFilename &filename, ossim_uint32 entryIdx) const
virtual ossimRefPtr< ossimImageData > getTile(const ossimIrect &tileRect, ossim_uint32 resLevel=0)
virtual ossim_uint32 getTileHeight() const
unsigned int ossim_uint32
#define PTR_CAST(T, p)
Definition: ossimRtti.h:321
virtual ossim_uint32 getImageTileWidth() const
T * release()
Definition: ossimRefPtr.h:93
virtual double getMinPixelValue(ossim_uint32 band=0) const
Retuns the min pixel value.
virtual void setOrigin(const ossimGpt &origin)
static ossimString downcase(const ossimString &aString)
Definition: ossimString.cpp:48
bool hasNans() const
Definition: ossimDrect.h:396
double cosd(double x)
Definition: ossimCommon.h:259
virtual void getEntryList(std::vector< ossim_uint32 > &entryList) const
bool intersects(const ossimDrect &rect) const
Definition: ossimDrect.cpp:289
ossim_float64 height() const
Definition: ossimDrect.h:517
virtual ossim_uint32 getNumberOfLines(ossim_uint32 reduced_res_level=0) const
virtual ossim_uint32 getTileWidth() const
Container class that holds both 2D transform and 3D projection information for an image Only one inst...
ossimScalarType
static ossimProjectionFactoryRegistry * instance()
bool intersects(double minX, double minY, double maxX, double maxY) const
This class defines an abstract Handler which all image handlers(loaders) should derive from...
virtual ossim_uint32 getNumberOfInputBands() const
const ossimDpt & ur() const
Definition: ossimDrect.h:340
virtual double getMaxPixelValue(ossim_uint32 band=0) const
Returns the max pixel of the band.
double x
Definition: ossimDpt.h:164
const char * c_str() const
Returns a pointer to a null-terminated array of characters representing the string&#39;s contents...
Definition: ossimString.h:396
bool empty() const
Definition: ossimString.h:411
virtual bool saveState(ossimKeywordlist &kwl, const char *prefix=0) const
ossimString ext() const
RTTI_DEF1(ossimOgrVectorTileSource, "ossimOgrVectorTileSource", ossimImageHandler)
virtual bool saveState(ossimKeywordlist &kwl, const char *prefix=0) const
Saves the transform (if any) and projection (if any) states to the KWL.
const ossimDpt & ll() const
Definition: ossimDrect.h:342
bool toOssimKwl(const ossimString &wktString, ossimKeywordlist &kwl, const char *prefix=NULL) const
virtual bool isOpen() const
Derived classes must implement this method to be concrete.
bool intersects(const ossimDrect &rect) const
virtual bool loadState(const ossimKeywordlist &kwl, const char *prefix=0)
virtual ossimRefPtr< ossimImageGeometry > getInternalImageGeometry() const
Returns the image geometry object associated with this tile source or NULL if non defined...
virtual void getEntryList(std::vector< ossim_uint32 > &entryList) const
virtual ossimRefPtr< ossimImageGeometry > getImageGeometry()
Returns the image geometry object associated with this tile source or NULL if non defined...
const ossimDpt & lr() const
Definition: ossimDrect.h:341
virtual ossim_uint32 getNumberOfDecimationLevels() const
virtual ossimIrect getImageRectangle(ossim_uint32 reduced_res_level=0) const
OSSIMDLLEXPORT std::ostream & ossimNotify(ossimNotifyLevel level=ossimNotifyLevel_WARN)
virtual bool saveState(ossimKeywordlist &kwl, const char *prefix=0) const
Method to save the state of an object to a keyword list.