Geometry methods do use BinaryOp internally + shortcuts (#542)
[geos.git] / capi / geos_ts_c.cpp
blob96513f98049c3ad15b65fbfce1f0cd9eddbbcd33
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/overlay/snap/GeometrySnapper.h>
54 #include <geos/operation/union/CascadedPolygonUnion.h>
55 #include <geos/operation/buffer/BufferOp.h>
56 #include <geos/operation/buffer/BufferParameters.h>
57 #include <geos/operation/buffer/BufferBuilder.h>
58 #include <geos/operation/relate/RelateOp.h>
59 #include <geos/operation/sharedpaths/SharedPathsOp.h>
60 #include <geos/linearref/LengthIndexedLine.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 return g1->intersection(g2);
1610 catch (const std::exception &e)
1612 handle->ERROR_MESSAGE("%s", e.what());
1614 catch (...)
1616 handle->ERROR_MESSAGE("Unknown exception thrown");
1619 return NULL;
1622 Geometry *
1623 GEOSBuffer_r(GEOSContextHandle_t extHandle, const Geometry *g1, double width, int quadrantsegments)
1625 if ( 0 == extHandle )
1627 return NULL;
1630 GEOSContextHandleInternal_t *handle = 0;
1631 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
1632 if ( 0 == handle->initialized )
1634 return NULL;
1639 Geometry *g3 = g1->buffer(width, quadrantsegments);
1640 return g3;
1642 catch (const std::exception &e)
1644 handle->ERROR_MESSAGE("%s", e.what());
1646 catch (...)
1648 handle->ERROR_MESSAGE("Unknown exception thrown");
1651 return NULL;
1654 Geometry *
1655 GEOSBufferWithStyle_r(GEOSContextHandle_t extHandle, const Geometry *g1, double width, int quadsegs, int endCapStyle, int joinStyle, double mitreLimit)
1657 using geos::operation::buffer::BufferParameters;
1658 using geos::operation::buffer::BufferOp;
1659 using geos::util::IllegalArgumentException;
1661 if ( 0 == extHandle )
1663 return NULL;
1666 GEOSContextHandleInternal_t *handle = 0;
1667 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
1668 if ( 0 == handle->initialized )
1670 return NULL;
1675 BufferParameters bp;
1676 bp.setQuadrantSegments(quadsegs);
1678 if ( endCapStyle > BufferParameters::CAP_SQUARE )
1680 throw IllegalArgumentException("Invalid buffer endCap style");
1682 bp.setEndCapStyle(
1683 static_cast<BufferParameters::EndCapStyle>(endCapStyle)
1686 if ( joinStyle > BufferParameters::JOIN_BEVEL )
1688 throw IllegalArgumentException("Invalid buffer join style");
1690 bp.setJoinStyle(
1691 static_cast<BufferParameters::JoinStyle>(joinStyle)
1693 bp.setMitreLimit(mitreLimit);
1694 BufferOp op(g1, bp);
1695 Geometry *g3 = op.getResultGeometry(width);
1696 return g3;
1698 catch (const std::exception &e)
1700 handle->ERROR_MESSAGE("%s", e.what());
1702 catch (...)
1704 handle->ERROR_MESSAGE("Unknown exception thrown");
1707 return NULL;
1710 Geometry *
1711 GEOSOffsetCurve_r(GEOSContextHandle_t extHandle, const Geometry *g1, double width, int quadsegs, int joinStyle, double mitreLimit)
1713 if ( 0 == extHandle ) return NULL;
1715 GEOSContextHandleInternal_t *handle = 0;
1716 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
1717 if ( 0 == handle->initialized ) return NULL;
1721 BufferParameters bp;
1722 bp.setEndCapStyle( BufferParameters::CAP_FLAT );
1723 bp.setQuadrantSegments(quadsegs);
1725 if ( joinStyle > BufferParameters::JOIN_BEVEL )
1727 throw IllegalArgumentException("Invalid buffer join style");
1729 bp.setJoinStyle(
1730 static_cast<BufferParameters::JoinStyle>(joinStyle)
1732 bp.setMitreLimit(mitreLimit);
1734 bool isLeftSide = true;
1735 if ( width < 0 ) {
1736 isLeftSide = false;
1737 width = -width;
1739 BufferBuilder bufBuilder (bp);
1740 Geometry *g3 = bufBuilder.bufferLineSingleSided(g1, width, isLeftSide);
1742 return g3;
1744 catch (const std::exception &e)
1746 handle->ERROR_MESSAGE("%s", e.what());
1748 catch (...)
1750 handle->ERROR_MESSAGE("Unknown exception thrown");
1753 return NULL;
1756 /* @deprecated in 3.3.0 */
1757 Geometry *
1758 GEOSSingleSidedBuffer_r(GEOSContextHandle_t extHandle, const Geometry *g1, double width, int quadsegs, int joinStyle, double mitreLimit, int leftSide)
1760 if ( 0 == extHandle ) return NULL;
1762 GEOSContextHandleInternal_t *handle = 0;
1763 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
1764 if ( 0 == handle->initialized ) return NULL;
1768 BufferParameters bp;
1769 bp.setEndCapStyle( BufferParameters::CAP_FLAT );
1770 bp.setQuadrantSegments(quadsegs);
1772 if ( joinStyle > BufferParameters::JOIN_BEVEL )
1774 throw IllegalArgumentException("Invalid buffer join style");
1776 bp.setJoinStyle(
1777 static_cast<BufferParameters::JoinStyle>(joinStyle)
1779 bp.setMitreLimit(mitreLimit);
1781 bool isLeftSide = leftSide == 0 ? false : true;
1782 BufferBuilder bufBuilder (bp);
1783 Geometry *g3 = bufBuilder.bufferLineSingleSided(g1, width, isLeftSide);
1785 return g3;
1787 catch (const std::exception &e)
1789 handle->ERROR_MESSAGE("%s", e.what());
1791 catch (...)
1793 handle->ERROR_MESSAGE("Unknown exception thrown");
1796 return NULL;
1799 Geometry *
1800 GEOSConvexHull_r(GEOSContextHandle_t extHandle, const Geometry *g1)
1802 if ( 0 == extHandle )
1804 return NULL;
1807 GEOSContextHandleInternal_t *handle = 0;
1808 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
1809 if ( 0 == handle->initialized )
1811 return NULL;
1816 Geometry *g3 = g1->convexHull();
1817 return g3;
1819 catch (const std::exception &e)
1821 handle->ERROR_MESSAGE("%s", e.what());
1823 catch (...)
1825 handle->ERROR_MESSAGE("Unknown exception thrown");
1828 return NULL;
1831 Geometry *
1832 GEOSDifference_r(GEOSContextHandle_t extHandle, const Geometry *g1, const Geometry *g2)
1834 if ( 0 == extHandle )
1836 return NULL;
1839 GEOSContextHandleInternal_t *handle = 0;
1840 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
1841 if ( 0 == handle->initialized )
1843 return NULL;
1848 return g1->difference(g2);
1850 catch (const std::exception &e)
1852 handle->ERROR_MESSAGE("%s", e.what());
1854 catch (...)
1856 handle->ERROR_MESSAGE("Unknown exception thrown");
1859 return NULL;
1862 Geometry *
1863 GEOSBoundary_r(GEOSContextHandle_t extHandle, const Geometry *g1)
1865 if ( 0 == extHandle )
1867 return NULL;
1870 GEOSContextHandleInternal_t *handle = 0;
1871 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
1872 if ( 0 == handle->initialized )
1874 return NULL;
1879 Geometry *g3 = g1->getBoundary();
1880 return g3;
1882 catch (const std::exception &e)
1884 handle->ERROR_MESSAGE("%s", e.what());
1886 catch (...)
1888 handle->ERROR_MESSAGE("Unknown exception thrown");
1891 return NULL;
1894 Geometry *
1895 GEOSSymDifference_r(GEOSContextHandle_t extHandle, const Geometry *g1, const Geometry *g2)
1897 if ( 0 == extHandle )
1899 return NULL;
1902 GEOSContextHandleInternal_t *handle = 0;
1903 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
1904 if ( 0 == handle->initialized )
1906 return NULL;
1911 return g1->symDifference(g2);
1913 catch (const std::exception &e)
1915 handle->ERROR_MESSAGE("%s", e.what());
1916 return NULL;
1919 catch (...)
1921 handle->ERROR_MESSAGE("Unknown exception thrown");
1922 return NULL;
1926 Geometry *
1927 GEOSUnion_r(GEOSContextHandle_t extHandle, const Geometry *g1, const Geometry *g2)
1929 if ( 0 == extHandle )
1931 return NULL;
1934 GEOSContextHandleInternal_t *handle = 0;
1935 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
1936 if ( 0 == handle->initialized )
1938 return NULL;
1943 return g1->Union(g2);
1945 catch (const std::exception &e)
1947 #if VERBOSE_EXCEPTIONS
1948 std::ostringstream s;
1949 s << "Exception on GEOSUnion with following inputs:" << std::endl;
1950 s << "A: "<<g1->toString() << std::endl;
1951 s << "B: "<<g2->toString() << std::endl;
1952 handle->NOTICE_MESSAGE("%s", s.str().c_str());
1953 #endif // VERBOSE_EXCEPTIONS
1954 handle->ERROR_MESSAGE("%s", e.what());
1956 catch (...)
1958 handle->ERROR_MESSAGE("Unknown exception thrown");
1961 return NULL;
1964 Geometry *
1965 GEOSUnaryUnion_r(GEOSContextHandle_t extHandle, const Geometry *g)
1967 if ( 0 == extHandle )
1969 return NULL;
1972 GEOSContextHandleInternal_t *handle = 0;
1973 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
1974 if ( 0 == handle->initialized )
1976 return NULL;
1981 GeomAutoPtr g3 ( g->Union() );
1982 return g3.release();
1984 catch (const std::exception &e)
1986 #if VERBOSE_EXCEPTIONS
1987 std::ostringstream s;
1988 s << "Exception on GEOSUnaryUnion with following inputs:" << std::endl;
1989 s << "A: "<<g1->toString() << std::endl;
1990 s << "B: "<<g2->toString() << std::endl;
1991 handle->NOTICE_MESSAGE("%s", s.str().c_str());
1992 #endif // VERBOSE_EXCEPTIONS
1993 handle->ERROR_MESSAGE("%s", e.what());
1995 catch (...)
1997 handle->ERROR_MESSAGE("Unknown exception thrown");
2000 return NULL;
2003 Geometry *
2004 GEOSUnionCascaded_r(GEOSContextHandle_t extHandle, const Geometry *g1)
2006 if ( 0 == extHandle )
2008 return NULL;
2011 GEOSContextHandleInternal_t *handle = 0;
2012 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
2013 if ( 0 == handle->initialized )
2015 return NULL;
2020 const geos::geom::MultiPolygon *p = dynamic_cast<const geos::geom::MultiPolygon *>(g1);
2021 if ( ! p )
2023 handle->ERROR_MESSAGE("Invalid argument (must be a MultiPolygon)");
2024 return NULL;
2027 using geos::operation::geounion::CascadedPolygonUnion;
2028 return CascadedPolygonUnion::Union(p);
2030 catch (const std::exception &e)
2032 handle->ERROR_MESSAGE("%s", e.what());
2034 catch (...)
2036 handle->ERROR_MESSAGE("Unknown exception thrown");
2039 return NULL;
2042 Geometry *
2043 GEOSPointOnSurface_r(GEOSContextHandle_t extHandle, const Geometry *g1)
2045 if ( 0 == extHandle )
2047 return NULL;
2050 GEOSContextHandleInternal_t *handle = 0;
2051 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
2052 if ( 0 == handle->initialized )
2054 return NULL;
2059 Geometry *ret = g1->getInteriorPoint();
2060 if ( ! ret )
2062 const GeometryFactory* gf = handle->geomFactory;
2063 // return an empty collection
2064 return gf->createGeometryCollection();
2066 return ret;
2068 catch (const std::exception &e)
2070 handle->ERROR_MESSAGE("%s", e.what());
2072 catch (...)
2074 handle->ERROR_MESSAGE("Unknown exception thrown");
2077 return NULL;
2080 //-------------------------------------------------------------------
2081 // memory management functions
2082 //------------------------------------------------------------------
2084 void
2085 GEOSGeom_destroy_r(GEOSContextHandle_t extHandle, Geometry *a)
2087 GEOSContextHandleInternal_t *handle = 0;
2089 // FIXME: mloskot: Does this try-catch around delete means that
2090 // destructors in GEOS may throw? If it does, this is a serious
2091 // violation of "never throw an exception from a destructor" principle
2095 delete a;
2097 catch (const std::exception &e)
2099 if ( 0 == extHandle )
2101 return;
2104 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
2105 if ( 0 == handle->initialized )
2107 return;
2110 handle->ERROR_MESSAGE("%s", e.what());
2112 catch (...)
2114 if ( 0 == extHandle )
2116 return;
2119 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
2120 if ( 0 == handle->initialized )
2122 return;
2125 handle->ERROR_MESSAGE("Unknown exception thrown");
2129 void
2130 GEOSSetSRID_r(GEOSContextHandle_t extHandle, Geometry *g, int srid)
2132 assert(0 != g);
2134 if ( 0 == extHandle )
2136 return;
2139 GEOSContextHandleInternal_t *handle = 0;
2140 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
2141 if ( 0 == handle->initialized )
2143 return;
2146 g->setSRID(srid);
2151 GEOSGetNumCoordinates_r(GEOSContextHandle_t extHandle, const Geometry *g)
2153 assert(0 != g);
2155 if ( 0 == extHandle )
2157 return -1;
2160 GEOSContextHandleInternal_t *handle = 0;
2161 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
2162 if ( 0 == handle->initialized )
2164 return -1;
2169 return static_cast<int>(g->getNumPoints());
2171 catch (const std::exception &e)
2173 handle->ERROR_MESSAGE("%s", e.what());
2175 catch (...)
2177 handle->ERROR_MESSAGE("Unknown exception thrown");
2180 return -1;
2184 * Return -1 on exception, 0 otherwise.
2185 * Converts Geometry to normal form (or canonical form).
2188 GEOSNormalize_r(GEOSContextHandle_t extHandle, Geometry *g)
2190 assert(0 != g);
2192 if ( 0 == extHandle )
2194 return -1;
2197 GEOSContextHandleInternal_t *handle = 0;
2198 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
2199 if ( 0 == handle->initialized )
2201 return -1;
2206 g->normalize();
2207 return 0; // SUCCESS
2209 catch (const std::exception &e)
2211 handle->ERROR_MESSAGE("%s", e.what());
2213 catch (...)
2215 handle->ERROR_MESSAGE("Unknown exception thrown");
2218 return -1;
2222 GEOSGetNumInteriorRings_r(GEOSContextHandle_t extHandle, const Geometry *g1)
2224 if ( 0 == extHandle )
2226 return -1;
2229 GEOSContextHandleInternal_t *handle = 0;
2230 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
2231 if ( 0 == handle->initialized )
2233 return -1;
2238 const Polygon *p = dynamic_cast<const Polygon *>(g1);
2239 if ( ! p )
2241 handle->ERROR_MESSAGE("Argument is not a Polygon");
2242 return -1;
2244 return static_cast<int>(p->getNumInteriorRing());
2246 catch (const std::exception &e)
2248 handle->ERROR_MESSAGE("%s", e.what());
2250 catch (...)
2252 handle->ERROR_MESSAGE("Unknown exception thrown");
2255 return -1;
2259 // returns -1 on error and 1 for non-multi geometries
2261 GEOSGetNumGeometries_r(GEOSContextHandle_t extHandle, const Geometry *g1)
2263 if ( 0 == extHandle )
2265 return -1;
2268 GEOSContextHandleInternal_t *handle = 0;
2269 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
2270 if ( 0 == handle->initialized )
2272 return -1;
2277 return static_cast<int>(g1->getNumGeometries());
2279 catch (const std::exception &e)
2281 handle->ERROR_MESSAGE("%s", e.what());
2283 catch (...)
2285 handle->ERROR_MESSAGE("Unknown exception thrown");
2288 return -1;
2293 * Call only on GEOMETRYCOLLECTION or MULTI*.
2294 * Return a pointer to the internal Geometry.
2296 const Geometry *
2297 GEOSGetGeometryN_r(GEOSContextHandle_t extHandle, const Geometry *g1, int n)
2299 if ( 0 == extHandle )
2301 return NULL;
2304 GEOSContextHandleInternal_t *handle = 0;
2305 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
2306 if ( 0 == handle->initialized )
2308 return NULL;
2313 return g1->getGeometryN(n);
2315 catch (const std::exception &e)
2317 handle->ERROR_MESSAGE("%s", e.what());
2319 catch (...)
2321 handle->ERROR_MESSAGE("Unknown exception thrown");
2324 return NULL;
2328 * Call only on LINESTRING
2329 * Returns NULL on exception
2331 Geometry *
2332 GEOSGeomGetPointN_r(GEOSContextHandle_t extHandle, const Geometry *g1, int n)
2334 if ( 0 == extHandle )
2336 return NULL;
2339 GEOSContextHandleInternal_t *handle = 0;
2340 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
2341 if ( 0 == handle->initialized )
2343 return NULL;
2348 using geos::geom::LineString;
2349 const LineString *ls = dynamic_cast<const LineString *>(g1);
2350 if ( ! ls )
2352 handle->ERROR_MESSAGE("Argument is not a LineString");
2353 return NULL;
2355 return ls->getPointN(n);
2357 catch (const std::exception &e)
2359 handle->ERROR_MESSAGE("%s", e.what());
2361 catch (...)
2363 handle->ERROR_MESSAGE("Unknown exception thrown");
2366 return NULL;
2370 * Call only on LINESTRING
2372 Geometry *
2373 GEOSGeomGetStartPoint_r(GEOSContextHandle_t extHandle, const Geometry *g1)
2375 if ( 0 == extHandle )
2377 return NULL;
2380 GEOSContextHandleInternal_t *handle = 0;
2381 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
2382 if ( 0 == handle->initialized )
2384 return NULL;
2389 using geos::geom::LineString;
2390 const LineString *ls = dynamic_cast<const LineString *>(g1);
2391 if ( ! ls )
2393 handle->ERROR_MESSAGE("Argument is not a LineString");
2394 return NULL;
2396 return ls->getStartPoint();
2398 catch (const std::exception &e)
2400 handle->ERROR_MESSAGE("%s", e.what());
2402 catch (...)
2404 handle->ERROR_MESSAGE("Unknown exception thrown");
2407 return NULL;
2411 * Call only on LINESTRING
2413 Geometry *
2414 GEOSGeomGetEndPoint_r(GEOSContextHandle_t extHandle, const Geometry *g1)
2416 if ( 0 == extHandle )
2418 return NULL;
2421 GEOSContextHandleInternal_t *handle = 0;
2422 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
2423 if ( 0 == handle->initialized )
2425 return NULL;
2430 using geos::geom::LineString;
2431 const LineString *ls = dynamic_cast<const LineString *>(g1);
2432 if ( ! ls )
2434 handle->ERROR_MESSAGE("Argument is not a LineString");
2435 return NULL;
2437 return ls->getEndPoint();
2439 catch (const std::exception &e)
2441 handle->ERROR_MESSAGE("%s", e.what());
2443 catch (...)
2445 handle->ERROR_MESSAGE("Unknown exception thrown");
2448 return NULL;
2452 * Call only on LINESTRING
2453 * return 2 on exception, 1 on true, 0 on false
2455 char
2456 GEOSisClosed_r(GEOSContextHandle_t extHandle, const Geometry *g1)
2458 if ( 0 == extHandle )
2460 return 2;
2463 GEOSContextHandleInternal_t *handle = 0;
2464 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
2465 if ( 0 == handle->initialized )
2467 return 2;
2472 using geos::geom::LineString;
2473 const LineString *ls = dynamic_cast<const LineString *>(g1);
2474 if ( ! ls )
2476 handle->ERROR_MESSAGE("Argument is not a LineString");
2477 return 2;
2479 return ls->isClosed();
2481 catch (const std::exception &e)
2483 handle->ERROR_MESSAGE("%s", e.what());
2485 catch (...)
2487 handle->ERROR_MESSAGE("Unknown exception thrown");
2490 return 2;
2494 * Call only on LINESTRING
2495 * return 0 on exception, otherwise 1
2498 GEOSGeomGetLength_r(GEOSContextHandle_t extHandle, const Geometry *g1, double *length)
2500 if ( 0 == extHandle )
2502 return 0;
2505 GEOSContextHandleInternal_t *handle = 0;
2506 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
2507 if ( 0 == handle->initialized )
2509 return 0;
2514 using geos::geom::LineString;
2515 const LineString *ls = dynamic_cast<const LineString *>(g1);
2516 if ( ! ls )
2518 handle->ERROR_MESSAGE("Argument is not a LineString");
2519 return 0;
2521 *length = ls->getLength();
2522 return 1;
2524 catch (const std::exception &e)
2526 handle->ERROR_MESSAGE("%s", e.what());
2528 catch (...)
2530 handle->ERROR_MESSAGE("Unknown exception thrown");
2533 return 0;
2537 * Call only on LINESTRING
2540 GEOSGeomGetNumPoints_r(GEOSContextHandle_t extHandle, const Geometry *g1)
2542 if ( 0 == extHandle )
2544 return -1;
2547 GEOSContextHandleInternal_t *handle = 0;
2548 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
2549 if ( 0 == handle->initialized )
2551 return -1;
2556 using geos::geom::LineString;
2557 const LineString *ls = dynamic_cast<const LineString *>(g1);
2558 if ( ! ls )
2560 handle->ERROR_MESSAGE("Argument is not a LineString");
2561 return -1;
2563 return ls->getNumPoints();
2565 catch (const std::exception &e)
2567 handle->ERROR_MESSAGE("%s", e.what());
2569 catch (...)
2571 handle->ERROR_MESSAGE("Unknown exception thrown");
2574 return -1;
2578 * For POINT
2579 * returns 0 on exception, otherwise 1
2582 GEOSGeomGetX_r(GEOSContextHandle_t extHandle, const Geometry *g1, double *x)
2584 if ( 0 == extHandle )
2586 return 0;
2589 GEOSContextHandleInternal_t *handle = 0;
2590 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
2591 if ( 0 == handle->initialized )
2593 return 0;
2598 using geos::geom::Point;
2599 const Point *po = dynamic_cast<const Point *>(g1);
2600 if ( ! po )
2602 handle->ERROR_MESSAGE("Argument is not a Point");
2603 return 0;
2605 *x = po->getX();
2606 return 1;
2608 catch (const std::exception &e)
2610 handle->ERROR_MESSAGE("%s", e.what());
2612 catch (...)
2614 handle->ERROR_MESSAGE("Unknown exception thrown");
2617 return 0;
2621 * For POINT
2622 * returns 0 on exception, otherwise 1
2625 GEOSGeomGetY_r(GEOSContextHandle_t extHandle, const Geometry *g1, double *y)
2627 if ( 0 == extHandle )
2629 return 0;
2632 GEOSContextHandleInternal_t *handle = 0;
2633 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
2634 if ( 0 == handle->initialized )
2636 return 0;
2641 using geos::geom::Point;
2642 const Point *po = dynamic_cast<const Point *>(g1);
2643 if ( ! po )
2645 handle->ERROR_MESSAGE("Argument is not a Point");
2646 return 0;
2648 *y = po->getY();
2649 return 1;
2651 catch (const std::exception &e)
2653 handle->ERROR_MESSAGE("%s", e.what());
2655 catch (...)
2657 handle->ERROR_MESSAGE("Unknown exception thrown");
2660 return 0;
2664 * Call only on polygon
2665 * Return a copy of the internal Geometry.
2667 const Geometry *
2668 GEOSGetExteriorRing_r(GEOSContextHandle_t extHandle, const Geometry *g1)
2670 if ( 0 == extHandle )
2672 return NULL;
2675 GEOSContextHandleInternal_t *handle = 0;
2676 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
2677 if ( 0 == handle->initialized )
2679 return NULL;
2684 const Polygon *p = dynamic_cast<const Polygon *>(g1);
2685 if ( ! p )
2687 handle->ERROR_MESSAGE("Invalid argument (must be a Polygon)");
2688 return NULL;
2690 return p->getExteriorRing();
2692 catch (const std::exception &e)
2694 handle->ERROR_MESSAGE("%s", e.what());
2696 catch (...)
2698 handle->ERROR_MESSAGE("Unknown exception thrown");
2701 return NULL;
2705 * Call only on polygon
2706 * Return a pointer to internal storage, do not destroy it.
2708 const Geometry *
2709 GEOSGetInteriorRingN_r(GEOSContextHandle_t extHandle, const Geometry *g1, int n)
2711 if ( 0 == extHandle )
2713 return NULL;
2716 GEOSContextHandleInternal_t *handle = 0;
2717 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
2718 if ( 0 == handle->initialized )
2720 return NULL;
2725 const Polygon *p = dynamic_cast<const Polygon *>(g1);
2726 if ( ! p )
2728 handle->ERROR_MESSAGE("Invalid argument (must be a Polygon)");
2729 return NULL;
2731 return p->getInteriorRingN(n);
2733 catch (const std::exception &e)
2735 handle->ERROR_MESSAGE("%s", e.what());
2737 catch (...)
2739 handle->ERROR_MESSAGE("Unknown exception thrown");
2742 return NULL;
2745 Geometry *
2746 GEOSGetCentroid_r(GEOSContextHandle_t extHandle, const Geometry *g)
2748 if ( 0 == extHandle )
2750 return NULL;
2753 GEOSContextHandleInternal_t *handle = 0;
2754 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
2755 if ( 0 == handle->initialized )
2757 return NULL;
2762 Geometry *ret = g->getCentroid();
2763 if (0 == ret)
2765 const GeometryFactory *gf = handle->geomFactory;
2766 return gf->createGeometryCollection();
2768 return ret;
2770 catch (const std::exception &e)
2772 handle->ERROR_MESSAGE("%s", e.what());
2774 catch (...)
2776 handle->ERROR_MESSAGE("Unknown exception thrown");
2779 return NULL;
2782 Geometry *
2783 GEOSGeom_createEmptyCollection_r(GEOSContextHandle_t extHandle, int type)
2785 if ( 0 == extHandle )
2787 return NULL;
2790 GEOSContextHandleInternal_t *handle = 0;
2791 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
2792 if ( 0 == handle->initialized )
2794 return NULL;
2797 #ifdef GEOS_DEBUG
2798 char buf[256];
2799 sprintf(buf, "createCollection: requested type %d, ngeoms: %d",
2800 type, ngeoms);
2801 handle->NOTICE_MESSAGE("%s", buf);// TODO: Can handle->NOTICE_MESSAGE format that directly?
2802 #endif
2806 const GeometryFactory* gf = handle->geomFactory;
2808 Geometry *g = 0;
2809 switch (type)
2811 case GEOS_GEOMETRYCOLLECTION:
2812 g = gf->createGeometryCollection();
2813 break;
2814 case GEOS_MULTIPOINT:
2815 g = gf->createMultiPoint();
2816 break;
2817 case GEOS_MULTILINESTRING:
2818 g = gf->createMultiLineString();
2819 break;
2820 case GEOS_MULTIPOLYGON:
2821 g = gf->createMultiPolygon();
2822 break;
2823 default:
2824 handle->ERROR_MESSAGE("Unsupported type request for GEOSGeom_createEmptyCollection_r");
2825 g = 0;
2829 return g;
2831 catch (const std::exception &e)
2833 handle->ERROR_MESSAGE("%s", e.what());
2835 catch (...)
2837 handle->ERROR_MESSAGE("Unknown exception thrown");
2840 return 0;
2843 Geometry *
2844 GEOSGeom_createCollection_r(GEOSContextHandle_t extHandle, int type, Geometry **geoms, unsigned int ngeoms)
2846 if ( 0 == extHandle )
2848 return NULL;
2851 GEOSContextHandleInternal_t *handle = 0;
2852 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
2853 if ( 0 == handle->initialized )
2855 return NULL;
2858 #ifdef GEOS_DEBUG
2859 char buf[256];
2860 sprintf(buf, "PostGIS2GEOS_collection: requested type %d, ngeoms: %d",
2861 type, ngeoms);
2862 handle->NOTICE_MESSAGE("%s", buf);// TODO: Can handle->NOTICE_MESSAGE format that directly?
2863 #endif
2867 const GeometryFactory* gf = handle->geomFactory;
2868 std::vector<Geometry*>* vgeoms = new std::vector<Geometry*>(geoms, geoms + ngeoms);
2870 Geometry *g = 0;
2871 switch (type)
2873 case GEOS_GEOMETRYCOLLECTION:
2874 g = gf->createGeometryCollection(vgeoms);
2875 break;
2876 case GEOS_MULTIPOINT:
2877 g = gf->createMultiPoint(vgeoms);
2878 break;
2879 case GEOS_MULTILINESTRING:
2880 g = gf->createMultiLineString(vgeoms);
2881 break;
2882 case GEOS_MULTIPOLYGON:
2883 g = gf->createMultiPolygon(vgeoms);
2884 break;
2885 default:
2886 handle->ERROR_MESSAGE("Unsupported type request for PostGIS2GEOS_collection");
2887 g = 0;
2891 return g;
2893 catch (const std::exception &e)
2895 handle->ERROR_MESSAGE("%s", e.what());
2897 catch (...)
2899 handle->ERROR_MESSAGE("Unknown exception thrown");
2902 return 0;
2905 Geometry *
2906 GEOSPolygonize_r(GEOSContextHandle_t extHandle, const Geometry * const * g, unsigned int ngeoms)
2908 if ( 0 == extHandle )
2910 return 0;
2913 GEOSContextHandleInternal_t *handle = 0;
2914 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
2915 if ( 0 == handle->initialized )
2917 return 0;
2920 Geometry *out = 0;
2924 // Polygonize
2925 using geos::operation::polygonize::Polygonizer;
2926 Polygonizer plgnzr;
2927 for (std::size_t i = 0; i < ngeoms; ++i)
2929 plgnzr.add(g[i]);
2932 #if GEOS_DEBUG
2933 handle->NOTICE_MESSAGE("geometry vector added to polygonizer");
2934 #endif
2936 std::vector<Polygon*> *polys = plgnzr.getPolygons();
2937 assert(0 != polys);
2939 #if GEOS_DEBUG
2940 handle->NOTICE_MESSAGE("output polygons got");
2941 #endif
2943 // We need a vector of Geometry pointers, not Polygon pointers.
2944 // STL vector doesn't allow transparent upcast of this
2945 // nature, so we explicitly convert.
2946 // (it's just a waste of processor and memory, btw)
2948 // XXX mloskot: Why not to extent GeometryFactory to accept
2949 // vector of polygons or extend Polygonizer to return list of Geometry*
2950 // or add a wrapper which semantic is similar to:
2951 // std::vector<as_polygon<Geometry*> >
2952 std::vector<Geometry*> *polyvec = new std::vector<Geometry *>(polys->size());
2954 for (std::size_t i = 0; i < polys->size(); ++i)
2956 (*polyvec)[i] = (*polys)[i];
2958 delete polys;
2959 polys = 0;
2961 const GeometryFactory *gf = handle->geomFactory;
2963 // The below takes ownership of the passed vector,
2964 // so we must *not* delete it
2965 out = gf->createGeometryCollection(polyvec);
2967 catch (const std::exception &e)
2969 handle->ERROR_MESSAGE("%s", e.what());
2971 catch (...)
2973 handle->ERROR_MESSAGE("Unknown exception thrown");
2976 return out;
2979 Geometry *
2980 GEOSPolygonizer_getCutEdges_r(GEOSContextHandle_t extHandle, const Geometry * const * g, unsigned int ngeoms)
2982 if ( 0 == extHandle )
2984 return 0;
2987 GEOSContextHandleInternal_t *handle = 0;
2988 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
2989 if ( 0 == handle->initialized )
2991 return 0;
2994 Geometry *out = 0;
2998 // Polygonize
2999 using geos::operation::polygonize::Polygonizer;
3000 Polygonizer plgnzr;
3001 for (std::size_t i = 0; i < ngeoms; ++i)
3003 plgnzr.add(g[i]);
3006 #if GEOS_DEBUG
3007 handle->NOTICE_MESSAGE("geometry vector added to polygonizer");
3008 #endif
3010 const std::vector<const LineString *>& lines = plgnzr.getCutEdges();
3012 #if GEOS_DEBUG
3013 handle->NOTICE_MESSAGE("output polygons got");
3014 #endif
3016 // We need a vector of Geometry pointers, not Polygon pointers.
3017 // STL vector doesn't allow transparent upcast of this
3018 // nature, so we explicitly convert.
3019 // (it's just a waste of processor and memory, btw)
3020 // XXX mloskot: See comment for GEOSPolygonize_r
3021 std::vector<Geometry*> *linevec = new std::vector<Geometry *>(lines.size());
3023 for (std::size_t i = 0, n=lines.size(); i < n; ++i)
3025 (*linevec)[i] = lines[i]->clone();
3028 const GeometryFactory *gf = handle->geomFactory;
3030 // The below takes ownership of the passed vector,
3031 // so we must *not* delete it
3032 out = gf->createGeometryCollection(linevec);
3034 catch (const std::exception &e)
3036 handle->ERROR_MESSAGE("%s", e.what());
3038 catch (...)
3040 handle->ERROR_MESSAGE("Unknown exception thrown");
3043 return out;
3046 Geometry *
3047 GEOSPolygonize_full_r(GEOSContextHandle_t extHandle, const Geometry* g,
3048 Geometry** cuts, Geometry** dangles, Geometry** invalid)
3050 if ( 0 == extHandle )
3052 return 0;
3055 GEOSContextHandleInternal_t *handle = 0;
3056 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
3057 if ( 0 == handle->initialized )
3059 return 0;
3064 // Polygonize
3065 using geos::operation::polygonize::Polygonizer;
3066 Polygonizer plgnzr;
3067 for (std::size_t i = 0; i <g->getNumGeometries(); ++i)
3069 plgnzr.add(g->getGeometryN(i));
3072 #if GEOS_DEBUG
3073 handle->NOTICE_MESSAGE("geometry vector added to polygonizer");
3074 #endif
3075 const GeometryFactory *gf = handle->geomFactory;
3077 if ( cuts ) {
3079 const std::vector<const LineString *>& lines = plgnzr.getCutEdges();
3080 std::vector<Geometry*> *linevec = new std::vector<Geometry *>(lines.size());
3081 for (std::size_t i = 0, n=lines.size(); i < n; ++i)
3083 (*linevec)[i] = lines[i]->clone();
3086 // The below takes ownership of the passed vector,
3087 // so we must *not* delete it
3088 *cuts = gf->createGeometryCollection(linevec);
3091 if ( dangles ) {
3093 const std::vector<const LineString *>& lines = plgnzr.getDangles();
3094 std::vector<Geometry*> *linevec = new std::vector<Geometry *>(lines.size());
3095 for (std::size_t i = 0, n=lines.size(); i < n; ++i)
3097 (*linevec)[i] = lines[i]->clone();
3100 // The below takes ownership of the passed vector,
3101 // so we must *not* delete it
3102 *dangles = gf->createGeometryCollection(linevec);
3105 if ( invalid ) {
3107 const std::vector<LineString *>& lines = plgnzr.getInvalidRingLines();
3108 std::vector<Geometry*> *linevec = new std::vector<Geometry *>(lines.size());
3109 for (std::size_t i = 0, n=lines.size(); i < n; ++i)
3111 (*linevec)[i] = lines[i]->clone();
3114 // The below takes ownership of the passed vector,
3115 // so we must *not* delete it
3116 *invalid = gf->createGeometryCollection(linevec);
3119 std::vector<Polygon*> *polys = plgnzr.getPolygons();
3120 std::vector<Geometry*> *polyvec = new std::vector<Geometry *>(polys->size());
3121 for (std::size_t i = 0; i < polys->size(); ++i)
3123 (*polyvec)[i] = (*polys)[i];
3125 delete polys;
3127 return gf->createGeometryCollection(polyvec);
3130 catch (const std::exception &e)
3132 handle->ERROR_MESSAGE("%s", e.what());
3133 return 0;
3135 catch (...)
3137 handle->ERROR_MESSAGE("Unknown exception thrown");
3138 return 0;
3142 Geometry *
3143 GEOSLineMerge_r(GEOSContextHandle_t extHandle, const Geometry *g)
3145 if ( 0 == extHandle )
3147 return 0;
3150 GEOSContextHandleInternal_t *handle = 0;
3151 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
3152 if ( 0 == handle->initialized )
3154 return 0;
3157 Geometry *out = 0;
3161 using geos::operation::linemerge::LineMerger;
3162 LineMerger lmrgr;
3163 lmrgr.add(g);
3165 std::vector<LineString *>* lines = lmrgr.getMergedLineStrings();
3166 assert(0 != lines);
3168 #if GEOS_DEBUG
3169 handle->NOTICE_MESSAGE("output lines got");
3170 #endif
3172 std::vector<Geometry *>*geoms = new std::vector<Geometry *>(lines->size());
3173 for (std::vector<Geometry *>::size_type i = 0; i < lines->size(); ++i)
3175 (*geoms)[i] = (*lines)[i];
3177 delete lines;
3178 lines = 0;
3180 const GeometryFactory *gf = handle->geomFactory;
3181 out = gf->buildGeometry(geoms);
3183 // XXX: old version
3184 //out = gf->createGeometryCollection(geoms);
3186 catch (const std::exception &e)
3188 handle->ERROR_MESSAGE("%s", e.what());
3190 catch (...)
3192 handle->ERROR_MESSAGE("Unknown exception thrown");
3195 return out;
3199 GEOSGetSRID_r(GEOSContextHandle_t extHandle, const Geometry *g)
3201 assert(0 != g);
3203 if ( 0 == extHandle )
3205 return 0;
3208 GEOSContextHandleInternal_t *handle = 0;
3209 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
3210 if ( 0 == handle->initialized )
3212 return 0;
3217 return g->getSRID();
3219 catch (const std::exception &e)
3221 handle->ERROR_MESSAGE("%s", e.what());
3223 catch (...)
3225 handle->ERROR_MESSAGE("Unknown exception thrown");
3228 return 0;
3231 const char* GEOSversion()
3233 return GEOS_CAPI_VERSION;
3236 const char* GEOSjtsport()
3238 return GEOS_JTS_PORT;
3241 char
3242 GEOSHasZ_r(GEOSContextHandle_t extHandle, const Geometry *g)
3244 assert(0 != g);
3246 if ( 0 == extHandle )
3248 return -1;
3251 GEOSContextHandleInternal_t *handle = 0;
3252 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
3253 if ( 0 == handle->initialized )
3255 return -1;
3258 if (g->isEmpty())
3260 return false;
3262 assert(0 != g->getCoordinate());
3264 double az = g->getCoordinate()->z;
3265 //handle->ERROR_MESSAGE("ZCoord: %g", az);
3267 return static_cast<char>(FINITE(az));
3271 GEOS_getWKBOutputDims_r(GEOSContextHandle_t extHandle)
3273 if ( 0 == extHandle )
3275 return -1;
3278 GEOSContextHandleInternal_t *handle = 0;
3279 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
3280 if ( 0 == handle->initialized )
3282 return -1;
3285 return handle->WKBOutputDims;
3289 GEOS_setWKBOutputDims_r(GEOSContextHandle_t extHandle, int newdims)
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 if ( newdims < 2 || newdims > 3 )
3305 handle->ERROR_MESSAGE("WKB output dimensions out of range 2..3");
3308 const int olddims = handle->WKBOutputDims;
3309 handle->WKBOutputDims = newdims;
3311 return olddims;
3315 GEOS_getWKBByteOrder_r(GEOSContextHandle_t extHandle)
3317 if ( 0 == extHandle )
3319 return -1;
3322 GEOSContextHandleInternal_t *handle = 0;
3323 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
3324 if ( 0 == handle->initialized )
3326 return -1;
3329 return handle->WKBByteOrder;
3333 GEOS_setWKBByteOrder_r(GEOSContextHandle_t extHandle, int byteOrder)
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 const int oldByteOrder = handle->WKBByteOrder;
3348 handle->WKBByteOrder = byteOrder;
3350 return oldByteOrder;
3354 CoordinateSequence *
3355 GEOSCoordSeq_create_r(GEOSContextHandle_t extHandle, unsigned int size, unsigned int dims)
3357 if ( 0 == extHandle )
3359 return NULL;
3362 GEOSContextHandleInternal_t *handle = 0;
3363 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
3364 if ( 0 == handle->initialized )
3366 return NULL;
3371 const GeometryFactory *gf = handle->geomFactory;
3372 return gf->getCoordinateSequenceFactory()->create(size, dims);
3374 catch (const std::exception &e)
3376 handle->ERROR_MESSAGE("%s", e.what());
3378 catch (...)
3380 handle->ERROR_MESSAGE("Unknown exception thrown");
3383 return NULL;
3387 GEOSCoordSeq_setOrdinate_r(GEOSContextHandle_t extHandle, CoordinateSequence *cs,
3388 unsigned int idx, unsigned int dim, double val)
3390 assert(0 != cs);
3391 if ( 0 == extHandle )
3393 return 0;
3396 GEOSContextHandleInternal_t *handle = 0;
3397 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
3398 if ( 0 == handle->initialized )
3400 return 0;
3405 cs->setOrdinate(static_cast<int>(idx), static_cast<int>(dim), val);
3406 return 1;
3408 catch (const std::exception &e)
3410 handle->ERROR_MESSAGE("%s", e.what());
3412 catch (...)
3414 handle->ERROR_MESSAGE("Unknown exception thrown");
3417 return 0;
3421 GEOSCoordSeq_setX_r(GEOSContextHandle_t extHandle, CoordinateSequence *s, unsigned int idx, double val)
3423 return GEOSCoordSeq_setOrdinate_r(extHandle, s, idx, 0, val);
3427 GEOSCoordSeq_setY_r(GEOSContextHandle_t extHandle, CoordinateSequence *s, unsigned int idx, double val)
3429 return GEOSCoordSeq_setOrdinate_r(extHandle, s, idx, 1, val);
3433 GEOSCoordSeq_setZ_r(GEOSContextHandle_t extHandle, CoordinateSequence *s, unsigned int idx, double val)
3435 return GEOSCoordSeq_setOrdinate_r(extHandle, s, idx, 2, val);
3438 CoordinateSequence *
3439 GEOSCoordSeq_clone_r(GEOSContextHandle_t extHandle, const CoordinateSequence *cs)
3441 assert(0 != cs);
3443 if ( 0 == extHandle )
3445 return NULL;
3448 GEOSContextHandleInternal_t *handle = 0;
3449 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
3450 if ( 0 == handle->initialized )
3452 return NULL;
3457 return cs->clone();
3459 catch (const std::exception &e)
3461 handle->ERROR_MESSAGE("%s", e.what());
3463 catch (...)
3465 handle->ERROR_MESSAGE("Unknown exception thrown");
3468 return NULL;
3472 GEOSCoordSeq_getOrdinate_r(GEOSContextHandle_t extHandle, const CoordinateSequence *cs,
3473 unsigned int idx, unsigned int dim, double *val)
3475 assert(0 != cs);
3476 assert(0 != val);
3478 if ( 0 == extHandle )
3480 return 0;
3483 GEOSContextHandleInternal_t *handle = 0;
3484 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
3485 if ( 0 == handle->initialized )
3487 return 0;
3492 double d = cs->getOrdinate(static_cast<int>(idx), static_cast<int>(dim));
3493 *val = d;
3495 return 1;
3497 catch (const std::exception &e)
3499 handle->ERROR_MESSAGE("%s", e.what());
3501 catch (...)
3503 handle->ERROR_MESSAGE("Unknown exception thrown");
3506 return 0;
3510 GEOSCoordSeq_getX_r(GEOSContextHandle_t extHandle, const CoordinateSequence *s, unsigned int idx, double *val)
3512 return GEOSCoordSeq_getOrdinate_r(extHandle, s, idx, 0, val);
3516 GEOSCoordSeq_getY_r(GEOSContextHandle_t extHandle, const CoordinateSequence *s, unsigned int idx, double *val)
3518 return GEOSCoordSeq_getOrdinate_r(extHandle, s, idx, 1, val);
3522 GEOSCoordSeq_getZ_r(GEOSContextHandle_t extHandle, const CoordinateSequence *s, unsigned int idx, double *val)
3524 return GEOSCoordSeq_getOrdinate_r(extHandle, s, idx, 2, val);
3528 GEOSCoordSeq_getSize_r(GEOSContextHandle_t extHandle, const CoordinateSequence *cs, unsigned int *size)
3530 assert(0 != cs);
3531 assert(0 != size);
3533 if ( 0 == extHandle )
3535 return 0;
3538 GEOSContextHandleInternal_t *handle = 0;
3539 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
3540 if ( 0 == handle->initialized )
3542 return 0;
3547 const std::size_t sz = cs->getSize();
3548 *size = static_cast<unsigned int>(sz);
3549 return 1;
3551 catch (const std::exception &e)
3553 handle->ERROR_MESSAGE("%s", e.what());
3555 catch (...)
3557 handle->ERROR_MESSAGE("Unknown exception thrown");
3560 return 0;
3564 GEOSCoordSeq_getDimensions_r(GEOSContextHandle_t extHandle, const CoordinateSequence *cs, unsigned int *dims)
3566 assert(0 != cs);
3567 assert(0 != dims);
3569 if ( 0 == extHandle )
3571 return 0;
3574 GEOSContextHandleInternal_t *handle = 0;
3575 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
3576 if ( 0 == handle->initialized )
3578 return 0;
3583 const std::size_t dim = cs->getDimension();
3584 *dims = static_cast<unsigned int>(dim);
3586 return 1;
3588 catch (const std::exception &e)
3590 handle->ERROR_MESSAGE("%s", e.what());
3593 catch (...)
3595 handle->ERROR_MESSAGE("Unknown exception thrown");
3598 return 0;
3601 void
3602 GEOSCoordSeq_destroy_r(GEOSContextHandle_t extHandle, CoordinateSequence *s)
3604 GEOSContextHandleInternal_t *handle = 0;
3608 delete s;
3610 catch (const std::exception &e)
3612 if ( 0 == extHandle )
3614 return;
3617 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
3618 if ( 0 == handle->initialized )
3620 return;
3623 handle->ERROR_MESSAGE("%s", e.what());
3625 catch (...)
3627 if ( 0 == extHandle )
3629 return;
3632 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
3633 if ( 0 == handle->initialized )
3635 return;
3638 handle->ERROR_MESSAGE("Unknown exception thrown");
3642 const CoordinateSequence *
3643 GEOSGeom_getCoordSeq_r(GEOSContextHandle_t extHandle, const Geometry *g)
3645 if ( 0 == extHandle )
3647 return 0;
3650 GEOSContextHandleInternal_t *handle = 0;
3651 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
3652 if ( 0 == handle->initialized )
3654 return 0;
3659 using geos::geom::Point;
3661 const LineString *ls = dynamic_cast<const LineString *>(g);
3662 if ( ls )
3664 return ls->getCoordinatesRO();
3667 const Point *p = dynamic_cast<const Point *>(g);
3668 if ( p )
3670 return p->getCoordinatesRO();
3673 handle->ERROR_MESSAGE("Geometry must be a Point or LineString");
3674 return 0;
3676 catch (const std::exception &e)
3678 handle->ERROR_MESSAGE("%s", e.what());
3680 catch (...)
3682 handle->ERROR_MESSAGE("Unknown exception thrown");
3685 return 0;
3688 Geometry *
3689 GEOSGeom_createEmptyPoint_r(GEOSContextHandle_t extHandle)
3691 if ( 0 == extHandle )
3693 return NULL;
3696 GEOSContextHandleInternal_t *handle = 0;
3697 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
3698 if ( 0 == handle->initialized )
3700 return NULL;
3705 const GeometryFactory *gf = handle->geomFactory;
3706 return gf->createPoint();
3708 catch (const std::exception &e)
3710 handle->ERROR_MESSAGE("%s", e.what());
3712 catch (...)
3714 handle->ERROR_MESSAGE("Unknown exception thrown");
3717 return NULL;
3720 Geometry *
3721 GEOSGeom_createPoint_r(GEOSContextHandle_t extHandle, CoordinateSequence *cs)
3723 if ( 0 == extHandle )
3725 return 0;
3728 GEOSContextHandleInternal_t *handle = 0;
3729 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
3730 if ( 0 == handle->initialized )
3732 return 0;
3737 const GeometryFactory *gf = handle->geomFactory;
3738 return gf->createPoint(cs);
3740 catch (const std::exception &e)
3742 handle->ERROR_MESSAGE("%s", e.what());
3744 catch (...)
3746 handle->ERROR_MESSAGE("Unknown exception thrown");
3749 return 0;
3752 Geometry *
3753 GEOSGeom_createLinearRing_r(GEOSContextHandle_t extHandle, CoordinateSequence *cs)
3755 if ( 0 == extHandle )
3757 return NULL;
3760 GEOSContextHandleInternal_t *handle = 0;
3761 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
3762 if ( 0 == handle->initialized )
3764 return NULL;
3769 const GeometryFactory *gf = handle->geomFactory;
3771 return gf->createLinearRing(cs);
3773 catch (const std::exception &e)
3775 handle->ERROR_MESSAGE("%s", e.what());
3777 catch (...)
3779 handle->ERROR_MESSAGE("Unknown exception thrown");
3782 return NULL;
3785 Geometry *
3786 GEOSGeom_createEmptyLineString_r(GEOSContextHandle_t extHandle)
3788 if ( 0 == extHandle )
3790 return NULL;
3793 GEOSContextHandleInternal_t *handle = 0;
3794 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
3795 if ( 0 == handle->initialized )
3797 return NULL;
3802 const GeometryFactory *gf = handle->geomFactory;
3804 return gf->createLineString();
3806 catch (const std::exception &e)
3808 handle->ERROR_MESSAGE("%s", e.what());
3810 catch (...)
3812 handle->ERROR_MESSAGE("Unknown exception thrown");
3815 return NULL;
3818 Geometry *
3819 GEOSGeom_createLineString_r(GEOSContextHandle_t extHandle, CoordinateSequence *cs)
3821 if ( 0 == extHandle )
3823 return NULL;
3826 GEOSContextHandleInternal_t *handle = 0;
3827 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
3828 if ( 0 == handle->initialized )
3830 return NULL;
3835 const GeometryFactory *gf = handle->geomFactory;
3837 return gf->createLineString(cs);
3839 catch (const std::exception &e)
3841 handle->ERROR_MESSAGE("%s", e.what());
3843 catch (...)
3845 handle->ERROR_MESSAGE("Unknown exception thrown");
3848 return NULL;
3851 Geometry *
3852 GEOSGeom_createEmptyPolygon_r(GEOSContextHandle_t extHandle)
3854 if ( 0 == extHandle )
3856 return NULL;
3859 GEOSContextHandleInternal_t *handle = 0;
3860 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
3861 if ( 0 == handle->initialized )
3863 return NULL;
3868 const GeometryFactory *gf = handle->geomFactory;
3869 return gf->createPolygon();
3871 catch (const std::exception &e)
3873 handle->ERROR_MESSAGE("%s", e.what());
3875 catch (...)
3877 handle->ERROR_MESSAGE("Unknown exception thrown");
3880 return NULL;
3883 Geometry *
3884 GEOSGeom_createPolygon_r(GEOSContextHandle_t extHandle, Geometry *shell, Geometry **holes, unsigned int nholes)
3886 // FIXME: holes must be non-nullptr or may be nullptr?
3887 //assert(0 != holes);
3889 if ( 0 == extHandle )
3891 return NULL;
3894 GEOSContextHandleInternal_t *handle = 0;
3895 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
3896 if ( 0 == handle->initialized )
3898 return NULL;
3903 using geos::geom::LinearRing;
3905 std::vector<Geometry *> *vholes = new std::vector<Geometry *>(holes, holes + nholes);
3907 LinearRing *nshell = dynamic_cast<LinearRing *>(shell);
3908 if ( ! nshell )
3910 handle->ERROR_MESSAGE("Shell is not a LinearRing");
3911 return NULL;
3913 const GeometryFactory *gf = handle->geomFactory;
3915 return gf->createPolygon(nshell, vholes);
3917 catch (const std::exception &e)
3919 handle->ERROR_MESSAGE("%s", e.what());
3921 catch (...)
3923 handle->ERROR_MESSAGE("Unknown exception thrown");
3926 return NULL;
3929 Geometry *
3930 GEOSGeom_clone_r(GEOSContextHandle_t extHandle, const Geometry *g)
3932 assert(0 != g);
3934 if ( 0 == extHandle )
3936 return NULL;
3939 GEOSContextHandleInternal_t *handle = 0;
3940 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
3941 if ( 0 == handle->initialized )
3943 return NULL;
3948 return g->clone();
3950 catch (const std::exception &e)
3952 handle->ERROR_MESSAGE("%s", e.what());
3954 catch (...)
3956 handle->ERROR_MESSAGE("Unknown exception thrown");
3959 return NULL;
3963 GEOSGeom_getDimensions_r(GEOSContextHandle_t extHandle, const Geometry *g)
3965 if ( 0 == extHandle )
3967 return 0;
3970 GEOSContextHandleInternal_t *handle = 0;
3971 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
3972 if ( 0 == handle->initialized )
3974 return 0;
3979 return (int) g->getDimension();
3981 catch (const std::exception &e)
3983 handle->ERROR_MESSAGE("%s", e.what());
3985 catch (...)
3987 handle->ERROR_MESSAGE("Unknown exception thrown");
3990 return 0;
3994 GEOSGeom_getCoordinateDimension_r(GEOSContextHandle_t extHandle, const Geometry *g)
3996 if ( 0 == extHandle )
3998 return 0;
4001 GEOSContextHandleInternal_t *handle = 0;
4002 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
4003 if ( 0 == handle->initialized )
4005 return 0;
4010 return g->getCoordinateDimension();
4012 catch (const std::exception &e)
4014 handle->ERROR_MESSAGE("%s", e.what());
4016 catch (...)
4018 handle->ERROR_MESSAGE("Unknown exception thrown");
4021 return 0;
4024 Geometry *
4025 GEOSSimplify_r(GEOSContextHandle_t extHandle, const Geometry *g1, double tolerance)
4027 if ( 0 == extHandle )
4029 return NULL;
4032 GEOSContextHandleInternal_t *handle = 0;
4033 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
4034 if ( 0 == handle->initialized )
4036 return NULL;
4041 using namespace geos::simplify;
4042 Geometry::AutoPtr g(DouglasPeuckerSimplifier::simplify(g1, tolerance));
4043 return g.release();
4045 catch (const std::exception &e)
4047 handle->ERROR_MESSAGE("%s", e.what());
4049 catch (...)
4051 handle->ERROR_MESSAGE("Unknown exception thrown");
4054 return NULL;
4057 Geometry *
4058 GEOSTopologyPreserveSimplify_r(GEOSContextHandle_t extHandle, const Geometry *g1, double tolerance)
4060 if ( 0 == extHandle )
4062 return NULL;
4065 GEOSContextHandleInternal_t *handle = 0;
4066 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
4067 if ( 0 == handle->initialized )
4069 return NULL;
4074 using namespace geos::simplify;
4075 Geometry::AutoPtr g(TopologyPreservingSimplifier::simplify(g1, tolerance));
4076 return g.release();
4078 catch (const std::exception &e)
4080 handle->ERROR_MESSAGE("%s", e.what());
4082 catch (...)
4084 handle->ERROR_MESSAGE("Unknown exception thrown");
4087 return NULL;
4091 /* WKT Reader */
4092 WKTReader *
4093 GEOSWKTReader_create_r(GEOSContextHandle_t extHandle)
4095 if ( 0 == extHandle )
4097 return NULL;
4100 GEOSContextHandleInternal_t *handle = 0;
4101 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
4102 if ( 0 == handle->initialized )
4104 return NULL;
4109 using geos::io::WKTReader;
4110 return new WKTReader((GeometryFactory*)handle->geomFactory);
4112 catch (const std::exception &e)
4114 handle->ERROR_MESSAGE("%s", e.what());
4116 catch (...)
4118 handle->ERROR_MESSAGE("Unknown exception thrown");
4121 return NULL;
4124 void
4125 GEOSWKTReader_destroy_r(GEOSContextHandle_t extHandle, WKTReader *reader)
4127 GEOSContextHandleInternal_t *handle = 0;
4131 delete reader;
4133 catch (const std::exception &e)
4135 if ( 0 == extHandle )
4137 return;
4140 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
4141 if ( 0 == handle->initialized )
4143 return;
4146 handle->ERROR_MESSAGE("%s", e.what());
4148 catch (...)
4150 if ( 0 == extHandle )
4152 return;
4155 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
4156 if ( 0 == handle->initialized )
4158 return;
4161 handle->ERROR_MESSAGE("Unknown exception thrown");
4166 Geometry*
4167 GEOSWKTReader_read_r(GEOSContextHandle_t extHandle, WKTReader *reader, const char *wkt)
4169 assert(0 != reader);
4171 if ( 0 == extHandle )
4173 return 0;
4176 GEOSContextHandleInternal_t *handle = 0;
4177 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
4178 if ( 0 == handle->initialized )
4180 return 0;
4185 const std::string wktstring(wkt);
4186 Geometry *g = reader->read(wktstring);
4187 return g;
4189 catch (const std::exception &e)
4191 handle->ERROR_MESSAGE("%s", e.what());
4193 catch (...)
4195 handle->ERROR_MESSAGE("Unknown exception thrown");
4198 return 0;
4201 /* WKT Writer */
4202 WKTWriter *
4203 GEOSWKTWriter_create_r(GEOSContextHandle_t extHandle)
4205 if ( 0 == extHandle )
4207 return 0;
4210 GEOSContextHandleInternal_t *handle = 0;
4211 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
4212 if ( 0 == handle->initialized )
4214 return 0;
4219 using geos::io::WKTWriter;
4220 return new WKTWriter();
4222 catch (const std::exception &e)
4224 handle->ERROR_MESSAGE("%s", e.what());
4226 catch (...)
4228 handle->ERROR_MESSAGE("Unknown exception thrown");
4231 return 0;
4234 void
4235 GEOSWKTWriter_destroy_r(GEOSContextHandle_t extHandle, WKTWriter *Writer)
4238 GEOSContextHandleInternal_t *handle = 0;
4242 delete Writer;
4244 catch (const std::exception &e)
4246 if ( 0 == extHandle )
4248 return;
4251 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
4252 if ( 0 == handle->initialized )
4254 return;
4257 handle->ERROR_MESSAGE("%s", e.what());
4259 catch (...)
4261 if ( 0 == extHandle )
4263 return;
4266 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
4267 if ( 0 == handle->initialized )
4269 return;
4272 handle->ERROR_MESSAGE("Unknown exception thrown");
4277 char*
4278 GEOSWKTWriter_write_r(GEOSContextHandle_t extHandle, WKTWriter *writer, const Geometry *geom)
4280 assert(0 != writer);
4282 if ( 0 == extHandle )
4284 return NULL;
4287 GEOSContextHandleInternal_t *handle = 0;
4288 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
4289 if ( 0 == handle->initialized )
4291 return NULL;
4296 std::string sgeom(writer->write(geom));
4297 char *result = gstrdup(sgeom);
4298 return result;
4300 catch (const std::exception &e)
4302 handle->ERROR_MESSAGE("%s", e.what());
4304 catch (...)
4306 handle->ERROR_MESSAGE("Unknown exception thrown");
4309 return NULL;
4312 void
4313 GEOSWKTWriter_setTrim_r(GEOSContextHandle_t extHandle, WKTWriter *writer, char trim)
4315 assert(0 != writer);
4317 if ( 0 == extHandle )
4319 return;
4322 GEOSContextHandleInternal_t *handle = 0;
4323 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
4324 if ( 0 == handle->initialized )
4326 return;
4329 writer->setTrim(0 != trim);
4332 void
4333 GEOSWKTWriter_setRoundingPrecision_r(GEOSContextHandle_t extHandle, WKTWriter *writer, int precision)
4335 assert(0 != writer);
4337 if ( 0 == extHandle )
4339 return;
4342 GEOSContextHandleInternal_t *handle = 0;
4343 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
4344 if ( 0 == handle->initialized )
4346 return;
4349 writer->setRoundingPrecision(precision);
4352 void
4353 GEOSWKTWriter_setOutputDimension_r(GEOSContextHandle_t extHandle, WKTWriter *writer, int dim)
4355 assert(0 != writer);
4357 if ( 0 == extHandle )
4359 return;
4362 GEOSContextHandleInternal_t *handle = 0;
4363 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
4364 if ( 0 == handle->initialized )
4366 return;
4371 writer->setOutputDimension(dim);
4373 catch (const std::exception &e)
4375 handle->ERROR_MESSAGE("%s", e.what());
4377 catch (...)
4379 handle->ERROR_MESSAGE("Unknown exception thrown");
4384 GEOSWKTWriter_getOutputDimension_r(GEOSContextHandle_t extHandle, WKTWriter *writer)
4386 assert(0 != writer);
4388 if ( 0 == extHandle )
4390 return -1;
4393 GEOSContextHandleInternal_t *handle = 0;
4394 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
4395 if ( 0 == handle->initialized )
4397 return -1;
4400 int dim = -1;
4404 dim = writer->getOutputDimension();
4406 catch (const std::exception &e)
4408 handle->ERROR_MESSAGE("%s", e.what());
4410 catch (...)
4412 handle->ERROR_MESSAGE("Unknown exception thrown");
4415 return dim;
4418 void
4419 GEOSWKTWriter_setOld3D_r(GEOSContextHandle_t extHandle, WKTWriter *writer, int useOld3D)
4421 assert(0 != writer);
4423 if ( 0 == extHandle )
4425 return;
4428 GEOSContextHandleInternal_t *handle = 0;
4429 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
4430 if ( 0 == handle->initialized )
4432 return;
4435 writer->setOld3D(0 != useOld3D);
4438 /* WKB Reader */
4439 WKBReader *
4440 GEOSWKBReader_create_r(GEOSContextHandle_t extHandle)
4442 if ( 0 == extHandle )
4444 return NULL;
4447 GEOSContextHandleInternal_t *handle = 0;
4448 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
4449 if ( 0 == handle->initialized )
4451 return NULL;
4454 using geos::io::WKBReader;
4457 return new WKBReader(*(GeometryFactory*)handle->geomFactory);
4459 catch (const std::exception &e)
4461 handle->ERROR_MESSAGE("%s", e.what());
4463 catch (...)
4465 handle->ERROR_MESSAGE("Unknown exception thrown");
4468 return NULL;
4471 void
4472 GEOSWKBReader_destroy_r(GEOSContextHandle_t extHandle, WKBReader *reader)
4474 GEOSContextHandleInternal_t *handle = 0;
4478 delete reader;
4480 catch (const std::exception &e)
4482 if ( 0 == extHandle )
4484 return;
4487 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
4488 if ( 0 == handle->initialized )
4490 return;
4493 handle->ERROR_MESSAGE("%s", e.what());
4495 catch (...)
4497 if ( 0 == extHandle )
4499 return;
4502 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
4503 if ( 0 == handle->initialized )
4505 return;
4508 handle->ERROR_MESSAGE("Unknown exception thrown");
4513 Geometry*
4514 GEOSWKBReader_read_r(GEOSContextHandle_t extHandle, WKBReader *reader, const unsigned char *wkb, size_t size)
4516 assert(0 != reader);
4517 assert(0 != wkb);
4519 if ( 0 == extHandle )
4521 return 0;
4524 GEOSContextHandleInternal_t *handle = 0;
4525 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
4526 if ( 0 == handle->initialized )
4528 return 0;
4533 std::string wkbstring(reinterpret_cast<const char*>(wkb), size); // make it binary !
4534 std::istringstream is(std::ios_base::binary);
4535 is.str(wkbstring);
4536 is.seekg(0, std::ios::beg); // rewind reader pointer
4538 Geometry *g = reader->read(is);
4539 return g;
4541 catch (const std::exception &e)
4543 handle->ERROR_MESSAGE("%s", e.what());
4545 catch (...)
4547 handle->ERROR_MESSAGE("Unknown exception thrown");
4550 return 0;
4553 Geometry*
4554 GEOSWKBReader_readHEX_r(GEOSContextHandle_t extHandle, WKBReader *reader, const unsigned char *hex, size_t size)
4556 assert(0 != reader);
4557 assert(0 != hex);
4559 if ( 0 == extHandle )
4561 return 0;
4564 GEOSContextHandleInternal_t *handle = 0;
4565 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
4566 if ( 0 == handle->initialized )
4568 return 0;
4573 std::string hexstring(reinterpret_cast<const char*>(hex), size);
4574 std::istringstream is(std::ios_base::binary);
4575 is.str(hexstring);
4576 is.seekg(0, std::ios::beg); // rewind reader pointer
4578 Geometry *g = reader->readHEX(is);
4579 return g;
4581 catch (const std::exception &e)
4583 handle->ERROR_MESSAGE("%s", e.what());
4585 catch (...)
4587 handle->ERROR_MESSAGE("Unknown exception thrown");
4590 return 0;
4593 /* WKB Writer */
4594 WKBWriter *
4595 GEOSWKBWriter_create_r(GEOSContextHandle_t extHandle)
4597 if ( 0 == extHandle )
4599 return NULL;
4602 GEOSContextHandleInternal_t *handle = 0;
4603 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
4604 if ( 0 == handle->initialized )
4606 return NULL;
4611 using geos::io::WKBWriter;
4612 return new WKBWriter();
4614 catch (const std::exception &e)
4616 handle->ERROR_MESSAGE("%s", e.what());
4618 catch (...)
4620 handle->ERROR_MESSAGE("Unknown exception thrown");
4623 return NULL;
4626 void
4627 GEOSWKBWriter_destroy_r(GEOSContextHandle_t extHandle, WKBWriter *Writer)
4629 GEOSContextHandleInternal_t *handle = 0;
4633 delete Writer;
4635 catch (const std::exception &e)
4637 if ( 0 == extHandle )
4639 return;
4642 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
4643 if ( 0 == handle->initialized )
4645 return;
4648 handle->ERROR_MESSAGE("%s", e.what());
4650 catch (...)
4652 if ( 0 == extHandle )
4654 return;
4657 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
4658 if ( 0 == handle->initialized )
4660 return;
4663 handle->ERROR_MESSAGE("Unknown exception thrown");
4668 /* The caller owns the result */
4669 unsigned char*
4670 GEOSWKBWriter_write_r(GEOSContextHandle_t extHandle, WKBWriter *writer, const Geometry *geom, size_t *size)
4672 assert(0 != writer);
4673 assert(0 != geom);
4674 assert(0 != size);
4676 if ( 0 == extHandle )
4678 return NULL;
4681 GEOSContextHandleInternal_t *handle = 0;
4682 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
4683 if ( 0 == handle->initialized )
4685 return NULL;
4690 std::ostringstream os(std::ios_base::binary);
4691 writer->write(*geom, os);
4692 std::string wkbstring(os.str());
4693 const std::size_t len = wkbstring.length();
4695 unsigned char *result = NULL;
4696 result = (unsigned char*) std::malloc(len);
4697 std::memcpy(result, wkbstring.c_str(), len);
4698 *size = len;
4699 return result;
4701 catch (const std::exception &e)
4703 handle->ERROR_MESSAGE("%s", e.what());
4705 catch (...)
4707 handle->ERROR_MESSAGE("Unknown exception thrown");
4709 return NULL;
4712 /* The caller owns the result */
4713 unsigned char*
4714 GEOSWKBWriter_writeHEX_r(GEOSContextHandle_t extHandle, WKBWriter *writer, const Geometry *geom, size_t *size)
4716 assert(0 != writer);
4717 assert(0 != geom);
4718 assert(0 != size);
4720 if ( 0 == extHandle )
4722 return NULL;
4725 GEOSContextHandleInternal_t *handle = 0;
4726 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
4727 if ( 0 == handle->initialized )
4729 return NULL;
4734 std::ostringstream os(std::ios_base::binary);
4735 writer->writeHEX(*geom, os);
4736 std::string wkbstring(os.str());
4737 const std::size_t len = wkbstring.length();
4739 unsigned char *result = NULL;
4740 result = (unsigned char*) std::malloc(len);
4741 std::memcpy(result, wkbstring.c_str(), len);
4742 *size = len;
4743 return result;
4745 catch (const std::exception &e)
4747 handle->ERROR_MESSAGE("%s", e.what());
4749 catch (...)
4751 handle->ERROR_MESSAGE("Unknown exception thrown");
4754 return NULL;
4758 GEOSWKBWriter_getOutputDimension_r(GEOSContextHandle_t extHandle, const GEOSWKBWriter* writer)
4760 assert(0 != writer);
4762 if ( 0 == extHandle )
4764 return 0;
4767 int ret = 0;
4769 GEOSContextHandleInternal_t *handle = 0;
4770 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
4771 if ( 0 != handle->initialized )
4775 ret = writer->getOutputDimension();
4777 catch (...)
4779 handle->ERROR_MESSAGE("Unknown exception thrown");
4783 return ret;
4786 void
4787 GEOSWKBWriter_setOutputDimension_r(GEOSContextHandle_t extHandle, GEOSWKBWriter* writer, int newDimension)
4789 assert(0 != writer);
4791 if ( 0 == extHandle )
4793 return;
4796 GEOSContextHandleInternal_t *handle = 0;
4797 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
4798 if ( 0 != handle->initialized )
4802 writer->setOutputDimension(newDimension);
4804 catch (const std::exception &e)
4806 handle->ERROR_MESSAGE("%s", e.what());
4808 catch (...)
4810 handle->ERROR_MESSAGE("Unknown exception thrown");
4816 GEOSWKBWriter_getByteOrder_r(GEOSContextHandle_t extHandle, const GEOSWKBWriter* writer)
4818 assert(0 != writer);
4820 if ( 0 == extHandle )
4822 return 0;
4825 int ret = 0;
4827 GEOSContextHandleInternal_t *handle = 0;
4828 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
4829 if ( 0 != handle->initialized )
4833 ret = writer->getByteOrder();
4836 catch (...)
4838 handle->ERROR_MESSAGE("Unknown exception thrown");
4842 return ret;
4845 void
4846 GEOSWKBWriter_setByteOrder_r(GEOSContextHandle_t extHandle, GEOSWKBWriter* writer, int newByteOrder)
4848 assert(0 != writer);
4850 if ( 0 == extHandle )
4852 return;
4855 GEOSContextHandleInternal_t *handle = 0;
4856 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
4857 if ( 0 != handle->initialized )
4861 writer->setByteOrder(newByteOrder);
4863 catch (const std::exception &e)
4865 handle->ERROR_MESSAGE("%s", e.what());
4867 catch (...)
4869 handle->ERROR_MESSAGE("Unknown exception thrown");
4874 char
4875 GEOSWKBWriter_getIncludeSRID_r(GEOSContextHandle_t extHandle, const GEOSWKBWriter* writer)
4877 assert(0 != writer);
4879 if ( 0 == extHandle )
4881 return -1;
4884 int ret = -1;
4886 GEOSContextHandleInternal_t *handle = 0;
4887 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
4888 if ( 0 != handle->initialized )
4892 int srid = writer->getIncludeSRID();
4893 ret = static_cast<char>(srid);
4895 catch (...)
4897 handle->ERROR_MESSAGE("Unknown exception thrown");
4901 return static_cast<char>(ret);
4904 void
4905 GEOSWKBWriter_setIncludeSRID_r(GEOSContextHandle_t extHandle, GEOSWKBWriter* writer, const char newIncludeSRID)
4907 assert(0 != writer);
4909 if ( 0 == extHandle )
4911 return;
4914 GEOSContextHandleInternal_t *handle = 0;
4915 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
4916 if ( 0 != handle->initialized )
4920 writer->setIncludeSRID(newIncludeSRID);
4922 catch (...)
4924 handle->ERROR_MESSAGE("Unknown exception thrown");
4930 //-----------------------------------------------------------------
4931 // Prepared Geometry
4932 //-----------------------------------------------------------------
4934 const geos::geom::prep::PreparedGeometry*
4935 GEOSPrepare_r(GEOSContextHandle_t extHandle, const Geometry *g)
4937 if ( 0 == extHandle )
4939 return 0;
4942 GEOSContextHandleInternal_t *handle = 0;
4943 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
4944 if ( 0 == handle->initialized )
4946 return 0;
4949 const geos::geom::prep::PreparedGeometry* prep = 0;
4953 prep = geos::geom::prep::PreparedGeometryFactory::prepare(g);
4955 catch (const std::exception &e)
4957 handle->ERROR_MESSAGE("%s", e.what());
4959 catch (...)
4961 handle->ERROR_MESSAGE("Unknown exception thrown");
4964 return prep;
4967 void
4968 GEOSPreparedGeom_destroy_r(GEOSContextHandle_t extHandle, const geos::geom::prep::PreparedGeometry *a)
4970 GEOSContextHandleInternal_t *handle = 0;
4974 delete a;
4976 catch (const std::exception &e)
4978 if ( 0 == extHandle )
4980 return;
4983 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
4984 if ( 0 == handle->initialized )
4986 return;
4989 handle->ERROR_MESSAGE("%s", e.what());
4991 catch (...)
4993 if ( 0 == extHandle )
4995 return;
4998 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
4999 if ( 0 == handle->initialized )
5001 return;
5004 handle->ERROR_MESSAGE("Unknown exception thrown");
5008 char
5009 GEOSPreparedContains_r(GEOSContextHandle_t extHandle,
5010 const geos::geom::prep::PreparedGeometry *pg, const Geometry *g)
5012 assert(0 != pg);
5013 assert(0 != g);
5015 if ( 0 == extHandle )
5017 return 2;
5020 GEOSContextHandleInternal_t *handle = 0;
5021 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
5022 if ( 0 == handle->initialized )
5024 return 2;
5027 try
5029 bool result = pg->contains(g);
5030 return result;
5032 catch (const std::exception &e)
5034 handle->ERROR_MESSAGE("%s", e.what());
5036 catch (...)
5038 handle->ERROR_MESSAGE("Unknown exception thrown");
5041 return 2;
5044 char
5045 GEOSPreparedContainsProperly_r(GEOSContextHandle_t extHandle,
5046 const geos::geom::prep::PreparedGeometry *pg, const Geometry *g)
5048 assert(0 != pg);
5049 assert(0 != g);
5051 if ( 0 == extHandle )
5053 return 2;
5056 GEOSContextHandleInternal_t *handle = 0;
5057 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
5058 if ( 0 == handle->initialized )
5060 return 2;
5063 try
5065 bool result = pg->containsProperly(g);
5066 return result;
5068 catch (const std::exception &e)
5070 handle->ERROR_MESSAGE("%s", e.what());
5072 catch (...)
5074 handle->ERROR_MESSAGE("Unknown exception thrown");
5077 return 2;
5080 char
5081 GEOSPreparedCoveredBy_r(GEOSContextHandle_t extHandle,
5082 const geos::geom::prep::PreparedGeometry *pg, const Geometry *g)
5084 assert(0 != pg);
5085 assert(0 != g);
5087 if ( 0 == extHandle )
5089 return 2;
5092 GEOSContextHandleInternal_t *handle = 0;
5093 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
5094 if ( 0 == handle->initialized )
5096 return 2;
5099 try
5101 bool result = pg->coveredBy(g);
5102 return result;
5104 catch (const std::exception &e)
5106 handle->ERROR_MESSAGE("%s", e.what());
5108 catch (...)
5110 handle->ERROR_MESSAGE("Unknown exception thrown");
5113 return 2;
5116 char
5117 GEOSPreparedCovers_r(GEOSContextHandle_t extHandle,
5118 const geos::geom::prep::PreparedGeometry *pg, const Geometry *g)
5120 assert(0 != pg);
5121 assert(0 != g);
5123 if ( 0 == extHandle )
5125 return 2;
5128 GEOSContextHandleInternal_t *handle = 0;
5129 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
5130 if ( 0 == handle->initialized )
5132 return 2;
5135 try
5137 bool result = pg->covers(g);
5138 return result;
5140 catch (const std::exception &e)
5142 handle->ERROR_MESSAGE("%s", e.what());
5144 catch (...)
5146 handle->ERROR_MESSAGE("Unknown exception thrown");
5149 return 2;
5152 char
5153 GEOSPreparedCrosses_r(GEOSContextHandle_t extHandle,
5154 const geos::geom::prep::PreparedGeometry *pg, const Geometry *g)
5156 assert(0 != pg);
5157 assert(0 != g);
5159 if ( 0 == extHandle )
5161 return 2;
5164 GEOSContextHandleInternal_t *handle = 0;
5165 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
5166 if ( 0 == handle->initialized )
5168 return 2;
5171 try
5173 bool result = pg->crosses(g);
5174 return result;
5176 catch (const std::exception &e)
5178 handle->ERROR_MESSAGE("%s", e.what());
5180 catch (...)
5182 handle->ERROR_MESSAGE("Unknown exception thrown");
5185 return 2;
5188 char
5189 GEOSPreparedDisjoint_r(GEOSContextHandle_t extHandle,
5190 const geos::geom::prep::PreparedGeometry *pg, const Geometry *g)
5192 assert(0 != pg);
5193 assert(0 != g);
5195 if ( 0 == extHandle )
5197 return 2;
5200 GEOSContextHandleInternal_t *handle = 0;
5201 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
5202 if ( 0 == handle->initialized )
5204 return 2;
5207 try
5209 bool result = pg->disjoint(g);
5210 return result;
5212 catch (const std::exception &e)
5214 handle->ERROR_MESSAGE("%s", e.what());
5216 catch (...)
5218 handle->ERROR_MESSAGE("Unknown exception thrown");
5221 return 2;
5224 char
5225 GEOSPreparedIntersects_r(GEOSContextHandle_t extHandle,
5226 const geos::geom::prep::PreparedGeometry *pg, const Geometry *g)
5228 assert(0 != pg);
5229 assert(0 != g);
5231 if ( 0 == extHandle )
5233 return 2;
5236 GEOSContextHandleInternal_t *handle = 0;
5237 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
5238 if ( 0 == handle->initialized )
5240 return 2;
5243 try
5245 bool result = pg->intersects(g);
5246 return result;
5248 catch (const std::exception &e)
5250 handle->ERROR_MESSAGE("%s", e.what());
5252 catch (...)
5254 handle->ERROR_MESSAGE("Unknown exception thrown");
5257 return 2;
5260 char
5261 GEOSPreparedOverlaps_r(GEOSContextHandle_t extHandle,
5262 const geos::geom::prep::PreparedGeometry *pg, const Geometry *g)
5264 assert(0 != pg);
5265 assert(0 != g);
5267 if ( 0 == extHandle )
5269 return 2;
5272 GEOSContextHandleInternal_t *handle = 0;
5273 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
5274 if ( 0 == handle->initialized )
5276 return 2;
5279 try
5281 bool result = pg->overlaps(g);
5282 return result;
5284 catch (const std::exception &e)
5286 handle->ERROR_MESSAGE("%s", e.what());
5288 catch (...)
5290 handle->ERROR_MESSAGE("Unknown exception thrown");
5293 return 2;
5296 char
5297 GEOSPreparedTouches_r(GEOSContextHandle_t extHandle,
5298 const geos::geom::prep::PreparedGeometry *pg, const Geometry *g)
5300 assert(0 != pg);
5301 assert(0 != g);
5303 if ( 0 == extHandle )
5305 return 2;
5308 GEOSContextHandleInternal_t *handle = 0;
5309 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
5310 if ( 0 == handle->initialized )
5312 return 2;
5315 try
5317 bool result = pg->touches(g);
5318 return result;
5320 catch (const std::exception &e)
5322 handle->ERROR_MESSAGE("%s", e.what());
5324 catch (...)
5326 handle->ERROR_MESSAGE("Unknown exception thrown");
5329 return 2;
5332 char
5333 GEOSPreparedWithin_r(GEOSContextHandle_t extHandle,
5334 const geos::geom::prep::PreparedGeometry *pg, const Geometry *g)
5336 assert(0 != pg);
5337 assert(0 != g);
5339 if ( 0 == extHandle )
5341 return 2;
5344 GEOSContextHandleInternal_t *handle = 0;
5345 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
5346 if ( 0 == handle->initialized )
5348 return 2;
5351 try
5353 bool result = pg->within(g);
5354 return result;
5356 catch (const std::exception &e)
5358 handle->ERROR_MESSAGE("%s", e.what());
5360 catch (...)
5362 handle->ERROR_MESSAGE("Unknown exception thrown");
5365 return 2;
5368 //-----------------------------------------------------------------
5369 // STRtree
5370 //-----------------------------------------------------------------
5372 geos::index::strtree::STRtree *
5373 GEOSSTRtree_create_r(GEOSContextHandle_t extHandle,
5374 size_t nodeCapacity)
5376 if ( 0 == extHandle )
5378 return 0;
5381 GEOSContextHandleInternal_t *handle = 0;
5382 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
5383 if ( 0 == handle->initialized )
5385 return 0;
5388 geos::index::strtree::STRtree *tree = 0;
5392 tree = new geos::index::strtree::STRtree(nodeCapacity);
5394 catch (const std::exception &e)
5396 handle->ERROR_MESSAGE("%s", e.what());
5398 catch (...)
5400 handle->ERROR_MESSAGE("Unknown exception thrown");
5403 return tree;
5406 void
5407 GEOSSTRtree_insert_r(GEOSContextHandle_t extHandle,
5408 geos::index::strtree::STRtree *tree,
5409 const geos::geom::Geometry *g,
5410 void *item)
5412 GEOSContextHandleInternal_t *handle = 0;
5413 assert(tree != 0);
5414 assert(g != 0);
5418 tree->insert(g->getEnvelopeInternal(), item);
5420 catch (const std::exception &e)
5422 if ( 0 == extHandle )
5424 return;
5427 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
5428 if ( 0 == handle->initialized )
5430 return;
5433 handle->ERROR_MESSAGE("%s", e.what());
5435 catch (...)
5437 if ( 0 == extHandle )
5439 return;
5442 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
5443 if ( 0 == handle->initialized )
5445 return;
5448 handle->ERROR_MESSAGE("Unknown exception thrown");
5452 void
5453 GEOSSTRtree_query_r(GEOSContextHandle_t extHandle,
5454 geos::index::strtree::STRtree *tree,
5455 const geos::geom::Geometry *g,
5456 GEOSQueryCallback callback,
5457 void *userdata)
5459 GEOSContextHandleInternal_t *handle = 0;
5460 assert(tree != 0);
5461 assert(g != 0);
5462 assert(callback != 0);
5466 CAPI_ItemVisitor visitor(callback, userdata);
5467 tree->query(g->getEnvelopeInternal(), visitor);
5469 catch (const std::exception &e)
5471 if ( 0 == extHandle )
5473 return;
5476 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
5477 if ( 0 == handle->initialized )
5479 return;
5482 handle->ERROR_MESSAGE("%s", e.what());
5484 catch (...)
5486 if ( 0 == extHandle )
5488 return;
5491 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
5492 if ( 0 == handle->initialized )
5494 return;
5497 handle->ERROR_MESSAGE("Unknown exception thrown");
5501 void
5502 GEOSSTRtree_iterate_r(GEOSContextHandle_t extHandle,
5503 geos::index::strtree::STRtree *tree,
5504 GEOSQueryCallback callback,
5505 void *userdata)
5507 GEOSContextHandleInternal_t *handle = 0;
5508 assert(tree != 0);
5509 assert(callback != 0);
5513 CAPI_ItemVisitor visitor(callback, userdata);
5514 tree->iterate(visitor);
5516 catch (const std::exception &e)
5518 if ( 0 == extHandle )
5520 return;
5523 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
5524 if ( 0 == handle->initialized )
5526 return;
5529 handle->ERROR_MESSAGE("%s", e.what());
5531 catch (...)
5533 if ( 0 == extHandle )
5535 return;
5538 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
5539 if ( 0 == handle->initialized )
5541 return;
5544 handle->ERROR_MESSAGE("Unknown exception thrown");
5548 char
5549 GEOSSTRtree_remove_r(GEOSContextHandle_t extHandle,
5550 geos::index::strtree::STRtree *tree,
5551 const geos::geom::Geometry *g,
5552 void *item)
5554 assert(0 != tree);
5555 assert(0 != g);
5557 if ( 0 == extHandle )
5559 return 2;
5562 GEOSContextHandleInternal_t *handle = 0;
5563 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
5564 if ( 0 == handle->initialized )
5566 return 2;
5569 try
5571 bool result = tree->remove(g->getEnvelopeInternal(), item);
5572 return result;
5574 catch (const std::exception &e)
5576 handle->ERROR_MESSAGE("%s", e.what());
5578 catch (...)
5580 handle->ERROR_MESSAGE("Unknown exception thrown");
5583 return 2;
5586 void
5587 GEOSSTRtree_destroy_r(GEOSContextHandle_t extHandle,
5588 geos::index::strtree::STRtree *tree)
5590 GEOSContextHandleInternal_t *handle = 0;
5594 delete tree;
5596 catch (const std::exception &e)
5598 if ( 0 == extHandle )
5600 return;
5603 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
5604 if ( 0 == handle->initialized )
5606 return;
5609 handle->ERROR_MESSAGE("%s", e.what());
5611 catch (...)
5613 if ( 0 == extHandle )
5615 return;
5618 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
5619 if ( 0 == handle->initialized )
5621 return;
5624 handle->ERROR_MESSAGE("Unknown exception thrown");
5628 double
5629 GEOSProject_r(GEOSContextHandle_t extHandle,
5630 const Geometry *g,
5631 const Geometry *p)
5633 if ( 0 == extHandle ) return -1.0;
5634 GEOSContextHandleInternal_t *handle =
5635 reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
5636 if ( handle->initialized == 0 ) return -1.0;
5638 const geos::geom::Point* point = dynamic_cast<const geos::geom::Point*>(p);
5639 if (!point) {
5640 handle->ERROR_MESSAGE("third argument of GEOSProject_r must be Point*");
5641 return -1.0;
5644 const geos::geom::Coordinate* inputPt = p->getCoordinate();
5646 try {
5647 return geos::linearref::LengthIndexedLine(g).project(*inputPt);
5648 } catch (const std::exception &e) {
5649 handle->ERROR_MESSAGE("%s", e.what());
5650 return -1.0;
5651 } catch (...) {
5652 handle->ERROR_MESSAGE("Unknown exception thrown");
5653 return -1.0;
5658 Geometry*
5659 GEOSInterpolate_r(GEOSContextHandle_t extHandle, const Geometry *g, double d)
5661 if ( 0 == extHandle ) return 0;
5662 GEOSContextHandleInternal_t *handle =
5663 reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
5664 if ( handle->initialized == 0 ) return 0;
5666 try {
5667 geos::linearref::LengthIndexedLine lil(g);
5668 geos::geom::Coordinate coord = lil.extractPoint(d);
5669 const GeometryFactory *gf = handle->geomFactory;
5670 Geometry* point = gf->createPoint(coord);
5671 return point;
5672 } catch (const std::exception &e) {
5673 handle->ERROR_MESSAGE("%s", e.what());
5674 return 0;
5675 } catch (...) {
5676 handle->ERROR_MESSAGE("Unknown exception thrown");
5677 return 0;
5682 double
5683 GEOSProjectNormalized_r(GEOSContextHandle_t extHandle, const Geometry *g,
5684 const Geometry *p)
5687 double length;
5688 GEOSLength_r(extHandle, g, &length);
5689 return GEOSProject_r(extHandle, g, p) / length;
5693 Geometry*
5694 GEOSInterpolateNormalized_r(GEOSContextHandle_t extHandle, const Geometry *g,
5695 double d)
5697 double length;
5698 GEOSLength_r(extHandle, g, &length);
5699 return GEOSInterpolate_r(extHandle, g, d * length);
5702 GEOSGeometry*
5703 GEOSGeom_extractUniquePoints_r(GEOSContextHandle_t extHandle,
5704 const GEOSGeometry* g)
5706 if ( 0 == extHandle ) return 0;
5707 GEOSContextHandleInternal_t *handle = 0;
5708 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
5709 if ( handle->initialized == 0 ) return 0;
5711 using namespace geos::geom;
5712 using namespace geos::util;
5717 /* 1: extract points */
5718 std::vector<const Coordinate*> coords;
5719 UniqueCoordinateArrayFilter filter(coords);
5720 g->apply_ro(&filter);
5722 /* 2: for each point, create a geometry and put into a vector */
5723 std::vector<Geometry*>* points = new std::vector<Geometry*>();
5724 points->reserve(coords.size());
5725 const GeometryFactory* factory = g->getFactory();
5726 for (std::vector<const Coordinate*>::iterator it=coords.begin(),
5727 itE=coords.end();
5728 it != itE; ++it)
5730 Geometry* point = factory->createPoint(*(*it));
5731 points->push_back(point);
5734 /* 3: create a multipoint */
5735 return factory->createMultiPoint(points);
5738 catch (const std::exception &e)
5740 handle->ERROR_MESSAGE("%s", e.what());
5741 return 0;
5743 catch (...)
5745 handle->ERROR_MESSAGE("Unknown exception thrown");
5746 return 0;
5750 int GEOSOrientationIndex_r(GEOSContextHandle_t extHandle,
5751 double Ax, double Ay, double Bx, double By, double Px, double Py)
5753 GEOSContextHandleInternal_t *handle = 0;
5755 using geos::geom::Coordinate;
5756 using geos::algorithm::CGAlgorithms;
5758 if ( 0 == extHandle )
5760 return 2;
5763 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
5764 if ( 0 == handle->initialized )
5766 return 2;
5771 Coordinate A(Ax, Ay);
5772 Coordinate B(Bx, By);
5773 Coordinate P(Px, Py);
5774 return CGAlgorithms::orientationIndex(A, B, P);
5776 catch (const std::exception &e)
5778 handle->ERROR_MESSAGE("%s", e.what());
5779 return 2;
5781 catch (...)
5783 handle->ERROR_MESSAGE("Unknown exception thrown");
5784 return 2;
5788 GEOSGeometry *
5789 GEOSSharedPaths_r(GEOSContextHandle_t extHandle, const GEOSGeometry* g1, const GEOSGeometry* g2)
5791 using namespace geos::operation::sharedpaths;
5793 if ( 0 == extHandle ) return 0;
5794 GEOSContextHandleInternal_t *handle =
5795 reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
5796 if ( handle->initialized == 0 ) return 0;
5798 SharedPathsOp::PathList forw, back;
5799 try {
5800 SharedPathsOp::sharedPathsOp(*g1, *g2, forw, back);
5802 catch (const std::exception &e)
5804 SharedPathsOp::clearEdges(forw);
5805 SharedPathsOp::clearEdges(back);
5806 handle->ERROR_MESSAGE("%s", e.what());
5807 return 0;
5809 catch (...)
5811 SharedPathsOp::clearEdges(forw);
5812 SharedPathsOp::clearEdges(back);
5813 handle->ERROR_MESSAGE("Unknown exception thrown");
5814 return 0;
5817 // Now forw and back have the geoms we want to use to construct
5818 // our output GeometryCollections...
5820 const GeometryFactory* factory = g1->getFactory();
5821 size_t count;
5823 std::auto_ptr< std::vector<Geometry*> > out1(
5824 new std::vector<Geometry*>()
5826 count = forw.size();
5827 out1->reserve(count);
5828 for (size_t i=0; i<count; ++i) {
5829 out1->push_back(forw[i]);
5831 std::auto_ptr<Geometry> out1g (
5832 factory->createMultiLineString(out1.release())
5835 std::auto_ptr< std::vector<Geometry*> > out2(
5836 new std::vector<Geometry*>()
5838 count = back.size();
5839 out2->reserve(count);
5840 for (size_t i=0; i<count; ++i) {
5841 out2->push_back(back[i]);
5843 std::auto_ptr<Geometry> out2g (
5844 factory->createMultiLineString(out2.release())
5847 std::auto_ptr< std::vector<Geometry*> > out(
5848 new std::vector<Geometry*>()
5850 out->reserve(2);
5851 out->push_back(out1g.release());
5852 out->push_back(out2g.release());
5854 std::auto_ptr<Geometry> outg (
5855 factory->createGeometryCollection(out.release())
5858 return outg.release();
5862 GEOSGeometry *
5863 GEOSSnap_r(GEOSContextHandle_t extHandle, const GEOSGeometry* g1,
5864 const GEOSGeometry* g2, double tolerance)
5866 using namespace geos::operation::overlay::snap;
5868 if ( 0 == extHandle ) return 0;
5869 GEOSContextHandleInternal_t *handle =
5870 reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
5871 if ( handle->initialized == 0 ) return 0;
5873 try{
5874 GeometrySnapper snapper( *g1 );
5875 std::auto_ptr<Geometry> ret = snapper.snapTo(*g2, tolerance);
5876 return ret.release();
5878 catch (const std::exception &e)
5880 handle->ERROR_MESSAGE("%s", e.what());
5881 return 0;
5883 catch (...)
5885 handle->ERROR_MESSAGE("Unknown exception thrown");
5886 return 0;
5890 BufferParameters *
5891 GEOSBufferParams_create_r(GEOSContextHandle_t extHandle)
5893 if ( 0 == extHandle ) return NULL;
5895 GEOSContextHandleInternal_t *handle = 0;
5896 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
5897 if ( 0 == handle->initialized ) return NULL;
5901 BufferParameters *p = new BufferParameters();
5902 return p;
5904 catch (const std::exception &e)
5906 handle->ERROR_MESSAGE("%s", e.what());
5908 catch (...)
5910 handle->ERROR_MESSAGE("Unknown exception thrown");
5913 return 0;
5916 void
5917 GEOSBufferParams_destroy_r(GEOSContextHandle_t extHandle, BufferParameters* p)
5919 delete p;
5923 GEOSBufferParams_setEndCapStyle_r(GEOSContextHandle_t extHandle,
5924 GEOSBufferParams* p, int style)
5926 if ( 0 == extHandle ) return 0;
5928 GEOSContextHandleInternal_t *handle = 0;
5929 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
5930 if ( 0 == handle->initialized ) return 0;
5934 if ( style > BufferParameters::CAP_SQUARE )
5936 throw IllegalArgumentException("Invalid buffer endCap style");
5938 p->setEndCapStyle(static_cast<BufferParameters::EndCapStyle>(style));
5939 return 1;
5941 catch (const std::exception &e)
5943 handle->ERROR_MESSAGE("%s", e.what());
5945 catch (...)
5947 handle->ERROR_MESSAGE("Unknown exception thrown");
5950 return 0;
5954 GEOSBufferParams_setJoinStyle_r(GEOSContextHandle_t extHandle,
5955 GEOSBufferParams* p, int style)
5957 if ( 0 == extHandle ) return 0;
5959 GEOSContextHandleInternal_t *handle = 0;
5960 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
5961 if ( 0 == handle->initialized ) return 0;
5965 if ( style > BufferParameters::JOIN_BEVEL ) {
5966 throw IllegalArgumentException("Invalid buffer join style");
5968 p->setJoinStyle(static_cast<BufferParameters::JoinStyle>(style));
5969 return 1;
5971 catch (const std::exception &e)
5973 handle->ERROR_MESSAGE("%s", e.what());
5975 catch (...)
5977 handle->ERROR_MESSAGE("Unknown exception thrown");
5980 return 0;
5984 GEOSBufferParams_setMitreLimit_r(GEOSContextHandle_t extHandle,
5985 GEOSBufferParams* p, double limit)
5987 if ( 0 == extHandle ) return 0;
5989 GEOSContextHandleInternal_t *handle = 0;
5990 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
5991 if ( 0 == handle->initialized ) return 0;
5995 p->setMitreLimit(limit);
5996 return 1;
5998 catch (const std::exception &e)
6000 handle->ERROR_MESSAGE("%s", e.what());
6002 catch (...)
6004 handle->ERROR_MESSAGE("Unknown exception thrown");
6007 return 0;
6011 GEOSBufferParams_setQuadrantSegments_r(GEOSContextHandle_t extHandle,
6012 GEOSBufferParams* p, int segs)
6014 if ( 0 == extHandle ) return 0;
6016 GEOSContextHandleInternal_t *handle = 0;
6017 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
6018 if ( 0 == handle->initialized ) return 0;
6022 p->setQuadrantSegments(segs);
6023 return 1;
6025 catch (const std::exception &e)
6027 handle->ERROR_MESSAGE("%s", e.what());
6029 catch (...)
6031 handle->ERROR_MESSAGE("Unknown exception thrown");
6034 return 0;
6038 GEOSBufferParams_setSingleSided_r(GEOSContextHandle_t extHandle,
6039 GEOSBufferParams* p, int ss)
6041 if ( 0 == extHandle ) return 0;
6043 GEOSContextHandleInternal_t *handle = 0;
6044 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
6045 if ( 0 == handle->initialized ) return 0;
6049 p->setSingleSided( (ss != 0) );
6050 return 1;
6052 catch (const std::exception &e)
6054 handle->ERROR_MESSAGE("%s", e.what());
6056 catch (...)
6058 handle->ERROR_MESSAGE("Unknown exception thrown");
6061 return 0;
6064 Geometry *
6065 GEOSBufferWithParams_r(GEOSContextHandle_t extHandle, const Geometry *g1, const BufferParameters* bp, double width)
6067 using geos::operation::buffer::BufferOp;
6069 if ( 0 == extHandle ) return NULL;
6071 GEOSContextHandleInternal_t *handle = 0;
6072 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
6073 if ( 0 == handle->initialized ) return NULL;
6077 BufferOp op(g1, *bp);
6078 Geometry *g3 = op.getResultGeometry(width);
6079 return g3;
6081 catch (const std::exception &e)
6083 handle->ERROR_MESSAGE("%s", e.what());
6085 catch (...)
6087 handle->ERROR_MESSAGE("Unknown exception thrown");
6090 return NULL;
6093 } /* extern "C" */