Trim trailing whitespaces (as per .editorconfig)
[geos.git] / capi / geos_ts_c.cpp
blobe9392a1adfdc2addd42fdfc9229c851c1615bb31
1 /************************************************************************
4 * C-Wrapper for GEOS library
6 * Copyright (C) 2010-2012 Sandro Santilli <strk@keybit.net>
7 * Copyright (C) 2005-2006 Refractions Research Inc.
9 * This is free software; you can redistribute and/or modify it under
10 * the terms of the GNU Lesser General Public Licence as published
11 * by the Free Software Foundation.
12 * See the COPYING file for more information.
14 * Author: Sandro Santilli <strk@keybit.net>
15 * Thread Safety modifications: Chuck Thibert <charles.thibert@ingres.com>
17 ***********************************************************************/
19 #include <geos/platform.h> // for FINITE
20 #include <geos/geom/Geometry.h>
21 #include <geos/geom/prep/PreparedGeometry.h>
22 #include <geos/geom/prep/PreparedGeometryFactory.h>
23 #include <geos/geom/GeometryCollection.h>
24 #include <geos/geom/Polygon.h>
25 #include <geos/geom/Point.h>
26 #include <geos/geom/MultiPoint.h>
27 #include <geos/geom/MultiLineString.h>
28 #include <geos/geom/MultiPolygon.h>
29 #include <geos/geom/LinearRing.h>
30 #include <geos/geom/LineString.h>
31 #include <geos/geom/PrecisionModel.h>
32 #include <geos/geom/GeometryFactory.h>
33 #include <geos/geom/CoordinateSequenceFactory.h>
34 #include <geos/geom/Coordinate.h>
35 #include <geos/geom/IntersectionMatrix.h>
36 #include <geos/geom/Envelope.h>
37 #include <geos/index/strtree/STRtree.h>
38 #include <geos/index/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/CGAlgorithms.h>
46 #include <geos/algorithm/BoundaryNodeRule.h>
47 #include <geos/algorithm/MinimumDiameter.h>
48 #include <geos/simplify/DouglasPeuckerSimplifier.h>
49 #include <geos/simplify/TopologyPreservingSimplifier.h>
50 #include <geos/noding/GeometryNoder.h>
51 #include <geos/noding/Noder.h>
52 #include <geos/operation/buffer/BufferBuilder.h>
53 #include <geos/operation/buffer/BufferOp.h>
54 #include <geos/operation/buffer/BufferParameters.h>
55 #include <geos/operation/distance/DistanceOp.h>
56 #include <geos/operation/linemerge/LineMerger.h>
57 #include <geos/operation/overlay/OverlayOp.h>
58 #include <geos/operation/overlay/snap/GeometrySnapper.h>
59 #include <geos/operation/intersection/Rectangle.h>
60 #include <geos/operation/intersection/RectangleIntersection.h>
61 #include <geos/operation/polygonize/Polygonizer.h>
62 #include <geos/operation/relate/RelateOp.h>
63 #include <geos/operation/sharedpaths/SharedPathsOp.h>
64 #include <geos/operation/union/CascadedPolygonUnion.h>
65 #include <geos/operation/valid/IsValidOp.h>
66 #include <geos/precision/GeometryPrecisionReducer.h>
67 #include <geos/linearref/LengthIndexedLine.h>
68 #include <geos/triangulate/DelaunayTriangulationBuilder.h>
69 #include <geos/triangulate/VoronoiDiagramBuilder.h>
70 #include <geos/util/IllegalArgumentException.h>
71 #include <geos/util/Interrupt.h>
72 #include <geos/util/UniqueCoordinateArrayFilter.h>
73 #include <geos/util/Machine.h>
74 #include <geos/version.h>
76 // This should go away
77 #include <cmath> // finite
78 #include <cstdarg>
79 #include <cstddef>
80 #include <cstdio>
81 #include <cstdlib>
82 #include <cstring>
83 #include <fstream>
84 #include <iostream>
85 #include <sstream>
86 #include <string>
87 #include <memory>
89 #ifdef _MSC_VER
90 #pragma warning(disable : 4099)
91 #endif
93 // Some extra magic to make type declarations in geos_c.h work -
94 // for cross-checking of types in header.
95 #define GEOSGeometry geos::geom::Geometry
96 #define GEOSPreparedGeometry geos::geom::prep::PreparedGeometry
97 #define GEOSCoordSequence geos::geom::CoordinateSequence
98 #define GEOSBufferParams geos::operation::buffer::BufferParameters
99 #define GEOSSTRtree geos::index::strtree::STRtree
100 #define GEOSWKTReader_t geos::io::WKTReader
101 #define GEOSWKTWriter_t geos::io::WKTWriter
102 #define GEOSWKBReader_t geos::io::WKBReader
103 #define GEOSWKBWriter_t geos::io::WKBWriter
105 #include "geos_c.h"
106 #include "../geos_svn_revision.h"
108 // Intentional, to allow non-standard C elements like C99 functions to be
109 // imported through C++ headers of C library, like <cmath>.
110 using namespace std;
112 /// Define this if you want operations triggering Exceptions to
113 /// be printed.
114 /// (will use the NOTIFY channel - only implemented for GEOSUnion so far)
116 #undef VERBOSE_EXCEPTIONS
118 #include <geos/export.h>
121 // import the most frequently used definitions globally
122 using geos::geom::Geometry;
123 using geos::geom::LineString;
124 using geos::geom::Polygon;
125 using geos::geom::CoordinateSequence;
126 using geos::geom::GeometryFactory;
128 using geos::io::WKTReader;
129 using geos::io::WKTWriter;
130 using geos::io::WKBReader;
131 using geos::io::WKBWriter;
133 using geos::operation::overlay::OverlayOp;
134 using geos::operation::overlay::overlayOp;
135 using geos::operation::geounion::CascadedPolygonUnion;
136 using geos::operation::buffer::BufferParameters;
137 using geos::operation::buffer::BufferBuilder;
138 using geos::precision::GeometryPrecisionReducer;
139 using geos::util::IllegalArgumentException;
140 using geos::algorithm::distance::DiscreteHausdorffDistance;
142 typedef std::auto_ptr<Geometry> GeomAutoPtr;
144 typedef struct GEOSContextHandle_HS
146 const GeometryFactory *geomFactory;
147 char msgBuffer[1024];
148 GEOSMessageHandler noticeMessageOld;
149 GEOSMessageHandler_r noticeMessageNew;
150 void *noticeData;
151 GEOSMessageHandler errorMessageOld;
152 GEOSMessageHandler_r errorMessageNew;
153 void *errorData;
154 int WKBOutputDims;
155 int WKBByteOrder;
156 int initialized;
158 GEOSContextHandle_HS()
160 geomFactory(0),
161 noticeMessageOld(0),
162 noticeMessageNew(0),
163 noticeData(0),
164 errorMessageOld(0),
165 errorMessageNew(0),
166 errorData(0)
168 memset(msgBuffer, 0, sizeof(msgBuffer));
169 geomFactory = GeometryFactory::getDefaultInstance();
170 WKBOutputDims = 2;
171 WKBByteOrder = getMachineByteOrder();
172 setNoticeHandler(NULL);
173 setErrorHandler(NULL);
174 initialized = 1;
177 GEOSMessageHandler
178 setNoticeHandler(GEOSMessageHandler nf)
180 GEOSMessageHandler f = noticeMessageOld;
181 noticeMessageOld = nf;
182 noticeMessageNew = NULL;
183 noticeData = NULL;
185 return f;
188 GEOSMessageHandler
189 setErrorHandler(GEOSMessageHandler nf)
191 GEOSMessageHandler f = errorMessageOld;
192 errorMessageOld = nf;
193 errorMessageNew = NULL;
194 errorData = NULL;
196 return f;
199 GEOSMessageHandler_r
200 setNoticeHandler(GEOSMessageHandler_r nf, void *userData) {
201 GEOSMessageHandler_r f = noticeMessageNew;
202 noticeMessageOld = NULL;
203 noticeMessageNew = nf;
204 noticeData = userData;
206 return f;
209 GEOSMessageHandler_r
210 setErrorHandler(GEOSMessageHandler_r ef, void *userData)
212 GEOSMessageHandler_r f = errorMessageNew;
213 errorMessageOld = NULL;
214 errorMessageNew = ef;
215 errorData = userData;
217 return f;
220 void
221 NOTICE_MESSAGE(string fmt, ...)
223 if (NULL == noticeMessageOld && NULL == noticeMessageNew) {
224 return;
227 va_list args;
228 va_start(args, fmt);
229 int result = vsnprintf(msgBuffer, sizeof(msgBuffer) - 1, fmt.c_str(), args);
230 va_end(args);
232 if (result > 0) {
233 if (noticeMessageOld) {
234 noticeMessageOld("%s", msgBuffer);
235 } else {
236 noticeMessageNew(msgBuffer, noticeData);
241 void
242 ERROR_MESSAGE(string fmt, ...)
244 if (NULL == errorMessageOld && NULL == errorMessageNew) {
245 return;
248 va_list args;
249 va_start(args, fmt);
250 int result = vsnprintf(msgBuffer, sizeof(msgBuffer) - 1, fmt.c_str(), args);
251 va_end(args);
253 if (result > 0) {
254 if (errorMessageOld) {
255 errorMessageOld("%s", msgBuffer);
256 } else {
257 errorMessageNew(msgBuffer, errorData);
261 } GEOSContextHandleInternal_t;
263 // CAPI_ItemVisitor is used internally by the CAPI STRtree
264 // wrappers. It's defined here just to keep it out of the
265 // extern "C" block.
266 class CAPI_ItemVisitor : public geos::index::ItemVisitor {
267 GEOSQueryCallback callback;
268 void *userdata;
269 public:
270 CAPI_ItemVisitor (GEOSQueryCallback cb, void *ud)
271 : ItemVisitor(), callback(cb), userdata(ud) {}
272 void visitItem (void *item) { callback(item, userdata); }
276 //## PROTOTYPES #############################################
278 extern "C" const char GEOS_DLL *GEOSjtsport();
279 extern "C" char GEOS_DLL *GEOSasText(Geometry *g1);
282 namespace { // anonymous
284 char* gstrdup_s(const char* str, const std::size_t size)
286 char* out = static_cast<char*>(malloc(size + 1));
287 if (0 != out)
289 // as no strlen call necessary, memcpy may be faster than strcpy
290 std::memcpy(out, str, size + 1);
293 assert(0 != out);
295 // we haven't been checking allocation before ticket #371
296 if (0 == out)
298 throw(std::runtime_error("Failed to allocate memory for duplicate string"));
301 return out;
304 char* gstrdup(std::string const& str)
306 return gstrdup_s(str.c_str(), str.size());
309 } // namespace anonymous
311 extern "C" {
313 GEOSContextHandle_t
314 initGEOS_r(GEOSMessageHandler nf, GEOSMessageHandler ef)
316 GEOSContextHandle_t handle = GEOS_init_r();
318 if (0 != handle) {
319 GEOSContext_setNoticeHandler_r(handle, nf);
320 GEOSContext_setErrorHandler_r(handle, ef);
323 return handle;
326 GEOSContextHandle_t
327 GEOS_init_r()
329 GEOSContextHandleInternal_t *handle = new GEOSContextHandleInternal_t();
331 geos::util::Interrupt::cancel();
333 return static_cast<GEOSContextHandle_t>(handle);
336 GEOSMessageHandler
337 GEOSContext_setNoticeHandler_r(GEOSContextHandle_t extHandle, GEOSMessageHandler nf)
339 GEOSContextHandleInternal_t *handle = 0;
340 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
341 if ( 0 == handle->initialized )
343 return NULL;
346 return handle->setNoticeHandler(nf);
349 GEOSMessageHandler
350 GEOSContext_setErrorHandler_r(GEOSContextHandle_t extHandle, GEOSMessageHandler nf)
352 GEOSContextHandleInternal_t *handle = 0;
353 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
354 if ( 0 == handle->initialized )
356 return NULL;
359 return handle->setErrorHandler(nf);
362 GEOSMessageHandler_r
363 GEOSContext_setNoticeMessageHandler_r(GEOSContextHandle_t extHandle, GEOSMessageHandler_r nf, void *userData) {
364 GEOSContextHandleInternal_t *handle = 0;
365 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
366 if ( 0 == handle->initialized )
368 return NULL;
371 return handle->setNoticeHandler(nf, userData);
374 GEOSMessageHandler_r
375 GEOSContext_setErrorMessageHandler_r(GEOSContextHandle_t extHandle, GEOSMessageHandler_r ef, void *userData)
377 GEOSContextHandleInternal_t *handle = 0;
378 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
379 if ( 0 == handle->initialized )
381 return NULL;
384 return handle->setErrorHandler(ef, userData);
387 void
388 finishGEOS_r(GEOSContextHandle_t extHandle)
390 // Fix up freeing handle w.r.t. malloc above
391 delete extHandle;
392 extHandle = NULL;
395 void
396 GEOS_finish_r(GEOSContextHandle_t extHandle)
398 finishGEOS_r(extHandle);
401 void
402 GEOSFree_r (GEOSContextHandle_t extHandle, void* buffer)
404 assert(0 != extHandle);
406 free(buffer);
409 //-----------------------------------------------------------
410 // relate()-related functions
411 // return 0 = false, 1 = true, 2 = error occured
412 //-----------------------------------------------------------
414 char
415 GEOSDisjoint_r(GEOSContextHandle_t extHandle, const Geometry *g1, const Geometry *g2)
417 if ( 0 == extHandle )
419 return 2;
422 GEOSContextHandleInternal_t *handle = 0;
423 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
424 if ( handle->initialized == 0 )
426 return 2;
431 bool result = g1->disjoint(g2);
432 return result;
435 // TODO: mloskot is going to replace these double-catch block
436 // with a macro to remove redundant code in this and
437 // following functions.
438 catch (const std::exception &e)
440 handle->ERROR_MESSAGE("%s", e.what());
442 catch (...)
444 handle->ERROR_MESSAGE("Unknown exception thrown");
447 return 2;
450 char
451 GEOSTouches_r(GEOSContextHandle_t extHandle, const Geometry *g1, const Geometry *g2)
453 if ( 0 == extHandle )
455 return 2;
458 GEOSContextHandleInternal_t *handle = 0;
459 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
460 if ( 0 == handle->initialized )
462 return 2;
467 bool result = g1->touches(g2);
468 return result;
470 catch (const std::exception &e)
472 handle->ERROR_MESSAGE("%s", e.what());
474 catch (...)
476 handle->ERROR_MESSAGE("Unknown exception thrown");
479 return 2;
482 char
483 GEOSIntersects_r(GEOSContextHandle_t extHandle, const Geometry *g1, const Geometry *g2)
485 if ( 0 == extHandle )
487 return 2;
490 GEOSContextHandleInternal_t *handle = 0;
491 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
492 if ( 0 == handle->initialized )
494 return 2;
499 bool result = g1->intersects(g2);
500 return result;
502 catch (const std::exception &e)
504 handle->ERROR_MESSAGE("%s", e.what());
506 catch (...)
508 handle->ERROR_MESSAGE("Unknown exception thrown");
511 return 2;
514 char
515 GEOSCrosses_r(GEOSContextHandle_t extHandle, const Geometry *g1, const Geometry *g2)
517 if ( 0 == extHandle )
519 return 2;
522 GEOSContextHandleInternal_t *handle = 0;
523 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
524 if ( 0 == handle->initialized )
526 return 2;
531 bool result = g1->crosses(g2);
532 return result;
534 catch (const std::exception &e)
536 handle->ERROR_MESSAGE("%s", e.what());
538 catch (...)
540 handle->ERROR_MESSAGE("Unknown exception thrown");
543 return 2;
546 char
547 GEOSWithin_r(GEOSContextHandle_t extHandle, const Geometry *g1, const Geometry *g2)
549 if ( 0 == extHandle )
551 return 2;
554 GEOSContextHandleInternal_t *handle = 0;
555 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
556 if ( 0 == handle->initialized )
558 return 2;
563 bool result = g1->within(g2);
564 return result;
566 catch (const std::exception &e)
568 handle->ERROR_MESSAGE("%s", e.what());
570 catch (...)
572 handle->ERROR_MESSAGE("Unknown exception thrown");
575 return 2;
578 // call g1->contains(g2)
579 // returns 0 = false
580 // 1 = true
581 // 2 = error was trapped
582 char
583 GEOSContains_r(GEOSContextHandle_t extHandle, const Geometry *g1, const Geometry *g2)
585 if ( 0 == extHandle )
587 return 2;
590 GEOSContextHandleInternal_t *handle = 0;
591 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
592 if ( 0 == handle->initialized )
594 return 2;
599 bool result = g1->contains(g2);
600 return result;
602 catch (const std::exception &e)
604 handle->ERROR_MESSAGE("%s", e.what());
606 catch (...)
608 handle->ERROR_MESSAGE("Unknown exception thrown");
611 return 2;
614 char
615 GEOSOverlaps_r(GEOSContextHandle_t extHandle, const Geometry *g1, const Geometry *g2)
617 if ( 0 == extHandle )
619 return 2;
622 GEOSContextHandleInternal_t *handle = 0;
623 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
624 if ( 0 == handle->initialized )
626 return 2;
631 bool result = g1->overlaps(g2);
632 return result;
634 catch (const std::exception &e)
636 handle->ERROR_MESSAGE("%s", e.what());
638 catch (...)
640 handle->ERROR_MESSAGE("Unknown exception thrown");
643 return 2;
646 char
647 GEOSCovers_r(GEOSContextHandle_t extHandle, const Geometry *g1, const Geometry *g2)
649 if ( 0 == extHandle )
651 return 2;
654 GEOSContextHandleInternal_t *handle = 0;
655 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
656 if ( 0 == handle->initialized )
658 return 2;
663 bool result = g1->covers(g2);
664 return result;
666 catch (const std::exception &e)
668 handle->ERROR_MESSAGE("%s", e.what());
670 catch (...)
672 handle->ERROR_MESSAGE("Unknown exception thrown");
675 return 2;
678 char
679 GEOSCoveredBy_r(GEOSContextHandle_t extHandle, const Geometry *g1, const Geometry *g2)
681 if ( 0 == extHandle )
683 return 2;
686 GEOSContextHandleInternal_t *handle = 0;
687 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
688 if ( 0 == handle->initialized )
690 return 2;
695 bool result = g1->coveredBy(g2);
696 return result;
698 catch (const std::exception &e)
700 handle->ERROR_MESSAGE("%s", e.what());
702 catch (...)
704 handle->ERROR_MESSAGE("Unknown exception thrown");
707 return 2;
711 //-------------------------------------------------------------------
712 // low-level relate functions
713 //------------------------------------------------------------------
715 char
716 GEOSRelatePattern_r(GEOSContextHandle_t extHandle, const Geometry *g1, const Geometry *g2, const char *pat)
718 if ( 0 == extHandle )
720 return 2;
723 GEOSContextHandleInternal_t *handle = 0;
724 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
725 if ( 0 == handle->initialized )
727 return 2;
732 std::string s(pat);
733 bool result = g1->relate(g2, s);
734 return result;
736 catch (const std::exception &e)
738 handle->ERROR_MESSAGE("%s", e.what());
740 catch (...)
742 handle->ERROR_MESSAGE("Unknown exception thrown");
745 return 2;
748 char
749 GEOSRelatePatternMatch_r(GEOSContextHandle_t extHandle, const char *mat,
750 const char *pat)
752 if ( 0 == extHandle )
754 return 2;
757 GEOSContextHandleInternal_t *handle = 0;
758 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
759 if ( 0 == handle->initialized )
761 return 2;
766 using geos::geom::IntersectionMatrix;
768 std::string m(mat);
769 std::string p(pat);
770 IntersectionMatrix im(m);
772 bool result = im.matches(p);
773 return result;
775 catch (const std::exception &e)
777 handle->ERROR_MESSAGE("%s", e.what());
779 catch (...)
781 handle->ERROR_MESSAGE("Unknown exception thrown");
784 return 2;
787 char *
788 GEOSRelate_r(GEOSContextHandle_t extHandle, const Geometry *g1, const Geometry *g2)
790 if ( 0 == extHandle )
792 return NULL;
795 GEOSContextHandleInternal_t *handle = 0;
796 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
797 if ( 0 == handle->initialized )
799 return NULL;
804 using geos::geom::IntersectionMatrix;
806 IntersectionMatrix* im = g1->relate(g2);
807 if (0 == im)
809 return 0;
812 char *result = gstrdup(im->toString());
814 delete im;
815 im = 0;
817 return result;
819 catch (const std::exception &e)
821 handle->ERROR_MESSAGE("%s", e.what());
823 catch (...)
825 handle->ERROR_MESSAGE("Unknown exception thrown");
828 return NULL;
831 char *
832 GEOSRelateBoundaryNodeRule_r(GEOSContextHandle_t extHandle, const Geometry *g1, const Geometry *g2, int bnr)
834 if ( 0 == extHandle )
836 return NULL;
839 GEOSContextHandleInternal_t *handle = 0;
840 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
841 if ( 0 == handle->initialized )
843 return NULL;
848 using geos::operation::relate::RelateOp;
849 using geos::geom::IntersectionMatrix;
850 using geos::algorithm::BoundaryNodeRule;
852 IntersectionMatrix* im;
853 switch (bnr) {
854 case GEOSRELATE_BNR_MOD2: /* same as OGC */
855 im = RelateOp::relate(g1, g2,
856 BoundaryNodeRule::getBoundaryRuleMod2());
857 break;
858 case GEOSRELATE_BNR_ENDPOINT:
859 im = RelateOp::relate(g1, g2,
860 BoundaryNodeRule::getBoundaryEndPoint());
861 break;
862 case GEOSRELATE_BNR_MULTIVALENT_ENDPOINT:
863 im = RelateOp::relate(g1, g2,
864 BoundaryNodeRule::getBoundaryMultivalentEndPoint());
865 break;
866 case GEOSRELATE_BNR_MONOVALENT_ENDPOINT:
867 im = RelateOp::relate(g1, g2,
868 BoundaryNodeRule::getBoundaryMonovalentEndPoint());
869 break;
870 default:
871 handle->ERROR_MESSAGE("Invalid boundary node rule %d", bnr);
872 return 0;
873 break;
876 if (0 == im) return 0;
878 char *result = gstrdup(im->toString());
880 delete im;
881 im = 0;
883 return result;
885 catch (const std::exception &e)
887 handle->ERROR_MESSAGE("%s", e.what());
889 catch (...)
891 handle->ERROR_MESSAGE("Unknown exception thrown");
894 return NULL;
899 //-----------------------------------------------------------------
900 // isValid
901 //-----------------------------------------------------------------
904 char
905 GEOSisValid_r(GEOSContextHandle_t extHandle, const Geometry *g1)
907 if ( 0 == extHandle )
909 return 2;
912 GEOSContextHandleInternal_t *handle = 0;
913 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
914 if ( 0 == handle->initialized )
916 return 2;
921 using geos::operation::valid::IsValidOp;
922 using geos::operation::valid::TopologyValidationError;
924 IsValidOp ivo(g1);
925 TopologyValidationError *err = ivo.getValidationError();
926 if ( err )
928 handle->NOTICE_MESSAGE("%s", err->toString().c_str());
929 return 0;
931 else
933 return 1;
936 catch (const std::exception &e)
938 handle->ERROR_MESSAGE("%s", e.what());
940 catch (...)
942 handle->ERROR_MESSAGE("Unknown exception thrown");
945 return 2;
948 char *
949 GEOSisValidReason_r(GEOSContextHandle_t extHandle, const Geometry *g1)
951 if ( 0 == extHandle )
953 return NULL;
956 GEOSContextHandleInternal_t *handle = 0;
957 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
958 if ( 0 == handle->initialized )
960 return NULL;
965 using geos::operation::valid::IsValidOp;
966 using geos::operation::valid::TopologyValidationError;
968 char* result = 0;
969 char const* const validstr = "Valid Geometry";
971 IsValidOp ivo(g1);
972 TopologyValidationError *err = ivo.getValidationError();
973 if (0 != err)
975 std::ostringstream ss;
976 ss.precision(15);
977 ss << err->getCoordinate();
978 const std::string errloc = ss.str();
979 std::string errmsg(err->getMessage());
980 errmsg += "[" + errloc + "]";
981 result = gstrdup(errmsg);
983 else
985 result = gstrdup(std::string(validstr));
988 return result;
990 catch (const std::exception &e)
992 handle->ERROR_MESSAGE("%s", e.what());
994 catch (...)
996 handle->ERROR_MESSAGE("Unknown exception thrown");
999 return 0;
1002 char
1003 GEOSisValidDetail_r(GEOSContextHandle_t extHandle, const Geometry *g,
1004 int flags, char** reason, Geometry ** location)
1006 if ( 0 == extHandle )
1008 return 0;
1011 GEOSContextHandleInternal_t *handle = 0;
1012 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
1013 if ( 0 == handle->initialized )
1015 return 0;
1020 using geos::operation::valid::IsValidOp;
1021 using geos::operation::valid::TopologyValidationError;
1023 IsValidOp ivo(g);
1024 if ( flags & GEOSVALID_ALLOW_SELFTOUCHING_RING_FORMING_HOLE ) {
1025 ivo.setSelfTouchingRingFormingHoleValid(true);
1027 TopologyValidationError *err = ivo.getValidationError();
1028 if (0 != err)
1030 if ( location ) {
1031 *location = handle->geomFactory->createPoint(err->getCoordinate());
1033 if ( reason ) {
1034 std::string errmsg(err->getMessage());
1035 *reason = gstrdup(errmsg);
1037 return 0;
1040 if ( location ) *location = 0;
1041 if ( reason ) *reason = 0;
1042 return 1; /* valid */
1045 catch (const std::exception &e)
1047 handle->ERROR_MESSAGE("%s", e.what());
1049 catch (...)
1051 handle->ERROR_MESSAGE("Unknown exception thrown");
1054 return 2; /* exception */
1057 //-----------------------------------------------------------------
1058 // general purpose
1059 //-----------------------------------------------------------------
1061 char
1062 GEOSEquals_r(GEOSContextHandle_t extHandle, const Geometry *g1, const Geometry *g2)
1064 if ( 0 == extHandle )
1066 return 2;
1069 GEOSContextHandleInternal_t *handle = 0;
1070 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
1071 if ( 0 == handle->initialized )
1073 return 2;
1078 bool result = g1->equals(g2);
1079 return result;
1081 catch (const std::exception &e)
1083 handle->ERROR_MESSAGE("%s", e.what());
1085 catch (...)
1087 handle->ERROR_MESSAGE("Unknown exception thrown");
1090 return 2;
1093 char
1094 GEOSEqualsExact_r(GEOSContextHandle_t extHandle, const Geometry *g1, const Geometry *g2, double tolerance)
1096 if ( 0 == extHandle )
1098 return 2;
1101 GEOSContextHandleInternal_t *handle = 0;
1102 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
1103 if ( 0 == handle->initialized )
1105 return 2;
1110 bool result = g1->equalsExact(g2, tolerance);
1111 return result;
1113 catch (const std::exception &e)
1115 handle->ERROR_MESSAGE("%s", e.what());
1117 catch (...)
1119 handle->ERROR_MESSAGE("Unknown exception thrown");
1122 return 2;
1126 GEOSDistance_r(GEOSContextHandle_t extHandle, const Geometry *g1, const Geometry *g2, double *dist)
1128 assert(0 != dist);
1130 if ( 0 == extHandle )
1132 return 0;
1135 GEOSContextHandleInternal_t *handle = 0;
1136 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
1137 if ( 0 == handle->initialized )
1139 return 0;
1144 *dist = g1->distance(g2);
1145 return 1;
1147 catch (const std::exception &e)
1149 handle->ERROR_MESSAGE("%s", e.what());
1151 catch (...)
1153 handle->ERROR_MESSAGE("Unknown exception thrown");
1156 return 0;
1160 GEOSHausdorffDistance_r(GEOSContextHandle_t extHandle, const Geometry *g1, const Geometry *g2, double *dist)
1162 assert(0 != dist);
1164 if ( 0 == extHandle )
1166 return 0;
1169 GEOSContextHandleInternal_t *handle = 0;
1170 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
1171 if ( 0 == handle->initialized )
1173 return 0;
1178 *dist = DiscreteHausdorffDistance::distance(*g1, *g2);
1179 return 1;
1181 catch (const std::exception &e)
1183 handle->ERROR_MESSAGE("%s", e.what());
1185 catch (...)
1187 handle->ERROR_MESSAGE("Unknown exception thrown");
1190 return 0;
1194 GEOSHausdorffDistanceDensify_r(GEOSContextHandle_t extHandle, const Geometry *g1, const Geometry *g2, double densifyFrac, double *dist)
1196 assert(0 != dist);
1198 if ( 0 == extHandle )
1200 return 0;
1203 GEOSContextHandleInternal_t *handle = 0;
1204 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
1205 if ( 0 == handle->initialized )
1207 return 0;
1212 *dist = DiscreteHausdorffDistance::distance(*g1, *g2, densifyFrac);
1213 return 1;
1215 catch (const std::exception &e)
1217 handle->ERROR_MESSAGE("%s", e.what());
1219 catch (...)
1221 handle->ERROR_MESSAGE("Unknown exception thrown");
1224 return 0;
1228 GEOSArea_r(GEOSContextHandle_t extHandle, const Geometry *g, double *area)
1230 assert(0 != area);
1232 if ( 0 == extHandle )
1234 return 0;
1237 GEOSContextHandleInternal_t *handle = 0;
1238 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
1239 if ( 0 == handle->initialized )
1241 return 0;
1246 *area = g->getArea();
1247 return 1;
1249 catch (const std::exception &e)
1251 handle->ERROR_MESSAGE("%s", e.what());
1253 catch (...)
1255 handle->ERROR_MESSAGE("Unknown exception thrown");
1258 return 0;
1262 GEOSLength_r(GEOSContextHandle_t extHandle, const Geometry *g, double *length)
1264 assert(0 != length);
1266 if ( 0 == extHandle )
1268 return 2;
1271 GEOSContextHandleInternal_t *handle = 0;
1272 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
1273 if ( 0 == handle->initialized )
1275 return 0;
1280 *length = g->getLength();
1281 return 1;
1283 catch (const std::exception &e)
1285 handle->ERROR_MESSAGE("%s", e.what());
1287 catch (...)
1289 handle->ERROR_MESSAGE("Unknown exception thrown");
1292 return 0;
1295 CoordinateSequence *
1296 GEOSNearestPoints_r(GEOSContextHandle_t extHandle, const Geometry *g1, const Geometry *g2)
1298 if ( 0 == extHandle )
1300 return NULL;
1303 GEOSContextHandleInternal_t *handle = 0;
1304 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
1305 if ( 0 == handle->initialized )
1307 return NULL;
1312 if (g1->isEmpty() || g2->isEmpty()) return 0;
1313 return geos::operation::distance::DistanceOp::nearestPoints(g1, g2);
1315 catch (const std::exception &e)
1317 handle->ERROR_MESSAGE("%s", e.what());
1319 catch (...)
1321 handle->ERROR_MESSAGE("Unknown exception thrown");
1324 return NULL;
1328 Geometry *
1329 GEOSGeomFromWKT_r(GEOSContextHandle_t extHandle, const char *wkt)
1331 if ( 0 == extHandle )
1333 return NULL;
1336 GEOSContextHandleInternal_t *handle = 0;
1337 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
1338 if ( 0 == handle->initialized )
1340 return NULL;
1345 const std::string wktstring(wkt);
1346 WKTReader r(static_cast<GeometryFactory const*>(handle->geomFactory));
1348 Geometry *g = r.read(wktstring);
1349 return g;
1351 catch (const std::exception &e)
1353 handle->ERROR_MESSAGE("%s", e.what());
1355 catch (...)
1357 handle->ERROR_MESSAGE("Unknown exception thrown");
1360 return NULL;
1363 char *
1364 GEOSGeomToWKT_r(GEOSContextHandle_t extHandle, const Geometry *g1)
1366 if ( 0 == extHandle )
1368 return NULL;
1371 GEOSContextHandleInternal_t *handle = 0;
1372 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
1373 if ( 0 == handle->initialized )
1375 return NULL;
1381 char *result = gstrdup(g1->toString());
1382 return result;
1384 catch (const std::exception &e)
1386 handle->ERROR_MESSAGE("%s", e.what());
1388 catch (...)
1390 handle->ERROR_MESSAGE("Unknown exception thrown");
1392 return NULL;
1395 // Remember to free the result!
1396 unsigned char *
1397 GEOSGeomToWKB_buf_r(GEOSContextHandle_t extHandle, const Geometry *g, size_t *size)
1399 assert(0 != size);
1401 if ( 0 == extHandle )
1403 return NULL;
1406 GEOSContextHandleInternal_t *handle = 0;
1407 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
1408 if ( 0 == handle->initialized )
1410 return NULL;
1413 using geos::io::WKBWriter;
1416 int byteOrder = handle->WKBByteOrder;
1417 WKBWriter w(handle->WKBOutputDims, byteOrder);
1418 std::ostringstream os(std::ios_base::binary);
1419 w.write(*g, os);
1420 std::string wkbstring(os.str());
1421 const std::size_t len = wkbstring.length();
1423 unsigned char* result = 0;
1424 result = static_cast<unsigned char*>(malloc(len));
1425 if (0 != result)
1427 std::memcpy(result, wkbstring.c_str(), len);
1428 *size = len;
1430 return result;
1432 catch (const std::exception &e)
1434 handle->ERROR_MESSAGE("%s", e.what());
1436 catch (...)
1438 handle->ERROR_MESSAGE("Unknown exception thrown");
1441 return NULL;
1444 Geometry *
1445 GEOSGeomFromWKB_buf_r(GEOSContextHandle_t extHandle, const unsigned char *wkb, size_t size)
1447 if ( 0 == extHandle )
1449 return NULL;
1452 GEOSContextHandleInternal_t *handle = 0;
1453 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
1454 if ( 0 == handle->initialized )
1456 return NULL;
1459 using geos::io::WKBReader;
1462 std::string wkbstring(reinterpret_cast<const char*>(wkb), size); // make it binary !
1463 WKBReader r(*(static_cast<GeometryFactory const*>(handle->geomFactory)));
1464 std::istringstream is(std::ios_base::binary);
1465 is.str(wkbstring);
1466 is.seekg(0, std::ios::beg); // rewind reader pointer
1467 Geometry *g = r.read(is);
1468 return g;
1470 catch (const std::exception &e)
1472 handle->ERROR_MESSAGE("%s", e.what());
1474 catch (...)
1476 handle->ERROR_MESSAGE("Unknown exception thrown");
1479 return NULL;
1482 /* Read/write wkb hex values. Returned geometries are
1483 owned by the caller.*/
1484 unsigned char *
1485 GEOSGeomToHEX_buf_r(GEOSContextHandle_t extHandle, const Geometry *g, size_t *size)
1487 if ( 0 == extHandle )
1489 return NULL;
1492 GEOSContextHandleInternal_t *handle = 0;
1493 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
1494 if ( 0 == handle->initialized )
1496 return NULL;
1499 using geos::io::WKBWriter;
1502 int byteOrder = handle->WKBByteOrder;
1503 WKBWriter w(handle->WKBOutputDims, byteOrder);
1504 std::ostringstream os(std::ios_base::binary);
1505 w.writeHEX(*g, os);
1506 std::string hexstring(os.str());
1508 char *result = gstrdup(hexstring);
1509 if (0 != result)
1511 *size = hexstring.length();
1514 return reinterpret_cast<unsigned char*>(result);
1516 catch (const std::exception &e)
1518 handle->ERROR_MESSAGE("%s", e.what());
1520 catch (...)
1522 handle->ERROR_MESSAGE("Unknown exception thrown");
1525 return NULL;
1528 Geometry *
1529 GEOSGeomFromHEX_buf_r(GEOSContextHandle_t extHandle, const unsigned char *hex, size_t size)
1531 if ( 0 == extHandle )
1533 return NULL;
1536 GEOSContextHandleInternal_t *handle = 0;
1537 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
1538 if ( 0 == handle->initialized )
1540 return NULL;
1543 using geos::io::WKBReader;
1546 std::string hexstring(reinterpret_cast<const char*>(hex), size);
1547 WKBReader r(*(static_cast<GeometryFactory const*>(handle->geomFactory)));
1548 std::istringstream is(std::ios_base::binary);
1549 is.str(hexstring);
1550 is.seekg(0, std::ios::beg); // rewind reader pointer
1552 Geometry *g = r.readHEX(is);
1553 return g;
1555 catch (const std::exception &e)
1557 handle->ERROR_MESSAGE("%s", e.what());
1559 catch (...)
1561 handle->ERROR_MESSAGE("Unknown exception thrown");
1564 return NULL;
1567 char
1568 GEOSisEmpty_r(GEOSContextHandle_t extHandle, const Geometry *g1)
1570 if ( 0 == extHandle )
1572 return 2;
1575 GEOSContextHandleInternal_t *handle = 0;
1576 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
1577 if ( 0 == handle->initialized )
1579 return 2;
1584 return g1->isEmpty();
1586 catch (const std::exception &e)
1588 handle->ERROR_MESSAGE("%s", e.what());
1590 catch (...)
1592 handle->ERROR_MESSAGE("Unknown exception thrown");
1595 return 2;
1598 char
1599 GEOSisSimple_r(GEOSContextHandle_t extHandle, const Geometry *g1)
1601 if ( 0 == extHandle )
1603 return 2;
1606 GEOSContextHandleInternal_t *handle = 0;
1607 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
1608 if ( 0 == handle->initialized )
1610 return 2;
1615 return g1->isSimple();
1617 catch (const std::exception &e)
1619 handle->ERROR_MESSAGE("%s", e.what());
1620 return 2;
1623 catch (...)
1625 handle->ERROR_MESSAGE("Unknown exception thrown");
1626 return 2;
1630 char
1631 GEOSisRing_r(GEOSContextHandle_t extHandle, const Geometry *g)
1633 if ( 0 == extHandle )
1635 return 2;
1638 GEOSContextHandleInternal_t *handle = 0;
1639 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
1640 if ( 0 == handle->initialized )
1642 return 2;
1647 const LineString *ls = dynamic_cast<const LineString *>(g);
1648 if ( ls ) {
1649 return (ls->isRing());
1650 } else {
1651 return 0;
1654 catch (const std::exception &e)
1656 handle->ERROR_MESSAGE("%s", e.what());
1657 return 2;
1660 catch (...)
1662 handle->ERROR_MESSAGE("Unknown exception thrown");
1663 return 2;
1669 //free the result of this
1670 char *
1671 GEOSGeomType_r(GEOSContextHandle_t extHandle, const Geometry *g1)
1673 if ( 0 == extHandle )
1675 return NULL;
1678 GEOSContextHandleInternal_t *handle = 0;
1679 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
1680 if ( 0 == handle->initialized )
1682 return NULL;
1687 std::string s = g1->getGeometryType();
1689 char *result = gstrdup(s);
1690 return result;
1692 catch (const std::exception &e)
1694 handle->ERROR_MESSAGE("%s", e.what());
1696 catch (...)
1698 handle->ERROR_MESSAGE("Unknown exception thrown");
1701 return NULL;
1704 // Return postgis geometry type index
1706 GEOSGeomTypeId_r(GEOSContextHandle_t extHandle, const Geometry *g1)
1708 if ( 0 == extHandle )
1710 return -1;
1713 GEOSContextHandleInternal_t *handle = 0;
1714 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
1715 if ( 0 == handle->initialized )
1717 return -1;
1722 return g1->getGeometryTypeId();
1724 catch (const std::exception &e)
1726 handle->ERROR_MESSAGE("%s", e.what());
1728 catch (...)
1730 handle->ERROR_MESSAGE("Unknown exception thrown");
1733 return -1;
1736 //-------------------------------------------------------------------
1737 // GEOS functions that return geometries
1738 //-------------------------------------------------------------------
1740 Geometry *
1741 GEOSEnvelope_r(GEOSContextHandle_t extHandle, const Geometry *g1)
1743 if ( 0 == extHandle )
1745 return NULL;
1748 GEOSContextHandleInternal_t *handle = 0;
1749 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
1750 if ( 0 == handle->initialized )
1752 return NULL;
1757 Geometry *g3 = g1->getEnvelope();
1758 return g3;
1760 catch (const std::exception &e)
1762 handle->ERROR_MESSAGE("%s", e.what());
1764 catch (...)
1766 handle->ERROR_MESSAGE("Unknown exception thrown");
1769 return NULL;
1772 Geometry *
1773 GEOSIntersection_r(GEOSContextHandle_t extHandle, const Geometry *g1, const Geometry *g2)
1775 if ( 0 == extHandle )
1777 return NULL;
1780 GEOSContextHandleInternal_t *handle = 0;
1781 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
1782 if ( 0 == handle->initialized )
1784 return NULL;
1789 return g1->intersection(g2);
1791 catch (const std::exception &e)
1793 handle->ERROR_MESSAGE("%s", e.what());
1795 catch (...)
1797 handle->ERROR_MESSAGE("Unknown exception thrown");
1800 return NULL;
1803 Geometry *
1804 GEOSBuffer_r(GEOSContextHandle_t extHandle, const Geometry *g1, double width, int quadrantsegments)
1806 if ( 0 == extHandle )
1808 return NULL;
1811 GEOSContextHandleInternal_t *handle = 0;
1812 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
1813 if ( 0 == handle->initialized )
1815 return NULL;
1820 Geometry *g3 = g1->buffer(width, quadrantsegments);
1821 return g3;
1823 catch (const std::exception &e)
1825 handle->ERROR_MESSAGE("%s", e.what());
1827 catch (...)
1829 handle->ERROR_MESSAGE("Unknown exception thrown");
1832 return NULL;
1835 Geometry *
1836 GEOSBufferWithStyle_r(GEOSContextHandle_t extHandle, const Geometry *g1, double width, int quadsegs, int endCapStyle, int joinStyle, double mitreLimit)
1838 using geos::operation::buffer::BufferParameters;
1839 using geos::operation::buffer::BufferOp;
1840 using geos::util::IllegalArgumentException;
1842 if ( 0 == extHandle )
1844 return NULL;
1847 GEOSContextHandleInternal_t *handle = 0;
1848 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
1849 if ( 0 == handle->initialized )
1851 return NULL;
1856 BufferParameters bp;
1857 bp.setQuadrantSegments(quadsegs);
1859 if ( endCapStyle > BufferParameters::CAP_SQUARE )
1861 throw IllegalArgumentException("Invalid buffer endCap style");
1863 bp.setEndCapStyle(
1864 static_cast<BufferParameters::EndCapStyle>(endCapStyle)
1867 if ( joinStyle > BufferParameters::JOIN_BEVEL )
1869 throw IllegalArgumentException("Invalid buffer join style");
1871 bp.setJoinStyle(
1872 static_cast<BufferParameters::JoinStyle>(joinStyle)
1874 bp.setMitreLimit(mitreLimit);
1875 BufferOp op(g1, bp);
1876 Geometry *g3 = op.getResultGeometry(width);
1877 return g3;
1879 catch (const std::exception &e)
1881 handle->ERROR_MESSAGE("%s", e.what());
1883 catch (...)
1885 handle->ERROR_MESSAGE("Unknown exception thrown");
1888 return NULL;
1891 Geometry *
1892 GEOSOffsetCurve_r(GEOSContextHandle_t extHandle, const Geometry *g1, double width, int quadsegs, int joinStyle, double mitreLimit)
1894 if ( 0 == extHandle ) return NULL;
1896 GEOSContextHandleInternal_t *handle = 0;
1897 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
1898 if ( 0 == handle->initialized ) return NULL;
1902 BufferParameters bp;
1903 bp.setEndCapStyle( BufferParameters::CAP_FLAT );
1904 bp.setQuadrantSegments(quadsegs);
1906 if ( joinStyle > BufferParameters::JOIN_BEVEL )
1908 throw IllegalArgumentException("Invalid buffer join style");
1910 bp.setJoinStyle(
1911 static_cast<BufferParameters::JoinStyle>(joinStyle)
1913 bp.setMitreLimit(mitreLimit);
1915 bool isLeftSide = true;
1916 if ( width < 0 ) {
1917 isLeftSide = false;
1918 width = -width;
1920 BufferBuilder bufBuilder (bp);
1921 Geometry *g3 = bufBuilder.bufferLineSingleSided(g1, width, isLeftSide);
1923 return g3;
1925 catch (const std::exception &e)
1927 handle->ERROR_MESSAGE("%s", e.what());
1929 catch (...)
1931 handle->ERROR_MESSAGE("Unknown exception thrown");
1934 return NULL;
1937 /* @deprecated in 3.3.0 */
1938 Geometry *
1939 GEOSSingleSidedBuffer_r(GEOSContextHandle_t extHandle, const Geometry *g1, double width, int quadsegs, int joinStyle, double mitreLimit, int leftSide)
1941 if ( 0 == extHandle ) return NULL;
1943 GEOSContextHandleInternal_t *handle = 0;
1944 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
1945 if ( 0 == handle->initialized ) return NULL;
1949 BufferParameters bp;
1950 bp.setEndCapStyle( BufferParameters::CAP_FLAT );
1951 bp.setQuadrantSegments(quadsegs);
1953 if ( joinStyle > BufferParameters::JOIN_BEVEL )
1955 throw IllegalArgumentException("Invalid buffer join style");
1957 bp.setJoinStyle(
1958 static_cast<BufferParameters::JoinStyle>(joinStyle)
1960 bp.setMitreLimit(mitreLimit);
1962 bool isLeftSide = leftSide == 0 ? false : true;
1963 BufferBuilder bufBuilder (bp);
1964 Geometry *g3 = bufBuilder.bufferLineSingleSided(g1, width, isLeftSide);
1966 return g3;
1968 catch (const std::exception &e)
1970 handle->ERROR_MESSAGE("%s", e.what());
1972 catch (...)
1974 handle->ERROR_MESSAGE("Unknown exception thrown");
1977 return NULL;
1980 Geometry *
1981 GEOSConvexHull_r(GEOSContextHandle_t extHandle, const Geometry *g1)
1983 if ( 0 == extHandle )
1985 return NULL;
1988 GEOSContextHandleInternal_t *handle = 0;
1989 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
1990 if ( 0 == handle->initialized )
1992 return NULL;
1997 Geometry *g3 = g1->convexHull();
1998 return g3;
2000 catch (const std::exception &e)
2002 handle->ERROR_MESSAGE("%s", e.what());
2004 catch (...)
2006 handle->ERROR_MESSAGE("Unknown exception thrown");
2009 return NULL;
2013 Geometry *
2014 GEOSMinimumRotatedRectangle_r(GEOSContextHandle_t extHandle, const Geometry *g)
2016 if ( 0 == extHandle )
2018 return NULL;
2021 GEOSContextHandleInternal_t *handle = 0;
2022 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
2023 if ( 0 == handle->initialized )
2025 return NULL;
2030 geos::algorithm::MinimumDiameter m(g);
2032 Geometry *g3 = m.getMinimumRectangle();
2033 return g3;
2035 catch (const std::exception &e)
2037 handle->ERROR_MESSAGE("%s", e.what());
2039 catch (...)
2041 handle->ERROR_MESSAGE("Unknown exception thrown");
2044 return NULL;
2047 Geometry *
2048 GEOSMinimumWidth_r(GEOSContextHandle_t extHandle, const Geometry *g)
2050 if ( 0 == extHandle )
2052 return NULL;
2055 GEOSContextHandleInternal_t *handle = 0;
2056 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
2057 if ( 0 == handle->initialized )
2059 return NULL;
2064 geos::algorithm::MinimumDiameter m(g);
2066 Geometry *g3 = m.getDiameter();
2067 return g3;
2069 catch (const std::exception &e)
2071 handle->ERROR_MESSAGE("%s", e.what());
2073 catch (...)
2075 handle->ERROR_MESSAGE("Unknown exception thrown");
2078 return NULL;
2082 Geometry *
2083 GEOSDifference_r(GEOSContextHandle_t extHandle, const Geometry *g1, const Geometry *g2)
2085 if ( 0 == extHandle )
2087 return NULL;
2090 GEOSContextHandleInternal_t *handle = 0;
2091 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
2092 if ( 0 == handle->initialized )
2094 return NULL;
2099 return g1->difference(g2);
2101 catch (const std::exception &e)
2103 handle->ERROR_MESSAGE("%s", e.what());
2105 catch (...)
2107 handle->ERROR_MESSAGE("Unknown exception thrown");
2110 return NULL;
2113 Geometry *
2114 GEOSBoundary_r(GEOSContextHandle_t extHandle, const Geometry *g1)
2116 if ( 0 == extHandle )
2118 return NULL;
2121 GEOSContextHandleInternal_t *handle = 0;
2122 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
2123 if ( 0 == handle->initialized )
2125 return NULL;
2130 Geometry *g3 = g1->getBoundary();
2131 return g3;
2133 catch (const std::exception &e)
2135 handle->ERROR_MESSAGE("%s", e.what());
2137 catch (...)
2139 handle->ERROR_MESSAGE("Unknown exception thrown");
2142 return NULL;
2145 Geometry *
2146 GEOSSymDifference_r(GEOSContextHandle_t extHandle, const Geometry *g1, const Geometry *g2)
2148 if ( 0 == extHandle )
2150 return NULL;
2153 GEOSContextHandleInternal_t *handle = 0;
2154 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
2155 if ( 0 == handle->initialized )
2157 return NULL;
2162 return g1->symDifference(g2);
2164 catch (const std::exception &e)
2166 handle->ERROR_MESSAGE("%s", e.what());
2167 return NULL;
2170 catch (...)
2172 handle->ERROR_MESSAGE("Unknown exception thrown");
2173 return NULL;
2177 Geometry *
2178 GEOSUnion_r(GEOSContextHandle_t extHandle, const Geometry *g1, const Geometry *g2)
2180 if ( 0 == extHandle )
2182 return NULL;
2185 GEOSContextHandleInternal_t *handle = 0;
2186 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
2187 if ( 0 == handle->initialized )
2189 return NULL;
2194 return g1->Union(g2);
2196 catch (const std::exception &e)
2198 #if VERBOSE_EXCEPTIONS
2199 std::ostringstream s;
2200 s << "Exception on GEOSUnion with following inputs:" << std::endl;
2201 s << "A: "<<g1->toString() << std::endl;
2202 s << "B: "<<g2->toString() << std::endl;
2203 handle->NOTICE_MESSAGE("%s", s.str().c_str());
2204 #endif // VERBOSE_EXCEPTIONS
2205 handle->ERROR_MESSAGE("%s", e.what());
2207 catch (...)
2209 handle->ERROR_MESSAGE("Unknown exception thrown");
2212 return NULL;
2215 Geometry *
2216 GEOSUnaryUnion_r(GEOSContextHandle_t extHandle, const Geometry *g)
2218 if ( 0 == extHandle )
2220 return NULL;
2223 GEOSContextHandleInternal_t *handle = 0;
2224 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
2225 if ( 0 == handle->initialized )
2227 return NULL;
2232 GeomAutoPtr g3 ( g->Union() );
2233 return g3.release();
2235 catch (const std::exception &e)
2237 #if VERBOSE_EXCEPTIONS
2238 std::ostringstream s;
2239 s << "Exception on GEOSUnaryUnion with following inputs:" << std::endl;
2240 s << "A: "<<g1->toString() << std::endl;
2241 s << "B: "<<g2->toString() << std::endl;
2242 handle->NOTICE_MESSAGE("%s", s.str().c_str());
2243 #endif // VERBOSE_EXCEPTIONS
2244 handle->ERROR_MESSAGE("%s", e.what());
2246 catch (...)
2248 handle->ERROR_MESSAGE("Unknown exception thrown");
2251 return NULL;
2254 Geometry *
2255 GEOSNode_r(GEOSContextHandle_t extHandle, const Geometry *g)
2257 if ( 0 == extHandle )
2259 return NULL;
2262 GEOSContextHandleInternal_t *handle = 0;
2263 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
2264 if ( 0 == handle->initialized )
2266 return NULL;
2271 std::auto_ptr<Geometry> g3 = geos::noding::GeometryNoder::node(*g);
2272 return g3.release();
2274 catch (const std::exception &e)
2276 #if VERBOSE_EXCEPTIONS
2277 std::ostringstream s;
2278 s << "Exception on GEOSUnaryUnion with following inputs:" << std::endl;
2279 s << "A: "<<g1->toString() << std::endl;
2280 s << "B: "<<g2->toString() << std::endl;
2281 handle->NOTICE_MESSAGE("%s", s.str().c_str());
2282 #endif // VERBOSE_EXCEPTIONS
2283 handle->ERROR_MESSAGE("%s", e.what());
2285 catch (...)
2287 handle->ERROR_MESSAGE("Unknown exception thrown");
2290 return NULL;
2293 Geometry *
2294 GEOSUnionCascaded_r(GEOSContextHandle_t extHandle, const Geometry *g1)
2296 if ( 0 == extHandle )
2298 return NULL;
2301 GEOSContextHandleInternal_t *handle = 0;
2302 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
2303 if ( 0 == handle->initialized )
2305 return NULL;
2310 const geos::geom::MultiPolygon *p = dynamic_cast<const geos::geom::MultiPolygon *>(g1);
2311 if ( ! p )
2313 handle->ERROR_MESSAGE("Invalid argument (must be a MultiPolygon)");
2314 return NULL;
2317 using geos::operation::geounion::CascadedPolygonUnion;
2318 return CascadedPolygonUnion::Union(p);
2320 catch (const std::exception &e)
2322 handle->ERROR_MESSAGE("%s", e.what());
2324 catch (...)
2326 handle->ERROR_MESSAGE("Unknown exception thrown");
2329 return NULL;
2332 Geometry *
2333 GEOSPointOnSurface_r(GEOSContextHandle_t extHandle, const Geometry *g1)
2335 if ( 0 == extHandle )
2337 return NULL;
2340 GEOSContextHandleInternal_t *handle = 0;
2341 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
2342 if ( 0 == handle->initialized )
2344 return NULL;
2349 Geometry *ret = g1->getInteriorPoint();
2350 if ( ! ret )
2352 const GeometryFactory* gf = handle->geomFactory;
2353 // return an empty point
2354 return gf->createPoint();
2356 return ret;
2358 catch (const std::exception &e)
2360 handle->ERROR_MESSAGE("%s", e.what());
2362 catch (...)
2364 handle->ERROR_MESSAGE("Unknown exception thrown");
2367 return NULL;
2370 Geometry *
2371 GEOSClipByRect_r(GEOSContextHandle_t extHandle, const Geometry *g, double xmin, double ymin, double xmax, double ymax)
2373 if ( 0 == extHandle )
2375 return NULL;
2378 GEOSContextHandleInternal_t *handle = 0;
2379 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
2380 if ( 0 == handle->initialized )
2382 return NULL;
2387 using geos::operation::intersection::Rectangle;
2388 using geos::operation::intersection::RectangleIntersection;
2389 Rectangle rect(xmin, ymin, xmax, ymax);
2390 std::auto_ptr<Geometry> g3 = RectangleIntersection::clip(*g, rect);
2391 return g3.release();
2393 catch (const std::exception &e)
2395 #if VERBOSE_EXCEPTIONS
2396 std::ostringstream s;
2397 s << "Exception on GEOSClipByRect with following inputs:" << std::endl;
2398 s << "A: "<<g1->toString() << std::endl;
2399 s << "B: "<<g2->toString() << std::endl;
2400 handle->NOTICE_MESSAGE("%s", s.str().c_str());
2401 #endif // VERBOSE_EXCEPTIONS
2402 handle->ERROR_MESSAGE("%s", e.what());
2404 catch (...)
2406 handle->ERROR_MESSAGE("Unknown exception thrown");
2409 return NULL;
2412 //-------------------------------------------------------------------
2413 // memory management functions
2414 //------------------------------------------------------------------
2416 void
2417 GEOSGeom_destroy_r(GEOSContextHandle_t extHandle, Geometry *a)
2419 GEOSContextHandleInternal_t *handle = 0;
2421 // FIXME: mloskot: Does this try-catch around delete means that
2422 // destructors in GEOS may throw? If it does, this is a serious
2423 // violation of "never throw an exception from a destructor" principle
2427 delete a;
2429 catch (const std::exception &e)
2431 if ( 0 == extHandle )
2433 return;
2436 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
2437 if ( 0 == handle->initialized )
2439 return;
2442 handle->ERROR_MESSAGE("%s", e.what());
2444 catch (...)
2446 if ( 0 == extHandle )
2448 return;
2451 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
2452 if ( 0 == handle->initialized )
2454 return;
2457 handle->ERROR_MESSAGE("Unknown exception thrown");
2461 void
2462 GEOSSetSRID_r(GEOSContextHandle_t extHandle, Geometry *g, int srid)
2464 assert(0 != g);
2466 if ( 0 == extHandle )
2468 return;
2471 GEOSContextHandleInternal_t *handle = 0;
2472 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
2473 if ( 0 == handle->initialized )
2475 return;
2478 g->setSRID(srid);
2483 GEOSGetNumCoordinates_r(GEOSContextHandle_t extHandle, const Geometry *g)
2485 assert(0 != g);
2487 if ( 0 == extHandle )
2489 return -1;
2492 GEOSContextHandleInternal_t *handle = 0;
2493 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
2494 if ( 0 == handle->initialized )
2496 return -1;
2501 return static_cast<int>(g->getNumPoints());
2503 catch (const std::exception &e)
2505 handle->ERROR_MESSAGE("%s", e.what());
2507 catch (...)
2509 handle->ERROR_MESSAGE("Unknown exception thrown");
2512 return -1;
2516 * Return -1 on exception, 0 otherwise.
2517 * Converts Geometry to normal form (or canonical form).
2520 GEOSNormalize_r(GEOSContextHandle_t extHandle, Geometry *g)
2522 assert(0 != g);
2524 if ( 0 == extHandle )
2526 return -1;
2529 GEOSContextHandleInternal_t *handle = 0;
2530 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
2531 if ( 0 == handle->initialized )
2533 return -1;
2538 g->normalize();
2539 return 0; // SUCCESS
2541 catch (const std::exception &e)
2543 handle->ERROR_MESSAGE("%s", e.what());
2545 catch (...)
2547 handle->ERROR_MESSAGE("Unknown exception thrown");
2550 return -1;
2554 GEOSGetNumInteriorRings_r(GEOSContextHandle_t extHandle, const Geometry *g1)
2556 if ( 0 == extHandle )
2558 return -1;
2561 GEOSContextHandleInternal_t *handle = 0;
2562 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
2563 if ( 0 == handle->initialized )
2565 return -1;
2570 const Polygon *p = dynamic_cast<const Polygon *>(g1);
2571 if ( ! p )
2573 handle->ERROR_MESSAGE("Argument is not a Polygon");
2574 return -1;
2576 return static_cast<int>(p->getNumInteriorRing());
2578 catch (const std::exception &e)
2580 handle->ERROR_MESSAGE("%s", e.what());
2582 catch (...)
2584 handle->ERROR_MESSAGE("Unknown exception thrown");
2587 return -1;
2591 // returns -1 on error and 1 for non-multi geometries
2593 GEOSGetNumGeometries_r(GEOSContextHandle_t extHandle, const Geometry *g1)
2595 if ( 0 == extHandle )
2597 return -1;
2600 GEOSContextHandleInternal_t *handle = 0;
2601 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
2602 if ( 0 == handle->initialized )
2604 return -1;
2609 return static_cast<int>(g1->getNumGeometries());
2611 catch (const std::exception &e)
2613 handle->ERROR_MESSAGE("%s", e.what());
2615 catch (...)
2617 handle->ERROR_MESSAGE("Unknown exception thrown");
2620 return -1;
2625 * Call only on GEOMETRYCOLLECTION or MULTI*.
2626 * Return a pointer to the internal Geometry.
2628 const Geometry *
2629 GEOSGetGeometryN_r(GEOSContextHandle_t extHandle, const Geometry *g1, int n)
2631 if ( 0 == extHandle )
2633 return NULL;
2636 GEOSContextHandleInternal_t *handle = 0;
2637 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
2638 if ( 0 == handle->initialized )
2640 return NULL;
2645 return g1->getGeometryN(n);
2647 catch (const std::exception &e)
2649 handle->ERROR_MESSAGE("%s", e.what());
2651 catch (...)
2653 handle->ERROR_MESSAGE("Unknown exception thrown");
2656 return NULL;
2660 * Call only on LINESTRING
2661 * Returns NULL on exception
2663 Geometry *
2664 GEOSGeomGetPointN_r(GEOSContextHandle_t extHandle, const Geometry *g1, int n)
2666 if ( 0 == extHandle )
2668 return NULL;
2671 GEOSContextHandleInternal_t *handle = 0;
2672 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
2673 if ( 0 == handle->initialized )
2675 return NULL;
2680 using geos::geom::LineString;
2681 const LineString *ls = dynamic_cast<const LineString *>(g1);
2682 if ( ! ls )
2684 handle->ERROR_MESSAGE("Argument is not a LineString");
2685 return NULL;
2687 return ls->getPointN(n);
2689 catch (const std::exception &e)
2691 handle->ERROR_MESSAGE("%s", e.what());
2693 catch (...)
2695 handle->ERROR_MESSAGE("Unknown exception thrown");
2698 return NULL;
2702 * Call only on LINESTRING
2704 Geometry *
2705 GEOSGeomGetStartPoint_r(GEOSContextHandle_t extHandle, const Geometry *g1)
2707 if ( 0 == extHandle )
2709 return NULL;
2712 GEOSContextHandleInternal_t *handle = 0;
2713 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
2714 if ( 0 == handle->initialized )
2716 return NULL;
2721 using geos::geom::LineString;
2722 const LineString *ls = dynamic_cast<const LineString *>(g1);
2723 if ( ! ls )
2725 handle->ERROR_MESSAGE("Argument is not a LineString");
2726 return NULL;
2728 return ls->getStartPoint();
2730 catch (const std::exception &e)
2732 handle->ERROR_MESSAGE("%s", e.what());
2734 catch (...)
2736 handle->ERROR_MESSAGE("Unknown exception thrown");
2739 return NULL;
2743 * Call only on LINESTRING
2745 Geometry *
2746 GEOSGeomGetEndPoint_r(GEOSContextHandle_t extHandle, const Geometry *g1)
2748 if ( 0 == extHandle )
2750 return NULL;
2753 GEOSContextHandleInternal_t *handle = 0;
2754 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
2755 if ( 0 == handle->initialized )
2757 return NULL;
2762 using geos::geom::LineString;
2763 const LineString *ls = dynamic_cast<const LineString *>(g1);
2764 if ( ! ls )
2766 handle->ERROR_MESSAGE("Argument is not a LineString");
2767 return NULL;
2769 return ls->getEndPoint();
2771 catch (const std::exception &e)
2773 handle->ERROR_MESSAGE("%s", e.what());
2775 catch (...)
2777 handle->ERROR_MESSAGE("Unknown exception thrown");
2780 return NULL;
2784 * Call only on LINESTRING or MULTILINESTRING
2785 * return 2 on exception, 1 on true, 0 on false
2787 char
2788 GEOSisClosed_r(GEOSContextHandle_t extHandle, const Geometry *g1)
2790 if ( 0 == extHandle )
2792 return 2;
2795 GEOSContextHandleInternal_t *handle = 0;
2796 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
2797 if ( 0 == handle->initialized )
2799 return 2;
2804 using geos::geom::LineString;
2805 using geos::geom::MultiLineString;
2807 const LineString *ls = dynamic_cast<const LineString *>(g1);
2808 if ( ls ) {
2809 return ls->isClosed();
2812 const MultiLineString *mls = dynamic_cast<const MultiLineString *>(g1);
2813 if ( mls ) {
2814 return mls->isClosed();
2817 handle->ERROR_MESSAGE("Argument is not a LineString or MultiLineString");
2818 return 2;
2820 catch (const std::exception &e)
2822 handle->ERROR_MESSAGE("%s", e.what());
2824 catch (...)
2826 handle->ERROR_MESSAGE("Unknown exception thrown");
2829 return 2;
2833 * Call only on LINESTRING
2834 * return 0 on exception, otherwise 1
2837 GEOSGeomGetLength_r(GEOSContextHandle_t extHandle, const Geometry *g1, double *length)
2839 if ( 0 == extHandle )
2841 return 0;
2844 GEOSContextHandleInternal_t *handle = 0;
2845 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
2846 if ( 0 == handle->initialized )
2848 return 0;
2853 using geos::geom::LineString;
2854 const LineString *ls = dynamic_cast<const LineString *>(g1);
2855 if ( ! ls )
2857 handle->ERROR_MESSAGE("Argument is not a LineString");
2858 return 0;
2860 *length = ls->getLength();
2861 return 1;
2863 catch (const std::exception &e)
2865 handle->ERROR_MESSAGE("%s", e.what());
2867 catch (...)
2869 handle->ERROR_MESSAGE("Unknown exception thrown");
2872 return 0;
2876 * Call only on LINESTRING
2879 GEOSGeomGetNumPoints_r(GEOSContextHandle_t extHandle, const Geometry *g1)
2881 if ( 0 == extHandle )
2883 return -1;
2886 GEOSContextHandleInternal_t *handle = 0;
2887 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
2888 if ( 0 == handle->initialized )
2890 return -1;
2895 using geos::geom::LineString;
2896 const LineString *ls = dynamic_cast<const LineString *>(g1);
2897 if ( ! ls )
2899 handle->ERROR_MESSAGE("Argument is not a LineString");
2900 return -1;
2902 return static_cast<int>(ls->getNumPoints());
2904 catch (const std::exception &e)
2906 handle->ERROR_MESSAGE("%s", e.what());
2908 catch (...)
2910 handle->ERROR_MESSAGE("Unknown exception thrown");
2913 return -1;
2917 * For POINT
2918 * returns 0 on exception, otherwise 1
2921 GEOSGeomGetX_r(GEOSContextHandle_t extHandle, const Geometry *g1, double *x)
2923 if ( 0 == extHandle )
2925 return 0;
2928 GEOSContextHandleInternal_t *handle = 0;
2929 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
2930 if ( 0 == handle->initialized )
2932 return 0;
2937 using geos::geom::Point;
2938 const Point *po = dynamic_cast<const Point *>(g1);
2939 if ( ! po )
2941 handle->ERROR_MESSAGE("Argument is not a Point");
2942 return 0;
2944 *x = po->getX();
2945 return 1;
2947 catch (const std::exception &e)
2949 handle->ERROR_MESSAGE("%s", e.what());
2951 catch (...)
2953 handle->ERROR_MESSAGE("Unknown exception thrown");
2956 return 0;
2960 * For POINT
2961 * returns 0 on exception, otherwise 1
2964 GEOSGeomGetY_r(GEOSContextHandle_t extHandle, const Geometry *g1, double *y)
2966 if ( 0 == extHandle )
2968 return 0;
2971 GEOSContextHandleInternal_t *handle = 0;
2972 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
2973 if ( 0 == handle->initialized )
2975 return 0;
2980 using geos::geom::Point;
2981 const Point *po = dynamic_cast<const Point *>(g1);
2982 if ( ! po )
2984 handle->ERROR_MESSAGE("Argument is not a Point");
2985 return 0;
2987 *y = po->getY();
2988 return 1;
2990 catch (const std::exception &e)
2992 handle->ERROR_MESSAGE("%s", e.what());
2994 catch (...)
2996 handle->ERROR_MESSAGE("Unknown exception thrown");
2999 return 0;
3003 * Call only on polygon
3004 * Return a copy of the internal Geometry.
3006 const Geometry *
3007 GEOSGetExteriorRing_r(GEOSContextHandle_t extHandle, const Geometry *g1)
3009 if ( 0 == extHandle )
3011 return NULL;
3014 GEOSContextHandleInternal_t *handle = 0;
3015 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
3016 if ( 0 == handle->initialized )
3018 return NULL;
3023 const Polygon *p = dynamic_cast<const Polygon *>(g1);
3024 if ( ! p )
3026 handle->ERROR_MESSAGE("Invalid argument (must be a Polygon)");
3027 return NULL;
3029 return p->getExteriorRing();
3031 catch (const std::exception &e)
3033 handle->ERROR_MESSAGE("%s", e.what());
3035 catch (...)
3037 handle->ERROR_MESSAGE("Unknown exception thrown");
3040 return NULL;
3044 * Call only on polygon
3045 * Return a pointer to internal storage, do not destroy it.
3047 const Geometry *
3048 GEOSGetInteriorRingN_r(GEOSContextHandle_t extHandle, const Geometry *g1, int n)
3050 if ( 0 == extHandle )
3052 return NULL;
3055 GEOSContextHandleInternal_t *handle = 0;
3056 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
3057 if ( 0 == handle->initialized )
3059 return NULL;
3064 const Polygon *p = dynamic_cast<const Polygon *>(g1);
3065 if ( ! p )
3067 handle->ERROR_MESSAGE("Invalid argument (must be a Polygon)");
3068 return NULL;
3070 return p->getInteriorRingN(n);
3072 catch (const std::exception &e)
3074 handle->ERROR_MESSAGE("%s", e.what());
3076 catch (...)
3078 handle->ERROR_MESSAGE("Unknown exception thrown");
3081 return NULL;
3084 Geometry *
3085 GEOSGetCentroid_r(GEOSContextHandle_t extHandle, const Geometry *g)
3087 if ( 0 == extHandle )
3089 return NULL;
3092 GEOSContextHandleInternal_t *handle = 0;
3093 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
3094 if ( 0 == handle->initialized )
3096 return NULL;
3101 Geometry *ret = g->getCentroid();
3102 if (0 == ret)
3104 const GeometryFactory *gf = handle->geomFactory;
3105 return gf->createPoint();
3107 return ret;
3109 catch (const std::exception &e)
3111 handle->ERROR_MESSAGE("%s", e.what());
3113 catch (...)
3115 handle->ERROR_MESSAGE("Unknown exception thrown");
3118 return NULL;
3121 Geometry *
3122 GEOSGeom_createEmptyCollection_r(GEOSContextHandle_t extHandle, int type)
3124 if ( 0 == extHandle )
3126 return NULL;
3129 GEOSContextHandleInternal_t *handle = 0;
3130 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
3131 if ( 0 == handle->initialized )
3133 return NULL;
3136 #ifdef GEOS_DEBUG
3137 char buf[256];
3138 sprintf(buf, "createCollection: requested type %d", type);
3139 handle->NOTICE_MESSAGE("%s", buf);// TODO: Can handle->NOTICE_MESSAGE format that directly?
3140 #endif
3144 const GeometryFactory* gf = handle->geomFactory;
3146 Geometry *g = 0;
3147 switch (type)
3149 case GEOS_GEOMETRYCOLLECTION:
3150 g = gf->createGeometryCollection();
3151 break;
3152 case GEOS_MULTIPOINT:
3153 g = gf->createMultiPoint();
3154 break;
3155 case GEOS_MULTILINESTRING:
3156 g = gf->createMultiLineString();
3157 break;
3158 case GEOS_MULTIPOLYGON:
3159 g = gf->createMultiPolygon();
3160 break;
3161 default:
3162 handle->ERROR_MESSAGE("Unsupported type request for GEOSGeom_createEmptyCollection_r");
3163 g = 0;
3167 return g;
3169 catch (const std::exception &e)
3171 handle->ERROR_MESSAGE("%s", e.what());
3173 catch (...)
3175 handle->ERROR_MESSAGE("Unknown exception thrown");
3178 return 0;
3181 Geometry *
3182 GEOSGeom_createCollection_r(GEOSContextHandle_t extHandle, int type, Geometry **geoms, unsigned int ngeoms)
3184 if ( 0 == extHandle )
3186 return NULL;
3189 GEOSContextHandleInternal_t *handle = 0;
3190 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
3191 if ( 0 == handle->initialized )
3193 return NULL;
3196 #ifdef GEOS_DEBUG
3197 char buf[256];
3198 sprintf(buf, "PostGIS2GEOS_collection: requested type %d, ngeoms: %d",
3199 type, ngeoms);
3200 handle->NOTICE_MESSAGE("%s", buf);// TODO: Can handle->NOTICE_MESSAGE format that directly?
3201 #endif
3205 const GeometryFactory* gf = handle->geomFactory;
3206 std::vector<Geometry*>* vgeoms = new std::vector<Geometry*>(geoms, geoms + ngeoms);
3208 Geometry *g = 0;
3209 switch (type)
3211 case GEOS_GEOMETRYCOLLECTION:
3212 g = gf->createGeometryCollection(vgeoms);
3213 break;
3214 case GEOS_MULTIPOINT:
3215 g = gf->createMultiPoint(vgeoms);
3216 break;
3217 case GEOS_MULTILINESTRING:
3218 g = gf->createMultiLineString(vgeoms);
3219 break;
3220 case GEOS_MULTIPOLYGON:
3221 g = gf->createMultiPolygon(vgeoms);
3222 break;
3223 default:
3224 handle->ERROR_MESSAGE("Unsupported type request for PostGIS2GEOS_collection");
3225 delete vgeoms;
3226 g = 0;
3230 return g;
3232 catch (const std::exception &e)
3234 handle->ERROR_MESSAGE("%s", e.what());
3236 catch (...)
3238 handle->ERROR_MESSAGE("Unknown exception thrown");
3241 return 0;
3244 Geometry *
3245 GEOSPolygonize_r(GEOSContextHandle_t extHandle, const Geometry * const * g, unsigned int ngeoms)
3247 if ( 0 == extHandle )
3249 return 0;
3252 GEOSContextHandleInternal_t *handle = 0;
3253 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
3254 if ( 0 == handle->initialized )
3256 return 0;
3259 Geometry *out = 0;
3263 // Polygonize
3264 using geos::operation::polygonize::Polygonizer;
3265 Polygonizer plgnzr;
3266 for (std::size_t i = 0; i < ngeoms; ++i)
3268 plgnzr.add(g[i]);
3271 #if GEOS_DEBUG
3272 handle->NOTICE_MESSAGE("geometry vector added to polygonizer");
3273 #endif
3275 std::vector<Polygon*> *polys = plgnzr.getPolygons();
3276 assert(0 != polys);
3278 #if GEOS_DEBUG
3279 handle->NOTICE_MESSAGE("output polygons got");
3280 #endif
3282 // We need a vector of Geometry pointers, not Polygon pointers.
3283 // STL vector doesn't allow transparent upcast of this
3284 // nature, so we explicitly convert.
3285 // (it's just a waste of processor and memory, btw)
3287 // XXX mloskot: Why not to extent GeometryFactory to accept
3288 // vector of polygons or extend Polygonizer to return list of Geometry*
3289 // or add a wrapper which semantic is similar to:
3290 // std::vector<as_polygon<Geometry*> >
3291 std::vector<Geometry*> *polyvec = new std::vector<Geometry *>(polys->size());
3293 for (std::size_t i = 0; i < polys->size(); ++i)
3295 (*polyvec)[i] = (*polys)[i];
3297 delete polys;
3298 polys = 0;
3300 const GeometryFactory *gf = handle->geomFactory;
3302 // The below takes ownership of the passed vector,
3303 // so we must *not* delete it
3304 out = gf->createGeometryCollection(polyvec);
3306 catch (const std::exception &e)
3308 handle->ERROR_MESSAGE("%s", e.what());
3310 catch (...)
3312 handle->ERROR_MESSAGE("Unknown exception thrown");
3315 return out;
3318 Geometry *
3319 GEOSPolygonizer_getCutEdges_r(GEOSContextHandle_t extHandle, const Geometry * const * g, unsigned int ngeoms)
3321 if ( 0 == extHandle )
3323 return 0;
3326 GEOSContextHandleInternal_t *handle = 0;
3327 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
3328 if ( 0 == handle->initialized )
3330 return 0;
3333 Geometry *out = 0;
3337 // Polygonize
3338 using geos::operation::polygonize::Polygonizer;
3339 Polygonizer plgnzr;
3340 for (std::size_t i = 0; i < ngeoms; ++i)
3342 plgnzr.add(g[i]);
3345 #if GEOS_DEBUG
3346 handle->NOTICE_MESSAGE("geometry vector added to polygonizer");
3347 #endif
3349 const std::vector<const LineString *>& lines = plgnzr.getCutEdges();
3351 #if GEOS_DEBUG
3352 handle->NOTICE_MESSAGE("output polygons got");
3353 #endif
3355 // We need a vector of Geometry pointers, not Polygon pointers.
3356 // STL vector doesn't allow transparent upcast of this
3357 // nature, so we explicitly convert.
3358 // (it's just a waste of processor and memory, btw)
3359 // XXX mloskot: See comment for GEOSPolygonize_r
3360 std::vector<Geometry*> *linevec = new std::vector<Geometry *>(lines.size());
3362 for (std::size_t i = 0, n=lines.size(); i < n; ++i)
3364 (*linevec)[i] = lines[i]->clone();
3367 const GeometryFactory *gf = handle->geomFactory;
3369 // The below takes ownership of the passed vector,
3370 // so we must *not* delete it
3371 out = gf->createGeometryCollection(linevec);
3373 catch (const std::exception &e)
3375 handle->ERROR_MESSAGE("%s", e.what());
3377 catch (...)
3379 handle->ERROR_MESSAGE("Unknown exception thrown");
3382 return out;
3385 Geometry *
3386 GEOSPolygonize_full_r(GEOSContextHandle_t extHandle, const Geometry* g,
3387 Geometry** cuts, Geometry** dangles, Geometry** invalid)
3389 if ( 0 == extHandle )
3391 return 0;
3394 GEOSContextHandleInternal_t *handle = 0;
3395 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
3396 if ( 0 == handle->initialized )
3398 return 0;
3403 // Polygonize
3404 using geos::operation::polygonize::Polygonizer;
3405 Polygonizer plgnzr;
3406 for (std::size_t i = 0; i <g->getNumGeometries(); ++i)
3408 plgnzr.add(g->getGeometryN(i));
3411 #if GEOS_DEBUG
3412 handle->NOTICE_MESSAGE("geometry vector added to polygonizer");
3413 #endif
3414 const GeometryFactory *gf = handle->geomFactory;
3416 if ( cuts ) {
3418 const std::vector<const LineString *>& lines = plgnzr.getCutEdges();
3419 std::vector<Geometry*> *linevec = new std::vector<Geometry *>(lines.size());
3420 for (std::size_t i = 0, n=lines.size(); i < n; ++i)
3422 (*linevec)[i] = lines[i]->clone();
3425 // The below takes ownership of the passed vector,
3426 // so we must *not* delete it
3427 *cuts = gf->createGeometryCollection(linevec);
3430 if ( dangles ) {
3432 const std::vector<const LineString *>& lines = plgnzr.getDangles();
3433 std::vector<Geometry*> *linevec = new std::vector<Geometry *>(lines.size());
3434 for (std::size_t i = 0, n=lines.size(); i < n; ++i)
3436 (*linevec)[i] = lines[i]->clone();
3439 // The below takes ownership of the passed vector,
3440 // so we must *not* delete it
3441 *dangles = gf->createGeometryCollection(linevec);
3444 if ( invalid ) {
3446 const std::vector<LineString *>& lines = plgnzr.getInvalidRingLines();
3447 std::vector<Geometry*> *linevec = new std::vector<Geometry *>(lines.size());
3448 for (std::size_t i = 0, n=lines.size(); i < n; ++i)
3450 (*linevec)[i] = lines[i]->clone();
3453 // The below takes ownership of the passed vector,
3454 // so we must *not* delete it
3455 *invalid = gf->createGeometryCollection(linevec);
3458 std::vector<Polygon*> *polys = plgnzr.getPolygons();
3459 std::vector<Geometry*> *polyvec = new std::vector<Geometry *>(polys->size());
3460 for (std::size_t i = 0; i < polys->size(); ++i)
3462 (*polyvec)[i] = (*polys)[i];
3464 delete polys;
3466 return gf->createGeometryCollection(polyvec);
3469 catch (const std::exception &e)
3471 handle->ERROR_MESSAGE("%s", e.what());
3472 return 0;
3474 catch (...)
3476 handle->ERROR_MESSAGE("Unknown exception thrown");
3477 return 0;
3481 Geometry *
3482 GEOSLineMerge_r(GEOSContextHandle_t extHandle, const Geometry *g)
3484 if ( 0 == extHandle )
3486 return 0;
3489 GEOSContextHandleInternal_t *handle = 0;
3490 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
3491 if ( 0 == handle->initialized )
3493 return 0;
3496 Geometry *out = 0;
3500 using geos::operation::linemerge::LineMerger;
3501 LineMerger lmrgr;
3502 lmrgr.add(g);
3504 std::vector<LineString *>* lines = lmrgr.getMergedLineStrings();
3505 assert(0 != lines);
3507 #if GEOS_DEBUG
3508 handle->NOTICE_MESSAGE("output lines got");
3509 #endif
3511 std::vector<Geometry *>*geoms = new std::vector<Geometry *>(lines->size());
3512 for (std::vector<Geometry *>::size_type i = 0; i < lines->size(); ++i)
3514 (*geoms)[i] = (*lines)[i];
3516 delete lines;
3517 lines = 0;
3519 const GeometryFactory *gf = handle->geomFactory;
3520 out = gf->buildGeometry(geoms);
3522 // XXX: old version
3523 //out = gf->createGeometryCollection(geoms);
3525 catch (const std::exception &e)
3527 handle->ERROR_MESSAGE("%s", e.what());
3529 catch (...)
3531 handle->ERROR_MESSAGE("Unknown exception thrown");
3534 return out;
3538 GEOSGetSRID_r(GEOSContextHandle_t extHandle, const Geometry *g)
3540 assert(0 != g);
3542 if ( 0 == extHandle )
3544 return 0;
3547 GEOSContextHandleInternal_t *handle = 0;
3548 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
3549 if ( 0 == handle->initialized )
3551 return 0;
3556 return g->getSRID();
3558 catch (const std::exception &e)
3560 handle->ERROR_MESSAGE("%s", e.what());
3562 catch (...)
3564 handle->ERROR_MESSAGE("Unknown exception thrown");
3567 return 0;
3570 const char* GEOSversion()
3572 static char version[256];
3573 sprintf(version, "%s r%d", GEOS_CAPI_VERSION, GEOS_SVN_REVISION);
3574 return version;
3577 const char* GEOSjtsport()
3579 return GEOS_JTS_PORT;
3582 char
3583 GEOSHasZ_r(GEOSContextHandle_t extHandle, const Geometry *g)
3585 assert(0 != g);
3587 if ( 0 == extHandle )
3589 return -1;
3592 GEOSContextHandleInternal_t *handle = 0;
3593 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
3594 if ( 0 == handle->initialized )
3596 return -1;
3599 if (g->isEmpty())
3601 return false;
3603 assert(0 != g->getCoordinate());
3605 double az = g->getCoordinate()->z;
3606 //handle->ERROR_MESSAGE("ZCoord: %g", az);
3608 return static_cast<char>(FINITE(az));
3612 GEOS_getWKBOutputDims_r(GEOSContextHandle_t extHandle)
3614 if ( 0 == extHandle )
3616 return -1;
3619 GEOSContextHandleInternal_t *handle = 0;
3620 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
3621 if ( 0 == handle->initialized )
3623 return -1;
3626 return handle->WKBOutputDims;
3630 GEOS_setWKBOutputDims_r(GEOSContextHandle_t extHandle, int newdims)
3632 if ( 0 == extHandle )
3634 return -1;
3637 GEOSContextHandleInternal_t *handle = 0;
3638 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
3639 if ( 0 == handle->initialized )
3641 return -1;
3644 if ( newdims < 2 || newdims > 3 )
3646 handle->ERROR_MESSAGE("WKB output dimensions out of range 2..3");
3649 const int olddims = handle->WKBOutputDims;
3650 handle->WKBOutputDims = newdims;
3652 return olddims;
3656 GEOS_getWKBByteOrder_r(GEOSContextHandle_t extHandle)
3658 if ( 0 == extHandle )
3660 return -1;
3663 GEOSContextHandleInternal_t *handle = 0;
3664 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
3665 if ( 0 == handle->initialized )
3667 return -1;
3670 return handle->WKBByteOrder;
3674 GEOS_setWKBByteOrder_r(GEOSContextHandle_t extHandle, int byteOrder)
3676 if ( 0 == extHandle )
3678 return -1;
3681 GEOSContextHandleInternal_t *handle = 0;
3682 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
3683 if ( 0 == handle->initialized )
3685 return -1;
3688 const int oldByteOrder = handle->WKBByteOrder;
3689 handle->WKBByteOrder = byteOrder;
3691 return oldByteOrder;
3695 CoordinateSequence *
3696 GEOSCoordSeq_create_r(GEOSContextHandle_t extHandle, unsigned int size, unsigned int dims)
3698 if ( 0 == extHandle )
3700 return NULL;
3703 GEOSContextHandleInternal_t *handle = 0;
3704 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
3705 if ( 0 == handle->initialized )
3707 return NULL;
3712 const GeometryFactory *gf = handle->geomFactory;
3713 return gf->getCoordinateSequenceFactory()->create(size, dims);
3715 catch (const std::exception &e)
3717 handle->ERROR_MESSAGE("%s", e.what());
3719 catch (...)
3721 handle->ERROR_MESSAGE("Unknown exception thrown");
3724 return NULL;
3728 GEOSCoordSeq_setOrdinate_r(GEOSContextHandle_t extHandle, CoordinateSequence *cs,
3729 unsigned int idx, unsigned int dim, double val)
3731 assert(0 != cs);
3732 if ( 0 == extHandle )
3734 return 0;
3737 GEOSContextHandleInternal_t *handle = 0;
3738 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
3739 if ( 0 == handle->initialized )
3741 return 0;
3746 cs->setOrdinate(idx, dim, val);
3747 return 1;
3749 catch (const std::exception &e)
3751 handle->ERROR_MESSAGE("%s", e.what());
3753 catch (...)
3755 handle->ERROR_MESSAGE("Unknown exception thrown");
3758 return 0;
3762 GEOSCoordSeq_setX_r(GEOSContextHandle_t extHandle, CoordinateSequence *s, unsigned int idx, double val)
3764 return GEOSCoordSeq_setOrdinate_r(extHandle, s, idx, 0, val);
3768 GEOSCoordSeq_setY_r(GEOSContextHandle_t extHandle, CoordinateSequence *s, unsigned int idx, double val)
3770 return GEOSCoordSeq_setOrdinate_r(extHandle, s, idx, 1, val);
3774 GEOSCoordSeq_setZ_r(GEOSContextHandle_t extHandle, CoordinateSequence *s, unsigned int idx, double val)
3776 return GEOSCoordSeq_setOrdinate_r(extHandle, s, idx, 2, val);
3779 CoordinateSequence *
3780 GEOSCoordSeq_clone_r(GEOSContextHandle_t extHandle, const CoordinateSequence *cs)
3782 assert(0 != cs);
3784 if ( 0 == extHandle )
3786 return NULL;
3789 GEOSContextHandleInternal_t *handle = 0;
3790 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
3791 if ( 0 == handle->initialized )
3793 return NULL;
3798 return cs->clone();
3800 catch (const std::exception &e)
3802 handle->ERROR_MESSAGE("%s", e.what());
3804 catch (...)
3806 handle->ERROR_MESSAGE("Unknown exception thrown");
3809 return NULL;
3813 GEOSCoordSeq_getOrdinate_r(GEOSContextHandle_t extHandle, const CoordinateSequence *cs,
3814 unsigned int idx, unsigned int dim, double *val)
3816 assert(0 != cs);
3817 assert(0 != val);
3819 if ( 0 == extHandle )
3821 return 0;
3824 GEOSContextHandleInternal_t *handle = 0;
3825 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
3826 if ( 0 == handle->initialized )
3828 return 0;
3833 double d = cs->getOrdinate(idx, dim);
3834 *val = d;
3836 return 1;
3838 catch (const std::exception &e)
3840 handle->ERROR_MESSAGE("%s", e.what());
3842 catch (...)
3844 handle->ERROR_MESSAGE("Unknown exception thrown");
3847 return 0;
3851 GEOSCoordSeq_getX_r(GEOSContextHandle_t extHandle, const CoordinateSequence *s, unsigned int idx, double *val)
3853 return GEOSCoordSeq_getOrdinate_r(extHandle, s, idx, 0, val);
3857 GEOSCoordSeq_getY_r(GEOSContextHandle_t extHandle, const CoordinateSequence *s, unsigned int idx, double *val)
3859 return GEOSCoordSeq_getOrdinate_r(extHandle, s, idx, 1, val);
3863 GEOSCoordSeq_getZ_r(GEOSContextHandle_t extHandle, const CoordinateSequence *s, unsigned int idx, double *val)
3865 return GEOSCoordSeq_getOrdinate_r(extHandle, s, idx, 2, val);
3869 GEOSCoordSeq_getSize_r(GEOSContextHandle_t extHandle, const CoordinateSequence *cs, unsigned int *size)
3871 assert(0 != cs);
3872 assert(0 != size);
3874 if ( 0 == extHandle )
3876 return 0;
3879 GEOSContextHandleInternal_t *handle = 0;
3880 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
3881 if ( 0 == handle->initialized )
3883 return 0;
3888 const std::size_t sz = cs->getSize();
3889 *size = static_cast<unsigned int>(sz);
3890 return 1;
3892 catch (const std::exception &e)
3894 handle->ERROR_MESSAGE("%s", e.what());
3896 catch (...)
3898 handle->ERROR_MESSAGE("Unknown exception thrown");
3901 return 0;
3905 GEOSCoordSeq_getDimensions_r(GEOSContextHandle_t extHandle, const CoordinateSequence *cs, unsigned int *dims)
3907 assert(0 != cs);
3908 assert(0 != dims);
3910 if ( 0 == extHandle )
3912 return 0;
3915 GEOSContextHandleInternal_t *handle = 0;
3916 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
3917 if ( 0 == handle->initialized )
3919 return 0;
3924 const std::size_t dim = cs->getDimension();
3925 *dims = static_cast<unsigned int>(dim);
3927 return 1;
3929 catch (const std::exception &e)
3931 handle->ERROR_MESSAGE("%s", e.what());
3934 catch (...)
3936 handle->ERROR_MESSAGE("Unknown exception thrown");
3939 return 0;
3942 void
3943 GEOSCoordSeq_destroy_r(GEOSContextHandle_t extHandle, CoordinateSequence *s)
3945 GEOSContextHandleInternal_t *handle = 0;
3949 delete s;
3951 catch (const std::exception &e)
3953 if ( 0 == extHandle )
3955 return;
3958 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
3959 if ( 0 == handle->initialized )
3961 return;
3964 handle->ERROR_MESSAGE("%s", e.what());
3966 catch (...)
3968 if ( 0 == extHandle )
3970 return;
3973 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
3974 if ( 0 == handle->initialized )
3976 return;
3979 handle->ERROR_MESSAGE("Unknown exception thrown");
3983 const CoordinateSequence *
3984 GEOSGeom_getCoordSeq_r(GEOSContextHandle_t extHandle, const Geometry *g)
3986 if ( 0 == extHandle )
3988 return 0;
3991 GEOSContextHandleInternal_t *handle = 0;
3992 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
3993 if ( 0 == handle->initialized )
3995 return 0;
4000 using geos::geom::Point;
4002 const LineString *ls = dynamic_cast<const LineString *>(g);
4003 if ( ls )
4005 return ls->getCoordinatesRO();
4008 const Point *p = dynamic_cast<const Point *>(g);
4009 if ( p )
4011 return p->getCoordinatesRO();
4014 handle->ERROR_MESSAGE("Geometry must be a Point or LineString");
4015 return 0;
4017 catch (const std::exception &e)
4019 handle->ERROR_MESSAGE("%s", e.what());
4021 catch (...)
4023 handle->ERROR_MESSAGE("Unknown exception thrown");
4026 return 0;
4029 Geometry *
4030 GEOSGeom_createEmptyPoint_r(GEOSContextHandle_t extHandle)
4032 if ( 0 == extHandle )
4034 return NULL;
4037 GEOSContextHandleInternal_t *handle = 0;
4038 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
4039 if ( 0 == handle->initialized )
4041 return NULL;
4046 const GeometryFactory *gf = handle->geomFactory;
4047 return gf->createPoint();
4049 catch (const std::exception &e)
4051 handle->ERROR_MESSAGE("%s", e.what());
4053 catch (...)
4055 handle->ERROR_MESSAGE("Unknown exception thrown");
4058 return NULL;
4061 Geometry *
4062 GEOSGeom_createPoint_r(GEOSContextHandle_t extHandle, CoordinateSequence *cs)
4064 if ( 0 == extHandle )
4066 return 0;
4069 GEOSContextHandleInternal_t *handle = 0;
4070 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
4071 if ( 0 == handle->initialized )
4073 return 0;
4078 const GeometryFactory *gf = handle->geomFactory;
4079 return gf->createPoint(cs);
4081 catch (const std::exception &e)
4083 handle->ERROR_MESSAGE("%s", e.what());
4085 catch (...)
4087 handle->ERROR_MESSAGE("Unknown exception thrown");
4090 return 0;
4093 Geometry *
4094 GEOSGeom_createLinearRing_r(GEOSContextHandle_t extHandle, CoordinateSequence *cs)
4096 if ( 0 == extHandle )
4098 return NULL;
4101 GEOSContextHandleInternal_t *handle = 0;
4102 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
4103 if ( 0 == handle->initialized )
4105 return NULL;
4110 const GeometryFactory *gf = handle->geomFactory;
4112 return gf->createLinearRing(cs);
4114 catch (const std::exception &e)
4116 handle->ERROR_MESSAGE("%s", e.what());
4118 catch (...)
4120 handle->ERROR_MESSAGE("Unknown exception thrown");
4123 return NULL;
4126 Geometry *
4127 GEOSGeom_createEmptyLineString_r(GEOSContextHandle_t extHandle)
4129 if ( 0 == extHandle )
4131 return NULL;
4134 GEOSContextHandleInternal_t *handle = 0;
4135 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
4136 if ( 0 == handle->initialized )
4138 return NULL;
4143 const GeometryFactory *gf = handle->geomFactory;
4145 return gf->createLineString();
4147 catch (const std::exception &e)
4149 handle->ERROR_MESSAGE("%s", e.what());
4151 catch (...)
4153 handle->ERROR_MESSAGE("Unknown exception thrown");
4156 return NULL;
4159 Geometry *
4160 GEOSGeom_createLineString_r(GEOSContextHandle_t extHandle, CoordinateSequence *cs)
4162 if ( 0 == extHandle )
4164 return NULL;
4167 GEOSContextHandleInternal_t *handle = 0;
4168 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
4169 if ( 0 == handle->initialized )
4171 return NULL;
4176 const GeometryFactory *gf = handle->geomFactory;
4178 return gf->createLineString(cs);
4180 catch (const std::exception &e)
4182 handle->ERROR_MESSAGE("%s", e.what());
4184 catch (...)
4186 handle->ERROR_MESSAGE("Unknown exception thrown");
4189 return NULL;
4192 Geometry *
4193 GEOSGeom_createEmptyPolygon_r(GEOSContextHandle_t extHandle)
4195 if ( 0 == extHandle )
4197 return NULL;
4200 GEOSContextHandleInternal_t *handle = 0;
4201 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
4202 if ( 0 == handle->initialized )
4204 return NULL;
4209 const GeometryFactory *gf = handle->geomFactory;
4210 return gf->createPolygon();
4212 catch (const std::exception &e)
4214 handle->ERROR_MESSAGE("%s", e.what());
4216 catch (...)
4218 handle->ERROR_MESSAGE("Unknown exception thrown");
4221 return NULL;
4224 Geometry *
4225 GEOSGeom_createPolygon_r(GEOSContextHandle_t extHandle, Geometry *shell, Geometry **holes, unsigned int nholes)
4227 // FIXME: holes must be non-nullptr or may be nullptr?
4228 //assert(0 != holes);
4230 if ( 0 == extHandle )
4232 return NULL;
4235 GEOSContextHandleInternal_t *handle = 0;
4236 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
4237 if ( 0 == handle->initialized )
4239 return NULL;
4244 using geos::geom::LinearRing;
4246 std::vector<Geometry *> *vholes = new std::vector<Geometry *>(holes, holes + nholes);
4248 LinearRing *nshell = dynamic_cast<LinearRing *>(shell);
4249 if ( ! nshell )
4251 handle->ERROR_MESSAGE("Shell is not a LinearRing");
4252 delete vholes;
4253 return NULL;
4255 const GeometryFactory *gf = handle->geomFactory;
4257 return gf->createPolygon(nshell, vholes);
4259 catch (const std::exception &e)
4261 handle->ERROR_MESSAGE("%s", e.what());
4263 catch (...)
4265 handle->ERROR_MESSAGE("Unknown exception thrown");
4268 return NULL;
4271 Geometry *
4272 GEOSGeom_clone_r(GEOSContextHandle_t extHandle, const Geometry *g)
4274 assert(0 != g);
4276 if ( 0 == extHandle )
4278 return NULL;
4281 GEOSContextHandleInternal_t *handle = 0;
4282 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
4283 if ( 0 == handle->initialized )
4285 return NULL;
4290 return g->clone();
4292 catch (const std::exception &e)
4294 handle->ERROR_MESSAGE("%s", e.what());
4296 catch (...)
4298 handle->ERROR_MESSAGE("Unknown exception thrown");
4301 return NULL;
4304 GEOSGeometry *
4305 GEOSGeom_setPrecision_r(GEOSContextHandle_t extHandle, const GEOSGeometry *g,
4306 double gridSize, int flags)
4308 using namespace geos::geom;
4310 assert(0 != g);
4312 if ( 0 == extHandle )
4314 return NULL;
4317 GEOSContextHandleInternal_t *handle = 0;
4318 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
4319 if ( 0 == handle->initialized )
4321 return NULL;
4326 const PrecisionModel *pm = g->getPrecisionModel();
4327 double cursize = pm->isFloating() ? 0 : 1.0/pm->getScale();
4328 std::auto_ptr<PrecisionModel> newpm;
4329 if ( gridSize ) newpm.reset( new PrecisionModel(1.0/gridSize) );
4330 else newpm.reset( new PrecisionModel() );
4331 GeometryFactory::unique_ptr gf =
4332 GeometryFactory::create( newpm.get(), g->getSRID() );
4333 Geometry *ret;
4334 if ( gridSize && cursize != gridSize )
4336 // We need to snap the geometry
4337 GeometryPrecisionReducer reducer( *gf );
4338 reducer.setPointwise( flags & GEOS_PREC_NO_TOPO );
4339 reducer.setRemoveCollapsedComponents( ! (flags & GEOS_PREC_KEEP_COLLAPSED) );
4340 ret = reducer.reduce( *g ).release();
4342 else
4344 // No need or willing to snap, just change the factory
4345 ret = gf->createGeometry(g);
4347 return ret;
4349 catch (const std::exception &e)
4351 handle->ERROR_MESSAGE("%s", e.what());
4353 catch (...)
4355 handle->ERROR_MESSAGE("Unknown exception thrown");
4358 return NULL;
4361 double
4362 GEOSGeom_getPrecision_r(GEOSContextHandle_t extHandle, const GEOSGeometry *g)
4364 using namespace geos::geom;
4366 assert(0 != g);
4368 if ( 0 == extHandle )
4370 return -1;
4373 GEOSContextHandleInternal_t *handle = 0;
4374 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
4375 if ( 0 == handle->initialized )
4377 return -1;
4382 const PrecisionModel *pm = g->getPrecisionModel();
4383 double cursize = pm->isFloating() ? 0 : 1.0/pm->getScale();
4384 return cursize;
4386 catch (const std::exception &e)
4388 handle->ERROR_MESSAGE("%s", e.what());
4390 catch (...)
4392 handle->ERROR_MESSAGE("Unknown exception thrown");
4395 return -1;
4399 GEOSGeom_getDimensions_r(GEOSContextHandle_t extHandle, const Geometry *g)
4401 if ( 0 == extHandle )
4403 return 0;
4406 GEOSContextHandleInternal_t *handle = 0;
4407 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
4408 if ( 0 == handle->initialized )
4410 return 0;
4415 return (int) g->getDimension();
4417 catch (const std::exception &e)
4419 handle->ERROR_MESSAGE("%s", e.what());
4421 catch (...)
4423 handle->ERROR_MESSAGE("Unknown exception thrown");
4426 return 0;
4430 GEOSGeom_getCoordinateDimension_r(GEOSContextHandle_t extHandle, const Geometry *g)
4432 if ( 0 == extHandle )
4434 return 0;
4437 GEOSContextHandleInternal_t *handle = 0;
4438 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
4439 if ( 0 == handle->initialized )
4441 return 0;
4446 return g->getCoordinateDimension();
4448 catch (const std::exception &e)
4450 handle->ERROR_MESSAGE("%s", e.what());
4452 catch (...)
4454 handle->ERROR_MESSAGE("Unknown exception thrown");
4457 return 0;
4460 Geometry *
4461 GEOSSimplify_r(GEOSContextHandle_t extHandle, const Geometry *g1, double tolerance)
4463 if ( 0 == extHandle )
4465 return NULL;
4468 GEOSContextHandleInternal_t *handle = 0;
4469 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
4470 if ( 0 == handle->initialized )
4472 return NULL;
4477 using namespace geos::simplify;
4478 Geometry::AutoPtr g(DouglasPeuckerSimplifier::simplify(g1, tolerance));
4479 return g.release();
4481 catch (const std::exception &e)
4483 handle->ERROR_MESSAGE("%s", e.what());
4485 catch (...)
4487 handle->ERROR_MESSAGE("Unknown exception thrown");
4490 return NULL;
4493 Geometry *
4494 GEOSTopologyPreserveSimplify_r(GEOSContextHandle_t extHandle, const Geometry *g1, double tolerance)
4496 if ( 0 == extHandle )
4498 return NULL;
4501 GEOSContextHandleInternal_t *handle = 0;
4502 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
4503 if ( 0 == handle->initialized )
4505 return NULL;
4510 using namespace geos::simplify;
4511 Geometry::AutoPtr g(TopologyPreservingSimplifier::simplify(g1, tolerance));
4512 return g.release();
4514 catch (const std::exception &e)
4516 handle->ERROR_MESSAGE("%s", e.what());
4518 catch (...)
4520 handle->ERROR_MESSAGE("Unknown exception thrown");
4523 return NULL;
4527 /* WKT Reader */
4528 WKTReader *
4529 GEOSWKTReader_create_r(GEOSContextHandle_t extHandle)
4531 if ( 0 == extHandle )
4533 return NULL;
4536 GEOSContextHandleInternal_t *handle = 0;
4537 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
4538 if ( 0 == handle->initialized )
4540 return NULL;
4545 using geos::io::WKTReader;
4546 return new WKTReader((GeometryFactory*)handle->geomFactory);
4548 catch (const std::exception &e)
4550 handle->ERROR_MESSAGE("%s", e.what());
4552 catch (...)
4554 handle->ERROR_MESSAGE("Unknown exception thrown");
4557 return NULL;
4560 void
4561 GEOSWKTReader_destroy_r(GEOSContextHandle_t extHandle, WKTReader *reader)
4563 GEOSContextHandleInternal_t *handle = 0;
4567 delete reader;
4569 catch (const std::exception &e)
4571 if ( 0 == extHandle )
4573 return;
4576 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
4577 if ( 0 == handle->initialized )
4579 return;
4582 handle->ERROR_MESSAGE("%s", e.what());
4584 catch (...)
4586 if ( 0 == extHandle )
4588 return;
4591 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
4592 if ( 0 == handle->initialized )
4594 return;
4597 handle->ERROR_MESSAGE("Unknown exception thrown");
4602 Geometry*
4603 GEOSWKTReader_read_r(GEOSContextHandle_t extHandle, WKTReader *reader, const char *wkt)
4605 assert(0 != reader);
4607 if ( 0 == extHandle )
4609 return 0;
4612 GEOSContextHandleInternal_t *handle = 0;
4613 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
4614 if ( 0 == handle->initialized )
4616 return 0;
4621 const std::string wktstring(wkt);
4622 Geometry *g = reader->read(wktstring);
4623 return g;
4625 catch (const std::exception &e)
4627 handle->ERROR_MESSAGE("%s", e.what());
4629 catch (...)
4631 handle->ERROR_MESSAGE("Unknown exception thrown");
4634 return 0;
4637 /* WKT Writer */
4638 WKTWriter *
4639 GEOSWKTWriter_create_r(GEOSContextHandle_t extHandle)
4641 if ( 0 == extHandle )
4643 return 0;
4646 GEOSContextHandleInternal_t *handle = 0;
4647 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
4648 if ( 0 == handle->initialized )
4650 return 0;
4655 using geos::io::WKTWriter;
4656 return new WKTWriter();
4658 catch (const std::exception &e)
4660 handle->ERROR_MESSAGE("%s", e.what());
4662 catch (...)
4664 handle->ERROR_MESSAGE("Unknown exception thrown");
4667 return 0;
4670 void
4671 GEOSWKTWriter_destroy_r(GEOSContextHandle_t extHandle, WKTWriter *Writer)
4674 GEOSContextHandleInternal_t *handle = 0;
4678 delete Writer;
4680 catch (const std::exception &e)
4682 if ( 0 == extHandle )
4684 return;
4687 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
4688 if ( 0 == handle->initialized )
4690 return;
4693 handle->ERROR_MESSAGE("%s", e.what());
4695 catch (...)
4697 if ( 0 == extHandle )
4699 return;
4702 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
4703 if ( 0 == handle->initialized )
4705 return;
4708 handle->ERROR_MESSAGE("Unknown exception thrown");
4713 char*
4714 GEOSWKTWriter_write_r(GEOSContextHandle_t extHandle, WKTWriter *writer, const Geometry *geom)
4716 assert(0 != writer);
4718 if ( 0 == extHandle )
4720 return NULL;
4723 GEOSContextHandleInternal_t *handle = 0;
4724 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
4725 if ( 0 == handle->initialized )
4727 return NULL;
4732 std::string sgeom(writer->write(geom));
4733 char *result = gstrdup(sgeom);
4734 return result;
4736 catch (const std::exception &e)
4738 handle->ERROR_MESSAGE("%s", e.what());
4740 catch (...)
4742 handle->ERROR_MESSAGE("Unknown exception thrown");
4745 return NULL;
4748 void
4749 GEOSWKTWriter_setTrim_r(GEOSContextHandle_t extHandle, WKTWriter *writer, char trim)
4751 assert(0 != writer);
4753 if ( 0 == extHandle )
4755 return;
4758 GEOSContextHandleInternal_t *handle = 0;
4759 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
4760 if ( 0 == handle->initialized )
4762 return;
4765 writer->setTrim(0 != trim);
4768 void
4769 GEOSWKTWriter_setRoundingPrecision_r(GEOSContextHandle_t extHandle, WKTWriter *writer, int precision)
4771 assert(0 != writer);
4773 if ( 0 == extHandle )
4775 return;
4778 GEOSContextHandleInternal_t *handle = 0;
4779 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
4780 if ( 0 == handle->initialized )
4782 return;
4785 writer->setRoundingPrecision(precision);
4788 void
4789 GEOSWKTWriter_setOutputDimension_r(GEOSContextHandle_t extHandle, WKTWriter *writer, int dim)
4791 assert(0 != writer);
4793 if ( 0 == extHandle )
4795 return;
4798 GEOSContextHandleInternal_t *handle = 0;
4799 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
4800 if ( 0 == handle->initialized )
4802 return;
4807 writer->setOutputDimension(dim);
4809 catch (const std::exception &e)
4811 handle->ERROR_MESSAGE("%s", e.what());
4813 catch (...)
4815 handle->ERROR_MESSAGE("Unknown exception thrown");
4820 GEOSWKTWriter_getOutputDimension_r(GEOSContextHandle_t extHandle, WKTWriter *writer)
4822 assert(0 != writer);
4824 if ( 0 == extHandle )
4826 return -1;
4829 GEOSContextHandleInternal_t *handle = 0;
4830 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
4831 if ( 0 == handle->initialized )
4833 return -1;
4836 int dim = -1;
4840 dim = writer->getOutputDimension();
4842 catch (const std::exception &e)
4844 handle->ERROR_MESSAGE("%s", e.what());
4846 catch (...)
4848 handle->ERROR_MESSAGE("Unknown exception thrown");
4851 return dim;
4854 void
4855 GEOSWKTWriter_setOld3D_r(GEOSContextHandle_t extHandle, WKTWriter *writer, int useOld3D)
4857 assert(0 != writer);
4859 if ( 0 == extHandle )
4861 return;
4864 GEOSContextHandleInternal_t *handle = 0;
4865 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
4866 if ( 0 == handle->initialized )
4868 return;
4871 writer->setOld3D(0 != useOld3D);
4874 /* WKB Reader */
4875 WKBReader *
4876 GEOSWKBReader_create_r(GEOSContextHandle_t extHandle)
4878 if ( 0 == extHandle )
4880 return NULL;
4883 GEOSContextHandleInternal_t *handle = 0;
4884 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
4885 if ( 0 == handle->initialized )
4887 return NULL;
4890 using geos::io::WKBReader;
4893 return new WKBReader(*(GeometryFactory*)handle->geomFactory);
4895 catch (const std::exception &e)
4897 handle->ERROR_MESSAGE("%s", e.what());
4899 catch (...)
4901 handle->ERROR_MESSAGE("Unknown exception thrown");
4904 return NULL;
4907 void
4908 GEOSWKBReader_destroy_r(GEOSContextHandle_t extHandle, WKBReader *reader)
4910 GEOSContextHandleInternal_t *handle = 0;
4914 delete reader;
4916 catch (const std::exception &e)
4918 if ( 0 == extHandle )
4920 return;
4923 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
4924 if ( 0 == handle->initialized )
4926 return;
4929 handle->ERROR_MESSAGE("%s", e.what());
4931 catch (...)
4933 if ( 0 == extHandle )
4935 return;
4938 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
4939 if ( 0 == handle->initialized )
4941 return;
4944 handle->ERROR_MESSAGE("Unknown exception thrown");
4948 struct membuf : public std::streambuf
4950 membuf(char* s, std::size_t n)
4952 setg(s, s, s + n);
4956 Geometry*
4957 GEOSWKBReader_read_r(GEOSContextHandle_t extHandle, WKBReader *reader, const unsigned char *wkb, size_t size)
4959 assert(0 != reader);
4960 assert(0 != wkb);
4962 if ( 0 == extHandle )
4964 return 0;
4967 GEOSContextHandleInternal_t *handle = 0;
4968 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
4969 if ( 0 == handle->initialized )
4971 return 0;
4976 //std::string wkbstring(reinterpret_cast<const char*>(wkb), size); // make it binary !
4977 //std::istringstream is(std::ios_base::binary);
4978 //is.str(wkbstring);
4979 //is.seekg(0, std::ios::beg); // rewind reader pointer
4981 // http://stackoverflow.com/questions/2079912/simpler-way-to-create-a-c-memorystream-from-char-size-t-without-copying-t
4982 membuf mb((char*)wkb, size);
4983 istream is(&mb);
4985 Geometry *g = reader->read(is);
4986 return g;
4988 catch (const std::exception &e)
4990 handle->ERROR_MESSAGE("%s", e.what());
4992 catch (...)
4994 handle->ERROR_MESSAGE("Unknown exception thrown");
4997 return 0;
5000 Geometry*
5001 GEOSWKBReader_readHEX_r(GEOSContextHandle_t extHandle, WKBReader *reader, const unsigned char *hex, size_t size)
5003 assert(0 != reader);
5004 assert(0 != hex);
5006 if ( 0 == extHandle )
5008 return 0;
5011 GEOSContextHandleInternal_t *handle = 0;
5012 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
5013 if ( 0 == handle->initialized )
5015 return 0;
5020 std::string hexstring(reinterpret_cast<const char*>(hex), size);
5021 std::istringstream is(std::ios_base::binary);
5022 is.str(hexstring);
5023 is.seekg(0, std::ios::beg); // rewind reader pointer
5025 Geometry *g = reader->readHEX(is);
5026 return g;
5028 catch (const std::exception &e)
5030 handle->ERROR_MESSAGE("%s", e.what());
5032 catch (...)
5034 handle->ERROR_MESSAGE("Unknown exception thrown");
5037 return 0;
5040 /* WKB Writer */
5041 WKBWriter *
5042 GEOSWKBWriter_create_r(GEOSContextHandle_t extHandle)
5044 if ( 0 == extHandle )
5046 return NULL;
5049 GEOSContextHandleInternal_t *handle = 0;
5050 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
5051 if ( 0 == handle->initialized )
5053 return NULL;
5058 using geos::io::WKBWriter;
5059 return new WKBWriter();
5061 catch (const std::exception &e)
5063 handle->ERROR_MESSAGE("%s", e.what());
5065 catch (...)
5067 handle->ERROR_MESSAGE("Unknown exception thrown");
5070 return NULL;
5073 void
5074 GEOSWKBWriter_destroy_r(GEOSContextHandle_t extHandle, WKBWriter *Writer)
5076 GEOSContextHandleInternal_t *handle = 0;
5080 delete Writer;
5082 catch (const std::exception &e)
5084 if ( 0 == extHandle )
5086 return;
5089 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
5090 if ( 0 == handle->initialized )
5092 return;
5095 handle->ERROR_MESSAGE("%s", e.what());
5097 catch (...)
5099 if ( 0 == extHandle )
5101 return;
5104 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
5105 if ( 0 == handle->initialized )
5107 return;
5110 handle->ERROR_MESSAGE("Unknown exception thrown");
5115 /* The caller owns the result */
5116 unsigned char*
5117 GEOSWKBWriter_write_r(GEOSContextHandle_t extHandle, WKBWriter *writer, const Geometry *geom, size_t *size)
5119 assert(0 != writer);
5120 assert(0 != geom);
5121 assert(0 != size);
5123 if ( 0 == extHandle )
5125 return NULL;
5128 GEOSContextHandleInternal_t *handle = 0;
5129 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
5130 if ( 0 == handle->initialized )
5132 return NULL;
5137 std::ostringstream os(std::ios_base::binary);
5138 writer->write(*geom, os);
5140 const std::string& wkbstring = os.str();
5141 const std::size_t len = wkbstring.length();
5143 unsigned char *result = NULL;
5144 result = (unsigned char*) malloc(len);
5145 std::memcpy(result, wkbstring.c_str(), len);
5146 *size = len;
5147 return result;
5149 catch (const std::exception &e)
5151 handle->ERROR_MESSAGE("%s", e.what());
5153 catch (...)
5155 handle->ERROR_MESSAGE("Unknown exception thrown");
5157 return NULL;
5160 /* The caller owns the result */
5161 unsigned char*
5162 GEOSWKBWriter_writeHEX_r(GEOSContextHandle_t extHandle, WKBWriter *writer, const Geometry *geom, size_t *size)
5164 assert(0 != writer);
5165 assert(0 != geom);
5166 assert(0 != size);
5168 if ( 0 == extHandle )
5170 return NULL;
5173 GEOSContextHandleInternal_t *handle = 0;
5174 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
5175 if ( 0 == handle->initialized )
5177 return NULL;
5182 std::ostringstream os(std::ios_base::binary);
5183 writer->writeHEX(*geom, os);
5184 std::string wkbstring(os.str());
5185 const std::size_t len = wkbstring.length();
5187 unsigned char *result = NULL;
5188 result = (unsigned char*) malloc(len);
5189 std::memcpy(result, wkbstring.c_str(), len);
5190 *size = len;
5191 return result;
5193 catch (const std::exception &e)
5195 handle->ERROR_MESSAGE("%s", e.what());
5197 catch (...)
5199 handle->ERROR_MESSAGE("Unknown exception thrown");
5202 return NULL;
5206 GEOSWKBWriter_getOutputDimension_r(GEOSContextHandle_t extHandle, const GEOSWKBWriter* writer)
5208 assert(0 != writer);
5210 if ( 0 == extHandle )
5212 return 0;
5215 int ret = 0;
5217 GEOSContextHandleInternal_t *handle = 0;
5218 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
5219 if ( 0 != handle->initialized )
5223 ret = writer->getOutputDimension();
5225 catch (...)
5227 handle->ERROR_MESSAGE("Unknown exception thrown");
5231 return ret;
5234 void
5235 GEOSWKBWriter_setOutputDimension_r(GEOSContextHandle_t extHandle, GEOSWKBWriter* writer, int newDimension)
5237 assert(0 != writer);
5239 if ( 0 == extHandle )
5241 return;
5244 GEOSContextHandleInternal_t *handle = 0;
5245 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
5246 if ( 0 != handle->initialized )
5250 writer->setOutputDimension(newDimension);
5252 catch (const std::exception &e)
5254 handle->ERROR_MESSAGE("%s", e.what());
5256 catch (...)
5258 handle->ERROR_MESSAGE("Unknown exception thrown");
5264 GEOSWKBWriter_getByteOrder_r(GEOSContextHandle_t extHandle, const GEOSWKBWriter* writer)
5266 assert(0 != writer);
5268 if ( 0 == extHandle )
5270 return 0;
5273 int ret = 0;
5275 GEOSContextHandleInternal_t *handle = 0;
5276 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
5277 if ( 0 != handle->initialized )
5281 ret = writer->getByteOrder();
5284 catch (...)
5286 handle->ERROR_MESSAGE("Unknown exception thrown");
5290 return ret;
5293 void
5294 GEOSWKBWriter_setByteOrder_r(GEOSContextHandle_t extHandle, GEOSWKBWriter* writer, int newByteOrder)
5296 assert(0 != writer);
5298 if ( 0 == extHandle )
5300 return;
5303 GEOSContextHandleInternal_t *handle = 0;
5304 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
5305 if ( 0 != handle->initialized )
5309 writer->setByteOrder(newByteOrder);
5311 catch (const std::exception &e)
5313 handle->ERROR_MESSAGE("%s", e.what());
5315 catch (...)
5317 handle->ERROR_MESSAGE("Unknown exception thrown");
5322 char
5323 GEOSWKBWriter_getIncludeSRID_r(GEOSContextHandle_t extHandle, const GEOSWKBWriter* writer)
5325 assert(0 != writer);
5327 if ( 0 == extHandle )
5329 return -1;
5332 int ret = -1;
5334 GEOSContextHandleInternal_t *handle = 0;
5335 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
5336 if ( 0 != handle->initialized )
5340 int srid = writer->getIncludeSRID();
5341 ret = srid;
5343 catch (...)
5345 handle->ERROR_MESSAGE("Unknown exception thrown");
5349 return static_cast<char>(ret);
5352 void
5353 GEOSWKBWriter_setIncludeSRID_r(GEOSContextHandle_t extHandle, GEOSWKBWriter* writer, const char newIncludeSRID)
5355 assert(0 != writer);
5357 if ( 0 == extHandle )
5359 return;
5362 GEOSContextHandleInternal_t *handle = 0;
5363 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
5364 if ( 0 != handle->initialized )
5368 writer->setIncludeSRID(newIncludeSRID);
5370 catch (...)
5372 handle->ERROR_MESSAGE("Unknown exception thrown");
5378 //-----------------------------------------------------------------
5379 // Prepared Geometry
5380 //-----------------------------------------------------------------
5382 const geos::geom::prep::PreparedGeometry*
5383 GEOSPrepare_r(GEOSContextHandle_t extHandle, const Geometry *g)
5385 if ( 0 == extHandle )
5387 return 0;
5390 GEOSContextHandleInternal_t *handle = 0;
5391 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
5392 if ( 0 == handle->initialized )
5394 return 0;
5397 const geos::geom::prep::PreparedGeometry* prep = 0;
5401 prep = geos::geom::prep::PreparedGeometryFactory::prepare(g);
5403 catch (const std::exception &e)
5405 handle->ERROR_MESSAGE("%s", e.what());
5407 catch (...)
5409 handle->ERROR_MESSAGE("Unknown exception thrown");
5412 return prep;
5415 void
5416 GEOSPreparedGeom_destroy_r(GEOSContextHandle_t extHandle, const geos::geom::prep::PreparedGeometry *a)
5418 GEOSContextHandleInternal_t *handle = 0;
5422 delete a;
5424 catch (const std::exception &e)
5426 if ( 0 == extHandle )
5428 return;
5431 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
5432 if ( 0 == handle->initialized )
5434 return;
5437 handle->ERROR_MESSAGE("%s", e.what());
5439 catch (...)
5441 if ( 0 == extHandle )
5443 return;
5446 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
5447 if ( 0 == handle->initialized )
5449 return;
5452 handle->ERROR_MESSAGE("Unknown exception thrown");
5456 char
5457 GEOSPreparedContains_r(GEOSContextHandle_t extHandle,
5458 const geos::geom::prep::PreparedGeometry *pg, const Geometry *g)
5460 assert(0 != pg);
5461 assert(0 != g);
5463 if ( 0 == extHandle )
5465 return 2;
5468 GEOSContextHandleInternal_t *handle = 0;
5469 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
5470 if ( 0 == handle->initialized )
5472 return 2;
5477 bool result = pg->contains(g);
5478 return result;
5480 catch (const std::exception &e)
5482 handle->ERROR_MESSAGE("%s", e.what());
5484 catch (...)
5486 handle->ERROR_MESSAGE("Unknown exception thrown");
5489 return 2;
5492 char
5493 GEOSPreparedContainsProperly_r(GEOSContextHandle_t extHandle,
5494 const geos::geom::prep::PreparedGeometry *pg, const Geometry *g)
5496 assert(0 != pg);
5497 assert(0 != g);
5499 if ( 0 == extHandle )
5501 return 2;
5504 GEOSContextHandleInternal_t *handle = 0;
5505 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
5506 if ( 0 == handle->initialized )
5508 return 2;
5513 bool result = pg->containsProperly(g);
5514 return result;
5516 catch (const std::exception &e)
5518 handle->ERROR_MESSAGE("%s", e.what());
5520 catch (...)
5522 handle->ERROR_MESSAGE("Unknown exception thrown");
5525 return 2;
5528 char
5529 GEOSPreparedCoveredBy_r(GEOSContextHandle_t extHandle,
5530 const geos::geom::prep::PreparedGeometry *pg, const Geometry *g)
5532 assert(0 != pg);
5533 assert(0 != g);
5535 if ( 0 == extHandle )
5537 return 2;
5540 GEOSContextHandleInternal_t *handle = 0;
5541 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
5542 if ( 0 == handle->initialized )
5544 return 2;
5549 bool result = pg->coveredBy(g);
5550 return result;
5552 catch (const std::exception &e)
5554 handle->ERROR_MESSAGE("%s", e.what());
5556 catch (...)
5558 handle->ERROR_MESSAGE("Unknown exception thrown");
5561 return 2;
5564 char
5565 GEOSPreparedCovers_r(GEOSContextHandle_t extHandle,
5566 const geos::geom::prep::PreparedGeometry *pg, const Geometry *g)
5568 assert(0 != pg);
5569 assert(0 != g);
5571 if ( 0 == extHandle )
5573 return 2;
5576 GEOSContextHandleInternal_t *handle = 0;
5577 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
5578 if ( 0 == handle->initialized )
5580 return 2;
5585 bool result = pg->covers(g);
5586 return result;
5588 catch (const std::exception &e)
5590 handle->ERROR_MESSAGE("%s", e.what());
5592 catch (...)
5594 handle->ERROR_MESSAGE("Unknown exception thrown");
5597 return 2;
5600 char
5601 GEOSPreparedCrosses_r(GEOSContextHandle_t extHandle,
5602 const geos::geom::prep::PreparedGeometry *pg, const Geometry *g)
5604 assert(0 != pg);
5605 assert(0 != g);
5607 if ( 0 == extHandle )
5609 return 2;
5612 GEOSContextHandleInternal_t *handle = 0;
5613 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
5614 if ( 0 == handle->initialized )
5616 return 2;
5621 bool result = pg->crosses(g);
5622 return result;
5624 catch (const std::exception &e)
5626 handle->ERROR_MESSAGE("%s", e.what());
5628 catch (...)
5630 handle->ERROR_MESSAGE("Unknown exception thrown");
5633 return 2;
5636 char
5637 GEOSPreparedDisjoint_r(GEOSContextHandle_t extHandle,
5638 const geos::geom::prep::PreparedGeometry *pg, const Geometry *g)
5640 assert(0 != pg);
5641 assert(0 != g);
5643 if ( 0 == extHandle )
5645 return 2;
5648 GEOSContextHandleInternal_t *handle = 0;
5649 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
5650 if ( 0 == handle->initialized )
5652 return 2;
5657 bool result = pg->disjoint(g);
5658 return result;
5660 catch (const std::exception &e)
5662 handle->ERROR_MESSAGE("%s", e.what());
5664 catch (...)
5666 handle->ERROR_MESSAGE("Unknown exception thrown");
5669 return 2;
5672 char
5673 GEOSPreparedIntersects_r(GEOSContextHandle_t extHandle,
5674 const geos::geom::prep::PreparedGeometry *pg, const Geometry *g)
5676 assert(0 != pg);
5677 assert(0 != g);
5679 if ( 0 == extHandle )
5681 return 2;
5684 GEOSContextHandleInternal_t *handle = 0;
5685 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
5686 if ( 0 == handle->initialized )
5688 return 2;
5693 bool result = pg->intersects(g);
5694 return result;
5696 catch (const std::exception &e)
5698 handle->ERROR_MESSAGE("%s", e.what());
5700 catch (...)
5702 handle->ERROR_MESSAGE("Unknown exception thrown");
5705 return 2;
5708 char
5709 GEOSPreparedOverlaps_r(GEOSContextHandle_t extHandle,
5710 const geos::geom::prep::PreparedGeometry *pg, const Geometry *g)
5712 assert(0 != pg);
5713 assert(0 != g);
5715 if ( 0 == extHandle )
5717 return 2;
5720 GEOSContextHandleInternal_t *handle = 0;
5721 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
5722 if ( 0 == handle->initialized )
5724 return 2;
5729 bool result = pg->overlaps(g);
5730 return result;
5732 catch (const std::exception &e)
5734 handle->ERROR_MESSAGE("%s", e.what());
5736 catch (...)
5738 handle->ERROR_MESSAGE("Unknown exception thrown");
5741 return 2;
5744 char
5745 GEOSPreparedTouches_r(GEOSContextHandle_t extHandle,
5746 const geos::geom::prep::PreparedGeometry *pg, const Geometry *g)
5748 assert(0 != pg);
5749 assert(0 != g);
5751 if ( 0 == extHandle )
5753 return 2;
5756 GEOSContextHandleInternal_t *handle = 0;
5757 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
5758 if ( 0 == handle->initialized )
5760 return 2;
5765 bool result = pg->touches(g);
5766 return result;
5768 catch (const std::exception &e)
5770 handle->ERROR_MESSAGE("%s", e.what());
5772 catch (...)
5774 handle->ERROR_MESSAGE("Unknown exception thrown");
5777 return 2;
5780 char
5781 GEOSPreparedWithin_r(GEOSContextHandle_t extHandle,
5782 const geos::geom::prep::PreparedGeometry *pg, const Geometry *g)
5784 assert(0 != pg);
5785 assert(0 != g);
5787 if ( 0 == extHandle )
5789 return 2;
5792 GEOSContextHandleInternal_t *handle = 0;
5793 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
5794 if ( 0 == handle->initialized )
5796 return 2;
5801 bool result = pg->within(g);
5802 return result;
5804 catch (const std::exception &e)
5806 handle->ERROR_MESSAGE("%s", e.what());
5808 catch (...)
5810 handle->ERROR_MESSAGE("Unknown exception thrown");
5813 return 2;
5816 //-----------------------------------------------------------------
5817 // STRtree
5818 //-----------------------------------------------------------------
5820 geos::index::strtree::STRtree *
5821 GEOSSTRtree_create_r(GEOSContextHandle_t extHandle,
5822 size_t nodeCapacity)
5824 if ( 0 == extHandle )
5826 return 0;
5829 GEOSContextHandleInternal_t *handle = 0;
5830 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
5831 if ( 0 == handle->initialized )
5833 return 0;
5836 geos::index::strtree::STRtree *tree = 0;
5840 tree = new geos::index::strtree::STRtree(nodeCapacity);
5842 catch (const std::exception &e)
5844 handle->ERROR_MESSAGE("%s", e.what());
5846 catch (...)
5848 handle->ERROR_MESSAGE("Unknown exception thrown");
5851 return tree;
5854 void
5855 GEOSSTRtree_insert_r(GEOSContextHandle_t extHandle,
5856 geos::index::strtree::STRtree *tree,
5857 const geos::geom::Geometry *g,
5858 void *item)
5860 GEOSContextHandleInternal_t *handle = 0;
5861 assert(tree != 0);
5862 assert(g != 0);
5866 tree->insert(g->getEnvelopeInternal(), item);
5868 catch (const std::exception &e)
5870 if ( 0 == extHandle )
5872 return;
5875 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
5876 if ( 0 == handle->initialized )
5878 return;
5881 handle->ERROR_MESSAGE("%s", e.what());
5883 catch (...)
5885 if ( 0 == extHandle )
5887 return;
5890 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
5891 if ( 0 == handle->initialized )
5893 return;
5896 handle->ERROR_MESSAGE("Unknown exception thrown");
5900 void
5901 GEOSSTRtree_query_r(GEOSContextHandle_t extHandle,
5902 geos::index::strtree::STRtree *tree,
5903 const geos::geom::Geometry *g,
5904 GEOSQueryCallback callback,
5905 void *userdata)
5907 GEOSContextHandleInternal_t *handle = 0;
5908 assert(tree != 0);
5909 assert(g != 0);
5910 assert(callback != 0);
5914 CAPI_ItemVisitor visitor(callback, userdata);
5915 tree->query(g->getEnvelopeInternal(), visitor);
5917 catch (const std::exception &e)
5919 if ( 0 == extHandle )
5921 return;
5924 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
5925 if ( 0 == handle->initialized )
5927 return;
5930 handle->ERROR_MESSAGE("%s", e.what());
5932 catch (...)
5934 if ( 0 == extHandle )
5936 return;
5939 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
5940 if ( 0 == handle->initialized )
5942 return;
5945 handle->ERROR_MESSAGE("Unknown exception thrown");
5949 const void *
5950 GEOSSTRtree_nearest_generic_r(GEOSContextHandle_t extHandle,
5951 geos::index::strtree::STRtree *tree,
5952 const void* item,
5953 const geos::geom::Geometry* itemEnvelope,
5954 GEOSDistanceCallback distancefn,
5955 void* userdata)
5958 GEOSContextHandleInternal_t *handle = 0;
5961 if (distancefn) {
5962 struct CustomItemDistance : public ItemDistance {
5963 CustomItemDistance(GEOSDistanceCallback p_distancefn, void* p_userdata)
5964 : m_distancefn(p_distancefn), m_userdata(p_userdata) {}
5966 GEOSDistanceCallback m_distancefn;
5967 void* m_userdata;
5969 double distance(const ItemBoundable* item1, const ItemBoundable* item2) {
5970 const void* a = item1->getItem();
5971 const void* b = item2->getItem();
5972 double d;
5974 if (!m_distancefn(a, b, &d, m_userdata)) {
5975 throw std::runtime_error(std::string("Failed to compute distance."));
5978 return d;
5982 CustomItemDistance itemDistance(distancefn, userdata);
5983 return tree->nearestNeighbour(itemEnvelope->getEnvelopeInternal(), item, &itemDistance);
5984 } else {
5985 GeometryItemDistance itemDistance = GeometryItemDistance();
5986 return tree->nearestNeighbour(itemEnvelope->getEnvelopeInternal(), item, &itemDistance);
5989 catch (const std::exception &e)
5991 if ( 0 == extHandle )
5993 return NULL;
5996 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
5997 if ( 0 == handle->initialized )
5999 return NULL;
6002 handle->ERROR_MESSAGE("%s", e.what());
6004 catch (...)
6006 if ( 0 == extHandle )
6008 return NULL;
6011 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
6012 if ( 0 == handle->initialized )
6014 return NULL;
6017 handle->ERROR_MESSAGE("Unknown exception thrown");
6020 return NULL;
6023 void
6024 GEOSSTRtree_iterate_r(GEOSContextHandle_t extHandle,
6025 geos::index::strtree::STRtree *tree,
6026 GEOSQueryCallback callback,
6027 void *userdata)
6029 GEOSContextHandleInternal_t *handle = 0;
6030 assert(tree != 0);
6031 assert(callback != 0);
6035 CAPI_ItemVisitor visitor(callback, userdata);
6036 tree->iterate(visitor);
6038 catch (const std::exception &e)
6040 if ( 0 == extHandle )
6042 return;
6045 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
6046 if ( 0 == handle->initialized )
6048 return;
6051 handle->ERROR_MESSAGE("%s", e.what());
6053 catch (...)
6055 if ( 0 == extHandle )
6057 return;
6060 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
6061 if ( 0 == handle->initialized )
6063 return;
6066 handle->ERROR_MESSAGE("Unknown exception thrown");
6070 char
6071 GEOSSTRtree_remove_r(GEOSContextHandle_t extHandle,
6072 geos::index::strtree::STRtree *tree,
6073 const geos::geom::Geometry *g,
6074 void *item)
6076 assert(0 != tree);
6077 assert(0 != g);
6079 if ( 0 == extHandle )
6081 return 2;
6084 GEOSContextHandleInternal_t *handle = 0;
6085 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
6086 if ( 0 == handle->initialized )
6088 return 2;
6093 bool result = tree->remove(g->getEnvelopeInternal(), item);
6094 return result;
6096 catch (const std::exception &e)
6098 handle->ERROR_MESSAGE("%s", e.what());
6100 catch (...)
6102 handle->ERROR_MESSAGE("Unknown exception thrown");
6105 return 2;
6108 void
6109 GEOSSTRtree_destroy_r(GEOSContextHandle_t extHandle,
6110 geos::index::strtree::STRtree *tree)
6112 GEOSContextHandleInternal_t *handle = 0;
6116 delete tree;
6118 catch (const std::exception &e)
6120 if ( 0 == extHandle )
6122 return;
6125 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
6126 if ( 0 == handle->initialized )
6128 return;
6131 handle->ERROR_MESSAGE("%s", e.what());
6133 catch (...)
6135 if ( 0 == extHandle )
6137 return;
6140 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
6141 if ( 0 == handle->initialized )
6143 return;
6146 handle->ERROR_MESSAGE("Unknown exception thrown");
6150 double
6151 GEOSProject_r(GEOSContextHandle_t extHandle,
6152 const Geometry *g,
6153 const Geometry *p)
6155 if ( 0 == extHandle ) return -1.0;
6156 GEOSContextHandleInternal_t *handle =
6157 reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
6158 if ( handle->initialized == 0 ) return -1.0;
6160 const geos::geom::Point* point = dynamic_cast<const geos::geom::Point*>(p);
6161 if (!point) {
6162 handle->ERROR_MESSAGE("third argument of GEOSProject_r must be Point*");
6163 return -1.0;
6166 const geos::geom::Coordinate* inputPt = p->getCoordinate();
6168 try {
6169 return geos::linearref::LengthIndexedLine(g).project(*inputPt);
6170 } catch (const std::exception &e) {
6171 handle->ERROR_MESSAGE("%s", e.what());
6172 return -1.0;
6173 } catch (...) {
6174 handle->ERROR_MESSAGE("Unknown exception thrown");
6175 return -1.0;
6180 Geometry*
6181 GEOSInterpolate_r(GEOSContextHandle_t extHandle, const Geometry *g, double d)
6183 if ( 0 == extHandle ) return 0;
6184 GEOSContextHandleInternal_t *handle =
6185 reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
6186 if ( handle->initialized == 0 ) return 0;
6188 try {
6189 geos::linearref::LengthIndexedLine lil(g);
6190 geos::geom::Coordinate coord = lil.extractPoint(d);
6191 const GeometryFactory *gf = handle->geomFactory;
6192 Geometry* point = gf->createPoint(coord);
6193 return point;
6194 } catch (const std::exception &e) {
6195 handle->ERROR_MESSAGE("%s", e.what());
6196 return 0;
6197 } catch (...) {
6198 handle->ERROR_MESSAGE("Unknown exception thrown");
6199 return 0;
6204 double
6205 GEOSProjectNormalized_r(GEOSContextHandle_t extHandle, const Geometry *g,
6206 const Geometry *p)
6209 double length;
6210 GEOSLength_r(extHandle, g, &length);
6211 return GEOSProject_r(extHandle, g, p) / length;
6215 Geometry*
6216 GEOSInterpolateNormalized_r(GEOSContextHandle_t extHandle, const Geometry *g,
6217 double d)
6219 double length;
6220 GEOSLength_r(extHandle, g, &length);
6221 return GEOSInterpolate_r(extHandle, g, d * length);
6224 GEOSGeometry*
6225 GEOSGeom_extractUniquePoints_r(GEOSContextHandle_t extHandle,
6226 const GEOSGeometry* g)
6228 if ( 0 == extHandle ) return 0;
6229 GEOSContextHandleInternal_t *handle = 0;
6230 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
6231 if ( handle->initialized == 0 ) return 0;
6233 using namespace geos::geom;
6234 using namespace geos::util;
6239 /* 1: extract points */
6240 std::vector<const Coordinate*> coords;
6241 UniqueCoordinateArrayFilter filter(coords);
6242 g->apply_ro(&filter);
6244 /* 2: for each point, create a geometry and put into a vector */
6245 std::vector<Geometry*>* points = new std::vector<Geometry*>();
6246 points->reserve(coords.size());
6247 const GeometryFactory* factory = g->getFactory();
6248 for (std::vector<const Coordinate*>::iterator it=coords.begin(),
6249 itE=coords.end();
6250 it != itE; ++it)
6252 Geometry* point = factory->createPoint(*(*it));
6253 points->push_back(point);
6256 /* 3: create a multipoint */
6257 return factory->createMultiPoint(points);
6260 catch (const std::exception &e)
6262 handle->ERROR_MESSAGE("%s", e.what());
6263 return 0;
6265 catch (...)
6267 handle->ERROR_MESSAGE("Unknown exception thrown");
6268 return 0;
6272 int GEOSOrientationIndex_r(GEOSContextHandle_t extHandle,
6273 double Ax, double Ay, double Bx, double By, double Px, double Py)
6275 GEOSContextHandleInternal_t *handle = 0;
6277 using geos::geom::Coordinate;
6278 using geos::algorithm::CGAlgorithms;
6280 if ( 0 == extHandle )
6282 return 2;
6285 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
6286 if ( 0 == handle->initialized )
6288 return 2;
6293 Coordinate A(Ax, Ay);
6294 Coordinate B(Bx, By);
6295 Coordinate P(Px, Py);
6296 return CGAlgorithms::orientationIndex(A, B, P);
6298 catch (const std::exception &e)
6300 handle->ERROR_MESSAGE("%s", e.what());
6301 return 2;
6303 catch (...)
6305 handle->ERROR_MESSAGE("Unknown exception thrown");
6306 return 2;
6310 GEOSGeometry *
6311 GEOSSharedPaths_r(GEOSContextHandle_t extHandle, const GEOSGeometry* g1, const GEOSGeometry* g2)
6313 using namespace geos::operation::sharedpaths;
6315 if ( 0 == extHandle ) return 0;
6316 GEOSContextHandleInternal_t *handle =
6317 reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
6318 if ( handle->initialized == 0 ) return 0;
6320 SharedPathsOp::PathList forw, back;
6321 try {
6322 SharedPathsOp::sharedPathsOp(*g1, *g2, forw, back);
6324 catch (const std::exception &e)
6326 SharedPathsOp::clearEdges(forw);
6327 SharedPathsOp::clearEdges(back);
6328 handle->ERROR_MESSAGE("%s", e.what());
6329 return 0;
6331 catch (...)
6333 SharedPathsOp::clearEdges(forw);
6334 SharedPathsOp::clearEdges(back);
6335 handle->ERROR_MESSAGE("Unknown exception thrown");
6336 return 0;
6339 // Now forw and back have the geoms we want to use to construct
6340 // our output GeometryCollections...
6342 const GeometryFactory* factory = g1->getFactory();
6343 size_t count;
6345 std::auto_ptr< std::vector<Geometry*> > out1(
6346 new std::vector<Geometry*>()
6348 count = forw.size();
6349 out1->reserve(count);
6350 for (size_t i=0; i<count; ++i) {
6351 out1->push_back(forw[i]);
6353 std::auto_ptr<Geometry> out1g (
6354 factory->createMultiLineString(out1.release())
6357 std::auto_ptr< std::vector<Geometry*> > out2(
6358 new std::vector<Geometry*>()
6360 count = back.size();
6361 out2->reserve(count);
6362 for (size_t i=0; i<count; ++i) {
6363 out2->push_back(back[i]);
6365 std::auto_ptr<Geometry> out2g (
6366 factory->createMultiLineString(out2.release())
6369 std::auto_ptr< std::vector<Geometry*> > out(
6370 new std::vector<Geometry*>()
6372 out->reserve(2);
6373 out->push_back(out1g.release());
6374 out->push_back(out2g.release());
6376 std::auto_ptr<Geometry> outg (
6377 factory->createGeometryCollection(out.release())
6380 return outg.release();
6384 GEOSGeometry *
6385 GEOSSnap_r(GEOSContextHandle_t extHandle, const GEOSGeometry* g1,
6386 const GEOSGeometry* g2, double tolerance)
6388 using namespace geos::operation::overlay::snap;
6390 if ( 0 == extHandle ) return 0;
6391 GEOSContextHandleInternal_t *handle =
6392 reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
6393 if ( handle->initialized == 0 ) return 0;
6395 try{
6396 GeometrySnapper snapper( *g1 );
6397 std::auto_ptr<Geometry> ret = snapper.snapTo(*g2, tolerance);
6398 return ret.release();
6400 catch (const std::exception &e)
6402 handle->ERROR_MESSAGE("%s", e.what());
6403 return 0;
6405 catch (...)
6407 handle->ERROR_MESSAGE("Unknown exception thrown");
6408 return 0;
6412 BufferParameters *
6413 GEOSBufferParams_create_r(GEOSContextHandle_t extHandle)
6415 if ( 0 == extHandle ) return NULL;
6417 GEOSContextHandleInternal_t *handle = 0;
6418 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
6419 if ( 0 == handle->initialized ) return NULL;
6423 BufferParameters *p = new BufferParameters();
6424 return p;
6426 catch (const std::exception &e)
6428 handle->ERROR_MESSAGE("%s", e.what());
6430 catch (...)
6432 handle->ERROR_MESSAGE("Unknown exception thrown");
6435 return 0;
6438 void
6439 GEOSBufferParams_destroy_r(GEOSContextHandle_t extHandle, BufferParameters* p)
6441 (void)extHandle;
6442 delete p;
6446 GEOSBufferParams_setEndCapStyle_r(GEOSContextHandle_t extHandle,
6447 GEOSBufferParams* p, int style)
6449 if ( 0 == extHandle ) return 0;
6451 GEOSContextHandleInternal_t *handle = 0;
6452 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
6453 if ( 0 == handle->initialized ) return 0;
6457 if ( style > BufferParameters::CAP_SQUARE )
6459 throw IllegalArgumentException("Invalid buffer endCap style");
6461 p->setEndCapStyle(static_cast<BufferParameters::EndCapStyle>(style));
6462 return 1;
6464 catch (const std::exception &e)
6466 handle->ERROR_MESSAGE("%s", e.what());
6468 catch (...)
6470 handle->ERROR_MESSAGE("Unknown exception thrown");
6473 return 0;
6477 GEOSBufferParams_setJoinStyle_r(GEOSContextHandle_t extHandle,
6478 GEOSBufferParams* p, int style)
6480 if ( 0 == extHandle ) return 0;
6482 GEOSContextHandleInternal_t *handle = 0;
6483 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
6484 if ( 0 == handle->initialized ) return 0;
6488 if ( style > BufferParameters::JOIN_BEVEL ) {
6489 throw IllegalArgumentException("Invalid buffer join style");
6491 p->setJoinStyle(static_cast<BufferParameters::JoinStyle>(style));
6492 return 1;
6494 catch (const std::exception &e)
6496 handle->ERROR_MESSAGE("%s", e.what());
6498 catch (...)
6500 handle->ERROR_MESSAGE("Unknown exception thrown");
6503 return 0;
6507 GEOSBufferParams_setMitreLimit_r(GEOSContextHandle_t extHandle,
6508 GEOSBufferParams* p, double limit)
6510 if ( 0 == extHandle ) return 0;
6512 GEOSContextHandleInternal_t *handle = 0;
6513 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
6514 if ( 0 == handle->initialized ) return 0;
6518 p->setMitreLimit(limit);
6519 return 1;
6521 catch (const std::exception &e)
6523 handle->ERROR_MESSAGE("%s", e.what());
6525 catch (...)
6527 handle->ERROR_MESSAGE("Unknown exception thrown");
6530 return 0;
6534 GEOSBufferParams_setQuadrantSegments_r(GEOSContextHandle_t extHandle,
6535 GEOSBufferParams* p, int segs)
6537 if ( 0 == extHandle ) return 0;
6539 GEOSContextHandleInternal_t *handle = 0;
6540 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
6541 if ( 0 == handle->initialized ) return 0;
6545 p->setQuadrantSegments(segs);
6546 return 1;
6548 catch (const std::exception &e)
6550 handle->ERROR_MESSAGE("%s", e.what());
6552 catch (...)
6554 handle->ERROR_MESSAGE("Unknown exception thrown");
6557 return 0;
6561 GEOSBufferParams_setSingleSided_r(GEOSContextHandle_t extHandle,
6562 GEOSBufferParams* p, int ss)
6564 if ( 0 == extHandle ) return 0;
6566 GEOSContextHandleInternal_t *handle = 0;
6567 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
6568 if ( 0 == handle->initialized ) return 0;
6572 p->setSingleSided( (ss != 0) );
6573 return 1;
6575 catch (const std::exception &e)
6577 handle->ERROR_MESSAGE("%s", e.what());
6579 catch (...)
6581 handle->ERROR_MESSAGE("Unknown exception thrown");
6584 return 0;
6587 Geometry *
6588 GEOSBufferWithParams_r(GEOSContextHandle_t extHandle, const Geometry *g1, const BufferParameters* bp, double width)
6590 using geos::operation::buffer::BufferOp;
6592 if ( 0 == extHandle ) return NULL;
6594 GEOSContextHandleInternal_t *handle = 0;
6595 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
6596 if ( 0 == handle->initialized ) return NULL;
6600 BufferOp op(g1, *bp);
6601 Geometry *g3 = op.getResultGeometry(width);
6602 return g3;
6604 catch (const std::exception &e)
6606 handle->ERROR_MESSAGE("%s", e.what());
6608 catch (...)
6610 handle->ERROR_MESSAGE("Unknown exception thrown");
6613 return NULL;
6616 Geometry *
6617 GEOSDelaunayTriangulation_r(GEOSContextHandle_t extHandle, const Geometry *g1, double tolerance, int onlyEdges)
6619 if ( 0 == extHandle ) return NULL;
6621 GEOSContextHandleInternal_t *handle = 0;
6622 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
6623 if ( 0 == handle->initialized ) return NULL;
6625 using geos::triangulate::DelaunayTriangulationBuilder;
6629 DelaunayTriangulationBuilder builder;
6630 builder.setTolerance(tolerance);
6631 builder.setSites(*g1);
6633 if ( onlyEdges ) return builder.getEdges( *g1->getFactory() ).release();
6634 else return builder.getTriangles( *g1->getFactory() ).release();
6637 catch (const std::exception &e)
6639 handle->ERROR_MESSAGE("%s", e.what());
6641 catch (...)
6643 handle->ERROR_MESSAGE("Unknown exception thrown");
6646 return NULL;
6648 Geometry*
6649 GEOSVoronoiDiagram_r(GEOSContextHandle_t extHandle, const Geometry *g1, const Geometry *env, double tolerance ,int onlyEdges)
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;
6657 using geos::triangulate::VoronoiDiagramBuilder;
6661 VoronoiDiagramBuilder builder;
6662 builder.setSites(*g1);
6663 builder.setTolerance(tolerance);
6664 if(env) builder.setClipEnvelope(env->getEnvelopeInternal());
6665 if(onlyEdges) return builder.getDiagramEdges(*g1->getFactory()).release();
6666 else return builder.getDiagram(*g1->getFactory()).release();
6668 catch(const std::exception &e)
6670 handle->ERROR_MESSAGE("%s", e.what());
6672 catch(...)
6674 handle->ERROR_MESSAGE("Unknow exception thrown");
6677 return NULL;
6680 } /* extern "C" */