HotPixel: do not invalidate reference to original point. Fixes #498.
[geos.git] / capi / geos_ts_c.cpp
blob0a72ed826d027277c12988910add9afc4e61c572
1 /************************************************************************
3 * $Id$
5 * C-Wrapper for GEOS library
7 * Copyright (C) 2010 2011 Sandro Santilli <strk@keybit.net>
8 * Copyright (C) 2005-2006 Refractions Research Inc.
10 * This is free software; you can redistribute and/or modify it under
11 * the terms of the GNU Lesser General Public Licence as published
12 * by the Free Software Foundation.
13 * See the COPYING file for more information.
15 * Author: Sandro Santilli <strk@refractions.net>
16 * Thread Safety modifications: Chuck Thibert <charles.thibert@ingres.com>
18 ***********************************************************************/
20 #include <geos/platform.h> // for FINITE
21 #include <geos/geom/Geometry.h>
22 #include <geos/geom/prep/PreparedGeometry.h>
23 #include <geos/geom/prep/PreparedGeometryFactory.h>
24 #include <geos/geom/GeometryCollection.h>
25 #include <geos/geom/Polygon.h>
26 #include <geos/geom/Point.h>
27 #include <geos/geom/MultiPoint.h>
28 #include <geos/geom/MultiLineString.h>
29 #include <geos/geom/MultiPolygon.h>
30 #include <geos/geom/LinearRing.h>
31 #include <geos/geom/LineString.h>
32 #include <geos/geom/PrecisionModel.h>
33 #include <geos/geom/GeometryFactory.h>
34 #include <geos/geom/CoordinateSequenceFactory.h>
35 #include <geos/geom/Coordinate.h>
36 #include <geos/geom/IntersectionMatrix.h>
37 #include <geos/geom/Envelope.h>
38 #include <geos/index/strtree/STRtree.h>
39 #include <geos/index/ItemVisitor.h>
40 #include <geos/io/WKTReader.h>
41 #include <geos/io/WKBReader.h>
42 #include <geos/io/WKTWriter.h>
43 #include <geos/io/WKBWriter.h>
44 #include <geos/algorithm/distance/DiscreteHausdorffDistance.h>
45 #include <geos/algorithm/CGAlgorithms.h>
46 #include <geos/algorithm/BoundaryNodeRule.h>
47 #include <geos/simplify/DouglasPeuckerSimplifier.h>
48 #include <geos/simplify/TopologyPreservingSimplifier.h>
49 #include <geos/operation/valid/IsValidOp.h>
50 #include <geos/operation/polygonize/Polygonizer.h>
51 #include <geos/operation/linemerge/LineMerger.h>
52 #include <geos/operation/overlay/OverlayOp.h>
53 #include <geos/operation/union/CascadedPolygonUnion.h>
54 #include <geos/operation/buffer/BufferOp.h>
55 #include <geos/operation/buffer/BufferParameters.h>
56 #include <geos/operation/buffer/BufferBuilder.h>
57 #include <geos/operation/relate/RelateOp.h>
58 #include <geos/operation/sharedpaths/SharedPathsOp.h>
59 #include <geos/linearref/LengthIndexedLine.h>
60 #include <geos/geom/BinaryOp.h>
61 #include <geos/util/IllegalArgumentException.h>
62 #include <geos/util/UniqueCoordinateArrayFilter.h>
63 #include <geos/util/Machine.h>
64 #include <geos/version.h>
66 // This should go away
67 #include <cmath> // finite
68 #include <cstddef>
69 #include <cstdio>
70 #include <cstdlib>
71 #include <cstring>
72 #include <fstream>
73 #include <iostream>
74 #include <sstream>
75 #include <string>
76 #include <memory>
78 #ifdef _MSC_VER
79 #pragma warning(disable : 4099)
80 #endif
82 // Some extra magic to make type declarations in geos_c.h work -
83 // for cross-checking of types in header.
84 #define GEOSGeometry geos::geom::Geometry
85 #define GEOSPreparedGeometry geos::geom::prep::PreparedGeometry
86 #define GEOSCoordSequence geos::geom::CoordinateSequence
87 #define GEOSBufferParams geos::operation::buffer::BufferParameters
88 #define GEOSSTRtree geos::index::strtree::STRtree
89 #define GEOSWKTReader_t geos::io::WKTReader
90 #define GEOSWKTWriter_t geos::io::WKTWriter
91 #define GEOSWKBReader_t geos::io::WKBReader
92 #define GEOSWKBWriter_t geos::io::WKBWriter
94 #include "geos_c.h"
96 // Intentional, to allow non-standard C elements like C99 functions to be
97 // imported through C++ headers of C library, like <cmath>.
98 using namespace std;
100 /// Define this if you want operations triggering Exceptions to
101 /// be printed.
102 /// (will use the NOTIFY channel - only implemented for GEOSUnion so far)
104 #undef VERBOSE_EXCEPTIONS
106 #include <geos/export.h>
109 // import the most frequently used definitions globally
110 using geos::geom::Geometry;
111 using geos::geom::LineString;
112 using geos::geom::Polygon;
113 using geos::geom::CoordinateSequence;
114 using geos::geom::GeometryFactory;
116 using geos::io::WKTReader;
117 using geos::io::WKTWriter;
118 using geos::io::WKBReader;
119 using geos::io::WKBWriter;
121 using geos::operation::overlay::OverlayOp;
122 using geos::operation::overlay::overlayOp;
123 using geos::operation::geounion::CascadedPolygonUnion;
124 using geos::operation::buffer::BufferParameters;
125 using geos::operation::buffer::BufferBuilder;
126 using geos::util::IllegalArgumentException;
127 using geos::algorithm::distance::DiscreteHausdorffDistance;
129 typedef std::auto_ptr<Geometry> GeomAutoPtr;
131 typedef struct GEOSContextHandleInternal
133 const GeometryFactory *geomFactory;
134 GEOSMessageHandler NOTICE_MESSAGE;
135 GEOSMessageHandler ERROR_MESSAGE;
136 int WKBOutputDims;
137 int WKBByteOrder;
138 int initialized;
139 } GEOSContextHandleInternal_t;
141 // CAPI_ItemVisitor is used internally by the CAPI STRtree
142 // wrappers. It's defined here just to keep it out of the
143 // extern "C" block.
144 class CAPI_ItemVisitor : public geos::index::ItemVisitor {
145 GEOSQueryCallback callback;
146 void *userdata;
147 public:
148 CAPI_ItemVisitor (GEOSQueryCallback cb, void *ud)
149 : ItemVisitor(), callback(cb), userdata(ud) {};
150 void visitItem (void *item) { callback(item, userdata); };
154 //## PROTOTYPES #############################################
156 extern "C" const char GEOS_DLL *GEOSjtsport();
157 extern "C" char GEOS_DLL *GEOSasText(Geometry *g1);
159 namespace { // anonymous
161 char* gstrdup_s(const char* str, const std::size_t size)
163 char* out = static_cast<char*>(std::malloc(size + 1));
164 if (0 != out)
166 // as no strlen call necessary, memcpy may be faster than strcpy
167 std::memcpy(out, str, size + 1);
170 assert(0 != out);
172 // we haven't been checking allocation before ticket #371
173 if (0 == out)
175 throw(std::runtime_error("Failed to allocate memory for duplicate string"));
178 return out;
181 char* gstrdup(std::string const& str)
183 return gstrdup_s(str.c_str(), str.size());
186 } // namespace anonymous
188 extern "C" {
190 GEOSContextHandle_t
191 initGEOS_r(GEOSMessageHandler nf, GEOSMessageHandler ef)
193 GEOSContextHandleInternal_t *handle = 0;
194 void *extHandle = 0;
196 extHandle = std::malloc(sizeof(GEOSContextHandleInternal_t));
197 if (0 != extHandle)
199 handle = static_cast<GEOSContextHandleInternal_t*>(extHandle);
200 handle->NOTICE_MESSAGE = nf;
201 handle->ERROR_MESSAGE = ef;
202 handle->geomFactory = GeometryFactory::getDefaultInstance();
203 handle->WKBOutputDims = 2;
204 handle->WKBByteOrder = getMachineByteOrder();
205 handle->initialized = 1;
208 return static_cast<GEOSContextHandle_t>(extHandle);
211 GEOSMessageHandler
212 GEOSContext_setNoticeHandler_r(GEOSContextHandle_t extHandle, GEOSMessageHandler nf)
214 GEOSMessageHandler f;
215 GEOSContextHandleInternal_t *handle = 0;
216 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
217 if ( 0 == handle->initialized )
219 return NULL;
222 f = handle->NOTICE_MESSAGE;
223 handle->NOTICE_MESSAGE = nf;
225 return f;
228 GEOSMessageHandler
229 GEOSContext_setErrorHandler_r(GEOSContextHandle_t extHandle, GEOSMessageHandler nf)
231 GEOSMessageHandler f;
232 GEOSContextHandleInternal_t *handle = 0;
233 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
234 if ( 0 == handle->initialized )
236 return NULL;
239 f = handle->ERROR_MESSAGE;
240 handle->ERROR_MESSAGE = nf;
242 return f;
245 void
246 finishGEOS_r(GEOSContextHandle_t extHandle)
248 // Fix up freeing handle w.r.t. malloc above
249 std::free(extHandle);
250 extHandle = NULL;
253 void
254 GEOSFree_r (GEOSContextHandle_t extHandle, void* buffer)
256 assert(0 != extHandle);
258 std::free(buffer);
261 //-----------------------------------------------------------
262 // relate()-related functions
263 // return 0 = false, 1 = true, 2 = error occured
264 //-----------------------------------------------------------
266 char
267 GEOSDisjoint_r(GEOSContextHandle_t extHandle, const Geometry *g1, const Geometry *g2)
269 if ( 0 == extHandle )
271 return 2;
274 GEOSContextHandleInternal_t *handle = 0;
275 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
276 if ( handle->initialized == 0 )
278 return 2;
283 bool result = g1->disjoint(g2);
284 return result;
287 // TODO: mloskot is going to replace these double-catch block
288 // with a macro to remove redundant code in this and
289 // following functions.
290 catch (const std::exception &e)
292 handle->ERROR_MESSAGE("%s", e.what());
294 catch (...)
296 handle->ERROR_MESSAGE("Unknown exception thrown");
299 return 2;
302 char
303 GEOSTouches_r(GEOSContextHandle_t extHandle, const Geometry *g1, const Geometry *g2)
305 if ( 0 == extHandle )
307 return 2;
310 GEOSContextHandleInternal_t *handle = 0;
311 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
312 if ( 0 == handle->initialized )
314 return 2;
319 bool result = g1->touches(g2);
320 return result;
322 catch (const std::exception &e)
324 handle->ERROR_MESSAGE("%s", e.what());
326 catch (...)
328 handle->ERROR_MESSAGE("Unknown exception thrown");
331 return 2;
334 char
335 GEOSIntersects_r(GEOSContextHandle_t extHandle, const Geometry *g1, const Geometry *g2)
337 if ( 0 == extHandle )
339 return 2;
342 GEOSContextHandleInternal_t *handle = 0;
343 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
344 if ( 0 == handle->initialized )
346 return 2;
351 bool result = g1->intersects(g2);
352 return result;
354 catch (const std::exception &e)
356 handle->ERROR_MESSAGE("%s", e.what());
358 catch (...)
360 handle->ERROR_MESSAGE("Unknown exception thrown");
363 return 2;
366 char
367 GEOSCrosses_r(GEOSContextHandle_t extHandle, const Geometry *g1, const Geometry *g2)
369 if ( 0 == extHandle )
371 return 2;
374 GEOSContextHandleInternal_t *handle = 0;
375 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
376 if ( 0 == handle->initialized )
378 return 2;
383 bool result = g1->crosses(g2);
384 return result;
386 catch (const std::exception &e)
388 handle->ERROR_MESSAGE("%s", e.what());
390 catch (...)
392 handle->ERROR_MESSAGE("Unknown exception thrown");
395 return 2;
398 char
399 GEOSWithin_r(GEOSContextHandle_t extHandle, const Geometry *g1, const Geometry *g2)
401 if ( 0 == extHandle )
403 return 2;
406 GEOSContextHandleInternal_t *handle = 0;
407 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
408 if ( 0 == handle->initialized )
410 return 2;
415 bool result = g1->within(g2);
416 return result;
418 catch (const std::exception &e)
420 handle->ERROR_MESSAGE("%s", e.what());
422 catch (...)
424 handle->ERROR_MESSAGE("Unknown exception thrown");
427 return 2;
430 // call g1->contains(g2)
431 // returns 0 = false
432 // 1 = true
433 // 2 = error was trapped
434 char
435 GEOSContains_r(GEOSContextHandle_t extHandle, const Geometry *g1, const Geometry *g2)
437 if ( 0 == extHandle )
439 return 2;
442 GEOSContextHandleInternal_t *handle = 0;
443 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
444 if ( 0 == handle->initialized )
446 return 2;
451 bool result = g1->contains(g2);
452 return result;
454 catch (const std::exception &e)
456 handle->ERROR_MESSAGE("%s", e.what());
458 catch (...)
460 handle->ERROR_MESSAGE("Unknown exception thrown");
463 return 2;
466 char
467 GEOSOverlaps_r(GEOSContextHandle_t extHandle, const Geometry *g1, const Geometry *g2)
469 if ( 0 == extHandle )
471 return 2;
474 GEOSContextHandleInternal_t *handle = 0;
475 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
476 if ( 0 == handle->initialized )
478 return 2;
483 bool result = g1->overlaps(g2);
484 return result;
486 catch (const std::exception &e)
488 handle->ERROR_MESSAGE("%s", e.what());
490 catch (...)
492 handle->ERROR_MESSAGE("Unknown exception thrown");
495 return 2;
498 char
499 GEOSCovers_r(GEOSContextHandle_t extHandle, const Geometry *g1, const Geometry *g2)
501 if ( 0 == extHandle )
503 return 2;
506 GEOSContextHandleInternal_t *handle = 0;
507 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
508 if ( 0 == handle->initialized )
510 return 2;
515 bool result = g1->covers(g2);
516 return result;
518 catch (const std::exception &e)
520 handle->ERROR_MESSAGE("%s", e.what());
522 catch (...)
524 handle->ERROR_MESSAGE("Unknown exception thrown");
527 return 2;
530 char
531 GEOSCoveredBy_r(GEOSContextHandle_t extHandle, const Geometry *g1, const Geometry *g2)
533 if ( 0 == extHandle )
535 return 2;
538 GEOSContextHandleInternal_t *handle = 0;
539 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
540 if ( 0 == handle->initialized )
542 return 2;
547 bool result = g1->coveredBy(g2);
548 return result;
550 catch (const std::exception &e)
552 handle->ERROR_MESSAGE("%s", e.what());
554 catch (...)
556 handle->ERROR_MESSAGE("Unknown exception thrown");
559 return 2;
563 //-------------------------------------------------------------------
564 // low-level relate functions
565 //------------------------------------------------------------------
567 char
568 GEOSRelatePattern_r(GEOSContextHandle_t extHandle, const Geometry *g1, const Geometry *g2, const char *pat)
570 if ( 0 == extHandle )
572 return 2;
575 GEOSContextHandleInternal_t *handle = 0;
576 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
577 if ( 0 == handle->initialized )
579 return 2;
584 std::string s(pat);
585 bool result = g1->relate(g2, s);
586 return result;
588 catch (const std::exception &e)
590 handle->ERROR_MESSAGE("%s", e.what());
592 catch (...)
594 handle->ERROR_MESSAGE("Unknown exception thrown");
597 return 2;
600 char
601 GEOSRelatePatternMatch_r(GEOSContextHandle_t extHandle, const char *mat,
602 const char *pat)
604 if ( 0 == extHandle )
606 return 2;
609 GEOSContextHandleInternal_t *handle = 0;
610 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
611 if ( 0 == handle->initialized )
613 return 2;
618 using geos::geom::IntersectionMatrix;
620 std::string m(mat);
621 std::string p(pat);
622 IntersectionMatrix im(m);
624 bool result = im.matches(p);
625 return result;
627 catch (const std::exception &e)
629 handle->ERROR_MESSAGE("%s", e.what());
631 catch (...)
633 handle->ERROR_MESSAGE("Unknown exception thrown");
636 return 2;
639 char *
640 GEOSRelate_r(GEOSContextHandle_t extHandle, const Geometry *g1, const Geometry *g2)
642 if ( 0 == extHandle )
644 return NULL;
647 GEOSContextHandleInternal_t *handle = 0;
648 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
649 if ( 0 == handle->initialized )
651 return NULL;
656 using geos::geom::IntersectionMatrix;
658 IntersectionMatrix* im = g1->relate(g2);
659 if (0 == im)
661 return 0;
664 char *result = gstrdup(im->toString());
666 delete im;
667 im = 0;
669 return result;
671 catch (const std::exception &e)
673 handle->ERROR_MESSAGE("%s", e.what());
675 catch (...)
677 handle->ERROR_MESSAGE("Unknown exception thrown");
680 return NULL;
683 char *
684 GEOSRelateBoundaryNodeRule_r(GEOSContextHandle_t extHandle, const Geometry *g1, const Geometry *g2, int bnr)
686 if ( 0 == extHandle )
688 return NULL;
691 GEOSContextHandleInternal_t *handle = 0;
692 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
693 if ( 0 == handle->initialized )
695 return NULL;
700 using geos::operation::relate::RelateOp;
701 using geos::geom::IntersectionMatrix;
702 using geos::algorithm::BoundaryNodeRule;
704 IntersectionMatrix* im;
705 switch (bnr) {
706 case GEOSRELATE_BNR_MOD2: /* same as OGC */
707 im = RelateOp::relate(g1, g2,
708 BoundaryNodeRule::MOD2_BOUNDARY_RULE);
709 break;
710 case GEOSRELATE_BNR_ENDPOINT:
711 im = RelateOp::relate(g1, g2,
712 BoundaryNodeRule::ENDPOINT_BOUNDARY_RULE);
713 break;
714 case GEOSRELATE_BNR_MULTIVALENT_ENDPOINT:
715 im = RelateOp::relate(g1, g2,
716 BoundaryNodeRule::MULTIVALENT_ENDPOINT_BOUNDARY_RULE);
717 break;
718 case GEOSRELATE_BNR_MONOVALENT_ENDPOINT:
719 im = RelateOp::relate(g1, g2,
720 BoundaryNodeRule::MONOVALENT_ENDPOINT_BOUNDARY_RULE);
721 break;
722 default:
723 handle->ERROR_MESSAGE("Invalid boundary node rule %d", bnr);
724 return 0;
725 break;
728 if (0 == im) return 0;
730 char *result = gstrdup(im->toString());
732 delete im;
733 im = 0;
735 return result;
737 catch (const std::exception &e)
739 handle->ERROR_MESSAGE("%s", e.what());
741 catch (...)
743 handle->ERROR_MESSAGE("Unknown exception thrown");
746 return NULL;
751 //-----------------------------------------------------------------
752 // isValid
753 //-----------------------------------------------------------------
756 char
757 GEOSisValid_r(GEOSContextHandle_t extHandle, const Geometry *g1)
759 if ( 0 == extHandle )
761 return 2;
764 GEOSContextHandleInternal_t *handle = 0;
765 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
766 if ( 0 == handle->initialized )
768 return 2;
773 using geos::operation::valid::IsValidOp;
774 using geos::operation::valid::TopologyValidationError;
776 IsValidOp ivo(g1);
777 TopologyValidationError *err = ivo.getValidationError();
778 if ( err )
780 handle->NOTICE_MESSAGE("%s", err->toString().c_str());
781 return 0;
783 else
785 return 1;
788 catch (const std::exception &e)
790 handle->ERROR_MESSAGE("%s", e.what());
792 catch (...)
794 handle->ERROR_MESSAGE("Unknown exception thrown");
797 return 2;
800 char *
801 GEOSisValidReason_r(GEOSContextHandle_t extHandle, const Geometry *g1)
803 if ( 0 == extHandle )
805 return NULL;
808 GEOSContextHandleInternal_t *handle = 0;
809 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
810 if ( 0 == handle->initialized )
812 return NULL;
817 using geos::operation::valid::IsValidOp;
818 using geos::operation::valid::TopologyValidationError;
820 char* result = 0;
821 char const* const validstr = "Valid Geometry";
823 IsValidOp ivo(g1);
824 TopologyValidationError *err = ivo.getValidationError();
825 if (0 != err)
827 std::ostringstream ss;
828 ss.precision(15);
829 ss << err->getCoordinate();
830 const std::string errloc = ss.str();
831 std::string errmsg(err->getMessage());
832 errmsg += "[" + errloc + "]";
833 result = gstrdup(errmsg);
835 else
837 result = gstrdup(std::string(validstr));
840 return result;
842 catch (const std::exception &e)
844 handle->ERROR_MESSAGE("%s", e.what());
846 catch (...)
848 handle->ERROR_MESSAGE("Unknown exception thrown");
851 return 0;
854 char
855 GEOSisValidDetail_r(GEOSContextHandle_t extHandle, const Geometry *g,
856 int flags, char** reason, Geometry ** location)
858 if ( 0 == extHandle )
860 return 0;
863 GEOSContextHandleInternal_t *handle = 0;
864 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
865 if ( 0 == handle->initialized )
867 return 0;
872 using geos::operation::valid::IsValidOp;
873 using geos::operation::valid::TopologyValidationError;
875 IsValidOp ivo(g);
876 if ( flags & GEOSVALID_ALLOW_SELFTOUCHING_RING_FORMING_HOLE ) {
877 ivo.setSelfTouchingRingFormingHoleValid(true);
879 TopologyValidationError *err = ivo.getValidationError();
880 if (0 != err)
882 if ( location ) {
883 *location = handle->geomFactory->createPoint(err->getCoordinate());
885 if ( reason ) {
886 std::string errmsg(err->getMessage());
887 *reason = gstrdup(errmsg);
889 return 0;
892 if ( location ) *location = 0;
893 if ( reason ) *reason = 0;
894 return 1; /* valid */
897 catch (const std::exception &e)
899 handle->ERROR_MESSAGE("%s", e.what());
901 catch (...)
903 handle->ERROR_MESSAGE("Unknown exception thrown");
906 return 2; /* exception */
909 //-----------------------------------------------------------------
910 // general purpose
911 //-----------------------------------------------------------------
913 char
914 GEOSEquals_r(GEOSContextHandle_t extHandle, const Geometry *g1, const Geometry *g2)
916 if ( 0 == extHandle )
918 return 2;
921 GEOSContextHandleInternal_t *handle = 0;
922 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
923 if ( 0 == handle->initialized )
925 return 2;
930 bool result = g1->equals(g2);
931 return result;
933 catch (const std::exception &e)
935 handle->ERROR_MESSAGE("%s", e.what());
937 catch (...)
939 handle->ERROR_MESSAGE("Unknown exception thrown");
942 return 2;
945 char
946 GEOSEqualsExact_r(GEOSContextHandle_t extHandle, const Geometry *g1, const Geometry *g2, double tolerance)
948 if ( 0 == extHandle )
950 return 2;
953 GEOSContextHandleInternal_t *handle = 0;
954 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
955 if ( 0 == handle->initialized )
957 return 2;
962 bool result = g1->equalsExact(g2, tolerance);
963 return result;
965 catch (const std::exception &e)
967 handle->ERROR_MESSAGE("%s", e.what());
969 catch (...)
971 handle->ERROR_MESSAGE("Unknown exception thrown");
974 return 2;
978 GEOSDistance_r(GEOSContextHandle_t extHandle, const Geometry *g1, const Geometry *g2, double *dist)
980 assert(0 != dist);
982 if ( 0 == extHandle )
984 return 0;
987 GEOSContextHandleInternal_t *handle = 0;
988 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
989 if ( 0 == handle->initialized )
991 return 0;
996 *dist = g1->distance(g2);
997 return 1;
999 catch (const std::exception &e)
1001 handle->ERROR_MESSAGE("%s", e.what());
1003 catch (...)
1005 handle->ERROR_MESSAGE("Unknown exception thrown");
1008 return 0;
1012 GEOSHausdorffDistance_r(GEOSContextHandle_t extHandle, const Geometry *g1, const Geometry *g2, double *dist)
1014 assert(0 != dist);
1016 if ( 0 == extHandle )
1018 return 0;
1021 GEOSContextHandleInternal_t *handle = 0;
1022 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
1023 if ( 0 == handle->initialized )
1025 return 0;
1030 *dist = DiscreteHausdorffDistance::distance(*g1, *g2);
1031 return 1;
1033 catch (const std::exception &e)
1035 handle->ERROR_MESSAGE("%s", e.what());
1037 catch (...)
1039 handle->ERROR_MESSAGE("Unknown exception thrown");
1042 return 0;
1046 GEOSHausdorffDistanceDensify_r(GEOSContextHandle_t extHandle, const Geometry *g1, const Geometry *g2, double densifyFrac, double *dist)
1048 assert(0 != dist);
1050 if ( 0 == extHandle )
1052 return 0;
1055 GEOSContextHandleInternal_t *handle = 0;
1056 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
1057 if ( 0 == handle->initialized )
1059 return 0;
1064 *dist = DiscreteHausdorffDistance::distance(*g1, *g2, densifyFrac);
1065 return 1;
1067 catch (const std::exception &e)
1069 handle->ERROR_MESSAGE("%s", e.what());
1071 catch (...)
1073 handle->ERROR_MESSAGE("Unknown exception thrown");
1076 return 0;
1080 GEOSArea_r(GEOSContextHandle_t extHandle, const Geometry *g, double *area)
1082 assert(0 != area);
1084 if ( 0 == extHandle )
1086 return 0;
1089 GEOSContextHandleInternal_t *handle = 0;
1090 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
1091 if ( 0 == handle->initialized )
1093 return 0;
1098 *area = g->getArea();
1099 return 1;
1101 catch (const std::exception &e)
1103 handle->ERROR_MESSAGE("%s", e.what());
1105 catch (...)
1107 handle->ERROR_MESSAGE("Unknown exception thrown");
1110 return 0;
1114 GEOSLength_r(GEOSContextHandle_t extHandle, const Geometry *g, double *length)
1116 assert(0 != length);
1118 if ( 0 == extHandle )
1120 return 2;
1123 GEOSContextHandleInternal_t *handle = 0;
1124 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
1125 if ( 0 == handle->initialized )
1127 return 0;
1132 *length = g->getLength();
1133 return 1;
1135 catch (const std::exception &e)
1137 handle->ERROR_MESSAGE("%s", e.what());
1139 catch (...)
1141 handle->ERROR_MESSAGE("Unknown exception thrown");
1144 return 0;
1147 Geometry *
1148 GEOSGeomFromWKT_r(GEOSContextHandle_t extHandle, const char *wkt)
1150 if ( 0 == extHandle )
1152 return NULL;
1155 GEOSContextHandleInternal_t *handle = 0;
1156 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
1157 if ( 0 == handle->initialized )
1159 return NULL;
1164 const std::string wktstring(wkt);
1165 WKTReader r(static_cast<GeometryFactory const*>(handle->geomFactory));
1167 Geometry *g = r.read(wktstring);
1168 return g;
1170 catch (const std::exception &e)
1172 handle->ERROR_MESSAGE("%s", e.what());
1174 catch (...)
1176 handle->ERROR_MESSAGE("Unknown exception thrown");
1179 return NULL;
1182 char *
1183 GEOSGeomToWKT_r(GEOSContextHandle_t extHandle, const Geometry *g1)
1185 if ( 0 == extHandle )
1187 return NULL;
1190 GEOSContextHandleInternal_t *handle = 0;
1191 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
1192 if ( 0 == handle->initialized )
1194 return NULL;
1200 char *result = gstrdup(g1->toString());
1201 return result;
1203 catch (const std::exception &e)
1205 handle->ERROR_MESSAGE("%s", e.what());
1207 catch (...)
1209 handle->ERROR_MESSAGE("Unknown exception thrown");
1211 return NULL;
1214 // Remember to free the result!
1215 unsigned char *
1216 GEOSGeomToWKB_buf_r(GEOSContextHandle_t extHandle, const Geometry *g, size_t *size)
1218 assert(0 != size);
1220 if ( 0 == extHandle )
1222 return NULL;
1225 GEOSContextHandleInternal_t *handle = 0;
1226 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
1227 if ( 0 == handle->initialized )
1229 return NULL;
1232 using geos::io::WKBWriter;
1235 int byteOrder = static_cast<int>(handle->WKBByteOrder);
1236 WKBWriter w(handle->WKBOutputDims, byteOrder);
1237 std::ostringstream os(std::ios_base::binary);
1238 w.write(*g, os);
1239 std::string wkbstring(os.str());
1240 const std::size_t len = wkbstring.length();
1242 unsigned char* result = 0;
1243 result = static_cast<unsigned char*>(std::malloc(len));
1244 if (0 != result)
1246 std::memcpy(result, wkbstring.c_str(), len);
1247 *size = len;
1249 return result;
1251 catch (const std::exception &e)
1253 handle->ERROR_MESSAGE("%s", e.what());
1255 catch (...)
1257 handle->ERROR_MESSAGE("Unknown exception thrown");
1260 return NULL;
1263 Geometry *
1264 GEOSGeomFromWKB_buf_r(GEOSContextHandle_t extHandle, const unsigned char *wkb, size_t size)
1266 if ( 0 == extHandle )
1268 return NULL;
1271 GEOSContextHandleInternal_t *handle = 0;
1272 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
1273 if ( 0 == handle->initialized )
1275 return NULL;
1278 using geos::io::WKBReader;
1281 std::string wkbstring(reinterpret_cast<const char*>(wkb), size); // make it binary !
1282 WKBReader r(*(static_cast<GeometryFactory const*>(handle->geomFactory)));
1283 std::istringstream is(std::ios_base::binary);
1284 is.str(wkbstring);
1285 is.seekg(0, std::ios::beg); // rewind reader pointer
1286 Geometry *g = r.read(is);
1287 return g;
1289 catch (const std::exception &e)
1291 handle->ERROR_MESSAGE("%s", e.what());
1293 catch (...)
1295 handle->ERROR_MESSAGE("Unknown exception thrown");
1298 return NULL;
1301 /* Read/write wkb hex values. Returned geometries are
1302 owned by the caller.*/
1303 unsigned char *
1304 GEOSGeomToHEX_buf_r(GEOSContextHandle_t extHandle, const Geometry *g, size_t *size)
1306 if ( 0 == extHandle )
1308 return NULL;
1311 GEOSContextHandleInternal_t *handle = 0;
1312 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
1313 if ( 0 == handle->initialized )
1315 return NULL;
1318 using geos::io::WKBWriter;
1321 int byteOrder = static_cast<int>(handle->WKBByteOrder);
1322 WKBWriter w(handle->WKBOutputDims, byteOrder);
1323 std::ostringstream os(std::ios_base::binary);
1324 w.writeHEX(*g, os);
1325 std::string hexstring(os.str());
1327 char *result = gstrdup(hexstring);
1328 if (0 != result)
1330 *size = hexstring.length();
1333 return reinterpret_cast<unsigned char*>(result);
1335 catch (const std::exception &e)
1337 handle->ERROR_MESSAGE("%s", e.what());
1339 catch (...)
1341 handle->ERROR_MESSAGE("Unknown exception thrown");
1344 return NULL;
1347 Geometry *
1348 GEOSGeomFromHEX_buf_r(GEOSContextHandle_t extHandle, const unsigned char *hex, size_t size)
1350 if ( 0 == extHandle )
1352 return NULL;
1355 GEOSContextHandleInternal_t *handle = 0;
1356 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
1357 if ( 0 == handle->initialized )
1359 return NULL;
1362 using geos::io::WKBReader;
1365 std::string hexstring(reinterpret_cast<const char*>(hex), size);
1366 WKBReader r(*(static_cast<GeometryFactory const*>(handle->geomFactory)));
1367 std::istringstream is(std::ios_base::binary);
1368 is.str(hexstring);
1369 is.seekg(0, std::ios::beg); // rewind reader pointer
1371 Geometry *g = r.readHEX(is);
1372 return g;
1374 catch (const std::exception &e)
1376 handle->ERROR_MESSAGE("%s", e.what());
1378 catch (...)
1380 handle->ERROR_MESSAGE("Unknown exception thrown");
1383 return NULL;
1386 char
1387 GEOSisEmpty_r(GEOSContextHandle_t extHandle, const Geometry *g1)
1389 if ( 0 == extHandle )
1391 return 2;
1394 GEOSContextHandleInternal_t *handle = 0;
1395 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
1396 if ( 0 == handle->initialized )
1398 return 2;
1403 return g1->isEmpty();
1405 catch (const std::exception &e)
1407 handle->ERROR_MESSAGE("%s", e.what());
1409 catch (...)
1411 handle->ERROR_MESSAGE("Unknown exception thrown");
1414 return 2;
1417 char
1418 GEOSisSimple_r(GEOSContextHandle_t extHandle, const Geometry *g1)
1420 if ( 0 == extHandle )
1422 return 2;
1425 GEOSContextHandleInternal_t *handle = 0;
1426 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
1427 if ( 0 == handle->initialized )
1429 return 2;
1434 return g1->isSimple();
1436 catch (const std::exception &e)
1438 handle->ERROR_MESSAGE("%s", e.what());
1439 return 2;
1442 catch (...)
1444 handle->ERROR_MESSAGE("Unknown exception thrown");
1445 return 2;
1449 char
1450 GEOSisRing_r(GEOSContextHandle_t extHandle, const Geometry *g)
1452 if ( 0 == extHandle )
1454 return 2;
1457 GEOSContextHandleInternal_t *handle = 0;
1458 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
1459 if ( 0 == handle->initialized )
1461 return 2;
1466 const LineString *ls = dynamic_cast<const LineString *>(g);
1467 if ( ls ) {
1468 return (ls->isRing());
1469 } else {
1470 return 0;
1473 catch (const std::exception &e)
1475 handle->ERROR_MESSAGE("%s", e.what());
1476 return 2;
1479 catch (...)
1481 handle->ERROR_MESSAGE("Unknown exception thrown");
1482 return 2;
1488 //free the result of this
1489 char *
1490 GEOSGeomType_r(GEOSContextHandle_t extHandle, const Geometry *g1)
1492 if ( 0 == extHandle )
1494 return NULL;
1497 GEOSContextHandleInternal_t *handle = 0;
1498 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
1499 if ( 0 == handle->initialized )
1501 return NULL;
1506 std::string s = g1->getGeometryType();
1508 char *result = gstrdup(s);
1509 return result;
1511 catch (const std::exception &e)
1513 handle->ERROR_MESSAGE("%s", e.what());
1515 catch (...)
1517 handle->ERROR_MESSAGE("Unknown exception thrown");
1520 return NULL;
1523 // Return postgis geometry type index
1525 GEOSGeomTypeId_r(GEOSContextHandle_t extHandle, const Geometry *g1)
1527 if ( 0 == extHandle )
1529 return -1;
1532 GEOSContextHandleInternal_t *handle = 0;
1533 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
1534 if ( 0 == handle->initialized )
1536 return -1;
1541 return g1->getGeometryTypeId();
1543 catch (const std::exception &e)
1545 handle->ERROR_MESSAGE("%s", e.what());
1547 catch (...)
1549 handle->ERROR_MESSAGE("Unknown exception thrown");
1552 return -1;
1555 //-------------------------------------------------------------------
1556 // GEOS functions that return geometries
1557 //-------------------------------------------------------------------
1559 Geometry *
1560 GEOSEnvelope_r(GEOSContextHandle_t extHandle, const Geometry *g1)
1562 if ( 0 == extHandle )
1564 return NULL;
1567 GEOSContextHandleInternal_t *handle = 0;
1568 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
1569 if ( 0 == handle->initialized )
1571 return NULL;
1576 Geometry *g3 = g1->getEnvelope();
1577 return g3;
1579 catch (const std::exception &e)
1581 handle->ERROR_MESSAGE("%s", e.what());
1583 catch (...)
1585 handle->ERROR_MESSAGE("Unknown exception thrown");
1588 return NULL;
1591 Geometry *
1592 GEOSIntersection_r(GEOSContextHandle_t extHandle, const Geometry *g1, const Geometry *g2)
1594 if ( 0 == extHandle )
1596 return NULL;
1599 GEOSContextHandleInternal_t *handle = 0;
1600 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
1601 if ( 0 == handle->initialized )
1603 return NULL;
1608 GeomAutoPtr g3(BinaryOp(g1, g2, overlayOp(OverlayOp::opINTERSECTION)));
1609 return g3.release();
1611 // XXX: old version
1612 //Geometry *g3 = g1->intersection(g2);
1613 //return g3;
1615 catch (const std::exception &e)
1617 handle->ERROR_MESSAGE("%s", e.what());
1619 catch (...)
1621 handle->ERROR_MESSAGE("Unknown exception thrown");
1624 return NULL;
1627 Geometry *
1628 GEOSBuffer_r(GEOSContextHandle_t extHandle, const Geometry *g1, double width, int quadrantsegments)
1630 if ( 0 == extHandle )
1632 return NULL;
1635 GEOSContextHandleInternal_t *handle = 0;
1636 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
1637 if ( 0 == handle->initialized )
1639 return NULL;
1644 Geometry *g3 = g1->buffer(width, quadrantsegments);
1645 return g3;
1647 catch (const std::exception &e)
1649 handle->ERROR_MESSAGE("%s", e.what());
1651 catch (...)
1653 handle->ERROR_MESSAGE("Unknown exception thrown");
1656 return NULL;
1659 Geometry *
1660 GEOSBufferWithStyle_r(GEOSContextHandle_t extHandle, const Geometry *g1, double width, int quadsegs, int endCapStyle, int joinStyle, double mitreLimit)
1662 using geos::operation::buffer::BufferParameters;
1663 using geos::operation::buffer::BufferOp;
1664 using geos::util::IllegalArgumentException;
1666 if ( 0 == extHandle )
1668 return NULL;
1671 GEOSContextHandleInternal_t *handle = 0;
1672 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
1673 if ( 0 == handle->initialized )
1675 return NULL;
1680 BufferParameters bp;
1681 bp.setQuadrantSegments(quadsegs);
1683 if ( endCapStyle > BufferParameters::CAP_SQUARE )
1685 throw IllegalArgumentException("Invalid buffer endCap style");
1687 bp.setEndCapStyle(
1688 static_cast<BufferParameters::EndCapStyle>(endCapStyle)
1691 if ( joinStyle > BufferParameters::JOIN_BEVEL )
1693 throw IllegalArgumentException("Invalid buffer join style");
1695 bp.setJoinStyle(
1696 static_cast<BufferParameters::JoinStyle>(joinStyle)
1698 bp.setMitreLimit(mitreLimit);
1699 BufferOp op(g1, bp);
1700 Geometry *g3 = op.getResultGeometry(width);
1701 return g3;
1703 catch (const std::exception &e)
1705 handle->ERROR_MESSAGE("%s", e.what());
1707 catch (...)
1709 handle->ERROR_MESSAGE("Unknown exception thrown");
1712 return NULL;
1715 Geometry *
1716 GEOSOffsetCurve_r(GEOSContextHandle_t extHandle, const Geometry *g1, double width, int quadsegs, int joinStyle, double mitreLimit)
1718 if ( 0 == extHandle ) return NULL;
1720 GEOSContextHandleInternal_t *handle = 0;
1721 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
1722 if ( 0 == handle->initialized ) return NULL;
1726 BufferParameters bp;
1727 bp.setEndCapStyle( BufferParameters::CAP_FLAT );
1728 bp.setQuadrantSegments(quadsegs);
1730 if ( joinStyle > BufferParameters::JOIN_BEVEL )
1732 throw IllegalArgumentException("Invalid buffer join style");
1734 bp.setJoinStyle(
1735 static_cast<BufferParameters::JoinStyle>(joinStyle)
1737 bp.setMitreLimit(mitreLimit);
1739 bool isLeftSide = true;
1740 if ( width < 0 ) {
1741 isLeftSide = false;
1742 width = -width;
1744 BufferBuilder bufBuilder (bp);
1745 Geometry *g3 = bufBuilder.bufferLineSingleSided(g1, width, isLeftSide);
1747 return g3;
1749 catch (const std::exception &e)
1751 handle->ERROR_MESSAGE("%s", e.what());
1753 catch (...)
1755 handle->ERROR_MESSAGE("Unknown exception thrown");
1758 return NULL;
1761 /* @deprecated in 3.3.0 */
1762 Geometry *
1763 GEOSSingleSidedBuffer_r(GEOSContextHandle_t extHandle, const Geometry *g1, double width, int quadsegs, int joinStyle, double mitreLimit, int leftSide)
1765 if ( 0 == extHandle ) return NULL;
1767 GEOSContextHandleInternal_t *handle = 0;
1768 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
1769 if ( 0 == handle->initialized ) return NULL;
1773 BufferParameters bp;
1774 bp.setEndCapStyle( BufferParameters::CAP_FLAT );
1775 bp.setQuadrantSegments(quadsegs);
1777 if ( joinStyle > BufferParameters::JOIN_BEVEL )
1779 throw IllegalArgumentException("Invalid buffer join style");
1781 bp.setJoinStyle(
1782 static_cast<BufferParameters::JoinStyle>(joinStyle)
1784 bp.setMitreLimit(mitreLimit);
1786 bool isLeftSide = leftSide == 0 ? false : true;
1787 BufferBuilder bufBuilder (bp);
1788 Geometry *g3 = bufBuilder.bufferLineSingleSided(g1, width, isLeftSide);
1790 return g3;
1792 catch (const std::exception &e)
1794 handle->ERROR_MESSAGE("%s", e.what());
1796 catch (...)
1798 handle->ERROR_MESSAGE("Unknown exception thrown");
1801 return NULL;
1804 Geometry *
1805 GEOSConvexHull_r(GEOSContextHandle_t extHandle, const Geometry *g1)
1807 if ( 0 == extHandle )
1809 return NULL;
1812 GEOSContextHandleInternal_t *handle = 0;
1813 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
1814 if ( 0 == handle->initialized )
1816 return NULL;
1821 Geometry *g3 = g1->convexHull();
1822 return g3;
1824 catch (const std::exception &e)
1826 handle->ERROR_MESSAGE("%s", e.what());
1828 catch (...)
1830 handle->ERROR_MESSAGE("Unknown exception thrown");
1833 return NULL;
1836 Geometry *
1837 GEOSDifference_r(GEOSContextHandle_t extHandle, const Geometry *g1, const Geometry *g2)
1839 if ( 0 == extHandle )
1841 return NULL;
1844 GEOSContextHandleInternal_t *handle = 0;
1845 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
1846 if ( 0 == handle->initialized )
1848 return NULL;
1853 GeomAutoPtr g3(BinaryOp(g1, g2, overlayOp(OverlayOp::opDIFFERENCE)));
1854 return g3.release();
1856 // XXX: old version
1857 //Geometry *g3 = g1->difference(g2);
1858 //return g3;
1860 catch (const std::exception &e)
1862 handle->ERROR_MESSAGE("%s", e.what());
1864 catch (...)
1866 handle->ERROR_MESSAGE("Unknown exception thrown");
1869 return NULL;
1872 Geometry *
1873 GEOSBoundary_r(GEOSContextHandle_t extHandle, const Geometry *g1)
1875 if ( 0 == extHandle )
1877 return NULL;
1880 GEOSContextHandleInternal_t *handle = 0;
1881 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
1882 if ( 0 == handle->initialized )
1884 return NULL;
1889 Geometry *g3 = g1->getBoundary();
1890 return g3;
1892 catch (const std::exception &e)
1894 handle->ERROR_MESSAGE("%s", e.what());
1896 catch (...)
1898 handle->ERROR_MESSAGE("Unknown exception thrown");
1901 return NULL;
1904 Geometry *
1905 GEOSSymDifference_r(GEOSContextHandle_t extHandle, const Geometry *g1, const Geometry *g2)
1907 if ( 0 == extHandle )
1909 return NULL;
1912 GEOSContextHandleInternal_t *handle = 0;
1913 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
1914 if ( 0 == handle->initialized )
1916 return NULL;
1921 GeomAutoPtr g3 = BinaryOp(g1, g2, overlayOp(OverlayOp::opSYMDIFFERENCE));
1922 return g3.release();
1923 //Geometry *g3 = g1->symDifference(g2);
1924 //return g3;
1926 catch (const std::exception &e)
1928 handle->ERROR_MESSAGE("%s", e.what());
1929 return NULL;
1932 catch (...)
1934 handle->ERROR_MESSAGE("Unknown exception thrown");
1935 return NULL;
1939 Geometry *
1940 GEOSUnion_r(GEOSContextHandle_t extHandle, const Geometry *g1, const Geometry *g2)
1942 if ( 0 == extHandle )
1944 return NULL;
1947 GEOSContextHandleInternal_t *handle = 0;
1948 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
1949 if ( 0 == handle->initialized )
1951 return NULL;
1956 GeomAutoPtr g3 = BinaryOp(g1, g2, overlayOp(OverlayOp::opUNION));
1957 return g3.release();
1959 // XXX: old version
1960 //Geometry *g3 = g1->Union(g2);
1961 //return g3;
1963 catch (const std::exception &e)
1965 #if VERBOSE_EXCEPTIONS
1966 std::ostringstream s;
1967 s << "Exception on GEOSUnion with following inputs:" << std::endl;
1968 s << "A: "<<g1->toString() << std::endl;
1969 s << "B: "<<g2->toString() << std::endl;
1970 handle->NOTICE_MESSAGE("%s", s.str().c_str());
1971 #endif // VERBOSE_EXCEPTIONS
1972 handle->ERROR_MESSAGE("%s", e.what());
1974 catch (...)
1976 handle->ERROR_MESSAGE("Unknown exception thrown");
1979 return NULL;
1982 Geometry *
1983 GEOSUnaryUnion_r(GEOSContextHandle_t extHandle, const Geometry *g)
1985 if ( 0 == extHandle )
1987 return NULL;
1990 GEOSContextHandleInternal_t *handle = 0;
1991 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
1992 if ( 0 == handle->initialized )
1994 return NULL;
1999 GeomAutoPtr g3 ( g->Union() );
2000 return g3.release();
2002 catch (const std::exception &e)
2004 #if VERBOSE_EXCEPTIONS
2005 std::ostringstream s;
2006 s << "Exception on GEOSUnaryUnion with following inputs:" << std::endl;
2007 s << "A: "<<g1->toString() << std::endl;
2008 s << "B: "<<g2->toString() << std::endl;
2009 handle->NOTICE_MESSAGE("%s", s.str().c_str());
2010 #endif // VERBOSE_EXCEPTIONS
2011 handle->ERROR_MESSAGE("%s", e.what());
2013 catch (...)
2015 handle->ERROR_MESSAGE("Unknown exception thrown");
2018 return NULL;
2021 Geometry *
2022 GEOSUnionCascaded_r(GEOSContextHandle_t extHandle, const Geometry *g1)
2024 if ( 0 == extHandle )
2026 return NULL;
2029 GEOSContextHandleInternal_t *handle = 0;
2030 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
2031 if ( 0 == handle->initialized )
2033 return NULL;
2038 const geos::geom::MultiPolygon *p = dynamic_cast<const geos::geom::MultiPolygon *>(g1);
2039 if ( ! p )
2041 handle->ERROR_MESSAGE("Invalid argument (must be a MultiPolygon)");
2042 return NULL;
2045 using geos::operation::geounion::CascadedPolygonUnion;
2046 return CascadedPolygonUnion::Union(p);
2048 catch (const std::exception &e)
2050 handle->ERROR_MESSAGE("%s", e.what());
2052 catch (...)
2054 handle->ERROR_MESSAGE("Unknown exception thrown");
2057 return NULL;
2060 Geometry *
2061 GEOSPointOnSurface_r(GEOSContextHandle_t extHandle, const Geometry *g1)
2063 if ( 0 == extHandle )
2065 return NULL;
2068 GEOSContextHandleInternal_t *handle = 0;
2069 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
2070 if ( 0 == handle->initialized )
2072 return NULL;
2077 Geometry *ret = g1->getInteriorPoint();
2078 if ( ! ret )
2080 const GeometryFactory* gf = handle->geomFactory;
2081 // return an empty collection
2082 return gf->createGeometryCollection();
2084 return ret;
2086 catch (const std::exception &e)
2088 handle->ERROR_MESSAGE("%s", e.what());
2090 catch (...)
2092 handle->ERROR_MESSAGE("Unknown exception thrown");
2095 return NULL;
2098 //-------------------------------------------------------------------
2099 // memory management functions
2100 //------------------------------------------------------------------
2102 void
2103 GEOSGeom_destroy_r(GEOSContextHandle_t extHandle, Geometry *a)
2105 GEOSContextHandleInternal_t *handle = 0;
2107 // FIXME: mloskot: Does this try-catch around delete means that
2108 // destructors in GEOS may throw? If it does, this is a serious
2109 // violation of "never throw an exception from a destructor" principle
2113 delete a;
2115 catch (const std::exception &e)
2117 if ( 0 == extHandle )
2119 return;
2122 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
2123 if ( 0 == handle->initialized )
2125 return;
2128 handle->ERROR_MESSAGE("%s", e.what());
2130 catch (...)
2132 if ( 0 == extHandle )
2134 return;
2137 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
2138 if ( 0 == handle->initialized )
2140 return;
2143 handle->ERROR_MESSAGE("Unknown exception thrown");
2147 void
2148 GEOSSetSRID_r(GEOSContextHandle_t extHandle, Geometry *g, int srid)
2150 assert(0 != g);
2152 if ( 0 == extHandle )
2154 return;
2157 GEOSContextHandleInternal_t *handle = 0;
2158 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
2159 if ( 0 == handle->initialized )
2161 return;
2164 g->setSRID(srid);
2169 GEOSGetNumCoordinates_r(GEOSContextHandle_t extHandle, const Geometry *g)
2171 assert(0 != g);
2173 if ( 0 == extHandle )
2175 return -1;
2178 GEOSContextHandleInternal_t *handle = 0;
2179 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
2180 if ( 0 == handle->initialized )
2182 return -1;
2187 return static_cast<int>(g->getNumPoints());
2189 catch (const std::exception &e)
2191 handle->ERROR_MESSAGE("%s", e.what());
2193 catch (...)
2195 handle->ERROR_MESSAGE("Unknown exception thrown");
2198 return -1;
2202 * Return -1 on exception, 0 otherwise.
2203 * Converts Geometry to normal form (or canonical form).
2206 GEOSNormalize_r(GEOSContextHandle_t extHandle, Geometry *g)
2208 assert(0 != g);
2210 if ( 0 == extHandle )
2212 return -1;
2215 GEOSContextHandleInternal_t *handle = 0;
2216 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
2217 if ( 0 == handle->initialized )
2219 return -1;
2224 g->normalize();
2225 return 0; // SUCCESS
2227 catch (const std::exception &e)
2229 handle->ERROR_MESSAGE("%s", e.what());
2231 catch (...)
2233 handle->ERROR_MESSAGE("Unknown exception thrown");
2236 return -1;
2240 GEOSGetNumInteriorRings_r(GEOSContextHandle_t extHandle, const Geometry *g1)
2242 if ( 0 == extHandle )
2244 return -1;
2247 GEOSContextHandleInternal_t *handle = 0;
2248 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
2249 if ( 0 == handle->initialized )
2251 return -1;
2256 const Polygon *p = dynamic_cast<const Polygon *>(g1);
2257 if ( ! p )
2259 handle->ERROR_MESSAGE("Argument is not a Polygon");
2260 return -1;
2262 return static_cast<int>(p->getNumInteriorRing());
2264 catch (const std::exception &e)
2266 handle->ERROR_MESSAGE("%s", e.what());
2268 catch (...)
2270 handle->ERROR_MESSAGE("Unknown exception thrown");
2273 return -1;
2277 // returns -1 on error and 1 for non-multi geometries
2279 GEOSGetNumGeometries_r(GEOSContextHandle_t extHandle, const Geometry *g1)
2281 if ( 0 == extHandle )
2283 return -1;
2286 GEOSContextHandleInternal_t *handle = 0;
2287 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
2288 if ( 0 == handle->initialized )
2290 return -1;
2295 return static_cast<int>(g1->getNumGeometries());
2297 catch (const std::exception &e)
2299 handle->ERROR_MESSAGE("%s", e.what());
2301 catch (...)
2303 handle->ERROR_MESSAGE("Unknown exception thrown");
2306 return -1;
2311 * Call only on GEOMETRYCOLLECTION or MULTI*.
2312 * Return a pointer to the internal Geometry.
2314 const Geometry *
2315 GEOSGetGeometryN_r(GEOSContextHandle_t extHandle, const Geometry *g1, int n)
2317 if ( 0 == extHandle )
2319 return NULL;
2322 GEOSContextHandleInternal_t *handle = 0;
2323 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
2324 if ( 0 == handle->initialized )
2326 return NULL;
2331 return g1->getGeometryN(n);
2333 catch (const std::exception &e)
2335 handle->ERROR_MESSAGE("%s", e.what());
2337 catch (...)
2339 handle->ERROR_MESSAGE("Unknown exception thrown");
2342 return NULL;
2346 * Call only on LINESTRING
2347 * Returns NULL on exception
2349 Geometry *
2350 GEOSGeomGetPointN_r(GEOSContextHandle_t extHandle, const Geometry *g1, int n)
2352 if ( 0 == extHandle )
2354 return NULL;
2357 GEOSContextHandleInternal_t *handle = 0;
2358 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
2359 if ( 0 == handle->initialized )
2361 return NULL;
2366 using geos::geom::LineString;
2367 const LineString *ls = dynamic_cast<const LineString *>(g1);
2368 if ( ! ls )
2370 handle->ERROR_MESSAGE("Argument is not a LineString");
2371 return NULL;
2373 return ls->getPointN(n);
2375 catch (const std::exception &e)
2377 handle->ERROR_MESSAGE("%s", e.what());
2379 catch (...)
2381 handle->ERROR_MESSAGE("Unknown exception thrown");
2384 return NULL;
2388 * Call only on LINESTRING
2390 Geometry *
2391 GEOSGeomGetStartPoint_r(GEOSContextHandle_t extHandle, const Geometry *g1)
2393 if ( 0 == extHandle )
2395 return NULL;
2398 GEOSContextHandleInternal_t *handle = 0;
2399 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
2400 if ( 0 == handle->initialized )
2402 return NULL;
2407 using geos::geom::LineString;
2408 const LineString *ls = dynamic_cast<const LineString *>(g1);
2409 if ( ! ls )
2411 handle->ERROR_MESSAGE("Argument is not a LineString");
2412 return NULL;
2414 return ls->getStartPoint();
2416 catch (const std::exception &e)
2418 handle->ERROR_MESSAGE("%s", e.what());
2420 catch (...)
2422 handle->ERROR_MESSAGE("Unknown exception thrown");
2425 return NULL;
2429 * Call only on LINESTRING
2431 Geometry *
2432 GEOSGeomGetEndPoint_r(GEOSContextHandle_t extHandle, const Geometry *g1)
2434 if ( 0 == extHandle )
2436 return NULL;
2439 GEOSContextHandleInternal_t *handle = 0;
2440 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
2441 if ( 0 == handle->initialized )
2443 return NULL;
2448 using geos::geom::LineString;
2449 const LineString *ls = dynamic_cast<const LineString *>(g1);
2450 if ( ! ls )
2452 handle->ERROR_MESSAGE("Argument is not a LineString");
2453 return NULL;
2455 return ls->getEndPoint();
2457 catch (const std::exception &e)
2459 handle->ERROR_MESSAGE("%s", e.what());
2461 catch (...)
2463 handle->ERROR_MESSAGE("Unknown exception thrown");
2466 return NULL;
2470 * Call only on LINESTRING
2471 * return 2 on exception, 1 on true, 0 on false
2473 char
2474 GEOSisClosed_r(GEOSContextHandle_t extHandle, const Geometry *g1)
2476 if ( 0 == extHandle )
2478 return 2;
2481 GEOSContextHandleInternal_t *handle = 0;
2482 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
2483 if ( 0 == handle->initialized )
2485 return 2;
2490 using geos::geom::LineString;
2491 const LineString *ls = dynamic_cast<const LineString *>(g1);
2492 if ( ! ls )
2494 handle->ERROR_MESSAGE("Argument is not a LineString");
2495 return 2;
2497 return ls->isClosed();
2499 catch (const std::exception &e)
2501 handle->ERROR_MESSAGE("%s", e.what());
2503 catch (...)
2505 handle->ERROR_MESSAGE("Unknown exception thrown");
2508 return 2;
2512 * Call only on LINESTRING
2513 * return 0 on exception, otherwise 1
2516 GEOSGeomGetLength_r(GEOSContextHandle_t extHandle, const Geometry *g1, double *length)
2518 if ( 0 == extHandle )
2520 return 0;
2523 GEOSContextHandleInternal_t *handle = 0;
2524 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
2525 if ( 0 == handle->initialized )
2527 return 0;
2532 using geos::geom::LineString;
2533 const LineString *ls = dynamic_cast<const LineString *>(g1);
2534 if ( ! ls )
2536 handle->ERROR_MESSAGE("Argument is not a LineString");
2537 return 0;
2539 *length = ls->getLength();
2540 return 1;
2542 catch (const std::exception &e)
2544 handle->ERROR_MESSAGE("%s", e.what());
2546 catch (...)
2548 handle->ERROR_MESSAGE("Unknown exception thrown");
2551 return 0;
2555 * Call only on LINESTRING
2558 GEOSGeomGetNumPoints_r(GEOSContextHandle_t extHandle, const Geometry *g1)
2560 if ( 0 == extHandle )
2562 return -1;
2565 GEOSContextHandleInternal_t *handle = 0;
2566 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
2567 if ( 0 == handle->initialized )
2569 return -1;
2574 using geos::geom::LineString;
2575 const LineString *ls = dynamic_cast<const LineString *>(g1);
2576 if ( ! ls )
2578 handle->ERROR_MESSAGE("Argument is not a LineString");
2579 return -1;
2581 return ls->getNumPoints();
2583 catch (const std::exception &e)
2585 handle->ERROR_MESSAGE("%s", e.what());
2587 catch (...)
2589 handle->ERROR_MESSAGE("Unknown exception thrown");
2592 return -1;
2596 * For POINT
2597 * returns 0 on exception, otherwise 1
2600 GEOSGeomGetX_r(GEOSContextHandle_t extHandle, const Geometry *g1, double *x)
2602 if ( 0 == extHandle )
2604 return 0;
2607 GEOSContextHandleInternal_t *handle = 0;
2608 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
2609 if ( 0 == handle->initialized )
2611 return 0;
2616 using geos::geom::Point;
2617 const Point *po = dynamic_cast<const Point *>(g1);
2618 if ( ! po )
2620 handle->ERROR_MESSAGE("Argument is not a Point");
2621 return 0;
2623 *x = po->getX();
2624 return 1;
2626 catch (const std::exception &e)
2628 handle->ERROR_MESSAGE("%s", e.what());
2630 catch (...)
2632 handle->ERROR_MESSAGE("Unknown exception thrown");
2635 return 0;
2639 * For POINT
2640 * returns 0 on exception, otherwise 1
2643 GEOSGeomGetY_r(GEOSContextHandle_t extHandle, const Geometry *g1, double *y)
2645 if ( 0 == extHandle )
2647 return 0;
2650 GEOSContextHandleInternal_t *handle = 0;
2651 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
2652 if ( 0 == handle->initialized )
2654 return 0;
2659 using geos::geom::Point;
2660 const Point *po = dynamic_cast<const Point *>(g1);
2661 if ( ! po )
2663 handle->ERROR_MESSAGE("Argument is not a Point");
2664 return 0;
2666 *y = po->getY();
2667 return 1;
2669 catch (const std::exception &e)
2671 handle->ERROR_MESSAGE("%s", e.what());
2673 catch (...)
2675 handle->ERROR_MESSAGE("Unknown exception thrown");
2678 return 0;
2682 * Call only on polygon
2683 * Return a copy of the internal Geometry.
2685 const Geometry *
2686 GEOSGetExteriorRing_r(GEOSContextHandle_t extHandle, const Geometry *g1)
2688 if ( 0 == extHandle )
2690 return NULL;
2693 GEOSContextHandleInternal_t *handle = 0;
2694 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
2695 if ( 0 == handle->initialized )
2697 return NULL;
2702 const Polygon *p = dynamic_cast<const Polygon *>(g1);
2703 if ( ! p )
2705 handle->ERROR_MESSAGE("Invalid argument (must be a Polygon)");
2706 return NULL;
2708 return p->getExteriorRing();
2710 catch (const std::exception &e)
2712 handle->ERROR_MESSAGE("%s", e.what());
2714 catch (...)
2716 handle->ERROR_MESSAGE("Unknown exception thrown");
2719 return NULL;
2723 * Call only on polygon
2724 * Return a pointer to internal storage, do not destroy it.
2726 const Geometry *
2727 GEOSGetInteriorRingN_r(GEOSContextHandle_t extHandle, const Geometry *g1, int n)
2729 if ( 0 == extHandle )
2731 return NULL;
2734 GEOSContextHandleInternal_t *handle = 0;
2735 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
2736 if ( 0 == handle->initialized )
2738 return NULL;
2743 const Polygon *p = dynamic_cast<const Polygon *>(g1);
2744 if ( ! p )
2746 handle->ERROR_MESSAGE("Invalid argument (must be a Polygon)");
2747 return NULL;
2749 return p->getInteriorRingN(n);
2751 catch (const std::exception &e)
2753 handle->ERROR_MESSAGE("%s", e.what());
2755 catch (...)
2757 handle->ERROR_MESSAGE("Unknown exception thrown");
2760 return NULL;
2763 Geometry *
2764 GEOSGetCentroid_r(GEOSContextHandle_t extHandle, const Geometry *g)
2766 if ( 0 == extHandle )
2768 return NULL;
2771 GEOSContextHandleInternal_t *handle = 0;
2772 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
2773 if ( 0 == handle->initialized )
2775 return NULL;
2780 Geometry *ret = g->getCentroid();
2781 if (0 == ret)
2783 const GeometryFactory *gf = handle->geomFactory;
2784 return gf->createGeometryCollection();
2786 return ret;
2788 catch (const std::exception &e)
2790 handle->ERROR_MESSAGE("%s", e.what());
2792 catch (...)
2794 handle->ERROR_MESSAGE("Unknown exception thrown");
2797 return NULL;
2800 Geometry *
2801 GEOSGeom_createEmptyCollection_r(GEOSContextHandle_t extHandle, int type)
2803 if ( 0 == extHandle )
2805 return NULL;
2808 GEOSContextHandleInternal_t *handle = 0;
2809 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
2810 if ( 0 == handle->initialized )
2812 return NULL;
2815 #ifdef GEOS_DEBUG
2816 char buf[256];
2817 sprintf(buf, "createCollection: requested type %d, ngeoms: %d",
2818 type, ngeoms);
2819 handle->NOTICE_MESSAGE("%s", buf);// TODO: Can handle->NOTICE_MESSAGE format that directly?
2820 #endif
2824 const GeometryFactory* gf = handle->geomFactory;
2826 Geometry *g = 0;
2827 switch (type)
2829 case GEOS_GEOMETRYCOLLECTION:
2830 g = gf->createGeometryCollection();
2831 break;
2832 case GEOS_MULTIPOINT:
2833 g = gf->createMultiPoint();
2834 break;
2835 case GEOS_MULTILINESTRING:
2836 g = gf->createMultiLineString();
2837 break;
2838 case GEOS_MULTIPOLYGON:
2839 g = gf->createMultiPolygon();
2840 break;
2841 default:
2842 handle->ERROR_MESSAGE("Unsupported type request for GEOSGeom_createEmptyCollection_r");
2843 g = 0;
2847 return g;
2849 catch (const std::exception &e)
2851 handle->ERROR_MESSAGE("%s", e.what());
2853 catch (...)
2855 handle->ERROR_MESSAGE("Unknown exception thrown");
2858 return 0;
2861 Geometry *
2862 GEOSGeom_createCollection_r(GEOSContextHandle_t extHandle, int type, Geometry **geoms, unsigned int ngeoms)
2864 if ( 0 == extHandle )
2866 return NULL;
2869 GEOSContextHandleInternal_t *handle = 0;
2870 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
2871 if ( 0 == handle->initialized )
2873 return NULL;
2876 #ifdef GEOS_DEBUG
2877 char buf[256];
2878 sprintf(buf, "PostGIS2GEOS_collection: requested type %d, ngeoms: %d",
2879 type, ngeoms);
2880 handle->NOTICE_MESSAGE("%s", buf);// TODO: Can handle->NOTICE_MESSAGE format that directly?
2881 #endif
2885 const GeometryFactory* gf = handle->geomFactory;
2886 std::vector<Geometry*>* vgeoms = new std::vector<Geometry*>(geoms, geoms + ngeoms);
2888 Geometry *g = 0;
2889 switch (type)
2891 case GEOS_GEOMETRYCOLLECTION:
2892 g = gf->createGeometryCollection(vgeoms);
2893 break;
2894 case GEOS_MULTIPOINT:
2895 g = gf->createMultiPoint(vgeoms);
2896 break;
2897 case GEOS_MULTILINESTRING:
2898 g = gf->createMultiLineString(vgeoms);
2899 break;
2900 case GEOS_MULTIPOLYGON:
2901 g = gf->createMultiPolygon(vgeoms);
2902 break;
2903 default:
2904 handle->ERROR_MESSAGE("Unsupported type request for PostGIS2GEOS_collection");
2905 g = 0;
2909 return g;
2911 catch (const std::exception &e)
2913 handle->ERROR_MESSAGE("%s", e.what());
2915 catch (...)
2917 handle->ERROR_MESSAGE("Unknown exception thrown");
2920 return 0;
2923 Geometry *
2924 GEOSPolygonize_r(GEOSContextHandle_t extHandle, const Geometry * const * g, unsigned int ngeoms)
2926 if ( 0 == extHandle )
2928 return 0;
2931 GEOSContextHandleInternal_t *handle = 0;
2932 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
2933 if ( 0 == handle->initialized )
2935 return 0;
2938 Geometry *out = 0;
2942 // Polygonize
2943 using geos::operation::polygonize::Polygonizer;
2944 Polygonizer plgnzr;
2945 for (std::size_t i = 0; i < ngeoms; ++i)
2947 plgnzr.add(g[i]);
2950 #if GEOS_DEBUG
2951 handle->NOTICE_MESSAGE("geometry vector added to polygonizer");
2952 #endif
2954 std::vector<Polygon*> *polys = plgnzr.getPolygons();
2955 assert(0 != polys);
2957 #if GEOS_DEBUG
2958 handle->NOTICE_MESSAGE("output polygons got");
2959 #endif
2961 // We need a vector of Geometry pointers, not Polygon pointers.
2962 // STL vector doesn't allow transparent upcast of this
2963 // nature, so we explicitly convert.
2964 // (it's just a waste of processor and memory, btw)
2966 // XXX mloskot: Why not to extent GeometryFactory to accept
2967 // vector of polygons or extend Polygonizer to return list of Geometry*
2968 // or add a wrapper which semantic is similar to:
2969 // std::vector<as_polygon<Geometry*> >
2970 std::vector<Geometry*> *polyvec = new std::vector<Geometry *>(polys->size());
2972 for (std::size_t i = 0; i < polys->size(); ++i)
2974 (*polyvec)[i] = (*polys)[i];
2976 delete polys;
2977 polys = 0;
2979 const GeometryFactory *gf = handle->geomFactory;
2981 // The below takes ownership of the passed vector,
2982 // so we must *not* delete it
2983 out = gf->createGeometryCollection(polyvec);
2985 catch (const std::exception &e)
2987 handle->ERROR_MESSAGE("%s", e.what());
2989 catch (...)
2991 handle->ERROR_MESSAGE("Unknown exception thrown");
2994 return out;
2997 Geometry *
2998 GEOSPolygonizer_getCutEdges_r(GEOSContextHandle_t extHandle, const Geometry * const * g, unsigned int ngeoms)
3000 if ( 0 == extHandle )
3002 return 0;
3005 GEOSContextHandleInternal_t *handle = 0;
3006 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
3007 if ( 0 == handle->initialized )
3009 return 0;
3012 Geometry *out = 0;
3016 // Polygonize
3017 using geos::operation::polygonize::Polygonizer;
3018 Polygonizer plgnzr;
3019 for (std::size_t i = 0; i < ngeoms; ++i)
3021 plgnzr.add(g[i]);
3024 #if GEOS_DEBUG
3025 handle->NOTICE_MESSAGE("geometry vector added to polygonizer");
3026 #endif
3028 const std::vector<const LineString *>& lines = plgnzr.getCutEdges();
3030 #if GEOS_DEBUG
3031 handle->NOTICE_MESSAGE("output polygons got");
3032 #endif
3034 // We need a vector of Geometry pointers, not Polygon pointers.
3035 // STL vector doesn't allow transparent upcast of this
3036 // nature, so we explicitly convert.
3037 // (it's just a waste of processor and memory, btw)
3038 // XXX mloskot: See comment for GEOSPolygonize_r
3039 std::vector<Geometry*> *linevec = new std::vector<Geometry *>(lines.size());
3041 for (std::size_t i = 0, n=lines.size(); i < n; ++i)
3043 (*linevec)[i] = lines[i]->clone();
3046 const GeometryFactory *gf = handle->geomFactory;
3048 // The below takes ownership of the passed vector,
3049 // so we must *not* delete it
3050 out = gf->createGeometryCollection(linevec);
3052 catch (const std::exception &e)
3054 handle->ERROR_MESSAGE("%s", e.what());
3056 catch (...)
3058 handle->ERROR_MESSAGE("Unknown exception thrown");
3061 return out;
3064 Geometry *
3065 GEOSPolygonize_full_r(GEOSContextHandle_t extHandle, const Geometry* g,
3066 Geometry** cuts, Geometry** dangles, Geometry** invalid)
3068 if ( 0 == extHandle )
3070 return 0;
3073 GEOSContextHandleInternal_t *handle = 0;
3074 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
3075 if ( 0 == handle->initialized )
3077 return 0;
3082 // Polygonize
3083 using geos::operation::polygonize::Polygonizer;
3084 Polygonizer plgnzr;
3085 for (std::size_t i = 0; i <g->getNumGeometries(); ++i)
3087 plgnzr.add(g->getGeometryN(i));
3090 #if GEOS_DEBUG
3091 handle->NOTICE_MESSAGE("geometry vector added to polygonizer");
3092 #endif
3093 const GeometryFactory *gf = handle->geomFactory;
3095 if ( cuts ) {
3097 const std::vector<const LineString *>& lines = plgnzr.getCutEdges();
3098 std::vector<Geometry*> *linevec = new std::vector<Geometry *>(lines.size());
3099 for (std::size_t i = 0, n=lines.size(); i < n; ++i)
3101 (*linevec)[i] = lines[i]->clone();
3104 // The below takes ownership of the passed vector,
3105 // so we must *not* delete it
3106 *cuts = gf->createGeometryCollection(linevec);
3109 if ( dangles ) {
3111 const std::vector<const LineString *>& lines = plgnzr.getDangles();
3112 std::vector<Geometry*> *linevec = new std::vector<Geometry *>(lines.size());
3113 for (std::size_t i = 0, n=lines.size(); i < n; ++i)
3115 (*linevec)[i] = lines[i]->clone();
3118 // The below takes ownership of the passed vector,
3119 // so we must *not* delete it
3120 *dangles = gf->createGeometryCollection(linevec);
3123 if ( invalid ) {
3125 const std::vector<LineString *>& lines = plgnzr.getInvalidRingLines();
3126 std::vector<Geometry*> *linevec = new std::vector<Geometry *>(lines.size());
3127 for (std::size_t i = 0, n=lines.size(); i < n; ++i)
3129 (*linevec)[i] = lines[i]->clone();
3132 // The below takes ownership of the passed vector,
3133 // so we must *not* delete it
3134 *invalid = gf->createGeometryCollection(linevec);
3137 std::vector<Polygon*> *polys = plgnzr.getPolygons();
3138 std::vector<Geometry*> *polyvec = new std::vector<Geometry *>(polys->size());
3139 for (std::size_t i = 0; i < polys->size(); ++i)
3141 (*polyvec)[i] = (*polys)[i];
3143 delete polys;
3145 return gf->createGeometryCollection(polyvec);
3148 catch (const std::exception &e)
3150 handle->ERROR_MESSAGE("%s", e.what());
3151 return 0;
3153 catch (...)
3155 handle->ERROR_MESSAGE("Unknown exception thrown");
3156 return 0;
3160 Geometry *
3161 GEOSLineMerge_r(GEOSContextHandle_t extHandle, const Geometry *g)
3163 if ( 0 == extHandle )
3165 return 0;
3168 GEOSContextHandleInternal_t *handle = 0;
3169 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
3170 if ( 0 == handle->initialized )
3172 return 0;
3175 Geometry *out = 0;
3179 using geos::operation::linemerge::LineMerger;
3180 LineMerger lmrgr;
3181 lmrgr.add(g);
3183 std::vector<LineString *>* lines = lmrgr.getMergedLineStrings();
3184 assert(0 != lines);
3186 #if GEOS_DEBUG
3187 handle->NOTICE_MESSAGE("output lines got");
3188 #endif
3190 std::vector<Geometry *>*geoms = new std::vector<Geometry *>(lines->size());
3191 for (std::vector<Geometry *>::size_type i = 0; i < lines->size(); ++i)
3193 (*geoms)[i] = (*lines)[i];
3195 delete lines;
3196 lines = 0;
3198 const GeometryFactory *gf = handle->geomFactory;
3199 out = gf->buildGeometry(geoms);
3201 // XXX: old version
3202 //out = gf->createGeometryCollection(geoms);
3204 catch (const std::exception &e)
3206 handle->ERROR_MESSAGE("%s", e.what());
3208 catch (...)
3210 handle->ERROR_MESSAGE("Unknown exception thrown");
3213 return out;
3217 GEOSGetSRID_r(GEOSContextHandle_t extHandle, const Geometry *g)
3219 assert(0 != g);
3221 if ( 0 == extHandle )
3223 return 0;
3226 GEOSContextHandleInternal_t *handle = 0;
3227 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
3228 if ( 0 == handle->initialized )
3230 return 0;
3235 return g->getSRID();
3237 catch (const std::exception &e)
3239 handle->ERROR_MESSAGE("%s", e.what());
3241 catch (...)
3243 handle->ERROR_MESSAGE("Unknown exception thrown");
3246 return 0;
3249 const char* GEOSversion()
3251 return GEOS_CAPI_VERSION;
3254 const char* GEOSjtsport()
3256 return GEOS_JTS_PORT;
3259 char
3260 GEOSHasZ_r(GEOSContextHandle_t extHandle, const Geometry *g)
3262 assert(0 != g);
3264 if ( 0 == extHandle )
3266 return -1;
3269 GEOSContextHandleInternal_t *handle = 0;
3270 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
3271 if ( 0 == handle->initialized )
3273 return -1;
3276 if (g->isEmpty())
3278 return false;
3280 assert(0 != g->getCoordinate());
3282 double az = g->getCoordinate()->z;
3283 //handle->ERROR_MESSAGE("ZCoord: %g", az);
3285 return static_cast<char>(FINITE(az));
3289 GEOS_getWKBOutputDims_r(GEOSContextHandle_t extHandle)
3291 if ( 0 == extHandle )
3293 return -1;
3296 GEOSContextHandleInternal_t *handle = 0;
3297 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
3298 if ( 0 == handle->initialized )
3300 return -1;
3303 return handle->WKBOutputDims;
3307 GEOS_setWKBOutputDims_r(GEOSContextHandle_t extHandle, int newdims)
3309 if ( 0 == extHandle )
3311 return -1;
3314 GEOSContextHandleInternal_t *handle = 0;
3315 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
3316 if ( 0 == handle->initialized )
3318 return -1;
3321 if ( newdims < 2 || newdims > 3 )
3323 handle->ERROR_MESSAGE("WKB output dimensions out of range 2..3");
3326 const int olddims = handle->WKBOutputDims;
3327 handle->WKBOutputDims = newdims;
3329 return olddims;
3333 GEOS_getWKBByteOrder_r(GEOSContextHandle_t extHandle)
3335 if ( 0 == extHandle )
3337 return -1;
3340 GEOSContextHandleInternal_t *handle = 0;
3341 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
3342 if ( 0 == handle->initialized )
3344 return -1;
3347 return handle->WKBByteOrder;
3351 GEOS_setWKBByteOrder_r(GEOSContextHandle_t extHandle, int byteOrder)
3353 if ( 0 == extHandle )
3355 return -1;
3358 GEOSContextHandleInternal_t *handle = 0;
3359 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
3360 if ( 0 == handle->initialized )
3362 return -1;
3365 const int oldByteOrder = handle->WKBByteOrder;
3366 handle->WKBByteOrder = byteOrder;
3368 return oldByteOrder;
3372 CoordinateSequence *
3373 GEOSCoordSeq_create_r(GEOSContextHandle_t extHandle, unsigned int size, unsigned int dims)
3375 if ( 0 == extHandle )
3377 return NULL;
3380 GEOSContextHandleInternal_t *handle = 0;
3381 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
3382 if ( 0 == handle->initialized )
3384 return NULL;
3389 const GeometryFactory *gf = handle->geomFactory;
3390 return gf->getCoordinateSequenceFactory()->create(size, dims);
3392 catch (const std::exception &e)
3394 handle->ERROR_MESSAGE("%s", e.what());
3396 catch (...)
3398 handle->ERROR_MESSAGE("Unknown exception thrown");
3401 return NULL;
3405 GEOSCoordSeq_setOrdinate_r(GEOSContextHandle_t extHandle, CoordinateSequence *cs,
3406 unsigned int idx, unsigned int dim, double val)
3408 assert(0 != cs);
3409 if ( 0 == extHandle )
3411 return 0;
3414 GEOSContextHandleInternal_t *handle = 0;
3415 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
3416 if ( 0 == handle->initialized )
3418 return 0;
3423 cs->setOrdinate(static_cast<int>(idx), static_cast<int>(dim), val);
3424 return 1;
3426 catch (const std::exception &e)
3428 handle->ERROR_MESSAGE("%s", e.what());
3430 catch (...)
3432 handle->ERROR_MESSAGE("Unknown exception thrown");
3435 return 0;
3439 GEOSCoordSeq_setX_r(GEOSContextHandle_t extHandle, CoordinateSequence *s, unsigned int idx, double val)
3441 return GEOSCoordSeq_setOrdinate_r(extHandle, s, idx, 0, val);
3445 GEOSCoordSeq_setY_r(GEOSContextHandle_t extHandle, CoordinateSequence *s, unsigned int idx, double val)
3447 return GEOSCoordSeq_setOrdinate_r(extHandle, s, idx, 1, val);
3451 GEOSCoordSeq_setZ_r(GEOSContextHandle_t extHandle, CoordinateSequence *s, unsigned int idx, double val)
3453 return GEOSCoordSeq_setOrdinate_r(extHandle, s, idx, 2, val);
3456 CoordinateSequence *
3457 GEOSCoordSeq_clone_r(GEOSContextHandle_t extHandle, const CoordinateSequence *cs)
3459 assert(0 != cs);
3461 if ( 0 == extHandle )
3463 return NULL;
3466 GEOSContextHandleInternal_t *handle = 0;
3467 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
3468 if ( 0 == handle->initialized )
3470 return NULL;
3475 return cs->clone();
3477 catch (const std::exception &e)
3479 handle->ERROR_MESSAGE("%s", e.what());
3481 catch (...)
3483 handle->ERROR_MESSAGE("Unknown exception thrown");
3486 return NULL;
3490 GEOSCoordSeq_getOrdinate_r(GEOSContextHandle_t extHandle, const CoordinateSequence *cs,
3491 unsigned int idx, unsigned int dim, double *val)
3493 assert(0 != cs);
3494 assert(0 != val);
3496 if ( 0 == extHandle )
3498 return 0;
3501 GEOSContextHandleInternal_t *handle = 0;
3502 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
3503 if ( 0 == handle->initialized )
3505 return 0;
3510 double d = cs->getOrdinate(static_cast<int>(idx), static_cast<int>(dim));
3511 *val = d;
3513 return 1;
3515 catch (const std::exception &e)
3517 handle->ERROR_MESSAGE("%s", e.what());
3519 catch (...)
3521 handle->ERROR_MESSAGE("Unknown exception thrown");
3524 return 0;
3528 GEOSCoordSeq_getX_r(GEOSContextHandle_t extHandle, const CoordinateSequence *s, unsigned int idx, double *val)
3530 return GEOSCoordSeq_getOrdinate_r(extHandle, s, idx, 0, val);
3534 GEOSCoordSeq_getY_r(GEOSContextHandle_t extHandle, const CoordinateSequence *s, unsigned int idx, double *val)
3536 return GEOSCoordSeq_getOrdinate_r(extHandle, s, idx, 1, val);
3540 GEOSCoordSeq_getZ_r(GEOSContextHandle_t extHandle, const CoordinateSequence *s, unsigned int idx, double *val)
3542 return GEOSCoordSeq_getOrdinate_r(extHandle, s, idx, 2, val);
3546 GEOSCoordSeq_getSize_r(GEOSContextHandle_t extHandle, const CoordinateSequence *cs, unsigned int *size)
3548 assert(0 != cs);
3549 assert(0 != size);
3551 if ( 0 == extHandle )
3553 return 0;
3556 GEOSContextHandleInternal_t *handle = 0;
3557 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
3558 if ( 0 == handle->initialized )
3560 return 0;
3565 const std::size_t sz = cs->getSize();
3566 *size = static_cast<unsigned int>(sz);
3567 return 1;
3569 catch (const std::exception &e)
3571 handle->ERROR_MESSAGE("%s", e.what());
3573 catch (...)
3575 handle->ERROR_MESSAGE("Unknown exception thrown");
3578 return 0;
3582 GEOSCoordSeq_getDimensions_r(GEOSContextHandle_t extHandle, const CoordinateSequence *cs, unsigned int *dims)
3584 assert(0 != cs);
3585 assert(0 != dims);
3587 if ( 0 == extHandle )
3589 return 0;
3592 GEOSContextHandleInternal_t *handle = 0;
3593 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
3594 if ( 0 == handle->initialized )
3596 return 0;
3601 const std::size_t dim = cs->getDimension();
3602 *dims = static_cast<unsigned int>(dim);
3604 return 1;
3606 catch (const std::exception &e)
3608 handle->ERROR_MESSAGE("%s", e.what());
3611 catch (...)
3613 handle->ERROR_MESSAGE("Unknown exception thrown");
3616 return 0;
3619 void
3620 GEOSCoordSeq_destroy_r(GEOSContextHandle_t extHandle, CoordinateSequence *s)
3622 GEOSContextHandleInternal_t *handle = 0;
3626 delete s;
3628 catch (const std::exception &e)
3630 if ( 0 == extHandle )
3632 return;
3635 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
3636 if ( 0 == handle->initialized )
3638 return;
3641 handle->ERROR_MESSAGE("%s", e.what());
3643 catch (...)
3645 if ( 0 == extHandle )
3647 return;
3650 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
3651 if ( 0 == handle->initialized )
3653 return;
3656 handle->ERROR_MESSAGE("Unknown exception thrown");
3660 const CoordinateSequence *
3661 GEOSGeom_getCoordSeq_r(GEOSContextHandle_t extHandle, const Geometry *g)
3663 if ( 0 == extHandle )
3665 return 0;
3668 GEOSContextHandleInternal_t *handle = 0;
3669 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
3670 if ( 0 == handle->initialized )
3672 return 0;
3677 using geos::geom::Point;
3679 const LineString *ls = dynamic_cast<const LineString *>(g);
3680 if ( ls )
3682 return ls->getCoordinatesRO();
3685 const Point *p = dynamic_cast<const Point *>(g);
3686 if ( p )
3688 return p->getCoordinatesRO();
3691 handle->ERROR_MESSAGE("Geometry must be a Point or LineString");
3692 return 0;
3694 catch (const std::exception &e)
3696 handle->ERROR_MESSAGE("%s", e.what());
3698 catch (...)
3700 handle->ERROR_MESSAGE("Unknown exception thrown");
3703 return 0;
3706 Geometry *
3707 GEOSGeom_createEmptyPoint_r(GEOSContextHandle_t extHandle)
3709 if ( 0 == extHandle )
3711 return NULL;
3714 GEOSContextHandleInternal_t *handle = 0;
3715 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
3716 if ( 0 == handle->initialized )
3718 return NULL;
3723 const GeometryFactory *gf = handle->geomFactory;
3724 return gf->createPoint();
3726 catch (const std::exception &e)
3728 handle->ERROR_MESSAGE("%s", e.what());
3730 catch (...)
3732 handle->ERROR_MESSAGE("Unknown exception thrown");
3735 return NULL;
3738 Geometry *
3739 GEOSGeom_createPoint_r(GEOSContextHandle_t extHandle, CoordinateSequence *cs)
3741 if ( 0 == extHandle )
3743 return 0;
3746 GEOSContextHandleInternal_t *handle = 0;
3747 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
3748 if ( 0 == handle->initialized )
3750 return 0;
3755 const GeometryFactory *gf = handle->geomFactory;
3756 return gf->createPoint(cs);
3758 catch (const std::exception &e)
3760 handle->ERROR_MESSAGE("%s", e.what());
3762 catch (...)
3764 handle->ERROR_MESSAGE("Unknown exception thrown");
3767 return 0;
3770 Geometry *
3771 GEOSGeom_createLinearRing_r(GEOSContextHandle_t extHandle, CoordinateSequence *cs)
3773 if ( 0 == extHandle )
3775 return NULL;
3778 GEOSContextHandleInternal_t *handle = 0;
3779 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
3780 if ( 0 == handle->initialized )
3782 return NULL;
3787 const GeometryFactory *gf = handle->geomFactory;
3789 return gf->createLinearRing(cs);
3791 catch (const std::exception &e)
3793 handle->ERROR_MESSAGE("%s", e.what());
3795 catch (...)
3797 handle->ERROR_MESSAGE("Unknown exception thrown");
3800 return NULL;
3803 Geometry *
3804 GEOSGeom_createEmptyLineString_r(GEOSContextHandle_t extHandle)
3806 if ( 0 == extHandle )
3808 return NULL;
3811 GEOSContextHandleInternal_t *handle = 0;
3812 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
3813 if ( 0 == handle->initialized )
3815 return NULL;
3820 const GeometryFactory *gf = handle->geomFactory;
3822 return gf->createLineString();
3824 catch (const std::exception &e)
3826 handle->ERROR_MESSAGE("%s", e.what());
3828 catch (...)
3830 handle->ERROR_MESSAGE("Unknown exception thrown");
3833 return NULL;
3836 Geometry *
3837 GEOSGeom_createLineString_r(GEOSContextHandle_t extHandle, CoordinateSequence *cs)
3839 if ( 0 == extHandle )
3841 return NULL;
3844 GEOSContextHandleInternal_t *handle = 0;
3845 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
3846 if ( 0 == handle->initialized )
3848 return NULL;
3853 const GeometryFactory *gf = handle->geomFactory;
3855 return gf->createLineString(cs);
3857 catch (const std::exception &e)
3859 handle->ERROR_MESSAGE("%s", e.what());
3861 catch (...)
3863 handle->ERROR_MESSAGE("Unknown exception thrown");
3866 return NULL;
3869 Geometry *
3870 GEOSGeom_createEmptyPolygon_r(GEOSContextHandle_t extHandle)
3872 if ( 0 == extHandle )
3874 return NULL;
3877 GEOSContextHandleInternal_t *handle = 0;
3878 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
3879 if ( 0 == handle->initialized )
3881 return NULL;
3886 const GeometryFactory *gf = handle->geomFactory;
3887 return gf->createPolygon();
3889 catch (const std::exception &e)
3891 handle->ERROR_MESSAGE("%s", e.what());
3893 catch (...)
3895 handle->ERROR_MESSAGE("Unknown exception thrown");
3898 return NULL;
3901 Geometry *
3902 GEOSGeom_createPolygon_r(GEOSContextHandle_t extHandle, Geometry *shell, Geometry **holes, unsigned int nholes)
3904 // FIXME: holes must be non-nullptr or may be nullptr?
3905 //assert(0 != holes);
3907 if ( 0 == extHandle )
3909 return NULL;
3912 GEOSContextHandleInternal_t *handle = 0;
3913 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
3914 if ( 0 == handle->initialized )
3916 return NULL;
3921 using geos::geom::LinearRing;
3923 std::vector<Geometry *> *vholes = new std::vector<Geometry *>(holes, holes + nholes);
3925 LinearRing *nshell = dynamic_cast<LinearRing *>(shell);
3926 if ( ! nshell )
3928 handle->ERROR_MESSAGE("Shell is not a LinearRing");
3929 return NULL;
3931 const GeometryFactory *gf = handle->geomFactory;
3933 return gf->createPolygon(nshell, vholes);
3935 catch (const std::exception &e)
3937 handle->ERROR_MESSAGE("%s", e.what());
3939 catch (...)
3941 handle->ERROR_MESSAGE("Unknown exception thrown");
3944 return NULL;
3947 Geometry *
3948 GEOSGeom_clone_r(GEOSContextHandle_t extHandle, const Geometry *g)
3950 assert(0 != g);
3952 if ( 0 == extHandle )
3954 return NULL;
3957 GEOSContextHandleInternal_t *handle = 0;
3958 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
3959 if ( 0 == handle->initialized )
3961 return NULL;
3966 return g->clone();
3968 catch (const std::exception &e)
3970 handle->ERROR_MESSAGE("%s", e.what());
3972 catch (...)
3974 handle->ERROR_MESSAGE("Unknown exception thrown");
3977 return NULL;
3981 GEOSGeom_getDimensions_r(GEOSContextHandle_t extHandle, const Geometry *g)
3983 if ( 0 == extHandle )
3985 return 0;
3988 GEOSContextHandleInternal_t *handle = 0;
3989 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
3990 if ( 0 == handle->initialized )
3992 return 0;
3997 return (int) g->getDimension();
3999 catch (const std::exception &e)
4001 handle->ERROR_MESSAGE("%s", e.what());
4003 catch (...)
4005 handle->ERROR_MESSAGE("Unknown exception thrown");
4008 return 0;
4012 GEOSGeom_getCoordinateDimension_r(GEOSContextHandle_t extHandle, const Geometry *g)
4014 if ( 0 == extHandle )
4016 return 0;
4019 GEOSContextHandleInternal_t *handle = 0;
4020 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
4021 if ( 0 == handle->initialized )
4023 return 0;
4028 return g->getCoordinateDimension();
4030 catch (const std::exception &e)
4032 handle->ERROR_MESSAGE("%s", e.what());
4034 catch (...)
4036 handle->ERROR_MESSAGE("Unknown exception thrown");
4039 return 0;
4042 Geometry *
4043 GEOSSimplify_r(GEOSContextHandle_t extHandle, const Geometry *g1, double tolerance)
4045 if ( 0 == extHandle )
4047 return NULL;
4050 GEOSContextHandleInternal_t *handle = 0;
4051 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
4052 if ( 0 == handle->initialized )
4054 return NULL;
4059 using namespace geos::simplify;
4060 Geometry::AutoPtr g(DouglasPeuckerSimplifier::simplify(g1, tolerance));
4061 return g.release();
4063 catch (const std::exception &e)
4065 handle->ERROR_MESSAGE("%s", e.what());
4067 catch (...)
4069 handle->ERROR_MESSAGE("Unknown exception thrown");
4072 return NULL;
4075 Geometry *
4076 GEOSTopologyPreserveSimplify_r(GEOSContextHandle_t extHandle, const Geometry *g1, double tolerance)
4078 if ( 0 == extHandle )
4080 return NULL;
4083 GEOSContextHandleInternal_t *handle = 0;
4084 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
4085 if ( 0 == handle->initialized )
4087 return NULL;
4092 using namespace geos::simplify;
4093 Geometry::AutoPtr g(TopologyPreservingSimplifier::simplify(g1, tolerance));
4094 return g.release();
4096 catch (const std::exception &e)
4098 handle->ERROR_MESSAGE("%s", e.what());
4100 catch (...)
4102 handle->ERROR_MESSAGE("Unknown exception thrown");
4105 return NULL;
4109 /* WKT Reader */
4110 WKTReader *
4111 GEOSWKTReader_create_r(GEOSContextHandle_t extHandle)
4113 if ( 0 == extHandle )
4115 return NULL;
4118 GEOSContextHandleInternal_t *handle = 0;
4119 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
4120 if ( 0 == handle->initialized )
4122 return NULL;
4127 using geos::io::WKTReader;
4128 return new WKTReader((GeometryFactory*)handle->geomFactory);
4130 catch (const std::exception &e)
4132 handle->ERROR_MESSAGE("%s", e.what());
4134 catch (...)
4136 handle->ERROR_MESSAGE("Unknown exception thrown");
4139 return NULL;
4142 void
4143 GEOSWKTReader_destroy_r(GEOSContextHandle_t extHandle, WKTReader *reader)
4145 GEOSContextHandleInternal_t *handle = 0;
4149 delete reader;
4151 catch (const std::exception &e)
4153 if ( 0 == extHandle )
4155 return;
4158 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
4159 if ( 0 == handle->initialized )
4161 return;
4164 handle->ERROR_MESSAGE("%s", e.what());
4166 catch (...)
4168 if ( 0 == extHandle )
4170 return;
4173 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
4174 if ( 0 == handle->initialized )
4176 return;
4179 handle->ERROR_MESSAGE("Unknown exception thrown");
4184 Geometry*
4185 GEOSWKTReader_read_r(GEOSContextHandle_t extHandle, WKTReader *reader, const char *wkt)
4187 assert(0 != reader);
4189 if ( 0 == extHandle )
4191 return 0;
4194 GEOSContextHandleInternal_t *handle = 0;
4195 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
4196 if ( 0 == handle->initialized )
4198 return 0;
4203 const std::string wktstring(wkt);
4204 Geometry *g = reader->read(wktstring);
4205 return g;
4207 catch (const std::exception &e)
4209 handle->ERROR_MESSAGE("%s", e.what());
4211 catch (...)
4213 handle->ERROR_MESSAGE("Unknown exception thrown");
4216 return 0;
4219 /* WKT Writer */
4220 WKTWriter *
4221 GEOSWKTWriter_create_r(GEOSContextHandle_t extHandle)
4223 if ( 0 == extHandle )
4225 return 0;
4228 GEOSContextHandleInternal_t *handle = 0;
4229 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
4230 if ( 0 == handle->initialized )
4232 return 0;
4237 using geos::io::WKTWriter;
4238 return new WKTWriter();
4240 catch (const std::exception &e)
4242 handle->ERROR_MESSAGE("%s", e.what());
4244 catch (...)
4246 handle->ERROR_MESSAGE("Unknown exception thrown");
4249 return 0;
4252 void
4253 GEOSWKTWriter_destroy_r(GEOSContextHandle_t extHandle, WKTWriter *Writer)
4256 GEOSContextHandleInternal_t *handle = 0;
4260 delete Writer;
4262 catch (const std::exception &e)
4264 if ( 0 == extHandle )
4266 return;
4269 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
4270 if ( 0 == handle->initialized )
4272 return;
4275 handle->ERROR_MESSAGE("%s", e.what());
4277 catch (...)
4279 if ( 0 == extHandle )
4281 return;
4284 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
4285 if ( 0 == handle->initialized )
4287 return;
4290 handle->ERROR_MESSAGE("Unknown exception thrown");
4295 char*
4296 GEOSWKTWriter_write_r(GEOSContextHandle_t extHandle, WKTWriter *writer, const Geometry *geom)
4298 assert(0 != writer);
4300 if ( 0 == extHandle )
4302 return NULL;
4305 GEOSContextHandleInternal_t *handle = 0;
4306 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
4307 if ( 0 == handle->initialized )
4309 return NULL;
4314 std::string sgeom(writer->write(geom));
4315 char *result = gstrdup(sgeom);
4316 return result;
4318 catch (const std::exception &e)
4320 handle->ERROR_MESSAGE("%s", e.what());
4322 catch (...)
4324 handle->ERROR_MESSAGE("Unknown exception thrown");
4327 return NULL;
4330 void
4331 GEOSWKTWriter_setTrim_r(GEOSContextHandle_t extHandle, WKTWriter *writer, char trim)
4333 assert(0 != writer);
4335 if ( 0 == extHandle )
4337 return;
4340 GEOSContextHandleInternal_t *handle = 0;
4341 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
4342 if ( 0 == handle->initialized )
4344 return;
4347 writer->setTrim(0 != trim);
4350 void
4351 GEOSWKTWriter_setRoundingPrecision_r(GEOSContextHandle_t extHandle, WKTWriter *writer, int precision)
4353 assert(0 != writer);
4355 if ( 0 == extHandle )
4357 return;
4360 GEOSContextHandleInternal_t *handle = 0;
4361 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
4362 if ( 0 == handle->initialized )
4364 return;
4367 writer->setRoundingPrecision(precision);
4370 void
4371 GEOSWKTWriter_setOutputDimension_r(GEOSContextHandle_t extHandle, WKTWriter *writer, int dim)
4373 assert(0 != writer);
4375 if ( 0 == extHandle )
4377 return;
4380 GEOSContextHandleInternal_t *handle = 0;
4381 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
4382 if ( 0 == handle->initialized )
4384 return;
4389 writer->setOutputDimension(dim);
4391 catch (const std::exception &e)
4393 handle->ERROR_MESSAGE("%s", e.what());
4395 catch (...)
4397 handle->ERROR_MESSAGE("Unknown exception thrown");
4402 GEOSWKTWriter_getOutputDimension_r(GEOSContextHandle_t extHandle, WKTWriter *writer)
4404 assert(0 != writer);
4406 if ( 0 == extHandle )
4408 return -1;
4411 GEOSContextHandleInternal_t *handle = 0;
4412 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
4413 if ( 0 == handle->initialized )
4415 return -1;
4418 int dim = -1;
4422 dim = writer->getOutputDimension();
4424 catch (const std::exception &e)
4426 handle->ERROR_MESSAGE("%s", e.what());
4428 catch (...)
4430 handle->ERROR_MESSAGE("Unknown exception thrown");
4433 return dim;
4436 void
4437 GEOSWKTWriter_setOld3D_r(GEOSContextHandle_t extHandle, WKTWriter *writer, int useOld3D)
4439 assert(0 != writer);
4441 if ( 0 == extHandle )
4443 return;
4446 GEOSContextHandleInternal_t *handle = 0;
4447 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
4448 if ( 0 == handle->initialized )
4450 return;
4453 writer->setOld3D(0 != useOld3D);
4456 /* WKB Reader */
4457 WKBReader *
4458 GEOSWKBReader_create_r(GEOSContextHandle_t extHandle)
4460 if ( 0 == extHandle )
4462 return NULL;
4465 GEOSContextHandleInternal_t *handle = 0;
4466 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
4467 if ( 0 == handle->initialized )
4469 return NULL;
4472 using geos::io::WKBReader;
4475 return new WKBReader(*(GeometryFactory*)handle->geomFactory);
4477 catch (const std::exception &e)
4479 handle->ERROR_MESSAGE("%s", e.what());
4481 catch (...)
4483 handle->ERROR_MESSAGE("Unknown exception thrown");
4486 return NULL;
4489 void
4490 GEOSWKBReader_destroy_r(GEOSContextHandle_t extHandle, WKBReader *reader)
4492 GEOSContextHandleInternal_t *handle = 0;
4496 delete reader;
4498 catch (const std::exception &e)
4500 if ( 0 == extHandle )
4502 return;
4505 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
4506 if ( 0 == handle->initialized )
4508 return;
4511 handle->ERROR_MESSAGE("%s", e.what());
4513 catch (...)
4515 if ( 0 == extHandle )
4517 return;
4520 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
4521 if ( 0 == handle->initialized )
4523 return;
4526 handle->ERROR_MESSAGE("Unknown exception thrown");
4531 Geometry*
4532 GEOSWKBReader_read_r(GEOSContextHandle_t extHandle, WKBReader *reader, const unsigned char *wkb, size_t size)
4534 assert(0 != reader);
4535 assert(0 != wkb);
4537 if ( 0 == extHandle )
4539 return 0;
4542 GEOSContextHandleInternal_t *handle = 0;
4543 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
4544 if ( 0 == handle->initialized )
4546 return 0;
4551 std::string wkbstring(reinterpret_cast<const char*>(wkb), size); // make it binary !
4552 std::istringstream is(std::ios_base::binary);
4553 is.str(wkbstring);
4554 is.seekg(0, std::ios::beg); // rewind reader pointer
4556 Geometry *g = reader->read(is);
4557 return g;
4559 catch (const std::exception &e)
4561 handle->ERROR_MESSAGE("%s", e.what());
4563 catch (...)
4565 handle->ERROR_MESSAGE("Unknown exception thrown");
4568 return 0;
4571 Geometry*
4572 GEOSWKBReader_readHEX_r(GEOSContextHandle_t extHandle, WKBReader *reader, const unsigned char *hex, size_t size)
4574 assert(0 != reader);
4575 assert(0 != hex);
4577 if ( 0 == extHandle )
4579 return 0;
4582 GEOSContextHandleInternal_t *handle = 0;
4583 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
4584 if ( 0 == handle->initialized )
4586 return 0;
4591 std::string hexstring(reinterpret_cast<const char*>(hex), size);
4592 std::istringstream is(std::ios_base::binary);
4593 is.str(hexstring);
4594 is.seekg(0, std::ios::beg); // rewind reader pointer
4596 Geometry *g = reader->readHEX(is);
4597 return g;
4599 catch (const std::exception &e)
4601 handle->ERROR_MESSAGE("%s", e.what());
4603 catch (...)
4605 handle->ERROR_MESSAGE("Unknown exception thrown");
4608 return 0;
4611 /* WKB Writer */
4612 WKBWriter *
4613 GEOSWKBWriter_create_r(GEOSContextHandle_t extHandle)
4615 if ( 0 == extHandle )
4617 return NULL;
4620 GEOSContextHandleInternal_t *handle = 0;
4621 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
4622 if ( 0 == handle->initialized )
4624 return NULL;
4629 using geos::io::WKBWriter;
4630 return new WKBWriter();
4632 catch (const std::exception &e)
4634 handle->ERROR_MESSAGE("%s", e.what());
4636 catch (...)
4638 handle->ERROR_MESSAGE("Unknown exception thrown");
4641 return NULL;
4644 void
4645 GEOSWKBWriter_destroy_r(GEOSContextHandle_t extHandle, WKBWriter *Writer)
4647 GEOSContextHandleInternal_t *handle = 0;
4651 delete Writer;
4653 catch (const std::exception &e)
4655 if ( 0 == extHandle )
4657 return;
4660 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
4661 if ( 0 == handle->initialized )
4663 return;
4666 handle->ERROR_MESSAGE("%s", e.what());
4668 catch (...)
4670 if ( 0 == extHandle )
4672 return;
4675 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
4676 if ( 0 == handle->initialized )
4678 return;
4681 handle->ERROR_MESSAGE("Unknown exception thrown");
4686 /* The caller owns the result */
4687 unsigned char*
4688 GEOSWKBWriter_write_r(GEOSContextHandle_t extHandle, WKBWriter *writer, const Geometry *geom, size_t *size)
4690 assert(0 != writer);
4691 assert(0 != geom);
4692 assert(0 != size);
4694 if ( 0 == extHandle )
4696 return NULL;
4699 GEOSContextHandleInternal_t *handle = 0;
4700 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
4701 if ( 0 == handle->initialized )
4703 return NULL;
4708 std::ostringstream os(std::ios_base::binary);
4709 writer->write(*geom, os);
4710 std::string wkbstring(os.str());
4711 const std::size_t len = wkbstring.length();
4713 unsigned char *result = NULL;
4714 result = (unsigned char*) std::malloc(len);
4715 std::memcpy(result, wkbstring.c_str(), len);
4716 *size = len;
4717 return result;
4719 catch (const std::exception &e)
4721 handle->ERROR_MESSAGE("%s", e.what());
4723 catch (...)
4725 handle->ERROR_MESSAGE("Unknown exception thrown");
4727 return NULL;
4730 /* The caller owns the result */
4731 unsigned char*
4732 GEOSWKBWriter_writeHEX_r(GEOSContextHandle_t extHandle, WKBWriter *writer, const Geometry *geom, size_t *size)
4734 assert(0 != writer);
4735 assert(0 != geom);
4736 assert(0 != size);
4738 if ( 0 == extHandle )
4740 return NULL;
4743 GEOSContextHandleInternal_t *handle = 0;
4744 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
4745 if ( 0 == handle->initialized )
4747 return NULL;
4752 std::ostringstream os(std::ios_base::binary);
4753 writer->writeHEX(*geom, os);
4754 std::string wkbstring(os.str());
4755 const std::size_t len = wkbstring.length();
4757 unsigned char *result = NULL;
4758 result = (unsigned char*) std::malloc(len);
4759 std::memcpy(result, wkbstring.c_str(), len);
4760 *size = len;
4761 return result;
4763 catch (const std::exception &e)
4765 handle->ERROR_MESSAGE("%s", e.what());
4767 catch (...)
4769 handle->ERROR_MESSAGE("Unknown exception thrown");
4772 return NULL;
4776 GEOSWKBWriter_getOutputDimension_r(GEOSContextHandle_t extHandle, const GEOSWKBWriter* writer)
4778 assert(0 != writer);
4780 if ( 0 == extHandle )
4782 return 0;
4785 int ret = 0;
4787 GEOSContextHandleInternal_t *handle = 0;
4788 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
4789 if ( 0 != handle->initialized )
4793 ret = writer->getOutputDimension();
4795 catch (...)
4797 handle->ERROR_MESSAGE("Unknown exception thrown");
4801 return ret;
4804 void
4805 GEOSWKBWriter_setOutputDimension_r(GEOSContextHandle_t extHandle, GEOSWKBWriter* writer, int newDimension)
4807 assert(0 != writer);
4809 if ( 0 == extHandle )
4811 return;
4814 GEOSContextHandleInternal_t *handle = 0;
4815 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
4816 if ( 0 != handle->initialized )
4820 writer->setOutputDimension(newDimension);
4822 catch (const std::exception &e)
4824 handle->ERROR_MESSAGE("%s", e.what());
4826 catch (...)
4828 handle->ERROR_MESSAGE("Unknown exception thrown");
4834 GEOSWKBWriter_getByteOrder_r(GEOSContextHandle_t extHandle, const GEOSWKBWriter* writer)
4836 assert(0 != writer);
4838 if ( 0 == extHandle )
4840 return 0;
4843 int ret = 0;
4845 GEOSContextHandleInternal_t *handle = 0;
4846 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
4847 if ( 0 != handle->initialized )
4851 ret = writer->getByteOrder();
4854 catch (...)
4856 handle->ERROR_MESSAGE("Unknown exception thrown");
4860 return ret;
4863 void
4864 GEOSWKBWriter_setByteOrder_r(GEOSContextHandle_t extHandle, GEOSWKBWriter* writer, int newByteOrder)
4866 assert(0 != writer);
4868 if ( 0 == extHandle )
4870 return;
4873 GEOSContextHandleInternal_t *handle = 0;
4874 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
4875 if ( 0 != handle->initialized )
4879 writer->setByteOrder(newByteOrder);
4881 catch (const std::exception &e)
4883 handle->ERROR_MESSAGE("%s", e.what());
4885 catch (...)
4887 handle->ERROR_MESSAGE("Unknown exception thrown");
4892 char
4893 GEOSWKBWriter_getIncludeSRID_r(GEOSContextHandle_t extHandle, const GEOSWKBWriter* writer)
4895 assert(0 != writer);
4897 if ( 0 == extHandle )
4899 return -1;
4902 int ret = -1;
4904 GEOSContextHandleInternal_t *handle = 0;
4905 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
4906 if ( 0 != handle->initialized )
4910 int srid = writer->getIncludeSRID();
4911 ret = static_cast<char>(srid);
4913 catch (...)
4915 handle->ERROR_MESSAGE("Unknown exception thrown");
4919 return static_cast<char>(ret);
4922 void
4923 GEOSWKBWriter_setIncludeSRID_r(GEOSContextHandle_t extHandle, GEOSWKBWriter* writer, const char newIncludeSRID)
4925 assert(0 != writer);
4927 if ( 0 == extHandle )
4929 return;
4932 GEOSContextHandleInternal_t *handle = 0;
4933 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
4934 if ( 0 != handle->initialized )
4938 writer->setIncludeSRID(newIncludeSRID);
4940 catch (...)
4942 handle->ERROR_MESSAGE("Unknown exception thrown");
4948 //-----------------------------------------------------------------
4949 // Prepared Geometry
4950 //-----------------------------------------------------------------
4952 const geos::geom::prep::PreparedGeometry*
4953 GEOSPrepare_r(GEOSContextHandle_t extHandle, const Geometry *g)
4955 if ( 0 == extHandle )
4957 return 0;
4960 GEOSContextHandleInternal_t *handle = 0;
4961 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
4962 if ( 0 == handle->initialized )
4964 return 0;
4967 const geos::geom::prep::PreparedGeometry* prep = 0;
4971 prep = geos::geom::prep::PreparedGeometryFactory::prepare(g);
4973 catch (const std::exception &e)
4975 handle->ERROR_MESSAGE("%s", e.what());
4977 catch (...)
4979 handle->ERROR_MESSAGE("Unknown exception thrown");
4982 return prep;
4985 void
4986 GEOSPreparedGeom_destroy_r(GEOSContextHandle_t extHandle, const geos::geom::prep::PreparedGeometry *a)
4988 GEOSContextHandleInternal_t *handle = 0;
4992 delete a;
4994 catch (const std::exception &e)
4996 if ( 0 == extHandle )
4998 return;
5001 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
5002 if ( 0 == handle->initialized )
5004 return;
5007 handle->ERROR_MESSAGE("%s", e.what());
5009 catch (...)
5011 if ( 0 == extHandle )
5013 return;
5016 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
5017 if ( 0 == handle->initialized )
5019 return;
5022 handle->ERROR_MESSAGE("Unknown exception thrown");
5026 char
5027 GEOSPreparedContains_r(GEOSContextHandle_t extHandle,
5028 const geos::geom::prep::PreparedGeometry *pg, const Geometry *g)
5030 assert(0 != pg);
5031 assert(0 != g);
5033 if ( 0 == extHandle )
5035 return 2;
5038 GEOSContextHandleInternal_t *handle = 0;
5039 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
5040 if ( 0 == handle->initialized )
5042 return 2;
5045 try
5047 bool result = pg->contains(g);
5048 return result;
5050 catch (const std::exception &e)
5052 handle->ERROR_MESSAGE("%s", e.what());
5054 catch (...)
5056 handle->ERROR_MESSAGE("Unknown exception thrown");
5059 return 2;
5062 char
5063 GEOSPreparedContainsProperly_r(GEOSContextHandle_t extHandle,
5064 const geos::geom::prep::PreparedGeometry *pg, const Geometry *g)
5066 assert(0 != pg);
5067 assert(0 != g);
5069 if ( 0 == extHandle )
5071 return 2;
5074 GEOSContextHandleInternal_t *handle = 0;
5075 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
5076 if ( 0 == handle->initialized )
5078 return 2;
5081 try
5083 bool result = pg->containsProperly(g);
5084 return result;
5086 catch (const std::exception &e)
5088 handle->ERROR_MESSAGE("%s", e.what());
5090 catch (...)
5092 handle->ERROR_MESSAGE("Unknown exception thrown");
5095 return 2;
5098 char
5099 GEOSPreparedCoveredBy_r(GEOSContextHandle_t extHandle,
5100 const geos::geom::prep::PreparedGeometry *pg, const Geometry *g)
5102 assert(0 != pg);
5103 assert(0 != g);
5105 if ( 0 == extHandle )
5107 return 2;
5110 GEOSContextHandleInternal_t *handle = 0;
5111 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
5112 if ( 0 == handle->initialized )
5114 return 2;
5117 try
5119 bool result = pg->coveredBy(g);
5120 return result;
5122 catch (const std::exception &e)
5124 handle->ERROR_MESSAGE("%s", e.what());
5126 catch (...)
5128 handle->ERROR_MESSAGE("Unknown exception thrown");
5131 return 2;
5134 char
5135 GEOSPreparedCovers_r(GEOSContextHandle_t extHandle,
5136 const geos::geom::prep::PreparedGeometry *pg, const Geometry *g)
5138 assert(0 != pg);
5139 assert(0 != g);
5141 if ( 0 == extHandle )
5143 return 2;
5146 GEOSContextHandleInternal_t *handle = 0;
5147 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
5148 if ( 0 == handle->initialized )
5150 return 2;
5153 try
5155 bool result = pg->covers(g);
5156 return result;
5158 catch (const std::exception &e)
5160 handle->ERROR_MESSAGE("%s", e.what());
5162 catch (...)
5164 handle->ERROR_MESSAGE("Unknown exception thrown");
5167 return 2;
5170 char
5171 GEOSPreparedCrosses_r(GEOSContextHandle_t extHandle,
5172 const geos::geom::prep::PreparedGeometry *pg, const Geometry *g)
5174 assert(0 != pg);
5175 assert(0 != g);
5177 if ( 0 == extHandle )
5179 return 2;
5182 GEOSContextHandleInternal_t *handle = 0;
5183 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
5184 if ( 0 == handle->initialized )
5186 return 2;
5189 try
5191 bool result = pg->crosses(g);
5192 return result;
5194 catch (const std::exception &e)
5196 handle->ERROR_MESSAGE("%s", e.what());
5198 catch (...)
5200 handle->ERROR_MESSAGE("Unknown exception thrown");
5203 return 2;
5206 char
5207 GEOSPreparedDisjoint_r(GEOSContextHandle_t extHandle,
5208 const geos::geom::prep::PreparedGeometry *pg, const Geometry *g)
5210 assert(0 != pg);
5211 assert(0 != g);
5213 if ( 0 == extHandle )
5215 return 2;
5218 GEOSContextHandleInternal_t *handle = 0;
5219 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
5220 if ( 0 == handle->initialized )
5222 return 2;
5225 try
5227 bool result = pg->disjoint(g);
5228 return result;
5230 catch (const std::exception &e)
5232 handle->ERROR_MESSAGE("%s", e.what());
5234 catch (...)
5236 handle->ERROR_MESSAGE("Unknown exception thrown");
5239 return 2;
5242 char
5243 GEOSPreparedIntersects_r(GEOSContextHandle_t extHandle,
5244 const geos::geom::prep::PreparedGeometry *pg, const Geometry *g)
5246 assert(0 != pg);
5247 assert(0 != g);
5249 if ( 0 == extHandle )
5251 return 2;
5254 GEOSContextHandleInternal_t *handle = 0;
5255 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
5256 if ( 0 == handle->initialized )
5258 return 2;
5261 try
5263 bool result = pg->intersects(g);
5264 return result;
5266 catch (const std::exception &e)
5268 handle->ERROR_MESSAGE("%s", e.what());
5270 catch (...)
5272 handle->ERROR_MESSAGE("Unknown exception thrown");
5275 return 2;
5278 char
5279 GEOSPreparedOverlaps_r(GEOSContextHandle_t extHandle,
5280 const geos::geom::prep::PreparedGeometry *pg, const Geometry *g)
5282 assert(0 != pg);
5283 assert(0 != g);
5285 if ( 0 == extHandle )
5287 return 2;
5290 GEOSContextHandleInternal_t *handle = 0;
5291 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
5292 if ( 0 == handle->initialized )
5294 return 2;
5297 try
5299 bool result = pg->overlaps(g);
5300 return result;
5302 catch (const std::exception &e)
5304 handle->ERROR_MESSAGE("%s", e.what());
5306 catch (...)
5308 handle->ERROR_MESSAGE("Unknown exception thrown");
5311 return 2;
5314 char
5315 GEOSPreparedTouches_r(GEOSContextHandle_t extHandle,
5316 const geos::geom::prep::PreparedGeometry *pg, const Geometry *g)
5318 assert(0 != pg);
5319 assert(0 != g);
5321 if ( 0 == extHandle )
5323 return 2;
5326 GEOSContextHandleInternal_t *handle = 0;
5327 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
5328 if ( 0 == handle->initialized )
5330 return 2;
5333 try
5335 bool result = pg->touches(g);
5336 return result;
5338 catch (const std::exception &e)
5340 handle->ERROR_MESSAGE("%s", e.what());
5342 catch (...)
5344 handle->ERROR_MESSAGE("Unknown exception thrown");
5347 return 2;
5350 char
5351 GEOSPreparedWithin_r(GEOSContextHandle_t extHandle,
5352 const geos::geom::prep::PreparedGeometry *pg, const Geometry *g)
5354 assert(0 != pg);
5355 assert(0 != g);
5357 if ( 0 == extHandle )
5359 return 2;
5362 GEOSContextHandleInternal_t *handle = 0;
5363 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
5364 if ( 0 == handle->initialized )
5366 return 2;
5369 try
5371 bool result = pg->within(g);
5372 return result;
5374 catch (const std::exception &e)
5376 handle->ERROR_MESSAGE("%s", e.what());
5378 catch (...)
5380 handle->ERROR_MESSAGE("Unknown exception thrown");
5383 return 2;
5386 //-----------------------------------------------------------------
5387 // STRtree
5388 //-----------------------------------------------------------------
5390 geos::index::strtree::STRtree *
5391 GEOSSTRtree_create_r(GEOSContextHandle_t extHandle,
5392 size_t nodeCapacity)
5394 if ( 0 == extHandle )
5396 return 0;
5399 GEOSContextHandleInternal_t *handle = 0;
5400 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
5401 if ( 0 == handle->initialized )
5403 return 0;
5406 geos::index::strtree::STRtree *tree = 0;
5410 tree = new geos::index::strtree::STRtree(nodeCapacity);
5412 catch (const std::exception &e)
5414 handle->ERROR_MESSAGE("%s", e.what());
5416 catch (...)
5418 handle->ERROR_MESSAGE("Unknown exception thrown");
5421 return tree;
5424 void
5425 GEOSSTRtree_insert_r(GEOSContextHandle_t extHandle,
5426 geos::index::strtree::STRtree *tree,
5427 const geos::geom::Geometry *g,
5428 void *item)
5430 GEOSContextHandleInternal_t *handle = 0;
5431 assert(tree != 0);
5432 assert(g != 0);
5436 tree->insert(g->getEnvelopeInternal(), item);
5438 catch (const std::exception &e)
5440 if ( 0 == extHandle )
5442 return;
5445 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
5446 if ( 0 == handle->initialized )
5448 return;
5451 handle->ERROR_MESSAGE("%s", e.what());
5453 catch (...)
5455 if ( 0 == extHandle )
5457 return;
5460 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
5461 if ( 0 == handle->initialized )
5463 return;
5466 handle->ERROR_MESSAGE("Unknown exception thrown");
5470 void
5471 GEOSSTRtree_query_r(GEOSContextHandle_t extHandle,
5472 geos::index::strtree::STRtree *tree,
5473 const geos::geom::Geometry *g,
5474 GEOSQueryCallback callback,
5475 void *userdata)
5477 GEOSContextHandleInternal_t *handle = 0;
5478 assert(tree != 0);
5479 assert(g != 0);
5480 assert(callback != 0);
5484 CAPI_ItemVisitor visitor(callback, userdata);
5485 tree->query(g->getEnvelopeInternal(), visitor);
5487 catch (const std::exception &e)
5489 if ( 0 == extHandle )
5491 return;
5494 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
5495 if ( 0 == handle->initialized )
5497 return;
5500 handle->ERROR_MESSAGE("%s", e.what());
5502 catch (...)
5504 if ( 0 == extHandle )
5506 return;
5509 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
5510 if ( 0 == handle->initialized )
5512 return;
5515 handle->ERROR_MESSAGE("Unknown exception thrown");
5519 void
5520 GEOSSTRtree_iterate_r(GEOSContextHandle_t extHandle,
5521 geos::index::strtree::STRtree *tree,
5522 GEOSQueryCallback callback,
5523 void *userdata)
5525 GEOSContextHandleInternal_t *handle = 0;
5526 assert(tree != 0);
5527 assert(callback != 0);
5531 CAPI_ItemVisitor visitor(callback, userdata);
5532 tree->iterate(visitor);
5534 catch (const std::exception &e)
5536 if ( 0 == extHandle )
5538 return;
5541 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
5542 if ( 0 == handle->initialized )
5544 return;
5547 handle->ERROR_MESSAGE("%s", e.what());
5549 catch (...)
5551 if ( 0 == extHandle )
5553 return;
5556 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
5557 if ( 0 == handle->initialized )
5559 return;
5562 handle->ERROR_MESSAGE("Unknown exception thrown");
5566 char
5567 GEOSSTRtree_remove_r(GEOSContextHandle_t extHandle,
5568 geos::index::strtree::STRtree *tree,
5569 const geos::geom::Geometry *g,
5570 void *item)
5572 assert(0 != tree);
5573 assert(0 != g);
5575 if ( 0 == extHandle )
5577 return 2;
5580 GEOSContextHandleInternal_t *handle = 0;
5581 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
5582 if ( 0 == handle->initialized )
5584 return 2;
5587 try
5589 bool result = tree->remove(g->getEnvelopeInternal(), item);
5590 return result;
5592 catch (const std::exception &e)
5594 handle->ERROR_MESSAGE("%s", e.what());
5596 catch (...)
5598 handle->ERROR_MESSAGE("Unknown exception thrown");
5601 return 2;
5604 void
5605 GEOSSTRtree_destroy_r(GEOSContextHandle_t extHandle,
5606 geos::index::strtree::STRtree *tree)
5608 GEOSContextHandleInternal_t *handle = 0;
5612 delete tree;
5614 catch (const std::exception &e)
5616 if ( 0 == extHandle )
5618 return;
5621 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
5622 if ( 0 == handle->initialized )
5624 return;
5627 handle->ERROR_MESSAGE("%s", e.what());
5629 catch (...)
5631 if ( 0 == extHandle )
5633 return;
5636 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
5637 if ( 0 == handle->initialized )
5639 return;
5642 handle->ERROR_MESSAGE("Unknown exception thrown");
5646 double
5647 GEOSProject_r(GEOSContextHandle_t extHandle,
5648 const Geometry *g,
5649 const Geometry *p)
5651 if ( 0 == extHandle ) return -1.0;
5652 GEOSContextHandleInternal_t *handle =
5653 reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
5654 if ( handle->initialized == 0 ) return -1.0;
5656 const geos::geom::Point* point = dynamic_cast<const geos::geom::Point*>(p);
5657 if (!point) {
5658 handle->ERROR_MESSAGE("third argument of GEOSProject_r must be Point*");
5659 return -1.0;
5662 const geos::geom::Coordinate* inputPt = p->getCoordinate();
5664 try {
5665 return geos::linearref::LengthIndexedLine(g).project(*inputPt);
5666 } catch (const std::exception &e) {
5667 handle->ERROR_MESSAGE("%s", e.what());
5668 return -1.0;
5669 } catch (...) {
5670 handle->ERROR_MESSAGE("Unknown exception thrown");
5671 return -1.0;
5676 Geometry*
5677 GEOSInterpolate_r(GEOSContextHandle_t extHandle, const Geometry *g, double d)
5679 if ( 0 == extHandle ) return 0;
5680 GEOSContextHandleInternal_t *handle =
5681 reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
5682 if ( handle->initialized == 0 ) return 0;
5684 try {
5685 geos::linearref::LengthIndexedLine lil(g);
5686 geos::geom::Coordinate coord = lil.extractPoint(d);
5687 const GeometryFactory *gf = handle->geomFactory;
5688 Geometry* point = gf->createPoint(coord);
5689 return point;
5690 } catch (const std::exception &e) {
5691 handle->ERROR_MESSAGE("%s", e.what());
5692 return 0;
5693 } catch (...) {
5694 handle->ERROR_MESSAGE("Unknown exception thrown");
5695 return 0;
5700 double
5701 GEOSProjectNormalized_r(GEOSContextHandle_t extHandle, const Geometry *g,
5702 const Geometry *p)
5705 double length;
5706 GEOSLength_r(extHandle, g, &length);
5707 return GEOSProject_r(extHandle, g, p) / length;
5711 Geometry*
5712 GEOSInterpolateNormalized_r(GEOSContextHandle_t extHandle, const Geometry *g,
5713 double d)
5715 double length;
5716 GEOSLength_r(extHandle, g, &length);
5717 return GEOSInterpolate_r(extHandle, g, d * length);
5720 GEOSGeometry*
5721 GEOSGeom_extractUniquePoints_r(GEOSContextHandle_t extHandle,
5722 const GEOSGeometry* g)
5724 if ( 0 == extHandle ) return 0;
5725 GEOSContextHandleInternal_t *handle = 0;
5726 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
5727 if ( handle->initialized == 0 ) return 0;
5729 using namespace geos::geom;
5730 using namespace geos::util;
5735 /* 1: extract points */
5736 std::vector<const Coordinate*> coords;
5737 UniqueCoordinateArrayFilter filter(coords);
5738 g->apply_ro(&filter);
5740 /* 2: for each point, create a geometry and put into a vector */
5741 std::vector<Geometry*>* points = new std::vector<Geometry*>();
5742 points->reserve(coords.size());
5743 const GeometryFactory* factory = g->getFactory();
5744 for (std::vector<const Coordinate*>::iterator it=coords.begin(),
5745 itE=coords.end();
5746 it != itE; ++it)
5748 Geometry* point = factory->createPoint(*(*it));
5749 points->push_back(point);
5752 /* 3: create a multipoint */
5753 return factory->createMultiPoint(points);
5756 catch (const std::exception &e)
5758 handle->ERROR_MESSAGE("%s", e.what());
5759 return 0;
5761 catch (...)
5763 handle->ERROR_MESSAGE("Unknown exception thrown");
5764 return 0;
5768 int GEOSOrientationIndex_r(GEOSContextHandle_t extHandle,
5769 double Ax, double Ay, double Bx, double By, double Px, double Py)
5771 GEOSContextHandleInternal_t *handle = 0;
5773 using geos::geom::Coordinate;
5774 using geos::algorithm::CGAlgorithms;
5776 if ( 0 == extHandle )
5778 return 2;
5781 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
5782 if ( 0 == handle->initialized )
5784 return 2;
5789 Coordinate A(Ax, Ay);
5790 Coordinate B(Bx, By);
5791 Coordinate P(Px, Py);
5792 return CGAlgorithms::orientationIndex(A, B, P);
5794 catch (const std::exception &e)
5796 handle->ERROR_MESSAGE("%s", e.what());
5797 return 2;
5799 catch (...)
5801 handle->ERROR_MESSAGE("Unknown exception thrown");
5802 return 2;
5806 GEOSGeometry *
5807 GEOSSharedPaths_r(GEOSContextHandle_t extHandle, const GEOSGeometry* g1, const GEOSGeometry* g2)
5809 using namespace geos::operation::sharedpaths;
5811 if ( 0 == extHandle ) return 0;
5812 GEOSContextHandleInternal_t *handle =
5813 reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
5814 if ( handle->initialized == 0 ) return 0;
5816 SharedPathsOp::PathList forw, back;
5817 try {
5818 SharedPathsOp::sharedPathsOp(*g1, *g2, forw, back);
5820 catch (const std::exception &e)
5822 SharedPathsOp::clearEdges(forw);
5823 SharedPathsOp::clearEdges(back);
5824 handle->ERROR_MESSAGE("%s", e.what());
5825 return 0;
5827 catch (...)
5829 SharedPathsOp::clearEdges(forw);
5830 SharedPathsOp::clearEdges(back);
5831 handle->ERROR_MESSAGE("Unknown exception thrown");
5832 return 0;
5835 // Now forw and back have the geoms we want to use to construct
5836 // our output GeometryCollections...
5838 const GeometryFactory* factory = g1->getFactory();
5839 size_t count;
5841 std::auto_ptr< std::vector<Geometry*> > out1(
5842 new std::vector<Geometry*>()
5844 count = forw.size();
5845 out1->reserve(count);
5846 for (size_t i=0; i<count; ++i) {
5847 out1->push_back(forw[i]);
5849 std::auto_ptr<Geometry> out1g (
5850 factory->createMultiLineString(out1.release())
5853 std::auto_ptr< std::vector<Geometry*> > out2(
5854 new std::vector<Geometry*>()
5856 count = back.size();
5857 out2->reserve(count);
5858 for (size_t i=0; i<count; ++i) {
5859 out2->push_back(back[i]);
5861 std::auto_ptr<Geometry> out2g (
5862 factory->createMultiLineString(out2.release())
5865 std::auto_ptr< std::vector<Geometry*> > out(
5866 new std::vector<Geometry*>()
5868 out->reserve(2);
5869 out->push_back(out1g.release());
5870 out->push_back(out2g.release());
5872 std::auto_ptr<Geometry> outg (
5873 factory->createGeometryCollection(out.release())
5876 return outg.release();
5880 GEOSGeometry *
5881 GEOSSnap_r(GEOSContextHandle_t extHandle, const GEOSGeometry* g1,
5882 const GEOSGeometry* g2, double tolerance)
5884 using namespace geos::operation::overlay::snap;
5886 if ( 0 == extHandle ) return 0;
5887 GEOSContextHandleInternal_t *handle =
5888 reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
5889 if ( handle->initialized == 0 ) return 0;
5891 try{
5892 GeometrySnapper snapper( *g1 );
5893 std::auto_ptr<Geometry> ret = snapper.snapTo(*g2, tolerance);
5894 return ret.release();
5896 catch (const std::exception &e)
5898 handle->ERROR_MESSAGE("%s", e.what());
5899 return 0;
5901 catch (...)
5903 handle->ERROR_MESSAGE("Unknown exception thrown");
5904 return 0;
5908 BufferParameters *
5909 GEOSBufferParams_create_r(GEOSContextHandle_t extHandle)
5911 if ( 0 == extHandle ) return NULL;
5913 GEOSContextHandleInternal_t *handle = 0;
5914 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
5915 if ( 0 == handle->initialized ) return NULL;
5919 BufferParameters *p = new BufferParameters();
5920 return p;
5922 catch (const std::exception &e)
5924 handle->ERROR_MESSAGE("%s", e.what());
5926 catch (...)
5928 handle->ERROR_MESSAGE("Unknown exception thrown");
5931 return 0;
5934 void
5935 GEOSBufferParams_destroy_r(GEOSContextHandle_t extHandle, BufferParameters* p)
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" */