OSSIM - Open Source Software Image Map  Version 1.9.0 (20180803)
ossimVpfTable.cpp
Go to the documentation of this file.
1 //*******************************************************************
2 // Copyright (C) 2000 ImageLinks Inc.
3 //
4 // License: See top level LICENSE.txt file.
5 //
6 // Author: Garrett Potts
7 //
8 // Description: This class give the capability to access tiles from an
9 // vpf file.
10 //
11 //********************************************************************
12 // $Id: ossimVpfTable.cpp 23205 2015-03-25 13:54:42Z gpotts $
14 #include <ossim/vec/vpf.h>
16 #include <cstdlib>
17 #include <cstring>
18 
20  const ossimVpfTable& data)
21 {
22  data.print(out);
23  return out;
24 }
25 
27  :theTableInformation(NULL)
28 {
29 }
30 
32 {
34  {
35  closeTable();
36  delete theTableInformation;
37  theTableInformation = NULL;
38  }
39 }
40 
42 {
43  closeTable();
44 
45  if(is_vpf_table(const_cast<char*>(tableName.c_str())))
46  {
48  {
49  delete theTableInformation;
50  theTableInformation = NULL;
51  }
53  memset(theTableInformation, 0, sizeof(vpf_table_type));
54 
55  theTableName = tableName;
56  *theTableInformation = vpf_open_table(const_cast<char*>(tableName.c_str()),
57  disk,
58  "rb",
59  NULL);
60  }
61  else
62  {
63  delete theTableInformation;
64  theTableInformation = NULL;
65 
66  return false;
67  }
68 
69  return true;
70 }
71 
73 {
75  {
77  }
78 }
79 
81 {
83 
84  return 0;
85 }
86 
88 {
90 
91  return 0;
92 }
93 
95 {
96  if((idx < 0) || idx > getNumberOfColumns())
97  {
98  return ossimString("");
99  }
101 }
102 
104 {
105  return (theTableInformation &&
107 }
108 
110 {
111  if(theTableInformation &&
113  {
114  long fpos;
115 
116  fpos = index_pos(0, *theTableInformation);
117  fseek(theTableInformation->fp, fpos, SEEK_SET);
118  }
119 }
120 
121 bool ossimVpfTable::goToRow(long row)const
122 {
123  if(theTableInformation &&
125  {
126  long fpos;
127 
128  fpos = index_pos(row, *theTableInformation);
129  if(fpos < 0)
130  {
131  return false;
132  }
133  fseek(theTableInformation->fp, fpos, SEEK_SET);
134  }
135 
136  return true;
137 }
138 
139 std::vector<ossimString> ossimVpfTable::getColumnValues(const ossimString& columnName)const
140 {
141  std::vector<ossimString> result;
142 
143  if(theTableInformation &&
145  {
146  long int columnNumber = table_pos(const_cast<char*>(columnName.c_str()), *theTableInformation);
147 
148  if(columnNumber >=0 &&
149  (columnNumber < theTableInformation->nfields))
150  {
151  row_type row;
152  // start at the first row of the table
153  reset();
154  for (int rowIndex = 1;
155  rowIndex <= theTableInformation->nrows;
156  ++rowIndex)
157  {
160  columnNumber);
161  value.trim();
162  result.push_back(value);
164  }
165  }
166  }
167 
168  return result;
169 }
170 
171 std::vector<ossimString> ossimVpfTable::getColumnValues(long columnNumber)const
172 {
173  std::vector<ossimString> result;
174 
175  if(theTableInformation &&
177  {
178  if(columnNumber >=0 &&
179  (columnNumber < theTableInformation->nfields))
180  {
181  row_type row;
182  // start at the first row of the table
183  reset();
184  for (int rowIndex = 0;
185  rowIndex < theTableInformation->nrows;
186  ++rowIndex)
187  {
190  columnNumber);
191 // value = value.downcase();
192  value = value.trim();
193  result.push_back(value);
195  }
196  }
197  }
198 
199  return result;
200 }
201 
202 
203 
205  long columnNumber)const
206 {
207  ossimString result;
208  ossim_int32 n=1;
209  switch(theTableInformation->header[columnNumber].type)
210  {
211  case 'T': // it's of type text so
212  {
213  char c;
214  char *buf = (char *)get_table_element(columnNumber,
215  row,
217  &c,
218  &n);
219  if(buf)
220  {
221  result = buf;
222  free(buf);
223  }
224  else
225  {
226  result = c;
227  }
228  break;
229  }
230  case 'I':
231  {
232  ossim_int32 value;
233  ossim_int32* v = (ossim_int32*)get_table_element(columnNumber,
234  row,
236  &value,
237  &n);
238  if(v)
239  {
240  result = ossimString::toString(v[0]);
241 
242  free(v);
243  }
244  else
245  {
246  result = ossimString::toString(value);
247  }
248  break;
249  }
250  case 'S':
251  {
252  short int value;
253  short int* v = (short int*)get_table_element(columnNumber,
254  row,
256  &value,
257  &n);
258  if(v)
259  {
260  result = ossimString::toString(v[0]);
261 
262  free(v);
263  }
264  else
265  {
266  result = ossimString::toString(value);
267  }
268  break;
269  }
270  case 'F':
271  {
272  float value;
273  float* v = (float*)get_table_element(columnNumber,
274  row,
276  &value,
277  &n);
278  if(v)
279  {
280  value = v[0];
281  free(v);
282  }
283  if (!is_vpf_null_float(value))
284  {
285  result = ossimString::toString(value);
286  }
287  else
288  {
289  result = "nan";
290  }
291  break;
292  }
293  case 'B':
294  {
295  double value;
296  double* tempBuf = (double*)get_table_element(columnNumber,
297  row,
299  &value,
300  &n);
301  if(tempBuf)
302  {
303  value = tempBuf[0];
304  free(tempBuf);
305  }
306  if (!is_vpf_null_double(value))
307  {
308  result = ossimString::toString(value);
309  }
310  else
311  {
312  result = "nan";
313  }
314 
315  break;
316  }
317  case 'C':
318  {
319  coordinate_type coordType;
320  coordinate_type* temp = (coordinate_type*)get_table_element(columnNumber,
321  row,
323  &coordType,
324  &n);
325  if(temp)
326  {
327  coordType = temp[0];
328  free(temp);
329  }
330  result = ossimString::toString(coordType.x)+
331  ossimString(" ") +
332  ossimString::toString(coordType.y);
333  break;
334  }
335  case 'K':
336  {
337  id_triplet_type idTripletType;
338  id_triplet_type* tempType = (id_triplet_type*)get_table_element(columnNumber,
339  row,
341  &idTripletType,
342  &n);
343  if(tempType)
344  {
345  idTripletType = tempType[0];
346  free (tempType);
347  }
348  result = ossimString(idTripletType.type) +
349  ossimString(" ") +
350  ossimString::toString(static_cast<ossim_int32>(idTripletType.id)) +
351  ossimString(" ") +
352  ossimString::toString(static_cast<ossim_int32>(idTripletType.tile))+
353  ossimString(" ") +
354  ossimString::toString(static_cast<ossim_int32>(idTripletType.exid));
355  break;
356  }
357  case 'D':
358  {
359  date_type date, formatDate;
360  date_type* temp = (date_type*)get_table_element(columnNumber,
361  row,
363  &date,
364  &n);
365  if(temp)
366  {
367  free(temp);
368  }
369  format_date(date, formatDate);
370  result = formatDate;
371  break;
372  }
373  case 'Z': // tri coordinate types x, y, z
374  {
375  tri_coordinate_type *tcptr=NULL;
376  tcptr = (tri_coordinate_type *) get_table_element(columnNumber,row,
377  *theTableInformation, NULL,&n);
378  result = "";
379  for (int k=0;k<n;k++)
380  {
381 
382  result = result + " (" +
383  ossimString::toString(tcptr[k].x) +
384  ossimString(" ") +
385  ossimString::toString(tcptr[k].y) +
386  ossimString(" ") +
387  ossimString::toString(tcptr[k].z) +
388  ossimString(") ") ;
389  }
390 
391  if (tcptr)
392  {
393  free(tcptr);
394  }
395  break;
396  }
397  }
398  result.trim();
399  return result;
400 }
401 
403  long columnNumber)const
404 {
405  row_type row = read_row( rowNumber, *theTableInformation);
406  ossimString result = getColumnValueAsString(row,
407  columnNumber);
409 
410  return result;
411 }
412 
414 {
415  if(theTableInformation &&
417  {
418  long int columnNumber = table_pos(const_cast<char*>(columnName.c_str()),
421  ossimString result = getColumnValueAsString(row,
422  columnNumber);
424  return result;
425  }
426 
427  return "";
428 }
429 
431 {
432  return ((ossim_int32)table_pos(const_cast<char*>(columnName.c_str()), *theTableInformation));
433 }
434 
436 {
437  if(theTableInformation &&
439  {
440  // make sure we are at the beginning of the table.
441  this->reset();
442 
443 
444  // the rest of this code is from the vpfutil. The vpfutil
445  // was grabbed from the vhcl map server software.
447 
448  ossim_int32 i,j,k,n;
449  ossim_int16 ival=0,*iptr=NULL;
450  ossim_int32 lval=0,*lptr=NULL;
451  ossim_float32 fval=0,*fptr=NULL;
452  date_type dval,*dptr=NULL;
453  id_triplet_type kval={0,0,0}, *kptr=NULL;
454  coordinate_type cval={0,0}, *cptr=NULL;
455  char *buf, ch, date[40];
456  row_type row;
457 
458 
459  out << "table name: " << theTableName << std::endl
460  << "desciption: " << table.description << std::endl
461  << std::endl;
462 
463  out << "Definition:" << std::endl;
464  for (i=0;i<table.nfields;i++) {
465  if (table.header[i].count < 0)
466  {
467 
468  out << table.header[i].name << " ("
469  << table.header[i].type << ",*) "
470  << table.header[i].description << std::endl;
471  }
472  else
473  {
474  out << table.header[i].name << " ("
475  << table.header[i].type << ","
476  << table.header[i].count << ") "
477  << table.header[i].description << std::endl;
478  }
479  }
480 
481  out << "\nContents:" << std::endl;
482  for (i=1;i<=table.nrows;i++)
483  {
484  row = read_next_row(table);
485  for (j=0;j<table.nfields;j++)
486  {
487  out << table.header[j].name << ": ";
488  switch (table.header[j].type) {
489  case 'T':
490  if (table.header[j].count==1)
491  {
492  get_table_element(j,row,table,&ch,&n);
493 
494  out << ch << std::endl;
495  }
496  else
497  {
498  buf = (char *)get_table_element(j,row,table,NULL,&n);
499  n = (long)strlen(table.header[j].name) + 2;
500  for (k=0;k<(long)strlen(buf);k++)
501  {
502  out << buf[k];
503  n++;
504  if (n>80)
505  {
506  out << std::endl;
507  n = 0;
508  }
509  }
510  out << std::endl;
511  free(buf);
512  }
513  break;
514  case 'I': // long
515  if (table.header[j].count==1)
516  {
517  get_table_element(j,row,table,&lval,&n);
518  if (!is_vpf_null_float(lval))
519  {
520  out << lval << std::endl;
521  }
522  else
523  {
524  out << "null" << std::endl;
525  }
526  }
527  else
528  {
529  lptr = (ossim_int32*)get_table_element(j,row,table,NULL,&n);
530  for (k=0;k<n;k++)
531  {
532  if (!is_vpf_null_float(lptr[k]))
533  {
534  out << lptr[k];
535  }
536  else
537  {
538  out << "null";
539  }
540  }
541  out << std::endl;
542  free(lptr);
543  }
544  break;
545  case 'R': // for double
546  {
547  double *dptr=NULL;
548  double value = 0;
549  if (table.header[j].count==1)
550  {
551  get_table_element(j,row,table,&value,&n);
552  if(!is_vpf_null_double(value))
553  {
554  out << "null" << std::endl;
555  }
556  else
557  {
558  out << value << std::endl;
559  }
560  }
561  else
562  {
563  dptr = (double*)get_table_element(j,row,table,NULL,&n);
564  for (k=0;k<n;k++)
565  {
566  if (!is_vpf_null_double(dptr[k]))
567  {
568  out << dptr[k];
569  }
570  else
571  {
572  out << "null ";
573  }
574  }
575  out << std::endl;
576  free(dptr);
577  }
578  }
579  case 'S': // integer
580  if (table.header[j].count==1)
581  {
582  get_table_element(j,row,table,&ival,&n);
583  if (ival != (short int)INT_MAX)
584  {
585  out << ival << std::endl;
586  }
587  else
588  {
589  out << "null" << std::endl;
590  }
591  }
592  else
593  {
594  iptr = (short int*)get_table_element(j,row,table,NULL,&n);
595  for (k=0;k<n;k++)
596  {
597  if (iptr[k] != (short int)INT_MAX)
598  {
599  out << iptr[k];
600  }
601  else
602  {
603  out << "null ";
604  }
605  }
606  out << std::endl;
607  free(iptr);
608  }
609  break;
610  case 'F': // float type
611  if (table.header[j].count==1)
612  {
613  get_table_element(j,row,table,&fval,&n);
614  if (!is_vpf_null_float(fval))
615  {
616  out << fval << std::endl;
617  }
618  else
619  {
620  out << "null" << std::endl;
621  }
622  }
623  else
624  {
625  fptr = (float*)get_table_element(j,row,table,NULL,&n);
626  for (k=0;k<n;k++)
627  {
628  if (!is_vpf_null_float(fptr[k]))
629  {
630  out << fptr[k] << " ";
631  }
632  else
633  {
634  out << "null ";
635  }
636  }
637  out << std::endl;
638  free(fptr);
639  }
640  break;
641  case 'Y':
642  {
643  double_tri_coordinate_type dtctValue, *dtctPtr;
644  if (table.header[j].count==1)
645  {
646  get_table_element(j,row,table,&dtctValue,&n);
647  out << "(";
648  if(is_vpf_null_double(dtctValue.x))
649  {
650  out << "null, ";
651  }
652  else
653  {
654  out << dtctValue.x << ", ";
655  }
656  if(is_vpf_null_double(dtctValue.y))
657  {
658  out << "null, ";
659  }
660  else
661  {
662  out << dtctValue.y << ", ";
663  }
664  if(is_vpf_null_double(dtctValue.z))
665  {
666  out << "null, ";
667  }
668  else
669  {
670  out << dtctValue.z << ", ";
671  }
672  }
673  else
674  {
675  dtctPtr = (double_tri_coordinate_type*)get_table_element(j,row,table,NULL,&n);
676  for (k=0;k<n;k++)
677  {
678  out << "(";
679  if(is_vpf_null_double(dtctPtr[k].x))
680  {
681  out << "null, ";
682  }
683  else
684  {
685  out << dtctPtr[k].x << ", ";
686  }
687  if(is_vpf_null_double(dtctPtr[k].y))
688  {
689  out << "null, ";
690  }
691  else
692  {
693  out << dtctPtr[k].y << ", ";
694  }
695  if(is_vpf_null_double(dtctPtr[k].z))
696  {
697  out << "null, ";
698  }
699  else
700  {
701  out << dtctPtr[k].z << ", ";
702  }
703  }
704  free(dtctPtr);
705  }
706  break;
707  }
708  case 'Z': // tri coordinate types x, y, z
709  {
710  tri_coordinate_type tcval={0,0,0}, *tcptr=NULL;
711  if (table.header[j].count==1)
712  {
713  get_table_element(j,row,table,&tcval,&n);
714  out << "(";
715  if(is_vpf_null_float(tcval.x))
716  {
717  out << "null" << ",";
718  }
719  else
720  {
721  out << tcval.x << ", ";
722  }
723  if(is_vpf_null_float(tcval.y))
724  {
725  out << "null" << ", ";
726  }
727  else
728  {
729  out << tcval.y << ", ";
730  }
731  if(is_vpf_null_float(tcval.z))
732  {
733  out << "null)" << std::endl;
734  }
735  else
736  {
737  out << tcval.z << ")" << std::endl;
738  }
739  }
740  else
741  {
742  tcptr = (tri_coordinate_type*)get_table_element(j,row,table,NULL,&n);
743  for (k=0;k<n;k++)
744  {
745  out << "(";
746  if(is_vpf_null_float(tcptr[k].x))
747  {
748  out << "null, ";
749  }
750  else
751  {
752  out << tcptr[k].x << ", ";
753  }
754  if(is_vpf_null_float(tcptr[k].y))
755  {
756  out << "null, ";
757  }
758  else
759  {
760  out << tcptr[k].y << ", ";
761  }
762  if(is_vpf_null_float(tcptr[k].z))
763  {
764  out << "null)";
765  }
766  else
767  {
768  out << tcptr[k].z << ")";
769  }
770  }
771  out << std::endl;
772  free(tcptr);
773  }
774  break;
775  }
776  case 'C': // coordinate type x, y
777  if (table.header[j].count==1)
778  {
779  get_table_element(j,row,table,&cval,&n);
780  out << "(";
781  if(is_vpf_null_float(cval.x))
782  {
783  out << "null, ";
784  }
785  else
786  {
787  out << cval.x << ", ";
788  }
789  if(is_vpf_null_float(cval.y))
790  {
791  out << "null, ";
792  }
793  else
794  {
795  out << cval.y << ")";
796  }
797  }
798  else
799  {
800  cptr = (coordinate_type*)get_table_element(j,row,table,NULL,&n);
801  for (k=0;k<n;k++)
802  {
803 
804  out << "(" << cptr[k].x << "," << cptr[k].y << ") ";
805  }
806  out << std::endl;
807  free(cptr);
808  }
809  break;
810  case 'B': // double coordinate type
811  {
812  double_coordinate_type dct, *dctPtr;
813 
814  if (table.header[j].count==1)
815  {
816  get_table_element(j,row,table,&dct,&n);
817 
818  out << "(";
819  if(is_vpf_null_double(dct.x))
820  {
821  out << "null, ";
822  }
823  else
824  {
825  out << dct.x << ", ";
826  }
827  if(is_vpf_null_double(dct.y))
828  {
829  out << "null, ";
830  }
831  else
832  {
833  out << dct.y << ")";
834  }
835  }
836  else
837  {
838  dctPtr = (double_coordinate_type*)get_table_element(j,row,table,NULL,&n);
839  for (k=0;k<n;k++)
840  {
841  out << "(";
842  if(is_vpf_null_double(dctPtr[k].x))
843  {
844  out << "null,";
845  }
846  else
847  {
848  out << dctPtr[k].x << ", ";
849  }
850  if(is_vpf_null_double(dctPtr[k].y))
851  {
852  out << "null,";
853  }
854  else
855  {
856  out << dctPtr[k].y << ", ";
857  }
858  }
859  out << std::endl;
860  free(dctPtr);
861  }
862 
863  }
864  case 'K': //id triplet
865  if (table.header[j].count==1)
866  {
867  get_table_element(j,row,table,&kval,&n);
868  out << "(" << kval.id
869  << ", " << kval.tile
870  << ", " << kval.exid
871  << ")" << std::endl;
872  }
873  else
874  {
875  kptr = (id_triplet_type*)get_table_element(j,row,table,NULL,&n);
876  for (k=0;k<n;k++)
877  {
878  out << "(" << kptr[k].id
879  << ", " << kptr[k].tile
880  << ", " << kptr[k].exid
881  << ") " << std::endl;
882 
883  }
884  out << std::endl;
885  free(kptr);
886  }
887  break;
888  case 'D': /* Date */
889  if (table.header[j].count==1)
890  {
891  get_table_element(j,row,table,&dval,&n);
892  format_date(dval,date);
893  out << date << std::endl;
894  }
895  else
896  {
897  dptr = (date_type*)get_table_element(j,row,table,NULL,&n);
898  for (k=0;k<n;k++)
899  {
900  format_date((char*)(&dptr[k]),date);
901  out << dptr[k] << " ";
902  }
903  out << std::endl;
904  free(dptr);
905  }
906  break;
907  }
908  }
909  out << std::endl;
910  free_row( row, table );
911  }
912  }
913 }
virtual bool goToRow(long row) const
vpf_table_type * theTableInformation
Definition: ossimVpfTable.h:81
ossim_uint32 x
void free_row(row_type row, vpf_table_type table)
virtual void closeTable()
ossim_int32 id
Definition: vpftable.h:216
int getNumberOfRows() const
void format_date(date_type date, char *fmtdate)
vpf_table_type vpf_open_table(const char *tablename, storage_type storage, const char *mode, char *defstr)
char * name
Definition: vpftable.h:63
ossim_uint32 y
unsigned char status
Definition: vpftable.h:121
float ossim_float32
char description[81]
Definition: vpftable.h:107
header_type header
Definition: vpftable.h:109
static ossimString toString(bool aValue)
Numeric to string methods.
row_type read_row(ossim_int32 row_number, vpf_table_type table)
std::vector< ossimString > getColumnValues(const ossimString &columnName) const
void push_back(char c)
Equivalent to insert(end(), c).
Definition: ossimString.h:905
#define OPENED
Definition: vpftable.h:83
ossim_int32 index_pos(ossim_int32 row_number, vpf_table_type table)
virtual bool openTable(const ossimFilename &tableName)
ossim_int32 getColumnPosition(const ossimString &columnName) const
int getNumberOfColumns() const
ossim_int32 table_pos(const char *field_name, vpf_table_type table)
virtual bool isClosed() const
#define CLOSED
Definition: vpftable.h:82
os2<< "> n<< " > nendobj n
ossim_int32 tile
Definition: vpftable.h:216
ossim_int32 is_vpf_table(const char *fname)
int is_vpf_null_float(float num)
ossimString trim(const ossimString &valueToTrim=ossimString(" \\)) const
this will strip lead and trailing character passed in.
ossimString getColumnName(int idx) const
ossim_int32 nrows
Definition: vpftable.h:114
char description[81]
Definition: vpftable.h:64
virtual void reset() const
ossimString getColumnValueAsString(row_type &row, long columnNumber) const
virtual ~ossimVpfTable()
void * get_table_element(ossim_int32 field_number, row_type row, vpf_table_type table, void *value, ossim_int32 *count)
short ossim_int16
ossim_int32 nfields
Definition: vpftable.h:106
unsigned char type
Definition: vpftable.h:215
int is_vpf_null_double(double num)
row_type read_next_row(vpf_table_type table)
void vpf_close_table(vpf_table_type *table)
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
ossimFilename theTableName
Definition: ossimVpfTable.h:86
ossim_int32 count
Definition: vpftable.h:69
virtual void print(std::ostream &out) const
char date_type[21]
Definition: vpftable.h:47
Definition: vpftable.h:74
ossim_int32 exid
Definition: vpftable.h:216
char type
Definition: vpftable.h:68
std::basic_ostream< char > ostream
Base class for char output streams.
Definition: ossimIosFwd.h:23
int ossim_int32
std::ostream & operator<<(std::ostream &out, const ossimVpfTable &data)