Regnerate geos wrapper with correct version for this branch
[geos.git] / capi / geos_ts_c.cpp
blobcbfe21f392685699be8b671c9b6c845b4fe20348
1 /************************************************************************
4 * C-Wrapper for GEOS library
6 * Copyright (C) 2010 2011 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/union/CascadedPolygonUnion.h>
54 #include <geos/operation/buffer/BufferOp.h>
55 #include <geos/operation/buffer/BufferParameters.h>
56 #include <geos/operation/buffer/BufferBuilder.h>
57 #include <geos/operation/relate/RelateOp.h>
58 #include <geos/operation/sharedpaths/SharedPathsOp.h>
59 #include <geos/linearref/LengthIndexedLine.h>
60 #include <geos/geom/BinaryOp.h>
61 #include <geos/util/IllegalArgumentException.h>
62 #include <geos/util/UniqueCoordinateArrayFilter.h>
63 #include <geos/util/Machine.h>
64 #include <geos/version.h>
66 // This should go away
67 #include <cmath> // finite
68 #include <cstddef>
69 #include <cstdio>
70 #include <cstdlib>
71 #include <cstring>
72 #include <fstream>
73 #include <iostream>
74 #include <sstream>
75 #include <string>
76 #include <memory>
78 #ifdef _MSC_VER
79 #pragma warning(disable : 4099)
80 #endif
82 // Some extra magic to make type declarations in geos_c.h work -
83 // for cross-checking of types in header.
84 #define GEOSGeometry geos::geom::Geometry
85 #define GEOSPreparedGeometry geos::geom::prep::PreparedGeometry
86 #define GEOSCoordSequence geos::geom::CoordinateSequence
87 #define GEOSBufferParams geos::operation::buffer::BufferParameters
88 #define GEOSSTRtree geos::index::strtree::STRtree
89 #define GEOSWKTReader_t geos::io::WKTReader
90 #define GEOSWKTWriter_t geos::io::WKTWriter
91 #define GEOSWKBReader_t geos::io::WKBReader
92 #define GEOSWKBWriter_t geos::io::WKBWriter
94 #include "geos_c.h"
96 // Intentional, to allow non-standard C elements like C99 functions to be
97 // imported through C++ headers of C library, like <cmath>.
98 using namespace std;
100 /// Define this if you want operations triggering Exceptions to
101 /// be printed.
102 /// (will use the NOTIFY channel - only implemented for GEOSUnion so far)
104 #undef VERBOSE_EXCEPTIONS
106 #include <geos/export.h>
109 // import the most frequently used definitions globally
110 using geos::geom::Geometry;
111 using geos::geom::LineString;
112 using geos::geom::Polygon;
113 using geos::geom::CoordinateSequence;
114 using geos::geom::GeometryFactory;
116 using geos::io::WKTReader;
117 using geos::io::WKTWriter;
118 using geos::io::WKBReader;
119 using geos::io::WKBWriter;
121 using geos::operation::overlay::OverlayOp;
122 using geos::operation::overlay::overlayOp;
123 using geos::operation::geounion::CascadedPolygonUnion;
124 using geos::operation::buffer::BufferParameters;
125 using geos::operation::buffer::BufferBuilder;
126 using geos::util::IllegalArgumentException;
127 using geos::algorithm::distance::DiscreteHausdorffDistance;
129 typedef std::auto_ptr<Geometry> GeomAutoPtr;
131 typedef struct GEOSContextHandleInternal
133 const GeometryFactory *geomFactory;
134 GEOSMessageHandler NOTICE_MESSAGE;
135 GEOSMessageHandler ERROR_MESSAGE;
136 int WKBOutputDims;
137 int WKBByteOrder;
138 int initialized;
139 } GEOSContextHandleInternal_t;
141 // CAPI_ItemVisitor is used internally by the CAPI STRtree
142 // wrappers. It's defined here just to keep it out of the
143 // extern "C" block.
144 class CAPI_ItemVisitor : public geos::index::ItemVisitor {
145 GEOSQueryCallback callback;
146 void *userdata;
147 public:
148 CAPI_ItemVisitor (GEOSQueryCallback cb, void *ud)
149 : ItemVisitor(), callback(cb), userdata(ud) {}
150 void visitItem (void *item) { callback(item, userdata); }
154 //## PROTOTYPES #############################################
156 extern "C" const char GEOS_DLL *GEOSjtsport();
157 extern "C" char GEOS_DLL *GEOSasText(Geometry *g1);
159 namespace { // anonymous
161 char* gstrdup_s(const char* str, const std::size_t size)
163 char* out = static_cast<char*>(std::malloc(size + 1));
164 if (0 != out)
166 // as no strlen call necessary, memcpy may be faster than strcpy
167 std::memcpy(out, str, size + 1);
170 assert(0 != out);
172 // we haven't been checking allocation before ticket #371
173 if (0 == out)
175 throw(std::runtime_error("Failed to allocate memory for duplicate string"));
178 return out;
181 char* gstrdup(std::string const& str)
183 return gstrdup_s(str.c_str(), str.size());
186 } // namespace anonymous
188 extern "C" {
190 GEOSContextHandle_t
191 initGEOS_r(GEOSMessageHandler nf, GEOSMessageHandler ef)
193 GEOSContextHandleInternal_t *handle = 0;
194 void *extHandle = 0;
196 extHandle = std::malloc(sizeof(GEOSContextHandleInternal_t));
197 if (0 != extHandle)
199 handle = static_cast<GEOSContextHandleInternal_t*>(extHandle);
200 handle->NOTICE_MESSAGE = nf;
201 handle->ERROR_MESSAGE = ef;
202 handle->geomFactory = GeometryFactory::getDefaultInstance();
203 handle->WKBOutputDims = 2;
204 handle->WKBByteOrder = getMachineByteOrder();
205 handle->initialized = 1;
208 return static_cast<GEOSContextHandle_t>(extHandle);
211 GEOSMessageHandler
212 GEOSContext_setNoticeHandler_r(GEOSContextHandle_t extHandle, GEOSMessageHandler nf)
214 GEOSMessageHandler f;
215 GEOSContextHandleInternal_t *handle = 0;
216 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
217 if ( 0 == handle->initialized )
219 return NULL;
222 f = handle->NOTICE_MESSAGE;
223 handle->NOTICE_MESSAGE = nf;
225 return f;
228 GEOSMessageHandler
229 GEOSContext_setErrorHandler_r(GEOSContextHandle_t extHandle, GEOSMessageHandler nf)
231 GEOSMessageHandler f;
232 GEOSContextHandleInternal_t *handle = 0;
233 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
234 if ( 0 == handle->initialized )
236 return NULL;
239 f = handle->ERROR_MESSAGE;
240 handle->ERROR_MESSAGE = nf;
242 return f;
245 void
246 finishGEOS_r(GEOSContextHandle_t extHandle)
248 // Fix up freeing handle w.r.t. malloc above
249 std::free(extHandle);
250 extHandle = NULL;
253 void
254 GEOSFree_r (GEOSContextHandle_t extHandle, void* buffer)
256 assert(0 != extHandle);
258 std::free(buffer);
261 //-----------------------------------------------------------
262 // relate()-related functions
263 // return 0 = false, 1 = true, 2 = error occured
264 //-----------------------------------------------------------
266 char
267 GEOSDisjoint_r(GEOSContextHandle_t extHandle, const Geometry *g1, const Geometry *g2)
269 if ( 0 == extHandle )
271 return 2;
274 GEOSContextHandleInternal_t *handle = 0;
275 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
276 if ( handle->initialized == 0 )
278 return 2;
283 bool result = g1->disjoint(g2);
284 return result;
287 // TODO: mloskot is going to replace these double-catch block
288 // with a macro to remove redundant code in this and
289 // following functions.
290 catch (const std::exception &e)
292 handle->ERROR_MESSAGE("%s", e.what());
294 catch (...)
296 handle->ERROR_MESSAGE("Unknown exception thrown");
299 return 2;
302 char
303 GEOSTouches_r(GEOSContextHandle_t extHandle, const Geometry *g1, const Geometry *g2)
305 if ( 0 == extHandle )
307 return 2;
310 GEOSContextHandleInternal_t *handle = 0;
311 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
312 if ( 0 == handle->initialized )
314 return 2;
319 bool result = g1->touches(g2);
320 return result;
322 catch (const std::exception &e)
324 handle->ERROR_MESSAGE("%s", e.what());
326 catch (...)
328 handle->ERROR_MESSAGE("Unknown exception thrown");
331 return 2;
334 char
335 GEOSIntersects_r(GEOSContextHandle_t extHandle, const Geometry *g1, const Geometry *g2)
337 if ( 0 == extHandle )
339 return 2;
342 GEOSContextHandleInternal_t *handle = 0;
343 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
344 if ( 0 == handle->initialized )
346 return 2;
351 bool result = g1->intersects(g2);
352 return result;
354 catch (const std::exception &e)
356 handle->ERROR_MESSAGE("%s", e.what());
358 catch (...)
360 handle->ERROR_MESSAGE("Unknown exception thrown");
363 return 2;
366 char
367 GEOSCrosses_r(GEOSContextHandle_t extHandle, const Geometry *g1, const Geometry *g2)
369 if ( 0 == extHandle )
371 return 2;
374 GEOSContextHandleInternal_t *handle = 0;
375 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
376 if ( 0 == handle->initialized )
378 return 2;
383 bool result = g1->crosses(g2);
384 return result;
386 catch (const std::exception &e)
388 handle->ERROR_MESSAGE("%s", e.what());
390 catch (...)
392 handle->ERROR_MESSAGE("Unknown exception thrown");
395 return 2;
398 char
399 GEOSWithin_r(GEOSContextHandle_t extHandle, const Geometry *g1, const Geometry *g2)
401 if ( 0 == extHandle )
403 return 2;
406 GEOSContextHandleInternal_t *handle = 0;
407 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
408 if ( 0 == handle->initialized )
410 return 2;
415 bool result = g1->within(g2);
416 return result;
418 catch (const std::exception &e)
420 handle->ERROR_MESSAGE("%s", e.what());
422 catch (...)
424 handle->ERROR_MESSAGE("Unknown exception thrown");
427 return 2;
430 // call g1->contains(g2)
431 // returns 0 = false
432 // 1 = true
433 // 2 = error was trapped
434 char
435 GEOSContains_r(GEOSContextHandle_t extHandle, const Geometry *g1, const Geometry *g2)
437 if ( 0 == extHandle )
439 return 2;
442 GEOSContextHandleInternal_t *handle = 0;
443 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
444 if ( 0 == handle->initialized )
446 return 2;
451 bool result = g1->contains(g2);
452 return result;
454 catch (const std::exception &e)
456 handle->ERROR_MESSAGE("%s", e.what());
458 catch (...)
460 handle->ERROR_MESSAGE("Unknown exception thrown");
463 return 2;
466 char
467 GEOSOverlaps_r(GEOSContextHandle_t extHandle, const Geometry *g1, const Geometry *g2)
469 if ( 0 == extHandle )
471 return 2;
474 GEOSContextHandleInternal_t *handle = 0;
475 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
476 if ( 0 == handle->initialized )
478 return 2;
483 bool result = g1->overlaps(g2);
484 return result;
486 catch (const std::exception &e)
488 handle->ERROR_MESSAGE("%s", e.what());
490 catch (...)
492 handle->ERROR_MESSAGE("Unknown exception thrown");
495 return 2;
498 char
499 GEOSCovers_r(GEOSContextHandle_t extHandle, const Geometry *g1, const Geometry *g2)
501 if ( 0 == extHandle )
503 return 2;
506 GEOSContextHandleInternal_t *handle = 0;
507 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
508 if ( 0 == handle->initialized )
510 return 2;
515 bool result = g1->covers(g2);
516 return result;
518 catch (const std::exception &e)
520 handle->ERROR_MESSAGE("%s", e.what());
522 catch (...)
524 handle->ERROR_MESSAGE("Unknown exception thrown");
527 return 2;
530 char
531 GEOSCoveredBy_r(GEOSContextHandle_t extHandle, const Geometry *g1, const Geometry *g2)
533 if ( 0 == extHandle )
535 return 2;
538 GEOSContextHandleInternal_t *handle = 0;
539 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
540 if ( 0 == handle->initialized )
542 return 2;
547 bool result = g1->coveredBy(g2);
548 return result;
550 catch (const std::exception &e)
552 handle->ERROR_MESSAGE("%s", e.what());
554 catch (...)
556 handle->ERROR_MESSAGE("Unknown exception thrown");
559 return 2;
563 //-------------------------------------------------------------------
564 // low-level relate functions
565 //------------------------------------------------------------------
567 char
568 GEOSRelatePattern_r(GEOSContextHandle_t extHandle, const Geometry *g1, const Geometry *g2, const char *pat)
570 if ( 0 == extHandle )
572 return 2;
575 GEOSContextHandleInternal_t *handle = 0;
576 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
577 if ( 0 == handle->initialized )
579 return 2;
584 std::string s(pat);
585 bool result = g1->relate(g2, s);
586 return result;
588 catch (const std::exception &e)
590 handle->ERROR_MESSAGE("%s", e.what());
592 catch (...)
594 handle->ERROR_MESSAGE("Unknown exception thrown");
597 return 2;
600 char
601 GEOSRelatePatternMatch_r(GEOSContextHandle_t extHandle, const char *mat,
602 const char *pat)
604 if ( 0 == extHandle )
606 return 2;
609 GEOSContextHandleInternal_t *handle = 0;
610 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
611 if ( 0 == handle->initialized )
613 return 2;
618 using geos::geom::IntersectionMatrix;
620 std::string m(mat);
621 std::string p(pat);
622 IntersectionMatrix im(m);
624 bool result = im.matches(p);
625 return result;
627 catch (const std::exception &e)
629 handle->ERROR_MESSAGE("%s", e.what());
631 catch (...)
633 handle->ERROR_MESSAGE("Unknown exception thrown");
636 return 2;
639 char *
640 GEOSRelate_r(GEOSContextHandle_t extHandle, const Geometry *g1, const Geometry *g2)
642 if ( 0 == extHandle )
644 return NULL;
647 GEOSContextHandleInternal_t *handle = 0;
648 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
649 if ( 0 == handle->initialized )
651 return NULL;
656 using geos::geom::IntersectionMatrix;
658 IntersectionMatrix* im = g1->relate(g2);
659 if (0 == im)
661 return 0;
664 char *result = gstrdup(im->toString());
666 delete im;
667 im = 0;
669 return result;
671 catch (const std::exception &e)
673 handle->ERROR_MESSAGE("%s", e.what());
675 catch (...)
677 handle->ERROR_MESSAGE("Unknown exception thrown");
680 return NULL;
683 char *
684 GEOSRelateBoundaryNodeRule_r(GEOSContextHandle_t extHandle, const Geometry *g1, const Geometry *g2, int bnr)
686 if ( 0 == extHandle )
688 return NULL;
691 GEOSContextHandleInternal_t *handle = 0;
692 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
693 if ( 0 == handle->initialized )
695 return NULL;
700 using geos::operation::relate::RelateOp;
701 using geos::geom::IntersectionMatrix;
702 using geos::algorithm::BoundaryNodeRule;
704 IntersectionMatrix* im;
705 switch (bnr) {
706 case GEOSRELATE_BNR_MOD2: /* same as OGC */
707 im = RelateOp::relate(g1, g2,
708 BoundaryNodeRule::MOD2_BOUNDARY_RULE);
709 break;
710 case GEOSRELATE_BNR_ENDPOINT:
711 im = RelateOp::relate(g1, g2,
712 BoundaryNodeRule::ENDPOINT_BOUNDARY_RULE);
713 break;
714 case GEOSRELATE_BNR_MULTIVALENT_ENDPOINT:
715 im = RelateOp::relate(g1, g2,
716 BoundaryNodeRule::MULTIVALENT_ENDPOINT_BOUNDARY_RULE);
717 break;
718 case GEOSRELATE_BNR_MONOVALENT_ENDPOINT:
719 im = RelateOp::relate(g1, g2,
720 BoundaryNodeRule::MONOVALENT_ENDPOINT_BOUNDARY_RULE);
721 break;
722 default:
723 handle->ERROR_MESSAGE("Invalid boundary node rule %d", bnr);
724 return 0;
725 break;
728 if (0 == im) return 0;
730 char *result = gstrdup(im->toString());
732 delete im;
733 im = 0;
735 return result;
737 catch (const std::exception &e)
739 handle->ERROR_MESSAGE("%s", e.what());
741 catch (...)
743 handle->ERROR_MESSAGE("Unknown exception thrown");
746 return NULL;
751 //-----------------------------------------------------------------
752 // isValid
753 //-----------------------------------------------------------------
756 char
757 GEOSisValid_r(GEOSContextHandle_t extHandle, const Geometry *g1)
759 if ( 0 == extHandle )
761 return 2;
764 GEOSContextHandleInternal_t *handle = 0;
765 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
766 if ( 0 == handle->initialized )
768 return 2;
773 using geos::operation::valid::IsValidOp;
774 using geos::operation::valid::TopologyValidationError;
776 IsValidOp ivo(g1);
777 TopologyValidationError *err = ivo.getValidationError();
778 if ( err )
780 handle->NOTICE_MESSAGE("%s", err->toString().c_str());
781 return 0;
783 else
785 return 1;
788 catch (const std::exception &e)
790 handle->ERROR_MESSAGE("%s", e.what());
792 catch (...)
794 handle->ERROR_MESSAGE("Unknown exception thrown");
797 return 2;
800 char *
801 GEOSisValidReason_r(GEOSContextHandle_t extHandle, const Geometry *g1)
803 if ( 0 == extHandle )
805 return NULL;
808 GEOSContextHandleInternal_t *handle = 0;
809 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
810 if ( 0 == handle->initialized )
812 return NULL;
817 using geos::operation::valid::IsValidOp;
818 using geos::operation::valid::TopologyValidationError;
820 char* result = 0;
821 char const* const validstr = "Valid Geometry";
823 IsValidOp ivo(g1);
824 TopologyValidationError *err = ivo.getValidationError();
825 if (0 != err)
827 std::ostringstream ss;
828 ss.precision(15);
829 ss << err->getCoordinate();
830 const std::string errloc = ss.str();
831 std::string errmsg(err->getMessage());
832 errmsg += "[" + errloc + "]";
833 result = gstrdup(errmsg);
835 else
837 result = gstrdup(std::string(validstr));
840 return result;
842 catch (const std::exception &e)
844 handle->ERROR_MESSAGE("%s", e.what());
846 catch (...)
848 handle->ERROR_MESSAGE("Unknown exception thrown");
851 return 0;
854 char
855 GEOSisValidDetail_r(GEOSContextHandle_t extHandle, const Geometry *g,
856 int flags, char** reason, Geometry ** location)
858 if ( 0 == extHandle )
860 return 0;
863 GEOSContextHandleInternal_t *handle = 0;
864 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
865 if ( 0 == handle->initialized )
867 return 0;
872 using geos::operation::valid::IsValidOp;
873 using geos::operation::valid::TopologyValidationError;
875 IsValidOp ivo(g);
876 if ( flags & GEOSVALID_ALLOW_SELFTOUCHING_RING_FORMING_HOLE ) {
877 ivo.setSelfTouchingRingFormingHoleValid(true);
879 TopologyValidationError *err = ivo.getValidationError();
880 if (0 != err)
882 if ( location ) {
883 *location = handle->geomFactory->createPoint(err->getCoordinate());
885 if ( reason ) {
886 std::string errmsg(err->getMessage());
887 *reason = gstrdup(errmsg);
889 return 0;
892 if ( location ) *location = 0;
893 if ( reason ) *reason = 0;
894 return 1; /* valid */
897 catch (const std::exception &e)
899 handle->ERROR_MESSAGE("%s", e.what());
901 catch (...)
903 handle->ERROR_MESSAGE("Unknown exception thrown");
906 return 2; /* exception */
909 //-----------------------------------------------------------------
910 // general purpose
911 //-----------------------------------------------------------------
913 char
914 GEOSEquals_r(GEOSContextHandle_t extHandle, const Geometry *g1, const Geometry *g2)
916 if ( 0 == extHandle )
918 return 2;
921 GEOSContextHandleInternal_t *handle = 0;
922 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
923 if ( 0 == handle->initialized )
925 return 2;
930 bool result = g1->equals(g2);
931 return result;
933 catch (const std::exception &e)
935 handle->ERROR_MESSAGE("%s", e.what());
937 catch (...)
939 handle->ERROR_MESSAGE("Unknown exception thrown");
942 return 2;
945 char
946 GEOSEqualsExact_r(GEOSContextHandle_t extHandle, const Geometry *g1, const Geometry *g2, double tolerance)
948 if ( 0 == extHandle )
950 return 2;
953 GEOSContextHandleInternal_t *handle = 0;
954 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
955 if ( 0 == handle->initialized )
957 return 2;
962 bool result = g1->equalsExact(g2, tolerance);
963 return result;
965 catch (const std::exception &e)
967 handle->ERROR_MESSAGE("%s", e.what());
969 catch (...)
971 handle->ERROR_MESSAGE("Unknown exception thrown");
974 return 2;
978 GEOSDistance_r(GEOSContextHandle_t extHandle, const Geometry *g1, const Geometry *g2, double *dist)
980 assert(0 != dist);
982 if ( 0 == extHandle )
984 return 0;
987 GEOSContextHandleInternal_t *handle = 0;
988 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
989 if ( 0 == handle->initialized )
991 return 0;
996 *dist = g1->distance(g2);
997 return 1;
999 catch (const std::exception &e)
1001 handle->ERROR_MESSAGE("%s", e.what());
1003 catch (...)
1005 handle->ERROR_MESSAGE("Unknown exception thrown");
1008 return 0;
1012 GEOSHausdorffDistance_r(GEOSContextHandle_t extHandle, const Geometry *g1, const Geometry *g2, double *dist)
1014 assert(0 != dist);
1016 if ( 0 == extHandle )
1018 return 0;
1021 GEOSContextHandleInternal_t *handle = 0;
1022 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
1023 if ( 0 == handle->initialized )
1025 return 0;
1030 *dist = DiscreteHausdorffDistance::distance(*g1, *g2);
1031 return 1;
1033 catch (const std::exception &e)
1035 handle->ERROR_MESSAGE("%s", e.what());
1037 catch (...)
1039 handle->ERROR_MESSAGE("Unknown exception thrown");
1042 return 0;
1046 GEOSHausdorffDistanceDensify_r(GEOSContextHandle_t extHandle, const Geometry *g1, const Geometry *g2, double densifyFrac, double *dist)
1048 assert(0 != dist);
1050 if ( 0 == extHandle )
1052 return 0;
1055 GEOSContextHandleInternal_t *handle = 0;
1056 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
1057 if ( 0 == handle->initialized )
1059 return 0;
1064 *dist = DiscreteHausdorffDistance::distance(*g1, *g2, densifyFrac);
1065 return 1;
1067 catch (const std::exception &e)
1069 handle->ERROR_MESSAGE("%s", e.what());
1071 catch (...)
1073 handle->ERROR_MESSAGE("Unknown exception thrown");
1076 return 0;
1080 GEOSArea_r(GEOSContextHandle_t extHandle, const Geometry *g, double *area)
1082 assert(0 != area);
1084 if ( 0 == extHandle )
1086 return 0;
1089 GEOSContextHandleInternal_t *handle = 0;
1090 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
1091 if ( 0 == handle->initialized )
1093 return 0;
1098 *area = g->getArea();
1099 return 1;
1101 catch (const std::exception &e)
1103 handle->ERROR_MESSAGE("%s", e.what());
1105 catch (...)
1107 handle->ERROR_MESSAGE("Unknown exception thrown");
1110 return 0;
1114 GEOSLength_r(GEOSContextHandle_t extHandle, const Geometry *g, double *length)
1116 assert(0 != length);
1118 if ( 0 == extHandle )
1120 return 2;
1123 GEOSContextHandleInternal_t *handle = 0;
1124 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
1125 if ( 0 == handle->initialized )
1127 return 0;
1132 *length = g->getLength();
1133 return 1;
1135 catch (const std::exception &e)
1137 handle->ERROR_MESSAGE("%s", e.what());
1139 catch (...)
1141 handle->ERROR_MESSAGE("Unknown exception thrown");
1144 return 0;
1147 Geometry *
1148 GEOSGeomFromWKT_r(GEOSContextHandle_t extHandle, const char *wkt)
1150 if ( 0 == extHandle )
1152 return NULL;
1155 GEOSContextHandleInternal_t *handle = 0;
1156 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
1157 if ( 0 == handle->initialized )
1159 return NULL;
1164 const std::string wktstring(wkt);
1165 WKTReader r(static_cast<GeometryFactory const*>(handle->geomFactory));
1167 Geometry *g = r.read(wktstring);
1168 return g;
1170 catch (const std::exception &e)
1172 handle->ERROR_MESSAGE("%s", e.what());
1174 catch (...)
1176 handle->ERROR_MESSAGE("Unknown exception thrown");
1179 return NULL;
1182 char *
1183 GEOSGeomToWKT_r(GEOSContextHandle_t extHandle, const Geometry *g1)
1185 if ( 0 == extHandle )
1187 return NULL;
1190 GEOSContextHandleInternal_t *handle = 0;
1191 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
1192 if ( 0 == handle->initialized )
1194 return NULL;
1200 char *result = gstrdup(g1->toString());
1201 return result;
1203 catch (const std::exception &e)
1205 handle->ERROR_MESSAGE("%s", e.what());
1207 catch (...)
1209 handle->ERROR_MESSAGE("Unknown exception thrown");
1211 return NULL;
1214 // Remember to free the result!
1215 unsigned char *
1216 GEOSGeomToWKB_buf_r(GEOSContextHandle_t extHandle, const Geometry *g, size_t *size)
1218 assert(0 != size);
1220 if ( 0 == extHandle )
1222 return NULL;
1225 GEOSContextHandleInternal_t *handle = 0;
1226 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
1227 if ( 0 == handle->initialized )
1229 return NULL;
1232 using geos::io::WKBWriter;
1235 int byteOrder = handle->WKBByteOrder;
1236 WKBWriter w(handle->WKBOutputDims, byteOrder);
1237 std::ostringstream os(std::ios_base::binary);
1238 w.write(*g, os);
1239 std::string wkbstring(os.str());
1240 const std::size_t len = wkbstring.length();
1242 unsigned char* result = 0;
1243 result = static_cast<unsigned char*>(std::malloc(len));
1244 if (0 != result)
1246 std::memcpy(result, wkbstring.c_str(), len);
1247 *size = len;
1249 return result;
1251 catch (const std::exception &e)
1253 handle->ERROR_MESSAGE("%s", e.what());
1255 catch (...)
1257 handle->ERROR_MESSAGE("Unknown exception thrown");
1260 return NULL;
1263 Geometry *
1264 GEOSGeomFromWKB_buf_r(GEOSContextHandle_t extHandle, const unsigned char *wkb, size_t size)
1266 if ( 0 == extHandle )
1268 return NULL;
1271 GEOSContextHandleInternal_t *handle = 0;
1272 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
1273 if ( 0 == handle->initialized )
1275 return NULL;
1278 using geos::io::WKBReader;
1281 std::string wkbstring(reinterpret_cast<const char*>(wkb), size); // make it binary !
1282 WKBReader r(*(static_cast<GeometryFactory const*>(handle->geomFactory)));
1283 std::istringstream is(std::ios_base::binary);
1284 is.str(wkbstring);
1285 is.seekg(0, std::ios::beg); // rewind reader pointer
1286 Geometry *g = r.read(is);
1287 return g;
1289 catch (const std::exception &e)
1291 handle->ERROR_MESSAGE("%s", e.what());
1293 catch (...)
1295 handle->ERROR_MESSAGE("Unknown exception thrown");
1298 return NULL;
1301 /* Read/write wkb hex values. Returned geometries are
1302 owned by the caller.*/
1303 unsigned char *
1304 GEOSGeomToHEX_buf_r(GEOSContextHandle_t extHandle, const Geometry *g, size_t *size)
1306 if ( 0 == extHandle )
1308 return NULL;
1311 GEOSContextHandleInternal_t *handle = 0;
1312 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
1313 if ( 0 == handle->initialized )
1315 return NULL;
1318 using geos::io::WKBWriter;
1321 int byteOrder = handle->WKBByteOrder;
1322 WKBWriter w(handle->WKBOutputDims, byteOrder);
1323 std::ostringstream os(std::ios_base::binary);
1324 w.writeHEX(*g, os);
1325 std::string hexstring(os.str());
1327 char *result = gstrdup(hexstring);
1328 if (0 != result)
1330 *size = hexstring.length();
1333 return reinterpret_cast<unsigned char*>(result);
1335 catch (const std::exception &e)
1337 handle->ERROR_MESSAGE("%s", e.what());
1339 catch (...)
1341 handle->ERROR_MESSAGE("Unknown exception thrown");
1344 return NULL;
1347 Geometry *
1348 GEOSGeomFromHEX_buf_r(GEOSContextHandle_t extHandle, const unsigned char *hex, size_t size)
1350 if ( 0 == extHandle )
1352 return NULL;
1355 GEOSContextHandleInternal_t *handle = 0;
1356 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
1357 if ( 0 == handle->initialized )
1359 return NULL;
1362 using geos::io::WKBReader;
1365 std::string hexstring(reinterpret_cast<const char*>(hex), size);
1366 WKBReader r(*(static_cast<GeometryFactory const*>(handle->geomFactory)));
1367 std::istringstream is(std::ios_base::binary);
1368 is.str(hexstring);
1369 is.seekg(0, std::ios::beg); // rewind reader pointer
1371 Geometry *g = r.readHEX(is);
1372 return g;
1374 catch (const std::exception &e)
1376 handle->ERROR_MESSAGE("%s", e.what());
1378 catch (...)
1380 handle->ERROR_MESSAGE("Unknown exception thrown");
1383 return NULL;
1386 char
1387 GEOSisEmpty_r(GEOSContextHandle_t extHandle, const Geometry *g1)
1389 if ( 0 == extHandle )
1391 return 2;
1394 GEOSContextHandleInternal_t *handle = 0;
1395 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
1396 if ( 0 == handle->initialized )
1398 return 2;
1403 return g1->isEmpty();
1405 catch (const std::exception &e)
1407 handle->ERROR_MESSAGE("%s", e.what());
1409 catch (...)
1411 handle->ERROR_MESSAGE("Unknown exception thrown");
1414 return 2;
1417 char
1418 GEOSisSimple_r(GEOSContextHandle_t extHandle, const Geometry *g1)
1420 if ( 0 == extHandle )
1422 return 2;
1425 GEOSContextHandleInternal_t *handle = 0;
1426 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
1427 if ( 0 == handle->initialized )
1429 return 2;
1434 return g1->isSimple();
1436 catch (const std::exception &e)
1438 handle->ERROR_MESSAGE("%s", e.what());
1439 return 2;
1442 catch (...)
1444 handle->ERROR_MESSAGE("Unknown exception thrown");
1445 return 2;
1449 char
1450 GEOSisRing_r(GEOSContextHandle_t extHandle, const Geometry *g)
1452 if ( 0 == extHandle )
1454 return 2;
1457 GEOSContextHandleInternal_t *handle = 0;
1458 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
1459 if ( 0 == handle->initialized )
1461 return 2;
1466 const LineString *ls = dynamic_cast<const LineString *>(g);
1467 if ( ls ) {
1468 return (ls->isRing());
1469 } else {
1470 return 0;
1473 catch (const std::exception &e)
1475 handle->ERROR_MESSAGE("%s", e.what());
1476 return 2;
1479 catch (...)
1481 handle->ERROR_MESSAGE("Unknown exception thrown");
1482 return 2;
1488 //free the result of this
1489 char *
1490 GEOSGeomType_r(GEOSContextHandle_t extHandle, const Geometry *g1)
1492 if ( 0 == extHandle )
1494 return NULL;
1497 GEOSContextHandleInternal_t *handle = 0;
1498 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
1499 if ( 0 == handle->initialized )
1501 return NULL;
1506 std::string s = g1->getGeometryType();
1508 char *result = gstrdup(s);
1509 return result;
1511 catch (const std::exception &e)
1513 handle->ERROR_MESSAGE("%s", e.what());
1515 catch (...)
1517 handle->ERROR_MESSAGE("Unknown exception thrown");
1520 return NULL;
1523 // Return postgis geometry type index
1525 GEOSGeomTypeId_r(GEOSContextHandle_t extHandle, const Geometry *g1)
1527 if ( 0 == extHandle )
1529 return -1;
1532 GEOSContextHandleInternal_t *handle = 0;
1533 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
1534 if ( 0 == handle->initialized )
1536 return -1;
1541 return g1->getGeometryTypeId();
1543 catch (const std::exception &e)
1545 handle->ERROR_MESSAGE("%s", e.what());
1547 catch (...)
1549 handle->ERROR_MESSAGE("Unknown exception thrown");
1552 return -1;
1555 //-------------------------------------------------------------------
1556 // GEOS functions that return geometries
1557 //-------------------------------------------------------------------
1559 Geometry *
1560 GEOSEnvelope_r(GEOSContextHandle_t extHandle, const Geometry *g1)
1562 if ( 0 == extHandle )
1564 return NULL;
1567 GEOSContextHandleInternal_t *handle = 0;
1568 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
1569 if ( 0 == handle->initialized )
1571 return NULL;
1576 Geometry *g3 = g1->getEnvelope();
1577 return g3;
1579 catch (const std::exception &e)
1581 handle->ERROR_MESSAGE("%s", e.what());
1583 catch (...)
1585 handle->ERROR_MESSAGE("Unknown exception thrown");
1588 return NULL;
1591 Geometry *
1592 GEOSIntersection_r(GEOSContextHandle_t extHandle, const Geometry *g1, const Geometry *g2)
1594 if ( 0 == extHandle )
1596 return NULL;
1599 GEOSContextHandleInternal_t *handle = 0;
1600 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
1601 if ( 0 == handle->initialized )
1603 return NULL;
1608 GeomAutoPtr g3(BinaryOp(g1, g2, overlayOp(OverlayOp::opINTERSECTION)));
1609 return g3.release();
1611 // XXX: old version
1612 //Geometry *g3 = g1->intersection(g2);
1613 //return g3;
1615 catch (const std::exception &e)
1617 handle->ERROR_MESSAGE("%s", e.what());
1619 catch (...)
1621 handle->ERROR_MESSAGE("Unknown exception thrown");
1624 return NULL;
1627 Geometry *
1628 GEOSBuffer_r(GEOSContextHandle_t extHandle, const Geometry *g1, double width, int quadrantsegments)
1630 if ( 0 == extHandle )
1632 return NULL;
1635 GEOSContextHandleInternal_t *handle = 0;
1636 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
1637 if ( 0 == handle->initialized )
1639 return NULL;
1644 Geometry *g3 = g1->buffer(width, quadrantsegments);
1645 return g3;
1647 catch (const std::exception &e)
1649 handle->ERROR_MESSAGE("%s", e.what());
1651 catch (...)
1653 handle->ERROR_MESSAGE("Unknown exception thrown");
1656 return NULL;
1659 Geometry *
1660 GEOSBufferWithStyle_r(GEOSContextHandle_t extHandle, const Geometry *g1, double width, int quadsegs, int endCapStyle, int joinStyle, double mitreLimit)
1662 using geos::operation::buffer::BufferParameters;
1663 using geos::operation::buffer::BufferOp;
1664 using geos::util::IllegalArgumentException;
1666 if ( 0 == extHandle )
1668 return NULL;
1671 GEOSContextHandleInternal_t *handle = 0;
1672 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
1673 if ( 0 == handle->initialized )
1675 return NULL;
1680 BufferParameters bp;
1681 bp.setQuadrantSegments(quadsegs);
1683 if ( endCapStyle > BufferParameters::CAP_SQUARE )
1685 throw IllegalArgumentException("Invalid buffer endCap style");
1687 bp.setEndCapStyle(
1688 static_cast<BufferParameters::EndCapStyle>(endCapStyle)
1691 if ( joinStyle > BufferParameters::JOIN_BEVEL )
1693 throw IllegalArgumentException("Invalid buffer join style");
1695 bp.setJoinStyle(
1696 static_cast<BufferParameters::JoinStyle>(joinStyle)
1698 bp.setMitreLimit(mitreLimit);
1699 BufferOp op(g1, bp);
1700 Geometry *g3 = op.getResultGeometry(width);
1701 return g3;
1703 catch (const std::exception &e)
1705 handle->ERROR_MESSAGE("%s", e.what());
1707 catch (...)
1709 handle->ERROR_MESSAGE("Unknown exception thrown");
1712 return NULL;
1715 Geometry *
1716 GEOSOffsetCurve_r(GEOSContextHandle_t extHandle, const Geometry *g1, double width, int quadsegs, int joinStyle, double mitreLimit)
1718 if ( 0 == extHandle ) return NULL;
1720 GEOSContextHandleInternal_t *handle = 0;
1721 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
1722 if ( 0 == handle->initialized ) return NULL;
1726 BufferParameters bp;
1727 bp.setEndCapStyle( BufferParameters::CAP_FLAT );
1728 bp.setQuadrantSegments(quadsegs);
1730 if ( joinStyle > BufferParameters::JOIN_BEVEL )
1732 throw IllegalArgumentException("Invalid buffer join style");
1734 bp.setJoinStyle(
1735 static_cast<BufferParameters::JoinStyle>(joinStyle)
1737 bp.setMitreLimit(mitreLimit);
1739 bool isLeftSide = true;
1740 if ( width < 0 ) {
1741 isLeftSide = false;
1742 width = -width;
1744 BufferBuilder bufBuilder (bp);
1745 Geometry *g3 = bufBuilder.bufferLineSingleSided(g1, width, isLeftSide);
1747 return g3;
1749 catch (const std::exception &e)
1751 handle->ERROR_MESSAGE("%s", e.what());
1753 catch (...)
1755 handle->ERROR_MESSAGE("Unknown exception thrown");
1758 return NULL;
1761 /* @deprecated in 3.3.0 */
1762 Geometry *
1763 GEOSSingleSidedBuffer_r(GEOSContextHandle_t extHandle, const Geometry *g1, double width, int quadsegs, int joinStyle, double mitreLimit, int leftSide)
1765 if ( 0 == extHandle ) return NULL;
1767 GEOSContextHandleInternal_t *handle = 0;
1768 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
1769 if ( 0 == handle->initialized ) return NULL;
1773 BufferParameters bp;
1774 bp.setEndCapStyle( BufferParameters::CAP_FLAT );
1775 bp.setQuadrantSegments(quadsegs);
1777 if ( joinStyle > BufferParameters::JOIN_BEVEL )
1779 throw IllegalArgumentException("Invalid buffer join style");
1781 bp.setJoinStyle(
1782 static_cast<BufferParameters::JoinStyle>(joinStyle)
1784 bp.setMitreLimit(mitreLimit);
1786 bool isLeftSide = leftSide == 0 ? false : true;
1787 BufferBuilder bufBuilder (bp);
1788 Geometry *g3 = bufBuilder.bufferLineSingleSided(g1, width, isLeftSide);
1790 return g3;
1792 catch (const std::exception &e)
1794 handle->ERROR_MESSAGE("%s", e.what());
1796 catch (...)
1798 handle->ERROR_MESSAGE("Unknown exception thrown");
1801 return NULL;
1804 Geometry *
1805 GEOSConvexHull_r(GEOSContextHandle_t extHandle, const Geometry *g1)
1807 if ( 0 == extHandle )
1809 return NULL;
1812 GEOSContextHandleInternal_t *handle = 0;
1813 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
1814 if ( 0 == handle->initialized )
1816 return NULL;
1821 Geometry *g3 = g1->convexHull();
1822 return g3;
1824 catch (const std::exception &e)
1826 handle->ERROR_MESSAGE("%s", e.what());
1828 catch (...)
1830 handle->ERROR_MESSAGE("Unknown exception thrown");
1833 return NULL;
1836 Geometry *
1837 GEOSDifference_r(GEOSContextHandle_t extHandle, const Geometry *g1, const Geometry *g2)
1839 if ( 0 == extHandle )
1841 return NULL;
1844 GEOSContextHandleInternal_t *handle = 0;
1845 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
1846 if ( 0 == handle->initialized )
1848 return NULL;
1853 GeomAutoPtr g3(BinaryOp(g1, g2, overlayOp(OverlayOp::opDIFFERENCE)));
1854 return g3.release();
1856 // XXX: old version
1857 //Geometry *g3 = g1->difference(g2);
1858 //return g3;
1860 catch (const std::exception &e)
1862 handle->ERROR_MESSAGE("%s", e.what());
1864 catch (...)
1866 handle->ERROR_MESSAGE("Unknown exception thrown");
1869 return NULL;
1872 Geometry *
1873 GEOSBoundary_r(GEOSContextHandle_t extHandle, const Geometry *g1)
1875 if ( 0 == extHandle )
1877 return NULL;
1880 GEOSContextHandleInternal_t *handle = 0;
1881 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
1882 if ( 0 == handle->initialized )
1884 return NULL;
1889 Geometry *g3 = g1->getBoundary();
1890 return g3;
1892 catch (const std::exception &e)
1894 handle->ERROR_MESSAGE("%s", e.what());
1896 catch (...)
1898 handle->ERROR_MESSAGE("Unknown exception thrown");
1901 return NULL;
1904 Geometry *
1905 GEOSSymDifference_r(GEOSContextHandle_t extHandle, const Geometry *g1, const Geometry *g2)
1907 if ( 0 == extHandle )
1909 return NULL;
1912 GEOSContextHandleInternal_t *handle = 0;
1913 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
1914 if ( 0 == handle->initialized )
1916 return NULL;
1921 GeomAutoPtr g3 = BinaryOp(g1, g2, overlayOp(OverlayOp::opSYMDIFFERENCE));
1922 return g3.release();
1923 //Geometry *g3 = g1->symDifference(g2);
1924 //return g3;
1926 catch (const std::exception &e)
1928 handle->ERROR_MESSAGE("%s", e.what());
1929 return NULL;
1932 catch (...)
1934 handle->ERROR_MESSAGE("Unknown exception thrown");
1935 return NULL;
1939 Geometry *
1940 GEOSUnion_r(GEOSContextHandle_t extHandle, const Geometry *g1, const Geometry *g2)
1942 if ( 0 == extHandle )
1944 return NULL;
1947 GEOSContextHandleInternal_t *handle = 0;
1948 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
1949 if ( 0 == handle->initialized )
1951 return NULL;
1956 GeomAutoPtr g3 = BinaryOp(g1, g2, overlayOp(OverlayOp::opUNION));
1957 return g3.release();
1959 // XXX: old version
1960 //Geometry *g3 = g1->Union(g2);
1961 //return g3;
1963 catch (const std::exception &e)
1965 #if VERBOSE_EXCEPTIONS
1966 std::ostringstream s;
1967 s << "Exception on GEOSUnion with following inputs:" << std::endl;
1968 s << "A: "<<g1->toString() << std::endl;
1969 s << "B: "<<g2->toString() << std::endl;
1970 handle->NOTICE_MESSAGE("%s", s.str().c_str());
1971 #endif // VERBOSE_EXCEPTIONS
1972 handle->ERROR_MESSAGE("%s", e.what());
1974 catch (...)
1976 handle->ERROR_MESSAGE("Unknown exception thrown");
1979 return NULL;
1982 Geometry *
1983 GEOSUnaryUnion_r(GEOSContextHandle_t extHandle, const Geometry *g)
1985 if ( 0 == extHandle )
1987 return NULL;
1990 GEOSContextHandleInternal_t *handle = 0;
1991 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
1992 if ( 0 == handle->initialized )
1994 return NULL;
1999 GeomAutoPtr g3 ( g->Union() );
2000 return g3.release();
2002 catch (const std::exception &e)
2004 #if VERBOSE_EXCEPTIONS
2005 std::ostringstream s;
2006 s << "Exception on GEOSUnaryUnion with following inputs:" << std::endl;
2007 s << "A: "<<g1->toString() << std::endl;
2008 s << "B: "<<g2->toString() << std::endl;
2009 handle->NOTICE_MESSAGE("%s", s.str().c_str());
2010 #endif // VERBOSE_EXCEPTIONS
2011 handle->ERROR_MESSAGE("%s", e.what());
2013 catch (...)
2015 handle->ERROR_MESSAGE("Unknown exception thrown");
2018 return NULL;
2021 Geometry *
2022 GEOSNode_r(GEOSContextHandle_t extHandle, const Geometry *g)
2024 if ( 0 == extHandle )
2026 return NULL;
2029 GEOSContextHandleInternal_t *handle = 0;
2030 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
2031 if ( 0 == handle->initialized )
2033 return NULL;
2038 std::auto_ptr<Geometry> g3 = geos::noding::GeometryNoder::node(*g);
2039 return g3.release();
2041 catch (const std::exception &e)
2043 #if VERBOSE_EXCEPTIONS
2044 std::ostringstream s;
2045 s << "Exception on GEOSUnaryUnion with following inputs:" << std::endl;
2046 s << "A: "<<g1->toString() << std::endl;
2047 s << "B: "<<g2->toString() << std::endl;
2048 handle->NOTICE_MESSAGE("%s", s.str().c_str());
2049 #endif // VERBOSE_EXCEPTIONS
2050 handle->ERROR_MESSAGE("%s", e.what());
2052 catch (...)
2054 handle->ERROR_MESSAGE("Unknown exception thrown");
2057 return NULL;
2060 Geometry *
2061 GEOSUnionCascaded_r(GEOSContextHandle_t extHandle, const Geometry *g1)
2063 if ( 0 == extHandle )
2065 return NULL;
2068 GEOSContextHandleInternal_t *handle = 0;
2069 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
2070 if ( 0 == handle->initialized )
2072 return NULL;
2077 const geos::geom::MultiPolygon *p = dynamic_cast<const geos::geom::MultiPolygon *>(g1);
2078 if ( ! p )
2080 handle->ERROR_MESSAGE("Invalid argument (must be a MultiPolygon)");
2081 return NULL;
2084 using geos::operation::geounion::CascadedPolygonUnion;
2085 return CascadedPolygonUnion::Union(p);
2087 catch (const std::exception &e)
2089 handle->ERROR_MESSAGE("%s", e.what());
2091 catch (...)
2093 handle->ERROR_MESSAGE("Unknown exception thrown");
2096 return NULL;
2099 Geometry *
2100 GEOSPointOnSurface_r(GEOSContextHandle_t extHandle, const Geometry *g1)
2102 if ( 0 == extHandle )
2104 return NULL;
2107 GEOSContextHandleInternal_t *handle = 0;
2108 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
2109 if ( 0 == handle->initialized )
2111 return NULL;
2116 Geometry *ret = g1->getInteriorPoint();
2117 if ( ! ret )
2119 const GeometryFactory* gf = handle->geomFactory;
2120 // return an empty collection
2121 return gf->createGeometryCollection();
2123 return ret;
2125 catch (const std::exception &e)
2127 handle->ERROR_MESSAGE("%s", e.what());
2129 catch (...)
2131 handle->ERROR_MESSAGE("Unknown exception thrown");
2134 return NULL;
2137 //-------------------------------------------------------------------
2138 // memory management functions
2139 //------------------------------------------------------------------
2141 void
2142 GEOSGeom_destroy_r(GEOSContextHandle_t extHandle, Geometry *a)
2144 GEOSContextHandleInternal_t *handle = 0;
2146 // FIXME: mloskot: Does this try-catch around delete means that
2147 // destructors in GEOS may throw? If it does, this is a serious
2148 // violation of "never throw an exception from a destructor" principle
2152 delete a;
2154 catch (const std::exception &e)
2156 if ( 0 == extHandle )
2158 return;
2161 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
2162 if ( 0 == handle->initialized )
2164 return;
2167 handle->ERROR_MESSAGE("%s", e.what());
2169 catch (...)
2171 if ( 0 == extHandle )
2173 return;
2176 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
2177 if ( 0 == handle->initialized )
2179 return;
2182 handle->ERROR_MESSAGE("Unknown exception thrown");
2186 void
2187 GEOSSetSRID_r(GEOSContextHandle_t extHandle, Geometry *g, int srid)
2189 assert(0 != g);
2191 if ( 0 == extHandle )
2193 return;
2196 GEOSContextHandleInternal_t *handle = 0;
2197 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
2198 if ( 0 == handle->initialized )
2200 return;
2203 g->setSRID(srid);
2208 GEOSGetNumCoordinates_r(GEOSContextHandle_t extHandle, const Geometry *g)
2210 assert(0 != g);
2212 if ( 0 == extHandle )
2214 return -1;
2217 GEOSContextHandleInternal_t *handle = 0;
2218 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
2219 if ( 0 == handle->initialized )
2221 return -1;
2226 return static_cast<int>(g->getNumPoints());
2228 catch (const std::exception &e)
2230 handle->ERROR_MESSAGE("%s", e.what());
2232 catch (...)
2234 handle->ERROR_MESSAGE("Unknown exception thrown");
2237 return -1;
2241 * Return -1 on exception, 0 otherwise.
2242 * Converts Geometry to normal form (or canonical form).
2245 GEOSNormalize_r(GEOSContextHandle_t extHandle, Geometry *g)
2247 assert(0 != g);
2249 if ( 0 == extHandle )
2251 return -1;
2254 GEOSContextHandleInternal_t *handle = 0;
2255 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
2256 if ( 0 == handle->initialized )
2258 return -1;
2263 g->normalize();
2264 return 0; // SUCCESS
2266 catch (const std::exception &e)
2268 handle->ERROR_MESSAGE("%s", e.what());
2270 catch (...)
2272 handle->ERROR_MESSAGE("Unknown exception thrown");
2275 return -1;
2279 GEOSGetNumInteriorRings_r(GEOSContextHandle_t extHandle, const Geometry *g1)
2281 if ( 0 == extHandle )
2283 return -1;
2286 GEOSContextHandleInternal_t *handle = 0;
2287 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
2288 if ( 0 == handle->initialized )
2290 return -1;
2295 const Polygon *p = dynamic_cast<const Polygon *>(g1);
2296 if ( ! p )
2298 handle->ERROR_MESSAGE("Argument is not a Polygon");
2299 return -1;
2301 return static_cast<int>(p->getNumInteriorRing());
2303 catch (const std::exception &e)
2305 handle->ERROR_MESSAGE("%s", e.what());
2307 catch (...)
2309 handle->ERROR_MESSAGE("Unknown exception thrown");
2312 return -1;
2316 // returns -1 on error and 1 for non-multi geometries
2318 GEOSGetNumGeometries_r(GEOSContextHandle_t extHandle, const Geometry *g1)
2320 if ( 0 == extHandle )
2322 return -1;
2325 GEOSContextHandleInternal_t *handle = 0;
2326 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
2327 if ( 0 == handle->initialized )
2329 return -1;
2334 return static_cast<int>(g1->getNumGeometries());
2336 catch (const std::exception &e)
2338 handle->ERROR_MESSAGE("%s", e.what());
2340 catch (...)
2342 handle->ERROR_MESSAGE("Unknown exception thrown");
2345 return -1;
2350 * Call only on GEOMETRYCOLLECTION or MULTI*.
2351 * Return a pointer to the internal Geometry.
2353 const Geometry *
2354 GEOSGetGeometryN_r(GEOSContextHandle_t extHandle, const Geometry *g1, int n)
2356 if ( 0 == extHandle )
2358 return NULL;
2361 GEOSContextHandleInternal_t *handle = 0;
2362 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
2363 if ( 0 == handle->initialized )
2365 return NULL;
2370 return g1->getGeometryN(n);
2372 catch (const std::exception &e)
2374 handle->ERROR_MESSAGE("%s", e.what());
2376 catch (...)
2378 handle->ERROR_MESSAGE("Unknown exception thrown");
2381 return NULL;
2385 * Call only on LINESTRING
2386 * Returns NULL on exception
2388 Geometry *
2389 GEOSGeomGetPointN_r(GEOSContextHandle_t extHandle, const Geometry *g1, int n)
2391 if ( 0 == extHandle )
2393 return NULL;
2396 GEOSContextHandleInternal_t *handle = 0;
2397 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
2398 if ( 0 == handle->initialized )
2400 return NULL;
2405 using geos::geom::LineString;
2406 const LineString *ls = dynamic_cast<const LineString *>(g1);
2407 if ( ! ls )
2409 handle->ERROR_MESSAGE("Argument is not a LineString");
2410 return NULL;
2412 return ls->getPointN(n);
2414 catch (const std::exception &e)
2416 handle->ERROR_MESSAGE("%s", e.what());
2418 catch (...)
2420 handle->ERROR_MESSAGE("Unknown exception thrown");
2423 return NULL;
2427 * Call only on LINESTRING
2429 Geometry *
2430 GEOSGeomGetStartPoint_r(GEOSContextHandle_t extHandle, const Geometry *g1)
2432 if ( 0 == extHandle )
2434 return NULL;
2437 GEOSContextHandleInternal_t *handle = 0;
2438 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
2439 if ( 0 == handle->initialized )
2441 return NULL;
2446 using geos::geom::LineString;
2447 const LineString *ls = dynamic_cast<const LineString *>(g1);
2448 if ( ! ls )
2450 handle->ERROR_MESSAGE("Argument is not a LineString");
2451 return NULL;
2453 return ls->getStartPoint();
2455 catch (const std::exception &e)
2457 handle->ERROR_MESSAGE("%s", e.what());
2459 catch (...)
2461 handle->ERROR_MESSAGE("Unknown exception thrown");
2464 return NULL;
2468 * Call only on LINESTRING
2470 Geometry *
2471 GEOSGeomGetEndPoint_r(GEOSContextHandle_t extHandle, const Geometry *g1)
2473 if ( 0 == extHandle )
2475 return NULL;
2478 GEOSContextHandleInternal_t *handle = 0;
2479 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
2480 if ( 0 == handle->initialized )
2482 return NULL;
2487 using geos::geom::LineString;
2488 const LineString *ls = dynamic_cast<const LineString *>(g1);
2489 if ( ! ls )
2491 handle->ERROR_MESSAGE("Argument is not a LineString");
2492 return NULL;
2494 return ls->getEndPoint();
2496 catch (const std::exception &e)
2498 handle->ERROR_MESSAGE("%s", e.what());
2500 catch (...)
2502 handle->ERROR_MESSAGE("Unknown exception thrown");
2505 return NULL;
2509 * Call only on LINESTRING
2510 * return 2 on exception, 1 on true, 0 on false
2512 char
2513 GEOSisClosed_r(GEOSContextHandle_t extHandle, const Geometry *g1)
2515 if ( 0 == extHandle )
2517 return 2;
2520 GEOSContextHandleInternal_t *handle = 0;
2521 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
2522 if ( 0 == handle->initialized )
2524 return 2;
2529 using geos::geom::LineString;
2530 const LineString *ls = dynamic_cast<const LineString *>(g1);
2531 if ( ! ls )
2533 handle->ERROR_MESSAGE("Argument is not a LineString");
2534 return 2;
2536 return ls->isClosed();
2538 catch (const std::exception &e)
2540 handle->ERROR_MESSAGE("%s", e.what());
2542 catch (...)
2544 handle->ERROR_MESSAGE("Unknown exception thrown");
2547 return 2;
2551 * Call only on LINESTRING
2552 * return 0 on exception, otherwise 1
2555 GEOSGeomGetLength_r(GEOSContextHandle_t extHandle, const Geometry *g1, double *length)
2557 if ( 0 == extHandle )
2559 return 0;
2562 GEOSContextHandleInternal_t *handle = 0;
2563 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
2564 if ( 0 == handle->initialized )
2566 return 0;
2571 using geos::geom::LineString;
2572 const LineString *ls = dynamic_cast<const LineString *>(g1);
2573 if ( ! ls )
2575 handle->ERROR_MESSAGE("Argument is not a LineString");
2576 return 0;
2578 *length = ls->getLength();
2579 return 1;
2581 catch (const std::exception &e)
2583 handle->ERROR_MESSAGE("%s", e.what());
2585 catch (...)
2587 handle->ERROR_MESSAGE("Unknown exception thrown");
2590 return 0;
2594 * Call only on LINESTRING
2597 GEOSGeomGetNumPoints_r(GEOSContextHandle_t extHandle, const Geometry *g1)
2599 if ( 0 == extHandle )
2601 return -1;
2604 GEOSContextHandleInternal_t *handle = 0;
2605 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
2606 if ( 0 == handle->initialized )
2608 return -1;
2613 using geos::geom::LineString;
2614 const LineString *ls = dynamic_cast<const LineString *>(g1);
2615 if ( ! ls )
2617 handle->ERROR_MESSAGE("Argument is not a LineString");
2618 return -1;
2620 return static_cast<int>(ls->getNumPoints());
2622 catch (const std::exception &e)
2624 handle->ERROR_MESSAGE("%s", e.what());
2626 catch (...)
2628 handle->ERROR_MESSAGE("Unknown exception thrown");
2631 return -1;
2635 * For POINT
2636 * returns 0 on exception, otherwise 1
2639 GEOSGeomGetX_r(GEOSContextHandle_t extHandle, const Geometry *g1, double *x)
2641 if ( 0 == extHandle )
2643 return 0;
2646 GEOSContextHandleInternal_t *handle = 0;
2647 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
2648 if ( 0 == handle->initialized )
2650 return 0;
2655 using geos::geom::Point;
2656 const Point *po = dynamic_cast<const Point *>(g1);
2657 if ( ! po )
2659 handle->ERROR_MESSAGE("Argument is not a Point");
2660 return 0;
2662 *x = po->getX();
2663 return 1;
2665 catch (const std::exception &e)
2667 handle->ERROR_MESSAGE("%s", e.what());
2669 catch (...)
2671 handle->ERROR_MESSAGE("Unknown exception thrown");
2674 return 0;
2678 * For POINT
2679 * returns 0 on exception, otherwise 1
2682 GEOSGeomGetY_r(GEOSContextHandle_t extHandle, const Geometry *g1, double *y)
2684 if ( 0 == extHandle )
2686 return 0;
2689 GEOSContextHandleInternal_t *handle = 0;
2690 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
2691 if ( 0 == handle->initialized )
2693 return 0;
2698 using geos::geom::Point;
2699 const Point *po = dynamic_cast<const Point *>(g1);
2700 if ( ! po )
2702 handle->ERROR_MESSAGE("Argument is not a Point");
2703 return 0;
2705 *y = po->getY();
2706 return 1;
2708 catch (const std::exception &e)
2710 handle->ERROR_MESSAGE("%s", e.what());
2712 catch (...)
2714 handle->ERROR_MESSAGE("Unknown exception thrown");
2717 return 0;
2721 * Call only on polygon
2722 * Return a copy of the internal Geometry.
2724 const Geometry *
2725 GEOSGetExteriorRing_r(GEOSContextHandle_t extHandle, const Geometry *g1)
2727 if ( 0 == extHandle )
2729 return NULL;
2732 GEOSContextHandleInternal_t *handle = 0;
2733 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
2734 if ( 0 == handle->initialized )
2736 return NULL;
2741 const Polygon *p = dynamic_cast<const Polygon *>(g1);
2742 if ( ! p )
2744 handle->ERROR_MESSAGE("Invalid argument (must be a Polygon)");
2745 return NULL;
2747 return p->getExteriorRing();
2749 catch (const std::exception &e)
2751 handle->ERROR_MESSAGE("%s", e.what());
2753 catch (...)
2755 handle->ERROR_MESSAGE("Unknown exception thrown");
2758 return NULL;
2762 * Call only on polygon
2763 * Return a pointer to internal storage, do not destroy it.
2765 const Geometry *
2766 GEOSGetInteriorRingN_r(GEOSContextHandle_t extHandle, const Geometry *g1, int n)
2768 if ( 0 == extHandle )
2770 return NULL;
2773 GEOSContextHandleInternal_t *handle = 0;
2774 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
2775 if ( 0 == handle->initialized )
2777 return NULL;
2782 const Polygon *p = dynamic_cast<const Polygon *>(g1);
2783 if ( ! p )
2785 handle->ERROR_MESSAGE("Invalid argument (must be a Polygon)");
2786 return NULL;
2788 return p->getInteriorRingN(n);
2790 catch (const std::exception &e)
2792 handle->ERROR_MESSAGE("%s", e.what());
2794 catch (...)
2796 handle->ERROR_MESSAGE("Unknown exception thrown");
2799 return NULL;
2802 Geometry *
2803 GEOSGetCentroid_r(GEOSContextHandle_t extHandle, const Geometry *g)
2805 if ( 0 == extHandle )
2807 return NULL;
2810 GEOSContextHandleInternal_t *handle = 0;
2811 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
2812 if ( 0 == handle->initialized )
2814 return NULL;
2819 Geometry *ret = g->getCentroid();
2820 if (0 == ret)
2822 const GeometryFactory *gf = handle->geomFactory;
2823 return gf->createGeometryCollection();
2825 return ret;
2827 catch (const std::exception &e)
2829 handle->ERROR_MESSAGE("%s", e.what());
2831 catch (...)
2833 handle->ERROR_MESSAGE("Unknown exception thrown");
2836 return NULL;
2839 Geometry *
2840 GEOSGeom_createEmptyCollection_r(GEOSContextHandle_t extHandle, int type)
2842 if ( 0 == extHandle )
2844 return NULL;
2847 GEOSContextHandleInternal_t *handle = 0;
2848 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
2849 if ( 0 == handle->initialized )
2851 return NULL;
2854 #ifdef GEOS_DEBUG
2855 char buf[256];
2856 sprintf(buf, "createCollection: requested type %d, ngeoms: %d",
2857 type, ngeoms);
2858 handle->NOTICE_MESSAGE("%s", buf);// TODO: Can handle->NOTICE_MESSAGE format that directly?
2859 #endif
2863 const GeometryFactory* gf = handle->geomFactory;
2865 Geometry *g = 0;
2866 switch (type)
2868 case GEOS_GEOMETRYCOLLECTION:
2869 g = gf->createGeometryCollection();
2870 break;
2871 case GEOS_MULTIPOINT:
2872 g = gf->createMultiPoint();
2873 break;
2874 case GEOS_MULTILINESTRING:
2875 g = gf->createMultiLineString();
2876 break;
2877 case GEOS_MULTIPOLYGON:
2878 g = gf->createMultiPolygon();
2879 break;
2880 default:
2881 handle->ERROR_MESSAGE("Unsupported type request for GEOSGeom_createEmptyCollection_r");
2882 g = 0;
2886 return g;
2888 catch (const std::exception &e)
2890 handle->ERROR_MESSAGE("%s", e.what());
2892 catch (...)
2894 handle->ERROR_MESSAGE("Unknown exception thrown");
2897 return 0;
2900 Geometry *
2901 GEOSGeom_createCollection_r(GEOSContextHandle_t extHandle, int type, Geometry **geoms, unsigned int ngeoms)
2903 if ( 0 == extHandle )
2905 return NULL;
2908 GEOSContextHandleInternal_t *handle = 0;
2909 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
2910 if ( 0 == handle->initialized )
2912 return NULL;
2915 #ifdef GEOS_DEBUG
2916 char buf[256];
2917 sprintf(buf, "PostGIS2GEOS_collection: requested type %d, ngeoms: %d",
2918 type, ngeoms);
2919 handle->NOTICE_MESSAGE("%s", buf);// TODO: Can handle->NOTICE_MESSAGE format that directly?
2920 #endif
2924 const GeometryFactory* gf = handle->geomFactory;
2925 std::vector<Geometry*>* vgeoms = new std::vector<Geometry*>(geoms, geoms + ngeoms);
2927 Geometry *g = 0;
2928 switch (type)
2930 case GEOS_GEOMETRYCOLLECTION:
2931 g = gf->createGeometryCollection(vgeoms);
2932 break;
2933 case GEOS_MULTIPOINT:
2934 g = gf->createMultiPoint(vgeoms);
2935 break;
2936 case GEOS_MULTILINESTRING:
2937 g = gf->createMultiLineString(vgeoms);
2938 break;
2939 case GEOS_MULTIPOLYGON:
2940 g = gf->createMultiPolygon(vgeoms);
2941 break;
2942 default:
2943 handle->ERROR_MESSAGE("Unsupported type request for PostGIS2GEOS_collection");
2944 g = 0;
2948 return g;
2950 catch (const std::exception &e)
2952 handle->ERROR_MESSAGE("%s", e.what());
2954 catch (...)
2956 handle->ERROR_MESSAGE("Unknown exception thrown");
2959 return 0;
2962 Geometry *
2963 GEOSPolygonize_r(GEOSContextHandle_t extHandle, const Geometry * const * g, unsigned int ngeoms)
2965 if ( 0 == extHandle )
2967 return 0;
2970 GEOSContextHandleInternal_t *handle = 0;
2971 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
2972 if ( 0 == handle->initialized )
2974 return 0;
2977 Geometry *out = 0;
2981 // Polygonize
2982 using geos::operation::polygonize::Polygonizer;
2983 Polygonizer plgnzr;
2984 for (std::size_t i = 0; i < ngeoms; ++i)
2986 plgnzr.add(g[i]);
2989 #if GEOS_DEBUG
2990 handle->NOTICE_MESSAGE("geometry vector added to polygonizer");
2991 #endif
2993 std::vector<Polygon*> *polys = plgnzr.getPolygons();
2994 assert(0 != polys);
2996 #if GEOS_DEBUG
2997 handle->NOTICE_MESSAGE("output polygons got");
2998 #endif
3000 // We need a vector of Geometry pointers, not Polygon pointers.
3001 // STL vector doesn't allow transparent upcast of this
3002 // nature, so we explicitly convert.
3003 // (it's just a waste of processor and memory, btw)
3005 // XXX mloskot: Why not to extent GeometryFactory to accept
3006 // vector of polygons or extend Polygonizer to return list of Geometry*
3007 // or add a wrapper which semantic is similar to:
3008 // std::vector<as_polygon<Geometry*> >
3009 std::vector<Geometry*> *polyvec = new std::vector<Geometry *>(polys->size());
3011 for (std::size_t i = 0; i < polys->size(); ++i)
3013 (*polyvec)[i] = (*polys)[i];
3015 delete polys;
3016 polys = 0;
3018 const GeometryFactory *gf = handle->geomFactory;
3020 // The below takes ownership of the passed vector,
3021 // so we must *not* delete it
3022 out = gf->createGeometryCollection(polyvec);
3024 catch (const std::exception &e)
3026 handle->ERROR_MESSAGE("%s", e.what());
3028 catch (...)
3030 handle->ERROR_MESSAGE("Unknown exception thrown");
3033 return out;
3036 Geometry *
3037 GEOSPolygonizer_getCutEdges_r(GEOSContextHandle_t extHandle, const Geometry * const * g, unsigned int ngeoms)
3039 if ( 0 == extHandle )
3041 return 0;
3044 GEOSContextHandleInternal_t *handle = 0;
3045 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
3046 if ( 0 == handle->initialized )
3048 return 0;
3051 Geometry *out = 0;
3055 // Polygonize
3056 using geos::operation::polygonize::Polygonizer;
3057 Polygonizer plgnzr;
3058 for (std::size_t i = 0; i < ngeoms; ++i)
3060 plgnzr.add(g[i]);
3063 #if GEOS_DEBUG
3064 handle->NOTICE_MESSAGE("geometry vector added to polygonizer");
3065 #endif
3067 const std::vector<const LineString *>& lines = plgnzr.getCutEdges();
3069 #if GEOS_DEBUG
3070 handle->NOTICE_MESSAGE("output polygons got");
3071 #endif
3073 // We need a vector of Geometry pointers, not Polygon pointers.
3074 // STL vector doesn't allow transparent upcast of this
3075 // nature, so we explicitly convert.
3076 // (it's just a waste of processor and memory, btw)
3077 // XXX mloskot: See comment for GEOSPolygonize_r
3078 std::vector<Geometry*> *linevec = new std::vector<Geometry *>(lines.size());
3080 for (std::size_t i = 0, n=lines.size(); i < n; ++i)
3082 (*linevec)[i] = lines[i]->clone();
3085 const GeometryFactory *gf = handle->geomFactory;
3087 // The below takes ownership of the passed vector,
3088 // so we must *not* delete it
3089 out = gf->createGeometryCollection(linevec);
3091 catch (const std::exception &e)
3093 handle->ERROR_MESSAGE("%s", e.what());
3095 catch (...)
3097 handle->ERROR_MESSAGE("Unknown exception thrown");
3100 return out;
3103 Geometry *
3104 GEOSPolygonize_full_r(GEOSContextHandle_t extHandle, const Geometry* g,
3105 Geometry** cuts, Geometry** dangles, Geometry** invalid)
3107 if ( 0 == extHandle )
3109 return 0;
3112 GEOSContextHandleInternal_t *handle = 0;
3113 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
3114 if ( 0 == handle->initialized )
3116 return 0;
3121 // Polygonize
3122 using geos::operation::polygonize::Polygonizer;
3123 Polygonizer plgnzr;
3124 for (std::size_t i = 0; i <g->getNumGeometries(); ++i)
3126 plgnzr.add(g->getGeometryN(i));
3129 #if GEOS_DEBUG
3130 handle->NOTICE_MESSAGE("geometry vector added to polygonizer");
3131 #endif
3132 const GeometryFactory *gf = handle->geomFactory;
3134 if ( cuts ) {
3136 const std::vector<const LineString *>& lines = plgnzr.getCutEdges();
3137 std::vector<Geometry*> *linevec = new std::vector<Geometry *>(lines.size());
3138 for (std::size_t i = 0, n=lines.size(); i < n; ++i)
3140 (*linevec)[i] = lines[i]->clone();
3143 // The below takes ownership of the passed vector,
3144 // so we must *not* delete it
3145 *cuts = gf->createGeometryCollection(linevec);
3148 if ( dangles ) {
3150 const std::vector<const LineString *>& lines = plgnzr.getDangles();
3151 std::vector<Geometry*> *linevec = new std::vector<Geometry *>(lines.size());
3152 for (std::size_t i = 0, n=lines.size(); i < n; ++i)
3154 (*linevec)[i] = lines[i]->clone();
3157 // The below takes ownership of the passed vector,
3158 // so we must *not* delete it
3159 *dangles = gf->createGeometryCollection(linevec);
3162 if ( invalid ) {
3164 const std::vector<LineString *>& lines = plgnzr.getInvalidRingLines();
3165 std::vector<Geometry*> *linevec = new std::vector<Geometry *>(lines.size());
3166 for (std::size_t i = 0, n=lines.size(); i < n; ++i)
3168 (*linevec)[i] = lines[i]->clone();
3171 // The below takes ownership of the passed vector,
3172 // so we must *not* delete it
3173 *invalid = gf->createGeometryCollection(linevec);
3176 std::vector<Polygon*> *polys = plgnzr.getPolygons();
3177 std::vector<Geometry*> *polyvec = new std::vector<Geometry *>(polys->size());
3178 for (std::size_t i = 0; i < polys->size(); ++i)
3180 (*polyvec)[i] = (*polys)[i];
3182 delete polys;
3184 return gf->createGeometryCollection(polyvec);
3187 catch (const std::exception &e)
3189 handle->ERROR_MESSAGE("%s", e.what());
3190 return 0;
3192 catch (...)
3194 handle->ERROR_MESSAGE("Unknown exception thrown");
3195 return 0;
3199 Geometry *
3200 GEOSLineMerge_r(GEOSContextHandle_t extHandle, const Geometry *g)
3202 if ( 0 == extHandle )
3204 return 0;
3207 GEOSContextHandleInternal_t *handle = 0;
3208 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
3209 if ( 0 == handle->initialized )
3211 return 0;
3214 Geometry *out = 0;
3218 using geos::operation::linemerge::LineMerger;
3219 LineMerger lmrgr;
3220 lmrgr.add(g);
3222 std::vector<LineString *>* lines = lmrgr.getMergedLineStrings();
3223 assert(0 != lines);
3225 #if GEOS_DEBUG
3226 handle->NOTICE_MESSAGE("output lines got");
3227 #endif
3229 std::vector<Geometry *>*geoms = new std::vector<Geometry *>(lines->size());
3230 for (std::vector<Geometry *>::size_type i = 0; i < lines->size(); ++i)
3232 (*geoms)[i] = (*lines)[i];
3234 delete lines;
3235 lines = 0;
3237 const GeometryFactory *gf = handle->geomFactory;
3238 out = gf->buildGeometry(geoms);
3240 // XXX: old version
3241 //out = gf->createGeometryCollection(geoms);
3243 catch (const std::exception &e)
3245 handle->ERROR_MESSAGE("%s", e.what());
3247 catch (...)
3249 handle->ERROR_MESSAGE("Unknown exception thrown");
3252 return out;
3256 GEOSGetSRID_r(GEOSContextHandle_t extHandle, const Geometry *g)
3258 assert(0 != g);
3260 if ( 0 == extHandle )
3262 return 0;
3265 GEOSContextHandleInternal_t *handle = 0;
3266 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
3267 if ( 0 == handle->initialized )
3269 return 0;
3274 return g->getSRID();
3276 catch (const std::exception &e)
3278 handle->ERROR_MESSAGE("%s", e.what());
3280 catch (...)
3282 handle->ERROR_MESSAGE("Unknown exception thrown");
3285 return 0;
3288 const char* GEOSversion()
3290 return GEOS_CAPI_VERSION;
3293 const char* GEOSjtsport()
3295 return GEOS_JTS_PORT;
3298 char
3299 GEOSHasZ_r(GEOSContextHandle_t extHandle, const Geometry *g)
3301 assert(0 != g);
3303 if ( 0 == extHandle )
3305 return -1;
3308 GEOSContextHandleInternal_t *handle = 0;
3309 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
3310 if ( 0 == handle->initialized )
3312 return -1;
3315 if (g->isEmpty())
3317 return false;
3319 assert(0 != g->getCoordinate());
3321 double az = g->getCoordinate()->z;
3322 //handle->ERROR_MESSAGE("ZCoord: %g", az);
3324 return static_cast<char>(FINITE(az));
3328 GEOS_getWKBOutputDims_r(GEOSContextHandle_t extHandle)
3330 if ( 0 == extHandle )
3332 return -1;
3335 GEOSContextHandleInternal_t *handle = 0;
3336 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
3337 if ( 0 == handle->initialized )
3339 return -1;
3342 return handle->WKBOutputDims;
3346 GEOS_setWKBOutputDims_r(GEOSContextHandle_t extHandle, int newdims)
3348 if ( 0 == extHandle )
3350 return -1;
3353 GEOSContextHandleInternal_t *handle = 0;
3354 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
3355 if ( 0 == handle->initialized )
3357 return -1;
3360 if ( newdims < 2 || newdims > 3 )
3362 handle->ERROR_MESSAGE("WKB output dimensions out of range 2..3");
3365 const int olddims = handle->WKBOutputDims;
3366 handle->WKBOutputDims = newdims;
3368 return olddims;
3372 GEOS_getWKBByteOrder_r(GEOSContextHandle_t extHandle)
3374 if ( 0 == extHandle )
3376 return -1;
3379 GEOSContextHandleInternal_t *handle = 0;
3380 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
3381 if ( 0 == handle->initialized )
3383 return -1;
3386 return handle->WKBByteOrder;
3390 GEOS_setWKBByteOrder_r(GEOSContextHandle_t extHandle, int byteOrder)
3392 if ( 0 == extHandle )
3394 return -1;
3397 GEOSContextHandleInternal_t *handle = 0;
3398 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
3399 if ( 0 == handle->initialized )
3401 return -1;
3404 const int oldByteOrder = handle->WKBByteOrder;
3405 handle->WKBByteOrder = byteOrder;
3407 return oldByteOrder;
3411 CoordinateSequence *
3412 GEOSCoordSeq_create_r(GEOSContextHandle_t extHandle, unsigned int size, unsigned int dims)
3414 if ( 0 == extHandle )
3416 return NULL;
3419 GEOSContextHandleInternal_t *handle = 0;
3420 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
3421 if ( 0 == handle->initialized )
3423 return NULL;
3428 const GeometryFactory *gf = handle->geomFactory;
3429 return gf->getCoordinateSequenceFactory()->create(size, dims);
3431 catch (const std::exception &e)
3433 handle->ERROR_MESSAGE("%s", e.what());
3435 catch (...)
3437 handle->ERROR_MESSAGE("Unknown exception thrown");
3440 return NULL;
3444 GEOSCoordSeq_setOrdinate_r(GEOSContextHandle_t extHandle, CoordinateSequence *cs,
3445 unsigned int idx, unsigned int dim, double val)
3447 assert(0 != cs);
3448 if ( 0 == extHandle )
3450 return 0;
3453 GEOSContextHandleInternal_t *handle = 0;
3454 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
3455 if ( 0 == handle->initialized )
3457 return 0;
3462 cs->setOrdinate(idx, dim, val);
3463 return 1;
3465 catch (const std::exception &e)
3467 handle->ERROR_MESSAGE("%s", e.what());
3469 catch (...)
3471 handle->ERROR_MESSAGE("Unknown exception thrown");
3474 return 0;
3478 GEOSCoordSeq_setX_r(GEOSContextHandle_t extHandle, CoordinateSequence *s, unsigned int idx, double val)
3480 return GEOSCoordSeq_setOrdinate_r(extHandle, s, idx, 0, val);
3484 GEOSCoordSeq_setY_r(GEOSContextHandle_t extHandle, CoordinateSequence *s, unsigned int idx, double val)
3486 return GEOSCoordSeq_setOrdinate_r(extHandle, s, idx, 1, val);
3490 GEOSCoordSeq_setZ_r(GEOSContextHandle_t extHandle, CoordinateSequence *s, unsigned int idx, double val)
3492 return GEOSCoordSeq_setOrdinate_r(extHandle, s, idx, 2, val);
3495 CoordinateSequence *
3496 GEOSCoordSeq_clone_r(GEOSContextHandle_t extHandle, const CoordinateSequence *cs)
3498 assert(0 != cs);
3500 if ( 0 == extHandle )
3502 return NULL;
3505 GEOSContextHandleInternal_t *handle = 0;
3506 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
3507 if ( 0 == handle->initialized )
3509 return NULL;
3514 return cs->clone();
3516 catch (const std::exception &e)
3518 handle->ERROR_MESSAGE("%s", e.what());
3520 catch (...)
3522 handle->ERROR_MESSAGE("Unknown exception thrown");
3525 return NULL;
3529 GEOSCoordSeq_getOrdinate_r(GEOSContextHandle_t extHandle, const CoordinateSequence *cs,
3530 unsigned int idx, unsigned int dim, double *val)
3532 assert(0 != cs);
3533 assert(0 != val);
3535 if ( 0 == extHandle )
3537 return 0;
3540 GEOSContextHandleInternal_t *handle = 0;
3541 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
3542 if ( 0 == handle->initialized )
3544 return 0;
3549 double d = cs->getOrdinate(idx, dim);
3550 *val = d;
3552 return 1;
3554 catch (const std::exception &e)
3556 handle->ERROR_MESSAGE("%s", e.what());
3558 catch (...)
3560 handle->ERROR_MESSAGE("Unknown exception thrown");
3563 return 0;
3567 GEOSCoordSeq_getX_r(GEOSContextHandle_t extHandle, const CoordinateSequence *s, unsigned int idx, double *val)
3569 return GEOSCoordSeq_getOrdinate_r(extHandle, s, idx, 0, val);
3573 GEOSCoordSeq_getY_r(GEOSContextHandle_t extHandle, const CoordinateSequence *s, unsigned int idx, double *val)
3575 return GEOSCoordSeq_getOrdinate_r(extHandle, s, idx, 1, val);
3579 GEOSCoordSeq_getZ_r(GEOSContextHandle_t extHandle, const CoordinateSequence *s, unsigned int idx, double *val)
3581 return GEOSCoordSeq_getOrdinate_r(extHandle, s, idx, 2, val);
3585 GEOSCoordSeq_getSize_r(GEOSContextHandle_t extHandle, const CoordinateSequence *cs, unsigned int *size)
3587 assert(0 != cs);
3588 assert(0 != size);
3590 if ( 0 == extHandle )
3592 return 0;
3595 GEOSContextHandleInternal_t *handle = 0;
3596 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
3597 if ( 0 == handle->initialized )
3599 return 0;
3604 const std::size_t sz = cs->getSize();
3605 *size = static_cast<unsigned int>(sz);
3606 return 1;
3608 catch (const std::exception &e)
3610 handle->ERROR_MESSAGE("%s", e.what());
3612 catch (...)
3614 handle->ERROR_MESSAGE("Unknown exception thrown");
3617 return 0;
3621 GEOSCoordSeq_getDimensions_r(GEOSContextHandle_t extHandle, const CoordinateSequence *cs, unsigned int *dims)
3623 assert(0 != cs);
3624 assert(0 != dims);
3626 if ( 0 == extHandle )
3628 return 0;
3631 GEOSContextHandleInternal_t *handle = 0;
3632 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
3633 if ( 0 == handle->initialized )
3635 return 0;
3640 const std::size_t dim = cs->getDimension();
3641 *dims = static_cast<unsigned int>(dim);
3643 return 1;
3645 catch (const std::exception &e)
3647 handle->ERROR_MESSAGE("%s", e.what());
3650 catch (...)
3652 handle->ERROR_MESSAGE("Unknown exception thrown");
3655 return 0;
3658 void
3659 GEOSCoordSeq_destroy_r(GEOSContextHandle_t extHandle, CoordinateSequence *s)
3661 GEOSContextHandleInternal_t *handle = 0;
3665 delete s;
3667 catch (const std::exception &e)
3669 if ( 0 == extHandle )
3671 return;
3674 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
3675 if ( 0 == handle->initialized )
3677 return;
3680 handle->ERROR_MESSAGE("%s", e.what());
3682 catch (...)
3684 if ( 0 == extHandle )
3686 return;
3689 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
3690 if ( 0 == handle->initialized )
3692 return;
3695 handle->ERROR_MESSAGE("Unknown exception thrown");
3699 const CoordinateSequence *
3700 GEOSGeom_getCoordSeq_r(GEOSContextHandle_t extHandle, const Geometry *g)
3702 if ( 0 == extHandle )
3704 return 0;
3707 GEOSContextHandleInternal_t *handle = 0;
3708 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
3709 if ( 0 == handle->initialized )
3711 return 0;
3716 using geos::geom::Point;
3718 const LineString *ls = dynamic_cast<const LineString *>(g);
3719 if ( ls )
3721 return ls->getCoordinatesRO();
3724 const Point *p = dynamic_cast<const Point *>(g);
3725 if ( p )
3727 return p->getCoordinatesRO();
3730 handle->ERROR_MESSAGE("Geometry must be a Point or LineString");
3731 return 0;
3733 catch (const std::exception &e)
3735 handle->ERROR_MESSAGE("%s", e.what());
3737 catch (...)
3739 handle->ERROR_MESSAGE("Unknown exception thrown");
3742 return 0;
3745 Geometry *
3746 GEOSGeom_createEmptyPoint_r(GEOSContextHandle_t extHandle)
3748 if ( 0 == extHandle )
3750 return NULL;
3753 GEOSContextHandleInternal_t *handle = 0;
3754 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
3755 if ( 0 == handle->initialized )
3757 return NULL;
3762 const GeometryFactory *gf = handle->geomFactory;
3763 return gf->createPoint();
3765 catch (const std::exception &e)
3767 handle->ERROR_MESSAGE("%s", e.what());
3769 catch (...)
3771 handle->ERROR_MESSAGE("Unknown exception thrown");
3774 return NULL;
3777 Geometry *
3778 GEOSGeom_createPoint_r(GEOSContextHandle_t extHandle, CoordinateSequence *cs)
3780 if ( 0 == extHandle )
3782 return 0;
3785 GEOSContextHandleInternal_t *handle = 0;
3786 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
3787 if ( 0 == handle->initialized )
3789 return 0;
3794 const GeometryFactory *gf = handle->geomFactory;
3795 return gf->createPoint(cs);
3797 catch (const std::exception &e)
3799 handle->ERROR_MESSAGE("%s", e.what());
3801 catch (...)
3803 handle->ERROR_MESSAGE("Unknown exception thrown");
3806 return 0;
3809 Geometry *
3810 GEOSGeom_createLinearRing_r(GEOSContextHandle_t extHandle, CoordinateSequence *cs)
3812 if ( 0 == extHandle )
3814 return NULL;
3817 GEOSContextHandleInternal_t *handle = 0;
3818 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
3819 if ( 0 == handle->initialized )
3821 return NULL;
3826 const GeometryFactory *gf = handle->geomFactory;
3828 return gf->createLinearRing(cs);
3830 catch (const std::exception &e)
3832 handle->ERROR_MESSAGE("%s", e.what());
3834 catch (...)
3836 handle->ERROR_MESSAGE("Unknown exception thrown");
3839 return NULL;
3842 Geometry *
3843 GEOSGeom_createEmptyLineString_r(GEOSContextHandle_t extHandle)
3845 if ( 0 == extHandle )
3847 return NULL;
3850 GEOSContextHandleInternal_t *handle = 0;
3851 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
3852 if ( 0 == handle->initialized )
3854 return NULL;
3859 const GeometryFactory *gf = handle->geomFactory;
3861 return gf->createLineString();
3863 catch (const std::exception &e)
3865 handle->ERROR_MESSAGE("%s", e.what());
3867 catch (...)
3869 handle->ERROR_MESSAGE("Unknown exception thrown");
3872 return NULL;
3875 Geometry *
3876 GEOSGeom_createLineString_r(GEOSContextHandle_t extHandle, CoordinateSequence *cs)
3878 if ( 0 == extHandle )
3880 return NULL;
3883 GEOSContextHandleInternal_t *handle = 0;
3884 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
3885 if ( 0 == handle->initialized )
3887 return NULL;
3892 const GeometryFactory *gf = handle->geomFactory;
3894 return gf->createLineString(cs);
3896 catch (const std::exception &e)
3898 handle->ERROR_MESSAGE("%s", e.what());
3900 catch (...)
3902 handle->ERROR_MESSAGE("Unknown exception thrown");
3905 return NULL;
3908 Geometry *
3909 GEOSGeom_createEmptyPolygon_r(GEOSContextHandle_t extHandle)
3911 if ( 0 == extHandle )
3913 return NULL;
3916 GEOSContextHandleInternal_t *handle = 0;
3917 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
3918 if ( 0 == handle->initialized )
3920 return NULL;
3925 const GeometryFactory *gf = handle->geomFactory;
3926 return gf->createPolygon();
3928 catch (const std::exception &e)
3930 handle->ERROR_MESSAGE("%s", e.what());
3932 catch (...)
3934 handle->ERROR_MESSAGE("Unknown exception thrown");
3937 return NULL;
3940 Geometry *
3941 GEOSGeom_createPolygon_r(GEOSContextHandle_t extHandle, Geometry *shell, Geometry **holes, unsigned int nholes)
3943 // FIXME: holes must be non-nullptr or may be nullptr?
3944 //assert(0 != holes);
3946 if ( 0 == extHandle )
3948 return NULL;
3951 GEOSContextHandleInternal_t *handle = 0;
3952 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
3953 if ( 0 == handle->initialized )
3955 return NULL;
3960 using geos::geom::LinearRing;
3962 std::vector<Geometry *> *vholes = new std::vector<Geometry *>(holes, holes + nholes);
3964 LinearRing *nshell = dynamic_cast<LinearRing *>(shell);
3965 if ( ! nshell )
3967 handle->ERROR_MESSAGE("Shell is not a LinearRing");
3968 return NULL;
3970 const GeometryFactory *gf = handle->geomFactory;
3972 return gf->createPolygon(nshell, vholes);
3974 catch (const std::exception &e)
3976 handle->ERROR_MESSAGE("%s", e.what());
3978 catch (...)
3980 handle->ERROR_MESSAGE("Unknown exception thrown");
3983 return NULL;
3986 Geometry *
3987 GEOSGeom_clone_r(GEOSContextHandle_t extHandle, const Geometry *g)
3989 assert(0 != g);
3991 if ( 0 == extHandle )
3993 return NULL;
3996 GEOSContextHandleInternal_t *handle = 0;
3997 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
3998 if ( 0 == handle->initialized )
4000 return NULL;
4005 return g->clone();
4007 catch (const std::exception &e)
4009 handle->ERROR_MESSAGE("%s", e.what());
4011 catch (...)
4013 handle->ERROR_MESSAGE("Unknown exception thrown");
4016 return NULL;
4020 GEOSGeom_getDimensions_r(GEOSContextHandle_t extHandle, const Geometry *g)
4022 if ( 0 == extHandle )
4024 return 0;
4027 GEOSContextHandleInternal_t *handle = 0;
4028 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
4029 if ( 0 == handle->initialized )
4031 return 0;
4036 return (int) g->getDimension();
4038 catch (const std::exception &e)
4040 handle->ERROR_MESSAGE("%s", e.what());
4042 catch (...)
4044 handle->ERROR_MESSAGE("Unknown exception thrown");
4047 return 0;
4051 GEOSGeom_getCoordinateDimension_r(GEOSContextHandle_t extHandle, const Geometry *g)
4053 if ( 0 == extHandle )
4055 return 0;
4058 GEOSContextHandleInternal_t *handle = 0;
4059 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
4060 if ( 0 == handle->initialized )
4062 return 0;
4067 return g->getCoordinateDimension();
4069 catch (const std::exception &e)
4071 handle->ERROR_MESSAGE("%s", e.what());
4073 catch (...)
4075 handle->ERROR_MESSAGE("Unknown exception thrown");
4078 return 0;
4081 Geometry *
4082 GEOSSimplify_r(GEOSContextHandle_t extHandle, const Geometry *g1, double tolerance)
4084 if ( 0 == extHandle )
4086 return NULL;
4089 GEOSContextHandleInternal_t *handle = 0;
4090 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
4091 if ( 0 == handle->initialized )
4093 return NULL;
4098 using namespace geos::simplify;
4099 Geometry::AutoPtr g(DouglasPeuckerSimplifier::simplify(g1, tolerance));
4100 return g.release();
4102 catch (const std::exception &e)
4104 handle->ERROR_MESSAGE("%s", e.what());
4106 catch (...)
4108 handle->ERROR_MESSAGE("Unknown exception thrown");
4111 return NULL;
4114 Geometry *
4115 GEOSTopologyPreserveSimplify_r(GEOSContextHandle_t extHandle, const Geometry *g1, double tolerance)
4117 if ( 0 == extHandle )
4119 return NULL;
4122 GEOSContextHandleInternal_t *handle = 0;
4123 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
4124 if ( 0 == handle->initialized )
4126 return NULL;
4131 using namespace geos::simplify;
4132 Geometry::AutoPtr g(TopologyPreservingSimplifier::simplify(g1, tolerance));
4133 return g.release();
4135 catch (const std::exception &e)
4137 handle->ERROR_MESSAGE("%s", e.what());
4139 catch (...)
4141 handle->ERROR_MESSAGE("Unknown exception thrown");
4144 return NULL;
4148 /* WKT Reader */
4149 WKTReader *
4150 GEOSWKTReader_create_r(GEOSContextHandle_t extHandle)
4152 if ( 0 == extHandle )
4154 return NULL;
4157 GEOSContextHandleInternal_t *handle = 0;
4158 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
4159 if ( 0 == handle->initialized )
4161 return NULL;
4166 using geos::io::WKTReader;
4167 return new WKTReader((GeometryFactory*)handle->geomFactory);
4169 catch (const std::exception &e)
4171 handle->ERROR_MESSAGE("%s", e.what());
4173 catch (...)
4175 handle->ERROR_MESSAGE("Unknown exception thrown");
4178 return NULL;
4181 void
4182 GEOSWKTReader_destroy_r(GEOSContextHandle_t extHandle, WKTReader *reader)
4184 GEOSContextHandleInternal_t *handle = 0;
4188 delete reader;
4190 catch (const std::exception &e)
4192 if ( 0 == extHandle )
4194 return;
4197 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
4198 if ( 0 == handle->initialized )
4200 return;
4203 handle->ERROR_MESSAGE("%s", e.what());
4205 catch (...)
4207 if ( 0 == extHandle )
4209 return;
4212 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
4213 if ( 0 == handle->initialized )
4215 return;
4218 handle->ERROR_MESSAGE("Unknown exception thrown");
4223 Geometry*
4224 GEOSWKTReader_read_r(GEOSContextHandle_t extHandle, WKTReader *reader, const char *wkt)
4226 assert(0 != reader);
4228 if ( 0 == extHandle )
4230 return 0;
4233 GEOSContextHandleInternal_t *handle = 0;
4234 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
4235 if ( 0 == handle->initialized )
4237 return 0;
4242 const std::string wktstring(wkt);
4243 Geometry *g = reader->read(wktstring);
4244 return g;
4246 catch (const std::exception &e)
4248 handle->ERROR_MESSAGE("%s", e.what());
4250 catch (...)
4252 handle->ERROR_MESSAGE("Unknown exception thrown");
4255 return 0;
4258 /* WKT Writer */
4259 WKTWriter *
4260 GEOSWKTWriter_create_r(GEOSContextHandle_t extHandle)
4262 if ( 0 == extHandle )
4264 return 0;
4267 GEOSContextHandleInternal_t *handle = 0;
4268 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
4269 if ( 0 == handle->initialized )
4271 return 0;
4276 using geos::io::WKTWriter;
4277 return new WKTWriter();
4279 catch (const std::exception &e)
4281 handle->ERROR_MESSAGE("%s", e.what());
4283 catch (...)
4285 handle->ERROR_MESSAGE("Unknown exception thrown");
4288 return 0;
4291 void
4292 GEOSWKTWriter_destroy_r(GEOSContextHandle_t extHandle, WKTWriter *Writer)
4295 GEOSContextHandleInternal_t *handle = 0;
4299 delete Writer;
4301 catch (const std::exception &e)
4303 if ( 0 == extHandle )
4305 return;
4308 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
4309 if ( 0 == handle->initialized )
4311 return;
4314 handle->ERROR_MESSAGE("%s", e.what());
4316 catch (...)
4318 if ( 0 == extHandle )
4320 return;
4323 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
4324 if ( 0 == handle->initialized )
4326 return;
4329 handle->ERROR_MESSAGE("Unknown exception thrown");
4334 char*
4335 GEOSWKTWriter_write_r(GEOSContextHandle_t extHandle, WKTWriter *writer, const Geometry *geom)
4337 assert(0 != writer);
4339 if ( 0 == extHandle )
4341 return NULL;
4344 GEOSContextHandleInternal_t *handle = 0;
4345 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
4346 if ( 0 == handle->initialized )
4348 return NULL;
4353 std::string sgeom(writer->write(geom));
4354 char *result = gstrdup(sgeom);
4355 return result;
4357 catch (const std::exception &e)
4359 handle->ERROR_MESSAGE("%s", e.what());
4361 catch (...)
4363 handle->ERROR_MESSAGE("Unknown exception thrown");
4366 return NULL;
4369 void
4370 GEOSWKTWriter_setTrim_r(GEOSContextHandle_t extHandle, WKTWriter *writer, char trim)
4372 assert(0 != writer);
4374 if ( 0 == extHandle )
4376 return;
4379 GEOSContextHandleInternal_t *handle = 0;
4380 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
4381 if ( 0 == handle->initialized )
4383 return;
4386 writer->setTrim(0 != trim);
4389 void
4390 GEOSWKTWriter_setRoundingPrecision_r(GEOSContextHandle_t extHandle, WKTWriter *writer, int precision)
4392 assert(0 != writer);
4394 if ( 0 == extHandle )
4396 return;
4399 GEOSContextHandleInternal_t *handle = 0;
4400 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
4401 if ( 0 == handle->initialized )
4403 return;
4406 writer->setRoundingPrecision(precision);
4409 void
4410 GEOSWKTWriter_setOutputDimension_r(GEOSContextHandle_t extHandle, WKTWriter *writer, int dim)
4412 assert(0 != writer);
4414 if ( 0 == extHandle )
4416 return;
4419 GEOSContextHandleInternal_t *handle = 0;
4420 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
4421 if ( 0 == handle->initialized )
4423 return;
4428 writer->setOutputDimension(dim);
4430 catch (const std::exception &e)
4432 handle->ERROR_MESSAGE("%s", e.what());
4434 catch (...)
4436 handle->ERROR_MESSAGE("Unknown exception thrown");
4441 GEOSWKTWriter_getOutputDimension_r(GEOSContextHandle_t extHandle, WKTWriter *writer)
4443 assert(0 != writer);
4445 if ( 0 == extHandle )
4447 return -1;
4450 GEOSContextHandleInternal_t *handle = 0;
4451 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
4452 if ( 0 == handle->initialized )
4454 return -1;
4457 int dim = -1;
4461 dim = writer->getOutputDimension();
4463 catch (const std::exception &e)
4465 handle->ERROR_MESSAGE("%s", e.what());
4467 catch (...)
4469 handle->ERROR_MESSAGE("Unknown exception thrown");
4472 return dim;
4475 void
4476 GEOSWKTWriter_setOld3D_r(GEOSContextHandle_t extHandle, WKTWriter *writer, int useOld3D)
4478 assert(0 != writer);
4480 if ( 0 == extHandle )
4482 return;
4485 GEOSContextHandleInternal_t *handle = 0;
4486 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
4487 if ( 0 == handle->initialized )
4489 return;
4492 writer->setOld3D(0 != useOld3D);
4495 /* WKB Reader */
4496 WKBReader *
4497 GEOSWKBReader_create_r(GEOSContextHandle_t extHandle)
4499 if ( 0 == extHandle )
4501 return NULL;
4504 GEOSContextHandleInternal_t *handle = 0;
4505 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
4506 if ( 0 == handle->initialized )
4508 return NULL;
4511 using geos::io::WKBReader;
4514 return new WKBReader(*(GeometryFactory*)handle->geomFactory);
4516 catch (const std::exception &e)
4518 handle->ERROR_MESSAGE("%s", e.what());
4520 catch (...)
4522 handle->ERROR_MESSAGE("Unknown exception thrown");
4525 return NULL;
4528 void
4529 GEOSWKBReader_destroy_r(GEOSContextHandle_t extHandle, WKBReader *reader)
4531 GEOSContextHandleInternal_t *handle = 0;
4535 delete reader;
4537 catch (const std::exception &e)
4539 if ( 0 == extHandle )
4541 return;
4544 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
4545 if ( 0 == handle->initialized )
4547 return;
4550 handle->ERROR_MESSAGE("%s", e.what());
4552 catch (...)
4554 if ( 0 == extHandle )
4556 return;
4559 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
4560 if ( 0 == handle->initialized )
4562 return;
4565 handle->ERROR_MESSAGE("Unknown exception thrown");
4570 Geometry*
4571 GEOSWKBReader_read_r(GEOSContextHandle_t extHandle, WKBReader *reader, const unsigned char *wkb, size_t size)
4573 assert(0 != reader);
4574 assert(0 != wkb);
4576 if ( 0 == extHandle )
4578 return 0;
4581 GEOSContextHandleInternal_t *handle = 0;
4582 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
4583 if ( 0 == handle->initialized )
4585 return 0;
4590 std::string wkbstring(reinterpret_cast<const char*>(wkb), size); // make it binary !
4591 std::istringstream is(std::ios_base::binary);
4592 is.str(wkbstring);
4593 is.seekg(0, std::ios::beg); // rewind reader pointer
4595 Geometry *g = reader->read(is);
4596 return g;
4598 catch (const std::exception &e)
4600 handle->ERROR_MESSAGE("%s", e.what());
4602 catch (...)
4604 handle->ERROR_MESSAGE("Unknown exception thrown");
4607 return 0;
4610 Geometry*
4611 GEOSWKBReader_readHEX_r(GEOSContextHandle_t extHandle, WKBReader *reader, const unsigned char *hex, size_t size)
4613 assert(0 != reader);
4614 assert(0 != hex);
4616 if ( 0 == extHandle )
4618 return 0;
4621 GEOSContextHandleInternal_t *handle = 0;
4622 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
4623 if ( 0 == handle->initialized )
4625 return 0;
4630 std::string hexstring(reinterpret_cast<const char*>(hex), size);
4631 std::istringstream is(std::ios_base::binary);
4632 is.str(hexstring);
4633 is.seekg(0, std::ios::beg); // rewind reader pointer
4635 Geometry *g = reader->readHEX(is);
4636 return g;
4638 catch (const std::exception &e)
4640 handle->ERROR_MESSAGE("%s", e.what());
4642 catch (...)
4644 handle->ERROR_MESSAGE("Unknown exception thrown");
4647 return 0;
4650 /* WKB Writer */
4651 WKBWriter *
4652 GEOSWKBWriter_create_r(GEOSContextHandle_t extHandle)
4654 if ( 0 == extHandle )
4656 return NULL;
4659 GEOSContextHandleInternal_t *handle = 0;
4660 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
4661 if ( 0 == handle->initialized )
4663 return NULL;
4668 using geos::io::WKBWriter;
4669 return new WKBWriter();
4671 catch (const std::exception &e)
4673 handle->ERROR_MESSAGE("%s", e.what());
4675 catch (...)
4677 handle->ERROR_MESSAGE("Unknown exception thrown");
4680 return NULL;
4683 void
4684 GEOSWKBWriter_destroy_r(GEOSContextHandle_t extHandle, WKBWriter *Writer)
4686 GEOSContextHandleInternal_t *handle = 0;
4690 delete Writer;
4692 catch (const std::exception &e)
4694 if ( 0 == extHandle )
4696 return;
4699 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
4700 if ( 0 == handle->initialized )
4702 return;
4705 handle->ERROR_MESSAGE("%s", e.what());
4707 catch (...)
4709 if ( 0 == extHandle )
4711 return;
4714 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
4715 if ( 0 == handle->initialized )
4717 return;
4720 handle->ERROR_MESSAGE("Unknown exception thrown");
4725 /* The caller owns the result */
4726 unsigned char*
4727 GEOSWKBWriter_write_r(GEOSContextHandle_t extHandle, WKBWriter *writer, const Geometry *geom, size_t *size)
4729 assert(0 != writer);
4730 assert(0 != geom);
4731 assert(0 != size);
4733 if ( 0 == extHandle )
4735 return NULL;
4738 GEOSContextHandleInternal_t *handle = 0;
4739 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
4740 if ( 0 == handle->initialized )
4742 return NULL;
4747 std::ostringstream os(std::ios_base::binary);
4748 writer->write(*geom, os);
4749 std::string wkbstring(os.str());
4750 const std::size_t len = wkbstring.length();
4752 unsigned char *result = NULL;
4753 result = (unsigned char*) std::malloc(len);
4754 std::memcpy(result, wkbstring.c_str(), len);
4755 *size = len;
4756 return result;
4758 catch (const std::exception &e)
4760 handle->ERROR_MESSAGE("%s", e.what());
4762 catch (...)
4764 handle->ERROR_MESSAGE("Unknown exception thrown");
4766 return NULL;
4769 /* The caller owns the result */
4770 unsigned char*
4771 GEOSWKBWriter_writeHEX_r(GEOSContextHandle_t extHandle, WKBWriter *writer, const Geometry *geom, size_t *size)
4773 assert(0 != writer);
4774 assert(0 != geom);
4775 assert(0 != size);
4777 if ( 0 == extHandle )
4779 return NULL;
4782 GEOSContextHandleInternal_t *handle = 0;
4783 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
4784 if ( 0 == handle->initialized )
4786 return NULL;
4791 std::ostringstream os(std::ios_base::binary);
4792 writer->writeHEX(*geom, os);
4793 std::string wkbstring(os.str());
4794 const std::size_t len = wkbstring.length();
4796 unsigned char *result = NULL;
4797 result = (unsigned char*) std::malloc(len);
4798 std::memcpy(result, wkbstring.c_str(), len);
4799 *size = len;
4800 return result;
4802 catch (const std::exception &e)
4804 handle->ERROR_MESSAGE("%s", e.what());
4806 catch (...)
4808 handle->ERROR_MESSAGE("Unknown exception thrown");
4811 return NULL;
4815 GEOSWKBWriter_getOutputDimension_r(GEOSContextHandle_t extHandle, const GEOSWKBWriter* writer)
4817 assert(0 != writer);
4819 if ( 0 == extHandle )
4821 return 0;
4824 int ret = 0;
4826 GEOSContextHandleInternal_t *handle = 0;
4827 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
4828 if ( 0 != handle->initialized )
4832 ret = writer->getOutputDimension();
4834 catch (...)
4836 handle->ERROR_MESSAGE("Unknown exception thrown");
4840 return ret;
4843 void
4844 GEOSWKBWriter_setOutputDimension_r(GEOSContextHandle_t extHandle, GEOSWKBWriter* writer, int newDimension)
4846 assert(0 != writer);
4848 if ( 0 == extHandle )
4850 return;
4853 GEOSContextHandleInternal_t *handle = 0;
4854 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
4855 if ( 0 != handle->initialized )
4859 writer->setOutputDimension(newDimension);
4861 catch (const std::exception &e)
4863 handle->ERROR_MESSAGE("%s", e.what());
4865 catch (...)
4867 handle->ERROR_MESSAGE("Unknown exception thrown");
4873 GEOSWKBWriter_getByteOrder_r(GEOSContextHandle_t extHandle, const GEOSWKBWriter* writer)
4875 assert(0 != writer);
4877 if ( 0 == extHandle )
4879 return 0;
4882 int ret = 0;
4884 GEOSContextHandleInternal_t *handle = 0;
4885 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
4886 if ( 0 != handle->initialized )
4890 ret = writer->getByteOrder();
4893 catch (...)
4895 handle->ERROR_MESSAGE("Unknown exception thrown");
4899 return ret;
4902 void
4903 GEOSWKBWriter_setByteOrder_r(GEOSContextHandle_t extHandle, GEOSWKBWriter* writer, int newByteOrder)
4905 assert(0 != writer);
4907 if ( 0 == extHandle )
4909 return;
4912 GEOSContextHandleInternal_t *handle = 0;
4913 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
4914 if ( 0 != handle->initialized )
4918 writer->setByteOrder(newByteOrder);
4920 catch (const std::exception &e)
4922 handle->ERROR_MESSAGE("%s", e.what());
4924 catch (...)
4926 handle->ERROR_MESSAGE("Unknown exception thrown");
4931 char
4932 GEOSWKBWriter_getIncludeSRID_r(GEOSContextHandle_t extHandle, const GEOSWKBWriter* writer)
4934 assert(0 != writer);
4936 if ( 0 == extHandle )
4938 return -1;
4941 int ret = -1;
4943 GEOSContextHandleInternal_t *handle = 0;
4944 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
4945 if ( 0 != handle->initialized )
4949 int srid = writer->getIncludeSRID();
4950 ret = srid;
4952 catch (...)
4954 handle->ERROR_MESSAGE("Unknown exception thrown");
4958 return static_cast<char>(ret);
4961 void
4962 GEOSWKBWriter_setIncludeSRID_r(GEOSContextHandle_t extHandle, GEOSWKBWriter* writer, const char newIncludeSRID)
4964 assert(0 != writer);
4966 if ( 0 == extHandle )
4968 return;
4971 GEOSContextHandleInternal_t *handle = 0;
4972 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
4973 if ( 0 != handle->initialized )
4977 writer->setIncludeSRID(newIncludeSRID);
4979 catch (...)
4981 handle->ERROR_MESSAGE("Unknown exception thrown");
4987 //-----------------------------------------------------------------
4988 // Prepared Geometry
4989 //-----------------------------------------------------------------
4991 const geos::geom::prep::PreparedGeometry*
4992 GEOSPrepare_r(GEOSContextHandle_t extHandle, const Geometry *g)
4994 if ( 0 == extHandle )
4996 return 0;
4999 GEOSContextHandleInternal_t *handle = 0;
5000 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
5001 if ( 0 == handle->initialized )
5003 return 0;
5006 const geos::geom::prep::PreparedGeometry* prep = 0;
5010 prep = geos::geom::prep::PreparedGeometryFactory::prepare(g);
5012 catch (const std::exception &e)
5014 handle->ERROR_MESSAGE("%s", e.what());
5016 catch (...)
5018 handle->ERROR_MESSAGE("Unknown exception thrown");
5021 return prep;
5024 void
5025 GEOSPreparedGeom_destroy_r(GEOSContextHandle_t extHandle, const geos::geom::prep::PreparedGeometry *a)
5027 GEOSContextHandleInternal_t *handle = 0;
5031 delete a;
5033 catch (const std::exception &e)
5035 if ( 0 == extHandle )
5037 return;
5040 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
5041 if ( 0 == handle->initialized )
5043 return;
5046 handle->ERROR_MESSAGE("%s", e.what());
5048 catch (...)
5050 if ( 0 == extHandle )
5052 return;
5055 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
5056 if ( 0 == handle->initialized )
5058 return;
5061 handle->ERROR_MESSAGE("Unknown exception thrown");
5065 char
5066 GEOSPreparedContains_r(GEOSContextHandle_t extHandle,
5067 const geos::geom::prep::PreparedGeometry *pg, const Geometry *g)
5069 assert(0 != pg);
5070 assert(0 != g);
5072 if ( 0 == extHandle )
5074 return 2;
5077 GEOSContextHandleInternal_t *handle = 0;
5078 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
5079 if ( 0 == handle->initialized )
5081 return 2;
5084 try
5086 bool result = pg->contains(g);
5087 return result;
5089 catch (const std::exception &e)
5091 handle->ERROR_MESSAGE("%s", e.what());
5093 catch (...)
5095 handle->ERROR_MESSAGE("Unknown exception thrown");
5098 return 2;
5101 char
5102 GEOSPreparedContainsProperly_r(GEOSContextHandle_t extHandle,
5103 const geos::geom::prep::PreparedGeometry *pg, const Geometry *g)
5105 assert(0 != pg);
5106 assert(0 != g);
5108 if ( 0 == extHandle )
5110 return 2;
5113 GEOSContextHandleInternal_t *handle = 0;
5114 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
5115 if ( 0 == handle->initialized )
5117 return 2;
5120 try
5122 bool result = pg->containsProperly(g);
5123 return result;
5125 catch (const std::exception &e)
5127 handle->ERROR_MESSAGE("%s", e.what());
5129 catch (...)
5131 handle->ERROR_MESSAGE("Unknown exception thrown");
5134 return 2;
5137 char
5138 GEOSPreparedCoveredBy_r(GEOSContextHandle_t extHandle,
5139 const geos::geom::prep::PreparedGeometry *pg, const Geometry *g)
5141 assert(0 != pg);
5142 assert(0 != g);
5144 if ( 0 == extHandle )
5146 return 2;
5149 GEOSContextHandleInternal_t *handle = 0;
5150 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
5151 if ( 0 == handle->initialized )
5153 return 2;
5156 try
5158 bool result = pg->coveredBy(g);
5159 return result;
5161 catch (const std::exception &e)
5163 handle->ERROR_MESSAGE("%s", e.what());
5165 catch (...)
5167 handle->ERROR_MESSAGE("Unknown exception thrown");
5170 return 2;
5173 char
5174 GEOSPreparedCovers_r(GEOSContextHandle_t extHandle,
5175 const geos::geom::prep::PreparedGeometry *pg, const Geometry *g)
5177 assert(0 != pg);
5178 assert(0 != g);
5180 if ( 0 == extHandle )
5182 return 2;
5185 GEOSContextHandleInternal_t *handle = 0;
5186 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
5187 if ( 0 == handle->initialized )
5189 return 2;
5192 try
5194 bool result = pg->covers(g);
5195 return result;
5197 catch (const std::exception &e)
5199 handle->ERROR_MESSAGE("%s", e.what());
5201 catch (...)
5203 handle->ERROR_MESSAGE("Unknown exception thrown");
5206 return 2;
5209 char
5210 GEOSPreparedCrosses_r(GEOSContextHandle_t extHandle,
5211 const geos::geom::prep::PreparedGeometry *pg, const Geometry *g)
5213 assert(0 != pg);
5214 assert(0 != g);
5216 if ( 0 == extHandle )
5218 return 2;
5221 GEOSContextHandleInternal_t *handle = 0;
5222 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
5223 if ( 0 == handle->initialized )
5225 return 2;
5228 try
5230 bool result = pg->crosses(g);
5231 return result;
5233 catch (const std::exception &e)
5235 handle->ERROR_MESSAGE("%s", e.what());
5237 catch (...)
5239 handle->ERROR_MESSAGE("Unknown exception thrown");
5242 return 2;
5245 char
5246 GEOSPreparedDisjoint_r(GEOSContextHandle_t extHandle,
5247 const geos::geom::prep::PreparedGeometry *pg, const Geometry *g)
5249 assert(0 != pg);
5250 assert(0 != g);
5252 if ( 0 == extHandle )
5254 return 2;
5257 GEOSContextHandleInternal_t *handle = 0;
5258 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
5259 if ( 0 == handle->initialized )
5261 return 2;
5264 try
5266 bool result = pg->disjoint(g);
5267 return result;
5269 catch (const std::exception &e)
5271 handle->ERROR_MESSAGE("%s", e.what());
5273 catch (...)
5275 handle->ERROR_MESSAGE("Unknown exception thrown");
5278 return 2;
5281 char
5282 GEOSPreparedIntersects_r(GEOSContextHandle_t extHandle,
5283 const geos::geom::prep::PreparedGeometry *pg, const Geometry *g)
5285 assert(0 != pg);
5286 assert(0 != g);
5288 if ( 0 == extHandle )
5290 return 2;
5293 GEOSContextHandleInternal_t *handle = 0;
5294 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
5295 if ( 0 == handle->initialized )
5297 return 2;
5300 try
5302 bool result = pg->intersects(g);
5303 return result;
5305 catch (const std::exception &e)
5307 handle->ERROR_MESSAGE("%s", e.what());
5309 catch (...)
5311 handle->ERROR_MESSAGE("Unknown exception thrown");
5314 return 2;
5317 char
5318 GEOSPreparedOverlaps_r(GEOSContextHandle_t extHandle,
5319 const geos::geom::prep::PreparedGeometry *pg, const Geometry *g)
5321 assert(0 != pg);
5322 assert(0 != g);
5324 if ( 0 == extHandle )
5326 return 2;
5329 GEOSContextHandleInternal_t *handle = 0;
5330 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
5331 if ( 0 == handle->initialized )
5333 return 2;
5336 try
5338 bool result = pg->overlaps(g);
5339 return result;
5341 catch (const std::exception &e)
5343 handle->ERROR_MESSAGE("%s", e.what());
5345 catch (...)
5347 handle->ERROR_MESSAGE("Unknown exception thrown");
5350 return 2;
5353 char
5354 GEOSPreparedTouches_r(GEOSContextHandle_t extHandle,
5355 const geos::geom::prep::PreparedGeometry *pg, const Geometry *g)
5357 assert(0 != pg);
5358 assert(0 != g);
5360 if ( 0 == extHandle )
5362 return 2;
5365 GEOSContextHandleInternal_t *handle = 0;
5366 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
5367 if ( 0 == handle->initialized )
5369 return 2;
5372 try
5374 bool result = pg->touches(g);
5375 return result;
5377 catch (const std::exception &e)
5379 handle->ERROR_MESSAGE("%s", e.what());
5381 catch (...)
5383 handle->ERROR_MESSAGE("Unknown exception thrown");
5386 return 2;
5389 char
5390 GEOSPreparedWithin_r(GEOSContextHandle_t extHandle,
5391 const geos::geom::prep::PreparedGeometry *pg, const Geometry *g)
5393 assert(0 != pg);
5394 assert(0 != g);
5396 if ( 0 == extHandle )
5398 return 2;
5401 GEOSContextHandleInternal_t *handle = 0;
5402 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
5403 if ( 0 == handle->initialized )
5405 return 2;
5408 try
5410 bool result = pg->within(g);
5411 return result;
5413 catch (const std::exception &e)
5415 handle->ERROR_MESSAGE("%s", e.what());
5417 catch (...)
5419 handle->ERROR_MESSAGE("Unknown exception thrown");
5422 return 2;
5425 //-----------------------------------------------------------------
5426 // STRtree
5427 //-----------------------------------------------------------------
5429 geos::index::strtree::STRtree *
5430 GEOSSTRtree_create_r(GEOSContextHandle_t extHandle,
5431 size_t nodeCapacity)
5433 if ( 0 == extHandle )
5435 return 0;
5438 GEOSContextHandleInternal_t *handle = 0;
5439 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
5440 if ( 0 == handle->initialized )
5442 return 0;
5445 geos::index::strtree::STRtree *tree = 0;
5449 tree = new geos::index::strtree::STRtree(nodeCapacity);
5451 catch (const std::exception &e)
5453 handle->ERROR_MESSAGE("%s", e.what());
5455 catch (...)
5457 handle->ERROR_MESSAGE("Unknown exception thrown");
5460 return tree;
5463 void
5464 GEOSSTRtree_insert_r(GEOSContextHandle_t extHandle,
5465 geos::index::strtree::STRtree *tree,
5466 const geos::geom::Geometry *g,
5467 void *item)
5469 GEOSContextHandleInternal_t *handle = 0;
5470 assert(tree != 0);
5471 assert(g != 0);
5475 tree->insert(g->getEnvelopeInternal(), item);
5477 catch (const std::exception &e)
5479 if ( 0 == extHandle )
5481 return;
5484 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
5485 if ( 0 == handle->initialized )
5487 return;
5490 handle->ERROR_MESSAGE("%s", e.what());
5492 catch (...)
5494 if ( 0 == extHandle )
5496 return;
5499 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
5500 if ( 0 == handle->initialized )
5502 return;
5505 handle->ERROR_MESSAGE("Unknown exception thrown");
5509 void
5510 GEOSSTRtree_query_r(GEOSContextHandle_t extHandle,
5511 geos::index::strtree::STRtree *tree,
5512 const geos::geom::Geometry *g,
5513 GEOSQueryCallback callback,
5514 void *userdata)
5516 GEOSContextHandleInternal_t *handle = 0;
5517 assert(tree != 0);
5518 assert(g != 0);
5519 assert(callback != 0);
5523 CAPI_ItemVisitor visitor(callback, userdata);
5524 tree->query(g->getEnvelopeInternal(), visitor);
5526 catch (const std::exception &e)
5528 if ( 0 == extHandle )
5530 return;
5533 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
5534 if ( 0 == handle->initialized )
5536 return;
5539 handle->ERROR_MESSAGE("%s", e.what());
5541 catch (...)
5543 if ( 0 == extHandle )
5545 return;
5548 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
5549 if ( 0 == handle->initialized )
5551 return;
5554 handle->ERROR_MESSAGE("Unknown exception thrown");
5558 void
5559 GEOSSTRtree_iterate_r(GEOSContextHandle_t extHandle,
5560 geos::index::strtree::STRtree *tree,
5561 GEOSQueryCallback callback,
5562 void *userdata)
5564 GEOSContextHandleInternal_t *handle = 0;
5565 assert(tree != 0);
5566 assert(callback != 0);
5570 CAPI_ItemVisitor visitor(callback, userdata);
5571 tree->iterate(visitor);
5573 catch (const std::exception &e)
5575 if ( 0 == extHandle )
5577 return;
5580 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
5581 if ( 0 == handle->initialized )
5583 return;
5586 handle->ERROR_MESSAGE("%s", e.what());
5588 catch (...)
5590 if ( 0 == extHandle )
5592 return;
5595 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
5596 if ( 0 == handle->initialized )
5598 return;
5601 handle->ERROR_MESSAGE("Unknown exception thrown");
5605 char
5606 GEOSSTRtree_remove_r(GEOSContextHandle_t extHandle,
5607 geos::index::strtree::STRtree *tree,
5608 const geos::geom::Geometry *g,
5609 void *item)
5611 assert(0 != tree);
5612 assert(0 != g);
5614 if ( 0 == extHandle )
5616 return 2;
5619 GEOSContextHandleInternal_t *handle = 0;
5620 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
5621 if ( 0 == handle->initialized )
5623 return 2;
5626 try
5628 bool result = tree->remove(g->getEnvelopeInternal(), item);
5629 return result;
5631 catch (const std::exception &e)
5633 handle->ERROR_MESSAGE("%s", e.what());
5635 catch (...)
5637 handle->ERROR_MESSAGE("Unknown exception thrown");
5640 return 2;
5643 void
5644 GEOSSTRtree_destroy_r(GEOSContextHandle_t extHandle,
5645 geos::index::strtree::STRtree *tree)
5647 GEOSContextHandleInternal_t *handle = 0;
5651 delete tree;
5653 catch (const std::exception &e)
5655 if ( 0 == extHandle )
5657 return;
5660 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
5661 if ( 0 == handle->initialized )
5663 return;
5666 handle->ERROR_MESSAGE("%s", e.what());
5668 catch (...)
5670 if ( 0 == extHandle )
5672 return;
5675 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
5676 if ( 0 == handle->initialized )
5678 return;
5681 handle->ERROR_MESSAGE("Unknown exception thrown");
5685 double
5686 GEOSProject_r(GEOSContextHandle_t extHandle,
5687 const Geometry *g,
5688 const Geometry *p)
5690 if ( 0 == extHandle ) return -1.0;
5691 GEOSContextHandleInternal_t *handle =
5692 reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
5693 if ( handle->initialized == 0 ) return -1.0;
5695 const geos::geom::Point* point = dynamic_cast<const geos::geom::Point*>(p);
5696 if (!point) {
5697 handle->ERROR_MESSAGE("third argument of GEOSProject_r must be Point*");
5698 return -1.0;
5701 const geos::geom::Coordinate* inputPt = p->getCoordinate();
5703 try {
5704 return geos::linearref::LengthIndexedLine(g).project(*inputPt);
5705 } catch (const std::exception &e) {
5706 handle->ERROR_MESSAGE("%s", e.what());
5707 return -1.0;
5708 } catch (...) {
5709 handle->ERROR_MESSAGE("Unknown exception thrown");
5710 return -1.0;
5715 Geometry*
5716 GEOSInterpolate_r(GEOSContextHandle_t extHandle, const Geometry *g, double d)
5718 if ( 0 == extHandle ) return 0;
5719 GEOSContextHandleInternal_t *handle =
5720 reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
5721 if ( handle->initialized == 0 ) return 0;
5723 try {
5724 geos::linearref::LengthIndexedLine lil(g);
5725 geos::geom::Coordinate coord = lil.extractPoint(d);
5726 const GeometryFactory *gf = handle->geomFactory;
5727 Geometry* point = gf->createPoint(coord);
5728 return point;
5729 } catch (const std::exception &e) {
5730 handle->ERROR_MESSAGE("%s", e.what());
5731 return 0;
5732 } catch (...) {
5733 handle->ERROR_MESSAGE("Unknown exception thrown");
5734 return 0;
5739 double
5740 GEOSProjectNormalized_r(GEOSContextHandle_t extHandle, const Geometry *g,
5741 const Geometry *p)
5744 double length;
5745 GEOSLength_r(extHandle, g, &length);
5746 return GEOSProject_r(extHandle, g, p) / length;
5750 Geometry*
5751 GEOSInterpolateNormalized_r(GEOSContextHandle_t extHandle, const Geometry *g,
5752 double d)
5754 double length;
5755 GEOSLength_r(extHandle, g, &length);
5756 return GEOSInterpolate_r(extHandle, g, d * length);
5759 GEOSGeometry*
5760 GEOSGeom_extractUniquePoints_r(GEOSContextHandle_t extHandle,
5761 const GEOSGeometry* g)
5763 if ( 0 == extHandle ) return 0;
5764 GEOSContextHandleInternal_t *handle = 0;
5765 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
5766 if ( handle->initialized == 0 ) return 0;
5768 using namespace geos::geom;
5769 using namespace geos::util;
5774 /* 1: extract points */
5775 std::vector<const Coordinate*> coords;
5776 UniqueCoordinateArrayFilter filter(coords);
5777 g->apply_ro(&filter);
5779 /* 2: for each point, create a geometry and put into a vector */
5780 std::vector<Geometry*>* points = new std::vector<Geometry*>();
5781 points->reserve(coords.size());
5782 const GeometryFactory* factory = g->getFactory();
5783 for (std::vector<const Coordinate*>::iterator it=coords.begin(),
5784 itE=coords.end();
5785 it != itE; ++it)
5787 Geometry* point = factory->createPoint(*(*it));
5788 points->push_back(point);
5791 /* 3: create a multipoint */
5792 return factory->createMultiPoint(points);
5795 catch (const std::exception &e)
5797 handle->ERROR_MESSAGE("%s", e.what());
5798 return 0;
5800 catch (...)
5802 handle->ERROR_MESSAGE("Unknown exception thrown");
5803 return 0;
5807 int GEOSOrientationIndex_r(GEOSContextHandle_t extHandle,
5808 double Ax, double Ay, double Bx, double By, double Px, double Py)
5810 GEOSContextHandleInternal_t *handle = 0;
5812 using geos::geom::Coordinate;
5813 using geos::algorithm::CGAlgorithms;
5815 if ( 0 == extHandle )
5817 return 2;
5820 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
5821 if ( 0 == handle->initialized )
5823 return 2;
5828 Coordinate A(Ax, Ay);
5829 Coordinate B(Bx, By);
5830 Coordinate P(Px, Py);
5831 return CGAlgorithms::orientationIndex(A, B, P);
5833 catch (const std::exception &e)
5835 handle->ERROR_MESSAGE("%s", e.what());
5836 return 2;
5838 catch (...)
5840 handle->ERROR_MESSAGE("Unknown exception thrown");
5841 return 2;
5845 GEOSGeometry *
5846 GEOSSharedPaths_r(GEOSContextHandle_t extHandle, const GEOSGeometry* g1, const GEOSGeometry* g2)
5848 using namespace geos::operation::sharedpaths;
5850 if ( 0 == extHandle ) return 0;
5851 GEOSContextHandleInternal_t *handle =
5852 reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
5853 if ( handle->initialized == 0 ) return 0;
5855 SharedPathsOp::PathList forw, back;
5856 try {
5857 SharedPathsOp::sharedPathsOp(*g1, *g2, forw, back);
5859 catch (const std::exception &e)
5861 SharedPathsOp::clearEdges(forw);
5862 SharedPathsOp::clearEdges(back);
5863 handle->ERROR_MESSAGE("%s", e.what());
5864 return 0;
5866 catch (...)
5868 SharedPathsOp::clearEdges(forw);
5869 SharedPathsOp::clearEdges(back);
5870 handle->ERROR_MESSAGE("Unknown exception thrown");
5871 return 0;
5874 // Now forw and back have the geoms we want to use to construct
5875 // our output GeometryCollections...
5877 const GeometryFactory* factory = g1->getFactory();
5878 size_t count;
5880 std::auto_ptr< std::vector<Geometry*> > out1(
5881 new std::vector<Geometry*>()
5883 count = forw.size();
5884 out1->reserve(count);
5885 for (size_t i=0; i<count; ++i) {
5886 out1->push_back(forw[i]);
5888 std::auto_ptr<Geometry> out1g (
5889 factory->createMultiLineString(out1.release())
5892 std::auto_ptr< std::vector<Geometry*> > out2(
5893 new std::vector<Geometry*>()
5895 count = back.size();
5896 out2->reserve(count);
5897 for (size_t i=0; i<count; ++i) {
5898 out2->push_back(back[i]);
5900 std::auto_ptr<Geometry> out2g (
5901 factory->createMultiLineString(out2.release())
5904 std::auto_ptr< std::vector<Geometry*> > out(
5905 new std::vector<Geometry*>()
5907 out->reserve(2);
5908 out->push_back(out1g.release());
5909 out->push_back(out2g.release());
5911 std::auto_ptr<Geometry> outg (
5912 factory->createGeometryCollection(out.release())
5915 return outg.release();
5919 GEOSGeometry *
5920 GEOSSnap_r(GEOSContextHandle_t extHandle, const GEOSGeometry* g1,
5921 const GEOSGeometry* g2, double tolerance)
5923 using namespace geos::operation::overlay::snap;
5925 if ( 0 == extHandle ) return 0;
5926 GEOSContextHandleInternal_t *handle =
5927 reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
5928 if ( handle->initialized == 0 ) return 0;
5930 try{
5931 GeometrySnapper snapper( *g1 );
5932 std::auto_ptr<Geometry> ret = snapper.snapTo(*g2, tolerance);
5933 return ret.release();
5935 catch (const std::exception &e)
5937 handle->ERROR_MESSAGE("%s", e.what());
5938 return 0;
5940 catch (...)
5942 handle->ERROR_MESSAGE("Unknown exception thrown");
5943 return 0;
5947 BufferParameters *
5948 GEOSBufferParams_create_r(GEOSContextHandle_t extHandle)
5950 if ( 0 == extHandle ) return NULL;
5952 GEOSContextHandleInternal_t *handle = 0;
5953 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
5954 if ( 0 == handle->initialized ) return NULL;
5958 BufferParameters *p = new BufferParameters();
5959 return p;
5961 catch (const std::exception &e)
5963 handle->ERROR_MESSAGE("%s", e.what());
5965 catch (...)
5967 handle->ERROR_MESSAGE("Unknown exception thrown");
5970 return 0;
5973 void
5974 GEOSBufferParams_destroy_r(GEOSContextHandle_t extHandle, BufferParameters* p)
5976 (void)extHandle;
5977 delete p;
5981 GEOSBufferParams_setEndCapStyle_r(GEOSContextHandle_t extHandle,
5982 GEOSBufferParams* p, int style)
5984 if ( 0 == extHandle ) return 0;
5986 GEOSContextHandleInternal_t *handle = 0;
5987 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
5988 if ( 0 == handle->initialized ) return 0;
5992 if ( style > BufferParameters::CAP_SQUARE )
5994 throw IllegalArgumentException("Invalid buffer endCap style");
5996 p->setEndCapStyle(static_cast<BufferParameters::EndCapStyle>(style));
5997 return 1;
5999 catch (const std::exception &e)
6001 handle->ERROR_MESSAGE("%s", e.what());
6003 catch (...)
6005 handle->ERROR_MESSAGE("Unknown exception thrown");
6008 return 0;
6012 GEOSBufferParams_setJoinStyle_r(GEOSContextHandle_t extHandle,
6013 GEOSBufferParams* p, int style)
6015 if ( 0 == extHandle ) return 0;
6017 GEOSContextHandleInternal_t *handle = 0;
6018 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
6019 if ( 0 == handle->initialized ) return 0;
6023 if ( style > BufferParameters::JOIN_BEVEL ) {
6024 throw IllegalArgumentException("Invalid buffer join style");
6026 p->setJoinStyle(static_cast<BufferParameters::JoinStyle>(style));
6027 return 1;
6029 catch (const std::exception &e)
6031 handle->ERROR_MESSAGE("%s", e.what());
6033 catch (...)
6035 handle->ERROR_MESSAGE("Unknown exception thrown");
6038 return 0;
6042 GEOSBufferParams_setMitreLimit_r(GEOSContextHandle_t extHandle,
6043 GEOSBufferParams* p, double limit)
6045 if ( 0 == extHandle ) return 0;
6047 GEOSContextHandleInternal_t *handle = 0;
6048 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
6049 if ( 0 == handle->initialized ) return 0;
6053 p->setMitreLimit(limit);
6054 return 1;
6056 catch (const std::exception &e)
6058 handle->ERROR_MESSAGE("%s", e.what());
6060 catch (...)
6062 handle->ERROR_MESSAGE("Unknown exception thrown");
6065 return 0;
6069 GEOSBufferParams_setQuadrantSegments_r(GEOSContextHandle_t extHandle,
6070 GEOSBufferParams* p, int segs)
6072 if ( 0 == extHandle ) return 0;
6074 GEOSContextHandleInternal_t *handle = 0;
6075 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
6076 if ( 0 == handle->initialized ) return 0;
6080 p->setQuadrantSegments(segs);
6081 return 1;
6083 catch (const std::exception &e)
6085 handle->ERROR_MESSAGE("%s", e.what());
6087 catch (...)
6089 handle->ERROR_MESSAGE("Unknown exception thrown");
6092 return 0;
6096 GEOSBufferParams_setSingleSided_r(GEOSContextHandle_t extHandle,
6097 GEOSBufferParams* p, int ss)
6099 if ( 0 == extHandle ) return 0;
6101 GEOSContextHandleInternal_t *handle = 0;
6102 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
6103 if ( 0 == handle->initialized ) return 0;
6107 p->setSingleSided( (ss != 0) );
6108 return 1;
6110 catch (const std::exception &e)
6112 handle->ERROR_MESSAGE("%s", e.what());
6114 catch (...)
6116 handle->ERROR_MESSAGE("Unknown exception thrown");
6119 return 0;
6122 Geometry *
6123 GEOSBufferWithParams_r(GEOSContextHandle_t extHandle, const Geometry *g1, const BufferParameters* bp, double width)
6125 using geos::operation::buffer::BufferOp;
6127 if ( 0 == extHandle ) return NULL;
6129 GEOSContextHandleInternal_t *handle = 0;
6130 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
6131 if ( 0 == handle->initialized ) return NULL;
6135 BufferOp op(g1, *bp);
6136 Geometry *g3 = op.getResultGeometry(width);
6137 return g3;
6139 catch (const std::exception &e)
6141 handle->ERROR_MESSAGE("%s", e.what());
6143 catch (...)
6145 handle->ERROR_MESSAGE("Unknown exception thrown");
6148 return NULL;
6151 } /* extern "C" */