OSSIM - Open Source Software Image Map  Version 1.9.0 (20180803)
ossimIgenGenerator.cpp
Go to the documentation of this file.
1 //*******************************************************************
2 // Copyright (C) 2000 ImageLinks Inc.
3 //
4 // License: LGPL
5 //
6 // See LICENSE.txt file in the top level directory for more details.
7 //
8 // Author: Garrett Potts
9 //
10 //*************************************************************************
11 // $Id: ossimIgenGenerator.cpp 20206 2011-11-04 15:16:31Z dburken $
19 #include <ossim/base/ossimDatum.h>
21 
22 void ossimIgenGenerator::generateSpecList(bool outputToFileFlag)
23 {
24  if(theTilingFlag&&
27  {
28  generateTiledSpecList(outputToFileFlag);
29  }
30  else
31  {
32  generateNoTiledSpecList(outputToFileFlag);
33  }
34 
35 }
36 
37 void ossimIgenGenerator::getImageFilenamesFromSpecList(std::vector<ossimFilename>& filenameList)
38 {
39  if(!theSpecList.size())
40  {
42  }
43  if(theSpecList.size())
44  {
45  filenameList.clear();
46  for(ossim_uint32 i = 0; i < theSpecList.size(); ++i)
47  {
48  const char* filename = theSpecList[i].find("object2.", ossimKeywordNames::FILENAME_KW);
49 
50  if(filename)
51  {
52  filenameList.push_back(ossimFilename(filename));
53  }
54  else
55  {
56  filenameList.clear();
57  return;
58  }
59  }
60  }
61 
62 }
63 
65 {
70 }
71 
73 {
74 }
75 
77 {
78  theSpecList.clear();
79  generateInputKwl(input);
80  ossimImageSource* inputInterface = PTR_CAST(ossimImageSource,
81  input);
82  if(inputInterface)
83  {
84  theInputBoundingRect = inputInterface->getBoundingRect();
85  }
86 }
87 
88 
90 {
91  std::stack<ossimConnectableObject*> aStack;
92 
93  generateChainForMultiInputRecurse(aStack, connectable);
94 
95  ossim_int32 objectIndex = 1;
96  while(!aStack.empty())
97  {
98  ossimConnectableObject* current = aStack.top();
99  aStack.pop();
100  ossimString prefix = ossimString("object") + ossimString::toString(objectIndex) + ".";
101 
102  current->saveState(theInputKwl, prefix.c_str());
103 
104  ++objectIndex;
105  }
106  theInputKwl.add("type",
107  "ossimImageChain",
108  true);
109 
110 }
111 
112 void ossimIgenGenerator::generateChainForMultiInputRecurse(std::stack<ossimConnectableObject*>& aStack,
113  ossimConnectableObject* connectable)
114 {
115 
116 
117  if(connectable)
118  {
119  aStack.push(connectable);
120  for(ossim_uint32 i = 0; i < connectable->getNumberOfInputs(); ++i)
121  {
123  connectable->getInput(i));
124  }
125  }
126 }
127 
128 
130 {
132 }
133 
135 {
137 }
138 
140 {
142 }
143 
145 {
146  theInputKwl.clear();
147  if(connectable)
148  {
149  // if it has at least one non null input
150  // then we must build a chain and save it out
151  if(connectable->getInput())
152  {
153  generateChainForMultiInput(connectable);
154  }
155  else // it is a single input
156  {
157  connectable->saveState(theInputKwl);
158  }
159  }
160 }
161 
163 {
165  if(connectable)
166  {
167  connectable->saveState(theOutputKwl);
168  }
169 }
170 
172 {
173  theViewKwl.clear();
174  if(view)
175  {
176  view->saveState(theViewKwl);
177  }
178 }
179 
181 {
182 
183 }
184 
185 void ossimIgenGenerator::generateTiledSpecList(bool outputToFileFlag)
186 {
187  theSpecList.clear();
188  theSpecFileList.clear();
189  ossimDpt spacing = theTileSpacing;
190 
191  switch(theTileSpacingUnits)
192  {
193  case OSSIM_MINUTES:
194  {
195  spacing.x /= 60;
196  spacing.y /= 60;
197  generateGeoTiledSpecList(spacing, outputToFileFlag);
198  break;
199  }
200  case OSSIM_SECONDS:
201  {
202  spacing.x /= 3600;
203  spacing.y /= 3600;
204  generateGeoTiledSpecList(spacing, outputToFileFlag);
205  break;
206  }
207  case OSSIM_DEGREES:
208  {
209  generateGeoTiledSpecList(spacing, outputToFileFlag);
210  break;
211  }
212  case OSSIM_PIXEL:
213  {
214  generatePixelTiledSpecList(outputToFileFlag);
215  break;
216  }
217  default:
218  break;
219  }
220 }
221 
223 {
225 
226  if((!proj)||(theOutputGeoPolygon.size() != 4))
227  {
228  return;
229  }
230 
231  ossimIrect inputRect = getInputBoundingRect();
233  ossimGeoPolygon tempPoly;
234  ossimObject* obj = createInput();
236  obj);
238  chain->add(cutter);
239 
240  ossimKeywordlist kwl;
242 
243  if(outfile == "")
244  {
245  cerr << "Valid filename not given, returning!" << endl;
246  }
247 
248  kwl.add("object2.", theOutputKwl, true);
249  kwl.add("product.projection.",theViewKwl, true);
250  addPixelType(kwl, "product.");
251  ossimDpt p1;
252  ossimDpt p2;
253  ossimDpt p3;
254  ossimDpt p4;
255 
256 
261 
262  ossimIrect bounds(p1, p2, p3, p4);
263  if(inputRect.completely_within(bounds))
264  {
265  inputRect = bounds;
266  }
267 
269 
270  ossim_sint32 i = 1;
271  for(ossim_sint32 ulY = bounds.ul().y; ulY < bounds.lr().y; ulY+=(ossim_sint32)theTileSpacing.y)
272  {
273  for(ossim_sint32 ulX = bounds.ul().x; ulX < bounds.lr().x; ulX+=(ossim_sint32)theTileSpacing.x)
274  {
275  ossimDrect rect(ulX,
276  ulY,
277  ulX + (theTileSpacing.x - 1),
278  ulY + (theTileSpacing.y - 1));
279  ossimGpt gpt1;
280  ossimGpt gpt2;
281  ossimGpt gpt3;
282  ossimGpt gpt4;
283 
284  proj->lineSampleToWorld(rect.ul(), gpt1);
285  proj->lineSampleToWorld(rect.ur(), gpt2);
286  proj->lineSampleToWorld(rect.lr(), gpt3);
287  proj->lineSampleToWorld(rect.ll(), gpt4);
288 
289  tempPoly.clear();
290  tempPoly.addPoint(gpt1);
291  tempPoly.addPoint(gpt2);
292  tempPoly.addPoint(gpt3);
293  tempPoly.addPoint(gpt4);
294  addPadding(tempPoly, tempPoly);
295  cutter->setPolygon(tempPoly);
296  chain->saveState(kwl, "object1.");
297 
299  specName = specName.dirCat(ossimFilename("igen" + ossimString::toString(i) + ".spec"));
300  ossimFilename tilename = outfile.path();
301  if(theTileNamingConvention == "id")
302  {
303  int tileId = getTileId(inputRect, ossimDpt(ulX+theTileSpacing.x/2, ulY+theTileSpacing.y/2), theTileSpacing);
304  tilename = tilename.dirCat(ossimFilename(outfile.fileNoExtension() + "_" +
305  ossimString::toString(tileId)));
306  tilename.setExtension(outfile.ext());
307  }
308 
309  kwl.add("object2.",
311  tilename,
312  true);
313  kwl.add("object2.input_connection1",
314  kwl.find("object1.id"),
315  true);
316  theSpecFileList.push_back(specName);
317  if(outputToFileFlag)
318  {
319  kwl.write(specName.c_str());
320  }
321  else
322  {
323  theSpecList.push_back(kwl);
324  }
325  i++;
326  }
327  }
328 }
329 
330 void ossimIgenGenerator::generateGeoTiledSpecList(const ossimDpt& spacing, bool outputToFileFlag)
331 {
333  if((proj.valid())&&(theOutputGeoPolygon.size() == 4))
334  {
335  ossimDrect rect;
336 
337  ossimIrect inputRect = getInputBoundingRect();
338  ossimGpt gpt1;
339  ossimGpt gpt2;
340  ossimGpt gpt3;
341  ossimGpt gpt4;
342  proj->lineSampleToWorld(inputRect.ul(), gpt1);
343  proj->lineSampleToWorld(inputRect.ur(), gpt2);
344  proj->lineSampleToWorld(inputRect.lr(), gpt3);
345  proj->lineSampleToWorld(inputRect.ll(), gpt4);
346 
347  ossimDrect inputGroundRect(gpt1,
348  gpt2,
349  gpt3,
350  gpt4,
352 
358 
359  rect.stretchToTileBoundary(spacing);
360  inputGroundRect.stretchToTileBoundary(spacing);
361  double ulLat = rect.ul().lat;
362  double ulLon = rect.ul().lon;
363  ossimGeoPolygon tempPoly;
364  ossimObject* obj = createInput();
366  obj);
368  chain->add(cutter);
369 
370  ossimKeywordlist kwl;
372 
373  if(outfile == "")
374  {
375  cerr << "Valid filename not given, returning!" << endl;
376  }
377 
378  kwl.add("object2.", theOutputKwl, true);
379  kwl.add("object2.input_connection1",
380  kwl.find("object1.id"),
381  true);
382  kwl.add("product.projection.",theViewKwl, true);
383  addPixelType(kwl, "product.");
384  int i = 1;
385  int row = 0;
386  int col = 0;
387  // int tileId = 0;
388  while(ulLat > rect.ll().lat)
389  {
390  ulLon = rect.ul().lon;
391  col = 0;
392  while(ulLon < rect.ur().lon)
393  {
394  tempPoly.clear();
395  tempPoly.addPoint(ulLat, ulLon, ossim::nan(), theOutputGeoPolygon[0].datum());
396  tempPoly.addPoint(ulLat, ulLon+spacing.lon, ossim::nan(), theOutputGeoPolygon[0].datum());
397  tempPoly.addPoint(ulLat-spacing.lat, ulLon+spacing.lon, ossim::nan(), theOutputGeoPolygon[0].datum());
398  tempPoly.addPoint(ulLat-spacing.lat, ulLon, ossim::nan(), theOutputGeoPolygon[0].datum());
399 
400  addPadding(tempPoly, tempPoly);
401  cutter->setPolygon(tempPoly);
402  chain->saveState(kwl, "object1.");
403 
405  specName = specName.dirCat(ossimFilename("igen" + ossimString::toString(i) + ".spec"));
406  ossimFilename tilename = outfile.path();
407  if(theTileNamingConvention == "id")
408  {
409  int tileId = getTileId(inputGroundRect, ossimDpt(ulLon, ulLat), spacing);
410  tilename = tilename.dirCat(ossimFilename(outfile.fileNoExtension() + "_" +
411  ossimString::toString(tileId)));
412  tilename.setExtension(outfile.ext());
413  }
414  else
415  {
416  ossimUsgsQuad quad(ossimGpt(ulLat - (spacing.x), ulLon+(spacing.x), 0.0));
417  if(spacing.x == 7.5/60.0)
418  {
419  tilename = tilename.dirCat(ossimFilename(quad.quarterQuadName().downcase().trim()));
420  }
421  else
422  {
423  tilename = tilename.dirCat(ossimFilename(quad.quarterQuadSegName().downcase().trim()));
424  }
425 
426  tilename.setExtension(outfile.ext());
427  }
428 
429  kwl.add("object2.",
431  tilename,
432  true);
433  kwl.add("object2.input_connection1",
434  kwl.find("object1.id"),
435  true);
436  theSpecFileList.push_back(specName);
437  if(outputToFileFlag)
438  {
439  kwl.write(specName.c_str());
440  }
441  else
442  {
443  theSpecList.push_back(kwl);
444  }
445  ulLon += spacing.lon;
446  ++i;
447  ++col;
448  }
449  ++row;
450  ulLat -= spacing.lat;
451  }
452 
453  }
454 }
455 
456 
458 {
459  theSpecList.clear();
460  theSpecFileList.clear();
461 
462  if(theViewKwl.getSize()&&
463  theInputKwl.getSize()&&
465  {
466  ossimKeywordlist kwl;
468  {
471  obj.get());
472  if(chain.valid())
473  {
475 
477 
478  chain->add(cutter);
479  chain->saveState(kwl, "object1.");
480  }
481  else
482  {
483  kwl.add("object1.",theInputKwl, true);
484  }
485  obj = 0;
486  }
487  else
488  {
489  kwl.add("object1.",theInputKwl, true);
490  }
491  kwl.add("object2.", theOutputKwl, true);
492  kwl.add("object2.input_connection1",
493  kwl.find("object1.id"),
494  true);
495  kwl.add("product.projection.",theViewKwl, true);
496  addPixelType(kwl, "product.");
497 
499  "/"+
500  "igen1.spec");
501 
502  theSpecFileList.push_back(specName);
503  if(outputToFileFlag)
504  {
505  kwl.write(specName.c_str());
506  }
507  else
508  {
509  theSpecList.push_back(kwl);
510  }
511 
512  kwl.clear();
513  }
514 }
515 
517  const ossimDpt& currentPt,
518  const ossimDpt& spacing)const
519 {
520  ossimDpt ul(rect.ul());
521 
522  double relY = fabs(ul.y - currentPt.y);
523  double relX = fabs(ul.x - currentPt.x);
524 
525 
526  ossim_int32 spacingY = (ossim_int32)(relY/spacing.y);
527  ossim_int32 spacingX = (ossim_int32)(relX/spacing.x);
528  ossim_int32 maxHorizontalTiles = (ossim_int32)( (rect.width()-1)/spacing.x);
529 
530  return spacingY*maxHorizontalTiles + spacingX;
531 }
532 
533 
535 {
538  ossimIrect result;
539 
540  result.makeNan();
541  if(inter)
542  {
543  result = inter->getBoundingRect();
544  }
545  return result;
546 }
547 
549  const ossimGeoPolygon& input)const
550 {
551  output = input;
552  switch(theTilePaddingUnits)
553  {
554  case OSSIM_PIXEL:
555  {
557  if(proj.valid())
558  {
559  ossimDpt ul;
560  ossimDpt ur;
561  ossimDpt lr;
562  ossimDpt ll;
563 
564  if(input.getOrdering() == OSSIM_CLOCKWISE_ORDER)
565  {
566  proj->worldToLineSample(input[0], ul);
567  proj->worldToLineSample(input[1], ur);
568  proj->worldToLineSample(input[2], lr);
569  proj->worldToLineSample(input[3], ll);
570  }
571  else
572  {
573  proj->worldToLineSample(input[0], ul);
574  proj->worldToLineSample(input[1], ll);
575  proj->worldToLineSample(input[2], lr);
576  proj->worldToLineSample(input[3], ur);
577 
578  }
579 
580  ul -= theTilePadding;
581  ur += ossimDpt(theTilePadding.x,
582  -theTilePadding.y);
583  lr += theTilePadding;
584  ll += ossimDpt(-theTilePadding.x,
585  theTilePadding.y);
586 
587  proj->lineSampleToWorld(ul, output[0]);
588  proj->lineSampleToWorld(ur, output[1]);
589  proj->lineSampleToWorld(lr, output[2]);
590  proj->lineSampleToWorld(ll, output[3]);
591 
593 
594  }
595  break;
596  }
597  case OSSIM_DEGREES:
598  case OSSIM_MINUTES:
599  case OSSIM_SECONDS:
600  {
601  ossimGpt ul;
602  ossimGpt ur;
603  ossimGpt lr;
604  ossimGpt ll;
605  ossimDpt spacing = theTilePadding;
606 
608  {
609  spacing.x /= 60;
610  spacing.y /= 60;
611  }
613  {
614  spacing.x /= 3600;
615  spacing.y /= 3600;
616  }
617 
618  if(input.getOrdering() == OSSIM_CLOCKWISE_ORDER)
619  {
620  ul = input[0];
621  ur = input[1];
622  lr = input[2];
623  ll = input[3];
624  }
625  else
626  {
627  ul = input[0];
628  ll = input[1];
629  lr = input[2];
630  ur = input[3];
631  }
632  ul.lat += spacing.lat;
633  ul.lon -= spacing.lon;
634  ur.lat += spacing.lat;
635  ur.lon += spacing.lon;
636  lr.lat -= spacing.lat;
637  lr.lon += spacing.lon;
638  ll.lat -= spacing.lat;
639  ll.lon -= spacing.lon;
640 
641  output[0] = ul;
642  output[1] = ur;
643  output[2] = lr;
644  output[3] = ll;
646 
647  break;
648  }
649  case OSSIM_METERS:
650  {
651  break;
652  }
653  default:
654  break;
655  }
656 }
657 
659 {
661  {
662  kwl.add(prefix,
664  "PIXEL_IS_POINT",
665  true);
666  }
667  else
668  {
669  kwl.add(prefix,
671  "PIXEL_IS_AREA",
672  true);
673  }
674 }
675 
677 {
679 
680  theOutputKwl = kwl;
681 }
void addPixelType(ossimKeywordlist &kwl, const char *prefix="product.")
ossimGrect getBoundingGround() const
ossimVertexOrdering getOrdering() const
virtual ossimIrect getBoundingRect(ossim_uint32 resLevel=0) const
This will return the bounding rect of the source.
ossimKeywordlist theOutputKwl
ossimIrect theInputBoundingRect
virtual void setPolygon(const vector< ossimDpt > &polygon, ossim_uint32 i=0)
ossim_float64 width() const
Definition: ossimDrect.h:522
void addPadding(ossimGeoPolygon &output, const ossimGeoPolygon &input) const
Represents serializable keyword/value map.
void generateTiledSpecList(bool outputToFileFlag)
virtual bool saveState(ossimKeywordlist &kwl, const char *prefix=NULL) const
Will save the state of the chain.
bool valid() const
Definition: ossimRefPtr.h:75
const char * find(const char *key) const
vector< ossimKeywordlist > theSpecList
double nan()
Method to return ieee floating point double precision NAN.
Definition: ossimCommon.h:135
const ossimDpt & ul() const
Definition: ossimDrect.h:339
virtual bool saveState(ossimKeywordlist &kwl, const char *prefix=0) const
Definition: ossimObject.cpp:95
double y
Definition: ossimDpt.h:165
ossimString theTileNamingConvention
static ossimString toString(bool aValue)
Numeric to string methods.
ossimIrect getInputBoundingRect() const
const ossimIpt & ul() const
Definition: ossimIrect.h:274
static ossimObjectFactoryRegistry * instance()
ossimGeoPolygon theOutputGeoPolygon
ossimProjection * createProductProjection() const
const ossimIpt & ll() const
Definition: ossimIrect.h:277
virtual bool write(const char *file, const char *comment=0) const
Methods to dump the ossimKeywordlist to a file on disk.
void generateNoTiledSpecList(bool outputToFileFlag)
vector< ossimFilename > theSpecFileList
void setOrdering(ossimVertexOrdering ordering)
ossimConnectableObject * getInput(ossim_uint32 index=0)
returns the object at the specified index.
bool completely_within(const ossimIrect &rect) const
Definition: ossimIrect.cpp:425
void add(const char *prefix, const ossimKeywordlist &kwl, bool overwrite=true)
double lat
Definition: ossimDpt.h:165
ossimProjection * createProjection(const ossimFilename &filename, ossim_uint32 entryIdx) const
void generateGeoTiledSpecList(const ossimDpt &spacing, bool outputToFileFlag)
ossimKeywordlist theInputKwl
ossim_float64 lon
Definition: ossimGpt.h:266
void generatePixelTiledSpecList(bool outputToFileFlag)
unsigned int ossim_uint32
ossimString trim(const ossimString &valueToTrim=ossimString(" \\)) const
this will strip lead and trailing character passed in.
#define PTR_CAST(T, p)
Definition: ossimRtti.h:321
signed int ossim_sint32
void generateSpecList(bool outputToFileFlag=false)
void addPoint(const ossimGpt &pt)
const ossimIpt & lr() const
Definition: ossimIrect.h:276
virtual bool add(ossimConnectableObject *source)
Will return true or false if an image source was added to the chain.
static ossimString downcase(const ossimString &aString)
Definition: ossimString.cpp:48
bool hasNans() const
Definition: ossimDpt.h:67
void generateOutputKwl(ossimConnectableObject *connectable)
double lon
Definition: ossimDpt.h:164
ossim_uint32 size() const
const ossimIpt & ur() const
Definition: ossimIrect.h:275
ossimFilename theSpecFileLocation
static ossimProjectionFactoryRegistry * instance()
ossimPixelType thePixelType
ossimObject * createOutput() const
void stretchToTileBoundary(const ossimDpt &widthHeight)
Definition: ossimDrect.cpp:396
ossimKeywordlist theViewKwl
void setInput(ossimConnectableObject *input)
ossim_int32 y
Definition: ossimIpt.h:142
void makeNan()
Definition: ossimIrect.h:329
ossimUnitType theTilePaddingUnits
const ossimDpt & ur() const
Definition: ossimDrect.h:340
static const char * PIXEL_TYPE_KW
virtual ossim_uint32 getNumberOfInputs() const
Returns the number of input objects.
ossimString quarterQuadSegName() const
Definition: ossimUsgsQuad.h:76
ossimObject * createInput() const
ossimFilename fileNoExtension() const
void getImageFilenamesFromSpecList(std::vector< ossimFilename > &filenameList)
double x
Definition: ossimDpt.h:164
virtual ossimObject * createObject(const ossimString &name) const
ossimFilename dirCat(const ossimFilename &file) const
void generateInputKwl(ossimConnectableObject *connectable)
ossim_uint32 getSize() const
ossimString quarterQuadName() const
Definition: ossimUsgsQuad.h:70
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
void stretchToTileBoundary(const ossimIpt &tileWidthHeight)
Definition: ossimIrect.cpp:212
int getTileId(const ossimDrect &rect, const ossimDpt &currentPt, const ossimDpt &spacing) const
ossimString ext() const
ossim_int32 x
Definition: ossimIpt.h:141
const ossimDpt & ll() const
Definition: ossimDrect.h:342
ossim_float64 lat
Definition: ossimGpt.h:265
ossimUnitType theTileSpacingUnits
void generateViewKwl(ossimObject *view)
void generateChainForMultiInput(ossimConnectableObject *connectable)
virtual void lineSampleToWorld(const ossimDpt &lineSampPt, ossimGpt &worldPt) const =0
void generateChainForMultiInputRecurse(std::stack< ossimConnectableObject *> &aStack, ossimConnectableObject *connectable)
ossimFilename & setExtension(const ossimString &e)
Sets the extension of a file name.
virtual void worldToLineSample(const ossimGpt &worldPoint, ossimDpt &lineSampPt) const =0
const ossimDpt & lr() const
Definition: ossimDrect.h:341
static const char * FILENAME_KW
ossimFilename path() const
void setOutput(ossimConnectableObject *output)
virtual bool saveState(ossimKeywordlist &kwl, const char *prefix=0) const
int ossim_int32