Quotes around otherwise ambiguous (underline containing) name
[geos.git] / capi / geos_ts_c.cpp
blob1c522375765938bff77d0359f33a3f5dc5f20b39
1 /************************************************************************
4 * C-Wrapper for GEOS library
6 * Copyright (C) 2010-2012 Sandro Santilli <strk@kbt.io>
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@kbt.io>
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/strtree/GeometryItemDistance.h>
39 #include <geos/index/ItemVisitor.h>
40 #include <geos/io/WKTReader.h>
41 #include <geos/io/WKBReader.h>
42 #include <geos/io/WKTWriter.h>
43 #include <geos/io/WKBWriter.h>
44 #include <geos/algorithm/distance/DiscreteHausdorffDistance.h>
45 #include <geos/algorithm/distance/DiscreteFrechetDistance.h>
46 #include <geos/algorithm/CGAlgorithms.h>
47 #include <geos/algorithm/BoundaryNodeRule.h>
48 #include <geos/algorithm/MinimumDiameter.h>
49 #include <geos/simplify/DouglasPeuckerSimplifier.h>
50 #include <geos/simplify/TopologyPreservingSimplifier.h>
51 #include <geos/noding/GeometryNoder.h>
52 #include <geos/noding/Noder.h>
53 #include <geos/operation/buffer/BufferBuilder.h>
54 #include <geos/operation/buffer/BufferOp.h>
55 #include <geos/operation/buffer/BufferParameters.h>
56 #include <geos/operation/distance/DistanceOp.h>
57 #include <geos/operation/distance/IndexedFacetDistance.h>
58 #include <geos/operation/linemerge/LineMerger.h>
59 #include <geos/operation/overlay/OverlayOp.h>
60 #include <geos/operation/overlay/snap/GeometrySnapper.h>
61 #include <geos/operation/intersection/Rectangle.h>
62 #include <geos/operation/intersection/RectangleIntersection.h>
63 #include <geos/operation/polygonize/Polygonizer.h>
64 #include <geos/operation/relate/RelateOp.h>
65 #include <geos/operation/sharedpaths/SharedPathsOp.h>
66 #include <geos/operation/union/CascadedPolygonUnion.h>
67 #include <geos/operation/valid/IsValidOp.h>
68 #include <geos/precision/GeometryPrecisionReducer.h>
69 #include <geos/linearref/LengthIndexedLine.h>
70 #include <geos/triangulate/DelaunayTriangulationBuilder.h>
71 #include <geos/triangulate/VoronoiDiagramBuilder.h>
72 #include <geos/util/IllegalArgumentException.h>
73 #include <geos/util/Interrupt.h>
74 #include <geos/util/UniqueCoordinateArrayFilter.h>
75 #include <geos/util/Machine.h>
76 #include <geos/version.h>
78 // This should go away
79 #include <cmath> // finite
80 #include <cstdarg>
81 #include <cstddef>
82 #include <cstdio>
83 #include <cstdlib>
84 #include <cstring>
85 #include <fstream>
86 #include <iostream>
87 #include <sstream>
88 #include <string>
89 #include <memory>
91 #ifdef _MSC_VER
92 #pragma warning(disable : 4099)
93 #endif
95 // Some extra magic to make type declarations in geos_c.h work -
96 // for cross-checking of types in header.
97 #define GEOSGeometry geos::geom::Geometry
98 #define GEOSPreparedGeometry geos::geom::prep::PreparedGeometry
99 #define GEOSCoordSequence geos::geom::CoordinateSequence
100 #define GEOSBufferParams geos::operation::buffer::BufferParameters
101 #define GEOSSTRtree geos::index::strtree::STRtree
102 #define GEOSWKTReader_t geos::io::WKTReader
103 #define GEOSWKTWriter_t geos::io::WKTWriter
104 #define GEOSWKBReader_t geos::io::WKBReader
105 #define GEOSWKBWriter_t geos::io::WKBWriter
107 #include "geos_c.h"
108 #include "../geos_revision.h"
110 // Intentional, to allow non-standard C elements like C99 functions to be
111 // imported through C++ headers of C library, like <cmath>.
112 using namespace std;
114 /// Define this if you want operations triggering Exceptions to
115 /// be printed.
116 /// (will use the NOTIFY channel - only implemented for GEOSUnion so far)
118 #undef VERBOSE_EXCEPTIONS
120 #include <geos/export.h>
121 #include <geos/precision/MinimumClearance.h>
124 // import the most frequently used definitions globally
125 using geos::geom::Geometry;
126 using geos::geom::LineString;
127 using geos::geom::Polygon;
128 using geos::geom::CoordinateSequence;
129 using geos::geom::GeometryFactory;
131 using geos::io::WKTReader;
132 using geos::io::WKTWriter;
133 using geos::io::WKBReader;
134 using geos::io::WKBWriter;
136 using geos::operation::overlay::OverlayOp;
137 using geos::operation::overlay::overlayOp;
138 using geos::operation::geounion::CascadedPolygonUnion;
139 using geos::operation::distance::IndexedFacetDistance;
140 using geos::operation::buffer::BufferParameters;
141 using geos::operation::buffer::BufferBuilder;
142 using geos::precision::GeometryPrecisionReducer;
143 using geos::util::IllegalArgumentException;
144 using geos::algorithm::distance::DiscreteHausdorffDistance;
145 using geos::algorithm::distance::DiscreteFrechetDistance;
147 typedef std::auto_ptr<Geometry> GeomAutoPtr;
149 typedef struct GEOSContextHandle_HS
151 const GeometryFactory *geomFactory;
152 char msgBuffer[1024];
153 GEOSMessageHandler noticeMessageOld;
154 GEOSMessageHandler_r noticeMessageNew;
155 void *noticeData;
156 GEOSMessageHandler errorMessageOld;
157 GEOSMessageHandler_r errorMessageNew;
158 void *errorData;
159 int WKBOutputDims;
160 int WKBByteOrder;
161 int initialized;
163 GEOSContextHandle_HS()
165 geomFactory(0),
166 noticeMessageOld(0),
167 noticeMessageNew(0),
168 noticeData(0),
169 errorMessageOld(0),
170 errorMessageNew(0),
171 errorData(0)
173 memset(msgBuffer, 0, sizeof(msgBuffer));
174 geomFactory = GeometryFactory::getDefaultInstance();
175 WKBOutputDims = 2;
176 WKBByteOrder = getMachineByteOrder();
177 setNoticeHandler(NULL);
178 setErrorHandler(NULL);
179 initialized = 1;
182 GEOSMessageHandler
183 setNoticeHandler(GEOSMessageHandler nf)
185 GEOSMessageHandler f = noticeMessageOld;
186 noticeMessageOld = nf;
187 noticeMessageNew = NULL;
188 noticeData = NULL;
190 return f;
193 GEOSMessageHandler
194 setErrorHandler(GEOSMessageHandler nf)
196 GEOSMessageHandler f = errorMessageOld;
197 errorMessageOld = nf;
198 errorMessageNew = NULL;
199 errorData = NULL;
201 return f;
204 GEOSMessageHandler_r
205 setNoticeHandler(GEOSMessageHandler_r nf, void *userData) {
206 GEOSMessageHandler_r f = noticeMessageNew;
207 noticeMessageOld = NULL;
208 noticeMessageNew = nf;
209 noticeData = userData;
211 return f;
214 GEOSMessageHandler_r
215 setErrorHandler(GEOSMessageHandler_r ef, void *userData)
217 GEOSMessageHandler_r f = errorMessageNew;
218 errorMessageOld = NULL;
219 errorMessageNew = ef;
220 errorData = userData;
222 return f;
225 void
226 NOTICE_MESSAGE(string fmt, ...)
228 if (NULL == noticeMessageOld && NULL == noticeMessageNew) {
229 return;
232 va_list args;
233 va_start(args, fmt);
234 int result = vsnprintf(msgBuffer, sizeof(msgBuffer) - 1, fmt.c_str(), args);
235 va_end(args);
237 if (result > 0) {
238 if (noticeMessageOld) {
239 noticeMessageOld("%s", msgBuffer);
240 } else {
241 noticeMessageNew(msgBuffer, noticeData);
246 void
247 ERROR_MESSAGE(string fmt, ...)
249 if (NULL == errorMessageOld && NULL == errorMessageNew) {
250 return;
253 va_list args;
254 va_start(args, fmt);
255 int result = vsnprintf(msgBuffer, sizeof(msgBuffer) - 1, fmt.c_str(), args);
256 va_end(args);
258 if (result > 0) {
259 if (errorMessageOld) {
260 errorMessageOld("%s", msgBuffer);
261 } else {
262 errorMessageNew(msgBuffer, errorData);
266 } GEOSContextHandleInternal_t;
268 // CAPI_ItemVisitor is used internally by the CAPI STRtree
269 // wrappers. It's defined here just to keep it out of the
270 // extern "C" block.
271 class CAPI_ItemVisitor : public geos::index::ItemVisitor {
272 GEOSQueryCallback callback;
273 void *userdata;
274 public:
275 CAPI_ItemVisitor (GEOSQueryCallback cb, void *ud)
276 : ItemVisitor(), callback(cb), userdata(ud) {}
277 void visitItem (void *item) { callback(item, userdata); }
281 //## PROTOTYPES #############################################
283 extern "C" const char GEOS_DLL *GEOSjtsport();
284 extern "C" char GEOS_DLL *GEOSasText(Geometry *g1);
287 namespace { // anonymous
289 char* gstrdup_s(const char* str, const std::size_t size)
291 char* out = static_cast<char*>(malloc(size + 1));
292 if (0 != out)
294 // as no strlen call necessary, memcpy may be faster than strcpy
295 std::memcpy(out, str, size + 1);
298 assert(0 != out);
300 // we haven't been checking allocation before ticket #371
301 if (0 == out)
303 throw(std::runtime_error("Failed to allocate memory for duplicate string"));
306 return out;
309 char* gstrdup(std::string const& str)
311 return gstrdup_s(str.c_str(), str.size());
314 } // namespace anonymous
316 extern "C" {
318 GEOSContextHandle_t
319 initGEOS_r(GEOSMessageHandler nf, GEOSMessageHandler ef)
321 GEOSContextHandle_t handle = GEOS_init_r();
323 if (0 != handle) {
324 GEOSContext_setNoticeHandler_r(handle, nf);
325 GEOSContext_setErrorHandler_r(handle, ef);
328 return handle;
331 GEOSContextHandle_t
332 GEOS_init_r()
334 GEOSContextHandleInternal_t *handle = new GEOSContextHandleInternal_t();
336 geos::util::Interrupt::cancel();
338 return static_cast<GEOSContextHandle_t>(handle);
341 GEOSMessageHandler
342 GEOSContext_setNoticeHandler_r(GEOSContextHandle_t extHandle, GEOSMessageHandler nf)
344 GEOSContextHandleInternal_t *handle = 0;
345 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
346 if ( 0 == handle->initialized )
348 return NULL;
351 return handle->setNoticeHandler(nf);
354 GEOSMessageHandler
355 GEOSContext_setErrorHandler_r(GEOSContextHandle_t extHandle, GEOSMessageHandler nf)
357 GEOSContextHandleInternal_t *handle = 0;
358 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
359 if ( 0 == handle->initialized )
361 return NULL;
364 return handle->setErrorHandler(nf);
367 GEOSMessageHandler_r
368 GEOSContext_setNoticeMessageHandler_r(GEOSContextHandle_t extHandle, GEOSMessageHandler_r nf, void *userData) {
369 GEOSContextHandleInternal_t *handle = 0;
370 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
371 if ( 0 == handle->initialized )
373 return NULL;
376 return handle->setNoticeHandler(nf, userData);
379 GEOSMessageHandler_r
380 GEOSContext_setErrorMessageHandler_r(GEOSContextHandle_t extHandle, GEOSMessageHandler_r ef, void *userData)
382 GEOSContextHandleInternal_t *handle = 0;
383 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
384 if ( 0 == handle->initialized )
386 return NULL;
389 return handle->setErrorHandler(ef, userData);
392 void
393 finishGEOS_r(GEOSContextHandle_t extHandle)
395 // Fix up freeing handle w.r.t. malloc above
396 delete extHandle;
397 extHandle = NULL;
400 void
401 GEOS_finish_r(GEOSContextHandle_t extHandle)
403 finishGEOS_r(extHandle);
406 void
407 GEOSFree_r (GEOSContextHandle_t extHandle, void* buffer)
409 assert(0 != extHandle);
411 free(buffer);
414 //-----------------------------------------------------------
415 // relate()-related functions
416 // return 0 = false, 1 = true, 2 = error occured
417 //-----------------------------------------------------------
419 char
420 GEOSDisjoint_r(GEOSContextHandle_t extHandle, const Geometry *g1, const Geometry *g2)
422 if ( 0 == extHandle )
424 return 2;
427 GEOSContextHandleInternal_t *handle = 0;
428 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
429 if ( handle->initialized == 0 )
431 return 2;
436 bool result = g1->disjoint(g2);
437 return result;
440 // TODO: mloskot is going to replace these double-catch block
441 // with a macro to remove redundant code in this and
442 // following functions.
443 catch (const std::exception &e)
445 handle->ERROR_MESSAGE("%s", e.what());
447 catch (...)
449 handle->ERROR_MESSAGE("Unknown exception thrown");
452 return 2;
455 char
456 GEOSTouches_r(GEOSContextHandle_t extHandle, const Geometry *g1, const Geometry *g2)
458 if ( 0 == extHandle )
460 return 2;
463 GEOSContextHandleInternal_t *handle = 0;
464 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
465 if ( 0 == handle->initialized )
467 return 2;
472 bool result = g1->touches(g2);
473 return result;
475 catch (const std::exception &e)
477 handle->ERROR_MESSAGE("%s", e.what());
479 catch (...)
481 handle->ERROR_MESSAGE("Unknown exception thrown");
484 return 2;
487 char
488 GEOSIntersects_r(GEOSContextHandle_t extHandle, const Geometry *g1, const Geometry *g2)
490 if ( 0 == extHandle )
492 return 2;
495 GEOSContextHandleInternal_t *handle = 0;
496 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
497 if ( 0 == handle->initialized )
499 return 2;
504 bool result = g1->intersects(g2);
505 return result;
507 catch (const std::exception &e)
509 handle->ERROR_MESSAGE("%s", e.what());
511 catch (...)
513 handle->ERROR_MESSAGE("Unknown exception thrown");
516 return 2;
519 char
520 GEOSCrosses_r(GEOSContextHandle_t extHandle, const Geometry *g1, const Geometry *g2)
522 if ( 0 == extHandle )
524 return 2;
527 GEOSContextHandleInternal_t *handle = 0;
528 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
529 if ( 0 == handle->initialized )
531 return 2;
536 bool result = g1->crosses(g2);
537 return result;
539 catch (const std::exception &e)
541 handle->ERROR_MESSAGE("%s", e.what());
543 catch (...)
545 handle->ERROR_MESSAGE("Unknown exception thrown");
548 return 2;
551 char
552 GEOSWithin_r(GEOSContextHandle_t extHandle, const Geometry *g1, const Geometry *g2)
554 if ( 0 == extHandle )
556 return 2;
559 GEOSContextHandleInternal_t *handle = 0;
560 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
561 if ( 0 == handle->initialized )
563 return 2;
568 bool result = g1->within(g2);
569 return result;
571 catch (const std::exception &e)
573 handle->ERROR_MESSAGE("%s", e.what());
575 catch (...)
577 handle->ERROR_MESSAGE("Unknown exception thrown");
580 return 2;
583 // call g1->contains(g2)
584 // returns 0 = false
585 // 1 = true
586 // 2 = error was trapped
587 char
588 GEOSContains_r(GEOSContextHandle_t extHandle, const Geometry *g1, const Geometry *g2)
590 if ( 0 == extHandle )
592 return 2;
595 GEOSContextHandleInternal_t *handle = 0;
596 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
597 if ( 0 == handle->initialized )
599 return 2;
604 bool result = g1->contains(g2);
605 return result;
607 catch (const std::exception &e)
609 handle->ERROR_MESSAGE("%s", e.what());
611 catch (...)
613 handle->ERROR_MESSAGE("Unknown exception thrown");
616 return 2;
619 char
620 GEOSOverlaps_r(GEOSContextHandle_t extHandle, const Geometry *g1, const Geometry *g2)
622 if ( 0 == extHandle )
624 return 2;
627 GEOSContextHandleInternal_t *handle = 0;
628 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
629 if ( 0 == handle->initialized )
631 return 2;
636 bool result = g1->overlaps(g2);
637 return result;
639 catch (const std::exception &e)
641 handle->ERROR_MESSAGE("%s", e.what());
643 catch (...)
645 handle->ERROR_MESSAGE("Unknown exception thrown");
648 return 2;
651 char
652 GEOSCovers_r(GEOSContextHandle_t extHandle, const Geometry *g1, const Geometry *g2)
654 if ( 0 == extHandle )
656 return 2;
659 GEOSContextHandleInternal_t *handle = 0;
660 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
661 if ( 0 == handle->initialized )
663 return 2;
668 bool result = g1->covers(g2);
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 2;
683 char
684 GEOSCoveredBy_r(GEOSContextHandle_t extHandle, const Geometry *g1, const Geometry *g2)
686 if ( 0 == extHandle )
688 return 2;
691 GEOSContextHandleInternal_t *handle = 0;
692 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
693 if ( 0 == handle->initialized )
695 return 2;
700 bool result = g1->coveredBy(g2);
701 return result;
703 catch (const std::exception &e)
705 handle->ERROR_MESSAGE("%s", e.what());
707 catch (...)
709 handle->ERROR_MESSAGE("Unknown exception thrown");
712 return 2;
716 //-------------------------------------------------------------------
717 // low-level relate functions
718 //------------------------------------------------------------------
720 char
721 GEOSRelatePattern_r(GEOSContextHandle_t extHandle, const Geometry *g1, const Geometry *g2, const char *pat)
723 if ( 0 == extHandle )
725 return 2;
728 GEOSContextHandleInternal_t *handle = 0;
729 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
730 if ( 0 == handle->initialized )
732 return 2;
737 std::string s(pat);
738 bool result = g1->relate(g2, s);
739 return result;
741 catch (const std::exception &e)
743 handle->ERROR_MESSAGE("%s", e.what());
745 catch (...)
747 handle->ERROR_MESSAGE("Unknown exception thrown");
750 return 2;
753 char
754 GEOSRelatePatternMatch_r(GEOSContextHandle_t extHandle, const char *mat,
755 const char *pat)
757 if ( 0 == extHandle )
759 return 2;
762 GEOSContextHandleInternal_t *handle = 0;
763 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
764 if ( 0 == handle->initialized )
766 return 2;
771 using geos::geom::IntersectionMatrix;
773 std::string m(mat);
774 std::string p(pat);
775 IntersectionMatrix im(m);
777 bool result = im.matches(p);
778 return result;
780 catch (const std::exception &e)
782 handle->ERROR_MESSAGE("%s", e.what());
784 catch (...)
786 handle->ERROR_MESSAGE("Unknown exception thrown");
789 return 2;
792 char *
793 GEOSRelate_r(GEOSContextHandle_t extHandle, const Geometry *g1, const Geometry *g2)
795 if ( 0 == extHandle )
797 return NULL;
800 GEOSContextHandleInternal_t *handle = 0;
801 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
802 if ( 0 == handle->initialized )
804 return NULL;
809 using geos::geom::IntersectionMatrix;
811 IntersectionMatrix* im = g1->relate(g2);
812 if (0 == im)
814 return 0;
817 char *result = gstrdup(im->toString());
819 delete im;
820 im = 0;
822 return result;
824 catch (const std::exception &e)
826 handle->ERROR_MESSAGE("%s", e.what());
828 catch (...)
830 handle->ERROR_MESSAGE("Unknown exception thrown");
833 return NULL;
836 char *
837 GEOSRelateBoundaryNodeRule_r(GEOSContextHandle_t extHandle, const Geometry *g1, const Geometry *g2, int bnr)
839 if ( 0 == extHandle )
841 return NULL;
844 GEOSContextHandleInternal_t *handle = 0;
845 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
846 if ( 0 == handle->initialized )
848 return NULL;
853 using geos::operation::relate::RelateOp;
854 using geos::geom::IntersectionMatrix;
855 using geos::algorithm::BoundaryNodeRule;
857 IntersectionMatrix* im;
858 switch (bnr) {
859 case GEOSRELATE_BNR_MOD2: /* same as OGC */
860 im = RelateOp::relate(g1, g2,
861 BoundaryNodeRule::getBoundaryRuleMod2());
862 break;
863 case GEOSRELATE_BNR_ENDPOINT:
864 im = RelateOp::relate(g1, g2,
865 BoundaryNodeRule::getBoundaryEndPoint());
866 break;
867 case GEOSRELATE_BNR_MULTIVALENT_ENDPOINT:
868 im = RelateOp::relate(g1, g2,
869 BoundaryNodeRule::getBoundaryMultivalentEndPoint());
870 break;
871 case GEOSRELATE_BNR_MONOVALENT_ENDPOINT:
872 im = RelateOp::relate(g1, g2,
873 BoundaryNodeRule::getBoundaryMonovalentEndPoint());
874 break;
875 default:
876 handle->ERROR_MESSAGE("Invalid boundary node rule %d", bnr);
877 return 0;
878 break;
881 if (0 == im) return 0;
883 char *result = gstrdup(im->toString());
885 delete im;
886 im = 0;
888 return result;
890 catch (const std::exception &e)
892 handle->ERROR_MESSAGE("%s", e.what());
894 catch (...)
896 handle->ERROR_MESSAGE("Unknown exception thrown");
899 return NULL;
904 //-----------------------------------------------------------------
905 // isValid
906 //-----------------------------------------------------------------
909 char
910 GEOSisValid_r(GEOSContextHandle_t extHandle, const Geometry *g1)
912 if ( 0 == extHandle )
914 return 2;
917 GEOSContextHandleInternal_t *handle = 0;
918 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
919 if ( 0 == handle->initialized )
921 return 2;
926 using geos::operation::valid::IsValidOp;
927 using geos::operation::valid::TopologyValidationError;
929 IsValidOp ivo(g1);
930 TopologyValidationError *err = ivo.getValidationError();
931 if ( err )
933 handle->NOTICE_MESSAGE("%s", err->toString().c_str());
934 return 0;
936 else
938 return 1;
941 catch (const std::exception &e)
943 handle->ERROR_MESSAGE("%s", e.what());
945 catch (...)
947 handle->ERROR_MESSAGE("Unknown exception thrown");
950 return 2;
953 char *
954 GEOSisValidReason_r(GEOSContextHandle_t extHandle, const Geometry *g1)
956 if ( 0 == extHandle )
958 return NULL;
961 GEOSContextHandleInternal_t *handle = 0;
962 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
963 if ( 0 == handle->initialized )
965 return NULL;
970 using geos::operation::valid::IsValidOp;
971 using geos::operation::valid::TopologyValidationError;
973 char* result = 0;
974 char const* const validstr = "Valid Geometry";
976 IsValidOp ivo(g1);
977 TopologyValidationError *err = ivo.getValidationError();
978 if (0 != err)
980 std::ostringstream ss;
981 ss.precision(15);
982 ss << err->getCoordinate();
983 const std::string errloc = ss.str();
984 std::string errmsg(err->getMessage());
985 errmsg += "[" + errloc + "]";
986 result = gstrdup(errmsg);
988 else
990 result = gstrdup(std::string(validstr));
993 return result;
995 catch (const std::exception &e)
997 handle->ERROR_MESSAGE("%s", e.what());
999 catch (...)
1001 handle->ERROR_MESSAGE("Unknown exception thrown");
1004 return 0;
1007 char
1008 GEOSisValidDetail_r(GEOSContextHandle_t extHandle, const Geometry *g,
1009 int flags, char** reason, Geometry ** location)
1011 if ( 0 == extHandle )
1013 return 0;
1016 GEOSContextHandleInternal_t *handle = 0;
1017 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
1018 if ( 0 == handle->initialized )
1020 return 0;
1025 using geos::operation::valid::IsValidOp;
1026 using geos::operation::valid::TopologyValidationError;
1028 IsValidOp ivo(g);
1029 if ( flags & GEOSVALID_ALLOW_SELFTOUCHING_RING_FORMING_HOLE ) {
1030 ivo.setSelfTouchingRingFormingHoleValid(true);
1032 TopologyValidationError *err = ivo.getValidationError();
1033 if (0 != err)
1035 if ( location ) {
1036 *location = handle->geomFactory->createPoint(err->getCoordinate());
1038 if ( reason ) {
1039 std::string errmsg(err->getMessage());
1040 *reason = gstrdup(errmsg);
1042 return 0;
1045 if ( location ) *location = 0;
1046 if ( reason ) *reason = 0;
1047 return 1; /* valid */
1050 catch (const std::exception &e)
1052 handle->ERROR_MESSAGE("%s", e.what());
1054 catch (...)
1056 handle->ERROR_MESSAGE("Unknown exception thrown");
1059 return 2; /* exception */
1062 //-----------------------------------------------------------------
1063 // general purpose
1064 //-----------------------------------------------------------------
1066 char
1067 GEOSEquals_r(GEOSContextHandle_t extHandle, const Geometry *g1, const Geometry *g2)
1069 if ( 0 == extHandle )
1071 return 2;
1074 GEOSContextHandleInternal_t *handle = 0;
1075 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
1076 if ( 0 == handle->initialized )
1078 return 2;
1083 bool result = g1->equals(g2);
1084 return result;
1086 catch (const std::exception &e)
1088 handle->ERROR_MESSAGE("%s", e.what());
1090 catch (...)
1092 handle->ERROR_MESSAGE("Unknown exception thrown");
1095 return 2;
1098 char
1099 GEOSEqualsExact_r(GEOSContextHandle_t extHandle, const Geometry *g1, const Geometry *g2, double tolerance)
1101 if ( 0 == extHandle )
1103 return 2;
1106 GEOSContextHandleInternal_t *handle = 0;
1107 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
1108 if ( 0 == handle->initialized )
1110 return 2;
1115 bool result = g1->equalsExact(g2, tolerance);
1116 return result;
1118 catch (const std::exception &e)
1120 handle->ERROR_MESSAGE("%s", e.what());
1122 catch (...)
1124 handle->ERROR_MESSAGE("Unknown exception thrown");
1127 return 2;
1131 GEOSDistance_r(GEOSContextHandle_t extHandle, const Geometry *g1, const Geometry *g2, double *dist)
1133 assert(0 != dist);
1135 if ( 0 == extHandle )
1137 return 0;
1140 GEOSContextHandleInternal_t *handle = 0;
1141 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
1142 if ( 0 == handle->initialized )
1144 return 0;
1149 *dist = g1->distance(g2);
1150 return 1;
1152 catch (const std::exception &e)
1154 handle->ERROR_MESSAGE("%s", e.what());
1156 catch (...)
1158 handle->ERROR_MESSAGE("Unknown exception thrown");
1161 return 0;
1165 GEOSDistanceIndexed_r(GEOSContextHandle_t extHandle, const Geometry *g1, const Geometry *g2, double *dist)
1167 assert(0 != dist);
1169 if ( 0 == extHandle )
1171 return 0;
1174 GEOSContextHandleInternal_t *handle = 0;
1175 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
1176 if ( 0 == handle->initialized )
1178 return 0;
1183 *dist = IndexedFacetDistance::distance(g1, g2);
1184 return 1;
1186 catch (const std::exception &e)
1188 handle->ERROR_MESSAGE("%s", e.what());
1190 catch (...)
1192 handle->ERROR_MESSAGE("Unknown exception thrown");
1195 return 0;
1199 GEOSHausdorffDistance_r(GEOSContextHandle_t extHandle, const Geometry *g1, const Geometry *g2, double *dist)
1201 assert(0 != dist);
1203 if ( 0 == extHandle )
1205 return 0;
1208 GEOSContextHandleInternal_t *handle = 0;
1209 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
1210 if ( 0 == handle->initialized )
1212 return 0;
1217 *dist = DiscreteHausdorffDistance::distance(*g1, *g2);
1218 return 1;
1220 catch (const std::exception &e)
1222 handle->ERROR_MESSAGE("%s", e.what());
1224 catch (...)
1226 handle->ERROR_MESSAGE("Unknown exception thrown");
1229 return 0;
1233 GEOSHausdorffDistanceDensify_r(GEOSContextHandle_t extHandle, const Geometry *g1, const Geometry *g2, double densifyFrac, double *dist)
1235 assert(0 != dist);
1237 if ( 0 == extHandle )
1239 return 0;
1242 GEOSContextHandleInternal_t *handle = 0;
1243 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
1244 if ( 0 == handle->initialized )
1246 return 0;
1251 *dist = DiscreteHausdorffDistance::distance(*g1, *g2, densifyFrac);
1252 return 1;
1254 catch (const std::exception &e)
1256 handle->ERROR_MESSAGE("%s", e.what());
1258 catch (...)
1260 handle->ERROR_MESSAGE("Unknown exception thrown");
1263 return 0;
1267 GEOSFrechetDistance_r(GEOSContextHandle_t extHandle, const Geometry *g1, const Geometry *g2, double *dist)
1269 assert(0 != dist);
1271 if ( 0 == extHandle )
1273 return 0;
1276 GEOSContextHandleInternal_t *handle = 0;
1277 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
1278 if ( 0 == handle->initialized )
1280 return 0;
1285 *dist = DiscreteFrechetDistance::distance(*g1, *g2);
1286 return 1;
1288 catch (const std::exception &e)
1290 handle->ERROR_MESSAGE("%s", e.what());
1292 catch (...)
1294 handle->ERROR_MESSAGE("Unknown exception thrown");
1297 return 0;
1301 GEOSFrechetDistanceDensify_r(GEOSContextHandle_t extHandle, const Geometry *g1, const Geometry *g2, double densifyFrac, double *dist)
1303 assert(0 != dist);
1305 if ( 0 == extHandle )
1307 return 0;
1310 GEOSContextHandleInternal_t *handle = 0;
1311 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
1312 if ( 0 == handle->initialized )
1314 return 0;
1319 *dist = DiscreteFrechetDistance::distance(*g1, *g2, densifyFrac);
1320 return 1;
1322 catch (const std::exception &e)
1324 handle->ERROR_MESSAGE("%s", e.what());
1326 catch (...)
1328 handle->ERROR_MESSAGE("Unknown exception thrown");
1331 return 0;
1335 GEOSArea_r(GEOSContextHandle_t extHandle, const Geometry *g, double *area)
1337 assert(0 != area);
1339 if ( 0 == extHandle )
1341 return 0;
1344 GEOSContextHandleInternal_t *handle = 0;
1345 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
1346 if ( 0 == handle->initialized )
1348 return 0;
1353 *area = g->getArea();
1354 return 1;
1356 catch (const std::exception &e)
1358 handle->ERROR_MESSAGE("%s", e.what());
1360 catch (...)
1362 handle->ERROR_MESSAGE("Unknown exception thrown");
1365 return 0;
1369 GEOSLength_r(GEOSContextHandle_t extHandle, const Geometry *g, double *length)
1371 assert(0 != length);
1373 if ( 0 == extHandle )
1375 return 2;
1378 GEOSContextHandleInternal_t *handle = 0;
1379 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
1380 if ( 0 == handle->initialized )
1382 return 0;
1387 *length = g->getLength();
1388 return 1;
1390 catch (const std::exception &e)
1392 handle->ERROR_MESSAGE("%s", e.what());
1394 catch (...)
1396 handle->ERROR_MESSAGE("Unknown exception thrown");
1399 return 0;
1402 CoordinateSequence *
1403 GEOSNearestPoints_r(GEOSContextHandle_t extHandle, const Geometry *g1, const Geometry *g2)
1405 if ( 0 == extHandle )
1407 return NULL;
1410 GEOSContextHandleInternal_t *handle = 0;
1411 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
1412 if ( 0 == handle->initialized )
1414 return NULL;
1419 if (g1->isEmpty() || g2->isEmpty()) return 0;
1420 return geos::operation::distance::DistanceOp::nearestPoints(g1, g2);
1422 catch (const std::exception &e)
1424 handle->ERROR_MESSAGE("%s", e.what());
1426 catch (...)
1428 handle->ERROR_MESSAGE("Unknown exception thrown");
1431 return NULL;
1435 Geometry *
1436 GEOSGeomFromWKT_r(GEOSContextHandle_t extHandle, const char *wkt)
1438 if ( 0 == extHandle )
1440 return NULL;
1443 GEOSContextHandleInternal_t *handle = 0;
1444 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
1445 if ( 0 == handle->initialized )
1447 return NULL;
1452 const std::string wktstring(wkt);
1453 WKTReader r(static_cast<GeometryFactory const*>(handle->geomFactory));
1455 Geometry *g = r.read(wktstring);
1456 return g;
1458 catch (const std::exception &e)
1460 handle->ERROR_MESSAGE("%s", e.what());
1462 catch (...)
1464 handle->ERROR_MESSAGE("Unknown exception thrown");
1467 return NULL;
1470 char *
1471 GEOSGeomToWKT_r(GEOSContextHandle_t extHandle, const Geometry *g1)
1473 if ( 0 == extHandle )
1475 return NULL;
1478 GEOSContextHandleInternal_t *handle = 0;
1479 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
1480 if ( 0 == handle->initialized )
1482 return NULL;
1488 char *result = gstrdup(g1->toString());
1489 return result;
1491 catch (const std::exception &e)
1493 handle->ERROR_MESSAGE("%s", e.what());
1495 catch (...)
1497 handle->ERROR_MESSAGE("Unknown exception thrown");
1499 return NULL;
1502 // Remember to free the result!
1503 unsigned char *
1504 GEOSGeomToWKB_buf_r(GEOSContextHandle_t extHandle, const Geometry *g, size_t *size)
1506 assert(0 != size);
1508 if ( 0 == extHandle )
1510 return NULL;
1513 GEOSContextHandleInternal_t *handle = 0;
1514 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
1515 if ( 0 == handle->initialized )
1517 return NULL;
1520 using geos::io::WKBWriter;
1523 int byteOrder = handle->WKBByteOrder;
1524 WKBWriter w(handle->WKBOutputDims, byteOrder);
1525 std::ostringstream os(std::ios_base::binary);
1526 w.write(*g, os);
1527 std::string wkbstring(os.str());
1528 const std::size_t len = wkbstring.length();
1530 unsigned char* result = 0;
1531 result = static_cast<unsigned char*>(malloc(len));
1532 if (0 != result)
1534 std::memcpy(result, wkbstring.c_str(), len);
1535 *size = len;
1537 return result;
1539 catch (const std::exception &e)
1541 handle->ERROR_MESSAGE("%s", e.what());
1543 catch (...)
1545 handle->ERROR_MESSAGE("Unknown exception thrown");
1548 return NULL;
1551 Geometry *
1552 GEOSGeomFromWKB_buf_r(GEOSContextHandle_t extHandle, const unsigned char *wkb, size_t size)
1554 if ( 0 == extHandle )
1556 return NULL;
1559 GEOSContextHandleInternal_t *handle = 0;
1560 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
1561 if ( 0 == handle->initialized )
1563 return NULL;
1566 using geos::io::WKBReader;
1569 std::string wkbstring(reinterpret_cast<const char*>(wkb), size); // make it binary !
1570 WKBReader r(*(static_cast<GeometryFactory const*>(handle->geomFactory)));
1571 std::istringstream is(std::ios_base::binary);
1572 is.str(wkbstring);
1573 is.seekg(0, std::ios::beg); // rewind reader pointer
1574 Geometry *g = r.read(is);
1575 return g;
1577 catch (const std::exception &e)
1579 handle->ERROR_MESSAGE("%s", e.what());
1581 catch (...)
1583 handle->ERROR_MESSAGE("Unknown exception thrown");
1586 return NULL;
1589 /* Read/write wkb hex values. Returned geometries are
1590 owned by the caller.*/
1591 unsigned char *
1592 GEOSGeomToHEX_buf_r(GEOSContextHandle_t extHandle, const Geometry *g, size_t *size)
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;
1606 using geos::io::WKBWriter;
1609 int byteOrder = handle->WKBByteOrder;
1610 WKBWriter w(handle->WKBOutputDims, byteOrder);
1611 std::ostringstream os(std::ios_base::binary);
1612 w.writeHEX(*g, os);
1613 std::string hexstring(os.str());
1615 char *result = gstrdup(hexstring);
1616 if (0 != result)
1618 *size = hexstring.length();
1621 return reinterpret_cast<unsigned char*>(result);
1623 catch (const std::exception &e)
1625 handle->ERROR_MESSAGE("%s", e.what());
1627 catch (...)
1629 handle->ERROR_MESSAGE("Unknown exception thrown");
1632 return NULL;
1635 Geometry *
1636 GEOSGeomFromHEX_buf_r(GEOSContextHandle_t extHandle, const unsigned char *hex, size_t size)
1638 if ( 0 == extHandle )
1640 return NULL;
1643 GEOSContextHandleInternal_t *handle = 0;
1644 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
1645 if ( 0 == handle->initialized )
1647 return NULL;
1650 using geos::io::WKBReader;
1653 std::string hexstring(reinterpret_cast<const char*>(hex), size);
1654 WKBReader r(*(static_cast<GeometryFactory const*>(handle->geomFactory)));
1655 std::istringstream is(std::ios_base::binary);
1656 is.str(hexstring);
1657 is.seekg(0, std::ios::beg); // rewind reader pointer
1659 Geometry *g = r.readHEX(is);
1660 return g;
1662 catch (const std::exception &e)
1664 handle->ERROR_MESSAGE("%s", e.what());
1666 catch (...)
1668 handle->ERROR_MESSAGE("Unknown exception thrown");
1671 return NULL;
1674 char
1675 GEOSisEmpty_r(GEOSContextHandle_t extHandle, const Geometry *g1)
1677 if ( 0 == extHandle )
1679 return 2;
1682 GEOSContextHandleInternal_t *handle = 0;
1683 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
1684 if ( 0 == handle->initialized )
1686 return 2;
1691 return g1->isEmpty();
1693 catch (const std::exception &e)
1695 handle->ERROR_MESSAGE("%s", e.what());
1697 catch (...)
1699 handle->ERROR_MESSAGE("Unknown exception thrown");
1702 return 2;
1705 char
1706 GEOSisSimple_r(GEOSContextHandle_t extHandle, const Geometry *g1)
1708 if ( 0 == extHandle )
1710 return 2;
1713 GEOSContextHandleInternal_t *handle = 0;
1714 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
1715 if ( 0 == handle->initialized )
1717 return 2;
1722 return g1->isSimple();
1724 catch (const std::exception &e)
1726 handle->ERROR_MESSAGE("%s", e.what());
1727 return 2;
1730 catch (...)
1732 handle->ERROR_MESSAGE("Unknown exception thrown");
1733 return 2;
1737 char
1738 GEOSisRing_r(GEOSContextHandle_t extHandle, const Geometry *g)
1740 if ( 0 == extHandle )
1742 return 2;
1745 GEOSContextHandleInternal_t *handle = 0;
1746 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
1747 if ( 0 == handle->initialized )
1749 return 2;
1754 const LineString *ls = dynamic_cast<const LineString *>(g);
1755 if ( ls ) {
1756 return (ls->isRing());
1757 } else {
1758 return 0;
1761 catch (const std::exception &e)
1763 handle->ERROR_MESSAGE("%s", e.what());
1764 return 2;
1767 catch (...)
1769 handle->ERROR_MESSAGE("Unknown exception thrown");
1770 return 2;
1776 //free the result of this
1777 char *
1778 GEOSGeomType_r(GEOSContextHandle_t extHandle, const Geometry *g1)
1780 if ( 0 == extHandle )
1782 return NULL;
1785 GEOSContextHandleInternal_t *handle = 0;
1786 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
1787 if ( 0 == handle->initialized )
1789 return NULL;
1794 std::string s = g1->getGeometryType();
1796 char *result = gstrdup(s);
1797 return result;
1799 catch (const std::exception &e)
1801 handle->ERROR_MESSAGE("%s", e.what());
1803 catch (...)
1805 handle->ERROR_MESSAGE("Unknown exception thrown");
1808 return NULL;
1811 // Return postgis geometry type index
1813 GEOSGeomTypeId_r(GEOSContextHandle_t extHandle, const Geometry *g1)
1815 if ( 0 == extHandle )
1817 return -1;
1820 GEOSContextHandleInternal_t *handle = 0;
1821 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
1822 if ( 0 == handle->initialized )
1824 return -1;
1829 return g1->getGeometryTypeId();
1831 catch (const std::exception &e)
1833 handle->ERROR_MESSAGE("%s", e.what());
1835 catch (...)
1837 handle->ERROR_MESSAGE("Unknown exception thrown");
1840 return -1;
1843 //-------------------------------------------------------------------
1844 // GEOS functions that return geometries
1845 //-------------------------------------------------------------------
1847 Geometry *
1848 GEOSEnvelope_r(GEOSContextHandle_t extHandle, const Geometry *g1)
1850 if ( 0 == extHandle )
1852 return NULL;
1855 GEOSContextHandleInternal_t *handle = 0;
1856 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
1857 if ( 0 == handle->initialized )
1859 return NULL;
1864 Geometry *g3 = g1->getEnvelope();
1865 return g3;
1867 catch (const std::exception &e)
1869 handle->ERROR_MESSAGE("%s", e.what());
1871 catch (...)
1873 handle->ERROR_MESSAGE("Unknown exception thrown");
1876 return NULL;
1879 Geometry *
1880 GEOSIntersection_r(GEOSContextHandle_t extHandle, const Geometry *g1, const Geometry *g2)
1882 if ( 0 == extHandle )
1884 return NULL;
1887 GEOSContextHandleInternal_t *handle = 0;
1888 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
1889 if ( 0 == handle->initialized )
1891 return NULL;
1896 return g1->intersection(g2);
1898 catch (const std::exception &e)
1900 handle->ERROR_MESSAGE("%s", e.what());
1902 catch (...)
1904 handle->ERROR_MESSAGE("Unknown exception thrown");
1907 return NULL;
1910 Geometry *
1911 GEOSBuffer_r(GEOSContextHandle_t extHandle, const Geometry *g1, double width, int quadrantsegments)
1913 if ( 0 == extHandle )
1915 return NULL;
1918 GEOSContextHandleInternal_t *handle = 0;
1919 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
1920 if ( 0 == handle->initialized )
1922 return NULL;
1927 Geometry *g3 = g1->buffer(width, quadrantsegments);
1928 return g3;
1930 catch (const std::exception &e)
1932 handle->ERROR_MESSAGE("%s", e.what());
1934 catch (...)
1936 handle->ERROR_MESSAGE("Unknown exception thrown");
1939 return NULL;
1942 Geometry *
1943 GEOSBufferWithStyle_r(GEOSContextHandle_t extHandle, const Geometry *g1, double width, int quadsegs, int endCapStyle, int joinStyle, double mitreLimit)
1945 using geos::operation::buffer::BufferParameters;
1946 using geos::operation::buffer::BufferOp;
1947 using geos::util::IllegalArgumentException;
1949 if ( 0 == extHandle )
1951 return NULL;
1954 GEOSContextHandleInternal_t *handle = 0;
1955 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
1956 if ( 0 == handle->initialized )
1958 return NULL;
1963 BufferParameters bp;
1964 bp.setQuadrantSegments(quadsegs);
1966 if ( endCapStyle > BufferParameters::CAP_SQUARE )
1968 throw IllegalArgumentException("Invalid buffer endCap style");
1970 bp.setEndCapStyle(
1971 static_cast<BufferParameters::EndCapStyle>(endCapStyle)
1974 if ( joinStyle > BufferParameters::JOIN_BEVEL )
1976 throw IllegalArgumentException("Invalid buffer join style");
1978 bp.setJoinStyle(
1979 static_cast<BufferParameters::JoinStyle>(joinStyle)
1981 bp.setMitreLimit(mitreLimit);
1982 BufferOp op(g1, bp);
1983 Geometry *g3 = op.getResultGeometry(width);
1984 return g3;
1986 catch (const std::exception &e)
1988 handle->ERROR_MESSAGE("%s", e.what());
1990 catch (...)
1992 handle->ERROR_MESSAGE("Unknown exception thrown");
1995 return NULL;
1998 Geometry *
1999 GEOSOffsetCurve_r(GEOSContextHandle_t extHandle, const Geometry *g1, double width, int quadsegs, int joinStyle, double mitreLimit)
2001 if ( 0 == extHandle ) return NULL;
2003 GEOSContextHandleInternal_t *handle = 0;
2004 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
2005 if ( 0 == handle->initialized ) return NULL;
2009 BufferParameters bp;
2010 bp.setEndCapStyle( BufferParameters::CAP_FLAT );
2011 bp.setQuadrantSegments(quadsegs);
2013 if ( joinStyle > BufferParameters::JOIN_BEVEL )
2015 throw IllegalArgumentException("Invalid buffer join style");
2017 bp.setJoinStyle(
2018 static_cast<BufferParameters::JoinStyle>(joinStyle)
2020 bp.setMitreLimit(mitreLimit);
2022 bool isLeftSide = true;
2023 if ( width < 0 ) {
2024 isLeftSide = false;
2025 width = -width;
2027 BufferBuilder bufBuilder (bp);
2028 Geometry *g3 = bufBuilder.bufferLineSingleSided(g1, width, isLeftSide);
2030 return g3;
2032 catch (const std::exception &e)
2034 handle->ERROR_MESSAGE("%s", e.what());
2036 catch (...)
2038 handle->ERROR_MESSAGE("Unknown exception thrown");
2041 return NULL;
2044 /* @deprecated in 3.3.0 */
2045 Geometry *
2046 GEOSSingleSidedBuffer_r(GEOSContextHandle_t extHandle, const Geometry *g1, double width, int quadsegs, int joinStyle, double mitreLimit, int leftSide)
2048 if ( 0 == extHandle ) return NULL;
2050 GEOSContextHandleInternal_t *handle = 0;
2051 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
2052 if ( 0 == handle->initialized ) return NULL;
2056 BufferParameters bp;
2057 bp.setEndCapStyle( BufferParameters::CAP_FLAT );
2058 bp.setQuadrantSegments(quadsegs);
2060 if ( joinStyle > BufferParameters::JOIN_BEVEL )
2062 throw IllegalArgumentException("Invalid buffer join style");
2064 bp.setJoinStyle(
2065 static_cast<BufferParameters::JoinStyle>(joinStyle)
2067 bp.setMitreLimit(mitreLimit);
2069 bool isLeftSide = leftSide == 0 ? false : true;
2070 BufferBuilder bufBuilder (bp);
2071 Geometry *g3 = bufBuilder.bufferLineSingleSided(g1, width, isLeftSide);
2073 return g3;
2075 catch (const std::exception &e)
2077 handle->ERROR_MESSAGE("%s", e.what());
2079 catch (...)
2081 handle->ERROR_MESSAGE("Unknown exception thrown");
2084 return NULL;
2087 Geometry *
2088 GEOSConvexHull_r(GEOSContextHandle_t extHandle, const Geometry *g1)
2090 if ( 0 == extHandle )
2092 return NULL;
2095 GEOSContextHandleInternal_t *handle = 0;
2096 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
2097 if ( 0 == handle->initialized )
2099 return NULL;
2104 Geometry *g3 = g1->convexHull();
2105 return g3;
2107 catch (const std::exception &e)
2109 handle->ERROR_MESSAGE("%s", e.what());
2111 catch (...)
2113 handle->ERROR_MESSAGE("Unknown exception thrown");
2116 return NULL;
2120 Geometry *
2121 GEOSMinimumRotatedRectangle_r(GEOSContextHandle_t extHandle, const Geometry *g)
2123 if ( 0 == extHandle )
2125 return NULL;
2128 GEOSContextHandleInternal_t *handle = 0;
2129 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
2130 if ( 0 == handle->initialized )
2132 return NULL;
2137 geos::algorithm::MinimumDiameter m(g);
2139 Geometry *g3 = m.getMinimumRectangle();
2140 return g3;
2142 catch (const std::exception &e)
2144 handle->ERROR_MESSAGE("%s", e.what());
2146 catch (...)
2148 handle->ERROR_MESSAGE("Unknown exception thrown");
2151 return NULL;
2154 Geometry *
2155 GEOSMinimumWidth_r(GEOSContextHandle_t extHandle, const Geometry *g)
2157 if ( 0 == extHandle )
2159 return NULL;
2162 GEOSContextHandleInternal_t *handle = 0;
2163 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
2164 if ( 0 == handle->initialized )
2166 return NULL;
2171 geos::algorithm::MinimumDiameter m(g);
2173 Geometry *g3 = m.getDiameter();
2174 return g3;
2176 catch (const std::exception &e)
2178 handle->ERROR_MESSAGE("%s", e.what());
2180 catch (...)
2182 handle->ERROR_MESSAGE("Unknown exception thrown");
2185 return NULL;
2188 Geometry *
2189 GEOSMinimumClearanceLine_r(GEOSContextHandle_t extHandle, const Geometry *g)
2191 if ( 0 == extHandle )
2193 return NULL;
2196 GEOSContextHandleInternal_t *handle = 0;
2197 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
2198 if ( 0 == handle->initialized )
2200 return NULL;
2205 geos::precision::MinimumClearance mc(g);
2206 return mc.getLine().release();
2208 catch (const std::exception &e)
2210 handle->ERROR_MESSAGE("%s", e.what());
2212 catch (...)
2214 handle->ERROR_MESSAGE("Unknown exception thrown");
2217 return NULL;
2221 GEOSMinimumClearance_r(GEOSContextHandle_t extHandle, const Geometry *g, double *d)
2223 if ( 0 == extHandle )
2225 return 2;
2228 GEOSContextHandleInternal_t *handle = 0;
2229 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
2230 if ( 0 == handle->initialized )
2232 return 2;
2237 geos::precision::MinimumClearance mc(g);
2238 double res = mc.getDistance();
2239 *d = res;
2240 return 0;
2242 catch (const std::exception &e)
2244 handle->ERROR_MESSAGE("%s", e.what());
2246 catch (...)
2248 handle->ERROR_MESSAGE("Unknown exception thrown");
2251 return 2;
2255 Geometry *
2256 GEOSDifference_r(GEOSContextHandle_t extHandle, const Geometry *g1, const Geometry *g2)
2258 if ( 0 == extHandle )
2260 return NULL;
2263 GEOSContextHandleInternal_t *handle = 0;
2264 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
2265 if ( 0 == handle->initialized )
2267 return NULL;
2272 return g1->difference(g2);
2274 catch (const std::exception &e)
2276 handle->ERROR_MESSAGE("%s", e.what());
2278 catch (...)
2280 handle->ERROR_MESSAGE("Unknown exception thrown");
2283 return NULL;
2286 Geometry *
2287 GEOSBoundary_r(GEOSContextHandle_t extHandle, const Geometry *g1)
2289 if ( 0 == extHandle )
2291 return NULL;
2294 GEOSContextHandleInternal_t *handle = 0;
2295 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
2296 if ( 0 == handle->initialized )
2298 return NULL;
2303 Geometry *g3 = g1->getBoundary();
2304 return g3;
2306 catch (const std::exception &e)
2308 handle->ERROR_MESSAGE("%s", e.what());
2310 catch (...)
2312 handle->ERROR_MESSAGE("Unknown exception thrown");
2315 return NULL;
2318 Geometry *
2319 GEOSSymDifference_r(GEOSContextHandle_t extHandle, const Geometry *g1, const Geometry *g2)
2321 if ( 0 == extHandle )
2323 return NULL;
2326 GEOSContextHandleInternal_t *handle = 0;
2327 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
2328 if ( 0 == handle->initialized )
2330 return NULL;
2335 return g1->symDifference(g2);
2337 catch (const std::exception &e)
2339 handle->ERROR_MESSAGE("%s", e.what());
2340 return NULL;
2343 catch (...)
2345 handle->ERROR_MESSAGE("Unknown exception thrown");
2346 return NULL;
2350 Geometry *
2351 GEOSUnion_r(GEOSContextHandle_t extHandle, const Geometry *g1, const Geometry *g2)
2353 if ( 0 == extHandle )
2355 return NULL;
2358 GEOSContextHandleInternal_t *handle = 0;
2359 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
2360 if ( 0 == handle->initialized )
2362 return NULL;
2367 return g1->Union(g2);
2369 catch (const std::exception &e)
2371 #if VERBOSE_EXCEPTIONS
2372 std::ostringstream s;
2373 s << "Exception on GEOSUnion with following inputs:" << std::endl;
2374 s << "A: "<<g1->toString() << std::endl;
2375 s << "B: "<<g2->toString() << std::endl;
2376 handle->NOTICE_MESSAGE("%s", s.str().c_str());
2377 #endif // VERBOSE_EXCEPTIONS
2378 handle->ERROR_MESSAGE("%s", e.what());
2380 catch (...)
2382 handle->ERROR_MESSAGE("Unknown exception thrown");
2385 return NULL;
2388 Geometry *
2389 GEOSUnaryUnion_r(GEOSContextHandle_t extHandle, const Geometry *g)
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 GeomAutoPtr g3 ( g->Union() );
2406 return g3.release();
2408 catch (const std::exception &e)
2410 #if VERBOSE_EXCEPTIONS
2411 std::ostringstream s;
2412 s << "Exception on GEOSUnaryUnion with following inputs:" << std::endl;
2413 s << "A: "<<g1->toString() << std::endl;
2414 s << "B: "<<g2->toString() << std::endl;
2415 handle->NOTICE_MESSAGE("%s", s.str().c_str());
2416 #endif // VERBOSE_EXCEPTIONS
2417 handle->ERROR_MESSAGE("%s", e.what());
2419 catch (...)
2421 handle->ERROR_MESSAGE("Unknown exception thrown");
2424 return NULL;
2427 Geometry *
2428 GEOSNode_r(GEOSContextHandle_t extHandle, const Geometry *g)
2430 if ( 0 == extHandle )
2432 return NULL;
2435 GEOSContextHandleInternal_t *handle = 0;
2436 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
2437 if ( 0 == handle->initialized )
2439 return NULL;
2444 std::auto_ptr<Geometry> g3 = geos::noding::GeometryNoder::node(*g);
2445 return g3.release();
2447 catch (const std::exception &e)
2449 #if VERBOSE_EXCEPTIONS
2450 std::ostringstream s;
2451 s << "Exception on GEOSUnaryUnion with following inputs:" << std::endl;
2452 s << "A: "<<g1->toString() << std::endl;
2453 s << "B: "<<g2->toString() << std::endl;
2454 handle->NOTICE_MESSAGE("%s", s.str().c_str());
2455 #endif // VERBOSE_EXCEPTIONS
2456 handle->ERROR_MESSAGE("%s", e.what());
2458 catch (...)
2460 handle->ERROR_MESSAGE("Unknown exception thrown");
2463 return NULL;
2466 Geometry *
2467 GEOSUnionCascaded_r(GEOSContextHandle_t extHandle, const Geometry *g1)
2469 if ( 0 == extHandle )
2471 return NULL;
2474 GEOSContextHandleInternal_t *handle = 0;
2475 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
2476 if ( 0 == handle->initialized )
2478 return NULL;
2483 const geos::geom::MultiPolygon *p = dynamic_cast<const geos::geom::MultiPolygon *>(g1);
2484 if ( ! p )
2486 handle->ERROR_MESSAGE("Invalid argument (must be a MultiPolygon)");
2487 return NULL;
2490 using geos::operation::geounion::CascadedPolygonUnion;
2491 return CascadedPolygonUnion::Union(p);
2493 catch (const std::exception &e)
2495 handle->ERROR_MESSAGE("%s", e.what());
2497 catch (...)
2499 handle->ERROR_MESSAGE("Unknown exception thrown");
2502 return NULL;
2505 Geometry *
2506 GEOSPointOnSurface_r(GEOSContextHandle_t extHandle, const Geometry *g1)
2508 if ( 0 == extHandle )
2510 return NULL;
2513 GEOSContextHandleInternal_t *handle = 0;
2514 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
2515 if ( 0 == handle->initialized )
2517 return NULL;
2522 Geometry *ret = g1->getInteriorPoint();
2523 if ( ! ret )
2525 const GeometryFactory* gf = handle->geomFactory;
2526 // return an empty point
2527 return gf->createPoint();
2529 return ret;
2531 catch (const std::exception &e)
2533 handle->ERROR_MESSAGE("%s", e.what());
2535 catch (...)
2537 handle->ERROR_MESSAGE("Unknown exception thrown");
2540 return NULL;
2543 Geometry *
2544 GEOSClipByRect_r(GEOSContextHandle_t extHandle, const Geometry *g, double xmin, double ymin, double xmax, double ymax)
2546 if ( 0 == extHandle )
2548 return NULL;
2551 GEOSContextHandleInternal_t *handle = 0;
2552 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
2553 if ( 0 == handle->initialized )
2555 return NULL;
2560 using geos::operation::intersection::Rectangle;
2561 using geos::operation::intersection::RectangleIntersection;
2562 Rectangle rect(xmin, ymin, xmax, ymax);
2563 std::auto_ptr<Geometry> g3 = RectangleIntersection::clip(*g, rect);
2564 return g3.release();
2566 catch (const std::exception &e)
2568 #if VERBOSE_EXCEPTIONS
2569 std::ostringstream s;
2570 s << "Exception on GEOSClipByRect with following inputs:" << std::endl;
2571 s << "A: "<<g1->toString() << std::endl;
2572 s << "B: "<<g2->toString() << std::endl;
2573 handle->NOTICE_MESSAGE("%s", s.str().c_str());
2574 #endif // VERBOSE_EXCEPTIONS
2575 handle->ERROR_MESSAGE("%s", e.what());
2577 catch (...)
2579 handle->ERROR_MESSAGE("Unknown exception thrown");
2582 return NULL;
2585 //-------------------------------------------------------------------
2586 // memory management functions
2587 //------------------------------------------------------------------
2589 void
2590 GEOSGeom_destroy_r(GEOSContextHandle_t extHandle, Geometry *a)
2592 GEOSContextHandleInternal_t *handle = 0;
2594 // FIXME: mloskot: Does this try-catch around delete means that
2595 // destructors in GEOS may throw? If it does, this is a serious
2596 // violation of "never throw an exception from a destructor" principle
2600 delete a;
2602 catch (const std::exception &e)
2604 if ( 0 == extHandle )
2606 return;
2609 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
2610 if ( 0 == handle->initialized )
2612 return;
2615 handle->ERROR_MESSAGE("%s", e.what());
2617 catch (...)
2619 if ( 0 == extHandle )
2621 return;
2624 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
2625 if ( 0 == handle->initialized )
2627 return;
2630 handle->ERROR_MESSAGE("Unknown exception thrown");
2634 void
2635 GEOSGeom_setUserData_r(GEOSContextHandle_t extHandle, Geometry *g, void* userData)
2637 assert(0 != g);
2639 if ( 0 == extHandle )
2641 return;
2644 GEOSContextHandleInternal_t *handle = 0;
2645 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
2646 if ( 0 == handle->initialized )
2648 return;
2651 g->setUserData(userData);
2654 void
2655 GEOSSetSRID_r(GEOSContextHandle_t extHandle, Geometry *g, int srid)
2657 assert(0 != g);
2659 if ( 0 == extHandle )
2661 return;
2664 GEOSContextHandleInternal_t *handle = 0;
2665 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
2666 if ( 0 == handle->initialized )
2668 return;
2671 g->setSRID(srid);
2676 GEOSGetNumCoordinates_r(GEOSContextHandle_t extHandle, const Geometry *g)
2678 assert(0 != g);
2680 if ( 0 == extHandle )
2682 return -1;
2685 GEOSContextHandleInternal_t *handle = 0;
2686 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
2687 if ( 0 == handle->initialized )
2689 return -1;
2694 return static_cast<int>(g->getNumPoints());
2696 catch (const std::exception &e)
2698 handle->ERROR_MESSAGE("%s", e.what());
2700 catch (...)
2702 handle->ERROR_MESSAGE("Unknown exception thrown");
2705 return -1;
2709 * Return -1 on exception, 0 otherwise.
2710 * Converts Geometry to normal form (or canonical form).
2713 GEOSNormalize_r(GEOSContextHandle_t extHandle, Geometry *g)
2715 assert(0 != g);
2717 if ( 0 == extHandle )
2719 return -1;
2722 GEOSContextHandleInternal_t *handle = 0;
2723 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
2724 if ( 0 == handle->initialized )
2726 return -1;
2731 g->normalize();
2732 return 0; // SUCCESS
2734 catch (const std::exception &e)
2736 handle->ERROR_MESSAGE("%s", e.what());
2738 catch (...)
2740 handle->ERROR_MESSAGE("Unknown exception thrown");
2743 return -1;
2747 GEOSGetNumInteriorRings_r(GEOSContextHandle_t extHandle, const Geometry *g1)
2749 if ( 0 == extHandle )
2751 return -1;
2754 GEOSContextHandleInternal_t *handle = 0;
2755 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
2756 if ( 0 == handle->initialized )
2758 return -1;
2763 const Polygon *p = dynamic_cast<const Polygon *>(g1);
2764 if ( ! p )
2766 handle->ERROR_MESSAGE("Argument is not a Polygon");
2767 return -1;
2769 return static_cast<int>(p->getNumInteriorRing());
2771 catch (const std::exception &e)
2773 handle->ERROR_MESSAGE("%s", e.what());
2775 catch (...)
2777 handle->ERROR_MESSAGE("Unknown exception thrown");
2780 return -1;
2784 // returns -1 on error and 1 for non-multi geometries
2786 GEOSGetNumGeometries_r(GEOSContextHandle_t extHandle, const Geometry *g1)
2788 if ( 0 == extHandle )
2790 return -1;
2793 GEOSContextHandleInternal_t *handle = 0;
2794 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
2795 if ( 0 == handle->initialized )
2797 return -1;
2802 return static_cast<int>(g1->getNumGeometries());
2804 catch (const std::exception &e)
2806 handle->ERROR_MESSAGE("%s", e.what());
2808 catch (...)
2810 handle->ERROR_MESSAGE("Unknown exception thrown");
2813 return -1;
2818 * Call only on GEOMETRYCOLLECTION or MULTI*.
2819 * Return a pointer to the internal Geometry.
2821 const Geometry *
2822 GEOSGetGeometryN_r(GEOSContextHandle_t extHandle, const Geometry *g1, int n)
2824 if ( 0 == extHandle )
2826 return NULL;
2829 GEOSContextHandleInternal_t *handle = 0;
2830 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
2831 if ( 0 == handle->initialized )
2833 return NULL;
2838 return g1->getGeometryN(n);
2840 catch (const std::exception &e)
2842 handle->ERROR_MESSAGE("%s", e.what());
2844 catch (...)
2846 handle->ERROR_MESSAGE("Unknown exception thrown");
2849 return NULL;
2853 * Call only on LINESTRING
2854 * Returns NULL on exception
2856 Geometry *
2857 GEOSGeomGetPointN_r(GEOSContextHandle_t extHandle, const Geometry *g1, int n)
2859 if ( 0 == extHandle )
2861 return NULL;
2864 GEOSContextHandleInternal_t *handle = 0;
2865 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
2866 if ( 0 == handle->initialized )
2868 return NULL;
2873 using geos::geom::LineString;
2874 const LineString *ls = dynamic_cast<const LineString *>(g1);
2875 if ( ! ls )
2877 handle->ERROR_MESSAGE("Argument is not a LineString");
2878 return NULL;
2880 return ls->getPointN(n);
2882 catch (const std::exception &e)
2884 handle->ERROR_MESSAGE("%s", e.what());
2886 catch (...)
2888 handle->ERROR_MESSAGE("Unknown exception thrown");
2891 return NULL;
2895 * Call only on LINESTRING
2897 Geometry *
2898 GEOSGeomGetStartPoint_r(GEOSContextHandle_t extHandle, const Geometry *g1)
2900 if ( 0 == extHandle )
2902 return NULL;
2905 GEOSContextHandleInternal_t *handle = 0;
2906 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
2907 if ( 0 == handle->initialized )
2909 return NULL;
2914 using geos::geom::LineString;
2915 const LineString *ls = dynamic_cast<const LineString *>(g1);
2916 if ( ! ls )
2918 handle->ERROR_MESSAGE("Argument is not a LineString");
2919 return NULL;
2921 return ls->getStartPoint();
2923 catch (const std::exception &e)
2925 handle->ERROR_MESSAGE("%s", e.what());
2927 catch (...)
2929 handle->ERROR_MESSAGE("Unknown exception thrown");
2932 return NULL;
2936 * Call only on LINESTRING
2938 Geometry *
2939 GEOSGeomGetEndPoint_r(GEOSContextHandle_t extHandle, const Geometry *g1)
2941 if ( 0 == extHandle )
2943 return NULL;
2946 GEOSContextHandleInternal_t *handle = 0;
2947 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
2948 if ( 0 == handle->initialized )
2950 return NULL;
2955 using geos::geom::LineString;
2956 const LineString *ls = dynamic_cast<const LineString *>(g1);
2957 if ( ! ls )
2959 handle->ERROR_MESSAGE("Argument is not a LineString");
2960 return NULL;
2962 return ls->getEndPoint();
2964 catch (const std::exception &e)
2966 handle->ERROR_MESSAGE("%s", e.what());
2968 catch (...)
2970 handle->ERROR_MESSAGE("Unknown exception thrown");
2973 return NULL;
2977 * Call only on LINESTRING or MULTILINESTRING
2978 * return 2 on exception, 1 on true, 0 on false
2980 char
2981 GEOSisClosed_r(GEOSContextHandle_t extHandle, const Geometry *g1)
2983 if ( 0 == extHandle )
2985 return 2;
2988 GEOSContextHandleInternal_t *handle = 0;
2989 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
2990 if ( 0 == handle->initialized )
2992 return 2;
2997 using geos::geom::LineString;
2998 using geos::geom::MultiLineString;
3000 const LineString *ls = dynamic_cast<const LineString *>(g1);
3001 if ( ls ) {
3002 return ls->isClosed();
3005 const MultiLineString *mls = dynamic_cast<const MultiLineString *>(g1);
3006 if ( mls ) {
3007 return mls->isClosed();
3010 handle->ERROR_MESSAGE("Argument is not a LineString or MultiLineString");
3011 return 2;
3013 catch (const std::exception &e)
3015 handle->ERROR_MESSAGE("%s", e.what());
3017 catch (...)
3019 handle->ERROR_MESSAGE("Unknown exception thrown");
3022 return 2;
3026 * Call only on LINESTRING
3027 * return 0 on exception, otherwise 1
3030 GEOSGeomGetLength_r(GEOSContextHandle_t extHandle, const Geometry *g1, double *length)
3032 if ( 0 == extHandle )
3034 return 0;
3037 GEOSContextHandleInternal_t *handle = 0;
3038 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
3039 if ( 0 == handle->initialized )
3041 return 0;
3046 using geos::geom::LineString;
3047 const LineString *ls = dynamic_cast<const LineString *>(g1);
3048 if ( ! ls )
3050 handle->ERROR_MESSAGE("Argument is not a LineString");
3051 return 0;
3053 *length = ls->getLength();
3054 return 1;
3056 catch (const std::exception &e)
3058 handle->ERROR_MESSAGE("%s", e.what());
3060 catch (...)
3062 handle->ERROR_MESSAGE("Unknown exception thrown");
3065 return 0;
3069 * Call only on LINESTRING
3072 GEOSGeomGetNumPoints_r(GEOSContextHandle_t extHandle, const Geometry *g1)
3074 if ( 0 == extHandle )
3076 return -1;
3079 GEOSContextHandleInternal_t *handle = 0;
3080 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
3081 if ( 0 == handle->initialized )
3083 return -1;
3088 using geos::geom::LineString;
3089 const LineString *ls = dynamic_cast<const LineString *>(g1);
3090 if ( ! ls )
3092 handle->ERROR_MESSAGE("Argument is not a LineString");
3093 return -1;
3095 return static_cast<int>(ls->getNumPoints());
3097 catch (const std::exception &e)
3099 handle->ERROR_MESSAGE("%s", e.what());
3101 catch (...)
3103 handle->ERROR_MESSAGE("Unknown exception thrown");
3106 return -1;
3110 * For POINT
3111 * returns 0 on exception, otherwise 1
3114 GEOSGeomGetX_r(GEOSContextHandle_t extHandle, const Geometry *g1, double *x)
3116 if ( 0 == extHandle )
3118 return 0;
3121 GEOSContextHandleInternal_t *handle = 0;
3122 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
3123 if ( 0 == handle->initialized )
3125 return 0;
3130 using geos::geom::Point;
3131 const Point *po = dynamic_cast<const Point *>(g1);
3132 if ( ! po )
3134 handle->ERROR_MESSAGE("Argument is not a Point");
3135 return 0;
3137 *x = po->getX();
3138 return 1;
3140 catch (const std::exception &e)
3142 handle->ERROR_MESSAGE("%s", e.what());
3144 catch (...)
3146 handle->ERROR_MESSAGE("Unknown exception thrown");
3149 return 0;
3153 * For POINT
3154 * returns 0 on exception, otherwise 1
3157 GEOSGeomGetY_r(GEOSContextHandle_t extHandle, const Geometry *g1, double *y)
3159 if ( 0 == extHandle )
3161 return 0;
3164 GEOSContextHandleInternal_t *handle = 0;
3165 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
3166 if ( 0 == handle->initialized )
3168 return 0;
3173 using geos::geom::Point;
3174 const Point *po = dynamic_cast<const Point *>(g1);
3175 if ( ! po )
3177 handle->ERROR_MESSAGE("Argument is not a Point");
3178 return 0;
3180 *y = po->getY();
3181 return 1;
3183 catch (const std::exception &e)
3185 handle->ERROR_MESSAGE("%s", e.what());
3187 catch (...)
3189 handle->ERROR_MESSAGE("Unknown exception thrown");
3192 return 0;
3196 * Call only on polygon
3197 * Return a copy of the internal Geometry.
3199 const Geometry *
3200 GEOSGetExteriorRing_r(GEOSContextHandle_t extHandle, const Geometry *g1)
3202 if ( 0 == extHandle )
3204 return NULL;
3207 GEOSContextHandleInternal_t *handle = 0;
3208 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
3209 if ( 0 == handle->initialized )
3211 return NULL;
3216 const Polygon *p = dynamic_cast<const Polygon *>(g1);
3217 if ( ! p )
3219 handle->ERROR_MESSAGE("Invalid argument (must be a Polygon)");
3220 return NULL;
3222 return p->getExteriorRing();
3224 catch (const std::exception &e)
3226 handle->ERROR_MESSAGE("%s", e.what());
3228 catch (...)
3230 handle->ERROR_MESSAGE("Unknown exception thrown");
3233 return NULL;
3237 * Call only on polygon
3238 * Return a pointer to internal storage, do not destroy it.
3240 const Geometry *
3241 GEOSGetInteriorRingN_r(GEOSContextHandle_t extHandle, const Geometry *g1, int n)
3243 if ( 0 == extHandle )
3245 return NULL;
3248 GEOSContextHandleInternal_t *handle = 0;
3249 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
3250 if ( 0 == handle->initialized )
3252 return NULL;
3257 const Polygon *p = dynamic_cast<const Polygon *>(g1);
3258 if ( ! p )
3260 handle->ERROR_MESSAGE("Invalid argument (must be a Polygon)");
3261 return NULL;
3263 return p->getInteriorRingN(n);
3265 catch (const std::exception &e)
3267 handle->ERROR_MESSAGE("%s", e.what());
3269 catch (...)
3271 handle->ERROR_MESSAGE("Unknown exception thrown");
3274 return NULL;
3277 Geometry *
3278 GEOSGetCentroid_r(GEOSContextHandle_t extHandle, const Geometry *g)
3280 if ( 0 == extHandle )
3282 return NULL;
3285 GEOSContextHandleInternal_t *handle = 0;
3286 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
3287 if ( 0 == handle->initialized )
3289 return NULL;
3294 Geometry *ret = g->getCentroid();
3295 if (0 == ret)
3297 const GeometryFactory *gf = handle->geomFactory;
3298 return gf->createPoint();
3300 return ret;
3302 catch (const std::exception &e)
3304 handle->ERROR_MESSAGE("%s", e.what());
3306 catch (...)
3308 handle->ERROR_MESSAGE("Unknown exception thrown");
3311 return NULL;
3314 Geometry *
3315 GEOSGeom_createEmptyCollection_r(GEOSContextHandle_t extHandle, int type)
3317 if ( 0 == extHandle )
3319 return NULL;
3322 GEOSContextHandleInternal_t *handle = 0;
3323 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
3324 if ( 0 == handle->initialized )
3326 return NULL;
3329 #ifdef GEOS_DEBUG
3330 char buf[256];
3331 sprintf(buf, "createCollection: requested type %d", type);
3332 handle->NOTICE_MESSAGE("%s", buf);// TODO: Can handle->NOTICE_MESSAGE format that directly?
3333 #endif
3337 const GeometryFactory* gf = handle->geomFactory;
3339 Geometry *g = 0;
3340 switch (type)
3342 case GEOS_GEOMETRYCOLLECTION:
3343 g = gf->createGeometryCollection();
3344 break;
3345 case GEOS_MULTIPOINT:
3346 g = gf->createMultiPoint();
3347 break;
3348 case GEOS_MULTILINESTRING:
3349 g = gf->createMultiLineString();
3350 break;
3351 case GEOS_MULTIPOLYGON:
3352 g = gf->createMultiPolygon();
3353 break;
3354 default:
3355 handle->ERROR_MESSAGE("Unsupported type request for GEOSGeom_createEmptyCollection_r");
3356 g = 0;
3360 return g;
3362 catch (const std::exception &e)
3364 handle->ERROR_MESSAGE("%s", e.what());
3366 catch (...)
3368 handle->ERROR_MESSAGE("Unknown exception thrown");
3371 return 0;
3374 Geometry *
3375 GEOSGeom_createCollection_r(GEOSContextHandle_t extHandle, int type, Geometry **geoms, unsigned int ngeoms)
3377 if ( 0 == extHandle )
3379 return NULL;
3382 GEOSContextHandleInternal_t *handle = 0;
3383 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
3384 if ( 0 == handle->initialized )
3386 return NULL;
3389 #ifdef GEOS_DEBUG
3390 char buf[256];
3391 sprintf(buf, "PostGIS2GEOS_collection: requested type %d, ngeoms: %d",
3392 type, ngeoms);
3393 handle->NOTICE_MESSAGE("%s", buf);// TODO: Can handle->NOTICE_MESSAGE format that directly?
3394 #endif
3398 const GeometryFactory* gf = handle->geomFactory;
3399 std::vector<Geometry*>* vgeoms = new std::vector<Geometry*>(geoms, geoms + ngeoms);
3401 Geometry *g = 0;
3402 switch (type)
3404 case GEOS_GEOMETRYCOLLECTION:
3405 g = gf->createGeometryCollection(vgeoms);
3406 break;
3407 case GEOS_MULTIPOINT:
3408 g = gf->createMultiPoint(vgeoms);
3409 break;
3410 case GEOS_MULTILINESTRING:
3411 g = gf->createMultiLineString(vgeoms);
3412 break;
3413 case GEOS_MULTIPOLYGON:
3414 g = gf->createMultiPolygon(vgeoms);
3415 break;
3416 default:
3417 handle->ERROR_MESSAGE("Unsupported type request for PostGIS2GEOS_collection");
3418 delete vgeoms;
3419 g = 0;
3423 return g;
3425 catch (const std::exception &e)
3427 handle->ERROR_MESSAGE("%s", e.what());
3429 catch (...)
3431 handle->ERROR_MESSAGE("Unknown exception thrown");
3434 return 0;
3437 Geometry *
3438 GEOSPolygonize_r(GEOSContextHandle_t extHandle, const Geometry * const * g, unsigned int ngeoms)
3440 if ( 0 == extHandle )
3442 return 0;
3445 GEOSContextHandleInternal_t *handle = 0;
3446 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
3447 if ( 0 == handle->initialized )
3449 return 0;
3452 Geometry *out = 0;
3456 // Polygonize
3457 using geos::operation::polygonize::Polygonizer;
3458 Polygonizer plgnzr;
3459 for (std::size_t i = 0; i < ngeoms; ++i)
3461 plgnzr.add(g[i]);
3464 #if GEOS_DEBUG
3465 handle->NOTICE_MESSAGE("geometry vector added to polygonizer");
3466 #endif
3468 std::vector<Polygon*> *polys = plgnzr.getPolygons();
3469 assert(0 != polys);
3471 #if GEOS_DEBUG
3472 handle->NOTICE_MESSAGE("output polygons got");
3473 #endif
3475 // We need a vector of Geometry pointers, not Polygon pointers.
3476 // STL vector doesn't allow transparent upcast of this
3477 // nature, so we explicitly convert.
3478 // (it's just a waste of processor and memory, btw)
3480 // XXX mloskot: Why not to extent GeometryFactory to accept
3481 // vector of polygons or extend Polygonizer to return list of Geometry*
3482 // or add a wrapper which semantic is similar to:
3483 // std::vector<as_polygon<Geometry*> >
3484 std::vector<Geometry*> *polyvec = new std::vector<Geometry *>(polys->size());
3486 for (std::size_t i = 0; i < polys->size(); ++i)
3488 (*polyvec)[i] = (*polys)[i];
3490 delete polys;
3491 polys = 0;
3493 const GeometryFactory *gf = handle->geomFactory;
3495 // The below takes ownership of the passed vector,
3496 // so we must *not* delete it
3497 out = gf->createGeometryCollection(polyvec);
3499 catch (const std::exception &e)
3501 handle->ERROR_MESSAGE("%s", e.what());
3503 catch (...)
3505 handle->ERROR_MESSAGE("Unknown exception thrown");
3508 return out;
3511 Geometry *
3512 GEOSPolygonizer_getCutEdges_r(GEOSContextHandle_t extHandle, const Geometry * const * g, unsigned int ngeoms)
3514 if ( 0 == extHandle )
3516 return 0;
3519 GEOSContextHandleInternal_t *handle = 0;
3520 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
3521 if ( 0 == handle->initialized )
3523 return 0;
3526 Geometry *out = 0;
3530 // Polygonize
3531 using geos::operation::polygonize::Polygonizer;
3532 Polygonizer plgnzr;
3533 for (std::size_t i = 0; i < ngeoms; ++i)
3535 plgnzr.add(g[i]);
3538 #if GEOS_DEBUG
3539 handle->NOTICE_MESSAGE("geometry vector added to polygonizer");
3540 #endif
3542 const std::vector<const LineString *>& lines = plgnzr.getCutEdges();
3544 #if GEOS_DEBUG
3545 handle->NOTICE_MESSAGE("output polygons got");
3546 #endif
3548 // We need a vector of Geometry pointers, not Polygon pointers.
3549 // STL vector doesn't allow transparent upcast of this
3550 // nature, so we explicitly convert.
3551 // (it's just a waste of processor and memory, btw)
3552 // XXX mloskot: See comment for GEOSPolygonize_r
3553 std::vector<Geometry*> *linevec = new std::vector<Geometry *>(lines.size());
3555 for (std::size_t i = 0, n=lines.size(); i < n; ++i)
3557 (*linevec)[i] = lines[i]->clone();
3560 const GeometryFactory *gf = handle->geomFactory;
3562 // The below takes ownership of the passed vector,
3563 // so we must *not* delete it
3564 out = gf->createGeometryCollection(linevec);
3566 catch (const std::exception &e)
3568 handle->ERROR_MESSAGE("%s", e.what());
3570 catch (...)
3572 handle->ERROR_MESSAGE("Unknown exception thrown");
3575 return out;
3578 Geometry *
3579 GEOSPolygonize_full_r(GEOSContextHandle_t extHandle, const Geometry* g,
3580 Geometry** cuts, Geometry** dangles, Geometry** invalid)
3582 if ( 0 == extHandle )
3584 return 0;
3587 GEOSContextHandleInternal_t *handle = 0;
3588 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
3589 if ( 0 == handle->initialized )
3591 return 0;
3596 // Polygonize
3597 using geos::operation::polygonize::Polygonizer;
3598 Polygonizer plgnzr;
3599 for (std::size_t i = 0; i <g->getNumGeometries(); ++i)
3601 plgnzr.add(g->getGeometryN(i));
3604 #if GEOS_DEBUG
3605 handle->NOTICE_MESSAGE("geometry vector added to polygonizer");
3606 #endif
3607 const GeometryFactory *gf = handle->geomFactory;
3609 if ( cuts ) {
3611 const std::vector<const LineString *>& lines = plgnzr.getCutEdges();
3612 std::vector<Geometry*> *linevec = new std::vector<Geometry *>(lines.size());
3613 for (std::size_t i = 0, n=lines.size(); i < n; ++i)
3615 (*linevec)[i] = lines[i]->clone();
3618 // The below takes ownership of the passed vector,
3619 // so we must *not* delete it
3620 *cuts = gf->createGeometryCollection(linevec);
3623 if ( dangles ) {
3625 const std::vector<const LineString *>& lines = plgnzr.getDangles();
3626 std::vector<Geometry*> *linevec = new std::vector<Geometry *>(lines.size());
3627 for (std::size_t i = 0, n=lines.size(); i < n; ++i)
3629 (*linevec)[i] = lines[i]->clone();
3632 // The below takes ownership of the passed vector,
3633 // so we must *not* delete it
3634 *dangles = gf->createGeometryCollection(linevec);
3637 if ( invalid ) {
3639 const std::vector<LineString *>& lines = plgnzr.getInvalidRingLines();
3640 std::vector<Geometry*> *linevec = new std::vector<Geometry *>(lines.size());
3641 for (std::size_t i = 0, n=lines.size(); i < n; ++i)
3643 (*linevec)[i] = lines[i]->clone();
3646 // The below takes ownership of the passed vector,
3647 // so we must *not* delete it
3648 *invalid = gf->createGeometryCollection(linevec);
3651 std::vector<Polygon*> *polys = plgnzr.getPolygons();
3652 std::vector<Geometry*> *polyvec = new std::vector<Geometry *>(polys->size());
3653 for (std::size_t i = 0; i < polys->size(); ++i)
3655 (*polyvec)[i] = (*polys)[i];
3657 delete polys;
3659 return gf->createGeometryCollection(polyvec);
3662 catch (const std::exception &e)
3664 handle->ERROR_MESSAGE("%s", e.what());
3665 return 0;
3667 catch (...)
3669 handle->ERROR_MESSAGE("Unknown exception thrown");
3670 return 0;
3674 Geometry *
3675 GEOSLineMerge_r(GEOSContextHandle_t extHandle, const Geometry *g)
3677 if ( 0 == extHandle )
3679 return 0;
3682 GEOSContextHandleInternal_t *handle = 0;
3683 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
3684 if ( 0 == handle->initialized )
3686 return 0;
3689 Geometry *out = 0;
3693 using geos::operation::linemerge::LineMerger;
3694 LineMerger lmrgr;
3695 lmrgr.add(g);
3697 std::vector<LineString *>* lines = lmrgr.getMergedLineStrings();
3698 assert(0 != lines);
3700 #if GEOS_DEBUG
3701 handle->NOTICE_MESSAGE("output lines got");
3702 #endif
3704 std::vector<Geometry *>*geoms = new std::vector<Geometry *>(lines->size());
3705 for (std::vector<Geometry *>::size_type i = 0; i < lines->size(); ++i)
3707 (*geoms)[i] = (*lines)[i];
3709 delete lines;
3710 lines = 0;
3712 const GeometryFactory *gf = handle->geomFactory;
3713 out = gf->buildGeometry(geoms);
3715 // XXX: old version
3716 //out = gf->createGeometryCollection(geoms);
3718 catch (const std::exception &e)
3720 handle->ERROR_MESSAGE("%s", e.what());
3722 catch (...)
3724 handle->ERROR_MESSAGE("Unknown exception thrown");
3727 return out;
3730 void*
3731 GEOSGeom_getUserData_r(GEOSContextHandle_t extHandle, const Geometry *g)
3733 assert(0 != g);
3735 if ( 0 == extHandle )
3737 return 0;
3740 GEOSContextHandleInternal_t *handle = 0;
3741 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
3742 if ( 0 == handle->initialized )
3744 return 0;
3749 return g->getUserData();
3751 catch (const std::exception &e)
3753 handle->ERROR_MESSAGE("%s", e.what());
3755 catch (...)
3757 handle->ERROR_MESSAGE("Unknown exception thrown");
3760 return NULL;
3764 GEOSGetSRID_r(GEOSContextHandle_t extHandle, const Geometry *g)
3766 assert(0 != g);
3768 if ( 0 == extHandle )
3770 return 0;
3773 GEOSContextHandleInternal_t *handle = 0;
3774 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
3775 if ( 0 == handle->initialized )
3777 return 0;
3782 return g->getSRID();
3784 catch (const std::exception &e)
3786 handle->ERROR_MESSAGE("%s", e.what());
3788 catch (...)
3790 handle->ERROR_MESSAGE("Unknown exception thrown");
3793 return 0;
3796 const char* GEOSversion()
3798 static char version[256];
3799 sprintf(version, "%s " GEOS_REVISION, GEOS_CAPI_VERSION);
3800 return version;
3803 const char* GEOSjtsport()
3805 return GEOS_JTS_PORT;
3808 char
3809 GEOSHasZ_r(GEOSContextHandle_t extHandle, const Geometry *g)
3811 assert(0 != g);
3813 if ( 0 == extHandle )
3815 return -1;
3818 GEOSContextHandleInternal_t *handle = 0;
3819 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
3820 if ( 0 == handle->initialized )
3822 return -1;
3825 if (g->isEmpty())
3827 return false;
3829 assert(0 != g->getCoordinate());
3831 double az = g->getCoordinate()->z;
3832 //handle->ERROR_MESSAGE("ZCoord: %g", az);
3834 return static_cast<char>(FINITE(az));
3838 GEOS_getWKBOutputDims_r(GEOSContextHandle_t extHandle)
3840 if ( 0 == extHandle )
3842 return -1;
3845 GEOSContextHandleInternal_t *handle = 0;
3846 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
3847 if ( 0 == handle->initialized )
3849 return -1;
3852 return handle->WKBOutputDims;
3856 GEOS_setWKBOutputDims_r(GEOSContextHandle_t extHandle, int newdims)
3858 if ( 0 == extHandle )
3860 return -1;
3863 GEOSContextHandleInternal_t *handle = 0;
3864 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
3865 if ( 0 == handle->initialized )
3867 return -1;
3870 if ( newdims < 2 || newdims > 3 )
3872 handle->ERROR_MESSAGE("WKB output dimensions out of range 2..3");
3875 const int olddims = handle->WKBOutputDims;
3876 handle->WKBOutputDims = newdims;
3878 return olddims;
3882 GEOS_getWKBByteOrder_r(GEOSContextHandle_t extHandle)
3884 if ( 0 == extHandle )
3886 return -1;
3889 GEOSContextHandleInternal_t *handle = 0;
3890 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
3891 if ( 0 == handle->initialized )
3893 return -1;
3896 return handle->WKBByteOrder;
3900 GEOS_setWKBByteOrder_r(GEOSContextHandle_t extHandle, int byteOrder)
3902 if ( 0 == extHandle )
3904 return -1;
3907 GEOSContextHandleInternal_t *handle = 0;
3908 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
3909 if ( 0 == handle->initialized )
3911 return -1;
3914 const int oldByteOrder = handle->WKBByteOrder;
3915 handle->WKBByteOrder = byteOrder;
3917 return oldByteOrder;
3921 CoordinateSequence *
3922 GEOSCoordSeq_create_r(GEOSContextHandle_t extHandle, unsigned int size, unsigned int dims)
3924 if ( 0 == extHandle )
3926 return NULL;
3929 GEOSContextHandleInternal_t *handle = 0;
3930 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
3931 if ( 0 == handle->initialized )
3933 return NULL;
3938 const GeometryFactory *gf = handle->geomFactory;
3939 return gf->getCoordinateSequenceFactory()->create(size, dims);
3941 catch (const std::exception &e)
3943 handle->ERROR_MESSAGE("%s", e.what());
3945 catch (...)
3947 handle->ERROR_MESSAGE("Unknown exception thrown");
3950 return NULL;
3954 GEOSCoordSeq_setOrdinate_r(GEOSContextHandle_t extHandle, CoordinateSequence *cs,
3955 unsigned int idx, unsigned int dim, double val)
3957 assert(0 != cs);
3958 if ( 0 == extHandle )
3960 return 0;
3963 GEOSContextHandleInternal_t *handle = 0;
3964 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
3965 if ( 0 == handle->initialized )
3967 return 0;
3972 cs->setOrdinate(idx, dim, val);
3973 return 1;
3975 catch (const std::exception &e)
3977 handle->ERROR_MESSAGE("%s", e.what());
3979 catch (...)
3981 handle->ERROR_MESSAGE("Unknown exception thrown");
3984 return 0;
3988 GEOSCoordSeq_setX_r(GEOSContextHandle_t extHandle, CoordinateSequence *s, unsigned int idx, double val)
3990 return GEOSCoordSeq_setOrdinate_r(extHandle, s, idx, 0, val);
3994 GEOSCoordSeq_setY_r(GEOSContextHandle_t extHandle, CoordinateSequence *s, unsigned int idx, double val)
3996 return GEOSCoordSeq_setOrdinate_r(extHandle, s, idx, 1, val);
4000 GEOSCoordSeq_setZ_r(GEOSContextHandle_t extHandle, CoordinateSequence *s, unsigned int idx, double val)
4002 return GEOSCoordSeq_setOrdinate_r(extHandle, s, idx, 2, val);
4005 CoordinateSequence *
4006 GEOSCoordSeq_clone_r(GEOSContextHandle_t extHandle, const CoordinateSequence *cs)
4008 assert(0 != cs);
4010 if ( 0 == extHandle )
4012 return NULL;
4015 GEOSContextHandleInternal_t *handle = 0;
4016 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
4017 if ( 0 == handle->initialized )
4019 return NULL;
4024 return cs->clone();
4026 catch (const std::exception &e)
4028 handle->ERROR_MESSAGE("%s", e.what());
4030 catch (...)
4032 handle->ERROR_MESSAGE("Unknown exception thrown");
4035 return NULL;
4039 GEOSCoordSeq_getOrdinate_r(GEOSContextHandle_t extHandle, const CoordinateSequence *cs,
4040 unsigned int idx, unsigned int dim, double *val)
4042 assert(0 != cs);
4043 assert(0 != val);
4045 if ( 0 == extHandle )
4047 return 0;
4050 GEOSContextHandleInternal_t *handle = 0;
4051 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
4052 if ( 0 == handle->initialized )
4054 return 0;
4059 double d = cs->getOrdinate(idx, dim);
4060 *val = d;
4062 return 1;
4064 catch (const std::exception &e)
4066 handle->ERROR_MESSAGE("%s", e.what());
4068 catch (...)
4070 handle->ERROR_MESSAGE("Unknown exception thrown");
4073 return 0;
4077 GEOSCoordSeq_getX_r(GEOSContextHandle_t extHandle, const CoordinateSequence *s, unsigned int idx, double *val)
4079 return GEOSCoordSeq_getOrdinate_r(extHandle, s, idx, 0, val);
4083 GEOSCoordSeq_getY_r(GEOSContextHandle_t extHandle, const CoordinateSequence *s, unsigned int idx, double *val)
4085 return GEOSCoordSeq_getOrdinate_r(extHandle, s, idx, 1, val);
4089 GEOSCoordSeq_getZ_r(GEOSContextHandle_t extHandle, const CoordinateSequence *s, unsigned int idx, double *val)
4091 return GEOSCoordSeq_getOrdinate_r(extHandle, s, idx, 2, val);
4095 GEOSCoordSeq_getSize_r(GEOSContextHandle_t extHandle, const CoordinateSequence *cs, unsigned int *size)
4097 assert(0 != cs);
4098 assert(0 != size);
4100 if ( 0 == extHandle )
4102 return 0;
4105 GEOSContextHandleInternal_t *handle = 0;
4106 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
4107 if ( 0 == handle->initialized )
4109 return 0;
4114 const std::size_t sz = cs->getSize();
4115 *size = static_cast<unsigned int>(sz);
4116 return 1;
4118 catch (const std::exception &e)
4120 handle->ERROR_MESSAGE("%s", e.what());
4122 catch (...)
4124 handle->ERROR_MESSAGE("Unknown exception thrown");
4127 return 0;
4131 GEOSCoordSeq_getDimensions_r(GEOSContextHandle_t extHandle, const CoordinateSequence *cs, unsigned int *dims)
4133 assert(0 != cs);
4134 assert(0 != dims);
4136 if ( 0 == extHandle )
4138 return 0;
4141 GEOSContextHandleInternal_t *handle = 0;
4142 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
4143 if ( 0 == handle->initialized )
4145 return 0;
4150 const std::size_t dim = cs->getDimension();
4151 *dims = static_cast<unsigned int>(dim);
4153 return 1;
4155 catch (const std::exception &e)
4157 handle->ERROR_MESSAGE("%s", e.what());
4160 catch (...)
4162 handle->ERROR_MESSAGE("Unknown exception thrown");
4165 return 0;
4168 void
4169 GEOSCoordSeq_destroy_r(GEOSContextHandle_t extHandle, CoordinateSequence *s)
4171 GEOSContextHandleInternal_t *handle = 0;
4175 delete s;
4177 catch (const std::exception &e)
4179 if ( 0 == extHandle )
4181 return;
4184 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
4185 if ( 0 == handle->initialized )
4187 return;
4190 handle->ERROR_MESSAGE("%s", e.what());
4192 catch (...)
4194 if ( 0 == extHandle )
4196 return;
4199 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
4200 if ( 0 == handle->initialized )
4202 return;
4205 handle->ERROR_MESSAGE("Unknown exception thrown");
4209 const CoordinateSequence *
4210 GEOSGeom_getCoordSeq_r(GEOSContextHandle_t extHandle, const Geometry *g)
4212 if ( 0 == extHandle )
4214 return 0;
4217 GEOSContextHandleInternal_t *handle = 0;
4218 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
4219 if ( 0 == handle->initialized )
4221 return 0;
4226 using geos::geom::Point;
4228 const LineString *ls = dynamic_cast<const LineString *>(g);
4229 if ( ls )
4231 return ls->getCoordinatesRO();
4234 const Point *p = dynamic_cast<const Point *>(g);
4235 if ( p )
4237 return p->getCoordinatesRO();
4240 handle->ERROR_MESSAGE("Geometry must be a Point or LineString");
4241 return 0;
4243 catch (const std::exception &e)
4245 handle->ERROR_MESSAGE("%s", e.what());
4247 catch (...)
4249 handle->ERROR_MESSAGE("Unknown exception thrown");
4252 return 0;
4255 Geometry *
4256 GEOSGeom_createEmptyPoint_r(GEOSContextHandle_t extHandle)
4258 if ( 0 == extHandle )
4260 return NULL;
4263 GEOSContextHandleInternal_t *handle = 0;
4264 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
4265 if ( 0 == handle->initialized )
4267 return NULL;
4272 const GeometryFactory *gf = handle->geomFactory;
4273 return gf->createPoint();
4275 catch (const std::exception &e)
4277 handle->ERROR_MESSAGE("%s", e.what());
4279 catch (...)
4281 handle->ERROR_MESSAGE("Unknown exception thrown");
4284 return NULL;
4287 Geometry *
4288 GEOSGeom_createPoint_r(GEOSContextHandle_t extHandle, CoordinateSequence *cs)
4290 if ( 0 == extHandle )
4292 return 0;
4295 GEOSContextHandleInternal_t *handle = 0;
4296 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
4297 if ( 0 == handle->initialized )
4299 return 0;
4304 const GeometryFactory *gf = handle->geomFactory;
4305 return gf->createPoint(cs);
4307 catch (const std::exception &e)
4309 handle->ERROR_MESSAGE("%s", e.what());
4311 catch (...)
4313 handle->ERROR_MESSAGE("Unknown exception thrown");
4316 return 0;
4319 Geometry *
4320 GEOSGeom_createLinearRing_r(GEOSContextHandle_t extHandle, CoordinateSequence *cs)
4322 if ( 0 == extHandle )
4324 return NULL;
4327 GEOSContextHandleInternal_t *handle = 0;
4328 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
4329 if ( 0 == handle->initialized )
4331 return NULL;
4336 const GeometryFactory *gf = handle->geomFactory;
4338 return gf->createLinearRing(cs);
4340 catch (const std::exception &e)
4342 handle->ERROR_MESSAGE("%s", e.what());
4344 catch (...)
4346 handle->ERROR_MESSAGE("Unknown exception thrown");
4349 return NULL;
4352 Geometry *
4353 GEOSGeom_createEmptyLineString_r(GEOSContextHandle_t extHandle)
4355 if ( 0 == extHandle )
4357 return NULL;
4360 GEOSContextHandleInternal_t *handle = 0;
4361 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
4362 if ( 0 == handle->initialized )
4364 return NULL;
4369 const GeometryFactory *gf = handle->geomFactory;
4371 return gf->createLineString();
4373 catch (const std::exception &e)
4375 handle->ERROR_MESSAGE("%s", e.what());
4377 catch (...)
4379 handle->ERROR_MESSAGE("Unknown exception thrown");
4382 return NULL;
4385 Geometry *
4386 GEOSGeom_createLineString_r(GEOSContextHandle_t extHandle, CoordinateSequence *cs)
4388 if ( 0 == extHandle )
4390 return NULL;
4393 GEOSContextHandleInternal_t *handle = 0;
4394 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
4395 if ( 0 == handle->initialized )
4397 return NULL;
4402 const GeometryFactory *gf = handle->geomFactory;
4404 return gf->createLineString(cs);
4406 catch (const std::exception &e)
4408 handle->ERROR_MESSAGE("%s", e.what());
4410 catch (...)
4412 handle->ERROR_MESSAGE("Unknown exception thrown");
4415 return NULL;
4418 Geometry *
4419 GEOSGeom_createEmptyPolygon_r(GEOSContextHandle_t extHandle)
4421 if ( 0 == extHandle )
4423 return NULL;
4426 GEOSContextHandleInternal_t *handle = 0;
4427 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
4428 if ( 0 == handle->initialized )
4430 return NULL;
4435 const GeometryFactory *gf = handle->geomFactory;
4436 return gf->createPolygon();
4438 catch (const std::exception &e)
4440 handle->ERROR_MESSAGE("%s", e.what());
4442 catch (...)
4444 handle->ERROR_MESSAGE("Unknown exception thrown");
4447 return NULL;
4450 Geometry *
4451 GEOSGeom_createPolygon_r(GEOSContextHandle_t extHandle, Geometry *shell, Geometry **holes, unsigned int nholes)
4453 // FIXME: holes must be non-nullptr or may be nullptr?
4454 //assert(0 != holes);
4456 if ( 0 == extHandle )
4458 return NULL;
4461 GEOSContextHandleInternal_t *handle = 0;
4462 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
4463 if ( 0 == handle->initialized )
4465 return NULL;
4470 using geos::geom::LinearRing;
4472 std::vector<Geometry *> *vholes = new std::vector<Geometry *>(holes, holes + nholes);
4474 LinearRing *nshell = dynamic_cast<LinearRing *>(shell);
4475 if ( ! nshell )
4477 handle->ERROR_MESSAGE("Shell is not a LinearRing");
4478 delete vholes;
4479 return NULL;
4481 const GeometryFactory *gf = handle->geomFactory;
4483 return gf->createPolygon(nshell, vholes);
4485 catch (const std::exception &e)
4487 handle->ERROR_MESSAGE("%s", e.what());
4489 catch (...)
4491 handle->ERROR_MESSAGE("Unknown exception thrown");
4494 return NULL;
4497 Geometry *
4498 GEOSGeom_clone_r(GEOSContextHandle_t extHandle, const Geometry *g)
4500 assert(0 != g);
4502 if ( 0 == extHandle )
4504 return NULL;
4507 GEOSContextHandleInternal_t *handle = 0;
4508 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
4509 if ( 0 == handle->initialized )
4511 return NULL;
4516 return g->clone();
4518 catch (const std::exception &e)
4520 handle->ERROR_MESSAGE("%s", e.what());
4522 catch (...)
4524 handle->ERROR_MESSAGE("Unknown exception thrown");
4527 return NULL;
4530 GEOSGeometry *
4531 GEOSGeom_setPrecision_r(GEOSContextHandle_t extHandle, const GEOSGeometry *g,
4532 double gridSize, int flags)
4534 using namespace geos::geom;
4536 assert(0 != g);
4538 if ( 0 == extHandle )
4540 return NULL;
4543 GEOSContextHandleInternal_t *handle = 0;
4544 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
4545 if ( 0 == handle->initialized )
4547 return NULL;
4552 const PrecisionModel *pm = g->getPrecisionModel();
4553 double cursize = pm->isFloating() ? 0 : 1.0/pm->getScale();
4554 std::auto_ptr<PrecisionModel> newpm;
4555 if ( gridSize ) newpm.reset( new PrecisionModel(1.0/gridSize) );
4556 else newpm.reset( new PrecisionModel() );
4557 GeometryFactory::unique_ptr gf =
4558 GeometryFactory::create( newpm.get(), g->getSRID() );
4559 Geometry *ret;
4560 if ( gridSize && cursize != gridSize )
4562 // We need to snap the geometry
4563 GeometryPrecisionReducer reducer( *gf );
4564 reducer.setPointwise( flags & GEOS_PREC_NO_TOPO );
4565 reducer.setRemoveCollapsedComponents( ! (flags & GEOS_PREC_KEEP_COLLAPSED) );
4566 ret = reducer.reduce( *g ).release();
4568 else
4570 // No need or willing to snap, just change the factory
4571 ret = gf->createGeometry(g);
4573 return ret;
4575 catch (const std::exception &e)
4577 handle->ERROR_MESSAGE("%s", e.what());
4579 catch (...)
4581 handle->ERROR_MESSAGE("Unknown exception thrown");
4584 return NULL;
4587 double
4588 GEOSGeom_getPrecision_r(GEOSContextHandle_t extHandle, const GEOSGeometry *g)
4590 using namespace geos::geom;
4592 assert(0 != g);
4594 if ( 0 == extHandle )
4596 return -1;
4599 GEOSContextHandleInternal_t *handle = 0;
4600 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
4601 if ( 0 == handle->initialized )
4603 return -1;
4608 const PrecisionModel *pm = g->getPrecisionModel();
4609 double cursize = pm->isFloating() ? 0 : 1.0/pm->getScale();
4610 return cursize;
4612 catch (const std::exception &e)
4614 handle->ERROR_MESSAGE("%s", e.what());
4616 catch (...)
4618 handle->ERROR_MESSAGE("Unknown exception thrown");
4621 return -1;
4625 GEOSGeom_getDimensions_r(GEOSContextHandle_t extHandle, const Geometry *g)
4627 if ( 0 == extHandle )
4629 return 0;
4632 GEOSContextHandleInternal_t *handle = 0;
4633 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
4634 if ( 0 == handle->initialized )
4636 return 0;
4641 return (int) g->getDimension();
4643 catch (const std::exception &e)
4645 handle->ERROR_MESSAGE("%s", e.what());
4647 catch (...)
4649 handle->ERROR_MESSAGE("Unknown exception thrown");
4652 return 0;
4656 GEOSGeom_getCoordinateDimension_r(GEOSContextHandle_t extHandle, const Geometry *g)
4658 if ( 0 == extHandle )
4660 return 0;
4663 GEOSContextHandleInternal_t *handle = 0;
4664 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
4665 if ( 0 == handle->initialized )
4667 return 0;
4672 return g->getCoordinateDimension();
4674 catch (const std::exception &e)
4676 handle->ERROR_MESSAGE("%s", e.what());
4678 catch (...)
4680 handle->ERROR_MESSAGE("Unknown exception thrown");
4683 return 0;
4686 Geometry *
4687 GEOSSimplify_r(GEOSContextHandle_t extHandle, const Geometry *g1, double tolerance)
4689 if ( 0 == extHandle )
4691 return NULL;
4694 GEOSContextHandleInternal_t *handle = 0;
4695 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
4696 if ( 0 == handle->initialized )
4698 return NULL;
4703 using namespace geos::simplify;
4704 Geometry::AutoPtr g(DouglasPeuckerSimplifier::simplify(g1, tolerance));
4705 return g.release();
4707 catch (const std::exception &e)
4709 handle->ERROR_MESSAGE("%s", e.what());
4711 catch (...)
4713 handle->ERROR_MESSAGE("Unknown exception thrown");
4716 return NULL;
4719 Geometry *
4720 GEOSTopologyPreserveSimplify_r(GEOSContextHandle_t extHandle, const Geometry *g1, double tolerance)
4722 if ( 0 == extHandle )
4724 return NULL;
4727 GEOSContextHandleInternal_t *handle = 0;
4728 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
4729 if ( 0 == handle->initialized )
4731 return NULL;
4736 using namespace geos::simplify;
4737 Geometry::AutoPtr g(TopologyPreservingSimplifier::simplify(g1, tolerance));
4738 return g.release();
4740 catch (const std::exception &e)
4742 handle->ERROR_MESSAGE("%s", e.what());
4744 catch (...)
4746 handle->ERROR_MESSAGE("Unknown exception thrown");
4749 return NULL;
4753 /* WKT Reader */
4754 WKTReader *
4755 GEOSWKTReader_create_r(GEOSContextHandle_t extHandle)
4757 if ( 0 == extHandle )
4759 return NULL;
4762 GEOSContextHandleInternal_t *handle = 0;
4763 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
4764 if ( 0 == handle->initialized )
4766 return NULL;
4771 using geos::io::WKTReader;
4772 return new WKTReader((GeometryFactory*)handle->geomFactory);
4774 catch (const std::exception &e)
4776 handle->ERROR_MESSAGE("%s", e.what());
4778 catch (...)
4780 handle->ERROR_MESSAGE("Unknown exception thrown");
4783 return NULL;
4786 void
4787 GEOSWKTReader_destroy_r(GEOSContextHandle_t extHandle, WKTReader *reader)
4789 GEOSContextHandleInternal_t *handle = 0;
4793 delete reader;
4795 catch (const std::exception &e)
4797 if ( 0 == extHandle )
4799 return;
4802 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
4803 if ( 0 == handle->initialized )
4805 return;
4808 handle->ERROR_MESSAGE("%s", e.what());
4810 catch (...)
4812 if ( 0 == extHandle )
4814 return;
4817 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
4818 if ( 0 == handle->initialized )
4820 return;
4823 handle->ERROR_MESSAGE("Unknown exception thrown");
4828 Geometry*
4829 GEOSWKTReader_read_r(GEOSContextHandle_t extHandle, WKTReader *reader, const char *wkt)
4831 assert(0 != reader);
4833 if ( 0 == extHandle )
4835 return 0;
4838 GEOSContextHandleInternal_t *handle = 0;
4839 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
4840 if ( 0 == handle->initialized )
4842 return 0;
4847 const std::string wktstring(wkt);
4848 Geometry *g = reader->read(wktstring);
4849 return g;
4851 catch (const std::exception &e)
4853 handle->ERROR_MESSAGE("%s", e.what());
4855 catch (...)
4857 handle->ERROR_MESSAGE("Unknown exception thrown");
4860 return 0;
4863 /* WKT Writer */
4864 WKTWriter *
4865 GEOSWKTWriter_create_r(GEOSContextHandle_t extHandle)
4867 if ( 0 == extHandle )
4869 return 0;
4872 GEOSContextHandleInternal_t *handle = 0;
4873 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
4874 if ( 0 == handle->initialized )
4876 return 0;
4881 using geos::io::WKTWriter;
4882 return new WKTWriter();
4884 catch (const std::exception &e)
4886 handle->ERROR_MESSAGE("%s", e.what());
4888 catch (...)
4890 handle->ERROR_MESSAGE("Unknown exception thrown");
4893 return 0;
4896 void
4897 GEOSWKTWriter_destroy_r(GEOSContextHandle_t extHandle, WKTWriter *Writer)
4900 GEOSContextHandleInternal_t *handle = 0;
4904 delete Writer;
4906 catch (const std::exception &e)
4908 if ( 0 == extHandle )
4910 return;
4913 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
4914 if ( 0 == handle->initialized )
4916 return;
4919 handle->ERROR_MESSAGE("%s", e.what());
4921 catch (...)
4923 if ( 0 == extHandle )
4925 return;
4928 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
4929 if ( 0 == handle->initialized )
4931 return;
4934 handle->ERROR_MESSAGE("Unknown exception thrown");
4939 char*
4940 GEOSWKTWriter_write_r(GEOSContextHandle_t extHandle, WKTWriter *writer, const Geometry *geom)
4942 assert(0 != writer);
4944 if ( 0 == extHandle )
4946 return NULL;
4949 GEOSContextHandleInternal_t *handle = 0;
4950 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
4951 if ( 0 == handle->initialized )
4953 return NULL;
4958 std::string sgeom(writer->write(geom));
4959 char *result = gstrdup(sgeom);
4960 return result;
4962 catch (const std::exception &e)
4964 handle->ERROR_MESSAGE("%s", e.what());
4966 catch (...)
4968 handle->ERROR_MESSAGE("Unknown exception thrown");
4971 return NULL;
4974 void
4975 GEOSWKTWriter_setTrim_r(GEOSContextHandle_t extHandle, WKTWriter *writer, char trim)
4977 assert(0 != writer);
4979 if ( 0 == extHandle )
4981 return;
4984 GEOSContextHandleInternal_t *handle = 0;
4985 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
4986 if ( 0 == handle->initialized )
4988 return;
4991 writer->setTrim(0 != trim);
4994 void
4995 GEOSWKTWriter_setRoundingPrecision_r(GEOSContextHandle_t extHandle, WKTWriter *writer, int precision)
4997 assert(0 != writer);
4999 if ( 0 == extHandle )
5001 return;
5004 GEOSContextHandleInternal_t *handle = 0;
5005 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
5006 if ( 0 == handle->initialized )
5008 return;
5011 writer->setRoundingPrecision(precision);
5014 void
5015 GEOSWKTWriter_setOutputDimension_r(GEOSContextHandle_t extHandle, WKTWriter *writer, int dim)
5017 assert(0 != writer);
5019 if ( 0 == extHandle )
5021 return;
5024 GEOSContextHandleInternal_t *handle = 0;
5025 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
5026 if ( 0 == handle->initialized )
5028 return;
5033 writer->setOutputDimension(dim);
5035 catch (const std::exception &e)
5037 handle->ERROR_MESSAGE("%s", e.what());
5039 catch (...)
5041 handle->ERROR_MESSAGE("Unknown exception thrown");
5046 GEOSWKTWriter_getOutputDimension_r(GEOSContextHandle_t extHandle, WKTWriter *writer)
5048 assert(0 != writer);
5050 if ( 0 == extHandle )
5052 return -1;
5055 GEOSContextHandleInternal_t *handle = 0;
5056 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
5057 if ( 0 == handle->initialized )
5059 return -1;
5062 int dim = -1;
5066 dim = writer->getOutputDimension();
5068 catch (const std::exception &e)
5070 handle->ERROR_MESSAGE("%s", e.what());
5072 catch (...)
5074 handle->ERROR_MESSAGE("Unknown exception thrown");
5077 return dim;
5080 void
5081 GEOSWKTWriter_setOld3D_r(GEOSContextHandle_t extHandle, WKTWriter *writer, int useOld3D)
5083 assert(0 != writer);
5085 if ( 0 == extHandle )
5087 return;
5090 GEOSContextHandleInternal_t *handle = 0;
5091 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
5092 if ( 0 == handle->initialized )
5094 return;
5097 writer->setOld3D(0 != useOld3D);
5100 /* WKB Reader */
5101 WKBReader *
5102 GEOSWKBReader_create_r(GEOSContextHandle_t extHandle)
5104 if ( 0 == extHandle )
5106 return NULL;
5109 GEOSContextHandleInternal_t *handle = 0;
5110 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
5111 if ( 0 == handle->initialized )
5113 return NULL;
5116 using geos::io::WKBReader;
5119 return new WKBReader(*(GeometryFactory*)handle->geomFactory);
5121 catch (const std::exception &e)
5123 handle->ERROR_MESSAGE("%s", e.what());
5125 catch (...)
5127 handle->ERROR_MESSAGE("Unknown exception thrown");
5130 return NULL;
5133 void
5134 GEOSWKBReader_destroy_r(GEOSContextHandle_t extHandle, WKBReader *reader)
5136 GEOSContextHandleInternal_t *handle = 0;
5140 delete reader;
5142 catch (const std::exception &e)
5144 if ( 0 == extHandle )
5146 return;
5149 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
5150 if ( 0 == handle->initialized )
5152 return;
5155 handle->ERROR_MESSAGE("%s", e.what());
5157 catch (...)
5159 if ( 0 == extHandle )
5161 return;
5164 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
5165 if ( 0 == handle->initialized )
5167 return;
5170 handle->ERROR_MESSAGE("Unknown exception thrown");
5174 struct membuf : public std::streambuf
5176 membuf(char* s, std::size_t n)
5178 setg(s, s, s + n);
5182 Geometry*
5183 GEOSWKBReader_read_r(GEOSContextHandle_t extHandle, WKBReader *reader, const unsigned char *wkb, size_t size)
5185 assert(0 != reader);
5186 assert(0 != wkb);
5188 if ( 0 == extHandle )
5190 return 0;
5193 GEOSContextHandleInternal_t *handle = 0;
5194 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
5195 if ( 0 == handle->initialized )
5197 return 0;
5202 //std::string wkbstring(reinterpret_cast<const char*>(wkb), size); // make it binary !
5203 //std::istringstream is(std::ios_base::binary);
5204 //is.str(wkbstring);
5205 //is.seekg(0, std::ios::beg); // rewind reader pointer
5207 // http://stackoverflow.com/questions/2079912/simpler-way-to-create-a-c-memorystream-from-char-size-t-without-copying-t
5208 membuf mb((char*)wkb, size);
5209 istream is(&mb);
5211 Geometry *g = reader->read(is);
5212 return g;
5214 catch (const std::exception &e)
5216 handle->ERROR_MESSAGE("%s", e.what());
5218 catch (...)
5220 handle->ERROR_MESSAGE("Unknown exception thrown");
5223 return 0;
5226 Geometry*
5227 GEOSWKBReader_readHEX_r(GEOSContextHandle_t extHandle, WKBReader *reader, const unsigned char *hex, size_t size)
5229 assert(0 != reader);
5230 assert(0 != hex);
5232 if ( 0 == extHandle )
5234 return 0;
5237 GEOSContextHandleInternal_t *handle = 0;
5238 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
5239 if ( 0 == handle->initialized )
5241 return 0;
5246 std::string hexstring(reinterpret_cast<const char*>(hex), size);
5247 std::istringstream is(std::ios_base::binary);
5248 is.str(hexstring);
5249 is.seekg(0, std::ios::beg); // rewind reader pointer
5251 Geometry *g = reader->readHEX(is);
5252 return g;
5254 catch (const std::exception &e)
5256 handle->ERROR_MESSAGE("%s", e.what());
5258 catch (...)
5260 handle->ERROR_MESSAGE("Unknown exception thrown");
5263 return 0;
5266 /* WKB Writer */
5267 WKBWriter *
5268 GEOSWKBWriter_create_r(GEOSContextHandle_t extHandle)
5270 if ( 0 == extHandle )
5272 return NULL;
5275 GEOSContextHandleInternal_t *handle = 0;
5276 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
5277 if ( 0 == handle->initialized )
5279 return NULL;
5284 using geos::io::WKBWriter;
5285 return new WKBWriter();
5287 catch (const std::exception &e)
5289 handle->ERROR_MESSAGE("%s", e.what());
5291 catch (...)
5293 handle->ERROR_MESSAGE("Unknown exception thrown");
5296 return NULL;
5299 void
5300 GEOSWKBWriter_destroy_r(GEOSContextHandle_t extHandle, WKBWriter *Writer)
5302 GEOSContextHandleInternal_t *handle = 0;
5306 delete Writer;
5308 catch (const std::exception &e)
5310 if ( 0 == extHandle )
5312 return;
5315 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
5316 if ( 0 == handle->initialized )
5318 return;
5321 handle->ERROR_MESSAGE("%s", e.what());
5323 catch (...)
5325 if ( 0 == extHandle )
5327 return;
5330 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
5331 if ( 0 == handle->initialized )
5333 return;
5336 handle->ERROR_MESSAGE("Unknown exception thrown");
5341 /* The caller owns the result */
5342 unsigned char*
5343 GEOSWKBWriter_write_r(GEOSContextHandle_t extHandle, WKBWriter *writer, const Geometry *geom, size_t *size)
5345 assert(0 != writer);
5346 assert(0 != geom);
5347 assert(0 != size);
5349 if ( 0 == extHandle )
5351 return NULL;
5354 GEOSContextHandleInternal_t *handle = 0;
5355 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
5356 if ( 0 == handle->initialized )
5358 return NULL;
5363 std::ostringstream os(std::ios_base::binary);
5364 writer->write(*geom, os);
5366 const std::string& wkbstring = os.str();
5367 const std::size_t len = wkbstring.length();
5369 unsigned char *result = NULL;
5370 result = (unsigned char*) malloc(len);
5371 std::memcpy(result, wkbstring.c_str(), len);
5372 *size = len;
5373 return result;
5375 catch (const std::exception &e)
5377 handle->ERROR_MESSAGE("%s", e.what());
5379 catch (...)
5381 handle->ERROR_MESSAGE("Unknown exception thrown");
5383 return NULL;
5386 /* The caller owns the result */
5387 unsigned char*
5388 GEOSWKBWriter_writeHEX_r(GEOSContextHandle_t extHandle, WKBWriter *writer, const Geometry *geom, size_t *size)
5390 assert(0 != writer);
5391 assert(0 != geom);
5392 assert(0 != size);
5394 if ( 0 == extHandle )
5396 return NULL;
5399 GEOSContextHandleInternal_t *handle = 0;
5400 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
5401 if ( 0 == handle->initialized )
5403 return NULL;
5408 std::ostringstream os(std::ios_base::binary);
5409 writer->writeHEX(*geom, os);
5410 std::string wkbstring(os.str());
5411 const std::size_t len = wkbstring.length();
5413 unsigned char *result = NULL;
5414 result = (unsigned char*) malloc(len);
5415 std::memcpy(result, wkbstring.c_str(), len);
5416 *size = len;
5417 return result;
5419 catch (const std::exception &e)
5421 handle->ERROR_MESSAGE("%s", e.what());
5423 catch (...)
5425 handle->ERROR_MESSAGE("Unknown exception thrown");
5428 return NULL;
5432 GEOSWKBWriter_getOutputDimension_r(GEOSContextHandle_t extHandle, const GEOSWKBWriter* writer)
5434 assert(0 != writer);
5436 if ( 0 == extHandle )
5438 return 0;
5441 int ret = 0;
5443 GEOSContextHandleInternal_t *handle = 0;
5444 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
5445 if ( 0 != handle->initialized )
5449 ret = writer->getOutputDimension();
5451 catch (...)
5453 handle->ERROR_MESSAGE("Unknown exception thrown");
5457 return ret;
5460 void
5461 GEOSWKBWriter_setOutputDimension_r(GEOSContextHandle_t extHandle, GEOSWKBWriter* writer, int newDimension)
5463 assert(0 != writer);
5465 if ( 0 == extHandle )
5467 return;
5470 GEOSContextHandleInternal_t *handle = 0;
5471 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
5472 if ( 0 != handle->initialized )
5476 writer->setOutputDimension(newDimension);
5478 catch (const std::exception &e)
5480 handle->ERROR_MESSAGE("%s", e.what());
5482 catch (...)
5484 handle->ERROR_MESSAGE("Unknown exception thrown");
5490 GEOSWKBWriter_getByteOrder_r(GEOSContextHandle_t extHandle, const GEOSWKBWriter* writer)
5492 assert(0 != writer);
5494 if ( 0 == extHandle )
5496 return 0;
5499 int ret = 0;
5501 GEOSContextHandleInternal_t *handle = 0;
5502 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
5503 if ( 0 != handle->initialized )
5507 ret = writer->getByteOrder();
5510 catch (...)
5512 handle->ERROR_MESSAGE("Unknown exception thrown");
5516 return ret;
5519 void
5520 GEOSWKBWriter_setByteOrder_r(GEOSContextHandle_t extHandle, GEOSWKBWriter* writer, int newByteOrder)
5522 assert(0 != writer);
5524 if ( 0 == extHandle )
5526 return;
5529 GEOSContextHandleInternal_t *handle = 0;
5530 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
5531 if ( 0 != handle->initialized )
5535 writer->setByteOrder(newByteOrder);
5537 catch (const std::exception &e)
5539 handle->ERROR_MESSAGE("%s", e.what());
5541 catch (...)
5543 handle->ERROR_MESSAGE("Unknown exception thrown");
5548 char
5549 GEOSWKBWriter_getIncludeSRID_r(GEOSContextHandle_t extHandle, const GEOSWKBWriter* writer)
5551 assert(0 != writer);
5553 if ( 0 == extHandle )
5555 return -1;
5558 int ret = -1;
5560 GEOSContextHandleInternal_t *handle = 0;
5561 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
5562 if ( 0 != handle->initialized )
5566 int srid = writer->getIncludeSRID();
5567 ret = srid;
5569 catch (...)
5571 handle->ERROR_MESSAGE("Unknown exception thrown");
5575 return static_cast<char>(ret);
5578 void
5579 GEOSWKBWriter_setIncludeSRID_r(GEOSContextHandle_t extHandle, GEOSWKBWriter* writer, const char newIncludeSRID)
5581 assert(0 != writer);
5583 if ( 0 == extHandle )
5585 return;
5588 GEOSContextHandleInternal_t *handle = 0;
5589 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
5590 if ( 0 != handle->initialized )
5594 writer->setIncludeSRID(newIncludeSRID);
5596 catch (...)
5598 handle->ERROR_MESSAGE("Unknown exception thrown");
5604 //-----------------------------------------------------------------
5605 // Prepared Geometry
5606 //-----------------------------------------------------------------
5608 const geos::geom::prep::PreparedGeometry*
5609 GEOSPrepare_r(GEOSContextHandle_t extHandle, const Geometry *g)
5611 if ( 0 == extHandle )
5613 return 0;
5616 GEOSContextHandleInternal_t *handle = 0;
5617 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
5618 if ( 0 == handle->initialized )
5620 return 0;
5623 const geos::geom::prep::PreparedGeometry* prep = 0;
5627 prep = geos::geom::prep::PreparedGeometryFactory::prepare(g);
5629 catch (const std::exception &e)
5631 handle->ERROR_MESSAGE("%s", e.what());
5633 catch (...)
5635 handle->ERROR_MESSAGE("Unknown exception thrown");
5638 return prep;
5641 void
5642 GEOSPreparedGeom_destroy_r(GEOSContextHandle_t extHandle, const geos::geom::prep::PreparedGeometry *a)
5644 GEOSContextHandleInternal_t *handle = 0;
5648 delete a;
5650 catch (const std::exception &e)
5652 if ( 0 == extHandle )
5654 return;
5657 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
5658 if ( 0 == handle->initialized )
5660 return;
5663 handle->ERROR_MESSAGE("%s", e.what());
5665 catch (...)
5667 if ( 0 == extHandle )
5669 return;
5672 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
5673 if ( 0 == handle->initialized )
5675 return;
5678 handle->ERROR_MESSAGE("Unknown exception thrown");
5682 char
5683 GEOSPreparedContains_r(GEOSContextHandle_t extHandle,
5684 const geos::geom::prep::PreparedGeometry *pg, const Geometry *g)
5686 assert(0 != pg);
5687 assert(0 != g);
5689 if ( 0 == extHandle )
5691 return 2;
5694 GEOSContextHandleInternal_t *handle = 0;
5695 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
5696 if ( 0 == handle->initialized )
5698 return 2;
5703 bool result = pg->contains(g);
5704 return result;
5706 catch (const std::exception &e)
5708 handle->ERROR_MESSAGE("%s", e.what());
5710 catch (...)
5712 handle->ERROR_MESSAGE("Unknown exception thrown");
5715 return 2;
5718 char
5719 GEOSPreparedContainsProperly_r(GEOSContextHandle_t extHandle,
5720 const geos::geom::prep::PreparedGeometry *pg, const Geometry *g)
5722 assert(0 != pg);
5723 assert(0 != g);
5725 if ( 0 == extHandle )
5727 return 2;
5730 GEOSContextHandleInternal_t *handle = 0;
5731 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
5732 if ( 0 == handle->initialized )
5734 return 2;
5739 bool result = pg->containsProperly(g);
5740 return result;
5742 catch (const std::exception &e)
5744 handle->ERROR_MESSAGE("%s", e.what());
5746 catch (...)
5748 handle->ERROR_MESSAGE("Unknown exception thrown");
5751 return 2;
5754 char
5755 GEOSPreparedCoveredBy_r(GEOSContextHandle_t extHandle,
5756 const geos::geom::prep::PreparedGeometry *pg, const Geometry *g)
5758 assert(0 != pg);
5759 assert(0 != g);
5761 if ( 0 == extHandle )
5763 return 2;
5766 GEOSContextHandleInternal_t *handle = 0;
5767 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
5768 if ( 0 == handle->initialized )
5770 return 2;
5775 bool result = pg->coveredBy(g);
5776 return result;
5778 catch (const std::exception &e)
5780 handle->ERROR_MESSAGE("%s", e.what());
5782 catch (...)
5784 handle->ERROR_MESSAGE("Unknown exception thrown");
5787 return 2;
5790 char
5791 GEOSPreparedCovers_r(GEOSContextHandle_t extHandle,
5792 const geos::geom::prep::PreparedGeometry *pg, const Geometry *g)
5794 assert(0 != pg);
5795 assert(0 != g);
5797 if ( 0 == extHandle )
5799 return 2;
5802 GEOSContextHandleInternal_t *handle = 0;
5803 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
5804 if ( 0 == handle->initialized )
5806 return 2;
5811 bool result = pg->covers(g);
5812 return result;
5814 catch (const std::exception &e)
5816 handle->ERROR_MESSAGE("%s", e.what());
5818 catch (...)
5820 handle->ERROR_MESSAGE("Unknown exception thrown");
5823 return 2;
5826 char
5827 GEOSPreparedCrosses_r(GEOSContextHandle_t extHandle,
5828 const geos::geom::prep::PreparedGeometry *pg, const Geometry *g)
5830 assert(0 != pg);
5831 assert(0 != g);
5833 if ( 0 == extHandle )
5835 return 2;
5838 GEOSContextHandleInternal_t *handle = 0;
5839 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
5840 if ( 0 == handle->initialized )
5842 return 2;
5847 bool result = pg->crosses(g);
5848 return result;
5850 catch (const std::exception &e)
5852 handle->ERROR_MESSAGE("%s", e.what());
5854 catch (...)
5856 handle->ERROR_MESSAGE("Unknown exception thrown");
5859 return 2;
5862 char
5863 GEOSPreparedDisjoint_r(GEOSContextHandle_t extHandle,
5864 const geos::geom::prep::PreparedGeometry *pg, const Geometry *g)
5866 assert(0 != pg);
5867 assert(0 != g);
5869 if ( 0 == extHandle )
5871 return 2;
5874 GEOSContextHandleInternal_t *handle = 0;
5875 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
5876 if ( 0 == handle->initialized )
5878 return 2;
5883 bool result = pg->disjoint(g);
5884 return result;
5886 catch (const std::exception &e)
5888 handle->ERROR_MESSAGE("%s", e.what());
5890 catch (...)
5892 handle->ERROR_MESSAGE("Unknown exception thrown");
5895 return 2;
5898 char
5899 GEOSPreparedIntersects_r(GEOSContextHandle_t extHandle,
5900 const geos::geom::prep::PreparedGeometry *pg, const Geometry *g)
5902 assert(0 != pg);
5903 assert(0 != g);
5905 if ( 0 == extHandle )
5907 return 2;
5910 GEOSContextHandleInternal_t *handle = 0;
5911 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
5912 if ( 0 == handle->initialized )
5914 return 2;
5919 bool result = pg->intersects(g);
5920 return result;
5922 catch (const std::exception &e)
5924 handle->ERROR_MESSAGE("%s", e.what());
5926 catch (...)
5928 handle->ERROR_MESSAGE("Unknown exception thrown");
5931 return 2;
5934 char
5935 GEOSPreparedOverlaps_r(GEOSContextHandle_t extHandle,
5936 const geos::geom::prep::PreparedGeometry *pg, const Geometry *g)
5938 assert(0 != pg);
5939 assert(0 != g);
5941 if ( 0 == extHandle )
5943 return 2;
5946 GEOSContextHandleInternal_t *handle = 0;
5947 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
5948 if ( 0 == handle->initialized )
5950 return 2;
5955 bool result = pg->overlaps(g);
5956 return result;
5958 catch (const std::exception &e)
5960 handle->ERROR_MESSAGE("%s", e.what());
5962 catch (...)
5964 handle->ERROR_MESSAGE("Unknown exception thrown");
5967 return 2;
5970 char
5971 GEOSPreparedTouches_r(GEOSContextHandle_t extHandle,
5972 const geos::geom::prep::PreparedGeometry *pg, const Geometry *g)
5974 assert(0 != pg);
5975 assert(0 != g);
5977 if ( 0 == extHandle )
5979 return 2;
5982 GEOSContextHandleInternal_t *handle = 0;
5983 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
5984 if ( 0 == handle->initialized )
5986 return 2;
5991 bool result = pg->touches(g);
5992 return result;
5994 catch (const std::exception &e)
5996 handle->ERROR_MESSAGE("%s", e.what());
5998 catch (...)
6000 handle->ERROR_MESSAGE("Unknown exception thrown");
6003 return 2;
6006 char
6007 GEOSPreparedWithin_r(GEOSContextHandle_t extHandle,
6008 const geos::geom::prep::PreparedGeometry *pg, const Geometry *g)
6010 assert(0 != pg);
6011 assert(0 != g);
6013 if ( 0 == extHandle )
6015 return 2;
6018 GEOSContextHandleInternal_t *handle = 0;
6019 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
6020 if ( 0 == handle->initialized )
6022 return 2;
6027 bool result = pg->within(g);
6028 return result;
6030 catch (const std::exception &e)
6032 handle->ERROR_MESSAGE("%s", e.what());
6034 catch (...)
6036 handle->ERROR_MESSAGE("Unknown exception thrown");
6039 return 2;
6042 //-----------------------------------------------------------------
6043 // STRtree
6044 //-----------------------------------------------------------------
6046 geos::index::strtree::STRtree *
6047 GEOSSTRtree_create_r(GEOSContextHandle_t extHandle,
6048 size_t nodeCapacity)
6050 if ( 0 == extHandle )
6052 return 0;
6055 GEOSContextHandleInternal_t *handle = 0;
6056 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
6057 if ( 0 == handle->initialized )
6059 return 0;
6062 geos::index::strtree::STRtree *tree = 0;
6066 tree = new geos::index::strtree::STRtree(nodeCapacity);
6068 catch (const std::exception &e)
6070 handle->ERROR_MESSAGE("%s", e.what());
6072 catch (...)
6074 handle->ERROR_MESSAGE("Unknown exception thrown");
6077 return tree;
6080 void
6081 GEOSSTRtree_insert_r(GEOSContextHandle_t extHandle,
6082 geos::index::strtree::STRtree *tree,
6083 const geos::geom::Geometry *g,
6084 void *item)
6086 GEOSContextHandleInternal_t *handle = 0;
6087 assert(tree != 0);
6088 assert(g != 0);
6092 tree->insert(g->getEnvelopeInternal(), item);
6094 catch (const std::exception &e)
6096 if ( 0 == extHandle )
6098 return;
6101 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
6102 if ( 0 == handle->initialized )
6104 return;
6107 handle->ERROR_MESSAGE("%s", e.what());
6109 catch (...)
6111 if ( 0 == extHandle )
6113 return;
6116 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
6117 if ( 0 == handle->initialized )
6119 return;
6122 handle->ERROR_MESSAGE("Unknown exception thrown");
6126 void
6127 GEOSSTRtree_query_r(GEOSContextHandle_t extHandle,
6128 geos::index::strtree::STRtree *tree,
6129 const geos::geom::Geometry *g,
6130 GEOSQueryCallback callback,
6131 void *userdata)
6133 GEOSContextHandleInternal_t *handle = 0;
6134 assert(tree != 0);
6135 assert(g != 0);
6136 assert(callback != 0);
6140 CAPI_ItemVisitor visitor(callback, userdata);
6141 tree->query(g->getEnvelopeInternal(), visitor);
6143 catch (const std::exception &e)
6145 if ( 0 == extHandle )
6147 return;
6150 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
6151 if ( 0 == handle->initialized )
6153 return;
6156 handle->ERROR_MESSAGE("%s", e.what());
6158 catch (...)
6160 if ( 0 == extHandle )
6162 return;
6165 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
6166 if ( 0 == handle->initialized )
6168 return;
6171 handle->ERROR_MESSAGE("Unknown exception thrown");
6175 const GEOSGeometry *
6176 GEOSSTRtree_nearest_r(GEOSContextHandle_t extHandle,
6177 geos::index::strtree::STRtree *tree,
6178 const geos::geom::Geometry* geom)
6180 return (const GEOSGeometry*) GEOSSTRtree_nearest_generic_r( extHandle, tree, geom, geom->getEnvelope(), NULL, NULL);
6183 const void *
6184 GEOSSTRtree_nearest_generic_r(GEOSContextHandle_t extHandle,
6185 geos::index::strtree::STRtree *tree,
6186 const void* item,
6187 const geos::geom::Geometry* itemEnvelope,
6188 GEOSDistanceCallback distancefn,
6189 void* userdata)
6191 using namespace geos::index::strtree;
6193 GEOSContextHandleInternal_t *handle = 0;
6195 struct CustomItemDistance : public ItemDistance {
6196 CustomItemDistance(GEOSDistanceCallback p_distancefn, void* p_userdata)
6197 : m_distancefn(p_distancefn), m_userdata(p_userdata) {}
6199 GEOSDistanceCallback m_distancefn;
6200 void* m_userdata;
6202 double distance(const ItemBoundable* item1, const ItemBoundable* item2) {
6203 const void* a = item1->getItem();
6204 const void* b = item2->getItem();
6205 double d;
6207 if (!m_distancefn(a, b, &d, m_userdata)) {
6208 throw std::runtime_error(std::string("Failed to compute distance."));
6211 return d;
6217 if (distancefn) {
6218 CustomItemDistance itemDistance(distancefn, userdata);
6219 return tree->nearestNeighbour(itemEnvelope->getEnvelopeInternal(), item, &itemDistance);
6220 } else {
6221 GeometryItemDistance itemDistance = GeometryItemDistance();
6222 return tree->nearestNeighbour(itemEnvelope->getEnvelopeInternal(), item, &itemDistance);
6225 catch (const std::exception &e)
6227 if ( 0 == extHandle )
6229 return NULL;
6232 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
6233 if ( 0 == handle->initialized )
6235 return NULL;
6238 handle->ERROR_MESSAGE("%s", e.what());
6240 catch (...)
6242 if ( 0 == extHandle )
6244 return NULL;
6247 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
6248 if ( 0 == handle->initialized )
6250 return NULL;
6253 handle->ERROR_MESSAGE("Unknown exception thrown");
6256 return NULL;
6259 void
6260 GEOSSTRtree_iterate_r(GEOSContextHandle_t extHandle,
6261 geos::index::strtree::STRtree *tree,
6262 GEOSQueryCallback callback,
6263 void *userdata)
6265 GEOSContextHandleInternal_t *handle = 0;
6266 assert(tree != 0);
6267 assert(callback != 0);
6271 CAPI_ItemVisitor visitor(callback, userdata);
6272 tree->iterate(visitor);
6274 catch (const std::exception &e)
6276 if ( 0 == extHandle )
6278 return;
6281 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
6282 if ( 0 == handle->initialized )
6284 return;
6287 handle->ERROR_MESSAGE("%s", e.what());
6289 catch (...)
6291 if ( 0 == extHandle )
6293 return;
6296 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
6297 if ( 0 == handle->initialized )
6299 return;
6302 handle->ERROR_MESSAGE("Unknown exception thrown");
6306 char
6307 GEOSSTRtree_remove_r(GEOSContextHandle_t extHandle,
6308 geos::index::strtree::STRtree *tree,
6309 const geos::geom::Geometry *g,
6310 void *item)
6312 assert(0 != tree);
6313 assert(0 != g);
6315 if ( 0 == extHandle )
6317 return 2;
6320 GEOSContextHandleInternal_t *handle = 0;
6321 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
6322 if ( 0 == handle->initialized )
6324 return 2;
6329 bool result = tree->remove(g->getEnvelopeInternal(), item);
6330 return result;
6332 catch (const std::exception &e)
6334 handle->ERROR_MESSAGE("%s", e.what());
6336 catch (...)
6338 handle->ERROR_MESSAGE("Unknown exception thrown");
6341 return 2;
6344 void
6345 GEOSSTRtree_destroy_r(GEOSContextHandle_t extHandle,
6346 geos::index::strtree::STRtree *tree)
6348 GEOSContextHandleInternal_t *handle = 0;
6352 delete tree;
6354 catch (const std::exception &e)
6356 if ( 0 == extHandle )
6358 return;
6361 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
6362 if ( 0 == handle->initialized )
6364 return;
6367 handle->ERROR_MESSAGE("%s", e.what());
6369 catch (...)
6371 if ( 0 == extHandle )
6373 return;
6376 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
6377 if ( 0 == handle->initialized )
6379 return;
6382 handle->ERROR_MESSAGE("Unknown exception thrown");
6386 double
6387 GEOSProject_r(GEOSContextHandle_t extHandle,
6388 const Geometry *g,
6389 const Geometry *p)
6391 if ( 0 == extHandle ) return -1.0;
6392 GEOSContextHandleInternal_t *handle =
6393 reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
6394 if ( handle->initialized == 0 ) return -1.0;
6396 const geos::geom::Point* point = dynamic_cast<const geos::geom::Point*>(p);
6397 if (!point) {
6398 handle->ERROR_MESSAGE("third argument of GEOSProject_r must be Point*");
6399 return -1.0;
6402 const geos::geom::Coordinate* inputPt = p->getCoordinate();
6404 try {
6405 return geos::linearref::LengthIndexedLine(g).project(*inputPt);
6406 } catch (const std::exception &e) {
6407 handle->ERROR_MESSAGE("%s", e.what());
6408 return -1.0;
6409 } catch (...) {
6410 handle->ERROR_MESSAGE("Unknown exception thrown");
6411 return -1.0;
6416 Geometry*
6417 GEOSInterpolate_r(GEOSContextHandle_t extHandle, const Geometry *g, double d)
6419 if ( 0 == extHandle ) return 0;
6420 GEOSContextHandleInternal_t *handle =
6421 reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
6422 if ( handle->initialized == 0 ) return 0;
6424 try {
6425 geos::linearref::LengthIndexedLine lil(g);
6426 geos::geom::Coordinate coord = lil.extractPoint(d);
6427 const GeometryFactory *gf = handle->geomFactory;
6428 Geometry* point = gf->createPoint(coord);
6429 return point;
6430 } catch (const std::exception &e) {
6431 handle->ERROR_MESSAGE("%s", e.what());
6432 return 0;
6433 } catch (...) {
6434 handle->ERROR_MESSAGE("Unknown exception thrown");
6435 return 0;
6440 double
6441 GEOSProjectNormalized_r(GEOSContextHandle_t extHandle, const Geometry *g,
6442 const Geometry *p)
6445 double length;
6446 GEOSLength_r(extHandle, g, &length);
6447 return GEOSProject_r(extHandle, g, p) / length;
6451 Geometry*
6452 GEOSInterpolateNormalized_r(GEOSContextHandle_t extHandle, const Geometry *g,
6453 double d)
6455 double length;
6456 GEOSLength_r(extHandle, g, &length);
6457 return GEOSInterpolate_r(extHandle, g, d * length);
6460 GEOSGeometry*
6461 GEOSGeom_extractUniquePoints_r(GEOSContextHandle_t extHandle,
6462 const GEOSGeometry* g)
6464 if ( 0 == extHandle ) return 0;
6465 GEOSContextHandleInternal_t *handle = 0;
6466 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
6467 if ( handle->initialized == 0 ) return 0;
6469 using namespace geos::geom;
6470 using namespace geos::util;
6475 /* 1: extract points */
6476 std::vector<const Coordinate*> coords;
6477 UniqueCoordinateArrayFilter filter(coords);
6478 g->apply_ro(&filter);
6480 /* 2: for each point, create a geometry and put into a vector */
6481 std::vector<Geometry*>* points = new std::vector<Geometry*>();
6482 points->reserve(coords.size());
6483 const GeometryFactory* factory = g->getFactory();
6484 for (std::vector<const Coordinate*>::iterator it=coords.begin(),
6485 itE=coords.end();
6486 it != itE; ++it)
6488 Geometry* point = factory->createPoint(*(*it));
6489 points->push_back(point);
6492 /* 3: create a multipoint */
6493 return factory->createMultiPoint(points);
6496 catch (const std::exception &e)
6498 handle->ERROR_MESSAGE("%s", e.what());
6499 return 0;
6501 catch (...)
6503 handle->ERROR_MESSAGE("Unknown exception thrown");
6504 return 0;
6508 int GEOSOrientationIndex_r(GEOSContextHandle_t extHandle,
6509 double Ax, double Ay, double Bx, double By, double Px, double Py)
6511 GEOSContextHandleInternal_t *handle = 0;
6513 using geos::geom::Coordinate;
6514 using geos::algorithm::CGAlgorithms;
6516 if ( 0 == extHandle )
6518 return 2;
6521 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
6522 if ( 0 == handle->initialized )
6524 return 2;
6529 Coordinate A(Ax, Ay);
6530 Coordinate B(Bx, By);
6531 Coordinate P(Px, Py);
6532 return CGAlgorithms::orientationIndex(A, B, P);
6534 catch (const std::exception &e)
6536 handle->ERROR_MESSAGE("%s", e.what());
6537 return 2;
6539 catch (...)
6541 handle->ERROR_MESSAGE("Unknown exception thrown");
6542 return 2;
6546 GEOSGeometry *
6547 GEOSSharedPaths_r(GEOSContextHandle_t extHandle, const GEOSGeometry* g1, const GEOSGeometry* g2)
6549 using namespace geos::operation::sharedpaths;
6551 if ( 0 == extHandle ) return 0;
6552 GEOSContextHandleInternal_t *handle =
6553 reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
6554 if ( handle->initialized == 0 ) return 0;
6556 SharedPathsOp::PathList forw, back;
6557 try {
6558 SharedPathsOp::sharedPathsOp(*g1, *g2, forw, back);
6560 catch (const std::exception &e)
6562 SharedPathsOp::clearEdges(forw);
6563 SharedPathsOp::clearEdges(back);
6564 handle->ERROR_MESSAGE("%s", e.what());
6565 return 0;
6567 catch (...)
6569 SharedPathsOp::clearEdges(forw);
6570 SharedPathsOp::clearEdges(back);
6571 handle->ERROR_MESSAGE("Unknown exception thrown");
6572 return 0;
6575 // Now forw and back have the geoms we want to use to construct
6576 // our output GeometryCollections...
6578 const GeometryFactory* factory = g1->getFactory();
6579 size_t count;
6581 std::auto_ptr< std::vector<Geometry*> > out1(
6582 new std::vector<Geometry*>()
6584 count = forw.size();
6585 out1->reserve(count);
6586 for (size_t i=0; i<count; ++i) {
6587 out1->push_back(forw[i]);
6589 std::auto_ptr<Geometry> out1g (
6590 factory->createMultiLineString(out1.release())
6593 std::auto_ptr< std::vector<Geometry*> > out2(
6594 new std::vector<Geometry*>()
6596 count = back.size();
6597 out2->reserve(count);
6598 for (size_t i=0; i<count; ++i) {
6599 out2->push_back(back[i]);
6601 std::auto_ptr<Geometry> out2g (
6602 factory->createMultiLineString(out2.release())
6605 std::auto_ptr< std::vector<Geometry*> > out(
6606 new std::vector<Geometry*>()
6608 out->reserve(2);
6609 out->push_back(out1g.release());
6610 out->push_back(out2g.release());
6612 std::auto_ptr<Geometry> outg (
6613 factory->createGeometryCollection(out.release())
6616 return outg.release();
6620 GEOSGeometry *
6621 GEOSSnap_r(GEOSContextHandle_t extHandle, const GEOSGeometry* g1,
6622 const GEOSGeometry* g2, double tolerance)
6624 using namespace geos::operation::overlay::snap;
6626 if ( 0 == extHandle ) return 0;
6627 GEOSContextHandleInternal_t *handle =
6628 reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
6629 if ( handle->initialized == 0 ) return 0;
6631 try{
6632 GeometrySnapper snapper( *g1 );
6633 std::auto_ptr<Geometry> ret = snapper.snapTo(*g2, tolerance);
6634 return ret.release();
6636 catch (const std::exception &e)
6638 handle->ERROR_MESSAGE("%s", e.what());
6639 return 0;
6641 catch (...)
6643 handle->ERROR_MESSAGE("Unknown exception thrown");
6644 return 0;
6648 BufferParameters *
6649 GEOSBufferParams_create_r(GEOSContextHandle_t extHandle)
6651 if ( 0 == extHandle ) return NULL;
6653 GEOSContextHandleInternal_t *handle = 0;
6654 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
6655 if ( 0 == handle->initialized ) return NULL;
6659 BufferParameters *p = new BufferParameters();
6660 return p;
6662 catch (const std::exception &e)
6664 handle->ERROR_MESSAGE("%s", e.what());
6666 catch (...)
6668 handle->ERROR_MESSAGE("Unknown exception thrown");
6671 return 0;
6674 void
6675 GEOSBufferParams_destroy_r(GEOSContextHandle_t extHandle, BufferParameters* p)
6677 (void)extHandle;
6678 delete p;
6682 GEOSBufferParams_setEndCapStyle_r(GEOSContextHandle_t extHandle,
6683 GEOSBufferParams* p, int style)
6685 if ( 0 == extHandle ) return 0;
6687 GEOSContextHandleInternal_t *handle = 0;
6688 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
6689 if ( 0 == handle->initialized ) return 0;
6693 if ( style > BufferParameters::CAP_SQUARE )
6695 throw IllegalArgumentException("Invalid buffer endCap style");
6697 p->setEndCapStyle(static_cast<BufferParameters::EndCapStyle>(style));
6698 return 1;
6700 catch (const std::exception &e)
6702 handle->ERROR_MESSAGE("%s", e.what());
6704 catch (...)
6706 handle->ERROR_MESSAGE("Unknown exception thrown");
6709 return 0;
6713 GEOSBufferParams_setJoinStyle_r(GEOSContextHandle_t extHandle,
6714 GEOSBufferParams* p, int style)
6716 if ( 0 == extHandle ) return 0;
6718 GEOSContextHandleInternal_t *handle = 0;
6719 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
6720 if ( 0 == handle->initialized ) return 0;
6724 if ( style > BufferParameters::JOIN_BEVEL ) {
6725 throw IllegalArgumentException("Invalid buffer join style");
6727 p->setJoinStyle(static_cast<BufferParameters::JoinStyle>(style));
6728 return 1;
6730 catch (const std::exception &e)
6732 handle->ERROR_MESSAGE("%s", e.what());
6734 catch (...)
6736 handle->ERROR_MESSAGE("Unknown exception thrown");
6739 return 0;
6743 GEOSBufferParams_setMitreLimit_r(GEOSContextHandle_t extHandle,
6744 GEOSBufferParams* p, double limit)
6746 if ( 0 == extHandle ) return 0;
6748 GEOSContextHandleInternal_t *handle = 0;
6749 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
6750 if ( 0 == handle->initialized ) return 0;
6754 p->setMitreLimit(limit);
6755 return 1;
6757 catch (const std::exception &e)
6759 handle->ERROR_MESSAGE("%s", e.what());
6761 catch (...)
6763 handle->ERROR_MESSAGE("Unknown exception thrown");
6766 return 0;
6770 GEOSBufferParams_setQuadrantSegments_r(GEOSContextHandle_t extHandle,
6771 GEOSBufferParams* p, int segs)
6773 if ( 0 == extHandle ) return 0;
6775 GEOSContextHandleInternal_t *handle = 0;
6776 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
6777 if ( 0 == handle->initialized ) return 0;
6781 p->setQuadrantSegments(segs);
6782 return 1;
6784 catch (const std::exception &e)
6786 handle->ERROR_MESSAGE("%s", e.what());
6788 catch (...)
6790 handle->ERROR_MESSAGE("Unknown exception thrown");
6793 return 0;
6797 GEOSBufferParams_setSingleSided_r(GEOSContextHandle_t extHandle,
6798 GEOSBufferParams* p, int ss)
6800 if ( 0 == extHandle ) return 0;
6802 GEOSContextHandleInternal_t *handle = 0;
6803 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
6804 if ( 0 == handle->initialized ) return 0;
6808 p->setSingleSided( (ss != 0) );
6809 return 1;
6811 catch (const std::exception &e)
6813 handle->ERROR_MESSAGE("%s", e.what());
6815 catch (...)
6817 handle->ERROR_MESSAGE("Unknown exception thrown");
6820 return 0;
6823 Geometry *
6824 GEOSBufferWithParams_r(GEOSContextHandle_t extHandle, const Geometry *g1, const BufferParameters* bp, double width)
6826 using geos::operation::buffer::BufferOp;
6828 if ( 0 == extHandle ) return NULL;
6830 GEOSContextHandleInternal_t *handle = 0;
6831 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
6832 if ( 0 == handle->initialized ) return NULL;
6836 BufferOp op(g1, *bp);
6837 Geometry *g3 = op.getResultGeometry(width);
6838 return g3;
6840 catch (const std::exception &e)
6842 handle->ERROR_MESSAGE("%s", e.what());
6844 catch (...)
6846 handle->ERROR_MESSAGE("Unknown exception thrown");
6849 return NULL;
6852 Geometry *
6853 GEOSDelaunayTriangulation_r(GEOSContextHandle_t extHandle, const Geometry *g1, double tolerance, int onlyEdges)
6855 if ( 0 == extHandle ) return NULL;
6857 GEOSContextHandleInternal_t *handle = 0;
6858 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
6859 if ( 0 == handle->initialized ) return NULL;
6861 using geos::triangulate::DelaunayTriangulationBuilder;
6865 DelaunayTriangulationBuilder builder;
6866 builder.setTolerance(tolerance);
6867 builder.setSites(*g1);
6869 if ( onlyEdges ) return builder.getEdges( *g1->getFactory() ).release();
6870 else return builder.getTriangles( *g1->getFactory() ).release();
6873 catch (const std::exception &e)
6875 handle->ERROR_MESSAGE("%s", e.what());
6877 catch (...)
6879 handle->ERROR_MESSAGE("Unknown exception thrown");
6882 return NULL;
6884 Geometry*
6885 GEOSVoronoiDiagram_r(GEOSContextHandle_t extHandle, const Geometry *g1, const Geometry *env, double tolerance ,int onlyEdges)
6887 if ( 0 == extHandle ) return NULL;
6889 GEOSContextHandleInternal_t *handle = 0;
6890 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
6891 if ( 0 == handle->initialized ) return NULL;
6893 using geos::triangulate::VoronoiDiagramBuilder;
6897 VoronoiDiagramBuilder builder;
6898 builder.setSites(*g1);
6899 builder.setTolerance(tolerance);
6900 if(env) builder.setClipEnvelope(env->getEnvelopeInternal());
6901 if(onlyEdges) return builder.getDiagramEdges(*g1->getFactory()).release();
6902 else return builder.getDiagram(*g1->getFactory()).release();
6904 catch(const std::exception &e)
6906 handle->ERROR_MESSAGE("%s", e.what());
6908 catch(...)
6910 handle->ERROR_MESSAGE("Unknow exception thrown");
6913 return NULL;
6916 } /* extern "C" */