OSSIM - Open Source Software Image Map  Version 1.9.0 (20180803)
ossimShapeDatabase.cpp
Go to the documentation of this file.
1 
2 #include <iomanip>
3 
4 #include <ossimShapeDatabase.h>
5 #include <ossimShapeFile.h>
6 
7 RTTI_DEF1(ossimShapeDatabase, "ossimShapeDatabase", ossimObject);
8 using namespace std;
9 
11 {
12  switch(theFieldType)
13  {
14  case FTString:
15  {
16  return "String";
17  }
18  case FTInteger:
19  {
20  return "Integer";
21  }
22  case FTDouble:
23  {
24  return "Double";
25  }
26  default:
27  {
28  return "Unknown";
29  }
30  };
31 
32  return "";
33 
34 }
35 
37  ossim_uint32 i)
38 {
39  if(i < theFieldArray.size())
40  {
41  result = theFieldArray[i];
42  return true;
43  }
44  return false;
45 }
46 
48  ossim_uint32 i)
49 {
50  if(i < theFieldArray.size())
51  {
52  theFieldArray[i] = field;
53  return true;
54  }
55  return false;
56 }
57 
59 {
60  return theFieldArray.size();
61 }
62 
64 {
65  if(n)
66  {
67  theFieldArray.resize(n);
68  }
69  else
70  {
71  theFieldArray.clear();
72  }
73 }
74 
76  bool caseInsensitive)const
77 {
78  ossimString searchString = name;
79  if(caseInsensitive) searchString = searchString.downcase();
80  ossim_int32 idx = 0;
81  for(idx = 0; idx < (int)theFieldArray.size(); ++idx)
82  {
83  if(caseInsensitive)
84  {
85  if(ossimString(theFieldArray[idx].theName).downcase() == searchString)
86  {
87  return idx;
88  }
89  }
90  else
91  {
92  if(theFieldArray[idx].theName == searchString)
93  {
94  return idx;
95  }
96  }
97  }
98  return -1;
99 }
100 
102 {
103  rhs.print(out);
104 
105  return out;
106 }
107 
109  :theHandle(NULL),
110  theFilename("")
111 {
112  theRecordNumber = -1;
113 }
114 
116 {
117  close();
118 }
119 
121  const ossimString& flags)
122 {
123  if(isOpen()) close();
124 
125  theHandle = DBFOpen(file.c_str(), flags.c_str());
126  if(theHandle)
127  {
128  theFilename = file;
129  theRecordNumber = -1;
130  }
131 
132  return (theHandle != NULL);
133 }
134 
136 {
137  if(isOpen())
138  {
140  theHandle = NULL;
141  theRecordNumber = -1;
142  }
143 }
145 {
146  if(isOpen())
147  {
148  return theHandle->nRecords;
149  }
150 
151  return 0;
152 }
153 
155 {
156  if(isOpen()&&( (theRecordNumber < theHandle->nRecords) ))
157  {
158  if(result.getNumberOfFields() != theHandle->nFields)
159  {
160  result.setNumberOfFields(theHandle->nFields);
161  }
162 
163  char name[1024] = {'\0'};
164  int width = 0;
165  int decimals = 0;
166  int iField = 0;
167  std::vector<int> fieldWidths;
168 
169  for(iField = 0; iField < theHandle->nFields; ++iField)
170  {
171  DBFFieldType fieldType = DBFGetFieldInfo(theHandle,
172  iField,
173  name,
174  &width,
175  &decimals);
177  field.theName = name;
178  field.theWidth = width;
179  field.theDecimals = decimals;
180  field.theFieldType = fieldType;
181 
182  ossimString key = "field";
183  key+=ossimString::toString(iField+1);
184  key+=(ossimString(".")+name+":");
185 
186  switch(fieldType)
187  {
188  case FTString:
189  {
191  break;
192  }
193  case FTInteger:
194  {
196  break;
197  }
198  case FTDouble:
199  {
201  break;
202  }
203  case FTLogical:
204  {
205  break;
206  }
207  case FTInvalid:
208  {
209  break;
210  }
211  }
212 
213  result.setField(field,
214  iField);
215  }
216  return true;
217  }
218 
219  return false;
220 }
221 
223  int recordNumber)
224 {
225  if(isOpen())
226  {
227  if(recordNumber < getNumberOfRecords())
228  {
229  theRecordNumber = recordNumber;
230  return getRecord(result);
231  }
232  }
233 
234  return false;
235 }
236 
238 {
239  if(isOpen() && ((theRecordNumber+1) < getNumberOfRecords()))
240  {
241  ++theRecordNumber;
242  return getRecord(result);
243  }
244 
245  return false;
246 }
247 
249 {
250  return (theHandle!=NULL);
251 }
252 
254 {
255  return theHandle;
256 }
257 
258 const DBFHandle& ossimShapeDatabase::getHandle()const
259 {
260  return theHandle;
261 }
262 
264 {
265  if(isOpen())
266  {
267  out << std::setw(15)<<setiosflags(std::ios::left)
268  <<"DB filename:" << theFilename << std::endl
269  << std::setw(15)<<setiosflags(std::ios::left)
270  <<"records:" << theHandle->nRecords << std::endl
271  << std::setw(15)<<setiosflags(std::ios::left)
272  <<"fields:" << theHandle->nFields << std::endl;
273 
274  char name[1024] = {'\0'};
275  int width = 0;
276  int decimals = 0;
277  int iField = 0;
278  std::vector<int> fieldWidths;
279 
280 
281  for(iField = 0; iField < theHandle->nFields; ++iField)
282  {
283  DBFFieldType fieldType = DBFGetFieldInfo(theHandle,
284  iField,
285  name,
286  &width,
287  &decimals);
288  ossimString s = "field " + ossimString::toString(iField+1) + " name:";
289  switch(fieldType)
290  {
291  case FTString:
292  case FTInteger:
293  case FTDouble:
294  {
295  out << std::setw(15) << setiosflags(std::ios::left) << s.c_str() << name << std::endl;
296  break;
297  }
298  default:
299  {
300  out << std::setw(15) << setiosflags(std::ios::left) << s.c_str() << "INVALID"<<std::endl;
301  break;
302  }
303  }
304  }
305  for(int iShape = 0; iShape < theHandle->nRecords; ++iShape)
306  {
307  for(iField = 0; iField < theHandle->nFields; ++iField)
308  {
309  DBFFieldType fieldType = DBFGetFieldInfo(theHandle,
310  iField,
311  name,
312  &width,
313  &decimals);
314 
315  ossimString key = "field";
316  key+=ossimString::toString(iField+1);
317  key+=(ossimString(".")+name+":");
318 
319  switch(fieldType)
320  {
321  case FTString:
322  {
323 
324  out << std::setw(25) << setiosflags(std::ios::left) << key.c_str()
325  << DBFReadStringAttribute(theHandle, iShape, iField) <<std::endl;
326 
327  break;
328  }
329  case FTInteger:
330  {
331  out << std::setw(25) << setiosflags(std::ios::left) << key.c_str()
332  << DBFReadIntegerAttribute(theHandle, iShape, iField) << std::endl;
333 
334  break;
335  }
336  case FTDouble:
337  {
338  out << std::setw(25) << setiosflags(std::ios::left) << key.c_str()
339  << DBFReadDoubleAttribute(theHandle, iShape, iField) << std::endl;
340 
341  break;
342  }
343  case FTLogical:
344  {
345  break;
346  }
347  case FTInvalid:
348  {
349  break;
350  }
351  }
352  }
353  out << "_________________________________"<<std::endl;
354  }
355  }
356 
357  return out;
358 }
virtual std::ostream & print(std::ostream &out) const
Generic print method.
DBFFieldType SHPAPI_CALL DBFGetFieldInfo(DBFHandle psDBF, int iField, char *pszFieldName, int *pnWidth, int *pnDecimals)
void SHPAPI_CALL DBFClose(DBFHandle hDBF)
static ossimString toString(bool aValue)
Numeric to string methods.
bool getField(ossimShapeDatabaseField &result, ossim_uint32 i)
virtual bool open(const ossimFilename &file, const ossimString &flags=ossimString("rb"))
std::ostream & operator<<(std::ostream &out, const ossimShapeDatabase &rhs)
bool getRecord(ossimShapeDatabaseRecord &result)
double SHPAPI_CALL DBFReadDoubleAttribute(DBFHandle hDBF, int iShape, int iField)
bool getNextRecord(ossimShapeDatabaseRecord &result)
os2<< "> n<< " > nendobj n
int SHPAPI_CALL DBFReadIntegerAttribute(DBFHandle hDBF, int iShape, int iField)
unsigned int ossim_uint32
static ossimString downcase(const ossimString &aString)
Definition: ossimString.cpp:48
ossimString fieldTypeAsString() const
RTTI_DEF1(ossimShapeDatabase, "ossimShapeDatabase", ossimObject)
ossim_int32 getFieldIdx(const ossimString &name, bool caseInsensitive=true) const
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 setField(const ossimShapeDatabaseField &field, ossim_uint32 i)
const char SHPAPI_CALL1 * DBFReadStringAttribute(DBFHandle hDBF, int iShape, int iField);const char SHPAPI_CALL1(*) DBFReadLogicalAttribute(DBFHandle hDBF, int iShape, int iField
virtual DBFHandle getHandle()
ossimFilename theFilename
std::basic_ostream< char > ostream
Base class for char output streams.
Definition: ossimIosFwd.h:23
int ossim_int32