Isis 3 Programmer Reference
GisTopology.cpp
1 
6 /* SPDX-License-Identifier: CC0-1.0 */
7 #include "GisTopology.h"
8 
9 // std library
10 #include <cstdio>
11 
12 // Qt library
13 #include <QByteArray>
14 #include <QCoreApplication>
15 #include <QString>
16 
17 // geos library
18 #include <geos_c.h>
19 
20 // other ISIS
21 #include "IException.h"
22 
23 using namespace std;
24 
25 namespace Isis {
26 
31  GisTopology *GisTopology::m_gisfactory = 0;
32 
33 
39  GisTopology::GisTopology() : m_WKTreader(0), m_WKTwriter(0),
40  m_WKBreader(0), m_WKBwriter(0) {
41  geosInit();
42  // This ensures this singleton is shut down when the application exists,
43  // so the GEOS system can be shut down cleanly.
44  qAddPostRoutine(dieAtExit);
45  }
46 
47 
53  if (m_WKTreader) {
54  GEOSWKTReader_destroy(m_WKTreader);
55  m_WKTreader = 0;
56  }
57 
58  if (m_WKTwriter) {
59  GEOSWKTWriter_destroy(m_WKTwriter);
60  m_WKTwriter = 0;
61  }
62 
63  if (m_WKBreader) {
64  GEOSWKBReader_destroy(m_WKBreader);
65  m_WKBreader = 0;
66  }
67 
68  if (m_WKBwriter) {
69  GEOSWKBWriter_destroy(m_WKBwriter);
70  m_WKBwriter = 0;
71  }
72 
73  geosFinish();
74  }
75 
76 
84  if (!m_gisfactory) {
85  m_gisfactory = new GisTopology();
86  }
87  return (m_gisfactory);
88  }
89 
90 
100  GEOSGeometry *GisTopology::geomFromWKB(const QString &wkb) {
101  #if 0
102  QByteArray wkb_data = wkb.toLatin1();
103  const unsigned char *footy = reinterpret_cast<const unsigned char *> (wkb_data.data());
104  GEOSGeometry *geom = GEOSWKBReader_readHEX(wkbReader(), footy, wkb.size());
105  #else
106  GEOSGeometry *geom = GEOSWKBReader_readHEX(wkbReader(),
107  reinterpret_cast<const unsigned char *> (wkb.toLatin1().data()),
108  wkb.size());
109  #endif
110  if (!geom) {
111  QString mess = "Unable convert the given WKB string [" + wkb + "] to a GEOSGeometry";
112  throw IException(IException::Programmer, mess, _FILEINFO_);
113 
114  }
115  return (geom);
116  }
117 
118 
128  GEOSGeometry *GisTopology::geomFromWKT(const QString &wkt) {
129  QByteArray wkt_data = wkt.toLatin1();
130  const char *footy = wkt_data.data();
131  GEOSGeometry *geom = GEOSWKTReader_read(wktReader(), footy);
132  if (!geom) {
133  QString mess = "Unable convert the given WKT string [" + wkt + "] to a GEOSGeometry";
134  throw IException(IException::Programmer, mess, _FILEINFO_);
135 
136  }
137  return (geom);
138  }
139 
140 
148  GEOSGeometry *GisTopology::clone(const GEOSGeometry *geom) const {
149  if (!geom) return (0);
150  return (GEOSGeom_clone(geom));
151  }
152 
153 
163  const GEOSPreparedGeometry *GisTopology::preparedGeometry(const GEOSGeometry *geom) const {
164  const GEOSPreparedGeometry *ppgeom = GEOSPrepare(geom);
165  if (!ppgeom) {
167  "Unable convert the given GEOSGeometry to a GEOSPreparedGeometry",
168  _FILEINFO_);
169 
170  }
171  return (ppgeom);
172  }
173 
174 
186  QString GisTopology::wkt(const GEOSGeometry *geom,
187  const GisTopology::Disposition &disp) {
188  char *wkt_h = GEOSWKTWriter_write(wktWriter(), geom);
189  QString thegeom = QString::fromLatin1(reinterpret_cast<const char *> (wkt_h));
190 
191  if (disp == DestroyGeometry) {
192  destroy(geom);
193  }
194  destroy(wkt_h);
195 
196  return (thegeom);
197  }
198 
199 
212  QString GisTopology::wkb(const GEOSGeometry *geom,
213  const GisTopology::Disposition &disp) {
214  size_t length;
215  unsigned char *wkt_h = GEOSWKBWriter_writeHEX(wkbWriter(), geom, &length);
216  QString thegeom = QString::fromLatin1(reinterpret_cast<const char *> (wkt_h), length);
217 
218  if (disp == DestroyGeometry) {
219  destroy(geom);
220  }
221  destroy(wkt_h);
222 
223  return (thegeom);
224  }
225 
226 
232  void GisTopology::destroy(GEOSGeometry *geom) const {
233  if (geom) {
234  GEOSGeom_destroy(geom);
235  }
236  return;
237  }
238 
239 
245  void GisTopology::destroy(const GEOSGeometry *geom) const {
246  if (geom) {
247  destroy(const_cast<GEOSGeometry *> (geom));
248  }
249  return;
250  }
251 
252 
258  void GisTopology::destroy(const GEOSPreparedGeometry *geom) const {
259  if (geom) {
260  GEOSPreparedGeom_destroy(geom);
261  }
262  return;
263  }
264 
265 
271  void GisTopology::destroy(GEOSCoordSequence *sequence) const {
272  if (sequence) {
273  GEOSCoordSeq_destroy(sequence);
274  }
275  return;
276  }
277 
278 
284  void GisTopology::destroy(const char *geos_text) const {
285  if (geos_text) {
286  GEOSFree(const_cast<char *> (geos_text));
287  }
288  return;
289  }
290 
291 
297  void GisTopology::destroy(const unsigned char *geos_text) const {
298  if (geos_text) {
299  GEOSFree(const_cast<unsigned char *> (geos_text));
300  }
301  return;
302  }
303 
304 
310  }
311 
312 
318  finishGEOS();
319  }
320 
321 
329  void GisTopology::notice(const char *fmt, ...) {
330  va_list ap;
331  va_start(ap, fmt);
332  char buffer[1024];
333  vsprintf(buffer, fmt, ap);
334  va_end(ap);
335  throw IException(IException::Programmer, buffer, _FILEINFO_);
336  }
337 
338 
346  void GisTopology::error(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);
352  throw IException(IException::Programmer, buffer, _FILEINFO_);
353  }
354 
355 
362  GEOSWKTReader *GisTopology::wktReader() {
363  if (!m_WKTreader) {
364  m_WKTreader = GEOSWKTReader_create();
365  }
366  return (m_WKTreader);
367  }
368 
369 
376  GEOSWKTWriter *GisTopology::wktWriter() {
377  if (!m_WKTwriter) {
378  m_WKTwriter = GEOSWKTWriter_create();
379  }
380  return (m_WKTwriter);
381  }
382 
383 
390  GEOSWKBReader *GisTopology::wkbReader() {
391  if (!m_WKBreader) {
392  m_WKBreader = GEOSWKBReader_create();
393  }
394  return (m_WKBreader);
395  }
396 
397 
404  GEOSWKBWriter *GisTopology::wkbWriter() {
405  if (!m_WKBwriter) {
406  m_WKBwriter = GEOSWKBWriter_create();
407  }
408  return (m_WKBwriter);
409  }
410 
423  delete m_gisfactory;
424  m_gisfactory = 0;
425  return;
426  }
427 
428 } // namespace Isis
Isis::GisTopology::preparedGeometry
const GEOSPreparedGeometry * preparedGeometry(const GEOSGeometry *geom) const
Gets a GEOSPreparedGeometry from the given GEOSGeometry.
Definition: GisTopology.cpp:163
Isis::GisTopology::dieAtExit
static void dieAtExit()
Exit termination routine.
Definition: GisTopology.cpp:422
Isis::GisTopology::m_WKTreader
GEOSWKTReader * m_WKTreader
A GEOS library parser for well-known text format.
Definition: GisTopology.h:88
Isis::GisTopology
This class models GIS topology.
Definition: GisTopology.h:34
Isis::GisTopology::wkbReader
GEOSWKBReader * wkbReader()
Accessor for the GEOS well-known binary reader.
Definition: GisTopology.cpp:390
Isis::GisTopology::wktWriter
GEOSWKTWriter * wktWriter()
Accessor for the GEOS well-known text writer.
Definition: GisTopology.cpp:376
Isis::GisTopology::notice
static void notice(const char *fmt,...)
A static method for handling errors.
Definition: GisTopology.cpp:329
Isis::GisTopology::DestroyGeometry
@ DestroyGeometry
Destroy the geometry.
Definition: GisTopology.h:41
Isis::GisTopology::clone
GEOSGeometry * clone(const GEOSGeometry *geom) const
Clones the given GEOSGeometry pointer.
Definition: GisTopology.cpp:148
Isis::GisTopology::Disposition
Disposition
Enumeration to indicate whether the geometry should be preserved.
Definition: GisTopology.h:40
Isis::GisTopology::wktReader
GEOSWKTReader * wktReader()
Accessor for the GEOS well-known text reader.
Definition: GisTopology.cpp:362
Isis::GisTopology::geomFromWKT
GEOSGeometry * geomFromWKT(const QString &wkt)
Reads in the geometry from the given well-known text formatted string.
Definition: GisTopology.cpp:128
Isis::GisTopology::instance
static GisTopology * instance()
Gets the singleton instance of this class.
Definition: GisTopology.cpp:83
Isis::GisTopology::geomFromWKB
GEOSGeometry * geomFromWKB(const QString &wkb)
Reads in the geometry from the given well-known binary formatted string.
Definition: GisTopology.cpp:100
Isis::GisTopology::destroy
void destroy(GEOSGeometry *geom) const
Destroys the given GEOS geometry.
Definition: GisTopology.cpp:232
Isis::GisTopology::~GisTopology
~GisTopology()
Destroy the GisTopology object.
Definition: GisTopology.cpp:52
Isis::GisTopology::m_gisfactory
static GisTopology * m_gisfactory
A static member variable representing the GIS factory.
Definition: GisTopology.h:87
Isis::IException
Isis exception class.
Definition: IException.h:91
Isis::GisTopology::m_WKBwriter
GEOSWKBWriter * m_WKBwriter
A GEOS library writer for well-known binary format.
Definition: GisTopology.h:91
Isis::GisTopology::m_WKTwriter
GEOSWKTWriter * m_WKTwriter
A GEOS library writer for well-known text format.
Definition: GisTopology.h:89
Isis::IException::Programmer
@ Programmer
This error is for when a programmer made an API call that was illegal.
Definition: IException.h:146
std
Namespace for the standard library.
Isis::GisTopology::wkbWriter
GEOSWKBWriter * wkbWriter()
Accessor for the GEOS well-known binary writer.
Definition: GisTopology.cpp:404
Isis::GisTopology::error
static void error(const char *fmt,...)
A static method for handling errors.
Definition: GisTopology.cpp:346
Isis::GisTopology::GisTopology
GisTopology()
Private default constructor so that this class is only instatiated through the instance() method.
Definition: GisTopology.cpp:39
Isis::GisTopology::wkt
QString wkt(const GEOSGeometry *geom, const Disposition &disp=PreserveGeometry)
Writes a well-known text string from the given geometry.
Definition: GisTopology.cpp:186
Isis::GisTopology::geosInit
void geosInit()
Initializes the GEOS C API.
Definition: GisTopology.cpp:308
Isis::GisTopology::geosFinish
void geosFinish()
Shuts down the GEOS C API.
Definition: GisTopology.cpp:317
Isis
This is free and unencumbered software released into the public domain.
Definition: Apollo.h:16
Isis::GisTopology::m_WKBreader
GEOSWKBReader * m_WKBreader
A GEOS library parser for well-known binary format.
Definition: GisTopology.h:90
Isis::GisTopology::wkb
QString wkb(const GEOSGeometry *geom, const Disposition &disp=PreserveGeometry)
Writes a well-known binary string from the given geometry.
Definition: GisTopology.cpp:212