Complete Note#1 in the http://wiki.osgeo.org/wiki/GEOS_Provenance_Review to get out...
[geos.git] / capi / geos_ts_c.cpp
bloba98761192fd8c4b3903c7420c4b004bcde6340cb
1 /************************************************************************
4 * C-Wrapper for GEOS library
6 * Copyright (C) 2010 2011 Sandro Santilli <strk@keybit.net>
7 * Copyright (C) 2005-2006 Refractions Research Inc.
9 * This is free software; you can redistribute and/or modify it under
10 * the terms of the GNU Lesser General Public Licence as published
11 * by the Free Software Foundation.
12 * See the COPYING file for more information.
14 * Author: Sandro Santilli <strk@keybit.net>
15 * Thread Safety modifications: Chuck Thibert <charles.thibert@ingres.com>
17 ***********************************************************************/
19 #include <geos/platform.h> // for FINITE
20 #include <geos/geom/Geometry.h>
21 #include <geos/geom/prep/PreparedGeometry.h>
22 #include <geos/geom/prep/PreparedGeometryFactory.h>
23 #include <geos/geom/GeometryCollection.h>
24 #include <geos/geom/Polygon.h>
25 #include <geos/geom/Point.h>
26 #include <geos/geom/MultiPoint.h>
27 #include <geos/geom/MultiLineString.h>
28 #include <geos/geom/MultiPolygon.h>
29 #include <geos/geom/LinearRing.h>
30 #include <geos/geom/LineString.h>
31 #include <geos/geom/PrecisionModel.h>
32 #include <geos/geom/GeometryFactory.h>
33 #include <geos/geom/CoordinateSequenceFactory.h>
34 #include <geos/geom/Coordinate.h>
35 #include <geos/geom/IntersectionMatrix.h>
36 #include <geos/geom/Envelope.h>
37 #include <geos/index/strtree/STRtree.h>
38 #include <geos/index/ItemVisitor.h>
39 #include <geos/io/WKTReader.h>
40 #include <geos/io/WKBReader.h>
41 #include <geos/io/WKTWriter.h>
42 #include <geos/io/WKBWriter.h>
43 #include <geos/algorithm/distance/DiscreteHausdorffDistance.h>
44 #include <geos/algorithm/CGAlgorithms.h>
45 #include <geos/algorithm/BoundaryNodeRule.h>
46 #include <geos/simplify/DouglasPeuckerSimplifier.h>
47 #include <geos/simplify/TopologyPreservingSimplifier.h>
48 #include <geos/operation/valid/IsValidOp.h>
49 #include <geos/operation/polygonize/Polygonizer.h>
50 #include <geos/operation/linemerge/LineMerger.h>
51 #include <geos/operation/overlay/OverlayOp.h>
52 #include <geos/operation/union/CascadedPolygonUnion.h>
53 #include <geos/operation/buffer/BufferOp.h>
54 #include <geos/operation/buffer/BufferParameters.h>
55 #include <geos/operation/buffer/BufferBuilder.h>
56 #include <geos/operation/relate/RelateOp.h>
57 #include <geos/operation/sharedpaths/SharedPathsOp.h>
58 #include <geos/linearref/LengthIndexedLine.h>
59 #include <geos/geom/BinaryOp.h>
60 #include <geos/util/IllegalArgumentException.h>
61 #include <geos/util/UniqueCoordinateArrayFilter.h>
62 #include <geos/util/Machine.h>
63 #include <geos/version.h>
65 // This should go away
66 #include <cmath> // finite
67 #include <cstddef>
68 #include <cstdio>
69 #include <cstdlib>
70 #include <cstring>
71 #include <fstream>
72 #include <iostream>
73 #include <sstream>
74 #include <string>
75 #include <memory>
77 #ifdef _MSC_VER
78 #pragma warning(disable : 4099)
79 #endif
81 // Some extra magic to make type declarations in geos_c.h work -
82 // for cross-checking of types in header.
83 #define GEOSGeometry geos::geom::Geometry
84 #define GEOSPreparedGeometry geos::geom::prep::PreparedGeometry
85 #define GEOSCoordSequence geos::geom::CoordinateSequence
86 #define GEOSBufferParams geos::operation::buffer::BufferParameters
87 #define GEOSSTRtree geos::index::strtree::STRtree
88 #define GEOSWKTReader_t geos::io::WKTReader
89 #define GEOSWKTWriter_t geos::io::WKTWriter
90 #define GEOSWKBReader_t geos::io::WKBReader
91 #define GEOSWKBWriter_t geos::io::WKBWriter
93 #include "geos_c.h"
95 // Intentional, to allow non-standard C elements like C99 functions to be
96 // imported through C++ headers of C library, like <cmath>.
97 using namespace std;
99 /// Define this if you want operations triggering Exceptions to
100 /// be printed.
101 /// (will use the NOTIFY channel - only implemented for GEOSUnion so far)
103 #undef VERBOSE_EXCEPTIONS
105 #include <geos/export.h>
108 // import the most frequently used definitions globally
109 using geos::geom::Geometry;
110 using geos::geom::LineString;
111 using geos::geom::Polygon;
112 using geos::geom::CoordinateSequence;
113 using geos::geom::GeometryFactory;
115 using geos::io::WKTReader;
116 using geos::io::WKTWriter;
117 using geos::io::WKBReader;
118 using geos::io::WKBWriter;
120 using geos::operation::overlay::OverlayOp;
121 using geos::operation::overlay::overlayOp;
122 using geos::operation::geounion::CascadedPolygonUnion;
123 using geos::operation::buffer::BufferParameters;
124 using geos::operation::buffer::BufferBuilder;
125 using geos::util::IllegalArgumentException;
126 using geos::algorithm::distance::DiscreteHausdorffDistance;
128 typedef std::auto_ptr<Geometry> GeomAutoPtr;
130 typedef struct GEOSContextHandleInternal
132 const GeometryFactory *geomFactory;
133 GEOSMessageHandler NOTICE_MESSAGE;
134 GEOSMessageHandler ERROR_MESSAGE;
135 int WKBOutputDims;
136 int WKBByteOrder;
137 int initialized;
138 } GEOSContextHandleInternal_t;
140 // CAPI_ItemVisitor is used internally by the CAPI STRtree
141 // wrappers. It's defined here just to keep it out of the
142 // extern "C" block.
143 class CAPI_ItemVisitor : public geos::index::ItemVisitor {
144 GEOSQueryCallback callback;
145 void *userdata;
146 public:
147 CAPI_ItemVisitor (GEOSQueryCallback cb, void *ud)
148 : ItemVisitor(), callback(cb), userdata(ud) {}
149 void visitItem (void *item) { callback(item, userdata); }
153 //## PROTOTYPES #############################################
155 extern "C" const char GEOS_DLL *GEOSjtsport();
156 extern "C" char GEOS_DLL *GEOSasText(Geometry *g1);
158 namespace { // anonymous
160 char* gstrdup_s(const char* str, const std::size_t size)
162 char* out = static_cast<char*>(std::malloc(size + 1));
163 if (0 != out)
165 // as no strlen call necessary, memcpy may be faster than strcpy
166 std::memcpy(out, str, size + 1);
169 assert(0 != out);
171 // we haven't been checking allocation before ticket #371
172 if (0 == out)
174 throw(std::runtime_error("Failed to allocate memory for duplicate string"));
177 return out;
180 char* gstrdup(std::string const& str)
182 return gstrdup_s(str.c_str(), str.size());
185 } // namespace anonymous
187 extern "C" {
189 GEOSContextHandle_t
190 initGEOS_r(GEOSMessageHandler nf, GEOSMessageHandler ef)
192 GEOSContextHandleInternal_t *handle = 0;
193 void *extHandle = 0;
195 extHandle = std::malloc(sizeof(GEOSContextHandleInternal_t));
196 if (0 != extHandle)
198 handle = static_cast<GEOSContextHandleInternal_t*>(extHandle);
199 handle->NOTICE_MESSAGE = nf;
200 handle->ERROR_MESSAGE = ef;
201 handle->geomFactory = GeometryFactory::getDefaultInstance();
202 handle->WKBOutputDims = 2;
203 handle->WKBByteOrder = getMachineByteOrder();
204 handle->initialized = 1;
207 return static_cast<GEOSContextHandle_t>(extHandle);
210 GEOSMessageHandler
211 GEOSContext_setNoticeHandler_r(GEOSContextHandle_t extHandle, GEOSMessageHandler nf)
213 GEOSMessageHandler f;
214 GEOSContextHandleInternal_t *handle = 0;
215 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
216 if ( 0 == handle->initialized )
218 return NULL;
221 f = handle->NOTICE_MESSAGE;
222 handle->NOTICE_MESSAGE = nf;
224 return f;
227 GEOSMessageHandler
228 GEOSContext_setErrorHandler_r(GEOSContextHandle_t extHandle, GEOSMessageHandler nf)
230 GEOSMessageHandler f;
231 GEOSContextHandleInternal_t *handle = 0;
232 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
233 if ( 0 == handle->initialized )
235 return NULL;
238 f = handle->ERROR_MESSAGE;
239 handle->ERROR_MESSAGE = nf;
241 return f;
244 void
245 finishGEOS_r(GEOSContextHandle_t extHandle)
247 // Fix up freeing handle w.r.t. malloc above
248 std::free(extHandle);
249 extHandle = NULL;
252 void
253 GEOSFree_r (GEOSContextHandle_t extHandle, void* buffer)
255 assert(0 != extHandle);
257 std::free(buffer);
260 //-----------------------------------------------------------
261 // relate()-related functions
262 // return 0 = false, 1 = true, 2 = error occured
263 //-----------------------------------------------------------
265 char
266 GEOSDisjoint_r(GEOSContextHandle_t extHandle, const Geometry *g1, const Geometry *g2)
268 if ( 0 == extHandle )
270 return 2;
273 GEOSContextHandleInternal_t *handle = 0;
274 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
275 if ( handle->initialized == 0 )
277 return 2;
282 bool result = g1->disjoint(g2);
283 return result;
286 // TODO: mloskot is going to replace these double-catch block
287 // with a macro to remove redundant code in this and
288 // following functions.
289 catch (const std::exception &e)
291 handle->ERROR_MESSAGE("%s", e.what());
293 catch (...)
295 handle->ERROR_MESSAGE("Unknown exception thrown");
298 return 2;
301 char
302 GEOSTouches_r(GEOSContextHandle_t extHandle, const Geometry *g1, const Geometry *g2)
304 if ( 0 == extHandle )
306 return 2;
309 GEOSContextHandleInternal_t *handle = 0;
310 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
311 if ( 0 == handle->initialized )
313 return 2;
318 bool result = g1->touches(g2);
319 return result;
321 catch (const std::exception &e)
323 handle->ERROR_MESSAGE("%s", e.what());
325 catch (...)
327 handle->ERROR_MESSAGE("Unknown exception thrown");
330 return 2;
333 char
334 GEOSIntersects_r(GEOSContextHandle_t extHandle, const Geometry *g1, const Geometry *g2)
336 if ( 0 == extHandle )
338 return 2;
341 GEOSContextHandleInternal_t *handle = 0;
342 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
343 if ( 0 == handle->initialized )
345 return 2;
350 bool result = g1->intersects(g2);
351 return result;
353 catch (const std::exception &e)
355 handle->ERROR_MESSAGE("%s", e.what());
357 catch (...)
359 handle->ERROR_MESSAGE("Unknown exception thrown");
362 return 2;
365 char
366 GEOSCrosses_r(GEOSContextHandle_t extHandle, const Geometry *g1, const Geometry *g2)
368 if ( 0 == extHandle )
370 return 2;
373 GEOSContextHandleInternal_t *handle = 0;
374 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
375 if ( 0 == handle->initialized )
377 return 2;
382 bool result = g1->crosses(g2);
383 return result;
385 catch (const std::exception &e)
387 handle->ERROR_MESSAGE("%s", e.what());
389 catch (...)
391 handle->ERROR_MESSAGE("Unknown exception thrown");
394 return 2;
397 char
398 GEOSWithin_r(GEOSContextHandle_t extHandle, const Geometry *g1, const Geometry *g2)
400 if ( 0 == extHandle )
402 return 2;
405 GEOSContextHandleInternal_t *handle = 0;
406 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
407 if ( 0 == handle->initialized )
409 return 2;
414 bool result = g1->within(g2);
415 return result;
417 catch (const std::exception &e)
419 handle->ERROR_MESSAGE("%s", e.what());
421 catch (...)
423 handle->ERROR_MESSAGE("Unknown exception thrown");
426 return 2;
429 // call g1->contains(g2)
430 // returns 0 = false
431 // 1 = true
432 // 2 = error was trapped
433 char
434 GEOSContains_r(GEOSContextHandle_t extHandle, const Geometry *g1, const Geometry *g2)
436 if ( 0 == extHandle )
438 return 2;
441 GEOSContextHandleInternal_t *handle = 0;
442 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
443 if ( 0 == handle->initialized )
445 return 2;
450 bool result = g1->contains(g2);
451 return result;
453 catch (const std::exception &e)
455 handle->ERROR_MESSAGE("%s", e.what());
457 catch (...)
459 handle->ERROR_MESSAGE("Unknown exception thrown");
462 return 2;
465 char
466 GEOSOverlaps_r(GEOSContextHandle_t extHandle, const Geometry *g1, const Geometry *g2)
468 if ( 0 == extHandle )
470 return 2;
473 GEOSContextHandleInternal_t *handle = 0;
474 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
475 if ( 0 == handle->initialized )
477 return 2;
482 bool result = g1->overlaps(g2);
483 return result;
485 catch (const std::exception &e)
487 handle->ERROR_MESSAGE("%s", e.what());
489 catch (...)
491 handle->ERROR_MESSAGE("Unknown exception thrown");
494 return 2;
497 char
498 GEOSCovers_r(GEOSContextHandle_t extHandle, const Geometry *g1, const Geometry *g2)
500 if ( 0 == extHandle )
502 return 2;
505 GEOSContextHandleInternal_t *handle = 0;
506 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
507 if ( 0 == handle->initialized )
509 return 2;
514 bool result = g1->covers(g2);
515 return result;
517 catch (const std::exception &e)
519 handle->ERROR_MESSAGE("%s", e.what());
521 catch (...)
523 handle->ERROR_MESSAGE("Unknown exception thrown");
526 return 2;
529 char
530 GEOSCoveredBy_r(GEOSContextHandle_t extHandle, const Geometry *g1, const Geometry *g2)
532 if ( 0 == extHandle )
534 return 2;
537 GEOSContextHandleInternal_t *handle = 0;
538 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
539 if ( 0 == handle->initialized )
541 return 2;
546 bool result = g1->coveredBy(g2);
547 return result;
549 catch (const std::exception &e)
551 handle->ERROR_MESSAGE("%s", e.what());
553 catch (...)
555 handle->ERROR_MESSAGE("Unknown exception thrown");
558 return 2;
562 //-------------------------------------------------------------------
563 // low-level relate functions
564 //------------------------------------------------------------------
566 char
567 GEOSRelatePattern_r(GEOSContextHandle_t extHandle, const Geometry *g1, const Geometry *g2, const char *pat)
569 if ( 0 == extHandle )
571 return 2;
574 GEOSContextHandleInternal_t *handle = 0;
575 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
576 if ( 0 == handle->initialized )
578 return 2;
583 std::string s(pat);
584 bool result = g1->relate(g2, s);
585 return result;
587 catch (const std::exception &e)
589 handle->ERROR_MESSAGE("%s", e.what());
591 catch (...)
593 handle->ERROR_MESSAGE("Unknown exception thrown");
596 return 2;
599 char
600 GEOSRelatePatternMatch_r(GEOSContextHandle_t extHandle, const char *mat,
601 const char *pat)
603 if ( 0 == extHandle )
605 return 2;
608 GEOSContextHandleInternal_t *handle = 0;
609 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
610 if ( 0 == handle->initialized )
612 return 2;
617 using geos::geom::IntersectionMatrix;
619 std::string m(mat);
620 std::string p(pat);
621 IntersectionMatrix im(m);
623 bool result = im.matches(p);
624 return result;
626 catch (const std::exception &e)
628 handle->ERROR_MESSAGE("%s", e.what());
630 catch (...)
632 handle->ERROR_MESSAGE("Unknown exception thrown");
635 return 2;
638 char *
639 GEOSRelate_r(GEOSContextHandle_t extHandle, const Geometry *g1, const Geometry *g2)
641 if ( 0 == extHandle )
643 return NULL;
646 GEOSContextHandleInternal_t *handle = 0;
647 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
648 if ( 0 == handle->initialized )
650 return NULL;
655 using geos::geom::IntersectionMatrix;
657 IntersectionMatrix* im = g1->relate(g2);
658 if (0 == im)
660 return 0;
663 char *result = gstrdup(im->toString());
665 delete im;
666 im = 0;
668 return result;
670 catch (const std::exception &e)
672 handle->ERROR_MESSAGE("%s", e.what());
674 catch (...)
676 handle->ERROR_MESSAGE("Unknown exception thrown");
679 return NULL;
682 char *
683 GEOSRelateBoundaryNodeRule_r(GEOSContextHandle_t extHandle, const Geometry *g1, const Geometry *g2, int bnr)
685 if ( 0 == extHandle )
687 return NULL;
690 GEOSContextHandleInternal_t *handle = 0;
691 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
692 if ( 0 == handle->initialized )
694 return NULL;
699 using geos::operation::relate::RelateOp;
700 using geos::geom::IntersectionMatrix;
701 using geos::algorithm::BoundaryNodeRule;
703 IntersectionMatrix* im;
704 switch (bnr) {
705 case GEOSRELATE_BNR_MOD2: /* same as OGC */
706 im = RelateOp::relate(g1, g2,
707 BoundaryNodeRule::MOD2_BOUNDARY_RULE);
708 break;
709 case GEOSRELATE_BNR_ENDPOINT:
710 im = RelateOp::relate(g1, g2,
711 BoundaryNodeRule::ENDPOINT_BOUNDARY_RULE);
712 break;
713 case GEOSRELATE_BNR_MULTIVALENT_ENDPOINT:
714 im = RelateOp::relate(g1, g2,
715 BoundaryNodeRule::MULTIVALENT_ENDPOINT_BOUNDARY_RULE);
716 break;
717 case GEOSRELATE_BNR_MONOVALENT_ENDPOINT:
718 im = RelateOp::relate(g1, g2,
719 BoundaryNodeRule::MONOVALENT_ENDPOINT_BOUNDARY_RULE);
720 break;
721 default:
722 handle->ERROR_MESSAGE("Invalid boundary node rule %d", bnr);
723 return 0;
724 break;
727 if (0 == im) return 0;
729 char *result = gstrdup(im->toString());
731 delete im;
732 im = 0;
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 NULL;
750 //-----------------------------------------------------------------
751 // isValid
752 //-----------------------------------------------------------------
755 char
756 GEOSisValid_r(GEOSContextHandle_t extHandle, const Geometry *g1)
758 if ( 0 == extHandle )
760 return 2;
763 GEOSContextHandleInternal_t *handle = 0;
764 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
765 if ( 0 == handle->initialized )
767 return 2;
772 using geos::operation::valid::IsValidOp;
773 using geos::operation::valid::TopologyValidationError;
775 IsValidOp ivo(g1);
776 TopologyValidationError *err = ivo.getValidationError();
777 if ( err )
779 handle->NOTICE_MESSAGE("%s", err->toString().c_str());
780 return 0;
782 else
784 return 1;
787 catch (const std::exception &e)
789 handle->ERROR_MESSAGE("%s", e.what());
791 catch (...)
793 handle->ERROR_MESSAGE("Unknown exception thrown");
796 return 2;
799 char *
800 GEOSisValidReason_r(GEOSContextHandle_t extHandle, const Geometry *g1)
802 if ( 0 == extHandle )
804 return NULL;
807 GEOSContextHandleInternal_t *handle = 0;
808 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
809 if ( 0 == handle->initialized )
811 return NULL;
816 using geos::operation::valid::IsValidOp;
817 using geos::operation::valid::TopologyValidationError;
819 char* result = 0;
820 char const* const validstr = "Valid Geometry";
822 IsValidOp ivo(g1);
823 TopologyValidationError *err = ivo.getValidationError();
824 if (0 != err)
826 std::ostringstream ss;
827 ss.precision(15);
828 ss << err->getCoordinate();
829 const std::string errloc = ss.str();
830 std::string errmsg(err->getMessage());
831 errmsg += "[" + errloc + "]";
832 result = gstrdup(errmsg);
834 else
836 result = gstrdup(std::string(validstr));
839 return result;
841 catch (const std::exception &e)
843 handle->ERROR_MESSAGE("%s", e.what());
845 catch (...)
847 handle->ERROR_MESSAGE("Unknown exception thrown");
850 return 0;
853 char
854 GEOSisValidDetail_r(GEOSContextHandle_t extHandle, const Geometry *g,
855 int flags, char** reason, Geometry ** location)
857 if ( 0 == extHandle )
859 return 0;
862 GEOSContextHandleInternal_t *handle = 0;
863 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
864 if ( 0 == handle->initialized )
866 return 0;
871 using geos::operation::valid::IsValidOp;
872 using geos::operation::valid::TopologyValidationError;
874 IsValidOp ivo(g);
875 if ( flags & GEOSVALID_ALLOW_SELFTOUCHING_RING_FORMING_HOLE ) {
876 ivo.setSelfTouchingRingFormingHoleValid(true);
878 TopologyValidationError *err = ivo.getValidationError();
879 if (0 != err)
881 if ( location ) {
882 *location = handle->geomFactory->createPoint(err->getCoordinate());
884 if ( reason ) {
885 std::string errmsg(err->getMessage());
886 *reason = gstrdup(errmsg);
888 return 0;
891 if ( location ) *location = 0;
892 if ( reason ) *reason = 0;
893 return 1; /* valid */
896 catch (const std::exception &e)
898 handle->ERROR_MESSAGE("%s", e.what());
900 catch (...)
902 handle->ERROR_MESSAGE("Unknown exception thrown");
905 return 2; /* exception */
908 //-----------------------------------------------------------------
909 // general purpose
910 //-----------------------------------------------------------------
912 char
913 GEOSEquals_r(GEOSContextHandle_t extHandle, const Geometry *g1, const Geometry *g2)
915 if ( 0 == extHandle )
917 return 2;
920 GEOSContextHandleInternal_t *handle = 0;
921 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
922 if ( 0 == handle->initialized )
924 return 2;
929 bool result = g1->equals(g2);
930 return result;
932 catch (const std::exception &e)
934 handle->ERROR_MESSAGE("%s", e.what());
936 catch (...)
938 handle->ERROR_MESSAGE("Unknown exception thrown");
941 return 2;
944 char
945 GEOSEqualsExact_r(GEOSContextHandle_t extHandle, const Geometry *g1, const Geometry *g2, double tolerance)
947 if ( 0 == extHandle )
949 return 2;
952 GEOSContextHandleInternal_t *handle = 0;
953 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
954 if ( 0 == handle->initialized )
956 return 2;
961 bool result = g1->equalsExact(g2, tolerance);
962 return result;
964 catch (const std::exception &e)
966 handle->ERROR_MESSAGE("%s", e.what());
968 catch (...)
970 handle->ERROR_MESSAGE("Unknown exception thrown");
973 return 2;
977 GEOSDistance_r(GEOSContextHandle_t extHandle, const Geometry *g1, const Geometry *g2, double *dist)
979 assert(0 != dist);
981 if ( 0 == extHandle )
983 return 0;
986 GEOSContextHandleInternal_t *handle = 0;
987 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
988 if ( 0 == handle->initialized )
990 return 0;
995 *dist = g1->distance(g2);
996 return 1;
998 catch (const std::exception &e)
1000 handle->ERROR_MESSAGE("%s", e.what());
1002 catch (...)
1004 handle->ERROR_MESSAGE("Unknown exception thrown");
1007 return 0;
1011 GEOSHausdorffDistance_r(GEOSContextHandle_t extHandle, const Geometry *g1, const Geometry *g2, double *dist)
1013 assert(0 != dist);
1015 if ( 0 == extHandle )
1017 return 0;
1020 GEOSContextHandleInternal_t *handle = 0;
1021 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
1022 if ( 0 == handle->initialized )
1024 return 0;
1029 *dist = DiscreteHausdorffDistance::distance(*g1, *g2);
1030 return 1;
1032 catch (const std::exception &e)
1034 handle->ERROR_MESSAGE("%s", e.what());
1036 catch (...)
1038 handle->ERROR_MESSAGE("Unknown exception thrown");
1041 return 0;
1045 GEOSHausdorffDistanceDensify_r(GEOSContextHandle_t extHandle, const Geometry *g1, const Geometry *g2, double densifyFrac, double *dist)
1047 assert(0 != dist);
1049 if ( 0 == extHandle )
1051 return 0;
1054 GEOSContextHandleInternal_t *handle = 0;
1055 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
1056 if ( 0 == handle->initialized )
1058 return 0;
1063 *dist = DiscreteHausdorffDistance::distance(*g1, *g2, densifyFrac);
1064 return 1;
1066 catch (const std::exception &e)
1068 handle->ERROR_MESSAGE("%s", e.what());
1070 catch (...)
1072 handle->ERROR_MESSAGE("Unknown exception thrown");
1075 return 0;
1079 GEOSArea_r(GEOSContextHandle_t extHandle, const Geometry *g, double *area)
1081 assert(0 != area);
1083 if ( 0 == extHandle )
1085 return 0;
1088 GEOSContextHandleInternal_t *handle = 0;
1089 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
1090 if ( 0 == handle->initialized )
1092 return 0;
1097 *area = g->getArea();
1098 return 1;
1100 catch (const std::exception &e)
1102 handle->ERROR_MESSAGE("%s", e.what());
1104 catch (...)
1106 handle->ERROR_MESSAGE("Unknown exception thrown");
1109 return 0;
1113 GEOSLength_r(GEOSContextHandle_t extHandle, const Geometry *g, double *length)
1115 assert(0 != length);
1117 if ( 0 == extHandle )
1119 return 2;
1122 GEOSContextHandleInternal_t *handle = 0;
1123 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
1124 if ( 0 == handle->initialized )
1126 return 0;
1131 *length = g->getLength();
1132 return 1;
1134 catch (const std::exception &e)
1136 handle->ERROR_MESSAGE("%s", e.what());
1138 catch (...)
1140 handle->ERROR_MESSAGE("Unknown exception thrown");
1143 return 0;
1146 Geometry *
1147 GEOSGeomFromWKT_r(GEOSContextHandle_t extHandle, const char *wkt)
1149 if ( 0 == extHandle )
1151 return NULL;
1154 GEOSContextHandleInternal_t *handle = 0;
1155 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
1156 if ( 0 == handle->initialized )
1158 return NULL;
1163 const std::string wktstring(wkt);
1164 WKTReader r(static_cast<GeometryFactory const*>(handle->geomFactory));
1166 Geometry *g = r.read(wktstring);
1167 return g;
1169 catch (const std::exception &e)
1171 handle->ERROR_MESSAGE("%s", e.what());
1173 catch (...)
1175 handle->ERROR_MESSAGE("Unknown exception thrown");
1178 return NULL;
1181 char *
1182 GEOSGeomToWKT_r(GEOSContextHandle_t extHandle, const Geometry *g1)
1184 if ( 0 == extHandle )
1186 return NULL;
1189 GEOSContextHandleInternal_t *handle = 0;
1190 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
1191 if ( 0 == handle->initialized )
1193 return NULL;
1199 char *result = gstrdup(g1->toString());
1200 return result;
1202 catch (const std::exception &e)
1204 handle->ERROR_MESSAGE("%s", e.what());
1206 catch (...)
1208 handle->ERROR_MESSAGE("Unknown exception thrown");
1210 return NULL;
1213 // Remember to free the result!
1214 unsigned char *
1215 GEOSGeomToWKB_buf_r(GEOSContextHandle_t extHandle, const Geometry *g, size_t *size)
1217 assert(0 != size);
1219 if ( 0 == extHandle )
1221 return NULL;
1224 GEOSContextHandleInternal_t *handle = 0;
1225 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
1226 if ( 0 == handle->initialized )
1228 return NULL;
1231 using geos::io::WKBWriter;
1234 int byteOrder = handle->WKBByteOrder;
1235 WKBWriter w(handle->WKBOutputDims, byteOrder);
1236 std::ostringstream os(std::ios_base::binary);
1237 w.write(*g, os);
1238 std::string wkbstring(os.str());
1239 const std::size_t len = wkbstring.length();
1241 unsigned char* result = 0;
1242 result = static_cast<unsigned char*>(std::malloc(len));
1243 if (0 != result)
1245 std::memcpy(result, wkbstring.c_str(), len);
1246 *size = len;
1248 return result;
1250 catch (const std::exception &e)
1252 handle->ERROR_MESSAGE("%s", e.what());
1254 catch (...)
1256 handle->ERROR_MESSAGE("Unknown exception thrown");
1259 return NULL;
1262 Geometry *
1263 GEOSGeomFromWKB_buf_r(GEOSContextHandle_t extHandle, const unsigned char *wkb, size_t size)
1265 if ( 0 == extHandle )
1267 return NULL;
1270 GEOSContextHandleInternal_t *handle = 0;
1271 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
1272 if ( 0 == handle->initialized )
1274 return NULL;
1277 using geos::io::WKBReader;
1280 std::string wkbstring(reinterpret_cast<const char*>(wkb), size); // make it binary !
1281 WKBReader r(*(static_cast<GeometryFactory const*>(handle->geomFactory)));
1282 std::istringstream is(std::ios_base::binary);
1283 is.str(wkbstring);
1284 is.seekg(0, std::ios::beg); // rewind reader pointer
1285 Geometry *g = r.read(is);
1286 return g;
1288 catch (const std::exception &e)
1290 handle->ERROR_MESSAGE("%s", e.what());
1292 catch (...)
1294 handle->ERROR_MESSAGE("Unknown exception thrown");
1297 return NULL;
1300 /* Read/write wkb hex values. Returned geometries are
1301 owned by the caller.*/
1302 unsigned char *
1303 GEOSGeomToHEX_buf_r(GEOSContextHandle_t extHandle, const Geometry *g, size_t *size)
1305 if ( 0 == extHandle )
1307 return NULL;
1310 GEOSContextHandleInternal_t *handle = 0;
1311 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
1312 if ( 0 == handle->initialized )
1314 return NULL;
1317 using geos::io::WKBWriter;
1320 int byteOrder = handle->WKBByteOrder;
1321 WKBWriter w(handle->WKBOutputDims, byteOrder);
1322 std::ostringstream os(std::ios_base::binary);
1323 w.writeHEX(*g, os);
1324 std::string hexstring(os.str());
1326 char *result = gstrdup(hexstring);
1327 if (0 != result)
1329 *size = hexstring.length();
1332 return reinterpret_cast<unsigned char*>(result);
1334 catch (const std::exception &e)
1336 handle->ERROR_MESSAGE("%s", e.what());
1338 catch (...)
1340 handle->ERROR_MESSAGE("Unknown exception thrown");
1343 return NULL;
1346 Geometry *
1347 GEOSGeomFromHEX_buf_r(GEOSContextHandle_t extHandle, const unsigned char *hex, size_t size)
1349 if ( 0 == extHandle )
1351 return NULL;
1354 GEOSContextHandleInternal_t *handle = 0;
1355 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
1356 if ( 0 == handle->initialized )
1358 return NULL;
1361 using geos::io::WKBReader;
1364 std::string hexstring(reinterpret_cast<const char*>(hex), size);
1365 WKBReader r(*(static_cast<GeometryFactory const*>(handle->geomFactory)));
1366 std::istringstream is(std::ios_base::binary);
1367 is.str(hexstring);
1368 is.seekg(0, std::ios::beg); // rewind reader pointer
1370 Geometry *g = r.readHEX(is);
1371 return g;
1373 catch (const std::exception &e)
1375 handle->ERROR_MESSAGE("%s", e.what());
1377 catch (...)
1379 handle->ERROR_MESSAGE("Unknown exception thrown");
1382 return NULL;
1385 char
1386 GEOSisEmpty_r(GEOSContextHandle_t extHandle, const Geometry *g1)
1388 if ( 0 == extHandle )
1390 return 2;
1393 GEOSContextHandleInternal_t *handle = 0;
1394 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
1395 if ( 0 == handle->initialized )
1397 return 2;
1402 return g1->isEmpty();
1404 catch (const std::exception &e)
1406 handle->ERROR_MESSAGE("%s", e.what());
1408 catch (...)
1410 handle->ERROR_MESSAGE("Unknown exception thrown");
1413 return 2;
1416 char
1417 GEOSisSimple_r(GEOSContextHandle_t extHandle, const Geometry *g1)
1419 if ( 0 == extHandle )
1421 return 2;
1424 GEOSContextHandleInternal_t *handle = 0;
1425 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
1426 if ( 0 == handle->initialized )
1428 return 2;
1433 return g1->isSimple();
1435 catch (const std::exception &e)
1437 handle->ERROR_MESSAGE("%s", e.what());
1438 return 2;
1441 catch (...)
1443 handle->ERROR_MESSAGE("Unknown exception thrown");
1444 return 2;
1448 char
1449 GEOSisRing_r(GEOSContextHandle_t extHandle, const Geometry *g)
1451 if ( 0 == extHandle )
1453 return 2;
1456 GEOSContextHandleInternal_t *handle = 0;
1457 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
1458 if ( 0 == handle->initialized )
1460 return 2;
1465 const LineString *ls = dynamic_cast<const LineString *>(g);
1466 if ( ls ) {
1467 return (ls->isRing());
1468 } else {
1469 return 0;
1472 catch (const std::exception &e)
1474 handle->ERROR_MESSAGE("%s", e.what());
1475 return 2;
1478 catch (...)
1480 handle->ERROR_MESSAGE("Unknown exception thrown");
1481 return 2;
1487 //free the result of this
1488 char *
1489 GEOSGeomType_r(GEOSContextHandle_t extHandle, const Geometry *g1)
1491 if ( 0 == extHandle )
1493 return NULL;
1496 GEOSContextHandleInternal_t *handle = 0;
1497 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
1498 if ( 0 == handle->initialized )
1500 return NULL;
1505 std::string s = g1->getGeometryType();
1507 char *result = gstrdup(s);
1508 return result;
1510 catch (const std::exception &e)
1512 handle->ERROR_MESSAGE("%s", e.what());
1514 catch (...)
1516 handle->ERROR_MESSAGE("Unknown exception thrown");
1519 return NULL;
1522 // Return postgis geometry type index
1524 GEOSGeomTypeId_r(GEOSContextHandle_t extHandle, const Geometry *g1)
1526 if ( 0 == extHandle )
1528 return -1;
1531 GEOSContextHandleInternal_t *handle = 0;
1532 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
1533 if ( 0 == handle->initialized )
1535 return -1;
1540 return g1->getGeometryTypeId();
1542 catch (const std::exception &e)
1544 handle->ERROR_MESSAGE("%s", e.what());
1546 catch (...)
1548 handle->ERROR_MESSAGE("Unknown exception thrown");
1551 return -1;
1554 //-------------------------------------------------------------------
1555 // GEOS functions that return geometries
1556 //-------------------------------------------------------------------
1558 Geometry *
1559 GEOSEnvelope_r(GEOSContextHandle_t extHandle, const Geometry *g1)
1561 if ( 0 == extHandle )
1563 return NULL;
1566 GEOSContextHandleInternal_t *handle = 0;
1567 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
1568 if ( 0 == handle->initialized )
1570 return NULL;
1575 Geometry *g3 = g1->getEnvelope();
1576 return g3;
1578 catch (const std::exception &e)
1580 handle->ERROR_MESSAGE("%s", e.what());
1582 catch (...)
1584 handle->ERROR_MESSAGE("Unknown exception thrown");
1587 return NULL;
1590 Geometry *
1591 GEOSIntersection_r(GEOSContextHandle_t extHandle, const Geometry *g1, const Geometry *g2)
1593 if ( 0 == extHandle )
1595 return NULL;
1598 GEOSContextHandleInternal_t *handle = 0;
1599 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
1600 if ( 0 == handle->initialized )
1602 return NULL;
1607 GeomAutoPtr g3(BinaryOp(g1, g2, overlayOp(OverlayOp::opINTERSECTION)));
1608 return g3.release();
1610 // XXX: old version
1611 //Geometry *g3 = g1->intersection(g2);
1612 //return g3;
1614 catch (const std::exception &e)
1616 handle->ERROR_MESSAGE("%s", e.what());
1618 catch (...)
1620 handle->ERROR_MESSAGE("Unknown exception thrown");
1623 return NULL;
1626 Geometry *
1627 GEOSBuffer_r(GEOSContextHandle_t extHandle, const Geometry *g1, double width, int quadrantsegments)
1629 if ( 0 == extHandle )
1631 return NULL;
1634 GEOSContextHandleInternal_t *handle = 0;
1635 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
1636 if ( 0 == handle->initialized )
1638 return NULL;
1643 Geometry *g3 = g1->buffer(width, quadrantsegments);
1644 return g3;
1646 catch (const std::exception &e)
1648 handle->ERROR_MESSAGE("%s", e.what());
1650 catch (...)
1652 handle->ERROR_MESSAGE("Unknown exception thrown");
1655 return NULL;
1658 Geometry *
1659 GEOSBufferWithStyle_r(GEOSContextHandle_t extHandle, const Geometry *g1, double width, int quadsegs, int endCapStyle, int joinStyle, double mitreLimit)
1661 using geos::operation::buffer::BufferParameters;
1662 using geos::operation::buffer::BufferOp;
1663 using geos::util::IllegalArgumentException;
1665 if ( 0 == extHandle )
1667 return NULL;
1670 GEOSContextHandleInternal_t *handle = 0;
1671 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
1672 if ( 0 == handle->initialized )
1674 return NULL;
1679 BufferParameters bp;
1680 bp.setQuadrantSegments(quadsegs);
1682 if ( endCapStyle > BufferParameters::CAP_SQUARE )
1684 throw IllegalArgumentException("Invalid buffer endCap style");
1686 bp.setEndCapStyle(
1687 static_cast<BufferParameters::EndCapStyle>(endCapStyle)
1690 if ( joinStyle > BufferParameters::JOIN_BEVEL )
1692 throw IllegalArgumentException("Invalid buffer join style");
1694 bp.setJoinStyle(
1695 static_cast<BufferParameters::JoinStyle>(joinStyle)
1697 bp.setMitreLimit(mitreLimit);
1698 BufferOp op(g1, bp);
1699 Geometry *g3 = op.getResultGeometry(width);
1700 return g3;
1702 catch (const std::exception &e)
1704 handle->ERROR_MESSAGE("%s", e.what());
1706 catch (...)
1708 handle->ERROR_MESSAGE("Unknown exception thrown");
1711 return NULL;
1714 Geometry *
1715 GEOSOffsetCurve_r(GEOSContextHandle_t extHandle, const Geometry *g1, double width, int quadsegs, int joinStyle, double mitreLimit)
1717 if ( 0 == extHandle ) return NULL;
1719 GEOSContextHandleInternal_t *handle = 0;
1720 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
1721 if ( 0 == handle->initialized ) return NULL;
1725 BufferParameters bp;
1726 bp.setEndCapStyle( BufferParameters::CAP_FLAT );
1727 bp.setQuadrantSegments(quadsegs);
1729 if ( joinStyle > BufferParameters::JOIN_BEVEL )
1731 throw IllegalArgumentException("Invalid buffer join style");
1733 bp.setJoinStyle(
1734 static_cast<BufferParameters::JoinStyle>(joinStyle)
1736 bp.setMitreLimit(mitreLimit);
1738 bool isLeftSide = true;
1739 if ( width < 0 ) {
1740 isLeftSide = false;
1741 width = -width;
1743 BufferBuilder bufBuilder (bp);
1744 Geometry *g3 = bufBuilder.bufferLineSingleSided(g1, width, isLeftSide);
1746 return g3;
1748 catch (const std::exception &e)
1750 handle->ERROR_MESSAGE("%s", e.what());
1752 catch (...)
1754 handle->ERROR_MESSAGE("Unknown exception thrown");
1757 return NULL;
1760 /* @deprecated in 3.3.0 */
1761 Geometry *
1762 GEOSSingleSidedBuffer_r(GEOSContextHandle_t extHandle, const Geometry *g1, double width, int quadsegs, int joinStyle, double mitreLimit, int leftSide)
1764 if ( 0 == extHandle ) return NULL;
1766 GEOSContextHandleInternal_t *handle = 0;
1767 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
1768 if ( 0 == handle->initialized ) return NULL;
1772 BufferParameters bp;
1773 bp.setEndCapStyle( BufferParameters::CAP_FLAT );
1774 bp.setQuadrantSegments(quadsegs);
1776 if ( joinStyle > BufferParameters::JOIN_BEVEL )
1778 throw IllegalArgumentException("Invalid buffer join style");
1780 bp.setJoinStyle(
1781 static_cast<BufferParameters::JoinStyle>(joinStyle)
1783 bp.setMitreLimit(mitreLimit);
1785 bool isLeftSide = leftSide == 0 ? false : true;
1786 BufferBuilder bufBuilder (bp);
1787 Geometry *g3 = bufBuilder.bufferLineSingleSided(g1, width, isLeftSide);
1789 return g3;
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 GEOSConvexHull_r(GEOSContextHandle_t extHandle, const Geometry *g1)
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->convexHull();
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 GEOSDifference_r(GEOSContextHandle_t extHandle, const Geometry *g1, const Geometry *g2)
1838 if ( 0 == extHandle )
1840 return NULL;
1843 GEOSContextHandleInternal_t *handle = 0;
1844 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
1845 if ( 0 == handle->initialized )
1847 return NULL;
1852 GeomAutoPtr g3(BinaryOp(g1, g2, overlayOp(OverlayOp::opDIFFERENCE)));
1853 return g3.release();
1855 // XXX: old version
1856 //Geometry *g3 = g1->difference(g2);
1857 //return g3;
1859 catch (const std::exception &e)
1861 handle->ERROR_MESSAGE("%s", e.what());
1863 catch (...)
1865 handle->ERROR_MESSAGE("Unknown exception thrown");
1868 return NULL;
1871 Geometry *
1872 GEOSBoundary_r(GEOSContextHandle_t extHandle, const Geometry *g1)
1874 if ( 0 == extHandle )
1876 return NULL;
1879 GEOSContextHandleInternal_t *handle = 0;
1880 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
1881 if ( 0 == handle->initialized )
1883 return NULL;
1888 Geometry *g3 = g1->getBoundary();
1889 return g3;
1891 catch (const std::exception &e)
1893 handle->ERROR_MESSAGE("%s", e.what());
1895 catch (...)
1897 handle->ERROR_MESSAGE("Unknown exception thrown");
1900 return NULL;
1903 Geometry *
1904 GEOSSymDifference_r(GEOSContextHandle_t extHandle, const Geometry *g1, const Geometry *g2)
1906 if ( 0 == extHandle )
1908 return NULL;
1911 GEOSContextHandleInternal_t *handle = 0;
1912 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
1913 if ( 0 == handle->initialized )
1915 return NULL;
1920 GeomAutoPtr g3 = BinaryOp(g1, g2, overlayOp(OverlayOp::opSYMDIFFERENCE));
1921 return g3.release();
1922 //Geometry *g3 = g1->symDifference(g2);
1923 //return g3;
1925 catch (const std::exception &e)
1927 handle->ERROR_MESSAGE("%s", e.what());
1928 return NULL;
1931 catch (...)
1933 handle->ERROR_MESSAGE("Unknown exception thrown");
1934 return NULL;
1938 Geometry *
1939 GEOSUnion_r(GEOSContextHandle_t extHandle, const Geometry *g1, const Geometry *g2)
1941 if ( 0 == extHandle )
1943 return NULL;
1946 GEOSContextHandleInternal_t *handle = 0;
1947 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
1948 if ( 0 == handle->initialized )
1950 return NULL;
1955 GeomAutoPtr g3 = BinaryOp(g1, g2, overlayOp(OverlayOp::opUNION));
1956 return g3.release();
1958 // XXX: old version
1959 //Geometry *g3 = g1->Union(g2);
1960 //return g3;
1962 catch (const std::exception &e)
1964 #if VERBOSE_EXCEPTIONS
1965 std::ostringstream s;
1966 s << "Exception on GEOSUnion with following inputs:" << std::endl;
1967 s << "A: "<<g1->toString() << std::endl;
1968 s << "B: "<<g2->toString() << std::endl;
1969 handle->NOTICE_MESSAGE("%s", s.str().c_str());
1970 #endif // VERBOSE_EXCEPTIONS
1971 handle->ERROR_MESSAGE("%s", e.what());
1973 catch (...)
1975 handle->ERROR_MESSAGE("Unknown exception thrown");
1978 return NULL;
1981 Geometry *
1982 GEOSUnaryUnion_r(GEOSContextHandle_t extHandle, const Geometry *g)
1984 if ( 0 == extHandle )
1986 return NULL;
1989 GEOSContextHandleInternal_t *handle = 0;
1990 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
1991 if ( 0 == handle->initialized )
1993 return NULL;
1998 GeomAutoPtr g3 ( g->Union() );
1999 return g3.release();
2001 catch (const std::exception &e)
2003 #if VERBOSE_EXCEPTIONS
2004 std::ostringstream s;
2005 s << "Exception on GEOSUnaryUnion with following inputs:" << std::endl;
2006 s << "A: "<<g1->toString() << std::endl;
2007 s << "B: "<<g2->toString() << std::endl;
2008 handle->NOTICE_MESSAGE("%s", s.str().c_str());
2009 #endif // VERBOSE_EXCEPTIONS
2010 handle->ERROR_MESSAGE("%s", e.what());
2012 catch (...)
2014 handle->ERROR_MESSAGE("Unknown exception thrown");
2017 return NULL;
2020 Geometry *
2021 GEOSUnionCascaded_r(GEOSContextHandle_t extHandle, const Geometry *g1)
2023 if ( 0 == extHandle )
2025 return NULL;
2028 GEOSContextHandleInternal_t *handle = 0;
2029 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
2030 if ( 0 == handle->initialized )
2032 return NULL;
2037 const geos::geom::MultiPolygon *p = dynamic_cast<const geos::geom::MultiPolygon *>(g1);
2038 if ( ! p )
2040 handle->ERROR_MESSAGE("Invalid argument (must be a MultiPolygon)");
2041 return NULL;
2044 using geos::operation::geounion::CascadedPolygonUnion;
2045 return CascadedPolygonUnion::Union(p);
2047 catch (const std::exception &e)
2049 handle->ERROR_MESSAGE("%s", e.what());
2051 catch (...)
2053 handle->ERROR_MESSAGE("Unknown exception thrown");
2056 return NULL;
2059 Geometry *
2060 GEOSPointOnSurface_r(GEOSContextHandle_t extHandle, const Geometry *g1)
2062 if ( 0 == extHandle )
2064 return NULL;
2067 GEOSContextHandleInternal_t *handle = 0;
2068 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
2069 if ( 0 == handle->initialized )
2071 return NULL;
2076 Geometry *ret = g1->getInteriorPoint();
2077 if ( ! ret )
2079 const GeometryFactory* gf = handle->geomFactory;
2080 // return an empty collection
2081 return gf->createGeometryCollection();
2083 return ret;
2085 catch (const std::exception &e)
2087 handle->ERROR_MESSAGE("%s", e.what());
2089 catch (...)
2091 handle->ERROR_MESSAGE("Unknown exception thrown");
2094 return NULL;
2097 //-------------------------------------------------------------------
2098 // memory management functions
2099 //------------------------------------------------------------------
2101 void
2102 GEOSGeom_destroy_r(GEOSContextHandle_t extHandle, Geometry *a)
2104 GEOSContextHandleInternal_t *handle = 0;
2106 // FIXME: mloskot: Does this try-catch around delete means that
2107 // destructors in GEOS may throw? If it does, this is a serious
2108 // violation of "never throw an exception from a destructor" principle
2112 delete a;
2114 catch (const std::exception &e)
2116 if ( 0 == extHandle )
2118 return;
2121 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
2122 if ( 0 == handle->initialized )
2124 return;
2127 handle->ERROR_MESSAGE("%s", e.what());
2129 catch (...)
2131 if ( 0 == extHandle )
2133 return;
2136 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
2137 if ( 0 == handle->initialized )
2139 return;
2142 handle->ERROR_MESSAGE("Unknown exception thrown");
2146 void
2147 GEOSSetSRID_r(GEOSContextHandle_t extHandle, Geometry *g, int srid)
2149 assert(0 != g);
2151 if ( 0 == extHandle )
2153 return;
2156 GEOSContextHandleInternal_t *handle = 0;
2157 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
2158 if ( 0 == handle->initialized )
2160 return;
2163 g->setSRID(srid);
2168 GEOSGetNumCoordinates_r(GEOSContextHandle_t extHandle, const Geometry *g)
2170 assert(0 != g);
2172 if ( 0 == extHandle )
2174 return -1;
2177 GEOSContextHandleInternal_t *handle = 0;
2178 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
2179 if ( 0 == handle->initialized )
2181 return -1;
2186 return static_cast<int>(g->getNumPoints());
2188 catch (const std::exception &e)
2190 handle->ERROR_MESSAGE("%s", e.what());
2192 catch (...)
2194 handle->ERROR_MESSAGE("Unknown exception thrown");
2197 return -1;
2201 * Return -1 on exception, 0 otherwise.
2202 * Converts Geometry to normal form (or canonical form).
2205 GEOSNormalize_r(GEOSContextHandle_t extHandle, Geometry *g)
2207 assert(0 != g);
2209 if ( 0 == extHandle )
2211 return -1;
2214 GEOSContextHandleInternal_t *handle = 0;
2215 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
2216 if ( 0 == handle->initialized )
2218 return -1;
2223 g->normalize();
2224 return 0; // SUCCESS
2226 catch (const std::exception &e)
2228 handle->ERROR_MESSAGE("%s", e.what());
2230 catch (...)
2232 handle->ERROR_MESSAGE("Unknown exception thrown");
2235 return -1;
2239 GEOSGetNumInteriorRings_r(GEOSContextHandle_t extHandle, const Geometry *g1)
2241 if ( 0 == extHandle )
2243 return -1;
2246 GEOSContextHandleInternal_t *handle = 0;
2247 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
2248 if ( 0 == handle->initialized )
2250 return -1;
2255 const Polygon *p = dynamic_cast<const Polygon *>(g1);
2256 if ( ! p )
2258 handle->ERROR_MESSAGE("Argument is not a Polygon");
2259 return -1;
2261 return static_cast<int>(p->getNumInteriorRing());
2263 catch (const std::exception &e)
2265 handle->ERROR_MESSAGE("%s", e.what());
2267 catch (...)
2269 handle->ERROR_MESSAGE("Unknown exception thrown");
2272 return -1;
2276 // returns -1 on error and 1 for non-multi geometries
2278 GEOSGetNumGeometries_r(GEOSContextHandle_t extHandle, const Geometry *g1)
2280 if ( 0 == extHandle )
2282 return -1;
2285 GEOSContextHandleInternal_t *handle = 0;
2286 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
2287 if ( 0 == handle->initialized )
2289 return -1;
2294 return static_cast<int>(g1->getNumGeometries());
2296 catch (const std::exception &e)
2298 handle->ERROR_MESSAGE("%s", e.what());
2300 catch (...)
2302 handle->ERROR_MESSAGE("Unknown exception thrown");
2305 return -1;
2310 * Call only on GEOMETRYCOLLECTION or MULTI*.
2311 * Return a pointer to the internal Geometry.
2313 const Geometry *
2314 GEOSGetGeometryN_r(GEOSContextHandle_t extHandle, const Geometry *g1, int n)
2316 if ( 0 == extHandle )
2318 return NULL;
2321 GEOSContextHandleInternal_t *handle = 0;
2322 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
2323 if ( 0 == handle->initialized )
2325 return NULL;
2330 return g1->getGeometryN(n);
2332 catch (const std::exception &e)
2334 handle->ERROR_MESSAGE("%s", e.what());
2336 catch (...)
2338 handle->ERROR_MESSAGE("Unknown exception thrown");
2341 return NULL;
2345 * Call only on LINESTRING
2346 * Returns NULL on exception
2348 Geometry *
2349 GEOSGeomGetPointN_r(GEOSContextHandle_t extHandle, const Geometry *g1, int n)
2351 if ( 0 == extHandle )
2353 return NULL;
2356 GEOSContextHandleInternal_t *handle = 0;
2357 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
2358 if ( 0 == handle->initialized )
2360 return NULL;
2365 using geos::geom::LineString;
2366 const LineString *ls = dynamic_cast<const LineString *>(g1);
2367 if ( ! ls )
2369 handle->ERROR_MESSAGE("Argument is not a LineString");
2370 return NULL;
2372 return ls->getPointN(n);
2374 catch (const std::exception &e)
2376 handle->ERROR_MESSAGE("%s", e.what());
2378 catch (...)
2380 handle->ERROR_MESSAGE("Unknown exception thrown");
2383 return NULL;
2387 * Call only on LINESTRING
2389 Geometry *
2390 GEOSGeomGetStartPoint_r(GEOSContextHandle_t extHandle, const Geometry *g1)
2392 if ( 0 == extHandle )
2394 return NULL;
2397 GEOSContextHandleInternal_t *handle = 0;
2398 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
2399 if ( 0 == handle->initialized )
2401 return NULL;
2406 using geos::geom::LineString;
2407 const LineString *ls = dynamic_cast<const LineString *>(g1);
2408 if ( ! ls )
2410 handle->ERROR_MESSAGE("Argument is not a LineString");
2411 return NULL;
2413 return ls->getStartPoint();
2415 catch (const std::exception &e)
2417 handle->ERROR_MESSAGE("%s", e.what());
2419 catch (...)
2421 handle->ERROR_MESSAGE("Unknown exception thrown");
2424 return NULL;
2428 * Call only on LINESTRING
2430 Geometry *
2431 GEOSGeomGetEndPoint_r(GEOSContextHandle_t extHandle, const Geometry *g1)
2433 if ( 0 == extHandle )
2435 return NULL;
2438 GEOSContextHandleInternal_t *handle = 0;
2439 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
2440 if ( 0 == handle->initialized )
2442 return NULL;
2447 using geos::geom::LineString;
2448 const LineString *ls = dynamic_cast<const LineString *>(g1);
2449 if ( ! ls )
2451 handle->ERROR_MESSAGE("Argument is not a LineString");
2452 return NULL;
2454 return ls->getEndPoint();
2456 catch (const std::exception &e)
2458 handle->ERROR_MESSAGE("%s", e.what());
2460 catch (...)
2462 handle->ERROR_MESSAGE("Unknown exception thrown");
2465 return NULL;
2469 * Call only on LINESTRING
2470 * return 2 on exception, 1 on true, 0 on false
2472 char
2473 GEOSisClosed_r(GEOSContextHandle_t extHandle, const Geometry *g1)
2475 if ( 0 == extHandle )
2477 return 2;
2480 GEOSContextHandleInternal_t *handle = 0;
2481 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
2482 if ( 0 == handle->initialized )
2484 return 2;
2489 using geos::geom::LineString;
2490 const LineString *ls = dynamic_cast<const LineString *>(g1);
2491 if ( ! ls )
2493 handle->ERROR_MESSAGE("Argument is not a LineString");
2494 return 2;
2496 return ls->isClosed();
2498 catch (const std::exception &e)
2500 handle->ERROR_MESSAGE("%s", e.what());
2502 catch (...)
2504 handle->ERROR_MESSAGE("Unknown exception thrown");
2507 return 2;
2511 * Call only on LINESTRING
2512 * return 0 on exception, otherwise 1
2515 GEOSGeomGetLength_r(GEOSContextHandle_t extHandle, const Geometry *g1, double *length)
2517 if ( 0 == extHandle )
2519 return 0;
2522 GEOSContextHandleInternal_t *handle = 0;
2523 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
2524 if ( 0 == handle->initialized )
2526 return 0;
2531 using geos::geom::LineString;
2532 const LineString *ls = dynamic_cast<const LineString *>(g1);
2533 if ( ! ls )
2535 handle->ERROR_MESSAGE("Argument is not a LineString");
2536 return 0;
2538 *length = ls->getLength();
2539 return 1;
2541 catch (const std::exception &e)
2543 handle->ERROR_MESSAGE("%s", e.what());
2545 catch (...)
2547 handle->ERROR_MESSAGE("Unknown exception thrown");
2550 return 0;
2554 * Call only on LINESTRING
2557 GEOSGeomGetNumPoints_r(GEOSContextHandle_t extHandle, const Geometry *g1)
2559 if ( 0 == extHandle )
2561 return -1;
2564 GEOSContextHandleInternal_t *handle = 0;
2565 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
2566 if ( 0 == handle->initialized )
2568 return -1;
2573 using geos::geom::LineString;
2574 const LineString *ls = dynamic_cast<const LineString *>(g1);
2575 if ( ! ls )
2577 handle->ERROR_MESSAGE("Argument is not a LineString");
2578 return -1;
2580 return static_cast<int>(ls->getNumPoints());
2582 catch (const std::exception &e)
2584 handle->ERROR_MESSAGE("%s", e.what());
2586 catch (...)
2588 handle->ERROR_MESSAGE("Unknown exception thrown");
2591 return -1;
2595 * For POINT
2596 * returns 0 on exception, otherwise 1
2599 GEOSGeomGetX_r(GEOSContextHandle_t extHandle, const Geometry *g1, double *x)
2601 if ( 0 == extHandle )
2603 return 0;
2606 GEOSContextHandleInternal_t *handle = 0;
2607 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
2608 if ( 0 == handle->initialized )
2610 return 0;
2615 using geos::geom::Point;
2616 const Point *po = dynamic_cast<const Point *>(g1);
2617 if ( ! po )
2619 handle->ERROR_MESSAGE("Argument is not a Point");
2620 return 0;
2622 *x = po->getX();
2623 return 1;
2625 catch (const std::exception &e)
2627 handle->ERROR_MESSAGE("%s", e.what());
2629 catch (...)
2631 handle->ERROR_MESSAGE("Unknown exception thrown");
2634 return 0;
2638 * For POINT
2639 * returns 0 on exception, otherwise 1
2642 GEOSGeomGetY_r(GEOSContextHandle_t extHandle, const Geometry *g1, double *y)
2644 if ( 0 == extHandle )
2646 return 0;
2649 GEOSContextHandleInternal_t *handle = 0;
2650 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
2651 if ( 0 == handle->initialized )
2653 return 0;
2658 using geos::geom::Point;
2659 const Point *po = dynamic_cast<const Point *>(g1);
2660 if ( ! po )
2662 handle->ERROR_MESSAGE("Argument is not a Point");
2663 return 0;
2665 *y = po->getY();
2666 return 1;
2668 catch (const std::exception &e)
2670 handle->ERROR_MESSAGE("%s", e.what());
2672 catch (...)
2674 handle->ERROR_MESSAGE("Unknown exception thrown");
2677 return 0;
2681 * Call only on polygon
2682 * Return a copy of the internal Geometry.
2684 const Geometry *
2685 GEOSGetExteriorRing_r(GEOSContextHandle_t extHandle, const Geometry *g1)
2687 if ( 0 == extHandle )
2689 return NULL;
2692 GEOSContextHandleInternal_t *handle = 0;
2693 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
2694 if ( 0 == handle->initialized )
2696 return NULL;
2701 const Polygon *p = dynamic_cast<const Polygon *>(g1);
2702 if ( ! p )
2704 handle->ERROR_MESSAGE("Invalid argument (must be a Polygon)");
2705 return NULL;
2707 return p->getExteriorRing();
2709 catch (const std::exception &e)
2711 handle->ERROR_MESSAGE("%s", e.what());
2713 catch (...)
2715 handle->ERROR_MESSAGE("Unknown exception thrown");
2718 return NULL;
2722 * Call only on polygon
2723 * Return a pointer to internal storage, do not destroy it.
2725 const Geometry *
2726 GEOSGetInteriorRingN_r(GEOSContextHandle_t extHandle, const Geometry *g1, int n)
2728 if ( 0 == extHandle )
2730 return NULL;
2733 GEOSContextHandleInternal_t *handle = 0;
2734 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
2735 if ( 0 == handle->initialized )
2737 return NULL;
2742 const Polygon *p = dynamic_cast<const Polygon *>(g1);
2743 if ( ! p )
2745 handle->ERROR_MESSAGE("Invalid argument (must be a Polygon)");
2746 return NULL;
2748 return p->getInteriorRingN(n);
2750 catch (const std::exception &e)
2752 handle->ERROR_MESSAGE("%s", e.what());
2754 catch (...)
2756 handle->ERROR_MESSAGE("Unknown exception thrown");
2759 return NULL;
2762 Geometry *
2763 GEOSGetCentroid_r(GEOSContextHandle_t extHandle, const Geometry *g)
2765 if ( 0 == extHandle )
2767 return NULL;
2770 GEOSContextHandleInternal_t *handle = 0;
2771 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
2772 if ( 0 == handle->initialized )
2774 return NULL;
2779 Geometry *ret = g->getCentroid();
2780 if (0 == ret)
2782 const GeometryFactory *gf = handle->geomFactory;
2783 return gf->createGeometryCollection();
2785 return ret;
2787 catch (const std::exception &e)
2789 handle->ERROR_MESSAGE("%s", e.what());
2791 catch (...)
2793 handle->ERROR_MESSAGE("Unknown exception thrown");
2796 return NULL;
2799 Geometry *
2800 GEOSGeom_createEmptyCollection_r(GEOSContextHandle_t extHandle, int type)
2802 if ( 0 == extHandle )
2804 return NULL;
2807 GEOSContextHandleInternal_t *handle = 0;
2808 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
2809 if ( 0 == handle->initialized )
2811 return NULL;
2814 #ifdef GEOS_DEBUG
2815 char buf[256];
2816 sprintf(buf, "createCollection: requested type %d, ngeoms: %d",
2817 type, ngeoms);
2818 handle->NOTICE_MESSAGE("%s", buf);// TODO: Can handle->NOTICE_MESSAGE format that directly?
2819 #endif
2823 const GeometryFactory* gf = handle->geomFactory;
2825 Geometry *g = 0;
2826 switch (type)
2828 case GEOS_GEOMETRYCOLLECTION:
2829 g = gf->createGeometryCollection();
2830 break;
2831 case GEOS_MULTIPOINT:
2832 g = gf->createMultiPoint();
2833 break;
2834 case GEOS_MULTILINESTRING:
2835 g = gf->createMultiLineString();
2836 break;
2837 case GEOS_MULTIPOLYGON:
2838 g = gf->createMultiPolygon();
2839 break;
2840 default:
2841 handle->ERROR_MESSAGE("Unsupported type request for GEOSGeom_createEmptyCollection_r");
2842 g = 0;
2846 return g;
2848 catch (const std::exception &e)
2850 handle->ERROR_MESSAGE("%s", e.what());
2852 catch (...)
2854 handle->ERROR_MESSAGE("Unknown exception thrown");
2857 return 0;
2860 Geometry *
2861 GEOSGeom_createCollection_r(GEOSContextHandle_t extHandle, int type, Geometry **geoms, unsigned int ngeoms)
2863 if ( 0 == extHandle )
2865 return NULL;
2868 GEOSContextHandleInternal_t *handle = 0;
2869 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
2870 if ( 0 == handle->initialized )
2872 return NULL;
2875 #ifdef GEOS_DEBUG
2876 char buf[256];
2877 sprintf(buf, "PostGIS2GEOS_collection: requested type %d, ngeoms: %d",
2878 type, ngeoms);
2879 handle->NOTICE_MESSAGE("%s", buf);// TODO: Can handle->NOTICE_MESSAGE format that directly?
2880 #endif
2884 const GeometryFactory* gf = handle->geomFactory;
2885 std::vector<Geometry*>* vgeoms = new std::vector<Geometry*>(geoms, geoms + ngeoms);
2887 Geometry *g = 0;
2888 switch (type)
2890 case GEOS_GEOMETRYCOLLECTION:
2891 g = gf->createGeometryCollection(vgeoms);
2892 break;
2893 case GEOS_MULTIPOINT:
2894 g = gf->createMultiPoint(vgeoms);
2895 break;
2896 case GEOS_MULTILINESTRING:
2897 g = gf->createMultiLineString(vgeoms);
2898 break;
2899 case GEOS_MULTIPOLYGON:
2900 g = gf->createMultiPolygon(vgeoms);
2901 break;
2902 default:
2903 handle->ERROR_MESSAGE("Unsupported type request for PostGIS2GEOS_collection");
2904 g = 0;
2908 return g;
2910 catch (const std::exception &e)
2912 handle->ERROR_MESSAGE("%s", e.what());
2914 catch (...)
2916 handle->ERROR_MESSAGE("Unknown exception thrown");
2919 return 0;
2922 Geometry *
2923 GEOSPolygonize_r(GEOSContextHandle_t extHandle, const Geometry * const * g, unsigned int ngeoms)
2925 if ( 0 == extHandle )
2927 return 0;
2930 GEOSContextHandleInternal_t *handle = 0;
2931 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
2932 if ( 0 == handle->initialized )
2934 return 0;
2937 Geometry *out = 0;
2941 // Polygonize
2942 using geos::operation::polygonize::Polygonizer;
2943 Polygonizer plgnzr;
2944 for (std::size_t i = 0; i < ngeoms; ++i)
2946 plgnzr.add(g[i]);
2949 #if GEOS_DEBUG
2950 handle->NOTICE_MESSAGE("geometry vector added to polygonizer");
2951 #endif
2953 std::vector<Polygon*> *polys = plgnzr.getPolygons();
2954 assert(0 != polys);
2956 #if GEOS_DEBUG
2957 handle->NOTICE_MESSAGE("output polygons got");
2958 #endif
2960 // We need a vector of Geometry pointers, not Polygon pointers.
2961 // STL vector doesn't allow transparent upcast of this
2962 // nature, so we explicitly convert.
2963 // (it's just a waste of processor and memory, btw)
2965 // XXX mloskot: Why not to extent GeometryFactory to accept
2966 // vector of polygons or extend Polygonizer to return list of Geometry*
2967 // or add a wrapper which semantic is similar to:
2968 // std::vector<as_polygon<Geometry*> >
2969 std::vector<Geometry*> *polyvec = new std::vector<Geometry *>(polys->size());
2971 for (std::size_t i = 0; i < polys->size(); ++i)
2973 (*polyvec)[i] = (*polys)[i];
2975 delete polys;
2976 polys = 0;
2978 const GeometryFactory *gf = handle->geomFactory;
2980 // The below takes ownership of the passed vector,
2981 // so we must *not* delete it
2982 out = gf->createGeometryCollection(polyvec);
2984 catch (const std::exception &e)
2986 handle->ERROR_MESSAGE("%s", e.what());
2988 catch (...)
2990 handle->ERROR_MESSAGE("Unknown exception thrown");
2993 return out;
2996 Geometry *
2997 GEOSPolygonizer_getCutEdges_r(GEOSContextHandle_t extHandle, const Geometry * const * g, unsigned int ngeoms)
2999 if ( 0 == extHandle )
3001 return 0;
3004 GEOSContextHandleInternal_t *handle = 0;
3005 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
3006 if ( 0 == handle->initialized )
3008 return 0;
3011 Geometry *out = 0;
3015 // Polygonize
3016 using geos::operation::polygonize::Polygonizer;
3017 Polygonizer plgnzr;
3018 for (std::size_t i = 0; i < ngeoms; ++i)
3020 plgnzr.add(g[i]);
3023 #if GEOS_DEBUG
3024 handle->NOTICE_MESSAGE("geometry vector added to polygonizer");
3025 #endif
3027 const std::vector<const LineString *>& lines = plgnzr.getCutEdges();
3029 #if GEOS_DEBUG
3030 handle->NOTICE_MESSAGE("output polygons got");
3031 #endif
3033 // We need a vector of Geometry pointers, not Polygon pointers.
3034 // STL vector doesn't allow transparent upcast of this
3035 // nature, so we explicitly convert.
3036 // (it's just a waste of processor and memory, btw)
3037 // XXX mloskot: See comment for GEOSPolygonize_r
3038 std::vector<Geometry*> *linevec = new std::vector<Geometry *>(lines.size());
3040 for (std::size_t i = 0, n=lines.size(); i < n; ++i)
3042 (*linevec)[i] = lines[i]->clone();
3045 const GeometryFactory *gf = handle->geomFactory;
3047 // The below takes ownership of the passed vector,
3048 // so we must *not* delete it
3049 out = gf->createGeometryCollection(linevec);
3051 catch (const std::exception &e)
3053 handle->ERROR_MESSAGE("%s", e.what());
3055 catch (...)
3057 handle->ERROR_MESSAGE("Unknown exception thrown");
3060 return out;
3063 Geometry *
3064 GEOSPolygonize_full_r(GEOSContextHandle_t extHandle, const Geometry* g,
3065 Geometry** cuts, Geometry** dangles, Geometry** invalid)
3067 if ( 0 == extHandle )
3069 return 0;
3072 GEOSContextHandleInternal_t *handle = 0;
3073 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
3074 if ( 0 == handle->initialized )
3076 return 0;
3081 // Polygonize
3082 using geos::operation::polygonize::Polygonizer;
3083 Polygonizer plgnzr;
3084 for (std::size_t i = 0; i <g->getNumGeometries(); ++i)
3086 plgnzr.add(g->getGeometryN(i));
3089 #if GEOS_DEBUG
3090 handle->NOTICE_MESSAGE("geometry vector added to polygonizer");
3091 #endif
3092 const GeometryFactory *gf = handle->geomFactory;
3094 if ( cuts ) {
3096 const std::vector<const LineString *>& lines = plgnzr.getCutEdges();
3097 std::vector<Geometry*> *linevec = new std::vector<Geometry *>(lines.size());
3098 for (std::size_t i = 0, n=lines.size(); i < n; ++i)
3100 (*linevec)[i] = lines[i]->clone();
3103 // The below takes ownership of the passed vector,
3104 // so we must *not* delete it
3105 *cuts = gf->createGeometryCollection(linevec);
3108 if ( dangles ) {
3110 const std::vector<const LineString *>& lines = plgnzr.getDangles();
3111 std::vector<Geometry*> *linevec = new std::vector<Geometry *>(lines.size());
3112 for (std::size_t i = 0, n=lines.size(); i < n; ++i)
3114 (*linevec)[i] = lines[i]->clone();
3117 // The below takes ownership of the passed vector,
3118 // so we must *not* delete it
3119 *dangles = gf->createGeometryCollection(linevec);
3122 if ( invalid ) {
3124 const std::vector<LineString *>& lines = plgnzr.getInvalidRingLines();
3125 std::vector<Geometry*> *linevec = new std::vector<Geometry *>(lines.size());
3126 for (std::size_t i = 0, n=lines.size(); i < n; ++i)
3128 (*linevec)[i] = lines[i]->clone();
3131 // The below takes ownership of the passed vector,
3132 // so we must *not* delete it
3133 *invalid = gf->createGeometryCollection(linevec);
3136 std::vector<Polygon*> *polys = plgnzr.getPolygons();
3137 std::vector<Geometry*> *polyvec = new std::vector<Geometry *>(polys->size());
3138 for (std::size_t i = 0; i < polys->size(); ++i)
3140 (*polyvec)[i] = (*polys)[i];
3142 delete polys;
3144 return gf->createGeometryCollection(polyvec);
3147 catch (const std::exception &e)
3149 handle->ERROR_MESSAGE("%s", e.what());
3150 return 0;
3152 catch (...)
3154 handle->ERROR_MESSAGE("Unknown exception thrown");
3155 return 0;
3159 Geometry *
3160 GEOSLineMerge_r(GEOSContextHandle_t extHandle, const Geometry *g)
3162 if ( 0 == extHandle )
3164 return 0;
3167 GEOSContextHandleInternal_t *handle = 0;
3168 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
3169 if ( 0 == handle->initialized )
3171 return 0;
3174 Geometry *out = 0;
3178 using geos::operation::linemerge::LineMerger;
3179 LineMerger lmrgr;
3180 lmrgr.add(g);
3182 std::vector<LineString *>* lines = lmrgr.getMergedLineStrings();
3183 assert(0 != lines);
3185 #if GEOS_DEBUG
3186 handle->NOTICE_MESSAGE("output lines got");
3187 #endif
3189 std::vector<Geometry *>*geoms = new std::vector<Geometry *>(lines->size());
3190 for (std::vector<Geometry *>::size_type i = 0; i < lines->size(); ++i)
3192 (*geoms)[i] = (*lines)[i];
3194 delete lines;
3195 lines = 0;
3197 const GeometryFactory *gf = handle->geomFactory;
3198 out = gf->buildGeometry(geoms);
3200 // XXX: old version
3201 //out = gf->createGeometryCollection(geoms);
3203 catch (const std::exception &e)
3205 handle->ERROR_MESSAGE("%s", e.what());
3207 catch (...)
3209 handle->ERROR_MESSAGE("Unknown exception thrown");
3212 return out;
3216 GEOSGetSRID_r(GEOSContextHandle_t extHandle, const Geometry *g)
3218 assert(0 != g);
3220 if ( 0 == extHandle )
3222 return 0;
3225 GEOSContextHandleInternal_t *handle = 0;
3226 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
3227 if ( 0 == handle->initialized )
3229 return 0;
3234 return g->getSRID();
3236 catch (const std::exception &e)
3238 handle->ERROR_MESSAGE("%s", e.what());
3240 catch (...)
3242 handle->ERROR_MESSAGE("Unknown exception thrown");
3245 return 0;
3248 const char* GEOSversion()
3250 return GEOS_CAPI_VERSION;
3253 const char* GEOSjtsport()
3255 return GEOS_JTS_PORT;
3258 char
3259 GEOSHasZ_r(GEOSContextHandle_t extHandle, const Geometry *g)
3261 assert(0 != g);
3263 if ( 0 == extHandle )
3265 return -1;
3268 GEOSContextHandleInternal_t *handle = 0;
3269 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
3270 if ( 0 == handle->initialized )
3272 return -1;
3275 if (g->isEmpty())
3277 return false;
3279 assert(0 != g->getCoordinate());
3281 double az = g->getCoordinate()->z;
3282 //handle->ERROR_MESSAGE("ZCoord: %g", az);
3284 return static_cast<char>(FINITE(az));
3288 GEOS_getWKBOutputDims_r(GEOSContextHandle_t extHandle)
3290 if ( 0 == extHandle )
3292 return -1;
3295 GEOSContextHandleInternal_t *handle = 0;
3296 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
3297 if ( 0 == handle->initialized )
3299 return -1;
3302 return handle->WKBOutputDims;
3306 GEOS_setWKBOutputDims_r(GEOSContextHandle_t extHandle, int newdims)
3308 if ( 0 == extHandle )
3310 return -1;
3313 GEOSContextHandleInternal_t *handle = 0;
3314 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
3315 if ( 0 == handle->initialized )
3317 return -1;
3320 if ( newdims < 2 || newdims > 3 )
3322 handle->ERROR_MESSAGE("WKB output dimensions out of range 2..3");
3325 const int olddims = handle->WKBOutputDims;
3326 handle->WKBOutputDims = newdims;
3328 return olddims;
3332 GEOS_getWKBByteOrder_r(GEOSContextHandle_t extHandle)
3334 if ( 0 == extHandle )
3336 return -1;
3339 GEOSContextHandleInternal_t *handle = 0;
3340 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
3341 if ( 0 == handle->initialized )
3343 return -1;
3346 return handle->WKBByteOrder;
3350 GEOS_setWKBByteOrder_r(GEOSContextHandle_t extHandle, int byteOrder)
3352 if ( 0 == extHandle )
3354 return -1;
3357 GEOSContextHandleInternal_t *handle = 0;
3358 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
3359 if ( 0 == handle->initialized )
3361 return -1;
3364 const int oldByteOrder = handle->WKBByteOrder;
3365 handle->WKBByteOrder = byteOrder;
3367 return oldByteOrder;
3371 CoordinateSequence *
3372 GEOSCoordSeq_create_r(GEOSContextHandle_t extHandle, unsigned int size, unsigned int dims)
3374 if ( 0 == extHandle )
3376 return NULL;
3379 GEOSContextHandleInternal_t *handle = 0;
3380 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
3381 if ( 0 == handle->initialized )
3383 return NULL;
3388 const GeometryFactory *gf = handle->geomFactory;
3389 return gf->getCoordinateSequenceFactory()->create(size, dims);
3391 catch (const std::exception &e)
3393 handle->ERROR_MESSAGE("%s", e.what());
3395 catch (...)
3397 handle->ERROR_MESSAGE("Unknown exception thrown");
3400 return NULL;
3404 GEOSCoordSeq_setOrdinate_r(GEOSContextHandle_t extHandle, CoordinateSequence *cs,
3405 unsigned int idx, unsigned int dim, double val)
3407 assert(0 != cs);
3408 if ( 0 == extHandle )
3410 return 0;
3413 GEOSContextHandleInternal_t *handle = 0;
3414 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
3415 if ( 0 == handle->initialized )
3417 return 0;
3422 cs->setOrdinate(idx, dim, val);
3423 return 1;
3425 catch (const std::exception &e)
3427 handle->ERROR_MESSAGE("%s", e.what());
3429 catch (...)
3431 handle->ERROR_MESSAGE("Unknown exception thrown");
3434 return 0;
3438 GEOSCoordSeq_setX_r(GEOSContextHandle_t extHandle, CoordinateSequence *s, unsigned int idx, double val)
3440 return GEOSCoordSeq_setOrdinate_r(extHandle, s, idx, 0, val);
3444 GEOSCoordSeq_setY_r(GEOSContextHandle_t extHandle, CoordinateSequence *s, unsigned int idx, double val)
3446 return GEOSCoordSeq_setOrdinate_r(extHandle, s, idx, 1, val);
3450 GEOSCoordSeq_setZ_r(GEOSContextHandle_t extHandle, CoordinateSequence *s, unsigned int idx, double val)
3452 return GEOSCoordSeq_setOrdinate_r(extHandle, s, idx, 2, val);
3455 CoordinateSequence *
3456 GEOSCoordSeq_clone_r(GEOSContextHandle_t extHandle, const CoordinateSequence *cs)
3458 assert(0 != cs);
3460 if ( 0 == extHandle )
3462 return NULL;
3465 GEOSContextHandleInternal_t *handle = 0;
3466 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
3467 if ( 0 == handle->initialized )
3469 return NULL;
3474 return cs->clone();
3476 catch (const std::exception &e)
3478 handle->ERROR_MESSAGE("%s", e.what());
3480 catch (...)
3482 handle->ERROR_MESSAGE("Unknown exception thrown");
3485 return NULL;
3489 GEOSCoordSeq_getOrdinate_r(GEOSContextHandle_t extHandle, const CoordinateSequence *cs,
3490 unsigned int idx, unsigned int dim, double *val)
3492 assert(0 != cs);
3493 assert(0 != val);
3495 if ( 0 == extHandle )
3497 return 0;
3500 GEOSContextHandleInternal_t *handle = 0;
3501 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
3502 if ( 0 == handle->initialized )
3504 return 0;
3509 double d = cs->getOrdinate(idx, dim);
3510 *val = d;
3512 return 1;
3514 catch (const std::exception &e)
3516 handle->ERROR_MESSAGE("%s", e.what());
3518 catch (...)
3520 handle->ERROR_MESSAGE("Unknown exception thrown");
3523 return 0;
3527 GEOSCoordSeq_getX_r(GEOSContextHandle_t extHandle, const CoordinateSequence *s, unsigned int idx, double *val)
3529 return GEOSCoordSeq_getOrdinate_r(extHandle, s, idx, 0, val);
3533 GEOSCoordSeq_getY_r(GEOSContextHandle_t extHandle, const CoordinateSequence *s, unsigned int idx, double *val)
3535 return GEOSCoordSeq_getOrdinate_r(extHandle, s, idx, 1, val);
3539 GEOSCoordSeq_getZ_r(GEOSContextHandle_t extHandle, const CoordinateSequence *s, unsigned int idx, double *val)
3541 return GEOSCoordSeq_getOrdinate_r(extHandle, s, idx, 2, val);
3545 GEOSCoordSeq_getSize_r(GEOSContextHandle_t extHandle, const CoordinateSequence *cs, unsigned int *size)
3547 assert(0 != cs);
3548 assert(0 != size);
3550 if ( 0 == extHandle )
3552 return 0;
3555 GEOSContextHandleInternal_t *handle = 0;
3556 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
3557 if ( 0 == handle->initialized )
3559 return 0;
3564 const std::size_t sz = cs->getSize();
3565 *size = static_cast<unsigned int>(sz);
3566 return 1;
3568 catch (const std::exception &e)
3570 handle->ERROR_MESSAGE("%s", e.what());
3572 catch (...)
3574 handle->ERROR_MESSAGE("Unknown exception thrown");
3577 return 0;
3581 GEOSCoordSeq_getDimensions_r(GEOSContextHandle_t extHandle, const CoordinateSequence *cs, unsigned int *dims)
3583 assert(0 != cs);
3584 assert(0 != dims);
3586 if ( 0 == extHandle )
3588 return 0;
3591 GEOSContextHandleInternal_t *handle = 0;
3592 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
3593 if ( 0 == handle->initialized )
3595 return 0;
3600 const std::size_t dim = cs->getDimension();
3601 *dims = static_cast<unsigned int>(dim);
3603 return 1;
3605 catch (const std::exception &e)
3607 handle->ERROR_MESSAGE("%s", e.what());
3610 catch (...)
3612 handle->ERROR_MESSAGE("Unknown exception thrown");
3615 return 0;
3618 void
3619 GEOSCoordSeq_destroy_r(GEOSContextHandle_t extHandle, CoordinateSequence *s)
3621 GEOSContextHandleInternal_t *handle = 0;
3625 delete s;
3627 catch (const std::exception &e)
3629 if ( 0 == extHandle )
3631 return;
3634 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
3635 if ( 0 == handle->initialized )
3637 return;
3640 handle->ERROR_MESSAGE("%s", e.what());
3642 catch (...)
3644 if ( 0 == extHandle )
3646 return;
3649 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
3650 if ( 0 == handle->initialized )
3652 return;
3655 handle->ERROR_MESSAGE("Unknown exception thrown");
3659 const CoordinateSequence *
3660 GEOSGeom_getCoordSeq_r(GEOSContextHandle_t extHandle, const Geometry *g)
3662 if ( 0 == extHandle )
3664 return 0;
3667 GEOSContextHandleInternal_t *handle = 0;
3668 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
3669 if ( 0 == handle->initialized )
3671 return 0;
3676 using geos::geom::Point;
3678 const LineString *ls = dynamic_cast<const LineString *>(g);
3679 if ( ls )
3681 return ls->getCoordinatesRO();
3684 const Point *p = dynamic_cast<const Point *>(g);
3685 if ( p )
3687 return p->getCoordinatesRO();
3690 handle->ERROR_MESSAGE("Geometry must be a Point or LineString");
3691 return 0;
3693 catch (const std::exception &e)
3695 handle->ERROR_MESSAGE("%s", e.what());
3697 catch (...)
3699 handle->ERROR_MESSAGE("Unknown exception thrown");
3702 return 0;
3705 Geometry *
3706 GEOSGeom_createEmptyPoint_r(GEOSContextHandle_t extHandle)
3708 if ( 0 == extHandle )
3710 return NULL;
3713 GEOSContextHandleInternal_t *handle = 0;
3714 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
3715 if ( 0 == handle->initialized )
3717 return NULL;
3722 const GeometryFactory *gf = handle->geomFactory;
3723 return gf->createPoint();
3725 catch (const std::exception &e)
3727 handle->ERROR_MESSAGE("%s", e.what());
3729 catch (...)
3731 handle->ERROR_MESSAGE("Unknown exception thrown");
3734 return NULL;
3737 Geometry *
3738 GEOSGeom_createPoint_r(GEOSContextHandle_t extHandle, CoordinateSequence *cs)
3740 if ( 0 == extHandle )
3742 return 0;
3745 GEOSContextHandleInternal_t *handle = 0;
3746 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
3747 if ( 0 == handle->initialized )
3749 return 0;
3754 const GeometryFactory *gf = handle->geomFactory;
3755 return gf->createPoint(cs);
3757 catch (const std::exception &e)
3759 handle->ERROR_MESSAGE("%s", e.what());
3761 catch (...)
3763 handle->ERROR_MESSAGE("Unknown exception thrown");
3766 return 0;
3769 Geometry *
3770 GEOSGeom_createLinearRing_r(GEOSContextHandle_t extHandle, CoordinateSequence *cs)
3772 if ( 0 == extHandle )
3774 return NULL;
3777 GEOSContextHandleInternal_t *handle = 0;
3778 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
3779 if ( 0 == handle->initialized )
3781 return NULL;
3786 const GeometryFactory *gf = handle->geomFactory;
3788 return gf->createLinearRing(cs);
3790 catch (const std::exception &e)
3792 handle->ERROR_MESSAGE("%s", e.what());
3794 catch (...)
3796 handle->ERROR_MESSAGE("Unknown exception thrown");
3799 return NULL;
3802 Geometry *
3803 GEOSGeom_createEmptyLineString_r(GEOSContextHandle_t extHandle)
3805 if ( 0 == extHandle )
3807 return NULL;
3810 GEOSContextHandleInternal_t *handle = 0;
3811 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
3812 if ( 0 == handle->initialized )
3814 return NULL;
3819 const GeometryFactory *gf = handle->geomFactory;
3821 return gf->createLineString();
3823 catch (const std::exception &e)
3825 handle->ERROR_MESSAGE("%s", e.what());
3827 catch (...)
3829 handle->ERROR_MESSAGE("Unknown exception thrown");
3832 return NULL;
3835 Geometry *
3836 GEOSGeom_createLineString_r(GEOSContextHandle_t extHandle, CoordinateSequence *cs)
3838 if ( 0 == extHandle )
3840 return NULL;
3843 GEOSContextHandleInternal_t *handle = 0;
3844 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
3845 if ( 0 == handle->initialized )
3847 return NULL;
3852 const GeometryFactory *gf = handle->geomFactory;
3854 return gf->createLineString(cs);
3856 catch (const std::exception &e)
3858 handle->ERROR_MESSAGE("%s", e.what());
3860 catch (...)
3862 handle->ERROR_MESSAGE("Unknown exception thrown");
3865 return NULL;
3868 Geometry *
3869 GEOSGeom_createEmptyPolygon_r(GEOSContextHandle_t extHandle)
3871 if ( 0 == extHandle )
3873 return NULL;
3876 GEOSContextHandleInternal_t *handle = 0;
3877 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
3878 if ( 0 == handle->initialized )
3880 return NULL;
3885 const GeometryFactory *gf = handle->geomFactory;
3886 return gf->createPolygon();
3888 catch (const std::exception &e)
3890 handle->ERROR_MESSAGE("%s", e.what());
3892 catch (...)
3894 handle->ERROR_MESSAGE("Unknown exception thrown");
3897 return NULL;
3900 Geometry *
3901 GEOSGeom_createPolygon_r(GEOSContextHandle_t extHandle, Geometry *shell, Geometry **holes, unsigned int nholes)
3903 // FIXME: holes must be non-nullptr or may be nullptr?
3904 //assert(0 != holes);
3906 if ( 0 == extHandle )
3908 return NULL;
3911 GEOSContextHandleInternal_t *handle = 0;
3912 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
3913 if ( 0 == handle->initialized )
3915 return NULL;
3920 using geos::geom::LinearRing;
3922 std::vector<Geometry *> *vholes = new std::vector<Geometry *>(holes, holes + nholes);
3924 LinearRing *nshell = dynamic_cast<LinearRing *>(shell);
3925 if ( ! nshell )
3927 handle->ERROR_MESSAGE("Shell is not a LinearRing");
3928 return NULL;
3930 const GeometryFactory *gf = handle->geomFactory;
3932 return gf->createPolygon(nshell, vholes);
3934 catch (const std::exception &e)
3936 handle->ERROR_MESSAGE("%s", e.what());
3938 catch (...)
3940 handle->ERROR_MESSAGE("Unknown exception thrown");
3943 return NULL;
3946 Geometry *
3947 GEOSGeom_clone_r(GEOSContextHandle_t extHandle, const Geometry *g)
3949 assert(0 != g);
3951 if ( 0 == extHandle )
3953 return NULL;
3956 GEOSContextHandleInternal_t *handle = 0;
3957 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
3958 if ( 0 == handle->initialized )
3960 return NULL;
3965 return g->clone();
3967 catch (const std::exception &e)
3969 handle->ERROR_MESSAGE("%s", e.what());
3971 catch (...)
3973 handle->ERROR_MESSAGE("Unknown exception thrown");
3976 return NULL;
3980 GEOSGeom_getDimensions_r(GEOSContextHandle_t extHandle, const Geometry *g)
3982 if ( 0 == extHandle )
3984 return 0;
3987 GEOSContextHandleInternal_t *handle = 0;
3988 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
3989 if ( 0 == handle->initialized )
3991 return 0;
3996 return (int) g->getDimension();
3998 catch (const std::exception &e)
4000 handle->ERROR_MESSAGE("%s", e.what());
4002 catch (...)
4004 handle->ERROR_MESSAGE("Unknown exception thrown");
4007 return 0;
4011 GEOSGeom_getCoordinateDimension_r(GEOSContextHandle_t extHandle, const Geometry *g)
4013 if ( 0 == extHandle )
4015 return 0;
4018 GEOSContextHandleInternal_t *handle = 0;
4019 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
4020 if ( 0 == handle->initialized )
4022 return 0;
4027 return g->getCoordinateDimension();
4029 catch (const std::exception &e)
4031 handle->ERROR_MESSAGE("%s", e.what());
4033 catch (...)
4035 handle->ERROR_MESSAGE("Unknown exception thrown");
4038 return 0;
4041 Geometry *
4042 GEOSSimplify_r(GEOSContextHandle_t extHandle, const Geometry *g1, double tolerance)
4044 if ( 0 == extHandle )
4046 return NULL;
4049 GEOSContextHandleInternal_t *handle = 0;
4050 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
4051 if ( 0 == handle->initialized )
4053 return NULL;
4058 using namespace geos::simplify;
4059 Geometry::AutoPtr g(DouglasPeuckerSimplifier::simplify(g1, tolerance));
4060 return g.release();
4062 catch (const std::exception &e)
4064 handle->ERROR_MESSAGE("%s", e.what());
4066 catch (...)
4068 handle->ERROR_MESSAGE("Unknown exception thrown");
4071 return NULL;
4074 Geometry *
4075 GEOSTopologyPreserveSimplify_r(GEOSContextHandle_t extHandle, const Geometry *g1, double tolerance)
4077 if ( 0 == extHandle )
4079 return NULL;
4082 GEOSContextHandleInternal_t *handle = 0;
4083 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
4084 if ( 0 == handle->initialized )
4086 return NULL;
4091 using namespace geos::simplify;
4092 Geometry::AutoPtr g(TopologyPreservingSimplifier::simplify(g1, tolerance));
4093 return g.release();
4095 catch (const std::exception &e)
4097 handle->ERROR_MESSAGE("%s", e.what());
4099 catch (...)
4101 handle->ERROR_MESSAGE("Unknown exception thrown");
4104 return NULL;
4108 /* WKT Reader */
4109 WKTReader *
4110 GEOSWKTReader_create_r(GEOSContextHandle_t extHandle)
4112 if ( 0 == extHandle )
4114 return NULL;
4117 GEOSContextHandleInternal_t *handle = 0;
4118 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
4119 if ( 0 == handle->initialized )
4121 return NULL;
4126 using geos::io::WKTReader;
4127 return new WKTReader((GeometryFactory*)handle->geomFactory);
4129 catch (const std::exception &e)
4131 handle->ERROR_MESSAGE("%s", e.what());
4133 catch (...)
4135 handle->ERROR_MESSAGE("Unknown exception thrown");
4138 return NULL;
4141 void
4142 GEOSWKTReader_destroy_r(GEOSContextHandle_t extHandle, WKTReader *reader)
4144 GEOSContextHandleInternal_t *handle = 0;
4148 delete reader;
4150 catch (const std::exception &e)
4152 if ( 0 == extHandle )
4154 return;
4157 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
4158 if ( 0 == handle->initialized )
4160 return;
4163 handle->ERROR_MESSAGE("%s", e.what());
4165 catch (...)
4167 if ( 0 == extHandle )
4169 return;
4172 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
4173 if ( 0 == handle->initialized )
4175 return;
4178 handle->ERROR_MESSAGE("Unknown exception thrown");
4183 Geometry*
4184 GEOSWKTReader_read_r(GEOSContextHandle_t extHandle, WKTReader *reader, const char *wkt)
4186 assert(0 != reader);
4188 if ( 0 == extHandle )
4190 return 0;
4193 GEOSContextHandleInternal_t *handle = 0;
4194 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
4195 if ( 0 == handle->initialized )
4197 return 0;
4202 const std::string wktstring(wkt);
4203 Geometry *g = reader->read(wktstring);
4204 return g;
4206 catch (const std::exception &e)
4208 handle->ERROR_MESSAGE("%s", e.what());
4210 catch (...)
4212 handle->ERROR_MESSAGE("Unknown exception thrown");
4215 return 0;
4218 /* WKT Writer */
4219 WKTWriter *
4220 GEOSWKTWriter_create_r(GEOSContextHandle_t extHandle)
4222 if ( 0 == extHandle )
4224 return 0;
4227 GEOSContextHandleInternal_t *handle = 0;
4228 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
4229 if ( 0 == handle->initialized )
4231 return 0;
4236 using geos::io::WKTWriter;
4237 return new WKTWriter();
4239 catch (const std::exception &e)
4241 handle->ERROR_MESSAGE("%s", e.what());
4243 catch (...)
4245 handle->ERROR_MESSAGE("Unknown exception thrown");
4248 return 0;
4251 void
4252 GEOSWKTWriter_destroy_r(GEOSContextHandle_t extHandle, WKTWriter *Writer)
4255 GEOSContextHandleInternal_t *handle = 0;
4259 delete Writer;
4261 catch (const std::exception &e)
4263 if ( 0 == extHandle )
4265 return;
4268 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
4269 if ( 0 == handle->initialized )
4271 return;
4274 handle->ERROR_MESSAGE("%s", e.what());
4276 catch (...)
4278 if ( 0 == extHandle )
4280 return;
4283 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
4284 if ( 0 == handle->initialized )
4286 return;
4289 handle->ERROR_MESSAGE("Unknown exception thrown");
4294 char*
4295 GEOSWKTWriter_write_r(GEOSContextHandle_t extHandle, WKTWriter *writer, const Geometry *geom)
4297 assert(0 != writer);
4299 if ( 0 == extHandle )
4301 return NULL;
4304 GEOSContextHandleInternal_t *handle = 0;
4305 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
4306 if ( 0 == handle->initialized )
4308 return NULL;
4313 std::string sgeom(writer->write(geom));
4314 char *result = gstrdup(sgeom);
4315 return result;
4317 catch (const std::exception &e)
4319 handle->ERROR_MESSAGE("%s", e.what());
4321 catch (...)
4323 handle->ERROR_MESSAGE("Unknown exception thrown");
4326 return NULL;
4329 void
4330 GEOSWKTWriter_setTrim_r(GEOSContextHandle_t extHandle, WKTWriter *writer, char trim)
4332 assert(0 != writer);
4334 if ( 0 == extHandle )
4336 return;
4339 GEOSContextHandleInternal_t *handle = 0;
4340 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
4341 if ( 0 == handle->initialized )
4343 return;
4346 writer->setTrim(0 != trim);
4349 void
4350 GEOSWKTWriter_setRoundingPrecision_r(GEOSContextHandle_t extHandle, WKTWriter *writer, int precision)
4352 assert(0 != writer);
4354 if ( 0 == extHandle )
4356 return;
4359 GEOSContextHandleInternal_t *handle = 0;
4360 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
4361 if ( 0 == handle->initialized )
4363 return;
4366 writer->setRoundingPrecision(precision);
4369 void
4370 GEOSWKTWriter_setOutputDimension_r(GEOSContextHandle_t extHandle, WKTWriter *writer, int dim)
4372 assert(0 != writer);
4374 if ( 0 == extHandle )
4376 return;
4379 GEOSContextHandleInternal_t *handle = 0;
4380 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
4381 if ( 0 == handle->initialized )
4383 return;
4388 writer->setOutputDimension(dim);
4390 catch (const std::exception &e)
4392 handle->ERROR_MESSAGE("%s", e.what());
4394 catch (...)
4396 handle->ERROR_MESSAGE("Unknown exception thrown");
4401 GEOSWKTWriter_getOutputDimension_r(GEOSContextHandle_t extHandle, WKTWriter *writer)
4403 assert(0 != writer);
4405 if ( 0 == extHandle )
4407 return -1;
4410 GEOSContextHandleInternal_t *handle = 0;
4411 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
4412 if ( 0 == handle->initialized )
4414 return -1;
4417 int dim = -1;
4421 dim = writer->getOutputDimension();
4423 catch (const std::exception &e)
4425 handle->ERROR_MESSAGE("%s", e.what());
4427 catch (...)
4429 handle->ERROR_MESSAGE("Unknown exception thrown");
4432 return dim;
4435 void
4436 GEOSWKTWriter_setOld3D_r(GEOSContextHandle_t extHandle, WKTWriter *writer, int useOld3D)
4438 assert(0 != writer);
4440 if ( 0 == extHandle )
4442 return;
4445 GEOSContextHandleInternal_t *handle = 0;
4446 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
4447 if ( 0 == handle->initialized )
4449 return;
4452 writer->setOld3D(0 != useOld3D);
4455 /* WKB Reader */
4456 WKBReader *
4457 GEOSWKBReader_create_r(GEOSContextHandle_t extHandle)
4459 if ( 0 == extHandle )
4461 return NULL;
4464 GEOSContextHandleInternal_t *handle = 0;
4465 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
4466 if ( 0 == handle->initialized )
4468 return NULL;
4471 using geos::io::WKBReader;
4474 return new WKBReader(*(GeometryFactory*)handle->geomFactory);
4476 catch (const std::exception &e)
4478 handle->ERROR_MESSAGE("%s", e.what());
4480 catch (...)
4482 handle->ERROR_MESSAGE("Unknown exception thrown");
4485 return NULL;
4488 void
4489 GEOSWKBReader_destroy_r(GEOSContextHandle_t extHandle, WKBReader *reader)
4491 GEOSContextHandleInternal_t *handle = 0;
4495 delete reader;
4497 catch (const std::exception &e)
4499 if ( 0 == extHandle )
4501 return;
4504 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
4505 if ( 0 == handle->initialized )
4507 return;
4510 handle->ERROR_MESSAGE("%s", e.what());
4512 catch (...)
4514 if ( 0 == extHandle )
4516 return;
4519 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
4520 if ( 0 == handle->initialized )
4522 return;
4525 handle->ERROR_MESSAGE("Unknown exception thrown");
4530 Geometry*
4531 GEOSWKBReader_read_r(GEOSContextHandle_t extHandle, WKBReader *reader, const unsigned char *wkb, size_t size)
4533 assert(0 != reader);
4534 assert(0 != wkb);
4536 if ( 0 == extHandle )
4538 return 0;
4541 GEOSContextHandleInternal_t *handle = 0;
4542 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
4543 if ( 0 == handle->initialized )
4545 return 0;
4550 std::string wkbstring(reinterpret_cast<const char*>(wkb), size); // make it binary !
4551 std::istringstream is(std::ios_base::binary);
4552 is.str(wkbstring);
4553 is.seekg(0, std::ios::beg); // rewind reader pointer
4555 Geometry *g = reader->read(is);
4556 return g;
4558 catch (const std::exception &e)
4560 handle->ERROR_MESSAGE("%s", e.what());
4562 catch (...)
4564 handle->ERROR_MESSAGE("Unknown exception thrown");
4567 return 0;
4570 Geometry*
4571 GEOSWKBReader_readHEX_r(GEOSContextHandle_t extHandle, WKBReader *reader, const unsigned char *hex, size_t size)
4573 assert(0 != reader);
4574 assert(0 != hex);
4576 if ( 0 == extHandle )
4578 return 0;
4581 GEOSContextHandleInternal_t *handle = 0;
4582 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
4583 if ( 0 == handle->initialized )
4585 return 0;
4590 std::string hexstring(reinterpret_cast<const char*>(hex), size);
4591 std::istringstream is(std::ios_base::binary);
4592 is.str(hexstring);
4593 is.seekg(0, std::ios::beg); // rewind reader pointer
4595 Geometry *g = reader->readHEX(is);
4596 return g;
4598 catch (const std::exception &e)
4600 handle->ERROR_MESSAGE("%s", e.what());
4602 catch (...)
4604 handle->ERROR_MESSAGE("Unknown exception thrown");
4607 return 0;
4610 /* WKB Writer */
4611 WKBWriter *
4612 GEOSWKBWriter_create_r(GEOSContextHandle_t extHandle)
4614 if ( 0 == extHandle )
4616 return NULL;
4619 GEOSContextHandleInternal_t *handle = 0;
4620 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
4621 if ( 0 == handle->initialized )
4623 return NULL;
4628 using geos::io::WKBWriter;
4629 return new WKBWriter();
4631 catch (const std::exception &e)
4633 handle->ERROR_MESSAGE("%s", e.what());
4635 catch (...)
4637 handle->ERROR_MESSAGE("Unknown exception thrown");
4640 return NULL;
4643 void
4644 GEOSWKBWriter_destroy_r(GEOSContextHandle_t extHandle, WKBWriter *Writer)
4646 GEOSContextHandleInternal_t *handle = 0;
4650 delete Writer;
4652 catch (const std::exception &e)
4654 if ( 0 == extHandle )
4656 return;
4659 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
4660 if ( 0 == handle->initialized )
4662 return;
4665 handle->ERROR_MESSAGE("%s", e.what());
4667 catch (...)
4669 if ( 0 == extHandle )
4671 return;
4674 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
4675 if ( 0 == handle->initialized )
4677 return;
4680 handle->ERROR_MESSAGE("Unknown exception thrown");
4685 /* The caller owns the result */
4686 unsigned char*
4687 GEOSWKBWriter_write_r(GEOSContextHandle_t extHandle, WKBWriter *writer, const Geometry *geom, size_t *size)
4689 assert(0 != writer);
4690 assert(0 != geom);
4691 assert(0 != size);
4693 if ( 0 == extHandle )
4695 return NULL;
4698 GEOSContextHandleInternal_t *handle = 0;
4699 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
4700 if ( 0 == handle->initialized )
4702 return NULL;
4707 std::ostringstream os(std::ios_base::binary);
4708 writer->write(*geom, os);
4709 std::string wkbstring(os.str());
4710 const std::size_t len = wkbstring.length();
4712 unsigned char *result = NULL;
4713 result = (unsigned char*) std::malloc(len);
4714 std::memcpy(result, wkbstring.c_str(), len);
4715 *size = len;
4716 return result;
4718 catch (const std::exception &e)
4720 handle->ERROR_MESSAGE("%s", e.what());
4722 catch (...)
4724 handle->ERROR_MESSAGE("Unknown exception thrown");
4726 return NULL;
4729 /* The caller owns the result */
4730 unsigned char*
4731 GEOSWKBWriter_writeHEX_r(GEOSContextHandle_t extHandle, WKBWriter *writer, const Geometry *geom, size_t *size)
4733 assert(0 != writer);
4734 assert(0 != geom);
4735 assert(0 != size);
4737 if ( 0 == extHandle )
4739 return NULL;
4742 GEOSContextHandleInternal_t *handle = 0;
4743 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
4744 if ( 0 == handle->initialized )
4746 return NULL;
4751 std::ostringstream os(std::ios_base::binary);
4752 writer->writeHEX(*geom, os);
4753 std::string wkbstring(os.str());
4754 const std::size_t len = wkbstring.length();
4756 unsigned char *result = NULL;
4757 result = (unsigned char*) std::malloc(len);
4758 std::memcpy(result, wkbstring.c_str(), len);
4759 *size = len;
4760 return result;
4762 catch (const std::exception &e)
4764 handle->ERROR_MESSAGE("%s", e.what());
4766 catch (...)
4768 handle->ERROR_MESSAGE("Unknown exception thrown");
4771 return NULL;
4775 GEOSWKBWriter_getOutputDimension_r(GEOSContextHandle_t extHandle, const GEOSWKBWriter* writer)
4777 assert(0 != writer);
4779 if ( 0 == extHandle )
4781 return 0;
4784 int ret = 0;
4786 GEOSContextHandleInternal_t *handle = 0;
4787 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
4788 if ( 0 != handle->initialized )
4792 ret = writer->getOutputDimension();
4794 catch (...)
4796 handle->ERROR_MESSAGE("Unknown exception thrown");
4800 return ret;
4803 void
4804 GEOSWKBWriter_setOutputDimension_r(GEOSContextHandle_t extHandle, GEOSWKBWriter* writer, int newDimension)
4806 assert(0 != writer);
4808 if ( 0 == extHandle )
4810 return;
4813 GEOSContextHandleInternal_t *handle = 0;
4814 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
4815 if ( 0 != handle->initialized )
4819 writer->setOutputDimension(newDimension);
4821 catch (const std::exception &e)
4823 handle->ERROR_MESSAGE("%s", e.what());
4825 catch (...)
4827 handle->ERROR_MESSAGE("Unknown exception thrown");
4833 GEOSWKBWriter_getByteOrder_r(GEOSContextHandle_t extHandle, const GEOSWKBWriter* writer)
4835 assert(0 != writer);
4837 if ( 0 == extHandle )
4839 return 0;
4842 int ret = 0;
4844 GEOSContextHandleInternal_t *handle = 0;
4845 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
4846 if ( 0 != handle->initialized )
4850 ret = writer->getByteOrder();
4853 catch (...)
4855 handle->ERROR_MESSAGE("Unknown exception thrown");
4859 return ret;
4862 void
4863 GEOSWKBWriter_setByteOrder_r(GEOSContextHandle_t extHandle, GEOSWKBWriter* writer, int newByteOrder)
4865 assert(0 != writer);
4867 if ( 0 == extHandle )
4869 return;
4872 GEOSContextHandleInternal_t *handle = 0;
4873 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
4874 if ( 0 != handle->initialized )
4878 writer->setByteOrder(newByteOrder);
4880 catch (const std::exception &e)
4882 handle->ERROR_MESSAGE("%s", e.what());
4884 catch (...)
4886 handle->ERROR_MESSAGE("Unknown exception thrown");
4891 char
4892 GEOSWKBWriter_getIncludeSRID_r(GEOSContextHandle_t extHandle, const GEOSWKBWriter* writer)
4894 assert(0 != writer);
4896 if ( 0 == extHandle )
4898 return -1;
4901 int ret = -1;
4903 GEOSContextHandleInternal_t *handle = 0;
4904 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
4905 if ( 0 != handle->initialized )
4909 int srid = writer->getIncludeSRID();
4910 ret = srid;
4912 catch (...)
4914 handle->ERROR_MESSAGE("Unknown exception thrown");
4918 return static_cast<char>(ret);
4921 void
4922 GEOSWKBWriter_setIncludeSRID_r(GEOSContextHandle_t extHandle, GEOSWKBWriter* writer, const char newIncludeSRID)
4924 assert(0 != writer);
4926 if ( 0 == extHandle )
4928 return;
4931 GEOSContextHandleInternal_t *handle = 0;
4932 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
4933 if ( 0 != handle->initialized )
4937 writer->setIncludeSRID(newIncludeSRID);
4939 catch (...)
4941 handle->ERROR_MESSAGE("Unknown exception thrown");
4947 //-----------------------------------------------------------------
4948 // Prepared Geometry
4949 //-----------------------------------------------------------------
4951 const geos::geom::prep::PreparedGeometry*
4952 GEOSPrepare_r(GEOSContextHandle_t extHandle, const Geometry *g)
4954 if ( 0 == extHandle )
4956 return 0;
4959 GEOSContextHandleInternal_t *handle = 0;
4960 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
4961 if ( 0 == handle->initialized )
4963 return 0;
4966 const geos::geom::prep::PreparedGeometry* prep = 0;
4970 prep = geos::geom::prep::PreparedGeometryFactory::prepare(g);
4972 catch (const std::exception &e)
4974 handle->ERROR_MESSAGE("%s", e.what());
4976 catch (...)
4978 handle->ERROR_MESSAGE("Unknown exception thrown");
4981 return prep;
4984 void
4985 GEOSPreparedGeom_destroy_r(GEOSContextHandle_t extHandle, const geos::geom::prep::PreparedGeometry *a)
4987 GEOSContextHandleInternal_t *handle = 0;
4991 delete a;
4993 catch (const std::exception &e)
4995 if ( 0 == extHandle )
4997 return;
5000 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
5001 if ( 0 == handle->initialized )
5003 return;
5006 handle->ERROR_MESSAGE("%s", e.what());
5008 catch (...)
5010 if ( 0 == extHandle )
5012 return;
5015 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
5016 if ( 0 == handle->initialized )
5018 return;
5021 handle->ERROR_MESSAGE("Unknown exception thrown");
5025 char
5026 GEOSPreparedContains_r(GEOSContextHandle_t extHandle,
5027 const geos::geom::prep::PreparedGeometry *pg, const Geometry *g)
5029 assert(0 != pg);
5030 assert(0 != g);
5032 if ( 0 == extHandle )
5034 return 2;
5037 GEOSContextHandleInternal_t *handle = 0;
5038 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
5039 if ( 0 == handle->initialized )
5041 return 2;
5044 try
5046 bool result = pg->contains(g);
5047 return result;
5049 catch (const std::exception &e)
5051 handle->ERROR_MESSAGE("%s", e.what());
5053 catch (...)
5055 handle->ERROR_MESSAGE("Unknown exception thrown");
5058 return 2;
5061 char
5062 GEOSPreparedContainsProperly_r(GEOSContextHandle_t extHandle,
5063 const geos::geom::prep::PreparedGeometry *pg, const Geometry *g)
5065 assert(0 != pg);
5066 assert(0 != g);
5068 if ( 0 == extHandle )
5070 return 2;
5073 GEOSContextHandleInternal_t *handle = 0;
5074 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
5075 if ( 0 == handle->initialized )
5077 return 2;
5080 try
5082 bool result = pg->containsProperly(g);
5083 return result;
5085 catch (const std::exception &e)
5087 handle->ERROR_MESSAGE("%s", e.what());
5089 catch (...)
5091 handle->ERROR_MESSAGE("Unknown exception thrown");
5094 return 2;
5097 char
5098 GEOSPreparedCoveredBy_r(GEOSContextHandle_t extHandle,
5099 const geos::geom::prep::PreparedGeometry *pg, const Geometry *g)
5101 assert(0 != pg);
5102 assert(0 != g);
5104 if ( 0 == extHandle )
5106 return 2;
5109 GEOSContextHandleInternal_t *handle = 0;
5110 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
5111 if ( 0 == handle->initialized )
5113 return 2;
5116 try
5118 bool result = pg->coveredBy(g);
5119 return result;
5121 catch (const std::exception &e)
5123 handle->ERROR_MESSAGE("%s", e.what());
5125 catch (...)
5127 handle->ERROR_MESSAGE("Unknown exception thrown");
5130 return 2;
5133 char
5134 GEOSPreparedCovers_r(GEOSContextHandle_t extHandle,
5135 const geos::geom::prep::PreparedGeometry *pg, const Geometry *g)
5137 assert(0 != pg);
5138 assert(0 != g);
5140 if ( 0 == extHandle )
5142 return 2;
5145 GEOSContextHandleInternal_t *handle = 0;
5146 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
5147 if ( 0 == handle->initialized )
5149 return 2;
5152 try
5154 bool result = pg->covers(g);
5155 return result;
5157 catch (const std::exception &e)
5159 handle->ERROR_MESSAGE("%s", e.what());
5161 catch (...)
5163 handle->ERROR_MESSAGE("Unknown exception thrown");
5166 return 2;
5169 char
5170 GEOSPreparedCrosses_r(GEOSContextHandle_t extHandle,
5171 const geos::geom::prep::PreparedGeometry *pg, const Geometry *g)
5173 assert(0 != pg);
5174 assert(0 != g);
5176 if ( 0 == extHandle )
5178 return 2;
5181 GEOSContextHandleInternal_t *handle = 0;
5182 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
5183 if ( 0 == handle->initialized )
5185 return 2;
5188 try
5190 bool result = pg->crosses(g);
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 2;
5205 char
5206 GEOSPreparedDisjoint_r(GEOSContextHandle_t extHandle,
5207 const geos::geom::prep::PreparedGeometry *pg, const Geometry *g)
5209 assert(0 != pg);
5210 assert(0 != g);
5212 if ( 0 == extHandle )
5214 return 2;
5217 GEOSContextHandleInternal_t *handle = 0;
5218 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
5219 if ( 0 == handle->initialized )
5221 return 2;
5224 try
5226 bool result = pg->disjoint(g);
5227 return result;
5229 catch (const std::exception &e)
5231 handle->ERROR_MESSAGE("%s", e.what());
5233 catch (...)
5235 handle->ERROR_MESSAGE("Unknown exception thrown");
5238 return 2;
5241 char
5242 GEOSPreparedIntersects_r(GEOSContextHandle_t extHandle,
5243 const geos::geom::prep::PreparedGeometry *pg, const Geometry *g)
5245 assert(0 != pg);
5246 assert(0 != g);
5248 if ( 0 == extHandle )
5250 return 2;
5253 GEOSContextHandleInternal_t *handle = 0;
5254 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
5255 if ( 0 == handle->initialized )
5257 return 2;
5260 try
5262 bool result = pg->intersects(g);
5263 return result;
5265 catch (const std::exception &e)
5267 handle->ERROR_MESSAGE("%s", e.what());
5269 catch (...)
5271 handle->ERROR_MESSAGE("Unknown exception thrown");
5274 return 2;
5277 char
5278 GEOSPreparedOverlaps_r(GEOSContextHandle_t extHandle,
5279 const geos::geom::prep::PreparedGeometry *pg, const Geometry *g)
5281 assert(0 != pg);
5282 assert(0 != g);
5284 if ( 0 == extHandle )
5286 return 2;
5289 GEOSContextHandleInternal_t *handle = 0;
5290 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
5291 if ( 0 == handle->initialized )
5293 return 2;
5296 try
5298 bool result = pg->overlaps(g);
5299 return result;
5301 catch (const std::exception &e)
5303 handle->ERROR_MESSAGE("%s", e.what());
5305 catch (...)
5307 handle->ERROR_MESSAGE("Unknown exception thrown");
5310 return 2;
5313 char
5314 GEOSPreparedTouches_r(GEOSContextHandle_t extHandle,
5315 const geos::geom::prep::PreparedGeometry *pg, const Geometry *g)
5317 assert(0 != pg);
5318 assert(0 != g);
5320 if ( 0 == extHandle )
5322 return 2;
5325 GEOSContextHandleInternal_t *handle = 0;
5326 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
5327 if ( 0 == handle->initialized )
5329 return 2;
5332 try
5334 bool result = pg->touches(g);
5335 return result;
5337 catch (const std::exception &e)
5339 handle->ERROR_MESSAGE("%s", e.what());
5341 catch (...)
5343 handle->ERROR_MESSAGE("Unknown exception thrown");
5346 return 2;
5349 char
5350 GEOSPreparedWithin_r(GEOSContextHandle_t extHandle,
5351 const geos::geom::prep::PreparedGeometry *pg, const Geometry *g)
5353 assert(0 != pg);
5354 assert(0 != g);
5356 if ( 0 == extHandle )
5358 return 2;
5361 GEOSContextHandleInternal_t *handle = 0;
5362 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
5363 if ( 0 == handle->initialized )
5365 return 2;
5368 try
5370 bool result = pg->within(g);
5371 return result;
5373 catch (const std::exception &e)
5375 handle->ERROR_MESSAGE("%s", e.what());
5377 catch (...)
5379 handle->ERROR_MESSAGE("Unknown exception thrown");
5382 return 2;
5385 //-----------------------------------------------------------------
5386 // STRtree
5387 //-----------------------------------------------------------------
5389 geos::index::strtree::STRtree *
5390 GEOSSTRtree_create_r(GEOSContextHandle_t extHandle,
5391 size_t nodeCapacity)
5393 if ( 0 == extHandle )
5395 return 0;
5398 GEOSContextHandleInternal_t *handle = 0;
5399 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
5400 if ( 0 == handle->initialized )
5402 return 0;
5405 geos::index::strtree::STRtree *tree = 0;
5409 tree = new geos::index::strtree::STRtree(nodeCapacity);
5411 catch (const std::exception &e)
5413 handle->ERROR_MESSAGE("%s", e.what());
5415 catch (...)
5417 handle->ERROR_MESSAGE("Unknown exception thrown");
5420 return tree;
5423 void
5424 GEOSSTRtree_insert_r(GEOSContextHandle_t extHandle,
5425 geos::index::strtree::STRtree *tree,
5426 const geos::geom::Geometry *g,
5427 void *item)
5429 GEOSContextHandleInternal_t *handle = 0;
5430 assert(tree != 0);
5431 assert(g != 0);
5435 tree->insert(g->getEnvelopeInternal(), item);
5437 catch (const std::exception &e)
5439 if ( 0 == extHandle )
5441 return;
5444 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
5445 if ( 0 == handle->initialized )
5447 return;
5450 handle->ERROR_MESSAGE("%s", e.what());
5452 catch (...)
5454 if ( 0 == extHandle )
5456 return;
5459 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
5460 if ( 0 == handle->initialized )
5462 return;
5465 handle->ERROR_MESSAGE("Unknown exception thrown");
5469 void
5470 GEOSSTRtree_query_r(GEOSContextHandle_t extHandle,
5471 geos::index::strtree::STRtree *tree,
5472 const geos::geom::Geometry *g,
5473 GEOSQueryCallback callback,
5474 void *userdata)
5476 GEOSContextHandleInternal_t *handle = 0;
5477 assert(tree != 0);
5478 assert(g != 0);
5479 assert(callback != 0);
5483 CAPI_ItemVisitor visitor(callback, userdata);
5484 tree->query(g->getEnvelopeInternal(), visitor);
5486 catch (const std::exception &e)
5488 if ( 0 == extHandle )
5490 return;
5493 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
5494 if ( 0 == handle->initialized )
5496 return;
5499 handle->ERROR_MESSAGE("%s", e.what());
5501 catch (...)
5503 if ( 0 == extHandle )
5505 return;
5508 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
5509 if ( 0 == handle->initialized )
5511 return;
5514 handle->ERROR_MESSAGE("Unknown exception thrown");
5518 void
5519 GEOSSTRtree_iterate_r(GEOSContextHandle_t extHandle,
5520 geos::index::strtree::STRtree *tree,
5521 GEOSQueryCallback callback,
5522 void *userdata)
5524 GEOSContextHandleInternal_t *handle = 0;
5525 assert(tree != 0);
5526 assert(callback != 0);
5530 CAPI_ItemVisitor visitor(callback, userdata);
5531 tree->iterate(visitor);
5533 catch (const std::exception &e)
5535 if ( 0 == extHandle )
5537 return;
5540 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
5541 if ( 0 == handle->initialized )
5543 return;
5546 handle->ERROR_MESSAGE("%s", e.what());
5548 catch (...)
5550 if ( 0 == extHandle )
5552 return;
5555 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
5556 if ( 0 == handle->initialized )
5558 return;
5561 handle->ERROR_MESSAGE("Unknown exception thrown");
5565 char
5566 GEOSSTRtree_remove_r(GEOSContextHandle_t extHandle,
5567 geos::index::strtree::STRtree *tree,
5568 const geos::geom::Geometry *g,
5569 void *item)
5571 assert(0 != tree);
5572 assert(0 != g);
5574 if ( 0 == extHandle )
5576 return 2;
5579 GEOSContextHandleInternal_t *handle = 0;
5580 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
5581 if ( 0 == handle->initialized )
5583 return 2;
5586 try
5588 bool result = tree->remove(g->getEnvelopeInternal(), item);
5589 return result;
5591 catch (const std::exception &e)
5593 handle->ERROR_MESSAGE("%s", e.what());
5595 catch (...)
5597 handle->ERROR_MESSAGE("Unknown exception thrown");
5600 return 2;
5603 void
5604 GEOSSTRtree_destroy_r(GEOSContextHandle_t extHandle,
5605 geos::index::strtree::STRtree *tree)
5607 GEOSContextHandleInternal_t *handle = 0;
5611 delete tree;
5613 catch (const std::exception &e)
5615 if ( 0 == extHandle )
5617 return;
5620 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
5621 if ( 0 == handle->initialized )
5623 return;
5626 handle->ERROR_MESSAGE("%s", e.what());
5628 catch (...)
5630 if ( 0 == extHandle )
5632 return;
5635 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
5636 if ( 0 == handle->initialized )
5638 return;
5641 handle->ERROR_MESSAGE("Unknown exception thrown");
5645 double
5646 GEOSProject_r(GEOSContextHandle_t extHandle,
5647 const Geometry *g,
5648 const Geometry *p)
5650 if ( 0 == extHandle ) return -1.0;
5651 GEOSContextHandleInternal_t *handle =
5652 reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
5653 if ( handle->initialized == 0 ) return -1.0;
5655 const geos::geom::Point* point = dynamic_cast<const geos::geom::Point*>(p);
5656 if (!point) {
5657 handle->ERROR_MESSAGE("third argument of GEOSProject_r must be Point*");
5658 return -1.0;
5661 const geos::geom::Coordinate* inputPt = p->getCoordinate();
5663 try {
5664 return geos::linearref::LengthIndexedLine(g).project(*inputPt);
5665 } catch (const std::exception &e) {
5666 handle->ERROR_MESSAGE("%s", e.what());
5667 return -1.0;
5668 } catch (...) {
5669 handle->ERROR_MESSAGE("Unknown exception thrown");
5670 return -1.0;
5675 Geometry*
5676 GEOSInterpolate_r(GEOSContextHandle_t extHandle, const Geometry *g, double d)
5678 if ( 0 == extHandle ) return 0;
5679 GEOSContextHandleInternal_t *handle =
5680 reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
5681 if ( handle->initialized == 0 ) return 0;
5683 try {
5684 geos::linearref::LengthIndexedLine lil(g);
5685 geos::geom::Coordinate coord = lil.extractPoint(d);
5686 const GeometryFactory *gf = handle->geomFactory;
5687 Geometry* point = gf->createPoint(coord);
5688 return point;
5689 } catch (const std::exception &e) {
5690 handle->ERROR_MESSAGE("%s", e.what());
5691 return 0;
5692 } catch (...) {
5693 handle->ERROR_MESSAGE("Unknown exception thrown");
5694 return 0;
5699 double
5700 GEOSProjectNormalized_r(GEOSContextHandle_t extHandle, const Geometry *g,
5701 const Geometry *p)
5704 double length;
5705 GEOSLength_r(extHandle, g, &length);
5706 return GEOSProject_r(extHandle, g, p) / length;
5710 Geometry*
5711 GEOSInterpolateNormalized_r(GEOSContextHandle_t extHandle, const Geometry *g,
5712 double d)
5714 double length;
5715 GEOSLength_r(extHandle, g, &length);
5716 return GEOSInterpolate_r(extHandle, g, d * length);
5719 GEOSGeometry*
5720 GEOSGeom_extractUniquePoints_r(GEOSContextHandle_t extHandle,
5721 const GEOSGeometry* g)
5723 if ( 0 == extHandle ) return 0;
5724 GEOSContextHandleInternal_t *handle = 0;
5725 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
5726 if ( handle->initialized == 0 ) return 0;
5728 using namespace geos::geom;
5729 using namespace geos::util;
5734 /* 1: extract points */
5735 std::vector<const Coordinate*> coords;
5736 UniqueCoordinateArrayFilter filter(coords);
5737 g->apply_ro(&filter);
5739 /* 2: for each point, create a geometry and put into a vector */
5740 std::vector<Geometry*>* points = new std::vector<Geometry*>();
5741 points->reserve(coords.size());
5742 const GeometryFactory* factory = g->getFactory();
5743 for (std::vector<const Coordinate*>::iterator it=coords.begin(),
5744 itE=coords.end();
5745 it != itE; ++it)
5747 Geometry* point = factory->createPoint(*(*it));
5748 points->push_back(point);
5751 /* 3: create a multipoint */
5752 return factory->createMultiPoint(points);
5755 catch (const std::exception &e)
5757 handle->ERROR_MESSAGE("%s", e.what());
5758 return 0;
5760 catch (...)
5762 handle->ERROR_MESSAGE("Unknown exception thrown");
5763 return 0;
5767 int GEOSOrientationIndex_r(GEOSContextHandle_t extHandle,
5768 double Ax, double Ay, double Bx, double By, double Px, double Py)
5770 GEOSContextHandleInternal_t *handle = 0;
5772 using geos::geom::Coordinate;
5773 using geos::algorithm::CGAlgorithms;
5775 if ( 0 == extHandle )
5777 return 2;
5780 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
5781 if ( 0 == handle->initialized )
5783 return 2;
5788 Coordinate A(Ax, Ay);
5789 Coordinate B(Bx, By);
5790 Coordinate P(Px, Py);
5791 return CGAlgorithms::orientationIndex(A, B, P);
5793 catch (const std::exception &e)
5795 handle->ERROR_MESSAGE("%s", e.what());
5796 return 2;
5798 catch (...)
5800 handle->ERROR_MESSAGE("Unknown exception thrown");
5801 return 2;
5805 GEOSGeometry *
5806 GEOSSharedPaths_r(GEOSContextHandle_t extHandle, const GEOSGeometry* g1, const GEOSGeometry* g2)
5808 using namespace geos::operation::sharedpaths;
5810 if ( 0 == extHandle ) return 0;
5811 GEOSContextHandleInternal_t *handle =
5812 reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
5813 if ( handle->initialized == 0 ) return 0;
5815 SharedPathsOp::PathList forw, back;
5816 try {
5817 SharedPathsOp::sharedPathsOp(*g1, *g2, forw, back);
5819 catch (const std::exception &e)
5821 SharedPathsOp::clearEdges(forw);
5822 SharedPathsOp::clearEdges(back);
5823 handle->ERROR_MESSAGE("%s", e.what());
5824 return 0;
5826 catch (...)
5828 SharedPathsOp::clearEdges(forw);
5829 SharedPathsOp::clearEdges(back);
5830 handle->ERROR_MESSAGE("Unknown exception thrown");
5831 return 0;
5834 // Now forw and back have the geoms we want to use to construct
5835 // our output GeometryCollections...
5837 const GeometryFactory* factory = g1->getFactory();
5838 size_t count;
5840 std::auto_ptr< std::vector<Geometry*> > out1(
5841 new std::vector<Geometry*>()
5843 count = forw.size();
5844 out1->reserve(count);
5845 for (size_t i=0; i<count; ++i) {
5846 out1->push_back(forw[i]);
5848 std::auto_ptr<Geometry> out1g (
5849 factory->createMultiLineString(out1.release())
5852 std::auto_ptr< std::vector<Geometry*> > out2(
5853 new std::vector<Geometry*>()
5855 count = back.size();
5856 out2->reserve(count);
5857 for (size_t i=0; i<count; ++i) {
5858 out2->push_back(back[i]);
5860 std::auto_ptr<Geometry> out2g (
5861 factory->createMultiLineString(out2.release())
5864 std::auto_ptr< std::vector<Geometry*> > out(
5865 new std::vector<Geometry*>()
5867 out->reserve(2);
5868 out->push_back(out1g.release());
5869 out->push_back(out2g.release());
5871 std::auto_ptr<Geometry> outg (
5872 factory->createGeometryCollection(out.release())
5875 return outg.release();
5879 GEOSGeometry *
5880 GEOSSnap_r(GEOSContextHandle_t extHandle, const GEOSGeometry* g1,
5881 const GEOSGeometry* g2, double tolerance)
5883 using namespace geos::operation::overlay::snap;
5885 if ( 0 == extHandle ) return 0;
5886 GEOSContextHandleInternal_t *handle =
5887 reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
5888 if ( handle->initialized == 0 ) return 0;
5890 try{
5891 GeometrySnapper snapper( *g1 );
5892 std::auto_ptr<Geometry> ret = snapper.snapTo(*g2, tolerance);
5893 return ret.release();
5895 catch (const std::exception &e)
5897 handle->ERROR_MESSAGE("%s", e.what());
5898 return 0;
5900 catch (...)
5902 handle->ERROR_MESSAGE("Unknown exception thrown");
5903 return 0;
5907 BufferParameters *
5908 GEOSBufferParams_create_r(GEOSContextHandle_t extHandle)
5910 if ( 0 == extHandle ) return NULL;
5912 GEOSContextHandleInternal_t *handle = 0;
5913 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
5914 if ( 0 == handle->initialized ) return NULL;
5918 BufferParameters *p = new BufferParameters();
5919 return p;
5921 catch (const std::exception &e)
5923 handle->ERROR_MESSAGE("%s", e.what());
5925 catch (...)
5927 handle->ERROR_MESSAGE("Unknown exception thrown");
5930 return 0;
5933 void
5934 GEOSBufferParams_destroy_r(GEOSContextHandle_t extHandle, BufferParameters* p)
5936 (void)extHandle;
5937 delete p;
5941 GEOSBufferParams_setEndCapStyle_r(GEOSContextHandle_t extHandle,
5942 GEOSBufferParams* p, int style)
5944 if ( 0 == extHandle ) return 0;
5946 GEOSContextHandleInternal_t *handle = 0;
5947 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
5948 if ( 0 == handle->initialized ) return 0;
5952 if ( style > BufferParameters::CAP_SQUARE )
5954 throw IllegalArgumentException("Invalid buffer endCap style");
5956 p->setEndCapStyle(static_cast<BufferParameters::EndCapStyle>(style));
5957 return 1;
5959 catch (const std::exception &e)
5961 handle->ERROR_MESSAGE("%s", e.what());
5963 catch (...)
5965 handle->ERROR_MESSAGE("Unknown exception thrown");
5968 return 0;
5972 GEOSBufferParams_setJoinStyle_r(GEOSContextHandle_t extHandle,
5973 GEOSBufferParams* p, int style)
5975 if ( 0 == extHandle ) return 0;
5977 GEOSContextHandleInternal_t *handle = 0;
5978 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
5979 if ( 0 == handle->initialized ) return 0;
5983 if ( style > BufferParameters::JOIN_BEVEL ) {
5984 throw IllegalArgumentException("Invalid buffer join style");
5986 p->setJoinStyle(static_cast<BufferParameters::JoinStyle>(style));
5987 return 1;
5989 catch (const std::exception &e)
5991 handle->ERROR_MESSAGE("%s", e.what());
5993 catch (...)
5995 handle->ERROR_MESSAGE("Unknown exception thrown");
5998 return 0;
6002 GEOSBufferParams_setMitreLimit_r(GEOSContextHandle_t extHandle,
6003 GEOSBufferParams* p, double limit)
6005 if ( 0 == extHandle ) return 0;
6007 GEOSContextHandleInternal_t *handle = 0;
6008 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
6009 if ( 0 == handle->initialized ) return 0;
6013 p->setMitreLimit(limit);
6014 return 1;
6016 catch (const std::exception &e)
6018 handle->ERROR_MESSAGE("%s", e.what());
6020 catch (...)
6022 handle->ERROR_MESSAGE("Unknown exception thrown");
6025 return 0;
6029 GEOSBufferParams_setQuadrantSegments_r(GEOSContextHandle_t extHandle,
6030 GEOSBufferParams* p, int segs)
6032 if ( 0 == extHandle ) return 0;
6034 GEOSContextHandleInternal_t *handle = 0;
6035 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
6036 if ( 0 == handle->initialized ) return 0;
6040 p->setQuadrantSegments(segs);
6041 return 1;
6043 catch (const std::exception &e)
6045 handle->ERROR_MESSAGE("%s", e.what());
6047 catch (...)
6049 handle->ERROR_MESSAGE("Unknown exception thrown");
6052 return 0;
6056 GEOSBufferParams_setSingleSided_r(GEOSContextHandle_t extHandle,
6057 GEOSBufferParams* p, int ss)
6059 if ( 0 == extHandle ) return 0;
6061 GEOSContextHandleInternal_t *handle = 0;
6062 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
6063 if ( 0 == handle->initialized ) return 0;
6067 p->setSingleSided( (ss != 0) );
6068 return 1;
6070 catch (const std::exception &e)
6072 handle->ERROR_MESSAGE("%s", e.what());
6074 catch (...)
6076 handle->ERROR_MESSAGE("Unknown exception thrown");
6079 return 0;
6082 Geometry *
6083 GEOSBufferWithParams_r(GEOSContextHandle_t extHandle, const Geometry *g1, const BufferParameters* bp, double width)
6085 using geos::operation::buffer::BufferOp;
6087 if ( 0 == extHandle ) return NULL;
6089 GEOSContextHandleInternal_t *handle = 0;
6090 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
6091 if ( 0 == handle->initialized ) return NULL;
6095 BufferOp op(g1, *bp);
6096 Geometry *g3 = op.getResultGeometry(width);
6097 return g3;
6099 catch (const std::exception &e)
6101 handle->ERROR_MESSAGE("%s", e.what());
6103 catch (...)
6105 handle->ERROR_MESSAGE("Unknown exception thrown");
6108 return NULL;
6111 } /* extern "C" */