Isis 3 Programmer Reference
GisTopology.cpp
Go to the documentation of this file.
1 
24 #include "GisTopology.h"
25 
26 // std library
27 #include <cstdio>
28 
29 // Qt library
30 #include <QByteArray>
31 #include <QCoreApplication>
32 #include <QString>
33 
34 // geos library
35 #include <geos_c.h>
36 
37 // other ISIS
38 #include "IException.h"
39 
40 using namespace std;
41 
42 namespace Isis {
43 
48  GisTopology *GisTopology::m_gisfactory = 0;
49 
50 
56  GisTopology::GisTopology() : m_WKTreader(0), m_WKTwriter(0),
57  m_WKBreader(0), m_WKBwriter(0) {
58  geosInit();
59  // This ensures this singleton is shut down when the application exists,
60  // so the GEOS system can be shut down cleanly.
61  qAddPostRoutine(dieAtExit);
62  }
63 
64 
70  if (m_WKTreader) {
71  GEOSWKTReader_destroy(m_WKTreader);
72  m_WKTreader = 0;
73  }
74 
75  if (m_WKTwriter) {
76  GEOSWKTWriter_destroy(m_WKTwriter);
77  m_WKTwriter = 0;
78  }
79 
80  if (m_WKBreader) {
81  GEOSWKBReader_destroy(m_WKBreader);
82  m_WKBreader = 0;
83  }
84 
85  if (m_WKBwriter) {
86  GEOSWKBWriter_destroy(m_WKBwriter);
87  m_WKBwriter = 0;
88  }
89 
90  geosFinish();
91  }
92 
93 
101  if (!m_gisfactory) {
102  m_gisfactory = new GisTopology();
103  }
104  return (m_gisfactory);
105  }
106 
107 
117  GEOSGeometry *GisTopology::geomFromWKB(const QString &wkb) {
118  #if 0
119  QByteArray wkb_data = wkb.toLatin1();
120  const unsigned char *footy = reinterpret_cast<const unsigned char *> (wkb_data.data());
121  GEOSGeometry *geom = GEOSWKBReader_readHEX(wkbReader(), footy, wkb.size());
122  #else
123  GEOSGeometry *geom = GEOSWKBReader_readHEX(wkbReader(),
124  reinterpret_cast<const unsigned char *> (wkb.toLatin1().data()),
125  wkb.size());
126  #endif
127  if (!geom) {
128  QString mess = "Unable convert the given WKB string [" + wkb + "] to a GEOSGeometry";
130 
131  }
132  return (geom);
133  }
134 
135 
145  GEOSGeometry *GisTopology::geomFromWKT(const QString &wkt) {
146  QByteArray wkt_data = wkt.toLatin1();
147  const char *footy = wkt_data.data();
148  GEOSGeometry *geom = GEOSWKTReader_read(wktReader(), footy);
149  if (!geom) {
150  QString mess = "Unable convert the given WKT string [" + wkt + "] to a GEOSGeometry";
152 
153  }
154  return (geom);
155  }
156 
157 
165  GEOSGeometry *GisTopology::clone(const GEOSGeometry *geom) const {
166  if (!geom) return (0);
167  return (GEOSGeom_clone(geom));
168  }
169 
170 
180  const GEOSPreparedGeometry *GisTopology::preparedGeometry(const GEOSGeometry *geom) const {
181  const GEOSPreparedGeometry *ppgeom = GEOSPrepare(geom);
182  if (!ppgeom) {
184  "Unable convert the given GEOSGeometry to a GEOSPreparedGeometry",
185  _FILEINFO_);
186 
187  }
188  return (ppgeom);
189  }
190 
191 
203  QString GisTopology::wkt(const GEOSGeometry *geom,
204  const GisTopology::Disposition &disp) {
205  char *wkt_h = GEOSWKTWriter_write(wktWriter(), geom);
206  QString thegeom = QString::fromLatin1(reinterpret_cast<const char *> (wkt_h));
207 
208  if (disp == DestroyGeometry) {
209  destroy(geom);
210  }
211  destroy(wkt_h);
212 
213  return (thegeom);
214  }
215 
216 
229  QString GisTopology::wkb(const GEOSGeometry *geom,
230  const GisTopology::Disposition &disp) {
231  size_t length;
232  unsigned char *wkt_h = GEOSWKBWriter_writeHEX(wkbWriter(), geom, &length);
233  QString thegeom = QString::fromLatin1(reinterpret_cast<const char *> (wkt_h), length);
234 
235  if (disp == DestroyGeometry) {
236  destroy(geom);
237  }
238  destroy(wkt_h);
239 
240  return (thegeom);
241  }
242 
243 
249  void GisTopology::destroy(GEOSGeometry *geom) const {
250  if (geom) {
251  GEOSGeom_destroy(geom);
252  }
253  return;
254  }
255 
256 
262  void GisTopology::destroy(const GEOSGeometry *geom) const {
263  if (geom) {
264  destroy(const_cast<GEOSGeometry *> (geom));
265  }
266  return;
267  }
268 
269 
275  void GisTopology::destroy(const GEOSPreparedGeometry *geom) const {
276  if (geom) {
277  GEOSPreparedGeom_destroy(geom);
278  }
279  return;
280  }
281 
282 
288  void GisTopology::destroy(GEOSCoordSequence *sequence) const {
289  if (sequence) {
290  GEOSCoordSeq_destroy(sequence);
291  }
292  return;
293  }
294 
295 
301  void GisTopology::destroy(const char *geos_text) const {
302  if (geos_text) {
303  GEOSFree(const_cast<char *> (geos_text));
304  }
305  return;
306  }
307 
308 
314  void GisTopology::destroy(const unsigned char *geos_text) const {
315  if (geos_text) {
316  GEOSFree(const_cast<unsigned char *> (geos_text));
317  }
318  return;
319  }
320 
321 
327  }
328 
329 
335  finishGEOS();
336  }
337 
338 
346  void GisTopology::notice(const char *fmt, ...) {
347  va_list ap;
348  va_start(ap, fmt);
349  char buffer[1024];
350  vsprintf(buffer, fmt, ap);
351  va_end(ap);
353  }
354 
355 
363  void GisTopology::error(const char *fmt, ...) {
364  va_list ap;
365  va_start(ap, fmt);
366  char buffer[1024];
367  vsprintf(buffer, fmt, ap);
368  va_end(ap);
370  }
371 
372 
379  GEOSWKTReader *GisTopology::wktReader() {
380  if (!m_WKTreader) {
381  m_WKTreader = GEOSWKTReader_create();
382  }
383  return (m_WKTreader);
384  }
385 
386 
393  GEOSWKTWriter *GisTopology::wktWriter() {
394  if (!m_WKTwriter) {
395  m_WKTwriter = GEOSWKTWriter_create();
396  }
397  return (m_WKTwriter);
398  }
399 
400 
407  GEOSWKBReader *GisTopology::wkbReader() {
408  if (!m_WKBreader) {
409  m_WKBreader = GEOSWKBReader_create();
410  }
411  return (m_WKBreader);
412  }
413 
414 
421  GEOSWKBWriter *GisTopology::wkbWriter() {
422  if (!m_WKBwriter) {
423  m_WKBwriter = GEOSWKBWriter_create();
424  }
425  return (m_WKBwriter);
426  }
427 
440  delete m_gisfactory;
441  m_gisfactory = 0;
442  return;
443  }
444 
445 } // namespace Isis
GEOSGeometry * geomFromWKB(const QString &wkb)
Reads in the geometry from the given well-known binary formatted string.
GEOSWKBWriter * m_WKBwriter
A GEOS library writer for well-known binary format.
Definition: GisTopology.h:108
GEOSGeometry * geomFromWKT(const QString &wkt)
Reads in the geometry from the given well-known text formatted string.
GEOSWKBWriter * wkbWriter()
Accessor for the GEOS well-known binary writer.
Destroy the geometry.
Definition: GisTopology.h:58
Namespace for the standard library.
GEOSWKTWriter * m_WKTwriter
A GEOS library writer for well-known text format.
Definition: GisTopology.h:106
static GisTopology * m_gisfactory
A static member variable representing the GIS factory.
Definition: GisTopology.h:104
void geosInit()
Initializes the GEOS C API.
void geosFinish()
Shuts down the GEOS C API.
This error is for when a programmer made an API call that was illegal.
Definition: IException.h:162
GisTopology()
Private default constructor so that this class is only instatiated through the instance() method...
Definition: GisTopology.cpp:56
QString wkb(const GEOSGeometry *geom, const Disposition &disp=PreserveGeometry)
Writes a well-known binary string from the given geometry.
QString wkt(const GEOSGeometry *geom, const Disposition &disp=PreserveGeometry)
Writes a well-known text string from the given geometry.
static void error(const char *fmt,...)
A static method for handling errors.
#define _FILEINFO_
Macro for the filename and line number.
Definition: IException.h:40
GEOSWKBReader * wkbReader()
Accessor for the GEOS well-known binary reader.
const GEOSPreparedGeometry * preparedGeometry(const GEOSGeometry *geom) const
Gets a GEOSPreparedGeometry from the given GEOSGeometry.
GEOSWKBReader * m_WKBreader
A GEOS library parser for well-known binary format.
Definition: GisTopology.h:107
This class models GIS topology.
Definition: GisTopology.h:51
static void dieAtExit()
Exit termination routine.
GEOSWKTWriter * wktWriter()
Accessor for the GEOS well-known text writer.
static void notice(const char *fmt,...)
A static method for handling errors.
GEOSWKTReader * m_WKTreader
A GEOS library parser for well-known text format.
Definition: GisTopology.h:105
Disposition
Enumeration to indicate whether the geometry should be preserved.
Definition: GisTopology.h:57
Isis exception class.
Definition: IException.h:107
Namespace for ISIS/Bullet specific routines.
Definition: Apollo.h:31
GEOSGeometry * clone(const GEOSGeometry *geom) const
Clones the given GEOSGeometry pointer.
GEOSWKTReader * wktReader()
Accessor for the GEOS well-known text reader.
void destroy(GEOSGeometry *geom) const
Destroys the given GEOS geometry.
~GisTopology()
Destroy the GisTopology object.
Definition: GisTopology.cpp:69
static GisTopology * instance()
Gets the singleton instance of this class.