OSSIM - Open Source Software Image Map  Version 1.9.0 (20180803)
ossimShapeFile.cpp
Go to the documentation of this file.
1 //*******************************************************************
2 // License: See top level LICENSE.txt file.
3 //
4 // Author: Garrett Potts
5 //
6 // Description: This class extends the stl's string class.
7 //
8 //********************************************************************
9 // $Id: ossimShapeFile.cpp 22453 2013-10-23 18:12:16Z gpotts $
10 //
11 
12 #include <iomanip>
13 #include <ossimShapeFile.h>
14 
15 RTTI_DEF1(ossimShapeFile, "ossimShapeFile", ossimObject);
16 
17 using namespace std;
19 {
20  if(rhs.isLoaded())
21  {
22  double minx, miny, minz, minm;
23  double maxx, maxy, maxz, maxm;
24  ossim_uint32 i = 0;
25 
26  rhs.getBounds(minx, miny, minz, minm,
27  maxx, maxy, maxz, maxm);
28 
29  out << std::setw(15) << setiosflags(std::ios::left)<<"type:"<<rhs.getTypeByName() << std::endl
30  << std::setw(15) << setiosflags(std::ios::left)<<"id:"<<rhs.getId()<<std::endl
31  << std::setw(15) << setiosflags(std::ios::left)<<"minx:"<<minx <<std::endl
32  << std::setw(15) << setiosflags(std::ios::left)<<"miny:"<<miny <<std::endl
33  << std::setw(15) << setiosflags(std::ios::left)<<"minz:"<<minz <<std::endl
34  << std::setw(15) << setiosflags(std::ios::left)<<"minm:"<<minm <<std::endl
35  << std::setw(15) << setiosflags(std::ios::left)<<"maxx:"<<maxx <<std::endl
36  << std::setw(15) << setiosflags(std::ios::left)<<"maxy:"<<maxy <<std::endl
37  << std::setw(15) << setiosflags(std::ios::left)<<"maxz:"<<maxz <<std::endl
38  << std::setw(15) << setiosflags(std::ios::left)<<"maxm:"<<maxm <<std::endl
39  << std::setw(15) << setiosflags(std::ios::left)<<"parts:"<<rhs.getNumberOfParts()<<std::endl
40  << std::setw(15) << setiosflags(std::ios::left)<<"vertices:"<<rhs.getNumberOfVertices();
41 
42  if(rhs.getNumberOfParts())
43  {
44  out << std::endl;
45  for(i = 0; i < rhs.getNumberOfParts()-1; ++i)
46  {
47  ossimString s1 = "part start ";
48  ossimString s2 = "part type ";
49  s1 += (ossimString::toString(i+1)+":");
50  s2 += (ossimString::toString(i+1)+":");
51 
52  out << std::setw(15) << setiosflags(std::ios::left) << s1.c_str() << rhs.theShape->panPartStart[i]<<std::endl;
53  out << std::setw(15) << setiosflags(std::ios::left) << s2.c_str() << SHPPartTypeName(rhs.theShape->panPartType[i])<<std::endl;
54  }
55  ossimString s1 = "part start ";
56  ossimString s2 = "part type ";
57 
58  s1 += (ossimString::toString(rhs.getNumberOfParts())+":");
59  out << std::setw(15) << setiosflags(std::ios::left) << s1.c_str() << rhs.theShape->panPartStart[i]<<std::endl;
60  out << std::setw(15) << setiosflags(std::ios::left) << s2.c_str() << SHPPartTypeName(rhs.theShape->panPartType[i]);
61  }
62 
63  out << std::setw(0);
64 
65  }
66  return out;
67 }
68 
70  :theShape((SHPObject*)NULL),
71  theIndex(-1)
72 {
73 }
74 
76 {
77  if(theShape)
78  {
80  theShape = NULL;
81  }
82 }
83 
85  ossimCoordSysOrientMode orient)const
86 {
87  double minx, miny, maxx, maxy;
88 
89  if(theShape)
90  {
91  getBounds(minx, miny, maxx, maxy);
92 
93  if(orient == OSSIM_RIGHT_HANDED)
94  {
95  result = ossimDrect(minx, maxy, maxx, miny, orient);
96  }
97  else
98  {
99  result = ossimDrect(minx, miny, maxx, maxy, orient);
100  }
101  }
102  else
103  {
104  result = ossimDrect(0,0,0,0,orient);
105  result.makeNan();
106  }
107 }
108 
110 {
111  if(theShape)
112  {
114  theShape = NULL;
115  }
116 
117  theShape = obj;
118 }
119 
121 {
122  return (theShape!=NULL);
123 }
124 
126 {
127  return theIndex;
128 }
129 
131 {
132  if(theShape)
133  {
134  return theShape->nShapeId;
135  }
136 
137  return -1;
138 }
139 
141  long shapeRecord)
142 {
143  if(theShape)
144  {
146  theShape = NULL;
147  }
148  if(shapeFile.isOpen())
149  {
150  theShape = SHPReadObject(shapeFile.getHandle(),
151  shapeRecord);
152  theIndex = shapeRecord;
153  }
154  else
155  {
156  theIndex = -1;
157  }
158  return (theShape != (SHPObject*)NULL);
159 }
160 
162 {
163  if(theShape)
164  {
165  return theShape->nParts;
166  }
167 
168  return 0;
169 }
170 
172 {
173  if(theShape)
174  {
175  return theShape->nVertices;
176  }
177 
178  return 0;
179 }
180 
182  double& minX, double& minY, double& minZ, double& minM,
183  double& maxX, double& maxY, double& maxZ, double& maxM)const
184 {
185  if(theShape)
186  {
187  minX = theShape->dfXMin;
188  minY = theShape->dfYMin;
189  minZ = theShape->dfZMin;
190  minM = theShape->dfMMin;
191  maxX = theShape->dfXMax;
192  maxY = theShape->dfYMax;
193  maxZ = theShape->dfZMax;
194  maxM = theShape->dfMMax;
195  }
196  else
197  {
198  minX = minY = minZ = minM =
199  maxX = maxY = maxZ = maxM = ossim::nan();
200  }
201 }
202 
203 void ossimShapeObject::getBounds(double& minX, double& minY,
204  double& maxX, double& maxY)const
205 {
206  if(theShape)
207  {
208  minX = theShape->dfXMin;
209  minY = theShape->dfYMin;
210  maxX = theShape->dfXMax;
211  maxY = theShape->dfYMax;
212  }
213  else
214  {
215  minX = minY =
216  maxX = maxY = ossim::nan();
217  }
218 }
219 
221  ossimCoordSysOrientMode orient)const
222 {
223  ossimDrect result;
224 
225  getBoundingRect(result, orient);
226 
227  return result;
228 }
229 
231 {
232  if(theShape)
233  {
234  return theShape->nSHPType;
235  }
236  return SHPT_NULL;
237 }
238 
240 {
241  if(theShape)
242  {
244  }
245  return "unknown";
246 }
247 
249 {
250  if((partIndex > getNumberOfParts())||
251  (!theShape))
252  {
253  return -1;
254  }
255 
256  return theShape->panPartType[partIndex];
257 }
258 
260 {
261  if((partIndex > getNumberOfParts())||
262  (!theShape))
263  {
264  }
265  switch(theShape->panPartType[partIndex])
266  {
267  case SHPP_TRISTRIP:
268  {
269  ossimString("tristrip");
270  break;
271  }
272  case SHPP_TRIFAN:
273  {
274  ossimString("trifan");
275  break;
276  }
277  case SHPP_OUTERRING:
278  {
279  ossimString("outerring");
280  break;
281  }
282  case SHPP_INNERRING:
283  {
284  ossimString("innerring");
285  break;
286  }
287  case SHPP_RING:
288  {
289  ossimString("ring");
290  break;
291  }
292  }
293  return ossimString("unknown");
294 }
295 
297 {
298  return theShape;
299 }
300 
302 {
303  return theShape;
304 }
305 
307 {
308  rhs.print(out);
309 
310  return out;
311 }
312 
313 
315  :theHandle(NULL)
316 {
317 }
318 
320 {
321  close();
322 }
323 
325 {
326  if(theHandle)
327  {
329  theHandle = NULL;
330  }
331 }
332 
334  const ossimString& flags)
335 {
336  if(isOpen()) close();
337 
338  theHandle = SHPOpen( file.c_str(),
339  flags.c_str());
340  if(isOpen())
341  {
342  theFilename = file;
343  }
344 
345  return (theHandle != NULL);
346 }
347 
349 {
350  return (theHandle!=NULL);
351 }
352 
354 {
355  return theHandle;
356 }
357 
359 {
360  return theHandle;
361 }
362 
364 {
365  if(theHandle)
366  {
367  return theHandle->nRecords;
368  }
369  return 0;
370 }
371 
373  double& minX, double& minY, double& minZ, double& minM,
374  double& maxX, double& maxY, double& maxZ, double& maxM)const
375 {
376  if(theHandle)
377  {
378  minX = theHandle->adBoundsMin[0];
379  minY = theHandle->adBoundsMin[1];
380  minZ = theHandle->adBoundsMin[2];
381  minM = theHandle->adBoundsMin[3];
382  maxX = theHandle->adBoundsMax[0];
383  maxY = theHandle->adBoundsMax[1];
384  maxZ = theHandle->adBoundsMax[2];
385  maxM = theHandle->adBoundsMax[3];
386  }
387  else
388  {
389  minX = minY = minZ = minM =
390  maxX = maxY = maxZ = maxM = ossim::nan();
391  }
392 }
393 
394 void ossimShapeFile::getBounds(double& minX, double& minY,
395  double& maxX, double& maxY)const
396 {
397  if(theHandle)
398  {
399  minX = theHandle->adBoundsMin[0];
400  minY = theHandle->adBoundsMin[1];
401  maxX = theHandle->adBoundsMax[0];
402  maxY = theHandle->adBoundsMax[1];
403  }
404  else
405  {
406  minX = minY = maxX = maxY = ossim::nan();
407  }
408 }
409 
411 {
412  if(isOpen())
413  {
414  out << std::setw(15) << setiosflags(std::ios::left)<<"Shp filename:" << theFilename << std::endl;
415  out << std::setw(15) << setiosflags(std::ios::left)<<"Record count:" << theHandle->nRecords << std::endl;
416  out << std::setw(15) << setiosflags(std::ios::left)<<"File type:" << getShapeTypeString().c_str() << std::endl;
417  out << std::setw(15) << setiosflags(std::ios::left)<<"minx:" << theHandle->adBoundsMin[0] << std::endl;
418  out << std::setw(15) << setiosflags(std::ios::left)<<"miny:" << theHandle->adBoundsMin[1] << std::endl;
419  out << std::setw(15) << setiosflags(std::ios::left)<<"minz:" << theHandle->adBoundsMin[2] << std::endl;
420  out << std::setw(15) << setiosflags(std::ios::left)<<"minm:" << theHandle->adBoundsMin[3] << std::endl;
421  out << std::setw(15) << setiosflags(std::ios::left)<<"maxx:" << theHandle->adBoundsMax[0] << std::endl;
422  out << std::setw(15) << setiosflags(std::ios::left)<<"maxy:" << theHandle->adBoundsMax[1] << std::endl;
423  out << std::setw(15) << setiosflags(std::ios::left)<<"maxz:" << theHandle->adBoundsMax[2] << std::endl;
424  out << std::setw(15) << setiosflags(std::ios::left)<<"maxm:" << theHandle->adBoundsMax[3] << std::endl;
425 
426  ossimShapeObject shape;
427 
428  if(theHandle->nRecords)
429  {
430  out << std::setw(30) << std::setfill('_') << "" << std::setfill(' ')<<std::endl;
431  }
432  for(int i=0; i < theHandle->nRecords; ++i)
433  {
434  if(shape.loadShape(*this, i))
435  {
436  out << shape << std::endl;
437  out << std::setw(30) << std::setfill('_') << ""<<std::setfill(' ')<<std::endl;
438  }
439  }
440  }
441  return out;
442 }
443 
444 
446 {
447  if(theHandle)
448  {
450  }
451  return "";
452 }
453 
455  ossimCoordSysOrientMode orient)const
456 {
457  double minx, miny, maxx, maxy;
458 
459  getBounds(minx, miny, maxx, maxy);
460  if(orient == OSSIM_RIGHT_HANDED)
461  {
462  result = ossimDrect(minx, maxy, maxx, miny, orient);
463  }
464  else
465  {
466  result = ossimDrect(minx, miny, maxx, maxy, orient);
467  }
468 }
469 
471  ossimCoordSysOrientMode orient)const
472 {
473  ossimDrect result;
474 
475  getBoundingRect(result, orient);
476 
477  return result;
478 }
479 
481 {
482  return theFilename;
483 }
void makeNan()
Definition: ossimDrect.h:388
virtual std::ostream & print(std::ostream &out) const
Generic print method.
int getPartType(ossim_uint32 partIndex) const
ossimFilename theFilename
ossimString getTypeByName() const
double dfZMin
Definition: shapefil.h:295
ossim_uint32 getNumberOfParts() const
void setShape(SHPObject *obj)
virtual SHPHandle getHandle()
int nSHPType
Definition: shapefil.h:279
std::ostream & operator<<(std::ostream &out, const ossimShapeObject &rhs)
long getId() const
double nan()
Method to return ieee floating point double precision NAN.
Definition: ossimCommon.h:135
static ossimString toString(bool aValue)
Numeric to string methods.
double dfYMax
Definition: shapefil.h:299
double dfZMax
Definition: shapefil.h:300
virtual bool open(const ossimFilename &file, const ossimString &flags=ossimString("rb"))
int nParts
Definition: shapefil.h:283
int nShapeType
Definition: shapefil.h:222
void SHPAPI_CALL SHPClose(SHPHandle hSHP)
double dfXMax
Definition: shapefil.h:298
int * panPartType
Definition: shapefil.h:285
RTTI_DEF1(ossimShapeFile, "ossimShapeFile", ossimObject)
ossimCoordSysOrientMode
SHPObject * theShape
#define SHPP_TRIFAN
Definition: shapefil.h:267
int nVertices
Definition: shapefil.h:287
bool loadShape(const ossimShapeFile &shapeFile, long shapeRecord)
ossim_uint32 getNumberOfVertices() const
double adBoundsMax[4]
Definition: shapefil.h:232
unsigned int ossim_uint32
double dfMMax
Definition: shapefil.h:301
SHPHandle theHandle
virtual ossimString getShapeTypeString() const
#define SHPP_RING
Definition: shapefil.h:271
#define SHPP_TRISTRIP
Definition: shapefil.h:266
#define SHPP_INNERRING
Definition: shapefil.h:269
ossimString getPartByName(ossim_uint32 partIndex) const
SHPObject SHPAPI_CALL1 * SHPReadObject(SHPHandle hSHP, int iShape);int SHPAPI_CALL SHPWriteObject(SHPHandle hSHP, int iShape, SHPObject *psObject
double dfXMin
Definition: shapefil.h:293
int * panPartStart
Definition: shapefil.h:284
void getBoundingRect(ossimDrect &result, ossimCoordSysOrientMode orient=OSSIM_RIGHT_HANDED) const
virtual void close()
double dfMMin
Definition: shapefil.h:296
#define SHPP_OUTERRING
Definition: shapefil.h:268
double adBoundsMin[4]
Definition: shapefil.h:231
void getBounds(double &minX, double &minY, double &minZ, double &minM, double &maxX, double &maxY, double &maxZ, double &maxM) const
double dfYMin
Definition: shapefil.h:294
virtual long getNumberOfShapes() const
#define SHPT_NULL
Definition: shapefil.h:245
void getBounds(double &minX, double &minY, double &minZ, double &minM, double &maxX, double &maxY, double &maxZ, double &maxM) const
SHPHandle SHPAPI_CALL SHPOpen(const char *pszShapeFile, const char *pszAccess)
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
virtual ~ossimShapeFile()
SHPObject * getShapeObject()
void getBoundingRect(ossimDrect &result, ossimCoordSysOrientMode orient=OSSIM_RIGHT_HANDED) const
void SHPAPI_CALL SHPDestroyObject(SHPObject *psObject)
int nRecords
Definition: shapefil.h:226
bool isLoaded() const
long getIndex() const
bool isOpen() const
const ossimFilename & getFilename() const
std::basic_ostream< char > ostream
Base class for char output streams.
Definition: ossimIosFwd.h:23
int nShapeId
Definition: shapefil.h:281
const char SHPAPI_CALL1 * SHPTypeName(int nSHPType);const char SHPAPI_CALL1(*) SHPPartTypeName(int nPartType