Fix "puts puts" typo in ruby macro (#625)
[geos.git] / capi / geos_ts_c.cpp
blob3dd2cbb448e178d7338946050163becd4f93e6dd
1 /************************************************************************
4 * C-Wrapper for GEOS library
6 * Copyright (C) 2010-2012 Sandro Santilli <strk@keybit.net>
7 * Copyright (C) 2005-2006 Refractions Research Inc.
9 * This is free software; you can redistribute and/or modify it under
10 * the terms of the GNU Lesser General Public Licence as published
11 * by the Free Software Foundation.
12 * See the COPYING file for more information.
14 * Author: Sandro Santilli <strk@keybit.net>
15 * Thread Safety modifications: Chuck Thibert <charles.thibert@ingres.com>
17 ***********************************************************************/
19 #include <geos/platform.h> // for FINITE
20 #include <geos/geom/Geometry.h>
21 #include <geos/geom/prep/PreparedGeometry.h>
22 #include <geos/geom/prep/PreparedGeometryFactory.h>
23 #include <geos/geom/GeometryCollection.h>
24 #include <geos/geom/Polygon.h>
25 #include <geos/geom/Point.h>
26 #include <geos/geom/MultiPoint.h>
27 #include <geos/geom/MultiLineString.h>
28 #include <geos/geom/MultiPolygon.h>
29 #include <geos/geom/LinearRing.h>
30 #include <geos/geom/LineString.h>
31 #include <geos/geom/PrecisionModel.h>
32 #include <geos/geom/GeometryFactory.h>
33 #include <geos/geom/CoordinateSequenceFactory.h>
34 #include <geos/geom/Coordinate.h>
35 #include <geos/geom/IntersectionMatrix.h>
36 #include <geos/geom/Envelope.h>
37 #include <geos/index/strtree/STRtree.h>
38 #include <geos/index/ItemVisitor.h>
39 #include <geos/io/WKTReader.h>
40 #include <geos/io/WKBReader.h>
41 #include <geos/io/WKTWriter.h>
42 #include <geos/io/WKBWriter.h>
43 #include <geos/algorithm/distance/DiscreteHausdorffDistance.h>
44 #include <geos/algorithm/CGAlgorithms.h>
45 #include <geos/algorithm/BoundaryNodeRule.h>
46 #include <geos/simplify/DouglasPeuckerSimplifier.h>
47 #include <geos/simplify/TopologyPreservingSimplifier.h>
48 #include <geos/noding/GeometryNoder.h>
49 #include <geos/operation/valid/IsValidOp.h>
50 #include <geos/operation/polygonize/Polygonizer.h>
51 #include <geos/operation/linemerge/LineMerger.h>
52 #include <geos/operation/overlay/OverlayOp.h>
53 #include <geos/operation/overlay/snap/GeometrySnapper.h>
54 #include <geos/operation/union/CascadedPolygonUnion.h>
55 #include <geos/operation/buffer/BufferOp.h>
56 #include <geos/operation/buffer/BufferParameters.h>
57 #include <geos/operation/buffer/BufferBuilder.h>
58 #include <geos/operation/relate/RelateOp.h>
59 #include <geos/operation/sharedpaths/SharedPathsOp.h>
60 #include <geos/linearref/LengthIndexedLine.h>
61 #include <geos/triangulate/DelaunayTriangulationBuilder.h>
62 #include <geos/util/IllegalArgumentException.h>
63 #include <geos/util/Interrupt.h>
64 #include <geos/util/UniqueCoordinateArrayFilter.h>
65 #include <geos/util/Machine.h>
66 #include <geos/version.h>
68 // This should go away
69 #include <cmath> // finite
70 #include <cstddef>
71 #include <cstdio>
72 #include <cstdlib>
73 #include <cstring>
74 #include <fstream>
75 #include <iostream>
76 #include <sstream>
77 #include <string>
78 #include <memory>
80 #ifdef _MSC_VER
81 #pragma warning(disable : 4099)
82 #endif
84 // Some extra magic to make type declarations in geos_c.h work -
85 // for cross-checking of types in header.
86 #define GEOSGeometry geos::geom::Geometry
87 #define GEOSPreparedGeometry geos::geom::prep::PreparedGeometry
88 #define GEOSCoordSequence geos::geom::CoordinateSequence
89 #define GEOSBufferParams geos::operation::buffer::BufferParameters
90 #define GEOSSTRtree geos::index::strtree::STRtree
91 #define GEOSWKTReader_t geos::io::WKTReader
92 #define GEOSWKTWriter_t geos::io::WKTWriter
93 #define GEOSWKBReader_t geos::io::WKBReader
94 #define GEOSWKBWriter_t geos::io::WKBWriter
96 #include "geos_c.h"
97 #include "../geos_svn_revision.h"
99 // Intentional, to allow non-standard C elements like C99 functions to be
100 // imported through C++ headers of C library, like <cmath>.
101 using namespace std;
103 /// Define this if you want operations triggering Exceptions to
104 /// be printed.
105 /// (will use the NOTIFY channel - only implemented for GEOSUnion so far)
107 #undef VERBOSE_EXCEPTIONS
109 #include <geos/export.h>
112 // import the most frequently used definitions globally
113 using geos::geom::Geometry;
114 using geos::geom::LineString;
115 using geos::geom::Polygon;
116 using geos::geom::CoordinateSequence;
117 using geos::geom::GeometryFactory;
119 using geos::io::WKTReader;
120 using geos::io::WKTWriter;
121 using geos::io::WKBReader;
122 using geos::io::WKBWriter;
124 using geos::operation::overlay::OverlayOp;
125 using geos::operation::overlay::overlayOp;
126 using geos::operation::geounion::CascadedPolygonUnion;
127 using geos::operation::buffer::BufferParameters;
128 using geos::operation::buffer::BufferBuilder;
129 using geos::util::IllegalArgumentException;
130 using geos::algorithm::distance::DiscreteHausdorffDistance;
132 typedef std::auto_ptr<Geometry> GeomAutoPtr;
134 typedef struct GEOSContextHandleInternal
136 const GeometryFactory *geomFactory;
137 GEOSMessageHandler NOTICE_MESSAGE;
138 GEOSMessageHandler ERROR_MESSAGE;
139 int WKBOutputDims;
140 int WKBByteOrder;
141 int initialized;
142 } GEOSContextHandleInternal_t;
144 // CAPI_ItemVisitor is used internally by the CAPI STRtree
145 // wrappers. It's defined here just to keep it out of the
146 // extern "C" block.
147 class CAPI_ItemVisitor : public geos::index::ItemVisitor {
148 GEOSQueryCallback callback;
149 void *userdata;
150 public:
151 CAPI_ItemVisitor (GEOSQueryCallback cb, void *ud)
152 : ItemVisitor(), callback(cb), userdata(ud) {}
153 void visitItem (void *item) { callback(item, userdata); }
157 //## PROTOTYPES #############################################
159 extern "C" const char GEOS_DLL *GEOSjtsport();
160 extern "C" char GEOS_DLL *GEOSasText(Geometry *g1);
163 namespace { // anonymous
165 char* gstrdup_s(const char* str, const std::size_t size)
167 char* out = static_cast<char*>(malloc(size + 1));
168 if (0 != out)
170 // as no strlen call necessary, memcpy may be faster than strcpy
171 std::memcpy(out, str, size + 1);
174 assert(0 != out);
176 // we haven't been checking allocation before ticket #371
177 if (0 == out)
179 throw(std::runtime_error("Failed to allocate memory for duplicate string"));
182 return out;
185 char* gstrdup(std::string const& str)
187 return gstrdup_s(str.c_str(), str.size());
190 } // namespace anonymous
192 extern "C" {
194 GEOSContextHandle_t
195 initGEOS_r(GEOSMessageHandler nf, GEOSMessageHandler ef)
197 GEOSContextHandleInternal_t *handle = 0;
198 void *extHandle = 0;
200 extHandle = malloc(sizeof(GEOSContextHandleInternal_t));
201 if (0 != extHandle)
203 handle = static_cast<GEOSContextHandleInternal_t*>(extHandle);
204 handle->NOTICE_MESSAGE = nf;
205 handle->ERROR_MESSAGE = ef;
206 handle->geomFactory = GeometryFactory::getDefaultInstance();
207 handle->WKBOutputDims = 2;
208 handle->WKBByteOrder = getMachineByteOrder();
209 handle->initialized = 1;
212 geos::util::Interrupt::cancel();
214 return static_cast<GEOSContextHandle_t>(extHandle);
217 GEOSMessageHandler
218 GEOSContext_setNoticeHandler_r(GEOSContextHandle_t extHandle, GEOSMessageHandler nf)
220 GEOSMessageHandler f;
221 GEOSContextHandleInternal_t *handle = 0;
222 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
223 if ( 0 == handle->initialized )
225 return NULL;
228 f = handle->NOTICE_MESSAGE;
229 handle->NOTICE_MESSAGE = nf;
231 return f;
234 GEOSMessageHandler
235 GEOSContext_setErrorHandler_r(GEOSContextHandle_t extHandle, GEOSMessageHandler nf)
237 GEOSMessageHandler f;
238 GEOSContextHandleInternal_t *handle = 0;
239 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
240 if ( 0 == handle->initialized )
242 return NULL;
245 f = handle->ERROR_MESSAGE;
246 handle->ERROR_MESSAGE = nf;
248 return f;
251 void
252 finishGEOS_r(GEOSContextHandle_t extHandle)
254 // Fix up freeing handle w.r.t. malloc above
255 free(extHandle);
256 extHandle = NULL;
259 void
260 GEOSFree_r (GEOSContextHandle_t extHandle, void* buffer)
262 assert(0 != extHandle);
264 free(buffer);
267 //-----------------------------------------------------------
268 // relate()-related functions
269 // return 0 = false, 1 = true, 2 = error occured
270 //-----------------------------------------------------------
272 char
273 GEOSDisjoint_r(GEOSContextHandle_t extHandle, const Geometry *g1, const Geometry *g2)
275 if ( 0 == extHandle )
277 return 2;
280 GEOSContextHandleInternal_t *handle = 0;
281 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
282 if ( handle->initialized == 0 )
284 return 2;
289 bool result = g1->disjoint(g2);
290 return result;
293 // TODO: mloskot is going to replace these double-catch block
294 // with a macro to remove redundant code in this and
295 // following functions.
296 catch (const std::exception &e)
298 handle->ERROR_MESSAGE("%s", e.what());
300 catch (...)
302 handle->ERROR_MESSAGE("Unknown exception thrown");
305 return 2;
308 char
309 GEOSTouches_r(GEOSContextHandle_t extHandle, const Geometry *g1, const Geometry *g2)
311 if ( 0 == extHandle )
313 return 2;
316 GEOSContextHandleInternal_t *handle = 0;
317 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
318 if ( 0 == handle->initialized )
320 return 2;
325 bool result = g1->touches(g2);
326 return result;
328 catch (const std::exception &e)
330 handle->ERROR_MESSAGE("%s", e.what());
332 catch (...)
334 handle->ERROR_MESSAGE("Unknown exception thrown");
337 return 2;
340 char
341 GEOSIntersects_r(GEOSContextHandle_t extHandle, const Geometry *g1, const Geometry *g2)
343 if ( 0 == extHandle )
345 return 2;
348 GEOSContextHandleInternal_t *handle = 0;
349 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
350 if ( 0 == handle->initialized )
352 return 2;
357 bool result = g1->intersects(g2);
358 return result;
360 catch (const std::exception &e)
362 handle->ERROR_MESSAGE("%s", e.what());
364 catch (...)
366 handle->ERROR_MESSAGE("Unknown exception thrown");
369 return 2;
372 char
373 GEOSCrosses_r(GEOSContextHandle_t extHandle, const Geometry *g1, const Geometry *g2)
375 if ( 0 == extHandle )
377 return 2;
380 GEOSContextHandleInternal_t *handle = 0;
381 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
382 if ( 0 == handle->initialized )
384 return 2;
389 bool result = g1->crosses(g2);
390 return result;
392 catch (const std::exception &e)
394 handle->ERROR_MESSAGE("%s", e.what());
396 catch (...)
398 handle->ERROR_MESSAGE("Unknown exception thrown");
401 return 2;
404 char
405 GEOSWithin_r(GEOSContextHandle_t extHandle, const Geometry *g1, const Geometry *g2)
407 if ( 0 == extHandle )
409 return 2;
412 GEOSContextHandleInternal_t *handle = 0;
413 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
414 if ( 0 == handle->initialized )
416 return 2;
421 bool result = g1->within(g2);
422 return result;
424 catch (const std::exception &e)
426 handle->ERROR_MESSAGE("%s", e.what());
428 catch (...)
430 handle->ERROR_MESSAGE("Unknown exception thrown");
433 return 2;
436 // call g1->contains(g2)
437 // returns 0 = false
438 // 1 = true
439 // 2 = error was trapped
440 char
441 GEOSContains_r(GEOSContextHandle_t extHandle, const Geometry *g1, const Geometry *g2)
443 if ( 0 == extHandle )
445 return 2;
448 GEOSContextHandleInternal_t *handle = 0;
449 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
450 if ( 0 == handle->initialized )
452 return 2;
457 bool result = g1->contains(g2);
458 return result;
460 catch (const std::exception &e)
462 handle->ERROR_MESSAGE("%s", e.what());
464 catch (...)
466 handle->ERROR_MESSAGE("Unknown exception thrown");
469 return 2;
472 char
473 GEOSOverlaps_r(GEOSContextHandle_t extHandle, const Geometry *g1, const Geometry *g2)
475 if ( 0 == extHandle )
477 return 2;
480 GEOSContextHandleInternal_t *handle = 0;
481 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
482 if ( 0 == handle->initialized )
484 return 2;
489 bool result = g1->overlaps(g2);
490 return result;
492 catch (const std::exception &e)
494 handle->ERROR_MESSAGE("%s", e.what());
496 catch (...)
498 handle->ERROR_MESSAGE("Unknown exception thrown");
501 return 2;
504 char
505 GEOSCovers_r(GEOSContextHandle_t extHandle, const Geometry *g1, const Geometry *g2)
507 if ( 0 == extHandle )
509 return 2;
512 GEOSContextHandleInternal_t *handle = 0;
513 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
514 if ( 0 == handle->initialized )
516 return 2;
521 bool result = g1->covers(g2);
522 return result;
524 catch (const std::exception &e)
526 handle->ERROR_MESSAGE("%s", e.what());
528 catch (...)
530 handle->ERROR_MESSAGE("Unknown exception thrown");
533 return 2;
536 char
537 GEOSCoveredBy_r(GEOSContextHandle_t extHandle, const Geometry *g1, const Geometry *g2)
539 if ( 0 == extHandle )
541 return 2;
544 GEOSContextHandleInternal_t *handle = 0;
545 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
546 if ( 0 == handle->initialized )
548 return 2;
553 bool result = g1->coveredBy(g2);
554 return result;
556 catch (const std::exception &e)
558 handle->ERROR_MESSAGE("%s", e.what());
560 catch (...)
562 handle->ERROR_MESSAGE("Unknown exception thrown");
565 return 2;
569 //-------------------------------------------------------------------
570 // low-level relate functions
571 //------------------------------------------------------------------
573 char
574 GEOSRelatePattern_r(GEOSContextHandle_t extHandle, const Geometry *g1, const Geometry *g2, const char *pat)
576 if ( 0 == extHandle )
578 return 2;
581 GEOSContextHandleInternal_t *handle = 0;
582 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
583 if ( 0 == handle->initialized )
585 return 2;
590 std::string s(pat);
591 bool result = g1->relate(g2, s);
592 return result;
594 catch (const std::exception &e)
596 handle->ERROR_MESSAGE("%s", e.what());
598 catch (...)
600 handle->ERROR_MESSAGE("Unknown exception thrown");
603 return 2;
606 char
607 GEOSRelatePatternMatch_r(GEOSContextHandle_t extHandle, const char *mat,
608 const char *pat)
610 if ( 0 == extHandle )
612 return 2;
615 GEOSContextHandleInternal_t *handle = 0;
616 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
617 if ( 0 == handle->initialized )
619 return 2;
624 using geos::geom::IntersectionMatrix;
626 std::string m(mat);
627 std::string p(pat);
628 IntersectionMatrix im(m);
630 bool result = im.matches(p);
631 return result;
633 catch (const std::exception &e)
635 handle->ERROR_MESSAGE("%s", e.what());
637 catch (...)
639 handle->ERROR_MESSAGE("Unknown exception thrown");
642 return 2;
645 char *
646 GEOSRelate_r(GEOSContextHandle_t extHandle, const Geometry *g1, const Geometry *g2)
648 if ( 0 == extHandle )
650 return NULL;
653 GEOSContextHandleInternal_t *handle = 0;
654 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
655 if ( 0 == handle->initialized )
657 return NULL;
662 using geos::geom::IntersectionMatrix;
664 IntersectionMatrix* im = g1->relate(g2);
665 if (0 == im)
667 return 0;
670 char *result = gstrdup(im->toString());
672 delete im;
673 im = 0;
675 return result;
677 catch (const std::exception &e)
679 handle->ERROR_MESSAGE("%s", e.what());
681 catch (...)
683 handle->ERROR_MESSAGE("Unknown exception thrown");
686 return NULL;
689 char *
690 GEOSRelateBoundaryNodeRule_r(GEOSContextHandle_t extHandle, const Geometry *g1, const Geometry *g2, int bnr)
692 if ( 0 == extHandle )
694 return NULL;
697 GEOSContextHandleInternal_t *handle = 0;
698 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
699 if ( 0 == handle->initialized )
701 return NULL;
706 using geos::operation::relate::RelateOp;
707 using geos::geom::IntersectionMatrix;
708 using geos::algorithm::BoundaryNodeRule;
710 IntersectionMatrix* im;
711 switch (bnr) {
712 case GEOSRELATE_BNR_MOD2: /* same as OGC */
713 im = RelateOp::relate(g1, g2,
714 BoundaryNodeRule::getBoundaryRuleMod2());
715 break;
716 case GEOSRELATE_BNR_ENDPOINT:
717 im = RelateOp::relate(g1, g2,
718 BoundaryNodeRule::getBoundaryEndPoint());
719 break;
720 case GEOSRELATE_BNR_MULTIVALENT_ENDPOINT:
721 im = RelateOp::relate(g1, g2,
722 BoundaryNodeRule::getBoundaryMultivalentEndPoint());
723 break;
724 case GEOSRELATE_BNR_MONOVALENT_ENDPOINT:
725 im = RelateOp::relate(g1, g2,
726 BoundaryNodeRule::getBoundaryMonovalentEndPoint());
727 break;
728 default:
729 handle->ERROR_MESSAGE("Invalid boundary node rule %d", bnr);
730 return 0;
731 break;
734 if (0 == im) return 0;
736 char *result = gstrdup(im->toString());
738 delete im;
739 im = 0;
741 return result;
743 catch (const std::exception &e)
745 handle->ERROR_MESSAGE("%s", e.what());
747 catch (...)
749 handle->ERROR_MESSAGE("Unknown exception thrown");
752 return NULL;
757 //-----------------------------------------------------------------
758 // isValid
759 //-----------------------------------------------------------------
762 char
763 GEOSisValid_r(GEOSContextHandle_t extHandle, const Geometry *g1)
765 if ( 0 == extHandle )
767 return 2;
770 GEOSContextHandleInternal_t *handle = 0;
771 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
772 if ( 0 == handle->initialized )
774 return 2;
779 using geos::operation::valid::IsValidOp;
780 using geos::operation::valid::TopologyValidationError;
782 IsValidOp ivo(g1);
783 TopologyValidationError *err = ivo.getValidationError();
784 if ( err )
786 handle->NOTICE_MESSAGE("%s", err->toString().c_str());
787 return 0;
789 else
791 return 1;
794 catch (const std::exception &e)
796 handle->ERROR_MESSAGE("%s", e.what());
798 catch (...)
800 handle->ERROR_MESSAGE("Unknown exception thrown");
803 return 2;
806 char *
807 GEOSisValidReason_r(GEOSContextHandle_t extHandle, const Geometry *g1)
809 if ( 0 == extHandle )
811 return NULL;
814 GEOSContextHandleInternal_t *handle = 0;
815 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
816 if ( 0 == handle->initialized )
818 return NULL;
823 using geos::operation::valid::IsValidOp;
824 using geos::operation::valid::TopologyValidationError;
826 char* result = 0;
827 char const* const validstr = "Valid Geometry";
829 IsValidOp ivo(g1);
830 TopologyValidationError *err = ivo.getValidationError();
831 if (0 != err)
833 std::ostringstream ss;
834 ss.precision(15);
835 ss << err->getCoordinate();
836 const std::string errloc = ss.str();
837 std::string errmsg(err->getMessage());
838 errmsg += "[" + errloc + "]";
839 result = gstrdup(errmsg);
841 else
843 result = gstrdup(std::string(validstr));
846 return result;
848 catch (const std::exception &e)
850 handle->ERROR_MESSAGE("%s", e.what());
852 catch (...)
854 handle->ERROR_MESSAGE("Unknown exception thrown");
857 return 0;
860 char
861 GEOSisValidDetail_r(GEOSContextHandle_t extHandle, const Geometry *g,
862 int flags, char** reason, Geometry ** location)
864 if ( 0 == extHandle )
866 return 0;
869 GEOSContextHandleInternal_t *handle = 0;
870 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
871 if ( 0 == handle->initialized )
873 return 0;
878 using geos::operation::valid::IsValidOp;
879 using geos::operation::valid::TopologyValidationError;
881 IsValidOp ivo(g);
882 if ( flags & GEOSVALID_ALLOW_SELFTOUCHING_RING_FORMING_HOLE ) {
883 ivo.setSelfTouchingRingFormingHoleValid(true);
885 TopologyValidationError *err = ivo.getValidationError();
886 if (0 != err)
888 if ( location ) {
889 *location = handle->geomFactory->createPoint(err->getCoordinate());
891 if ( reason ) {
892 std::string errmsg(err->getMessage());
893 *reason = gstrdup(errmsg);
895 return 0;
898 if ( location ) *location = 0;
899 if ( reason ) *reason = 0;
900 return 1; /* valid */
903 catch (const std::exception &e)
905 handle->ERROR_MESSAGE("%s", e.what());
907 catch (...)
909 handle->ERROR_MESSAGE("Unknown exception thrown");
912 return 2; /* exception */
915 //-----------------------------------------------------------------
916 // general purpose
917 //-----------------------------------------------------------------
919 char
920 GEOSEquals_r(GEOSContextHandle_t extHandle, const Geometry *g1, const Geometry *g2)
922 if ( 0 == extHandle )
924 return 2;
927 GEOSContextHandleInternal_t *handle = 0;
928 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
929 if ( 0 == handle->initialized )
931 return 2;
936 bool result = g1->equals(g2);
937 return result;
939 catch (const std::exception &e)
941 handle->ERROR_MESSAGE("%s", e.what());
943 catch (...)
945 handle->ERROR_MESSAGE("Unknown exception thrown");
948 return 2;
951 char
952 GEOSEqualsExact_r(GEOSContextHandle_t extHandle, const Geometry *g1, const Geometry *g2, double tolerance)
954 if ( 0 == extHandle )
956 return 2;
959 GEOSContextHandleInternal_t *handle = 0;
960 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
961 if ( 0 == handle->initialized )
963 return 2;
968 bool result = g1->equalsExact(g2, tolerance);
969 return result;
971 catch (const std::exception &e)
973 handle->ERROR_MESSAGE("%s", e.what());
975 catch (...)
977 handle->ERROR_MESSAGE("Unknown exception thrown");
980 return 2;
984 GEOSDistance_r(GEOSContextHandle_t extHandle, const Geometry *g1, const Geometry *g2, double *dist)
986 assert(0 != dist);
988 if ( 0 == extHandle )
990 return 0;
993 GEOSContextHandleInternal_t *handle = 0;
994 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
995 if ( 0 == handle->initialized )
997 return 0;
1002 *dist = g1->distance(g2);
1003 return 1;
1005 catch (const std::exception &e)
1007 handle->ERROR_MESSAGE("%s", e.what());
1009 catch (...)
1011 handle->ERROR_MESSAGE("Unknown exception thrown");
1014 return 0;
1018 GEOSHausdorffDistance_r(GEOSContextHandle_t extHandle, const Geometry *g1, const Geometry *g2, double *dist)
1020 assert(0 != dist);
1022 if ( 0 == extHandle )
1024 return 0;
1027 GEOSContextHandleInternal_t *handle = 0;
1028 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
1029 if ( 0 == handle->initialized )
1031 return 0;
1036 *dist = DiscreteHausdorffDistance::distance(*g1, *g2);
1037 return 1;
1039 catch (const std::exception &e)
1041 handle->ERROR_MESSAGE("%s", e.what());
1043 catch (...)
1045 handle->ERROR_MESSAGE("Unknown exception thrown");
1048 return 0;
1052 GEOSHausdorffDistanceDensify_r(GEOSContextHandle_t extHandle, const Geometry *g1, const Geometry *g2, double densifyFrac, double *dist)
1054 assert(0 != dist);
1056 if ( 0 == extHandle )
1058 return 0;
1061 GEOSContextHandleInternal_t *handle = 0;
1062 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
1063 if ( 0 == handle->initialized )
1065 return 0;
1070 *dist = DiscreteHausdorffDistance::distance(*g1, *g2, densifyFrac);
1071 return 1;
1073 catch (const std::exception &e)
1075 handle->ERROR_MESSAGE("%s", e.what());
1077 catch (...)
1079 handle->ERROR_MESSAGE("Unknown exception thrown");
1082 return 0;
1086 GEOSArea_r(GEOSContextHandle_t extHandle, const Geometry *g, double *area)
1088 assert(0 != area);
1090 if ( 0 == extHandle )
1092 return 0;
1095 GEOSContextHandleInternal_t *handle = 0;
1096 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
1097 if ( 0 == handle->initialized )
1099 return 0;
1104 *area = g->getArea();
1105 return 1;
1107 catch (const std::exception &e)
1109 handle->ERROR_MESSAGE("%s", e.what());
1111 catch (...)
1113 handle->ERROR_MESSAGE("Unknown exception thrown");
1116 return 0;
1120 GEOSLength_r(GEOSContextHandle_t extHandle, const Geometry *g, double *length)
1122 assert(0 != length);
1124 if ( 0 == extHandle )
1126 return 2;
1129 GEOSContextHandleInternal_t *handle = 0;
1130 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
1131 if ( 0 == handle->initialized )
1133 return 0;
1138 *length = g->getLength();
1139 return 1;
1141 catch (const std::exception &e)
1143 handle->ERROR_MESSAGE("%s", e.what());
1145 catch (...)
1147 handle->ERROR_MESSAGE("Unknown exception thrown");
1150 return 0;
1153 Geometry *
1154 GEOSGeomFromWKT_r(GEOSContextHandle_t extHandle, const char *wkt)
1156 if ( 0 == extHandle )
1158 return NULL;
1161 GEOSContextHandleInternal_t *handle = 0;
1162 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
1163 if ( 0 == handle->initialized )
1165 return NULL;
1170 const std::string wktstring(wkt);
1171 WKTReader r(static_cast<GeometryFactory const*>(handle->geomFactory));
1173 Geometry *g = r.read(wktstring);
1174 return g;
1176 catch (const std::exception &e)
1178 handle->ERROR_MESSAGE("%s", e.what());
1180 catch (...)
1182 handle->ERROR_MESSAGE("Unknown exception thrown");
1185 return NULL;
1188 char *
1189 GEOSGeomToWKT_r(GEOSContextHandle_t extHandle, const Geometry *g1)
1191 if ( 0 == extHandle )
1193 return NULL;
1196 GEOSContextHandleInternal_t *handle = 0;
1197 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
1198 if ( 0 == handle->initialized )
1200 return NULL;
1206 char *result = gstrdup(g1->toString());
1207 return result;
1209 catch (const std::exception &e)
1211 handle->ERROR_MESSAGE("%s", e.what());
1213 catch (...)
1215 handle->ERROR_MESSAGE("Unknown exception thrown");
1217 return NULL;
1220 // Remember to free the result!
1221 unsigned char *
1222 GEOSGeomToWKB_buf_r(GEOSContextHandle_t extHandle, const Geometry *g, size_t *size)
1224 assert(0 != size);
1226 if ( 0 == extHandle )
1228 return NULL;
1231 GEOSContextHandleInternal_t *handle = 0;
1232 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
1233 if ( 0 == handle->initialized )
1235 return NULL;
1238 using geos::io::WKBWriter;
1241 int byteOrder = handle->WKBByteOrder;
1242 WKBWriter w(handle->WKBOutputDims, byteOrder);
1243 std::ostringstream os(std::ios_base::binary);
1244 w.write(*g, os);
1245 std::string wkbstring(os.str());
1246 const std::size_t len = wkbstring.length();
1248 unsigned char* result = 0;
1249 result = static_cast<unsigned char*>(malloc(len));
1250 if (0 != result)
1252 std::memcpy(result, wkbstring.c_str(), len);
1253 *size = len;
1255 return result;
1257 catch (const std::exception &e)
1259 handle->ERROR_MESSAGE("%s", e.what());
1261 catch (...)
1263 handle->ERROR_MESSAGE("Unknown exception thrown");
1266 return NULL;
1269 Geometry *
1270 GEOSGeomFromWKB_buf_r(GEOSContextHandle_t extHandle, const unsigned char *wkb, size_t size)
1272 if ( 0 == extHandle )
1274 return NULL;
1277 GEOSContextHandleInternal_t *handle = 0;
1278 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
1279 if ( 0 == handle->initialized )
1281 return NULL;
1284 using geos::io::WKBReader;
1287 std::string wkbstring(reinterpret_cast<const char*>(wkb), size); // make it binary !
1288 WKBReader r(*(static_cast<GeometryFactory const*>(handle->geomFactory)));
1289 std::istringstream is(std::ios_base::binary);
1290 is.str(wkbstring);
1291 is.seekg(0, std::ios::beg); // rewind reader pointer
1292 Geometry *g = r.read(is);
1293 return g;
1295 catch (const std::exception &e)
1297 handle->ERROR_MESSAGE("%s", e.what());
1299 catch (...)
1301 handle->ERROR_MESSAGE("Unknown exception thrown");
1304 return NULL;
1307 /* Read/write wkb hex values. Returned geometries are
1308 owned by the caller.*/
1309 unsigned char *
1310 GEOSGeomToHEX_buf_r(GEOSContextHandle_t extHandle, const Geometry *g, size_t *size)
1312 if ( 0 == extHandle )
1314 return NULL;
1317 GEOSContextHandleInternal_t *handle = 0;
1318 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
1319 if ( 0 == handle->initialized )
1321 return NULL;
1324 using geos::io::WKBWriter;
1327 int byteOrder = handle->WKBByteOrder;
1328 WKBWriter w(handle->WKBOutputDims, byteOrder);
1329 std::ostringstream os(std::ios_base::binary);
1330 w.writeHEX(*g, os);
1331 std::string hexstring(os.str());
1333 char *result = gstrdup(hexstring);
1334 if (0 != result)
1336 *size = hexstring.length();
1339 return reinterpret_cast<unsigned char*>(result);
1341 catch (const std::exception &e)
1343 handle->ERROR_MESSAGE("%s", e.what());
1345 catch (...)
1347 handle->ERROR_MESSAGE("Unknown exception thrown");
1350 return NULL;
1353 Geometry *
1354 GEOSGeomFromHEX_buf_r(GEOSContextHandle_t extHandle, const unsigned char *hex, size_t size)
1356 if ( 0 == extHandle )
1358 return NULL;
1361 GEOSContextHandleInternal_t *handle = 0;
1362 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
1363 if ( 0 == handle->initialized )
1365 return NULL;
1368 using geos::io::WKBReader;
1371 std::string hexstring(reinterpret_cast<const char*>(hex), size);
1372 WKBReader r(*(static_cast<GeometryFactory const*>(handle->geomFactory)));
1373 std::istringstream is(std::ios_base::binary);
1374 is.str(hexstring);
1375 is.seekg(0, std::ios::beg); // rewind reader pointer
1377 Geometry *g = r.readHEX(is);
1378 return g;
1380 catch (const std::exception &e)
1382 handle->ERROR_MESSAGE("%s", e.what());
1384 catch (...)
1386 handle->ERROR_MESSAGE("Unknown exception thrown");
1389 return NULL;
1392 char
1393 GEOSisEmpty_r(GEOSContextHandle_t extHandle, const Geometry *g1)
1395 if ( 0 == extHandle )
1397 return 2;
1400 GEOSContextHandleInternal_t *handle = 0;
1401 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
1402 if ( 0 == handle->initialized )
1404 return 2;
1409 return g1->isEmpty();
1411 catch (const std::exception &e)
1413 handle->ERROR_MESSAGE("%s", e.what());
1415 catch (...)
1417 handle->ERROR_MESSAGE("Unknown exception thrown");
1420 return 2;
1423 char
1424 GEOSisSimple_r(GEOSContextHandle_t extHandle, const Geometry *g1)
1426 if ( 0 == extHandle )
1428 return 2;
1431 GEOSContextHandleInternal_t *handle = 0;
1432 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
1433 if ( 0 == handle->initialized )
1435 return 2;
1440 return g1->isSimple();
1442 catch (const std::exception &e)
1444 handle->ERROR_MESSAGE("%s", e.what());
1445 return 2;
1448 catch (...)
1450 handle->ERROR_MESSAGE("Unknown exception thrown");
1451 return 2;
1455 char
1456 GEOSisRing_r(GEOSContextHandle_t extHandle, const Geometry *g)
1458 if ( 0 == extHandle )
1460 return 2;
1463 GEOSContextHandleInternal_t *handle = 0;
1464 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
1465 if ( 0 == handle->initialized )
1467 return 2;
1472 const LineString *ls = dynamic_cast<const LineString *>(g);
1473 if ( ls ) {
1474 return (ls->isRing());
1475 } else {
1476 return 0;
1479 catch (const std::exception &e)
1481 handle->ERROR_MESSAGE("%s", e.what());
1482 return 2;
1485 catch (...)
1487 handle->ERROR_MESSAGE("Unknown exception thrown");
1488 return 2;
1494 //free the result of this
1495 char *
1496 GEOSGeomType_r(GEOSContextHandle_t extHandle, const Geometry *g1)
1498 if ( 0 == extHandle )
1500 return NULL;
1503 GEOSContextHandleInternal_t *handle = 0;
1504 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
1505 if ( 0 == handle->initialized )
1507 return NULL;
1512 std::string s = g1->getGeometryType();
1514 char *result = gstrdup(s);
1515 return result;
1517 catch (const std::exception &e)
1519 handle->ERROR_MESSAGE("%s", e.what());
1521 catch (...)
1523 handle->ERROR_MESSAGE("Unknown exception thrown");
1526 return NULL;
1529 // Return postgis geometry type index
1531 GEOSGeomTypeId_r(GEOSContextHandle_t extHandle, const Geometry *g1)
1533 if ( 0 == extHandle )
1535 return -1;
1538 GEOSContextHandleInternal_t *handle = 0;
1539 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
1540 if ( 0 == handle->initialized )
1542 return -1;
1547 return g1->getGeometryTypeId();
1549 catch (const std::exception &e)
1551 handle->ERROR_MESSAGE("%s", e.what());
1553 catch (...)
1555 handle->ERROR_MESSAGE("Unknown exception thrown");
1558 return -1;
1561 //-------------------------------------------------------------------
1562 // GEOS functions that return geometries
1563 //-------------------------------------------------------------------
1565 Geometry *
1566 GEOSEnvelope_r(GEOSContextHandle_t extHandle, const Geometry *g1)
1568 if ( 0 == extHandle )
1570 return NULL;
1573 GEOSContextHandleInternal_t *handle = 0;
1574 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
1575 if ( 0 == handle->initialized )
1577 return NULL;
1582 Geometry *g3 = g1->getEnvelope();
1583 return g3;
1585 catch (const std::exception &e)
1587 handle->ERROR_MESSAGE("%s", e.what());
1589 catch (...)
1591 handle->ERROR_MESSAGE("Unknown exception thrown");
1594 return NULL;
1597 Geometry *
1598 GEOSIntersection_r(GEOSContextHandle_t extHandle, const Geometry *g1, const Geometry *g2)
1600 if ( 0 == extHandle )
1602 return NULL;
1605 GEOSContextHandleInternal_t *handle = 0;
1606 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
1607 if ( 0 == handle->initialized )
1609 return NULL;
1614 return g1->intersection(g2);
1616 catch (const std::exception &e)
1618 handle->ERROR_MESSAGE("%s", e.what());
1620 catch (...)
1622 handle->ERROR_MESSAGE("Unknown exception thrown");
1625 return NULL;
1628 Geometry *
1629 GEOSBuffer_r(GEOSContextHandle_t extHandle, const Geometry *g1, double width, int quadrantsegments)
1631 if ( 0 == extHandle )
1633 return NULL;
1636 GEOSContextHandleInternal_t *handle = 0;
1637 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
1638 if ( 0 == handle->initialized )
1640 return NULL;
1645 Geometry *g3 = g1->buffer(width, quadrantsegments);
1646 return g3;
1648 catch (const std::exception &e)
1650 handle->ERROR_MESSAGE("%s", e.what());
1652 catch (...)
1654 handle->ERROR_MESSAGE("Unknown exception thrown");
1657 return NULL;
1660 Geometry *
1661 GEOSBufferWithStyle_r(GEOSContextHandle_t extHandle, const Geometry *g1, double width, int quadsegs, int endCapStyle, int joinStyle, double mitreLimit)
1663 using geos::operation::buffer::BufferParameters;
1664 using geos::operation::buffer::BufferOp;
1665 using geos::util::IllegalArgumentException;
1667 if ( 0 == extHandle )
1669 return NULL;
1672 GEOSContextHandleInternal_t *handle = 0;
1673 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
1674 if ( 0 == handle->initialized )
1676 return NULL;
1681 BufferParameters bp;
1682 bp.setQuadrantSegments(quadsegs);
1684 if ( endCapStyle > BufferParameters::CAP_SQUARE )
1686 throw IllegalArgumentException("Invalid buffer endCap style");
1688 bp.setEndCapStyle(
1689 static_cast<BufferParameters::EndCapStyle>(endCapStyle)
1692 if ( joinStyle > BufferParameters::JOIN_BEVEL )
1694 throw IllegalArgumentException("Invalid buffer join style");
1696 bp.setJoinStyle(
1697 static_cast<BufferParameters::JoinStyle>(joinStyle)
1699 bp.setMitreLimit(mitreLimit);
1700 BufferOp op(g1, bp);
1701 Geometry *g3 = op.getResultGeometry(width);
1702 return g3;
1704 catch (const std::exception &e)
1706 handle->ERROR_MESSAGE("%s", e.what());
1708 catch (...)
1710 handle->ERROR_MESSAGE("Unknown exception thrown");
1713 return NULL;
1716 Geometry *
1717 GEOSOffsetCurve_r(GEOSContextHandle_t extHandle, const Geometry *g1, double width, int quadsegs, int joinStyle, double mitreLimit)
1719 if ( 0 == extHandle ) return NULL;
1721 GEOSContextHandleInternal_t *handle = 0;
1722 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
1723 if ( 0 == handle->initialized ) return NULL;
1727 BufferParameters bp;
1728 bp.setEndCapStyle( BufferParameters::CAP_FLAT );
1729 bp.setQuadrantSegments(quadsegs);
1731 if ( joinStyle > BufferParameters::JOIN_BEVEL )
1733 throw IllegalArgumentException("Invalid buffer join style");
1735 bp.setJoinStyle(
1736 static_cast<BufferParameters::JoinStyle>(joinStyle)
1738 bp.setMitreLimit(mitreLimit);
1740 bool isLeftSide = true;
1741 if ( width < 0 ) {
1742 isLeftSide = false;
1743 width = -width;
1745 BufferBuilder bufBuilder (bp);
1746 Geometry *g3 = bufBuilder.bufferLineSingleSided(g1, width, isLeftSide);
1748 return g3;
1750 catch (const std::exception &e)
1752 handle->ERROR_MESSAGE("%s", e.what());
1754 catch (...)
1756 handle->ERROR_MESSAGE("Unknown exception thrown");
1759 return NULL;
1762 /* @deprecated in 3.3.0 */
1763 Geometry *
1764 GEOSSingleSidedBuffer_r(GEOSContextHandle_t extHandle, const Geometry *g1, double width, int quadsegs, int joinStyle, double mitreLimit, int leftSide)
1766 if ( 0 == extHandle ) return NULL;
1768 GEOSContextHandleInternal_t *handle = 0;
1769 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
1770 if ( 0 == handle->initialized ) return NULL;
1774 BufferParameters bp;
1775 bp.setEndCapStyle( BufferParameters::CAP_FLAT );
1776 bp.setQuadrantSegments(quadsegs);
1778 if ( joinStyle > BufferParameters::JOIN_BEVEL )
1780 throw IllegalArgumentException("Invalid buffer join style");
1782 bp.setJoinStyle(
1783 static_cast<BufferParameters::JoinStyle>(joinStyle)
1785 bp.setMitreLimit(mitreLimit);
1787 bool isLeftSide = leftSide == 0 ? false : true;
1788 BufferBuilder bufBuilder (bp);
1789 Geometry *g3 = bufBuilder.bufferLineSingleSided(g1, width, isLeftSide);
1791 return g3;
1793 catch (const std::exception &e)
1795 handle->ERROR_MESSAGE("%s", e.what());
1797 catch (...)
1799 handle->ERROR_MESSAGE("Unknown exception thrown");
1802 return NULL;
1805 Geometry *
1806 GEOSConvexHull_r(GEOSContextHandle_t extHandle, const Geometry *g1)
1808 if ( 0 == extHandle )
1810 return NULL;
1813 GEOSContextHandleInternal_t *handle = 0;
1814 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
1815 if ( 0 == handle->initialized )
1817 return NULL;
1822 Geometry *g3 = g1->convexHull();
1823 return g3;
1825 catch (const std::exception &e)
1827 handle->ERROR_MESSAGE("%s", e.what());
1829 catch (...)
1831 handle->ERROR_MESSAGE("Unknown exception thrown");
1834 return NULL;
1837 Geometry *
1838 GEOSDifference_r(GEOSContextHandle_t extHandle, const Geometry *g1, const Geometry *g2)
1840 if ( 0 == extHandle )
1842 return NULL;
1845 GEOSContextHandleInternal_t *handle = 0;
1846 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
1847 if ( 0 == handle->initialized )
1849 return NULL;
1854 return g1->difference(g2);
1856 catch (const std::exception &e)
1858 handle->ERROR_MESSAGE("%s", e.what());
1860 catch (...)
1862 handle->ERROR_MESSAGE("Unknown exception thrown");
1865 return NULL;
1868 Geometry *
1869 GEOSBoundary_r(GEOSContextHandle_t extHandle, const Geometry *g1)
1871 if ( 0 == extHandle )
1873 return NULL;
1876 GEOSContextHandleInternal_t *handle = 0;
1877 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
1878 if ( 0 == handle->initialized )
1880 return NULL;
1885 Geometry *g3 = g1->getBoundary();
1886 return g3;
1888 catch (const std::exception &e)
1890 handle->ERROR_MESSAGE("%s", e.what());
1892 catch (...)
1894 handle->ERROR_MESSAGE("Unknown exception thrown");
1897 return NULL;
1900 Geometry *
1901 GEOSSymDifference_r(GEOSContextHandle_t extHandle, const Geometry *g1, const Geometry *g2)
1903 if ( 0 == extHandle )
1905 return NULL;
1908 GEOSContextHandleInternal_t *handle = 0;
1909 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
1910 if ( 0 == handle->initialized )
1912 return NULL;
1917 return g1->symDifference(g2);
1919 catch (const std::exception &e)
1921 handle->ERROR_MESSAGE("%s", e.what());
1922 return NULL;
1925 catch (...)
1927 handle->ERROR_MESSAGE("Unknown exception thrown");
1928 return NULL;
1932 Geometry *
1933 GEOSUnion_r(GEOSContextHandle_t extHandle, const Geometry *g1, const Geometry *g2)
1935 if ( 0 == extHandle )
1937 return NULL;
1940 GEOSContextHandleInternal_t *handle = 0;
1941 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
1942 if ( 0 == handle->initialized )
1944 return NULL;
1949 return g1->Union(g2);
1951 catch (const std::exception &e)
1953 #if VERBOSE_EXCEPTIONS
1954 std::ostringstream s;
1955 s << "Exception on GEOSUnion with following inputs:" << std::endl;
1956 s << "A: "<<g1->toString() << std::endl;
1957 s << "B: "<<g2->toString() << std::endl;
1958 handle->NOTICE_MESSAGE("%s", s.str().c_str());
1959 #endif // VERBOSE_EXCEPTIONS
1960 handle->ERROR_MESSAGE("%s", e.what());
1962 catch (...)
1964 handle->ERROR_MESSAGE("Unknown exception thrown");
1967 return NULL;
1970 Geometry *
1971 GEOSUnaryUnion_r(GEOSContextHandle_t extHandle, const Geometry *g)
1973 if ( 0 == extHandle )
1975 return NULL;
1978 GEOSContextHandleInternal_t *handle = 0;
1979 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
1980 if ( 0 == handle->initialized )
1982 return NULL;
1987 GeomAutoPtr g3 ( g->Union() );
1988 return g3.release();
1990 catch (const std::exception &e)
1992 #if VERBOSE_EXCEPTIONS
1993 std::ostringstream s;
1994 s << "Exception on GEOSUnaryUnion with following inputs:" << std::endl;
1995 s << "A: "<<g1->toString() << std::endl;
1996 s << "B: "<<g2->toString() << std::endl;
1997 handle->NOTICE_MESSAGE("%s", s.str().c_str());
1998 #endif // VERBOSE_EXCEPTIONS
1999 handle->ERROR_MESSAGE("%s", e.what());
2001 catch (...)
2003 handle->ERROR_MESSAGE("Unknown exception thrown");
2006 return NULL;
2009 Geometry *
2010 GEOSNode_r(GEOSContextHandle_t extHandle, const Geometry *g)
2012 if ( 0 == extHandle )
2014 return NULL;
2017 GEOSContextHandleInternal_t *handle = 0;
2018 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
2019 if ( 0 == handle->initialized )
2021 return NULL;
2026 std::auto_ptr<Geometry> g3 = geos::noding::GeometryNoder::node(*g);
2027 return g3.release();
2029 catch (const std::exception &e)
2031 #if VERBOSE_EXCEPTIONS
2032 std::ostringstream s;
2033 s << "Exception on GEOSUnaryUnion with following inputs:" << std::endl;
2034 s << "A: "<<g1->toString() << std::endl;
2035 s << "B: "<<g2->toString() << std::endl;
2036 handle->NOTICE_MESSAGE("%s", s.str().c_str());
2037 #endif // VERBOSE_EXCEPTIONS
2038 handle->ERROR_MESSAGE("%s", e.what());
2040 catch (...)
2042 handle->ERROR_MESSAGE("Unknown exception thrown");
2045 return NULL;
2048 Geometry *
2049 GEOSUnionCascaded_r(GEOSContextHandle_t extHandle, const Geometry *g1)
2051 if ( 0 == extHandle )
2053 return NULL;
2056 GEOSContextHandleInternal_t *handle = 0;
2057 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
2058 if ( 0 == handle->initialized )
2060 return NULL;
2065 const geos::geom::MultiPolygon *p = dynamic_cast<const geos::geom::MultiPolygon *>(g1);
2066 if ( ! p )
2068 handle->ERROR_MESSAGE("Invalid argument (must be a MultiPolygon)");
2069 return NULL;
2072 using geos::operation::geounion::CascadedPolygonUnion;
2073 return CascadedPolygonUnion::Union(p);
2075 catch (const std::exception &e)
2077 handle->ERROR_MESSAGE("%s", e.what());
2079 catch (...)
2081 handle->ERROR_MESSAGE("Unknown exception thrown");
2084 return NULL;
2087 Geometry *
2088 GEOSPointOnSurface_r(GEOSContextHandle_t extHandle, const Geometry *g1)
2090 if ( 0 == extHandle )
2092 return NULL;
2095 GEOSContextHandleInternal_t *handle = 0;
2096 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
2097 if ( 0 == handle->initialized )
2099 return NULL;
2104 Geometry *ret = g1->getInteriorPoint();
2105 if ( ! ret )
2107 const GeometryFactory* gf = handle->geomFactory;
2108 // return an empty point
2109 return gf->createPoint();
2111 return ret;
2113 catch (const std::exception &e)
2115 handle->ERROR_MESSAGE("%s", e.what());
2117 catch (...)
2119 handle->ERROR_MESSAGE("Unknown exception thrown");
2122 return NULL;
2125 //-------------------------------------------------------------------
2126 // memory management functions
2127 //------------------------------------------------------------------
2129 void
2130 GEOSGeom_destroy_r(GEOSContextHandle_t extHandle, Geometry *a)
2132 GEOSContextHandleInternal_t *handle = 0;
2134 // FIXME: mloskot: Does this try-catch around delete means that
2135 // destructors in GEOS may throw? If it does, this is a serious
2136 // violation of "never throw an exception from a destructor" principle
2140 delete a;
2142 catch (const std::exception &e)
2144 if ( 0 == extHandle )
2146 return;
2149 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
2150 if ( 0 == handle->initialized )
2152 return;
2155 handle->ERROR_MESSAGE("%s", e.what());
2157 catch (...)
2159 if ( 0 == extHandle )
2161 return;
2164 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
2165 if ( 0 == handle->initialized )
2167 return;
2170 handle->ERROR_MESSAGE("Unknown exception thrown");
2174 void
2175 GEOSSetSRID_r(GEOSContextHandle_t extHandle, Geometry *g, int srid)
2177 assert(0 != g);
2179 if ( 0 == extHandle )
2181 return;
2184 GEOSContextHandleInternal_t *handle = 0;
2185 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
2186 if ( 0 == handle->initialized )
2188 return;
2191 g->setSRID(srid);
2196 GEOSGetNumCoordinates_r(GEOSContextHandle_t extHandle, const Geometry *g)
2198 assert(0 != g);
2200 if ( 0 == extHandle )
2202 return -1;
2205 GEOSContextHandleInternal_t *handle = 0;
2206 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
2207 if ( 0 == handle->initialized )
2209 return -1;
2214 return static_cast<int>(g->getNumPoints());
2216 catch (const std::exception &e)
2218 handle->ERROR_MESSAGE("%s", e.what());
2220 catch (...)
2222 handle->ERROR_MESSAGE("Unknown exception thrown");
2225 return -1;
2229 * Return -1 on exception, 0 otherwise.
2230 * Converts Geometry to normal form (or canonical form).
2233 GEOSNormalize_r(GEOSContextHandle_t extHandle, Geometry *g)
2235 assert(0 != g);
2237 if ( 0 == extHandle )
2239 return -1;
2242 GEOSContextHandleInternal_t *handle = 0;
2243 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
2244 if ( 0 == handle->initialized )
2246 return -1;
2251 g->normalize();
2252 return 0; // SUCCESS
2254 catch (const std::exception &e)
2256 handle->ERROR_MESSAGE("%s", e.what());
2258 catch (...)
2260 handle->ERROR_MESSAGE("Unknown exception thrown");
2263 return -1;
2267 GEOSGetNumInteriorRings_r(GEOSContextHandle_t extHandle, const Geometry *g1)
2269 if ( 0 == extHandle )
2271 return -1;
2274 GEOSContextHandleInternal_t *handle = 0;
2275 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
2276 if ( 0 == handle->initialized )
2278 return -1;
2283 const Polygon *p = dynamic_cast<const Polygon *>(g1);
2284 if ( ! p )
2286 handle->ERROR_MESSAGE("Argument is not a Polygon");
2287 return -1;
2289 return static_cast<int>(p->getNumInteriorRing());
2291 catch (const std::exception &e)
2293 handle->ERROR_MESSAGE("%s", e.what());
2295 catch (...)
2297 handle->ERROR_MESSAGE("Unknown exception thrown");
2300 return -1;
2304 // returns -1 on error and 1 for non-multi geometries
2306 GEOSGetNumGeometries_r(GEOSContextHandle_t extHandle, const Geometry *g1)
2308 if ( 0 == extHandle )
2310 return -1;
2313 GEOSContextHandleInternal_t *handle = 0;
2314 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
2315 if ( 0 == handle->initialized )
2317 return -1;
2322 return static_cast<int>(g1->getNumGeometries());
2324 catch (const std::exception &e)
2326 handle->ERROR_MESSAGE("%s", e.what());
2328 catch (...)
2330 handle->ERROR_MESSAGE("Unknown exception thrown");
2333 return -1;
2338 * Call only on GEOMETRYCOLLECTION or MULTI*.
2339 * Return a pointer to the internal Geometry.
2341 const Geometry *
2342 GEOSGetGeometryN_r(GEOSContextHandle_t extHandle, const Geometry *g1, int n)
2344 if ( 0 == extHandle )
2346 return NULL;
2349 GEOSContextHandleInternal_t *handle = 0;
2350 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
2351 if ( 0 == handle->initialized )
2353 return NULL;
2358 return g1->getGeometryN(n);
2360 catch (const std::exception &e)
2362 handle->ERROR_MESSAGE("%s", e.what());
2364 catch (...)
2366 handle->ERROR_MESSAGE("Unknown exception thrown");
2369 return NULL;
2373 * Call only on LINESTRING
2374 * Returns NULL on exception
2376 Geometry *
2377 GEOSGeomGetPointN_r(GEOSContextHandle_t extHandle, const Geometry *g1, int n)
2379 if ( 0 == extHandle )
2381 return NULL;
2384 GEOSContextHandleInternal_t *handle = 0;
2385 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
2386 if ( 0 == handle->initialized )
2388 return NULL;
2393 using geos::geom::LineString;
2394 const LineString *ls = dynamic_cast<const LineString *>(g1);
2395 if ( ! ls )
2397 handle->ERROR_MESSAGE("Argument is not a LineString");
2398 return NULL;
2400 return ls->getPointN(n);
2402 catch (const std::exception &e)
2404 handle->ERROR_MESSAGE("%s", e.what());
2406 catch (...)
2408 handle->ERROR_MESSAGE("Unknown exception thrown");
2411 return NULL;
2415 * Call only on LINESTRING
2417 Geometry *
2418 GEOSGeomGetStartPoint_r(GEOSContextHandle_t extHandle, const Geometry *g1)
2420 if ( 0 == extHandle )
2422 return NULL;
2425 GEOSContextHandleInternal_t *handle = 0;
2426 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
2427 if ( 0 == handle->initialized )
2429 return NULL;
2434 using geos::geom::LineString;
2435 const LineString *ls = dynamic_cast<const LineString *>(g1);
2436 if ( ! ls )
2438 handle->ERROR_MESSAGE("Argument is not a LineString");
2439 return NULL;
2441 return ls->getStartPoint();
2443 catch (const std::exception &e)
2445 handle->ERROR_MESSAGE("%s", e.what());
2447 catch (...)
2449 handle->ERROR_MESSAGE("Unknown exception thrown");
2452 return NULL;
2456 * Call only on LINESTRING
2458 Geometry *
2459 GEOSGeomGetEndPoint_r(GEOSContextHandle_t extHandle, const Geometry *g1)
2461 if ( 0 == extHandle )
2463 return NULL;
2466 GEOSContextHandleInternal_t *handle = 0;
2467 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
2468 if ( 0 == handle->initialized )
2470 return NULL;
2475 using geos::geom::LineString;
2476 const LineString *ls = dynamic_cast<const LineString *>(g1);
2477 if ( ! ls )
2479 handle->ERROR_MESSAGE("Argument is not a LineString");
2480 return NULL;
2482 return ls->getEndPoint();
2484 catch (const std::exception &e)
2486 handle->ERROR_MESSAGE("%s", e.what());
2488 catch (...)
2490 handle->ERROR_MESSAGE("Unknown exception thrown");
2493 return NULL;
2497 * Call only on LINESTRING
2498 * return 2 on exception, 1 on true, 0 on false
2500 char
2501 GEOSisClosed_r(GEOSContextHandle_t extHandle, const Geometry *g1)
2503 if ( 0 == extHandle )
2505 return 2;
2508 GEOSContextHandleInternal_t *handle = 0;
2509 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
2510 if ( 0 == handle->initialized )
2512 return 2;
2517 using geos::geom::LineString;
2518 const LineString *ls = dynamic_cast<const LineString *>(g1);
2519 if ( ! ls )
2521 handle->ERROR_MESSAGE("Argument is not a LineString");
2522 return 2;
2524 return ls->isClosed();
2526 catch (const std::exception &e)
2528 handle->ERROR_MESSAGE("%s", e.what());
2530 catch (...)
2532 handle->ERROR_MESSAGE("Unknown exception thrown");
2535 return 2;
2539 * Call only on LINESTRING
2540 * return 0 on exception, otherwise 1
2543 GEOSGeomGetLength_r(GEOSContextHandle_t extHandle, const Geometry *g1, double *length)
2545 if ( 0 == extHandle )
2547 return 0;
2550 GEOSContextHandleInternal_t *handle = 0;
2551 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
2552 if ( 0 == handle->initialized )
2554 return 0;
2559 using geos::geom::LineString;
2560 const LineString *ls = dynamic_cast<const LineString *>(g1);
2561 if ( ! ls )
2563 handle->ERROR_MESSAGE("Argument is not a LineString");
2564 return 0;
2566 *length = ls->getLength();
2567 return 1;
2569 catch (const std::exception &e)
2571 handle->ERROR_MESSAGE("%s", e.what());
2573 catch (...)
2575 handle->ERROR_MESSAGE("Unknown exception thrown");
2578 return 0;
2582 * Call only on LINESTRING
2585 GEOSGeomGetNumPoints_r(GEOSContextHandle_t extHandle, const Geometry *g1)
2587 if ( 0 == extHandle )
2589 return -1;
2592 GEOSContextHandleInternal_t *handle = 0;
2593 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
2594 if ( 0 == handle->initialized )
2596 return -1;
2601 using geos::geom::LineString;
2602 const LineString *ls = dynamic_cast<const LineString *>(g1);
2603 if ( ! ls )
2605 handle->ERROR_MESSAGE("Argument is not a LineString");
2606 return -1;
2608 return static_cast<int>(ls->getNumPoints());
2610 catch (const std::exception &e)
2612 handle->ERROR_MESSAGE("%s", e.what());
2614 catch (...)
2616 handle->ERROR_MESSAGE("Unknown exception thrown");
2619 return -1;
2623 * For POINT
2624 * returns 0 on exception, otherwise 1
2627 GEOSGeomGetX_r(GEOSContextHandle_t extHandle, const Geometry *g1, double *x)
2629 if ( 0 == extHandle )
2631 return 0;
2634 GEOSContextHandleInternal_t *handle = 0;
2635 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
2636 if ( 0 == handle->initialized )
2638 return 0;
2643 using geos::geom::Point;
2644 const Point *po = dynamic_cast<const Point *>(g1);
2645 if ( ! po )
2647 handle->ERROR_MESSAGE("Argument is not a Point");
2648 return 0;
2650 *x = po->getX();
2651 return 1;
2653 catch (const std::exception &e)
2655 handle->ERROR_MESSAGE("%s", e.what());
2657 catch (...)
2659 handle->ERROR_MESSAGE("Unknown exception thrown");
2662 return 0;
2666 * For POINT
2667 * returns 0 on exception, otherwise 1
2670 GEOSGeomGetY_r(GEOSContextHandle_t extHandle, const Geometry *g1, double *y)
2672 if ( 0 == extHandle )
2674 return 0;
2677 GEOSContextHandleInternal_t *handle = 0;
2678 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
2679 if ( 0 == handle->initialized )
2681 return 0;
2686 using geos::geom::Point;
2687 const Point *po = dynamic_cast<const Point *>(g1);
2688 if ( ! po )
2690 handle->ERROR_MESSAGE("Argument is not a Point");
2691 return 0;
2693 *y = po->getY();
2694 return 1;
2696 catch (const std::exception &e)
2698 handle->ERROR_MESSAGE("%s", e.what());
2700 catch (...)
2702 handle->ERROR_MESSAGE("Unknown exception thrown");
2705 return 0;
2709 * Call only on polygon
2710 * Return a copy of the internal Geometry.
2712 const Geometry *
2713 GEOSGetExteriorRing_r(GEOSContextHandle_t extHandle, const Geometry *g1)
2715 if ( 0 == extHandle )
2717 return NULL;
2720 GEOSContextHandleInternal_t *handle = 0;
2721 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
2722 if ( 0 == handle->initialized )
2724 return NULL;
2729 const Polygon *p = dynamic_cast<const Polygon *>(g1);
2730 if ( ! p )
2732 handle->ERROR_MESSAGE("Invalid argument (must be a Polygon)");
2733 return NULL;
2735 return p->getExteriorRing();
2737 catch (const std::exception &e)
2739 handle->ERROR_MESSAGE("%s", e.what());
2741 catch (...)
2743 handle->ERROR_MESSAGE("Unknown exception thrown");
2746 return NULL;
2750 * Call only on polygon
2751 * Return a pointer to internal storage, do not destroy it.
2753 const Geometry *
2754 GEOSGetInteriorRingN_r(GEOSContextHandle_t extHandle, const Geometry *g1, int n)
2756 if ( 0 == extHandle )
2758 return NULL;
2761 GEOSContextHandleInternal_t *handle = 0;
2762 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
2763 if ( 0 == handle->initialized )
2765 return NULL;
2770 const Polygon *p = dynamic_cast<const Polygon *>(g1);
2771 if ( ! p )
2773 handle->ERROR_MESSAGE("Invalid argument (must be a Polygon)");
2774 return NULL;
2776 return p->getInteriorRingN(n);
2778 catch (const std::exception &e)
2780 handle->ERROR_MESSAGE("%s", e.what());
2782 catch (...)
2784 handle->ERROR_MESSAGE("Unknown exception thrown");
2787 return NULL;
2790 Geometry *
2791 GEOSGetCentroid_r(GEOSContextHandle_t extHandle, const Geometry *g)
2793 if ( 0 == extHandle )
2795 return NULL;
2798 GEOSContextHandleInternal_t *handle = 0;
2799 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
2800 if ( 0 == handle->initialized )
2802 return NULL;
2807 Geometry *ret = g->getCentroid();
2808 if (0 == ret)
2810 const GeometryFactory *gf = handle->geomFactory;
2811 return gf->createPoint();
2813 return ret;
2815 catch (const std::exception &e)
2817 handle->ERROR_MESSAGE("%s", e.what());
2819 catch (...)
2821 handle->ERROR_MESSAGE("Unknown exception thrown");
2824 return NULL;
2827 Geometry *
2828 GEOSGeom_createEmptyCollection_r(GEOSContextHandle_t extHandle, int type)
2830 if ( 0 == extHandle )
2832 return NULL;
2835 GEOSContextHandleInternal_t *handle = 0;
2836 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
2837 if ( 0 == handle->initialized )
2839 return NULL;
2842 #ifdef GEOS_DEBUG
2843 char buf[256];
2844 sprintf(buf, "createCollection: requested type %d, ngeoms: %d",
2845 type, ngeoms);
2846 handle->NOTICE_MESSAGE("%s", buf);// TODO: Can handle->NOTICE_MESSAGE format that directly?
2847 #endif
2851 const GeometryFactory* gf = handle->geomFactory;
2853 Geometry *g = 0;
2854 switch (type)
2856 case GEOS_GEOMETRYCOLLECTION:
2857 g = gf->createGeometryCollection();
2858 break;
2859 case GEOS_MULTIPOINT:
2860 g = gf->createMultiPoint();
2861 break;
2862 case GEOS_MULTILINESTRING:
2863 g = gf->createMultiLineString();
2864 break;
2865 case GEOS_MULTIPOLYGON:
2866 g = gf->createMultiPolygon();
2867 break;
2868 default:
2869 handle->ERROR_MESSAGE("Unsupported type request for GEOSGeom_createEmptyCollection_r");
2870 g = 0;
2874 return g;
2876 catch (const std::exception &e)
2878 handle->ERROR_MESSAGE("%s", e.what());
2880 catch (...)
2882 handle->ERROR_MESSAGE("Unknown exception thrown");
2885 return 0;
2888 Geometry *
2889 GEOSGeom_createCollection_r(GEOSContextHandle_t extHandle, int type, Geometry **geoms, unsigned int ngeoms)
2891 if ( 0 == extHandle )
2893 return NULL;
2896 GEOSContextHandleInternal_t *handle = 0;
2897 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
2898 if ( 0 == handle->initialized )
2900 return NULL;
2903 #ifdef GEOS_DEBUG
2904 char buf[256];
2905 sprintf(buf, "PostGIS2GEOS_collection: requested type %d, ngeoms: %d",
2906 type, ngeoms);
2907 handle->NOTICE_MESSAGE("%s", buf);// TODO: Can handle->NOTICE_MESSAGE format that directly?
2908 #endif
2912 const GeometryFactory* gf = handle->geomFactory;
2913 std::vector<Geometry*>* vgeoms = new std::vector<Geometry*>(geoms, geoms + ngeoms);
2915 Geometry *g = 0;
2916 switch (type)
2918 case GEOS_GEOMETRYCOLLECTION:
2919 g = gf->createGeometryCollection(vgeoms);
2920 break;
2921 case GEOS_MULTIPOINT:
2922 g = gf->createMultiPoint(vgeoms);
2923 break;
2924 case GEOS_MULTILINESTRING:
2925 g = gf->createMultiLineString(vgeoms);
2926 break;
2927 case GEOS_MULTIPOLYGON:
2928 g = gf->createMultiPolygon(vgeoms);
2929 break;
2930 default:
2931 handle->ERROR_MESSAGE("Unsupported type request for PostGIS2GEOS_collection");
2932 g = 0;
2936 return g;
2938 catch (const std::exception &e)
2940 handle->ERROR_MESSAGE("%s", e.what());
2942 catch (...)
2944 handle->ERROR_MESSAGE("Unknown exception thrown");
2947 return 0;
2950 Geometry *
2951 GEOSPolygonize_r(GEOSContextHandle_t extHandle, const Geometry * const * g, unsigned int ngeoms)
2953 if ( 0 == extHandle )
2955 return 0;
2958 GEOSContextHandleInternal_t *handle = 0;
2959 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
2960 if ( 0 == handle->initialized )
2962 return 0;
2965 Geometry *out = 0;
2969 // Polygonize
2970 using geos::operation::polygonize::Polygonizer;
2971 Polygonizer plgnzr;
2972 for (std::size_t i = 0; i < ngeoms; ++i)
2974 plgnzr.add(g[i]);
2977 #if GEOS_DEBUG
2978 handle->NOTICE_MESSAGE("geometry vector added to polygonizer");
2979 #endif
2981 std::vector<Polygon*> *polys = plgnzr.getPolygons();
2982 assert(0 != polys);
2984 #if GEOS_DEBUG
2985 handle->NOTICE_MESSAGE("output polygons got");
2986 #endif
2988 // We need a vector of Geometry pointers, not Polygon pointers.
2989 // STL vector doesn't allow transparent upcast of this
2990 // nature, so we explicitly convert.
2991 // (it's just a waste of processor and memory, btw)
2993 // XXX mloskot: Why not to extent GeometryFactory to accept
2994 // vector of polygons or extend Polygonizer to return list of Geometry*
2995 // or add a wrapper which semantic is similar to:
2996 // std::vector<as_polygon<Geometry*> >
2997 std::vector<Geometry*> *polyvec = new std::vector<Geometry *>(polys->size());
2999 for (std::size_t i = 0; i < polys->size(); ++i)
3001 (*polyvec)[i] = (*polys)[i];
3003 delete polys;
3004 polys = 0;
3006 const GeometryFactory *gf = handle->geomFactory;
3008 // The below takes ownership of the passed vector,
3009 // so we must *not* delete it
3010 out = gf->createGeometryCollection(polyvec);
3012 catch (const std::exception &e)
3014 handle->ERROR_MESSAGE("%s", e.what());
3016 catch (...)
3018 handle->ERROR_MESSAGE("Unknown exception thrown");
3021 return out;
3024 Geometry *
3025 GEOSPolygonizer_getCutEdges_r(GEOSContextHandle_t extHandle, const Geometry * const * g, unsigned int ngeoms)
3027 if ( 0 == extHandle )
3029 return 0;
3032 GEOSContextHandleInternal_t *handle = 0;
3033 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
3034 if ( 0 == handle->initialized )
3036 return 0;
3039 Geometry *out = 0;
3043 // Polygonize
3044 using geos::operation::polygonize::Polygonizer;
3045 Polygonizer plgnzr;
3046 for (std::size_t i = 0; i < ngeoms; ++i)
3048 plgnzr.add(g[i]);
3051 #if GEOS_DEBUG
3052 handle->NOTICE_MESSAGE("geometry vector added to polygonizer");
3053 #endif
3055 const std::vector<const LineString *>& lines = plgnzr.getCutEdges();
3057 #if GEOS_DEBUG
3058 handle->NOTICE_MESSAGE("output polygons got");
3059 #endif
3061 // We need a vector of Geometry pointers, not Polygon pointers.
3062 // STL vector doesn't allow transparent upcast of this
3063 // nature, so we explicitly convert.
3064 // (it's just a waste of processor and memory, btw)
3065 // XXX mloskot: See comment for GEOSPolygonize_r
3066 std::vector<Geometry*> *linevec = new std::vector<Geometry *>(lines.size());
3068 for (std::size_t i = 0, n=lines.size(); i < n; ++i)
3070 (*linevec)[i] = lines[i]->clone();
3073 const GeometryFactory *gf = handle->geomFactory;
3075 // The below takes ownership of the passed vector,
3076 // so we must *not* delete it
3077 out = gf->createGeometryCollection(linevec);
3079 catch (const std::exception &e)
3081 handle->ERROR_MESSAGE("%s", e.what());
3083 catch (...)
3085 handle->ERROR_MESSAGE("Unknown exception thrown");
3088 return out;
3091 Geometry *
3092 GEOSPolygonize_full_r(GEOSContextHandle_t extHandle, const Geometry* g,
3093 Geometry** cuts, Geometry** dangles, Geometry** invalid)
3095 if ( 0 == extHandle )
3097 return 0;
3100 GEOSContextHandleInternal_t *handle = 0;
3101 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
3102 if ( 0 == handle->initialized )
3104 return 0;
3109 // Polygonize
3110 using geos::operation::polygonize::Polygonizer;
3111 Polygonizer plgnzr;
3112 for (std::size_t i = 0; i <g->getNumGeometries(); ++i)
3114 plgnzr.add(g->getGeometryN(i));
3117 #if GEOS_DEBUG
3118 handle->NOTICE_MESSAGE("geometry vector added to polygonizer");
3119 #endif
3120 const GeometryFactory *gf = handle->geomFactory;
3122 if ( cuts ) {
3124 const std::vector<const LineString *>& lines = plgnzr.getCutEdges();
3125 std::vector<Geometry*> *linevec = new std::vector<Geometry *>(lines.size());
3126 for (std::size_t i = 0, n=lines.size(); i < n; ++i)
3128 (*linevec)[i] = lines[i]->clone();
3131 // The below takes ownership of the passed vector,
3132 // so we must *not* delete it
3133 *cuts = gf->createGeometryCollection(linevec);
3136 if ( dangles ) {
3138 const std::vector<const LineString *>& lines = plgnzr.getDangles();
3139 std::vector<Geometry*> *linevec = new std::vector<Geometry *>(lines.size());
3140 for (std::size_t i = 0, n=lines.size(); i < n; ++i)
3142 (*linevec)[i] = lines[i]->clone();
3145 // The below takes ownership of the passed vector,
3146 // so we must *not* delete it
3147 *dangles = gf->createGeometryCollection(linevec);
3150 if ( invalid ) {
3152 const std::vector<LineString *>& lines = plgnzr.getInvalidRingLines();
3153 std::vector<Geometry*> *linevec = new std::vector<Geometry *>(lines.size());
3154 for (std::size_t i = 0, n=lines.size(); i < n; ++i)
3156 (*linevec)[i] = lines[i]->clone();
3159 // The below takes ownership of the passed vector,
3160 // so we must *not* delete it
3161 *invalid = gf->createGeometryCollection(linevec);
3164 std::vector<Polygon*> *polys = plgnzr.getPolygons();
3165 std::vector<Geometry*> *polyvec = new std::vector<Geometry *>(polys->size());
3166 for (std::size_t i = 0; i < polys->size(); ++i)
3168 (*polyvec)[i] = (*polys)[i];
3170 delete polys;
3172 return gf->createGeometryCollection(polyvec);
3175 catch (const std::exception &e)
3177 handle->ERROR_MESSAGE("%s", e.what());
3178 return 0;
3180 catch (...)
3182 handle->ERROR_MESSAGE("Unknown exception thrown");
3183 return 0;
3187 Geometry *
3188 GEOSLineMerge_r(GEOSContextHandle_t extHandle, const Geometry *g)
3190 if ( 0 == extHandle )
3192 return 0;
3195 GEOSContextHandleInternal_t *handle = 0;
3196 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
3197 if ( 0 == handle->initialized )
3199 return 0;
3202 Geometry *out = 0;
3206 using geos::operation::linemerge::LineMerger;
3207 LineMerger lmrgr;
3208 lmrgr.add(g);
3210 std::vector<LineString *>* lines = lmrgr.getMergedLineStrings();
3211 assert(0 != lines);
3213 #if GEOS_DEBUG
3214 handle->NOTICE_MESSAGE("output lines got");
3215 #endif
3217 std::vector<Geometry *>*geoms = new std::vector<Geometry *>(lines->size());
3218 for (std::vector<Geometry *>::size_type i = 0; i < lines->size(); ++i)
3220 (*geoms)[i] = (*lines)[i];
3222 delete lines;
3223 lines = 0;
3225 const GeometryFactory *gf = handle->geomFactory;
3226 out = gf->buildGeometry(geoms);
3228 // XXX: old version
3229 //out = gf->createGeometryCollection(geoms);
3231 catch (const std::exception &e)
3233 handle->ERROR_MESSAGE("%s", e.what());
3235 catch (...)
3237 handle->ERROR_MESSAGE("Unknown exception thrown");
3240 return out;
3244 GEOSGetSRID_r(GEOSContextHandle_t extHandle, const Geometry *g)
3246 assert(0 != g);
3248 if ( 0 == extHandle )
3250 return 0;
3253 GEOSContextHandleInternal_t *handle = 0;
3254 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
3255 if ( 0 == handle->initialized )
3257 return 0;
3262 return g->getSRID();
3264 catch (const std::exception &e)
3266 handle->ERROR_MESSAGE("%s", e.what());
3268 catch (...)
3270 handle->ERROR_MESSAGE("Unknown exception thrown");
3273 return 0;
3276 const char* GEOSversion()
3278 static char version[256];
3279 sprintf(version, "%s r%d", GEOS_CAPI_VERSION, GEOS_SVN_REVISION);
3280 return version;
3283 const char* GEOSjtsport()
3285 return GEOS_JTS_PORT;
3288 char
3289 GEOSHasZ_r(GEOSContextHandle_t extHandle, const Geometry *g)
3291 assert(0 != g);
3293 if ( 0 == extHandle )
3295 return -1;
3298 GEOSContextHandleInternal_t *handle = 0;
3299 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
3300 if ( 0 == handle->initialized )
3302 return -1;
3305 if (g->isEmpty())
3307 return false;
3309 assert(0 != g->getCoordinate());
3311 double az = g->getCoordinate()->z;
3312 //handle->ERROR_MESSAGE("ZCoord: %g", az);
3314 return static_cast<char>(FINITE(az));
3318 GEOS_getWKBOutputDims_r(GEOSContextHandle_t extHandle)
3320 if ( 0 == extHandle )
3322 return -1;
3325 GEOSContextHandleInternal_t *handle = 0;
3326 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
3327 if ( 0 == handle->initialized )
3329 return -1;
3332 return handle->WKBOutputDims;
3336 GEOS_setWKBOutputDims_r(GEOSContextHandle_t extHandle, int newdims)
3338 if ( 0 == extHandle )
3340 return -1;
3343 GEOSContextHandleInternal_t *handle = 0;
3344 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
3345 if ( 0 == handle->initialized )
3347 return -1;
3350 if ( newdims < 2 || newdims > 3 )
3352 handle->ERROR_MESSAGE("WKB output dimensions out of range 2..3");
3355 const int olddims = handle->WKBOutputDims;
3356 handle->WKBOutputDims = newdims;
3358 return olddims;
3362 GEOS_getWKBByteOrder_r(GEOSContextHandle_t extHandle)
3364 if ( 0 == extHandle )
3366 return -1;
3369 GEOSContextHandleInternal_t *handle = 0;
3370 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
3371 if ( 0 == handle->initialized )
3373 return -1;
3376 return handle->WKBByteOrder;
3380 GEOS_setWKBByteOrder_r(GEOSContextHandle_t extHandle, int byteOrder)
3382 if ( 0 == extHandle )
3384 return -1;
3387 GEOSContextHandleInternal_t *handle = 0;
3388 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
3389 if ( 0 == handle->initialized )
3391 return -1;
3394 const int oldByteOrder = handle->WKBByteOrder;
3395 handle->WKBByteOrder = byteOrder;
3397 return oldByteOrder;
3401 CoordinateSequence *
3402 GEOSCoordSeq_create_r(GEOSContextHandle_t extHandle, unsigned int size, unsigned int dims)
3404 if ( 0 == extHandle )
3406 return NULL;
3409 GEOSContextHandleInternal_t *handle = 0;
3410 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
3411 if ( 0 == handle->initialized )
3413 return NULL;
3418 const GeometryFactory *gf = handle->geomFactory;
3419 return gf->getCoordinateSequenceFactory()->create(size, dims);
3421 catch (const std::exception &e)
3423 handle->ERROR_MESSAGE("%s", e.what());
3425 catch (...)
3427 handle->ERROR_MESSAGE("Unknown exception thrown");
3430 return NULL;
3434 GEOSCoordSeq_setOrdinate_r(GEOSContextHandle_t extHandle, CoordinateSequence *cs,
3435 unsigned int idx, unsigned int dim, double val)
3437 assert(0 != cs);
3438 if ( 0 == extHandle )
3440 return 0;
3443 GEOSContextHandleInternal_t *handle = 0;
3444 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
3445 if ( 0 == handle->initialized )
3447 return 0;
3452 cs->setOrdinate(idx, dim, val);
3453 return 1;
3455 catch (const std::exception &e)
3457 handle->ERROR_MESSAGE("%s", e.what());
3459 catch (...)
3461 handle->ERROR_MESSAGE("Unknown exception thrown");
3464 return 0;
3468 GEOSCoordSeq_setX_r(GEOSContextHandle_t extHandle, CoordinateSequence *s, unsigned int idx, double val)
3470 return GEOSCoordSeq_setOrdinate_r(extHandle, s, idx, 0, val);
3474 GEOSCoordSeq_setY_r(GEOSContextHandle_t extHandle, CoordinateSequence *s, unsigned int idx, double val)
3476 return GEOSCoordSeq_setOrdinate_r(extHandle, s, idx, 1, val);
3480 GEOSCoordSeq_setZ_r(GEOSContextHandle_t extHandle, CoordinateSequence *s, unsigned int idx, double val)
3482 return GEOSCoordSeq_setOrdinate_r(extHandle, s, idx, 2, val);
3485 CoordinateSequence *
3486 GEOSCoordSeq_clone_r(GEOSContextHandle_t extHandle, const CoordinateSequence *cs)
3488 assert(0 != cs);
3490 if ( 0 == extHandle )
3492 return NULL;
3495 GEOSContextHandleInternal_t *handle = 0;
3496 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
3497 if ( 0 == handle->initialized )
3499 return NULL;
3504 return cs->clone();
3506 catch (const std::exception &e)
3508 handle->ERROR_MESSAGE("%s", e.what());
3510 catch (...)
3512 handle->ERROR_MESSAGE("Unknown exception thrown");
3515 return NULL;
3519 GEOSCoordSeq_getOrdinate_r(GEOSContextHandle_t extHandle, const CoordinateSequence *cs,
3520 unsigned int idx, unsigned int dim, double *val)
3522 assert(0 != cs);
3523 assert(0 != val);
3525 if ( 0 == extHandle )
3527 return 0;
3530 GEOSContextHandleInternal_t *handle = 0;
3531 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
3532 if ( 0 == handle->initialized )
3534 return 0;
3539 double d = cs->getOrdinate(idx, dim);
3540 *val = d;
3542 return 1;
3544 catch (const std::exception &e)
3546 handle->ERROR_MESSAGE("%s", e.what());
3548 catch (...)
3550 handle->ERROR_MESSAGE("Unknown exception thrown");
3553 return 0;
3557 GEOSCoordSeq_getX_r(GEOSContextHandle_t extHandle, const CoordinateSequence *s, unsigned int idx, double *val)
3559 return GEOSCoordSeq_getOrdinate_r(extHandle, s, idx, 0, val);
3563 GEOSCoordSeq_getY_r(GEOSContextHandle_t extHandle, const CoordinateSequence *s, unsigned int idx, double *val)
3565 return GEOSCoordSeq_getOrdinate_r(extHandle, s, idx, 1, val);
3569 GEOSCoordSeq_getZ_r(GEOSContextHandle_t extHandle, const CoordinateSequence *s, unsigned int idx, double *val)
3571 return GEOSCoordSeq_getOrdinate_r(extHandle, s, idx, 2, val);
3575 GEOSCoordSeq_getSize_r(GEOSContextHandle_t extHandle, const CoordinateSequence *cs, unsigned int *size)
3577 assert(0 != cs);
3578 assert(0 != size);
3580 if ( 0 == extHandle )
3582 return 0;
3585 GEOSContextHandleInternal_t *handle = 0;
3586 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
3587 if ( 0 == handle->initialized )
3589 return 0;
3594 const std::size_t sz = cs->getSize();
3595 *size = static_cast<unsigned int>(sz);
3596 return 1;
3598 catch (const std::exception &e)
3600 handle->ERROR_MESSAGE("%s", e.what());
3602 catch (...)
3604 handle->ERROR_MESSAGE("Unknown exception thrown");
3607 return 0;
3611 GEOSCoordSeq_getDimensions_r(GEOSContextHandle_t extHandle, const CoordinateSequence *cs, unsigned int *dims)
3613 assert(0 != cs);
3614 assert(0 != dims);
3616 if ( 0 == extHandle )
3618 return 0;
3621 GEOSContextHandleInternal_t *handle = 0;
3622 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
3623 if ( 0 == handle->initialized )
3625 return 0;
3630 const std::size_t dim = cs->getDimension();
3631 *dims = static_cast<unsigned int>(dim);
3633 return 1;
3635 catch (const std::exception &e)
3637 handle->ERROR_MESSAGE("%s", e.what());
3640 catch (...)
3642 handle->ERROR_MESSAGE("Unknown exception thrown");
3645 return 0;
3648 void
3649 GEOSCoordSeq_destroy_r(GEOSContextHandle_t extHandle, CoordinateSequence *s)
3651 GEOSContextHandleInternal_t *handle = 0;
3655 delete s;
3657 catch (const std::exception &e)
3659 if ( 0 == extHandle )
3661 return;
3664 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
3665 if ( 0 == handle->initialized )
3667 return;
3670 handle->ERROR_MESSAGE("%s", e.what());
3672 catch (...)
3674 if ( 0 == extHandle )
3676 return;
3679 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
3680 if ( 0 == handle->initialized )
3682 return;
3685 handle->ERROR_MESSAGE("Unknown exception thrown");
3689 const CoordinateSequence *
3690 GEOSGeom_getCoordSeq_r(GEOSContextHandle_t extHandle, const Geometry *g)
3692 if ( 0 == extHandle )
3694 return 0;
3697 GEOSContextHandleInternal_t *handle = 0;
3698 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
3699 if ( 0 == handle->initialized )
3701 return 0;
3706 using geos::geom::Point;
3708 const LineString *ls = dynamic_cast<const LineString *>(g);
3709 if ( ls )
3711 return ls->getCoordinatesRO();
3714 const Point *p = dynamic_cast<const Point *>(g);
3715 if ( p )
3717 return p->getCoordinatesRO();
3720 handle->ERROR_MESSAGE("Geometry must be a Point or LineString");
3721 return 0;
3723 catch (const std::exception &e)
3725 handle->ERROR_MESSAGE("%s", e.what());
3727 catch (...)
3729 handle->ERROR_MESSAGE("Unknown exception thrown");
3732 return 0;
3735 Geometry *
3736 GEOSGeom_createEmptyPoint_r(GEOSContextHandle_t extHandle)
3738 if ( 0 == extHandle )
3740 return NULL;
3743 GEOSContextHandleInternal_t *handle = 0;
3744 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
3745 if ( 0 == handle->initialized )
3747 return NULL;
3752 const GeometryFactory *gf = handle->geomFactory;
3753 return gf->createPoint();
3755 catch (const std::exception &e)
3757 handle->ERROR_MESSAGE("%s", e.what());
3759 catch (...)
3761 handle->ERROR_MESSAGE("Unknown exception thrown");
3764 return NULL;
3767 Geometry *
3768 GEOSGeom_createPoint_r(GEOSContextHandle_t extHandle, CoordinateSequence *cs)
3770 if ( 0 == extHandle )
3772 return 0;
3775 GEOSContextHandleInternal_t *handle = 0;
3776 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
3777 if ( 0 == handle->initialized )
3779 return 0;
3784 const GeometryFactory *gf = handle->geomFactory;
3785 return gf->createPoint(cs);
3787 catch (const std::exception &e)
3789 handle->ERROR_MESSAGE("%s", e.what());
3791 catch (...)
3793 handle->ERROR_MESSAGE("Unknown exception thrown");
3796 return 0;
3799 Geometry *
3800 GEOSGeom_createLinearRing_r(GEOSContextHandle_t extHandle, CoordinateSequence *cs)
3802 if ( 0 == extHandle )
3804 return NULL;
3807 GEOSContextHandleInternal_t *handle = 0;
3808 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
3809 if ( 0 == handle->initialized )
3811 return NULL;
3816 const GeometryFactory *gf = handle->geomFactory;
3818 return gf->createLinearRing(cs);
3820 catch (const std::exception &e)
3822 handle->ERROR_MESSAGE("%s", e.what());
3824 catch (...)
3826 handle->ERROR_MESSAGE("Unknown exception thrown");
3829 return NULL;
3832 Geometry *
3833 GEOSGeom_createEmptyLineString_r(GEOSContextHandle_t extHandle)
3835 if ( 0 == extHandle )
3837 return NULL;
3840 GEOSContextHandleInternal_t *handle = 0;
3841 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
3842 if ( 0 == handle->initialized )
3844 return NULL;
3849 const GeometryFactory *gf = handle->geomFactory;
3851 return gf->createLineString();
3853 catch (const std::exception &e)
3855 handle->ERROR_MESSAGE("%s", e.what());
3857 catch (...)
3859 handle->ERROR_MESSAGE("Unknown exception thrown");
3862 return NULL;
3865 Geometry *
3866 GEOSGeom_createLineString_r(GEOSContextHandle_t extHandle, CoordinateSequence *cs)
3868 if ( 0 == extHandle )
3870 return NULL;
3873 GEOSContextHandleInternal_t *handle = 0;
3874 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
3875 if ( 0 == handle->initialized )
3877 return NULL;
3882 const GeometryFactory *gf = handle->geomFactory;
3884 return gf->createLineString(cs);
3886 catch (const std::exception &e)
3888 handle->ERROR_MESSAGE("%s", e.what());
3890 catch (...)
3892 handle->ERROR_MESSAGE("Unknown exception thrown");
3895 return NULL;
3898 Geometry *
3899 GEOSGeom_createEmptyPolygon_r(GEOSContextHandle_t extHandle)
3901 if ( 0 == extHandle )
3903 return NULL;
3906 GEOSContextHandleInternal_t *handle = 0;
3907 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
3908 if ( 0 == handle->initialized )
3910 return NULL;
3915 const GeometryFactory *gf = handle->geomFactory;
3916 return gf->createPolygon();
3918 catch (const std::exception &e)
3920 handle->ERROR_MESSAGE("%s", e.what());
3922 catch (...)
3924 handle->ERROR_MESSAGE("Unknown exception thrown");
3927 return NULL;
3930 Geometry *
3931 GEOSGeom_createPolygon_r(GEOSContextHandle_t extHandle, Geometry *shell, Geometry **holes, unsigned int nholes)
3933 // FIXME: holes must be non-nullptr or may be nullptr?
3934 //assert(0 != holes);
3936 if ( 0 == extHandle )
3938 return NULL;
3941 GEOSContextHandleInternal_t *handle = 0;
3942 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
3943 if ( 0 == handle->initialized )
3945 return NULL;
3950 using geos::geom::LinearRing;
3952 std::vector<Geometry *> *vholes = new std::vector<Geometry *>(holes, holes + nholes);
3954 LinearRing *nshell = dynamic_cast<LinearRing *>(shell);
3955 if ( ! nshell )
3957 handle->ERROR_MESSAGE("Shell is not a LinearRing");
3958 return NULL;
3960 const GeometryFactory *gf = handle->geomFactory;
3962 return gf->createPolygon(nshell, vholes);
3964 catch (const std::exception &e)
3966 handle->ERROR_MESSAGE("%s", e.what());
3968 catch (...)
3970 handle->ERROR_MESSAGE("Unknown exception thrown");
3973 return NULL;
3976 Geometry *
3977 GEOSGeom_clone_r(GEOSContextHandle_t extHandle, const Geometry *g)
3979 assert(0 != g);
3981 if ( 0 == extHandle )
3983 return NULL;
3986 GEOSContextHandleInternal_t *handle = 0;
3987 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
3988 if ( 0 == handle->initialized )
3990 return NULL;
3995 return g->clone();
3997 catch (const std::exception &e)
3999 handle->ERROR_MESSAGE("%s", e.what());
4001 catch (...)
4003 handle->ERROR_MESSAGE("Unknown exception thrown");
4006 return NULL;
4010 GEOSGeom_getDimensions_r(GEOSContextHandle_t extHandle, const Geometry *g)
4012 if ( 0 == extHandle )
4014 return 0;
4017 GEOSContextHandleInternal_t *handle = 0;
4018 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
4019 if ( 0 == handle->initialized )
4021 return 0;
4026 return (int) g->getDimension();
4028 catch (const std::exception &e)
4030 handle->ERROR_MESSAGE("%s", e.what());
4032 catch (...)
4034 handle->ERROR_MESSAGE("Unknown exception thrown");
4037 return 0;
4041 GEOSGeom_getCoordinateDimension_r(GEOSContextHandle_t extHandle, const Geometry *g)
4043 if ( 0 == extHandle )
4045 return 0;
4048 GEOSContextHandleInternal_t *handle = 0;
4049 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
4050 if ( 0 == handle->initialized )
4052 return 0;
4057 return g->getCoordinateDimension();
4059 catch (const std::exception &e)
4061 handle->ERROR_MESSAGE("%s", e.what());
4063 catch (...)
4065 handle->ERROR_MESSAGE("Unknown exception thrown");
4068 return 0;
4071 Geometry *
4072 GEOSSimplify_r(GEOSContextHandle_t extHandle, const Geometry *g1, double tolerance)
4074 if ( 0 == extHandle )
4076 return NULL;
4079 GEOSContextHandleInternal_t *handle = 0;
4080 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
4081 if ( 0 == handle->initialized )
4083 return NULL;
4088 using namespace geos::simplify;
4089 Geometry::AutoPtr g(DouglasPeuckerSimplifier::simplify(g1, tolerance));
4090 return g.release();
4092 catch (const std::exception &e)
4094 handle->ERROR_MESSAGE("%s", e.what());
4096 catch (...)
4098 handle->ERROR_MESSAGE("Unknown exception thrown");
4101 return NULL;
4104 Geometry *
4105 GEOSTopologyPreserveSimplify_r(GEOSContextHandle_t extHandle, const Geometry *g1, double tolerance)
4107 if ( 0 == extHandle )
4109 return NULL;
4112 GEOSContextHandleInternal_t *handle = 0;
4113 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
4114 if ( 0 == handle->initialized )
4116 return NULL;
4121 using namespace geos::simplify;
4122 Geometry::AutoPtr g(TopologyPreservingSimplifier::simplify(g1, tolerance));
4123 return g.release();
4125 catch (const std::exception &e)
4127 handle->ERROR_MESSAGE("%s", e.what());
4129 catch (...)
4131 handle->ERROR_MESSAGE("Unknown exception thrown");
4134 return NULL;
4138 /* WKT Reader */
4139 WKTReader *
4140 GEOSWKTReader_create_r(GEOSContextHandle_t extHandle)
4142 if ( 0 == extHandle )
4144 return NULL;
4147 GEOSContextHandleInternal_t *handle = 0;
4148 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
4149 if ( 0 == handle->initialized )
4151 return NULL;
4156 using geos::io::WKTReader;
4157 return new WKTReader((GeometryFactory*)handle->geomFactory);
4159 catch (const std::exception &e)
4161 handle->ERROR_MESSAGE("%s", e.what());
4163 catch (...)
4165 handle->ERROR_MESSAGE("Unknown exception thrown");
4168 return NULL;
4171 void
4172 GEOSWKTReader_destroy_r(GEOSContextHandle_t extHandle, WKTReader *reader)
4174 GEOSContextHandleInternal_t *handle = 0;
4178 delete reader;
4180 catch (const std::exception &e)
4182 if ( 0 == extHandle )
4184 return;
4187 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
4188 if ( 0 == handle->initialized )
4190 return;
4193 handle->ERROR_MESSAGE("%s", e.what());
4195 catch (...)
4197 if ( 0 == extHandle )
4199 return;
4202 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
4203 if ( 0 == handle->initialized )
4205 return;
4208 handle->ERROR_MESSAGE("Unknown exception thrown");
4213 Geometry*
4214 GEOSWKTReader_read_r(GEOSContextHandle_t extHandle, WKTReader *reader, const char *wkt)
4216 assert(0 != reader);
4218 if ( 0 == extHandle )
4220 return 0;
4223 GEOSContextHandleInternal_t *handle = 0;
4224 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
4225 if ( 0 == handle->initialized )
4227 return 0;
4232 const std::string wktstring(wkt);
4233 Geometry *g = reader->read(wktstring);
4234 return g;
4236 catch (const std::exception &e)
4238 handle->ERROR_MESSAGE("%s", e.what());
4240 catch (...)
4242 handle->ERROR_MESSAGE("Unknown exception thrown");
4245 return 0;
4248 /* WKT Writer */
4249 WKTWriter *
4250 GEOSWKTWriter_create_r(GEOSContextHandle_t extHandle)
4252 if ( 0 == extHandle )
4254 return 0;
4257 GEOSContextHandleInternal_t *handle = 0;
4258 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
4259 if ( 0 == handle->initialized )
4261 return 0;
4266 using geos::io::WKTWriter;
4267 return new WKTWriter();
4269 catch (const std::exception &e)
4271 handle->ERROR_MESSAGE("%s", e.what());
4273 catch (...)
4275 handle->ERROR_MESSAGE("Unknown exception thrown");
4278 return 0;
4281 void
4282 GEOSWKTWriter_destroy_r(GEOSContextHandle_t extHandle, WKTWriter *Writer)
4285 GEOSContextHandleInternal_t *handle = 0;
4289 delete Writer;
4291 catch (const std::exception &e)
4293 if ( 0 == extHandle )
4295 return;
4298 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
4299 if ( 0 == handle->initialized )
4301 return;
4304 handle->ERROR_MESSAGE("%s", e.what());
4306 catch (...)
4308 if ( 0 == extHandle )
4310 return;
4313 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
4314 if ( 0 == handle->initialized )
4316 return;
4319 handle->ERROR_MESSAGE("Unknown exception thrown");
4324 char*
4325 GEOSWKTWriter_write_r(GEOSContextHandle_t extHandle, WKTWriter *writer, const Geometry *geom)
4327 assert(0 != writer);
4329 if ( 0 == extHandle )
4331 return NULL;
4334 GEOSContextHandleInternal_t *handle = 0;
4335 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
4336 if ( 0 == handle->initialized )
4338 return NULL;
4343 std::string sgeom(writer->write(geom));
4344 char *result = gstrdup(sgeom);
4345 return result;
4347 catch (const std::exception &e)
4349 handle->ERROR_MESSAGE("%s", e.what());
4351 catch (...)
4353 handle->ERROR_MESSAGE("Unknown exception thrown");
4356 return NULL;
4359 void
4360 GEOSWKTWriter_setTrim_r(GEOSContextHandle_t extHandle, WKTWriter *writer, char trim)
4362 assert(0 != writer);
4364 if ( 0 == extHandle )
4366 return;
4369 GEOSContextHandleInternal_t *handle = 0;
4370 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
4371 if ( 0 == handle->initialized )
4373 return;
4376 writer->setTrim(0 != trim);
4379 void
4380 GEOSWKTWriter_setRoundingPrecision_r(GEOSContextHandle_t extHandle, WKTWriter *writer, int precision)
4382 assert(0 != writer);
4384 if ( 0 == extHandle )
4386 return;
4389 GEOSContextHandleInternal_t *handle = 0;
4390 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
4391 if ( 0 == handle->initialized )
4393 return;
4396 writer->setRoundingPrecision(precision);
4399 void
4400 GEOSWKTWriter_setOutputDimension_r(GEOSContextHandle_t extHandle, WKTWriter *writer, int dim)
4402 assert(0 != writer);
4404 if ( 0 == extHandle )
4406 return;
4409 GEOSContextHandleInternal_t *handle = 0;
4410 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
4411 if ( 0 == handle->initialized )
4413 return;
4418 writer->setOutputDimension(dim);
4420 catch (const std::exception &e)
4422 handle->ERROR_MESSAGE("%s", e.what());
4424 catch (...)
4426 handle->ERROR_MESSAGE("Unknown exception thrown");
4431 GEOSWKTWriter_getOutputDimension_r(GEOSContextHandle_t extHandle, WKTWriter *writer)
4433 assert(0 != writer);
4435 if ( 0 == extHandle )
4437 return -1;
4440 GEOSContextHandleInternal_t *handle = 0;
4441 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
4442 if ( 0 == handle->initialized )
4444 return -1;
4447 int dim = -1;
4451 dim = writer->getOutputDimension();
4453 catch (const std::exception &e)
4455 handle->ERROR_MESSAGE("%s", e.what());
4457 catch (...)
4459 handle->ERROR_MESSAGE("Unknown exception thrown");
4462 return dim;
4465 void
4466 GEOSWKTWriter_setOld3D_r(GEOSContextHandle_t extHandle, WKTWriter *writer, int useOld3D)
4468 assert(0 != writer);
4470 if ( 0 == extHandle )
4472 return;
4475 GEOSContextHandleInternal_t *handle = 0;
4476 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
4477 if ( 0 == handle->initialized )
4479 return;
4482 writer->setOld3D(0 != useOld3D);
4485 /* WKB Reader */
4486 WKBReader *
4487 GEOSWKBReader_create_r(GEOSContextHandle_t extHandle)
4489 if ( 0 == extHandle )
4491 return NULL;
4494 GEOSContextHandleInternal_t *handle = 0;
4495 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
4496 if ( 0 == handle->initialized )
4498 return NULL;
4501 using geos::io::WKBReader;
4504 return new WKBReader(*(GeometryFactory*)handle->geomFactory);
4506 catch (const std::exception &e)
4508 handle->ERROR_MESSAGE("%s", e.what());
4510 catch (...)
4512 handle->ERROR_MESSAGE("Unknown exception thrown");
4515 return NULL;
4518 void
4519 GEOSWKBReader_destroy_r(GEOSContextHandle_t extHandle, WKBReader *reader)
4521 GEOSContextHandleInternal_t *handle = 0;
4525 delete reader;
4527 catch (const std::exception &e)
4529 if ( 0 == extHandle )
4531 return;
4534 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
4535 if ( 0 == handle->initialized )
4537 return;
4540 handle->ERROR_MESSAGE("%s", e.what());
4542 catch (...)
4544 if ( 0 == extHandle )
4546 return;
4549 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
4550 if ( 0 == handle->initialized )
4552 return;
4555 handle->ERROR_MESSAGE("Unknown exception thrown");
4559 struct membuf : public std::streambuf
4561 membuf(char* s, std::size_t n)
4563 setg(s, s, s + n);
4567 Geometry*
4568 GEOSWKBReader_read_r(GEOSContextHandle_t extHandle, WKBReader *reader, const unsigned char *wkb, size_t size)
4570 assert(0 != reader);
4571 assert(0 != wkb);
4573 if ( 0 == extHandle )
4575 return 0;
4578 GEOSContextHandleInternal_t *handle = 0;
4579 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
4580 if ( 0 == handle->initialized )
4582 return 0;
4587 //std::string wkbstring(reinterpret_cast<const char*>(wkb), size); // make it binary !
4588 //std::istringstream is(std::ios_base::binary);
4589 //is.str(wkbstring);
4590 //is.seekg(0, std::ios::beg); // rewind reader pointer
4592 // http://stackoverflow.com/questions/2079912/simpler-way-to-create-a-c-memorystream-from-char-size-t-without-copying-t
4593 membuf mb((char*)wkb, size);
4594 istream is(&mb);
4596 Geometry *g = reader->read(is);
4597 return g;
4599 catch (const std::exception &e)
4601 handle->ERROR_MESSAGE("%s", e.what());
4603 catch (...)
4605 handle->ERROR_MESSAGE("Unknown exception thrown");
4608 return 0;
4611 Geometry*
4612 GEOSWKBReader_readHEX_r(GEOSContextHandle_t extHandle, WKBReader *reader, const unsigned char *hex, size_t size)
4614 assert(0 != reader);
4615 assert(0 != hex);
4617 if ( 0 == extHandle )
4619 return 0;
4622 GEOSContextHandleInternal_t *handle = 0;
4623 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
4624 if ( 0 == handle->initialized )
4626 return 0;
4631 std::string hexstring(reinterpret_cast<const char*>(hex), size);
4632 std::istringstream is(std::ios_base::binary);
4633 is.str(hexstring);
4634 is.seekg(0, std::ios::beg); // rewind reader pointer
4636 Geometry *g = reader->readHEX(is);
4637 return g;
4639 catch (const std::exception &e)
4641 handle->ERROR_MESSAGE("%s", e.what());
4643 catch (...)
4645 handle->ERROR_MESSAGE("Unknown exception thrown");
4648 return 0;
4651 /* WKB Writer */
4652 WKBWriter *
4653 GEOSWKBWriter_create_r(GEOSContextHandle_t extHandle)
4655 if ( 0 == extHandle )
4657 return NULL;
4660 GEOSContextHandleInternal_t *handle = 0;
4661 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
4662 if ( 0 == handle->initialized )
4664 return NULL;
4669 using geos::io::WKBWriter;
4670 return new WKBWriter();
4672 catch (const std::exception &e)
4674 handle->ERROR_MESSAGE("%s", e.what());
4676 catch (...)
4678 handle->ERROR_MESSAGE("Unknown exception thrown");
4681 return NULL;
4684 void
4685 GEOSWKBWriter_destroy_r(GEOSContextHandle_t extHandle, WKBWriter *Writer)
4687 GEOSContextHandleInternal_t *handle = 0;
4691 delete Writer;
4693 catch (const std::exception &e)
4695 if ( 0 == extHandle )
4697 return;
4700 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
4701 if ( 0 == handle->initialized )
4703 return;
4706 handle->ERROR_MESSAGE("%s", e.what());
4708 catch (...)
4710 if ( 0 == extHandle )
4712 return;
4715 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
4716 if ( 0 == handle->initialized )
4718 return;
4721 handle->ERROR_MESSAGE("Unknown exception thrown");
4726 /* The caller owns the result */
4727 unsigned char*
4728 GEOSWKBWriter_write_r(GEOSContextHandle_t extHandle, WKBWriter *writer, const Geometry *geom, size_t *size)
4730 assert(0 != writer);
4731 assert(0 != geom);
4732 assert(0 != size);
4734 if ( 0 == extHandle )
4736 return NULL;
4739 GEOSContextHandleInternal_t *handle = 0;
4740 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
4741 if ( 0 == handle->initialized )
4743 return NULL;
4748 std::ostringstream os(std::ios_base::binary);
4749 writer->write(*geom, os);
4751 const std::string& wkbstring = os.str();
4752 const std::size_t len = wkbstring.length();
4754 unsigned char *result = NULL;
4755 result = (unsigned char*) malloc(len);
4756 std::memcpy(result, wkbstring.c_str(), len);
4757 *size = len;
4758 return result;
4760 catch (const std::exception &e)
4762 handle->ERROR_MESSAGE("%s", e.what());
4764 catch (...)
4766 handle->ERROR_MESSAGE("Unknown exception thrown");
4768 return NULL;
4771 /* The caller owns the result */
4772 unsigned char*
4773 GEOSWKBWriter_writeHEX_r(GEOSContextHandle_t extHandle, WKBWriter *writer, const Geometry *geom, size_t *size)
4775 assert(0 != writer);
4776 assert(0 != geom);
4777 assert(0 != size);
4779 if ( 0 == extHandle )
4781 return NULL;
4784 GEOSContextHandleInternal_t *handle = 0;
4785 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
4786 if ( 0 == handle->initialized )
4788 return NULL;
4793 std::ostringstream os(std::ios_base::binary);
4794 writer->writeHEX(*geom, os);
4795 std::string wkbstring(os.str());
4796 const std::size_t len = wkbstring.length();
4798 unsigned char *result = NULL;
4799 result = (unsigned char*) malloc(len);
4800 std::memcpy(result, wkbstring.c_str(), len);
4801 *size = len;
4802 return result;
4804 catch (const std::exception &e)
4806 handle->ERROR_MESSAGE("%s", e.what());
4808 catch (...)
4810 handle->ERROR_MESSAGE("Unknown exception thrown");
4813 return NULL;
4817 GEOSWKBWriter_getOutputDimension_r(GEOSContextHandle_t extHandle, const GEOSWKBWriter* writer)
4819 assert(0 != writer);
4821 if ( 0 == extHandle )
4823 return 0;
4826 int ret = 0;
4828 GEOSContextHandleInternal_t *handle = 0;
4829 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
4830 if ( 0 != handle->initialized )
4834 ret = writer->getOutputDimension();
4836 catch (...)
4838 handle->ERROR_MESSAGE("Unknown exception thrown");
4842 return ret;
4845 void
4846 GEOSWKBWriter_setOutputDimension_r(GEOSContextHandle_t extHandle, GEOSWKBWriter* writer, int newDimension)
4848 assert(0 != writer);
4850 if ( 0 == extHandle )
4852 return;
4855 GEOSContextHandleInternal_t *handle = 0;
4856 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
4857 if ( 0 != handle->initialized )
4861 writer->setOutputDimension(newDimension);
4863 catch (const std::exception &e)
4865 handle->ERROR_MESSAGE("%s", e.what());
4867 catch (...)
4869 handle->ERROR_MESSAGE("Unknown exception thrown");
4875 GEOSWKBWriter_getByteOrder_r(GEOSContextHandle_t extHandle, const GEOSWKBWriter* writer)
4877 assert(0 != writer);
4879 if ( 0 == extHandle )
4881 return 0;
4884 int ret = 0;
4886 GEOSContextHandleInternal_t *handle = 0;
4887 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
4888 if ( 0 != handle->initialized )
4892 ret = writer->getByteOrder();
4895 catch (...)
4897 handle->ERROR_MESSAGE("Unknown exception thrown");
4901 return ret;
4904 void
4905 GEOSWKBWriter_setByteOrder_r(GEOSContextHandle_t extHandle, GEOSWKBWriter* writer, int newByteOrder)
4907 assert(0 != writer);
4909 if ( 0 == extHandle )
4911 return;
4914 GEOSContextHandleInternal_t *handle = 0;
4915 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
4916 if ( 0 != handle->initialized )
4920 writer->setByteOrder(newByteOrder);
4922 catch (const std::exception &e)
4924 handle->ERROR_MESSAGE("%s", e.what());
4926 catch (...)
4928 handle->ERROR_MESSAGE("Unknown exception thrown");
4933 char
4934 GEOSWKBWriter_getIncludeSRID_r(GEOSContextHandle_t extHandle, const GEOSWKBWriter* writer)
4936 assert(0 != writer);
4938 if ( 0 == extHandle )
4940 return -1;
4943 int ret = -1;
4945 GEOSContextHandleInternal_t *handle = 0;
4946 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
4947 if ( 0 != handle->initialized )
4951 int srid = writer->getIncludeSRID();
4952 ret = srid;
4954 catch (...)
4956 handle->ERROR_MESSAGE("Unknown exception thrown");
4960 return static_cast<char>(ret);
4963 void
4964 GEOSWKBWriter_setIncludeSRID_r(GEOSContextHandle_t extHandle, GEOSWKBWriter* writer, const char newIncludeSRID)
4966 assert(0 != writer);
4968 if ( 0 == extHandle )
4970 return;
4973 GEOSContextHandleInternal_t *handle = 0;
4974 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
4975 if ( 0 != handle->initialized )
4979 writer->setIncludeSRID(newIncludeSRID);
4981 catch (...)
4983 handle->ERROR_MESSAGE("Unknown exception thrown");
4989 //-----------------------------------------------------------------
4990 // Prepared Geometry
4991 //-----------------------------------------------------------------
4993 const geos::geom::prep::PreparedGeometry*
4994 GEOSPrepare_r(GEOSContextHandle_t extHandle, const Geometry *g)
4996 if ( 0 == extHandle )
4998 return 0;
5001 GEOSContextHandleInternal_t *handle = 0;
5002 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
5003 if ( 0 == handle->initialized )
5005 return 0;
5008 const geos::geom::prep::PreparedGeometry* prep = 0;
5012 prep = geos::geom::prep::PreparedGeometryFactory::prepare(g);
5014 catch (const std::exception &e)
5016 handle->ERROR_MESSAGE("%s", e.what());
5018 catch (...)
5020 handle->ERROR_MESSAGE("Unknown exception thrown");
5023 return prep;
5026 void
5027 GEOSPreparedGeom_destroy_r(GEOSContextHandle_t extHandle, const geos::geom::prep::PreparedGeometry *a)
5029 GEOSContextHandleInternal_t *handle = 0;
5033 delete a;
5035 catch (const std::exception &e)
5037 if ( 0 == extHandle )
5039 return;
5042 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
5043 if ( 0 == handle->initialized )
5045 return;
5048 handle->ERROR_MESSAGE("%s", e.what());
5050 catch (...)
5052 if ( 0 == extHandle )
5054 return;
5057 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
5058 if ( 0 == handle->initialized )
5060 return;
5063 handle->ERROR_MESSAGE("Unknown exception thrown");
5067 char
5068 GEOSPreparedContains_r(GEOSContextHandle_t extHandle,
5069 const geos::geom::prep::PreparedGeometry *pg, const Geometry *g)
5071 assert(0 != pg);
5072 assert(0 != g);
5074 if ( 0 == extHandle )
5076 return 2;
5079 GEOSContextHandleInternal_t *handle = 0;
5080 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
5081 if ( 0 == handle->initialized )
5083 return 2;
5086 try
5088 bool result = pg->contains(g);
5089 return result;
5091 catch (const std::exception &e)
5093 handle->ERROR_MESSAGE("%s", e.what());
5095 catch (...)
5097 handle->ERROR_MESSAGE("Unknown exception thrown");
5100 return 2;
5103 char
5104 GEOSPreparedContainsProperly_r(GEOSContextHandle_t extHandle,
5105 const geos::geom::prep::PreparedGeometry *pg, const Geometry *g)
5107 assert(0 != pg);
5108 assert(0 != g);
5110 if ( 0 == extHandle )
5112 return 2;
5115 GEOSContextHandleInternal_t *handle = 0;
5116 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
5117 if ( 0 == handle->initialized )
5119 return 2;
5122 try
5124 bool result = pg->containsProperly(g);
5125 return result;
5127 catch (const std::exception &e)
5129 handle->ERROR_MESSAGE("%s", e.what());
5131 catch (...)
5133 handle->ERROR_MESSAGE("Unknown exception thrown");
5136 return 2;
5139 char
5140 GEOSPreparedCoveredBy_r(GEOSContextHandle_t extHandle,
5141 const geos::geom::prep::PreparedGeometry *pg, const Geometry *g)
5143 assert(0 != pg);
5144 assert(0 != g);
5146 if ( 0 == extHandle )
5148 return 2;
5151 GEOSContextHandleInternal_t *handle = 0;
5152 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
5153 if ( 0 == handle->initialized )
5155 return 2;
5158 try
5160 bool result = pg->coveredBy(g);
5161 return result;
5163 catch (const std::exception &e)
5165 handle->ERROR_MESSAGE("%s", e.what());
5167 catch (...)
5169 handle->ERROR_MESSAGE("Unknown exception thrown");
5172 return 2;
5175 char
5176 GEOSPreparedCovers_r(GEOSContextHandle_t extHandle,
5177 const geos::geom::prep::PreparedGeometry *pg, const Geometry *g)
5179 assert(0 != pg);
5180 assert(0 != g);
5182 if ( 0 == extHandle )
5184 return 2;
5187 GEOSContextHandleInternal_t *handle = 0;
5188 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
5189 if ( 0 == handle->initialized )
5191 return 2;
5194 try
5196 bool result = pg->covers(g);
5197 return result;
5199 catch (const std::exception &e)
5201 handle->ERROR_MESSAGE("%s", e.what());
5203 catch (...)
5205 handle->ERROR_MESSAGE("Unknown exception thrown");
5208 return 2;
5211 char
5212 GEOSPreparedCrosses_r(GEOSContextHandle_t extHandle,
5213 const geos::geom::prep::PreparedGeometry *pg, const Geometry *g)
5215 assert(0 != pg);
5216 assert(0 != g);
5218 if ( 0 == extHandle )
5220 return 2;
5223 GEOSContextHandleInternal_t *handle = 0;
5224 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
5225 if ( 0 == handle->initialized )
5227 return 2;
5230 try
5232 bool result = pg->crosses(g);
5233 return result;
5235 catch (const std::exception &e)
5237 handle->ERROR_MESSAGE("%s", e.what());
5239 catch (...)
5241 handle->ERROR_MESSAGE("Unknown exception thrown");
5244 return 2;
5247 char
5248 GEOSPreparedDisjoint_r(GEOSContextHandle_t extHandle,
5249 const geos::geom::prep::PreparedGeometry *pg, const Geometry *g)
5251 assert(0 != pg);
5252 assert(0 != g);
5254 if ( 0 == extHandle )
5256 return 2;
5259 GEOSContextHandleInternal_t *handle = 0;
5260 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
5261 if ( 0 == handle->initialized )
5263 return 2;
5266 try
5268 bool result = pg->disjoint(g);
5269 return result;
5271 catch (const std::exception &e)
5273 handle->ERROR_MESSAGE("%s", e.what());
5275 catch (...)
5277 handle->ERROR_MESSAGE("Unknown exception thrown");
5280 return 2;
5283 char
5284 GEOSPreparedIntersects_r(GEOSContextHandle_t extHandle,
5285 const geos::geom::prep::PreparedGeometry *pg, const Geometry *g)
5287 assert(0 != pg);
5288 assert(0 != g);
5290 if ( 0 == extHandle )
5292 return 2;
5295 GEOSContextHandleInternal_t *handle = 0;
5296 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
5297 if ( 0 == handle->initialized )
5299 return 2;
5302 try
5304 bool result = pg->intersects(g);
5305 return result;
5307 catch (const std::exception &e)
5309 handle->ERROR_MESSAGE("%s", e.what());
5311 catch (...)
5313 handle->ERROR_MESSAGE("Unknown exception thrown");
5316 return 2;
5319 char
5320 GEOSPreparedOverlaps_r(GEOSContextHandle_t extHandle,
5321 const geos::geom::prep::PreparedGeometry *pg, const Geometry *g)
5323 assert(0 != pg);
5324 assert(0 != g);
5326 if ( 0 == extHandle )
5328 return 2;
5331 GEOSContextHandleInternal_t *handle = 0;
5332 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
5333 if ( 0 == handle->initialized )
5335 return 2;
5338 try
5340 bool result = pg->overlaps(g);
5341 return result;
5343 catch (const std::exception &e)
5345 handle->ERROR_MESSAGE("%s", e.what());
5347 catch (...)
5349 handle->ERROR_MESSAGE("Unknown exception thrown");
5352 return 2;
5355 char
5356 GEOSPreparedTouches_r(GEOSContextHandle_t extHandle,
5357 const geos::geom::prep::PreparedGeometry *pg, const Geometry *g)
5359 assert(0 != pg);
5360 assert(0 != g);
5362 if ( 0 == extHandle )
5364 return 2;
5367 GEOSContextHandleInternal_t *handle = 0;
5368 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
5369 if ( 0 == handle->initialized )
5371 return 2;
5374 try
5376 bool result = pg->touches(g);
5377 return result;
5379 catch (const std::exception &e)
5381 handle->ERROR_MESSAGE("%s", e.what());
5383 catch (...)
5385 handle->ERROR_MESSAGE("Unknown exception thrown");
5388 return 2;
5391 char
5392 GEOSPreparedWithin_r(GEOSContextHandle_t extHandle,
5393 const geos::geom::prep::PreparedGeometry *pg, const Geometry *g)
5395 assert(0 != pg);
5396 assert(0 != g);
5398 if ( 0 == extHandle )
5400 return 2;
5403 GEOSContextHandleInternal_t *handle = 0;
5404 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
5405 if ( 0 == handle->initialized )
5407 return 2;
5410 try
5412 bool result = pg->within(g);
5413 return result;
5415 catch (const std::exception &e)
5417 handle->ERROR_MESSAGE("%s", e.what());
5419 catch (...)
5421 handle->ERROR_MESSAGE("Unknown exception thrown");
5424 return 2;
5427 //-----------------------------------------------------------------
5428 // STRtree
5429 //-----------------------------------------------------------------
5431 geos::index::strtree::STRtree *
5432 GEOSSTRtree_create_r(GEOSContextHandle_t extHandle,
5433 size_t nodeCapacity)
5435 if ( 0 == extHandle )
5437 return 0;
5440 GEOSContextHandleInternal_t *handle = 0;
5441 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
5442 if ( 0 == handle->initialized )
5444 return 0;
5447 geos::index::strtree::STRtree *tree = 0;
5451 tree = new geos::index::strtree::STRtree(nodeCapacity);
5453 catch (const std::exception &e)
5455 handle->ERROR_MESSAGE("%s", e.what());
5457 catch (...)
5459 handle->ERROR_MESSAGE("Unknown exception thrown");
5462 return tree;
5465 void
5466 GEOSSTRtree_insert_r(GEOSContextHandle_t extHandle,
5467 geos::index::strtree::STRtree *tree,
5468 const geos::geom::Geometry *g,
5469 void *item)
5471 GEOSContextHandleInternal_t *handle = 0;
5472 assert(tree != 0);
5473 assert(g != 0);
5477 tree->insert(g->getEnvelopeInternal(), item);
5479 catch (const std::exception &e)
5481 if ( 0 == extHandle )
5483 return;
5486 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
5487 if ( 0 == handle->initialized )
5489 return;
5492 handle->ERROR_MESSAGE("%s", e.what());
5494 catch (...)
5496 if ( 0 == extHandle )
5498 return;
5501 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
5502 if ( 0 == handle->initialized )
5504 return;
5507 handle->ERROR_MESSAGE("Unknown exception thrown");
5511 void
5512 GEOSSTRtree_query_r(GEOSContextHandle_t extHandle,
5513 geos::index::strtree::STRtree *tree,
5514 const geos::geom::Geometry *g,
5515 GEOSQueryCallback callback,
5516 void *userdata)
5518 GEOSContextHandleInternal_t *handle = 0;
5519 assert(tree != 0);
5520 assert(g != 0);
5521 assert(callback != 0);
5525 CAPI_ItemVisitor visitor(callback, userdata);
5526 tree->query(g->getEnvelopeInternal(), visitor);
5528 catch (const std::exception &e)
5530 if ( 0 == extHandle )
5532 return;
5535 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
5536 if ( 0 == handle->initialized )
5538 return;
5541 handle->ERROR_MESSAGE("%s", e.what());
5543 catch (...)
5545 if ( 0 == extHandle )
5547 return;
5550 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
5551 if ( 0 == handle->initialized )
5553 return;
5556 handle->ERROR_MESSAGE("Unknown exception thrown");
5560 void
5561 GEOSSTRtree_iterate_r(GEOSContextHandle_t extHandle,
5562 geos::index::strtree::STRtree *tree,
5563 GEOSQueryCallback callback,
5564 void *userdata)
5566 GEOSContextHandleInternal_t *handle = 0;
5567 assert(tree != 0);
5568 assert(callback != 0);
5572 CAPI_ItemVisitor visitor(callback, userdata);
5573 tree->iterate(visitor);
5575 catch (const std::exception &e)
5577 if ( 0 == extHandle )
5579 return;
5582 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
5583 if ( 0 == handle->initialized )
5585 return;
5588 handle->ERROR_MESSAGE("%s", e.what());
5590 catch (...)
5592 if ( 0 == extHandle )
5594 return;
5597 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
5598 if ( 0 == handle->initialized )
5600 return;
5603 handle->ERROR_MESSAGE("Unknown exception thrown");
5607 char
5608 GEOSSTRtree_remove_r(GEOSContextHandle_t extHandle,
5609 geos::index::strtree::STRtree *tree,
5610 const geos::geom::Geometry *g,
5611 void *item)
5613 assert(0 != tree);
5614 assert(0 != g);
5616 if ( 0 == extHandle )
5618 return 2;
5621 GEOSContextHandleInternal_t *handle = 0;
5622 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
5623 if ( 0 == handle->initialized )
5625 return 2;
5628 try
5630 bool result = tree->remove(g->getEnvelopeInternal(), item);
5631 return result;
5633 catch (const std::exception &e)
5635 handle->ERROR_MESSAGE("%s", e.what());
5637 catch (...)
5639 handle->ERROR_MESSAGE("Unknown exception thrown");
5642 return 2;
5645 void
5646 GEOSSTRtree_destroy_r(GEOSContextHandle_t extHandle,
5647 geos::index::strtree::STRtree *tree)
5649 GEOSContextHandleInternal_t *handle = 0;
5653 delete tree;
5655 catch (const std::exception &e)
5657 if ( 0 == extHandle )
5659 return;
5662 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
5663 if ( 0 == handle->initialized )
5665 return;
5668 handle->ERROR_MESSAGE("%s", e.what());
5670 catch (...)
5672 if ( 0 == extHandle )
5674 return;
5677 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
5678 if ( 0 == handle->initialized )
5680 return;
5683 handle->ERROR_MESSAGE("Unknown exception thrown");
5687 double
5688 GEOSProject_r(GEOSContextHandle_t extHandle,
5689 const Geometry *g,
5690 const Geometry *p)
5692 if ( 0 == extHandle ) return -1.0;
5693 GEOSContextHandleInternal_t *handle =
5694 reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
5695 if ( handle->initialized == 0 ) return -1.0;
5697 const geos::geom::Point* point = dynamic_cast<const geos::geom::Point*>(p);
5698 if (!point) {
5699 handle->ERROR_MESSAGE("third argument of GEOSProject_r must be Point*");
5700 return -1.0;
5703 const geos::geom::Coordinate* inputPt = p->getCoordinate();
5705 try {
5706 return geos::linearref::LengthIndexedLine(g).project(*inputPt);
5707 } catch (const std::exception &e) {
5708 handle->ERROR_MESSAGE("%s", e.what());
5709 return -1.0;
5710 } catch (...) {
5711 handle->ERROR_MESSAGE("Unknown exception thrown");
5712 return -1.0;
5717 Geometry*
5718 GEOSInterpolate_r(GEOSContextHandle_t extHandle, const Geometry *g, double d)
5720 if ( 0 == extHandle ) return 0;
5721 GEOSContextHandleInternal_t *handle =
5722 reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
5723 if ( handle->initialized == 0 ) return 0;
5725 try {
5726 geos::linearref::LengthIndexedLine lil(g);
5727 geos::geom::Coordinate coord = lil.extractPoint(d);
5728 const GeometryFactory *gf = handle->geomFactory;
5729 Geometry* point = gf->createPoint(coord);
5730 return point;
5731 } catch (const std::exception &e) {
5732 handle->ERROR_MESSAGE("%s", e.what());
5733 return 0;
5734 } catch (...) {
5735 handle->ERROR_MESSAGE("Unknown exception thrown");
5736 return 0;
5741 double
5742 GEOSProjectNormalized_r(GEOSContextHandle_t extHandle, const Geometry *g,
5743 const Geometry *p)
5746 double length;
5747 GEOSLength_r(extHandle, g, &length);
5748 return GEOSProject_r(extHandle, g, p) / length;
5752 Geometry*
5753 GEOSInterpolateNormalized_r(GEOSContextHandle_t extHandle, const Geometry *g,
5754 double d)
5756 double length;
5757 GEOSLength_r(extHandle, g, &length);
5758 return GEOSInterpolate_r(extHandle, g, d * length);
5761 GEOSGeometry*
5762 GEOSGeom_extractUniquePoints_r(GEOSContextHandle_t extHandle,
5763 const GEOSGeometry* g)
5765 if ( 0 == extHandle ) return 0;
5766 GEOSContextHandleInternal_t *handle = 0;
5767 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
5768 if ( handle->initialized == 0 ) return 0;
5770 using namespace geos::geom;
5771 using namespace geos::util;
5776 /* 1: extract points */
5777 std::vector<const Coordinate*> coords;
5778 UniqueCoordinateArrayFilter filter(coords);
5779 g->apply_ro(&filter);
5781 /* 2: for each point, create a geometry and put into a vector */
5782 std::vector<Geometry*>* points = new std::vector<Geometry*>();
5783 points->reserve(coords.size());
5784 const GeometryFactory* factory = g->getFactory();
5785 for (std::vector<const Coordinate*>::iterator it=coords.begin(),
5786 itE=coords.end();
5787 it != itE; ++it)
5789 Geometry* point = factory->createPoint(*(*it));
5790 points->push_back(point);
5793 /* 3: create a multipoint */
5794 return factory->createMultiPoint(points);
5797 catch (const std::exception &e)
5799 handle->ERROR_MESSAGE("%s", e.what());
5800 return 0;
5802 catch (...)
5804 handle->ERROR_MESSAGE("Unknown exception thrown");
5805 return 0;
5809 int GEOSOrientationIndex_r(GEOSContextHandle_t extHandle,
5810 double Ax, double Ay, double Bx, double By, double Px, double Py)
5812 GEOSContextHandleInternal_t *handle = 0;
5814 using geos::geom::Coordinate;
5815 using geos::algorithm::CGAlgorithms;
5817 if ( 0 == extHandle )
5819 return 2;
5822 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
5823 if ( 0 == handle->initialized )
5825 return 2;
5830 Coordinate A(Ax, Ay);
5831 Coordinate B(Bx, By);
5832 Coordinate P(Px, Py);
5833 return CGAlgorithms::orientationIndex(A, B, P);
5835 catch (const std::exception &e)
5837 handle->ERROR_MESSAGE("%s", e.what());
5838 return 2;
5840 catch (...)
5842 handle->ERROR_MESSAGE("Unknown exception thrown");
5843 return 2;
5847 GEOSGeometry *
5848 GEOSSharedPaths_r(GEOSContextHandle_t extHandle, const GEOSGeometry* g1, const GEOSGeometry* g2)
5850 using namespace geos::operation::sharedpaths;
5852 if ( 0 == extHandle ) return 0;
5853 GEOSContextHandleInternal_t *handle =
5854 reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
5855 if ( handle->initialized == 0 ) return 0;
5857 SharedPathsOp::PathList forw, back;
5858 try {
5859 SharedPathsOp::sharedPathsOp(*g1, *g2, forw, back);
5861 catch (const std::exception &e)
5863 SharedPathsOp::clearEdges(forw);
5864 SharedPathsOp::clearEdges(back);
5865 handle->ERROR_MESSAGE("%s", e.what());
5866 return 0;
5868 catch (...)
5870 SharedPathsOp::clearEdges(forw);
5871 SharedPathsOp::clearEdges(back);
5872 handle->ERROR_MESSAGE("Unknown exception thrown");
5873 return 0;
5876 // Now forw and back have the geoms we want to use to construct
5877 // our output GeometryCollections...
5879 const GeometryFactory* factory = g1->getFactory();
5880 size_t count;
5882 std::auto_ptr< std::vector<Geometry*> > out1(
5883 new std::vector<Geometry*>()
5885 count = forw.size();
5886 out1->reserve(count);
5887 for (size_t i=0; i<count; ++i) {
5888 out1->push_back(forw[i]);
5890 std::auto_ptr<Geometry> out1g (
5891 factory->createMultiLineString(out1.release())
5894 std::auto_ptr< std::vector<Geometry*> > out2(
5895 new std::vector<Geometry*>()
5897 count = back.size();
5898 out2->reserve(count);
5899 for (size_t i=0; i<count; ++i) {
5900 out2->push_back(back[i]);
5902 std::auto_ptr<Geometry> out2g (
5903 factory->createMultiLineString(out2.release())
5906 std::auto_ptr< std::vector<Geometry*> > out(
5907 new std::vector<Geometry*>()
5909 out->reserve(2);
5910 out->push_back(out1g.release());
5911 out->push_back(out2g.release());
5913 std::auto_ptr<Geometry> outg (
5914 factory->createGeometryCollection(out.release())
5917 return outg.release();
5921 GEOSGeometry *
5922 GEOSSnap_r(GEOSContextHandle_t extHandle, const GEOSGeometry* g1,
5923 const GEOSGeometry* g2, double tolerance)
5925 using namespace geos::operation::overlay::snap;
5927 if ( 0 == extHandle ) return 0;
5928 GEOSContextHandleInternal_t *handle =
5929 reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
5930 if ( handle->initialized == 0 ) return 0;
5932 try{
5933 GeometrySnapper snapper( *g1 );
5934 std::auto_ptr<Geometry> ret = snapper.snapTo(*g2, tolerance);
5935 return ret.release();
5937 catch (const std::exception &e)
5939 handle->ERROR_MESSAGE("%s", e.what());
5940 return 0;
5942 catch (...)
5944 handle->ERROR_MESSAGE("Unknown exception thrown");
5945 return 0;
5949 BufferParameters *
5950 GEOSBufferParams_create_r(GEOSContextHandle_t extHandle)
5952 if ( 0 == extHandle ) return NULL;
5954 GEOSContextHandleInternal_t *handle = 0;
5955 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
5956 if ( 0 == handle->initialized ) return NULL;
5960 BufferParameters *p = new BufferParameters();
5961 return p;
5963 catch (const std::exception &e)
5965 handle->ERROR_MESSAGE("%s", e.what());
5967 catch (...)
5969 handle->ERROR_MESSAGE("Unknown exception thrown");
5972 return 0;
5975 void
5976 GEOSBufferParams_destroy_r(GEOSContextHandle_t extHandle, BufferParameters* p)
5978 (void)extHandle;
5979 delete p;
5983 GEOSBufferParams_setEndCapStyle_r(GEOSContextHandle_t extHandle,
5984 GEOSBufferParams* p, int style)
5986 if ( 0 == extHandle ) return 0;
5988 GEOSContextHandleInternal_t *handle = 0;
5989 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
5990 if ( 0 == handle->initialized ) return 0;
5994 if ( style > BufferParameters::CAP_SQUARE )
5996 throw IllegalArgumentException("Invalid buffer endCap style");
5998 p->setEndCapStyle(static_cast<BufferParameters::EndCapStyle>(style));
5999 return 1;
6001 catch (const std::exception &e)
6003 handle->ERROR_MESSAGE("%s", e.what());
6005 catch (...)
6007 handle->ERROR_MESSAGE("Unknown exception thrown");
6010 return 0;
6014 GEOSBufferParams_setJoinStyle_r(GEOSContextHandle_t extHandle,
6015 GEOSBufferParams* p, int style)
6017 if ( 0 == extHandle ) return 0;
6019 GEOSContextHandleInternal_t *handle = 0;
6020 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
6021 if ( 0 == handle->initialized ) return 0;
6025 if ( style > BufferParameters::JOIN_BEVEL ) {
6026 throw IllegalArgumentException("Invalid buffer join style");
6028 p->setJoinStyle(static_cast<BufferParameters::JoinStyle>(style));
6029 return 1;
6031 catch (const std::exception &e)
6033 handle->ERROR_MESSAGE("%s", e.what());
6035 catch (...)
6037 handle->ERROR_MESSAGE("Unknown exception thrown");
6040 return 0;
6044 GEOSBufferParams_setMitreLimit_r(GEOSContextHandle_t extHandle,
6045 GEOSBufferParams* p, double limit)
6047 if ( 0 == extHandle ) return 0;
6049 GEOSContextHandleInternal_t *handle = 0;
6050 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
6051 if ( 0 == handle->initialized ) return 0;
6055 p->setMitreLimit(limit);
6056 return 1;
6058 catch (const std::exception &e)
6060 handle->ERROR_MESSAGE("%s", e.what());
6062 catch (...)
6064 handle->ERROR_MESSAGE("Unknown exception thrown");
6067 return 0;
6071 GEOSBufferParams_setQuadrantSegments_r(GEOSContextHandle_t extHandle,
6072 GEOSBufferParams* p, int segs)
6074 if ( 0 == extHandle ) return 0;
6076 GEOSContextHandleInternal_t *handle = 0;
6077 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
6078 if ( 0 == handle->initialized ) return 0;
6082 p->setQuadrantSegments(segs);
6083 return 1;
6085 catch (const std::exception &e)
6087 handle->ERROR_MESSAGE("%s", e.what());
6089 catch (...)
6091 handle->ERROR_MESSAGE("Unknown exception thrown");
6094 return 0;
6098 GEOSBufferParams_setSingleSided_r(GEOSContextHandle_t extHandle,
6099 GEOSBufferParams* p, int ss)
6101 if ( 0 == extHandle ) return 0;
6103 GEOSContextHandleInternal_t *handle = 0;
6104 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
6105 if ( 0 == handle->initialized ) return 0;
6109 p->setSingleSided( (ss != 0) );
6110 return 1;
6112 catch (const std::exception &e)
6114 handle->ERROR_MESSAGE("%s", e.what());
6116 catch (...)
6118 handle->ERROR_MESSAGE("Unknown exception thrown");
6121 return 0;
6124 Geometry *
6125 GEOSBufferWithParams_r(GEOSContextHandle_t extHandle, const Geometry *g1, const BufferParameters* bp, double width)
6127 using geos::operation::buffer::BufferOp;
6129 if ( 0 == extHandle ) return NULL;
6131 GEOSContextHandleInternal_t *handle = 0;
6132 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
6133 if ( 0 == handle->initialized ) return NULL;
6137 BufferOp op(g1, *bp);
6138 Geometry *g3 = op.getResultGeometry(width);
6139 return g3;
6141 catch (const std::exception &e)
6143 handle->ERROR_MESSAGE("%s", e.what());
6145 catch (...)
6147 handle->ERROR_MESSAGE("Unknown exception thrown");
6150 return NULL;
6153 Geometry *
6154 GEOSDelaunayTriangulation_r(GEOSContextHandle_t extHandle, const Geometry *g1, double tolerance, int onlyEdges)
6156 if ( 0 == extHandle ) return NULL;
6158 GEOSContextHandleInternal_t *handle = 0;
6159 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
6160 if ( 0 == handle->initialized ) return NULL;
6162 using geos::triangulate::DelaunayTriangulationBuilder;
6166 DelaunayTriangulationBuilder builder;
6167 builder.setTolerance(tolerance);
6168 builder.setSites(*g1);
6170 if ( onlyEdges ) return builder.getEdges( *g1->getFactory() ).release();
6171 else return builder.getTriangles( *g1->getFactory() ).release();
6174 catch (const std::exception &e)
6176 handle->ERROR_MESSAGE("%s", e.what());
6178 catch (...)
6180 handle->ERROR_MESSAGE("Unknown exception thrown");
6183 return NULL;
6186 } /* extern "C" */