Add clear() method to CoordinateArraySequence, inline empty()
[geos.git] / capi / geos_ts_c.cpp
blob9b4af887023f35b917c9bb3756f071a398d6be09
1 /************************************************************************
3 * $Id$
5 * C-Wrapper for GEOS library
7 * Copyright (C) 2010 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/simplify/DouglasPeuckerSimplifier.h>
47 #include <geos/simplify/TopologyPreservingSimplifier.h>
48 #include <geos/operation/valid/IsValidOp.h>
49 #include <geos/operation/polygonize/Polygonizer.h>
50 #include <geos/operation/linemerge/LineMerger.h>
51 #include <geos/operation/overlay/OverlayOp.h>
52 #include <geos/operation/union/CascadedPolygonUnion.h>
53 #include <geos/operation/buffer/BufferOp.h>
54 #include <geos/operation/buffer/BufferParameters.h>
55 #include <geos/operation/buffer/BufferBuilder.h>
56 #include <geos/linearref/LengthIndexedLine.h>
57 #include <geos/geom/BinaryOp.h>
58 #include <geos/util/IllegalArgumentException.h>
59 #include <geos/util/UniqueCoordinateArrayFilter.h>
60 #include <geos/util/Machine.h>
61 #include <geos/version.h>
63 // This should go away
64 #include <cmath> // finite
65 #include <cstddef>
66 #include <cstdio>
67 #include <cstdlib>
68 #include <cstring>
69 #include <fstream>
70 #include <iostream>
71 #include <sstream>
72 #include <string>
73 #include <memory>
75 #ifdef _MSC_VER
76 #pragma warning(disable : 4099)
77 #endif
79 // Some extra magic to make type declarations in geos_c.h work -
80 // for cross-checking of types in header.
81 #define GEOSGeometry geos::geom::Geometry
82 #define GEOSPreparedGeometry geos::geom::prep::PreparedGeometry
83 #define GEOSCoordSequence geos::geom::CoordinateSequence
84 #define GEOSSTRtree geos::index::strtree::STRtree
85 #define GEOSWKTReader_t geos::io::WKTReader
86 #define GEOSWKTWriter_t geos::io::WKTWriter
87 #define GEOSWKBReader_t geos::io::WKBReader
88 #define GEOSWKBWriter_t geos::io::WKBWriter
90 #include "geos_c.h"
92 // Intentional, to allow non-standard C elements like C99 functions to be
93 // imported through C++ headers of C library, like <cmath>.
94 using namespace std;
96 /// Define this if you want operations triggering Exceptions to
97 /// be printed.
98 /// (will use the NOTIFY channel - only implemented for GEOSUnion so far)
99 ///
100 #undef VERBOSE_EXCEPTIONS
102 #include <geos/export.h>
105 // import the most frequently used definitions globally
106 using geos::geom::Geometry;
107 using geos::geom::LineString;
108 using geos::geom::Polygon;
109 using geos::geom::CoordinateSequence;
110 using geos::geom::GeometryFactory;
112 using geos::io::WKTReader;
113 using geos::io::WKTWriter;
114 using geos::io::WKBReader;
115 using geos::io::WKBWriter;
117 using geos::operation::overlay::OverlayOp;
118 using geos::operation::overlay::overlayOp;
119 using geos::operation::geounion::CascadedPolygonUnion;
121 using geos::algorithm::distance::DiscreteHausdorffDistance;
123 typedef std::auto_ptr<Geometry> GeomAutoPtr;
125 typedef struct GEOSContextHandleInternal
127 const GeometryFactory *geomFactory;
128 GEOSMessageHandler NOTICE_MESSAGE;
129 GEOSMessageHandler ERROR_MESSAGE;
130 int WKBOutputDims;
131 int WKBByteOrder;
132 int initialized;
133 } GEOSContextHandleInternal_t;
135 // CAPI_ItemVisitor is used internally by the CAPI STRtree
136 // wrappers. It's defined here just to keep it out of the
137 // extern "C" block.
138 class CAPI_ItemVisitor : public geos::index::ItemVisitor {
139 GEOSQueryCallback callback;
140 void *userdata;
141 public:
142 CAPI_ItemVisitor (GEOSQueryCallback cb, void *ud)
143 : ItemVisitor(), callback(cb), userdata(ud) {};
144 void visitItem (void *item) { callback(item, userdata); };
148 //## PROTOTYPES #############################################
150 extern "C" const char GEOS_DLL *GEOSjtsport();
151 extern "C" char GEOS_DLL *GEOSasText(Geometry *g1);
153 extern "C" {
155 char* gstrdup_s(const char* str, const std::size_t size)
157 char* out = static_cast<char*>(std::malloc(size + 1));
158 if (0 != out)
160 // as no strlen call necessary, memcpy may be faster than strcpy
161 std::memcpy(out, str, size + 1);
164 assert(0 != out);
165 return out;
168 char* gstrdup(std::string const& str)
170 return gstrdup_s(str.c_str(), str.size());
174 GEOSContextHandle_t
175 initGEOS_r(GEOSMessageHandler nf, GEOSMessageHandler ef)
177 GEOSContextHandleInternal_t *handle = 0;
178 void *extHandle = 0;
180 extHandle = std::malloc(sizeof(GEOSContextHandleInternal_t));
181 if (0 != extHandle)
183 handle = static_cast<GEOSContextHandleInternal_t*>(extHandle);
184 handle->NOTICE_MESSAGE = nf;
185 handle->ERROR_MESSAGE = ef;
186 handle->geomFactory = GeometryFactory::getDefaultInstance();
187 handle->WKBOutputDims = 2;
188 handle->WKBByteOrder = getMachineByteOrder();
189 handle->initialized = 1;
192 return static_cast<GEOSContextHandle_t>(extHandle);
195 GEOSMessageHandler
196 GEOSContext_setNoticeHandler_r(GEOSContextHandle_t extHandle, GEOSMessageHandler nf)
198 GEOSMessageHandler f;
199 GEOSContextHandleInternal_t *handle = 0;
200 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
201 if ( 0 == handle->initialized )
203 return NULL;
206 f = handle->NOTICE_MESSAGE;
207 handle->NOTICE_MESSAGE = nf;
209 return f;
212 GEOSMessageHandler
213 GEOSContext_setErrorHandler_r(GEOSContextHandle_t extHandle, GEOSMessageHandler nf)
215 GEOSMessageHandler f;
216 GEOSContextHandleInternal_t *handle = 0;
217 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
218 if ( 0 == handle->initialized )
220 return NULL;
223 f = handle->ERROR_MESSAGE;
224 handle->ERROR_MESSAGE = nf;
226 return f;
229 void
230 finishGEOS_r(GEOSContextHandle_t extHandle)
232 // Fix up freeing handle w.r.t. malloc above
233 std::free(extHandle);
234 extHandle = NULL;
237 void
238 GEOSFree_r (GEOSContextHandle_t extHandle, void* buffer)
240 assert(0 != extHandle);
242 std::free(buffer);
245 //-----------------------------------------------------------
246 // relate()-related functions
247 // return 0 = false, 1 = true, 2 = error occured
248 //-----------------------------------------------------------
250 char
251 GEOSDisjoint_r(GEOSContextHandle_t extHandle, const Geometry *g1, const Geometry *g2)
253 if ( 0 == extHandle )
255 return 2;
258 GEOSContextHandleInternal_t *handle = 0;
259 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
260 if ( handle->initialized == 0 )
262 return 2;
267 bool result = g1->disjoint(g2);
268 return result;
271 // TODO: mloskot is going to replace these double-catch block
272 // with a macro to remove redundant code in this and
273 // following functions.
274 catch (const std::exception &e)
276 handle->ERROR_MESSAGE("%s", e.what());
278 catch (...)
280 handle->ERROR_MESSAGE("Unknown exception thrown");
283 return 2;
286 char
287 GEOSTouches_r(GEOSContextHandle_t extHandle, const Geometry *g1, const Geometry *g2)
289 if ( 0 == extHandle )
291 return 2;
294 GEOSContextHandleInternal_t *handle = 0;
295 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
296 if ( 0 == handle->initialized )
298 return 2;
303 bool result = g1->touches(g2);
304 return result;
306 catch (const std::exception &e)
308 handle->ERROR_MESSAGE("%s", e.what());
310 catch (...)
312 handle->ERROR_MESSAGE("Unknown exception thrown");
315 return 2;
318 char
319 GEOSIntersects_r(GEOSContextHandle_t extHandle, const Geometry *g1, const Geometry *g2)
321 if ( 0 == extHandle )
323 return 2;
326 GEOSContextHandleInternal_t *handle = 0;
327 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
328 if ( 0 == handle->initialized )
330 return 2;
335 bool result = g1->intersects(g2);
336 return result;
338 catch (const std::exception &e)
340 handle->ERROR_MESSAGE("%s", e.what());
342 catch (...)
344 handle->ERROR_MESSAGE("Unknown exception thrown");
347 return 2;
350 char
351 GEOSCrosses_r(GEOSContextHandle_t extHandle, const Geometry *g1, const Geometry *g2)
353 if ( 0 == extHandle )
355 return 2;
358 GEOSContextHandleInternal_t *handle = 0;
359 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
360 if ( 0 == handle->initialized )
362 return 2;
367 bool result = g1->crosses(g2);
368 return result;
370 catch (const std::exception &e)
372 handle->ERROR_MESSAGE("%s", e.what());
374 catch (...)
376 handle->ERROR_MESSAGE("Unknown exception thrown");
379 return 2;
382 char
383 GEOSWithin_r(GEOSContextHandle_t extHandle, const Geometry *g1, const Geometry *g2)
385 if ( 0 == extHandle )
387 return 2;
390 GEOSContextHandleInternal_t *handle = 0;
391 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
392 if ( 0 == handle->initialized )
394 return 2;
399 bool result = g1->within(g2);
400 return result;
402 catch (const std::exception &e)
404 handle->ERROR_MESSAGE("%s", e.what());
406 catch (...)
408 handle->ERROR_MESSAGE("Unknown exception thrown");
411 return 2;
414 // call g1->contains(g2)
415 // returns 0 = false
416 // 1 = true
417 // 2 = error was trapped
418 char
419 GEOSContains_r(GEOSContextHandle_t extHandle, const Geometry *g1, const Geometry *g2)
421 if ( 0 == extHandle )
423 return 2;
426 GEOSContextHandleInternal_t *handle = 0;
427 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
428 if ( 0 == handle->initialized )
430 return 2;
435 bool result = g1->contains(g2);
436 return result;
438 catch (const std::exception &e)
440 handle->ERROR_MESSAGE("%s", e.what());
442 catch (...)
444 handle->ERROR_MESSAGE("Unknown exception thrown");
447 return 2;
450 char
451 GEOSOverlaps_r(GEOSContextHandle_t extHandle, const Geometry *g1, const Geometry *g2)
453 if ( 0 == extHandle )
455 return 2;
458 GEOSContextHandleInternal_t *handle = 0;
459 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
460 if ( 0 == handle->initialized )
462 return 2;
467 bool result = g1->overlaps(g2);
468 return result;
470 catch (const std::exception &e)
472 handle->ERROR_MESSAGE("%s", e.what());
474 catch (...)
476 handle->ERROR_MESSAGE("Unknown exception thrown");
479 return 2;
483 //-------------------------------------------------------------------
484 // low-level relate functions
485 //------------------------------------------------------------------
487 char
488 GEOSRelatePattern_r(GEOSContextHandle_t extHandle, const Geometry *g1, const Geometry *g2, const char *pat)
490 if ( 0 == extHandle )
492 return 2;
495 GEOSContextHandleInternal_t *handle = 0;
496 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
497 if ( 0 == handle->initialized )
499 return 2;
504 std::string s(pat);
505 bool result = g1->relate(g2, s);
506 return result;
508 catch (const std::exception &e)
510 handle->ERROR_MESSAGE("%s", e.what());
512 catch (...)
514 handle->ERROR_MESSAGE("Unknown exception thrown");
517 return 2;
520 char *
521 GEOSRelate_r(GEOSContextHandle_t extHandle, const Geometry *g1, const Geometry *g2)
523 if ( 0 == extHandle )
525 return NULL;
528 GEOSContextHandleInternal_t *handle = 0;
529 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
530 if ( 0 == handle->initialized )
532 return NULL;
537 using geos::geom::IntersectionMatrix;
539 IntersectionMatrix* im = g1->relate(g2);
540 if (0 == im)
542 return 0;
545 char *result = gstrdup(im->toString());
547 delete im;
548 im = 0;
550 return result;
552 catch (const std::exception &e)
554 handle->ERROR_MESSAGE("%s", e.what());
556 catch (...)
558 handle->ERROR_MESSAGE("Unknown exception thrown");
561 return NULL;
566 //-----------------------------------------------------------------
567 // isValid
568 //-----------------------------------------------------------------
571 char
572 GEOSisValid_r(GEOSContextHandle_t extHandle, const Geometry *g1)
574 if ( 0 == extHandle )
576 return 2;
579 GEOSContextHandleInternal_t *handle = 0;
580 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
581 if ( 0 == handle->initialized )
583 return 2;
588 using geos::operation::valid::IsValidOp;
589 using geos::operation::valid::TopologyValidationError;
591 IsValidOp ivo(g1);
592 TopologyValidationError *err = ivo.getValidationError();
593 if ( err )
595 handle->NOTICE_MESSAGE("%s", err->toString().c_str());
596 return 0;
598 else
600 return 1;
603 catch (const std::exception &e)
605 handle->ERROR_MESSAGE("%s", e.what());
607 catch (...)
609 handle->ERROR_MESSAGE("Unknown exception thrown");
612 return 2;
615 char *
616 GEOSisValidReason_r(GEOSContextHandle_t extHandle, const Geometry *g1)
618 if ( 0 == extHandle )
620 return NULL;
623 GEOSContextHandleInternal_t *handle = 0;
624 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
625 if ( 0 == handle->initialized )
627 return NULL;
632 using geos::operation::valid::IsValidOp;
633 using geos::operation::valid::TopologyValidationError;
635 char* result = 0;
636 char const* const validstr = "Valid Geometry";
638 IsValidOp ivo(g1);
639 TopologyValidationError *err = ivo.getValidationError();
640 if (0 != err)
642 std::ostringstream ss;
643 ss.precision(15);
644 ss << err->getCoordinate();
645 const std::string errloc = ss.str();
646 std::string errmsg(err->getMessage());
647 errmsg += "[" + errloc + "]";
648 result = gstrdup(errmsg);
650 else
652 result = gstrdup(std::string(validstr));
655 return result;
657 catch (const std::exception &e)
659 handle->ERROR_MESSAGE("%s", e.what());
661 catch (...)
663 handle->ERROR_MESSAGE("Unknown exception thrown");
666 return 0;
669 char
670 GEOSisValidDetail_r(GEOSContextHandle_t extHandle, const Geometry *g,
671 char** reason, const Geometry ** location)
673 if ( 0 == extHandle )
675 return 0;
678 GEOSContextHandleInternal_t *handle = 0;
679 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
680 if ( 0 == handle->initialized )
682 return 0;
687 using geos::operation::valid::IsValidOp;
688 using geos::operation::valid::TopologyValidationError;
690 IsValidOp ivo(g);
691 TopologyValidationError *err = ivo.getValidationError();
692 if (0 != err)
694 *location = handle->geomFactory->createPoint(err->getCoordinate());
695 std::string errmsg(err->getMessage());
696 *reason = gstrdup(errmsg);
697 return 0;
700 *location = 0;
701 *reason = 0;
702 return 1; /* valid */
705 catch (const std::exception &e)
707 handle->ERROR_MESSAGE("%s", e.what());
709 catch (...)
711 handle->ERROR_MESSAGE("Unknown exception thrown");
714 return 2; /* exception */
717 //-----------------------------------------------------------------
718 // general purpose
719 //-----------------------------------------------------------------
721 char
722 GEOSEquals_r(GEOSContextHandle_t extHandle, const Geometry *g1, const Geometry *g2)
724 if ( 0 == extHandle )
726 return 2;
729 GEOSContextHandleInternal_t *handle = 0;
730 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
731 if ( 0 == handle->initialized )
733 return 2;
738 bool result = g1->equals(g2);
739 return result;
741 catch (const std::exception &e)
743 handle->ERROR_MESSAGE("%s", e.what());
745 catch (...)
747 handle->ERROR_MESSAGE("Unknown exception thrown");
750 return 2;
753 char
754 GEOSEqualsExact_r(GEOSContextHandle_t extHandle, const Geometry *g1, const Geometry *g2, double tolerance)
756 if ( 0 == extHandle )
758 return 2;
761 GEOSContextHandleInternal_t *handle = 0;
762 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
763 if ( 0 == handle->initialized )
765 return 2;
770 bool result = g1->equalsExact(g2, tolerance);
771 return result;
773 catch (const std::exception &e)
775 handle->ERROR_MESSAGE("%s", e.what());
777 catch (...)
779 handle->ERROR_MESSAGE("Unknown exception thrown");
782 return 2;
786 GEOSDistance_r(GEOSContextHandle_t extHandle, const Geometry *g1, const Geometry *g2, double *dist)
788 assert(0 != dist);
790 if ( 0 == extHandle )
792 return 0;
795 GEOSContextHandleInternal_t *handle = 0;
796 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
797 if ( 0 == handle->initialized )
799 return 0;
804 *dist = g1->distance(g2);
805 return 1;
807 catch (const std::exception &e)
809 handle->ERROR_MESSAGE("%s", e.what());
811 catch (...)
813 handle->ERROR_MESSAGE("Unknown exception thrown");
816 return 0;
820 GEOSHausdorffDistance_r(GEOSContextHandle_t extHandle, const Geometry *g1, const Geometry *g2, double *dist)
822 assert(0 != dist);
824 if ( 0 == extHandle )
826 return 0;
829 GEOSContextHandleInternal_t *handle = 0;
830 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
831 if ( 0 == handle->initialized )
833 return 0;
838 *dist = DiscreteHausdorffDistance::distance(*g1, *g2);
839 return 1;
841 catch (const std::exception &e)
843 handle->ERROR_MESSAGE("%s", e.what());
845 catch (...)
847 handle->ERROR_MESSAGE("Unknown exception thrown");
850 return 0;
854 GEOSHausdorffDistanceDensify_r(GEOSContextHandle_t extHandle, const Geometry *g1, const Geometry *g2, double densifyFrac, double *dist)
856 assert(0 != dist);
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 *dist = DiscreteHausdorffDistance::distance(*g1, *g2, densifyFrac);
873 return 1;
875 catch (const std::exception &e)
877 handle->ERROR_MESSAGE("%s", e.what());
879 catch (...)
881 handle->ERROR_MESSAGE("Unknown exception thrown");
884 return 0;
888 GEOSArea_r(GEOSContextHandle_t extHandle, const Geometry *g, double *area)
890 assert(0 != area);
892 if ( 0 == extHandle )
894 return 0;
897 GEOSContextHandleInternal_t *handle = 0;
898 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
899 if ( 0 == handle->initialized )
901 return 0;
906 *area = g->getArea();
907 return 1;
909 catch (const std::exception &e)
911 handle->ERROR_MESSAGE("%s", e.what());
913 catch (...)
915 handle->ERROR_MESSAGE("Unknown exception thrown");
918 return 0;
922 GEOSLength_r(GEOSContextHandle_t extHandle, const Geometry *g, double *length)
924 assert(0 != length);
926 if ( 0 == extHandle )
928 return 2;
931 GEOSContextHandleInternal_t *handle = 0;
932 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
933 if ( 0 == handle->initialized )
935 return 0;
940 *length = g->getLength();
941 return 1;
943 catch (const std::exception &e)
945 handle->ERROR_MESSAGE("%s", e.what());
947 catch (...)
949 handle->ERROR_MESSAGE("Unknown exception thrown");
952 return 0;
955 Geometry *
956 GEOSGeomFromWKT_r(GEOSContextHandle_t extHandle, const char *wkt)
958 if ( 0 == extHandle )
960 return NULL;
963 GEOSContextHandleInternal_t *handle = 0;
964 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
965 if ( 0 == handle->initialized )
967 return NULL;
972 const std::string wktstring(wkt);
973 WKTReader r(static_cast<GeometryFactory const*>(handle->geomFactory));
975 Geometry *g = r.read(wktstring);
976 return g;
978 catch (const std::exception &e)
980 handle->ERROR_MESSAGE("%s", e.what());
982 catch (...)
984 handle->ERROR_MESSAGE("Unknown exception thrown");
987 return NULL;
990 char *
991 GEOSGeomToWKT_r(GEOSContextHandle_t extHandle, const Geometry *g1)
993 if ( 0 == extHandle )
995 return NULL;
998 GEOSContextHandleInternal_t *handle = 0;
999 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
1000 if ( 0 == handle->initialized )
1002 return NULL;
1008 char *result = gstrdup(g1->toString());
1009 return result;
1011 catch (const std::exception &e)
1013 handle->ERROR_MESSAGE("%s", e.what());
1015 catch (...)
1017 handle->ERROR_MESSAGE("Unknown exception thrown");
1019 return NULL;
1022 // Remember to free the result!
1023 unsigned char *
1024 GEOSGeomToWKB_buf_r(GEOSContextHandle_t extHandle, const Geometry *g, size_t *size)
1026 assert(0 != size);
1028 if ( 0 == extHandle )
1030 return NULL;
1033 GEOSContextHandleInternal_t *handle = 0;
1034 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
1035 if ( 0 == handle->initialized )
1037 return NULL;
1040 using geos::io::WKBWriter;
1043 int byteOrder = static_cast<int>(handle->WKBByteOrder);
1044 WKBWriter w(handle->WKBOutputDims, byteOrder);
1045 std::ostringstream os(std::ios_base::binary);
1046 w.write(*g, os);
1047 std::string wkbstring(os.str());
1048 const std::size_t len = wkbstring.length();
1050 unsigned char* result = 0;
1051 result = static_cast<unsigned char*>(std::malloc(len));
1052 if (0 != result)
1054 std::memcpy(result, wkbstring.c_str(), len);
1055 *size = len;
1057 return result;
1059 catch (const std::exception &e)
1061 handle->ERROR_MESSAGE("%s", e.what());
1063 catch (...)
1065 handle->ERROR_MESSAGE("Unknown exception thrown");
1068 return NULL;
1071 Geometry *
1072 GEOSGeomFromWKB_buf_r(GEOSContextHandle_t extHandle, const unsigned char *wkb, size_t size)
1074 if ( 0 == extHandle )
1076 return NULL;
1079 GEOSContextHandleInternal_t *handle = 0;
1080 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
1081 if ( 0 == handle->initialized )
1083 return NULL;
1086 using geos::io::WKBReader;
1089 std::string wkbstring(reinterpret_cast<const char*>(wkb), size); // make it binary !
1090 WKBReader r(*(static_cast<GeometryFactory const*>(handle->geomFactory)));
1091 std::istringstream is(std::ios_base::binary);
1092 is.str(wkbstring);
1093 is.seekg(0, std::ios::beg); // rewind reader pointer
1094 Geometry *g = r.read(is);
1095 return g;
1097 catch (const std::exception &e)
1099 handle->ERROR_MESSAGE("%s", e.what());
1101 catch (...)
1103 handle->ERROR_MESSAGE("Unknown exception thrown");
1106 return NULL;
1109 /* Read/write wkb hex values. Returned geometries are
1110 owned by the caller.*/
1111 unsigned char *
1112 GEOSGeomToHEX_buf_r(GEOSContextHandle_t extHandle, const Geometry *g, size_t *size)
1114 if ( 0 == extHandle )
1116 return NULL;
1119 GEOSContextHandleInternal_t *handle = 0;
1120 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
1121 if ( 0 == handle->initialized )
1123 return NULL;
1126 using geos::io::WKBWriter;
1129 int byteOrder = static_cast<int>(handle->WKBByteOrder);
1130 WKBWriter w(handle->WKBOutputDims, byteOrder);
1131 std::ostringstream os(std::ios_base::binary);
1132 w.writeHEX(*g, os);
1133 std::string hexstring(os.str());
1135 char *result = gstrdup(hexstring);
1136 if (0 != result)
1138 *size = hexstring.length();
1141 return reinterpret_cast<unsigned char*>(result);
1143 catch (const std::exception &e)
1145 handle->ERROR_MESSAGE("%s", e.what());
1147 catch (...)
1149 handle->ERROR_MESSAGE("Unknown exception thrown");
1152 return NULL;
1155 Geometry *
1156 GEOSGeomFromHEX_buf_r(GEOSContextHandle_t extHandle, const unsigned char *hex, size_t size)
1158 if ( 0 == extHandle )
1160 return NULL;
1163 GEOSContextHandleInternal_t *handle = 0;
1164 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
1165 if ( 0 == handle->initialized )
1167 return NULL;
1170 using geos::io::WKBReader;
1173 std::string hexstring(reinterpret_cast<const char*>(hex), size);
1174 WKBReader r(*(static_cast<GeometryFactory const*>(handle->geomFactory)));
1175 std::istringstream is(std::ios_base::binary);
1176 is.str(hexstring);
1177 is.seekg(0, std::ios::beg); // rewind reader pointer
1179 Geometry *g = r.readHEX(is);
1180 return g;
1182 catch (const std::exception &e)
1184 handle->ERROR_MESSAGE("%s", e.what());
1186 catch (...)
1188 handle->ERROR_MESSAGE("Unknown exception thrown");
1191 return NULL;
1194 char
1195 GEOSisEmpty_r(GEOSContextHandle_t extHandle, const Geometry *g1)
1197 if ( 0 == extHandle )
1199 return 2;
1202 GEOSContextHandleInternal_t *handle = 0;
1203 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
1204 if ( 0 == handle->initialized )
1206 return 2;
1211 return g1->isEmpty();
1213 catch (const std::exception &e)
1215 handle->ERROR_MESSAGE("%s", e.what());
1217 catch (...)
1219 handle->ERROR_MESSAGE("Unknown exception thrown");
1222 return 2;
1225 char
1226 GEOSisSimple_r(GEOSContextHandle_t extHandle, const Geometry *g1)
1228 if ( 0 == extHandle )
1230 return 2;
1233 GEOSContextHandleInternal_t *handle = 0;
1234 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
1235 if ( 0 == handle->initialized )
1237 return 2;
1242 return g1->isSimple();
1244 catch (const std::exception &e)
1246 handle->ERROR_MESSAGE("%s", e.what());
1247 return 2;
1250 catch (...)
1252 handle->ERROR_MESSAGE("Unknown exception thrown");
1253 return 2;
1257 char
1258 GEOSisRing_r(GEOSContextHandle_t extHandle, const Geometry *g)
1260 if ( 0 == extHandle )
1262 return 2;
1265 GEOSContextHandleInternal_t *handle = 0;
1266 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
1267 if ( 0 == handle->initialized )
1269 return 2;
1274 const LineString *ls = dynamic_cast<const LineString *>(g);
1275 if ( ls ) {
1276 return (ls->isRing());
1277 } else {
1278 return 0;
1281 catch (const std::exception &e)
1283 handle->ERROR_MESSAGE("%s", e.what());
1284 return 2;
1287 catch (...)
1289 handle->ERROR_MESSAGE("Unknown exception thrown");
1290 return 2;
1296 //free the result of this
1297 char *
1298 GEOSGeomType_r(GEOSContextHandle_t extHandle, const Geometry *g1)
1300 if ( 0 == extHandle )
1302 return NULL;
1305 GEOSContextHandleInternal_t *handle = 0;
1306 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
1307 if ( 0 == handle->initialized )
1309 return NULL;
1314 std::string s = g1->getGeometryType();
1316 char *result = gstrdup(s);
1317 return result;
1319 catch (const std::exception &e)
1321 handle->ERROR_MESSAGE("%s", e.what());
1323 catch (...)
1325 handle->ERROR_MESSAGE("Unknown exception thrown");
1328 return NULL;
1331 // Return postgis geometry type index
1333 GEOSGeomTypeId_r(GEOSContextHandle_t extHandle, const Geometry *g1)
1335 if ( 0 == extHandle )
1337 return -1;
1340 GEOSContextHandleInternal_t *handle = 0;
1341 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
1342 if ( 0 == handle->initialized )
1344 return -1;
1349 return g1->getGeometryTypeId();
1351 catch (const std::exception &e)
1353 handle->ERROR_MESSAGE("%s", e.what());
1355 catch (...)
1357 handle->ERROR_MESSAGE("Unknown exception thrown");
1360 return -1;
1363 //-------------------------------------------------------------------
1364 // GEOS functions that return geometries
1365 //-------------------------------------------------------------------
1367 Geometry *
1368 GEOSEnvelope_r(GEOSContextHandle_t extHandle, const Geometry *g1)
1370 if ( 0 == extHandle )
1372 return NULL;
1375 GEOSContextHandleInternal_t *handle = 0;
1376 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
1377 if ( 0 == handle->initialized )
1379 return NULL;
1384 Geometry *g3 = g1->getEnvelope();
1385 return g3;
1387 catch (const std::exception &e)
1389 handle->ERROR_MESSAGE("%s", e.what());
1391 catch (...)
1393 handle->ERROR_MESSAGE("Unknown exception thrown");
1396 return NULL;
1399 Geometry *
1400 GEOSIntersection_r(GEOSContextHandle_t extHandle, const Geometry *g1, const Geometry *g2)
1402 if ( 0 == extHandle )
1404 return NULL;
1407 GEOSContextHandleInternal_t *handle = 0;
1408 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
1409 if ( 0 == handle->initialized )
1411 return NULL;
1416 GeomAutoPtr g3(BinaryOp(g1, g2, overlayOp(OverlayOp::opINTERSECTION)));
1417 return g3.release();
1419 // XXX: old version
1420 //Geometry *g3 = g1->intersection(g2);
1421 //return g3;
1423 catch (const std::exception &e)
1425 handle->ERROR_MESSAGE("%s", e.what());
1427 catch (...)
1429 handle->ERROR_MESSAGE("Unknown exception thrown");
1432 return NULL;
1435 Geometry *
1436 GEOSBuffer_r(GEOSContextHandle_t extHandle, const Geometry *g1, double width, int quadrantsegments)
1438 if ( 0 == extHandle )
1440 return NULL;
1443 GEOSContextHandleInternal_t *handle = 0;
1444 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
1445 if ( 0 == handle->initialized )
1447 return NULL;
1452 Geometry *g3 = g1->buffer(width, quadrantsegments);
1453 return g3;
1455 catch (const std::exception &e)
1457 handle->ERROR_MESSAGE("%s", e.what());
1459 catch (...)
1461 handle->ERROR_MESSAGE("Unknown exception thrown");
1464 return NULL;
1467 Geometry *
1468 GEOSBufferWithStyle_r(GEOSContextHandle_t extHandle, const Geometry *g1, double width, int quadsegs, int endCapStyle, int joinStyle, double mitreLimit)
1470 using geos::operation::buffer::BufferParameters;
1471 using geos::operation::buffer::BufferOp;
1472 using geos::util::IllegalArgumentException;
1474 if ( 0 == extHandle )
1476 return NULL;
1479 GEOSContextHandleInternal_t *handle = 0;
1480 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
1481 if ( 0 == handle->initialized )
1483 return NULL;
1488 BufferParameters bp;
1489 bp.setQuadrantSegments(quadsegs);
1491 if ( endCapStyle > BufferParameters::CAP_SQUARE )
1493 throw IllegalArgumentException("Invalid buffer endCap style");
1495 bp.setEndCapStyle(
1496 static_cast<BufferParameters::EndCapStyle>(endCapStyle)
1499 if ( joinStyle > BufferParameters::JOIN_BEVEL )
1501 throw IllegalArgumentException("Invalid buffer join style");
1503 bp.setJoinStyle(
1504 static_cast<BufferParameters::JoinStyle>(joinStyle)
1506 bp.setMitreLimit(mitreLimit);
1507 BufferOp op(g1, bp);
1508 Geometry *g3 = op.getResultGeometry(width);
1509 return g3;
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 Geometry *
1524 GEOSSingleSidedBuffer_r(GEOSContextHandle_t extHandle, const Geometry *g1, double width, int quadsegs, int joinStyle, double mitreLimit, int leftSide)
1526 using geos::operation::buffer::BufferParameters;
1527 using geos::operation::buffer::BufferBuilder;
1528 using geos::operation::buffer::BufferOp;
1529 using geos::util::IllegalArgumentException;
1531 if ( 0 == extHandle )
1533 return NULL;
1536 GEOSContextHandleInternal_t *handle = 0;
1537 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
1538 if ( 0 == handle->initialized )
1540 return NULL;
1545 BufferParameters bp;
1546 bp.setEndCapStyle( BufferParameters::CAP_FLAT );
1547 bp.setQuadrantSegments(quadsegs);
1549 if ( joinStyle > BufferParameters::JOIN_BEVEL )
1551 throw IllegalArgumentException("Invalid buffer join style");
1553 bp.setJoinStyle(
1554 static_cast<BufferParameters::JoinStyle>(joinStyle)
1556 bp.setMitreLimit(mitreLimit);
1558 bool isLeftSide = leftSide == 0 ? false : true;
1559 BufferBuilder bufBuilder (bp);
1560 Geometry *g3 = bufBuilder.bufferLineSingleSided(g1, width, isLeftSide);
1562 return g3;
1564 catch (const std::exception &e)
1566 handle->ERROR_MESSAGE("%s", e.what());
1568 catch (...)
1570 handle->ERROR_MESSAGE("Unknown exception thrown");
1573 return NULL;
1576 Geometry *
1577 GEOSConvexHull_r(GEOSContextHandle_t extHandle, const Geometry *g1)
1579 if ( 0 == extHandle )
1581 return NULL;
1584 GEOSContextHandleInternal_t *handle = 0;
1585 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
1586 if ( 0 == handle->initialized )
1588 return NULL;
1593 Geometry *g3 = g1->convexHull();
1594 return g3;
1596 catch (const std::exception &e)
1598 handle->ERROR_MESSAGE("%s", e.what());
1600 catch (...)
1602 handle->ERROR_MESSAGE("Unknown exception thrown");
1605 return NULL;
1608 Geometry *
1609 GEOSDifference_r(GEOSContextHandle_t extHandle, const Geometry *g1, const Geometry *g2)
1611 if ( 0 == extHandle )
1613 return NULL;
1616 GEOSContextHandleInternal_t *handle = 0;
1617 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
1618 if ( 0 == handle->initialized )
1620 return NULL;
1625 GeomAutoPtr g3(BinaryOp(g1, g2, overlayOp(OverlayOp::opDIFFERENCE)));
1626 return g3.release();
1628 // XXX: old version
1629 //Geometry *g3 = g1->difference(g2);
1630 //return g3;
1632 catch (const std::exception &e)
1634 handle->ERROR_MESSAGE("%s", e.what());
1636 catch (...)
1638 handle->ERROR_MESSAGE("Unknown exception thrown");
1641 return NULL;
1644 Geometry *
1645 GEOSBoundary_r(GEOSContextHandle_t extHandle, const Geometry *g1)
1647 if ( 0 == extHandle )
1649 return NULL;
1652 GEOSContextHandleInternal_t *handle = 0;
1653 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
1654 if ( 0 == handle->initialized )
1656 return NULL;
1661 Geometry *g3 = g1->getBoundary();
1662 return g3;
1664 catch (const std::exception &e)
1666 handle->ERROR_MESSAGE("%s", e.what());
1668 catch (...)
1670 handle->ERROR_MESSAGE("Unknown exception thrown");
1673 return NULL;
1676 Geometry *
1677 GEOSSymDifference_r(GEOSContextHandle_t extHandle, const Geometry *g1, const Geometry *g2)
1679 if ( 0 == extHandle )
1681 return NULL;
1684 GEOSContextHandleInternal_t *handle = 0;
1685 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
1686 if ( 0 == handle->initialized )
1688 return NULL;
1693 GeomAutoPtr g3 = BinaryOp(g1, g2, overlayOp(OverlayOp::opSYMDIFFERENCE));
1694 return g3.release();
1695 //Geometry *g3 = g1->symDifference(g2);
1696 //return g3;
1698 catch (const std::exception &e)
1700 handle->ERROR_MESSAGE("%s", e.what());
1701 return NULL;
1704 catch (...)
1706 handle->ERROR_MESSAGE("Unknown exception thrown");
1707 return NULL;
1711 Geometry *
1712 GEOSUnion_r(GEOSContextHandle_t extHandle, const Geometry *g1, const Geometry *g2)
1714 if ( 0 == extHandle )
1716 return NULL;
1719 GEOSContextHandleInternal_t *handle = 0;
1720 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
1721 if ( 0 == handle->initialized )
1723 return NULL;
1728 GeomAutoPtr g3 = BinaryOp(g1, g2, overlayOp(OverlayOp::opUNION));
1729 return g3.release();
1731 // XXX: old version
1732 //Geometry *g3 = g1->Union(g2);
1733 //return g3;
1735 catch (const std::exception &e)
1737 #if VERBOSE_EXCEPTIONS
1738 std::ostringstream s;
1739 s << "Exception on GEOSUnion with following inputs:" << std::endl;
1740 s << "A: "<<g1->toString() << std::endl;
1741 s << "B: "<<g2->toString() << std::endl;
1742 handle->NOTICE_MESSAGE("%s", s.str().c_str());
1743 #endif // VERBOSE_EXCEPTIONS
1744 handle->ERROR_MESSAGE("%s", e.what());
1746 catch (...)
1748 handle->ERROR_MESSAGE("Unknown exception thrown");
1751 return NULL;
1754 Geometry *
1755 GEOSUnionCascaded_r(GEOSContextHandle_t extHandle, const Geometry *g1)
1757 if ( 0 == extHandle )
1759 return NULL;
1762 GEOSContextHandleInternal_t *handle = 0;
1763 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
1764 if ( 0 == handle->initialized )
1766 return NULL;
1771 const geos::geom::MultiPolygon *p = dynamic_cast<const geos::geom::MultiPolygon *>(g1);
1772 if ( ! p )
1774 handle->ERROR_MESSAGE("Invalid argument (must be a MultiPolygon)");
1775 return NULL;
1778 using geos::operation::geounion::CascadedPolygonUnion;
1779 return CascadedPolygonUnion::Union(p);
1781 catch (const std::exception &e)
1783 handle->ERROR_MESSAGE("%s", e.what());
1785 catch (...)
1787 handle->ERROR_MESSAGE("Unknown exception thrown");
1790 return NULL;
1793 Geometry *
1794 GEOSPointOnSurface_r(GEOSContextHandle_t extHandle, const Geometry *g1)
1796 if ( 0 == extHandle )
1798 return NULL;
1801 GEOSContextHandleInternal_t *handle = 0;
1802 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
1803 if ( 0 == handle->initialized )
1805 return NULL;
1810 Geometry *ret = g1->getInteriorPoint();
1811 if ( ! ret )
1813 const GeometryFactory* gf = handle->geomFactory;
1814 // return an empty collection
1815 return gf->createGeometryCollection();
1817 return ret;
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 //-------------------------------------------------------------------
1832 // memory management functions
1833 //------------------------------------------------------------------
1835 void
1836 GEOSGeom_destroy_r(GEOSContextHandle_t extHandle, Geometry *a)
1838 GEOSContextHandleInternal_t *handle = 0;
1840 // FIXME: mloskot: Does this try-catch around delete means that
1841 // destructors in GEOS may throw? If it does, this is a serious
1842 // violation of "never throw an exception from a destructor" principle
1846 delete a;
1848 catch (const std::exception &e)
1850 if ( 0 == extHandle )
1852 return;
1855 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
1856 if ( 0 == handle->initialized )
1858 return;
1861 handle->ERROR_MESSAGE("%s", e.what());
1863 catch (...)
1865 if ( 0 == extHandle )
1867 return;
1870 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
1871 if ( 0 == handle->initialized )
1873 return;
1876 handle->ERROR_MESSAGE("Unknown exception thrown");
1880 void
1881 GEOSSetSRID_r(GEOSContextHandle_t extHandle, Geometry *g, int srid)
1883 assert(0 != g);
1885 if ( 0 == extHandle )
1887 return;
1890 GEOSContextHandleInternal_t *handle = 0;
1891 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
1892 if ( 0 == handle->initialized )
1894 return;
1897 g->setSRID(srid);
1902 GEOSGetNumCoordinates_r(GEOSContextHandle_t extHandle, const Geometry *g)
1904 assert(0 != g);
1906 if ( 0 == extHandle )
1908 return -1;
1911 GEOSContextHandleInternal_t *handle = 0;
1912 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
1913 if ( 0 == handle->initialized )
1915 return -1;
1920 return static_cast<int>(g->getNumPoints());
1922 catch (const std::exception &e)
1924 handle->ERROR_MESSAGE("%s", e.what());
1926 catch (...)
1928 handle->ERROR_MESSAGE("Unknown exception thrown");
1931 return -1;
1935 * Return -1 on exception, 0 otherwise.
1936 * Converts Geometry to normal form (or canonical form).
1939 GEOSNormalize_r(GEOSContextHandle_t extHandle, Geometry *g)
1941 assert(0 != g);
1943 if ( 0 == extHandle )
1945 return -1;
1948 GEOSContextHandleInternal_t *handle = 0;
1949 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
1950 if ( 0 == handle->initialized )
1952 return -1;
1957 g->normalize();
1958 return 0; // SUCCESS
1960 catch (const std::exception &e)
1962 handle->ERROR_MESSAGE("%s", e.what());
1964 catch (...)
1966 handle->ERROR_MESSAGE("Unknown exception thrown");
1969 return -1;
1973 GEOSGetNumInteriorRings_r(GEOSContextHandle_t extHandle, const Geometry *g1)
1975 if ( 0 == extHandle )
1977 return -1;
1980 GEOSContextHandleInternal_t *handle = 0;
1981 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
1982 if ( 0 == handle->initialized )
1984 return -1;
1989 const Polygon *p = dynamic_cast<const Polygon *>(g1);
1990 if ( ! p )
1992 handle->ERROR_MESSAGE("Argument is not a Polygon");
1993 return -1;
1995 return static_cast<int>(p->getNumInteriorRing());
1997 catch (const std::exception &e)
1999 handle->ERROR_MESSAGE("%s", e.what());
2001 catch (...)
2003 handle->ERROR_MESSAGE("Unknown exception thrown");
2006 return -1;
2010 // returns -1 on error and 1 for non-multi geometries
2012 GEOSGetNumGeometries_r(GEOSContextHandle_t extHandle, const Geometry *g1)
2014 if ( 0 == extHandle )
2016 return -1;
2019 GEOSContextHandleInternal_t *handle = 0;
2020 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
2021 if ( 0 == handle->initialized )
2023 return -1;
2028 return static_cast<int>(g1->getNumGeometries());
2030 catch (const std::exception &e)
2032 handle->ERROR_MESSAGE("%s", e.what());
2034 catch (...)
2036 handle->ERROR_MESSAGE("Unknown exception thrown");
2039 return -1;
2044 * Call only on GEOMETRYCOLLECTION or MULTI*.
2045 * Return a pointer to the internal Geometry.
2047 const Geometry *
2048 GEOSGetGeometryN_r(GEOSContextHandle_t extHandle, const Geometry *g1, int n)
2050 if ( 0 == extHandle )
2052 return NULL;
2055 GEOSContextHandleInternal_t *handle = 0;
2056 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
2057 if ( 0 == handle->initialized )
2059 return NULL;
2064 return g1->getGeometryN(n);
2066 catch (const std::exception &e)
2068 handle->ERROR_MESSAGE("%s", e.what());
2070 catch (...)
2072 handle->ERROR_MESSAGE("Unknown exception thrown");
2075 return NULL;
2080 * Call only on polygon
2081 * Return a copy of the internal Geometry.
2083 const Geometry *
2084 GEOSGetExteriorRing_r(GEOSContextHandle_t extHandle, const Geometry *g1)
2086 if ( 0 == extHandle )
2088 return NULL;
2091 GEOSContextHandleInternal_t *handle = 0;
2092 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
2093 if ( 0 == handle->initialized )
2095 return NULL;
2100 const Polygon *p = dynamic_cast<const Polygon *>(g1);
2101 if ( ! p )
2103 handle->ERROR_MESSAGE("Invalid argument (must be a Polygon)");
2104 return NULL;
2106 return p->getExteriorRing();
2108 catch (const std::exception &e)
2110 handle->ERROR_MESSAGE("%s", e.what());
2112 catch (...)
2114 handle->ERROR_MESSAGE("Unknown exception thrown");
2117 return NULL;
2121 * Call only on polygon
2122 * Return a pointer to internal storage, do not destroy it.
2124 const Geometry *
2125 GEOSGetInteriorRingN_r(GEOSContextHandle_t extHandle, const Geometry *g1, int n)
2127 if ( 0 == extHandle )
2129 return NULL;
2132 GEOSContextHandleInternal_t *handle = 0;
2133 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
2134 if ( 0 == handle->initialized )
2136 return NULL;
2141 const Polygon *p = dynamic_cast<const Polygon *>(g1);
2142 if ( ! p )
2144 handle->ERROR_MESSAGE("Invalid argument (must be a Polygon)");
2145 return NULL;
2147 return p->getInteriorRingN(n);
2149 catch (const std::exception &e)
2151 handle->ERROR_MESSAGE("%s", e.what());
2153 catch (...)
2155 handle->ERROR_MESSAGE("Unknown exception thrown");
2158 return NULL;
2161 Geometry *
2162 GEOSGetCentroid_r(GEOSContextHandle_t extHandle, const Geometry *g)
2164 if ( 0 == extHandle )
2166 return NULL;
2169 GEOSContextHandleInternal_t *handle = 0;
2170 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
2171 if ( 0 == handle->initialized )
2173 return NULL;
2178 Geometry *ret = g->getCentroid();
2179 if (0 == ret)
2181 const GeometryFactory *gf = handle->geomFactory;
2182 return gf->createGeometryCollection();
2184 return ret;
2186 catch (const std::exception &e)
2188 handle->ERROR_MESSAGE("%s", e.what());
2190 catch (...)
2192 handle->ERROR_MESSAGE("Unknown exception thrown");
2195 return NULL;
2198 Geometry *
2199 GEOSGeom_createEmptyCollection_r(GEOSContextHandle_t extHandle, int type)
2201 if ( 0 == extHandle )
2203 return NULL;
2206 GEOSContextHandleInternal_t *handle = 0;
2207 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
2208 if ( 0 == handle->initialized )
2210 return NULL;
2213 #ifdef GEOS_DEBUG
2214 char buf[256];
2215 sprintf(buf, "createCollection: requested type %d, ngeoms: %d",
2216 type, ngeoms);
2217 handle->NOTICE_MESSAGE("%s", buf);// TODO: Can handle->NOTICE_MESSAGE format that directly?
2218 #endif
2222 const GeometryFactory* gf = handle->geomFactory;
2224 Geometry *g = 0;
2225 switch (type)
2227 case GEOS_GEOMETRYCOLLECTION:
2228 g = gf->createGeometryCollection();
2229 break;
2230 case GEOS_MULTIPOINT:
2231 g = gf->createMultiPoint();
2232 break;
2233 case GEOS_MULTILINESTRING:
2234 g = gf->createMultiLineString();
2235 break;
2236 case GEOS_MULTIPOLYGON:
2237 g = gf->createMultiPolygon();
2238 break;
2239 default:
2240 handle->ERROR_MESSAGE("Unsupported type request for GEOSGeom_createEmptyCollection_r");
2241 g = 0;
2245 return g;
2247 catch (const std::exception &e)
2249 handle->ERROR_MESSAGE("%s", e.what());
2251 catch (...)
2253 handle->ERROR_MESSAGE("Unknown exception thrown");
2256 return 0;
2259 Geometry *
2260 GEOSGeom_createCollection_r(GEOSContextHandle_t extHandle, int type, Geometry **geoms, unsigned int ngeoms)
2262 if ( 0 == extHandle )
2264 return NULL;
2267 GEOSContextHandleInternal_t *handle = 0;
2268 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
2269 if ( 0 == handle->initialized )
2271 return NULL;
2274 #ifdef GEOS_DEBUG
2275 char buf[256];
2276 sprintf(buf, "PostGIS2GEOS_collection: requested type %d, ngeoms: %d",
2277 type, ngeoms);
2278 handle->NOTICE_MESSAGE("%s", buf);// TODO: Can handle->NOTICE_MESSAGE format that directly?
2279 #endif
2283 const GeometryFactory* gf = handle->geomFactory;
2284 std::vector<Geometry*>* vgeoms = new std::vector<Geometry*>(geoms, geoms + ngeoms);
2286 Geometry *g = 0;
2287 switch (type)
2289 case GEOS_GEOMETRYCOLLECTION:
2290 g = gf->createGeometryCollection(vgeoms);
2291 break;
2292 case GEOS_MULTIPOINT:
2293 g = gf->createMultiPoint(vgeoms);
2294 break;
2295 case GEOS_MULTILINESTRING:
2296 g = gf->createMultiLineString(vgeoms);
2297 break;
2298 case GEOS_MULTIPOLYGON:
2299 g = gf->createMultiPolygon(vgeoms);
2300 break;
2301 default:
2302 handle->ERROR_MESSAGE("Unsupported type request for PostGIS2GEOS_collection");
2303 g = 0;
2307 return g;
2309 catch (const std::exception &e)
2311 handle->ERROR_MESSAGE("%s", e.what());
2313 catch (...)
2315 handle->ERROR_MESSAGE("Unknown exception thrown");
2318 return 0;
2321 Geometry *
2322 GEOSPolygonize_r(GEOSContextHandle_t extHandle, const Geometry * const * g, unsigned int ngeoms)
2324 if ( 0 == extHandle )
2326 return 0;
2329 GEOSContextHandleInternal_t *handle = 0;
2330 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
2331 if ( 0 == handle->initialized )
2333 return 0;
2336 Geometry *out = 0;
2340 // Polygonize
2341 using geos::operation::polygonize::Polygonizer;
2342 Polygonizer plgnzr;
2343 for (std::size_t i = 0; i < ngeoms; ++i)
2345 plgnzr.add(g[i]);
2348 #if GEOS_DEBUG
2349 handle->NOTICE_MESSAGE("geometry vector added to polygonizer");
2350 #endif
2352 std::vector<Polygon*> *polys = plgnzr.getPolygons();
2353 assert(0 != polys);
2355 #if GEOS_DEBUG
2356 handle->NOTICE_MESSAGE("output polygons got");
2357 #endif
2359 // We need a vector of Geometry pointers, not Polygon pointers.
2360 // STL vector doesn't allow transparent upcast of this
2361 // nature, so we explicitly convert.
2362 // (it's just a waste of processor and memory, btw)
2364 // XXX mloskot: Why not to extent GeometryFactory to accept
2365 // vector of polygons or extend Polygonizer to return list of Geometry*
2366 // or add a wrapper which semantic is similar to:
2367 // std::vector<as_polygon<Geometry*> >
2368 std::vector<Geometry*> *polyvec = new std::vector<Geometry *>(polys->size());
2370 for (std::size_t i = 0; i < polys->size(); ++i)
2372 (*polyvec)[i] = (*polys)[i];
2374 delete polys;
2375 polys = 0;
2377 const GeometryFactory *gf = handle->geomFactory;
2379 // The below takes ownership of the passed vector,
2380 // so we must *not* delete it
2381 out = gf->createGeometryCollection(polyvec);
2383 catch (const std::exception &e)
2385 handle->ERROR_MESSAGE("%s", e.what());
2387 catch (...)
2389 handle->ERROR_MESSAGE("Unknown exception thrown");
2392 return out;
2395 Geometry *
2396 GEOSPolygonizer_getCutEdges_r(GEOSContextHandle_t extHandle, const Geometry * const * g, unsigned int ngeoms)
2398 if ( 0 == extHandle )
2400 return 0;
2403 GEOSContextHandleInternal_t *handle = 0;
2404 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
2405 if ( 0 == handle->initialized )
2407 return 0;
2410 Geometry *out = 0;
2414 // Polygonize
2415 using geos::operation::polygonize::Polygonizer;
2416 Polygonizer plgnzr;
2417 for (std::size_t i = 0; i < ngeoms; ++i)
2419 plgnzr.add(g[i]);
2422 #if GEOS_DEBUG
2423 handle->NOTICE_MESSAGE("geometry vector added to polygonizer");
2424 #endif
2426 const std::vector<const LineString *>& lines = plgnzr.getCutEdges();
2428 #if GEOS_DEBUG
2429 handle->NOTICE_MESSAGE("output polygons got");
2430 #endif
2432 // We need a vector of Geometry pointers, not Polygon pointers.
2433 // STL vector doesn't allow transparent upcast of this
2434 // nature, so we explicitly convert.
2435 // (it's just a waste of processor and memory, btw)
2436 // XXX mloskot: See comment for GEOSPolygonize_r
2437 std::vector<Geometry*> *linevec = new std::vector<Geometry *>(lines.size());
2439 for (std::size_t i = 0, n=lines.size(); i < n; ++i)
2441 (*linevec)[i] = lines[i]->clone();
2444 const GeometryFactory *gf = handle->geomFactory;
2446 // The below takes ownership of the passed vector,
2447 // so we must *not* delete it
2448 out = gf->createGeometryCollection(linevec);
2450 catch (const std::exception &e)
2452 handle->ERROR_MESSAGE("%s", e.what());
2454 catch (...)
2456 handle->ERROR_MESSAGE("Unknown exception thrown");
2459 return out;
2462 Geometry *
2463 GEOSPolygonize_full_r(GEOSContextHandle_t extHandle, const Geometry* g,
2464 Geometry** cuts, Geometry** dangles, Geometry** invalid)
2466 if ( 0 == extHandle )
2468 return 0;
2471 GEOSContextHandleInternal_t *handle = 0;
2472 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
2473 if ( 0 == handle->initialized )
2475 return 0;
2480 // Polygonize
2481 using geos::operation::polygonize::Polygonizer;
2482 Polygonizer plgnzr;
2483 for (std::size_t i = 0; i <g->getNumGeometries(); ++i)
2485 plgnzr.add(g->getGeometryN(i));
2488 #if GEOS_DEBUG
2489 handle->NOTICE_MESSAGE("geometry vector added to polygonizer");
2490 #endif
2491 const GeometryFactory *gf = handle->geomFactory;
2493 if ( cuts ) {
2495 const std::vector<const LineString *>& lines = plgnzr.getCutEdges();
2496 std::vector<Geometry*> *linevec = new std::vector<Geometry *>(lines.size());
2497 for (std::size_t i = 0, n=lines.size(); i < n; ++i)
2499 (*linevec)[i] = lines[i]->clone();
2502 // The below takes ownership of the passed vector,
2503 // so we must *not* delete it
2504 *cuts = gf->createGeometryCollection(linevec);
2507 if ( dangles ) {
2509 const std::vector<const LineString *>& lines = plgnzr.getDangles();
2510 std::vector<Geometry*> *linevec = new std::vector<Geometry *>(lines.size());
2511 for (std::size_t i = 0, n=lines.size(); i < n; ++i)
2513 (*linevec)[i] = lines[i]->clone();
2516 // The below takes ownership of the passed vector,
2517 // so we must *not* delete it
2518 *dangles = gf->createGeometryCollection(linevec);
2521 if ( invalid ) {
2523 const std::vector<LineString *>& lines = plgnzr.getInvalidRingLines();
2524 std::vector<Geometry*> *linevec = new std::vector<Geometry *>(lines.size());
2525 for (std::size_t i = 0, n=lines.size(); i < n; ++i)
2527 (*linevec)[i] = lines[i]->clone();
2530 // The below takes ownership of the passed vector,
2531 // so we must *not* delete it
2532 *invalid = gf->createGeometryCollection(linevec);
2535 std::vector<Polygon*> *polys = plgnzr.getPolygons();
2536 std::vector<Geometry*> *polyvec = new std::vector<Geometry *>(polys->size());
2537 for (std::size_t i = 0; i < polys->size(); ++i)
2539 (*polyvec)[i] = (*polys)[i];
2541 delete polys;
2543 return gf->createGeometryCollection(polyvec);
2546 catch (const std::exception &e)
2548 handle->ERROR_MESSAGE("%s", e.what());
2549 return 0;
2551 catch (...)
2553 handle->ERROR_MESSAGE("Unknown exception thrown");
2554 return 0;
2558 Geometry *
2559 GEOSLineMerge_r(GEOSContextHandle_t extHandle, const Geometry *g)
2561 if ( 0 == extHandle )
2563 return 0;
2566 GEOSContextHandleInternal_t *handle = 0;
2567 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
2568 if ( 0 == handle->initialized )
2570 return 0;
2573 Geometry *out = 0;
2577 using geos::operation::linemerge::LineMerger;
2578 LineMerger lmrgr;
2579 lmrgr.add(g);
2581 std::vector<LineString *>* lines = lmrgr.getMergedLineStrings();
2582 assert(0 != lines);
2584 #if GEOS_DEBUG
2585 handle->NOTICE_MESSAGE("output lines got");
2586 #endif
2588 std::vector<Geometry *>*geoms = new std::vector<Geometry *>(lines->size());
2589 for (std::vector<Geometry *>::size_type i = 0; i < lines->size(); ++i)
2591 (*geoms)[i] = (*lines)[i];
2593 delete lines;
2594 lines = 0;
2596 const GeometryFactory *gf = handle->geomFactory;
2597 out = gf->buildGeometry(geoms);
2599 // XXX: old version
2600 //out = gf->createGeometryCollection(geoms);
2602 catch (const std::exception &e)
2604 handle->ERROR_MESSAGE("%s", e.what());
2606 catch (...)
2608 handle->ERROR_MESSAGE("Unknown exception thrown");
2611 return out;
2615 GEOSGetSRID_r(GEOSContextHandle_t extHandle, const Geometry *g)
2617 assert(0 != g);
2619 if ( 0 == extHandle )
2621 return 0;
2624 GEOSContextHandleInternal_t *handle = 0;
2625 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
2626 if ( 0 == handle->initialized )
2628 return 0;
2633 return g->getSRID();
2635 catch (const std::exception &e)
2637 handle->ERROR_MESSAGE("%s", e.what());
2639 catch (...)
2641 handle->ERROR_MESSAGE("Unknown exception thrown");
2644 return 0;
2647 const char* GEOSversion()
2649 return GEOS_CAPI_VERSION;
2652 const char* GEOSjtsport()
2654 return GEOS_JTS_PORT;
2657 char
2658 GEOSHasZ_r(GEOSContextHandle_t extHandle, const Geometry *g)
2660 assert(0 != g);
2662 if ( 0 == extHandle )
2664 return -1;
2667 GEOSContextHandleInternal_t *handle = 0;
2668 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
2669 if ( 0 == handle->initialized )
2671 return -1;
2674 if (g->isEmpty())
2676 return false;
2678 assert(0 != g->getCoordinate());
2680 double az = g->getCoordinate()->z;
2681 //handle->ERROR_MESSAGE("ZCoord: %g", az);
2683 return static_cast<char>(FINITE(az));
2687 GEOS_getWKBOutputDims_r(GEOSContextHandle_t extHandle)
2689 if ( 0 == extHandle )
2691 return -1;
2694 GEOSContextHandleInternal_t *handle = 0;
2695 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
2696 if ( 0 == handle->initialized )
2698 return -1;
2701 return handle->WKBOutputDims;
2705 GEOS_setWKBOutputDims_r(GEOSContextHandle_t extHandle, int newdims)
2707 if ( 0 == extHandle )
2709 return -1;
2712 GEOSContextHandleInternal_t *handle = 0;
2713 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
2714 if ( 0 == handle->initialized )
2716 return -1;
2719 if ( newdims < 2 || newdims > 3 )
2721 handle->ERROR_MESSAGE("WKB output dimensions out of range 2..3");
2724 const int olddims = handle->WKBOutputDims;
2725 handle->WKBOutputDims = newdims;
2727 return olddims;
2731 GEOS_getWKBByteOrder_r(GEOSContextHandle_t extHandle)
2733 if ( 0 == extHandle )
2735 return -1;
2738 GEOSContextHandleInternal_t *handle = 0;
2739 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
2740 if ( 0 == handle->initialized )
2742 return -1;
2745 return handle->WKBByteOrder;
2749 GEOS_setWKBByteOrder_r(GEOSContextHandle_t extHandle, int byteOrder)
2751 if ( 0 == extHandle )
2753 return -1;
2756 GEOSContextHandleInternal_t *handle = 0;
2757 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
2758 if ( 0 == handle->initialized )
2760 return -1;
2763 const int oldByteOrder = handle->WKBByteOrder;
2764 handle->WKBByteOrder = byteOrder;
2766 return oldByteOrder;
2770 CoordinateSequence *
2771 GEOSCoordSeq_create_r(GEOSContextHandle_t extHandle, unsigned int size, unsigned int dims)
2773 if ( 0 == extHandle )
2775 return NULL;
2778 GEOSContextHandleInternal_t *handle = 0;
2779 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
2780 if ( 0 == handle->initialized )
2782 return NULL;
2787 const GeometryFactory *gf = handle->geomFactory;
2788 return gf->getCoordinateSequenceFactory()->create(size, dims);
2790 catch (const std::exception &e)
2792 handle->ERROR_MESSAGE("%s", e.what());
2794 catch (...)
2796 handle->ERROR_MESSAGE("Unknown exception thrown");
2799 return NULL;
2803 GEOSCoordSeq_setOrdinate_r(GEOSContextHandle_t extHandle, CoordinateSequence *cs,
2804 unsigned int idx, unsigned int dim, double val)
2806 assert(0 != cs);
2807 if ( 0 == extHandle )
2809 return 0;
2812 GEOSContextHandleInternal_t *handle = 0;
2813 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
2814 if ( 0 == handle->initialized )
2816 return 0;
2821 cs->setOrdinate(static_cast<int>(idx), static_cast<int>(dim), val);
2822 return 1;
2824 catch (const std::exception &e)
2826 handle->ERROR_MESSAGE("%s", e.what());
2828 catch (...)
2830 handle->ERROR_MESSAGE("Unknown exception thrown");
2833 return 0;
2837 GEOSCoordSeq_setX_r(GEOSContextHandle_t extHandle, CoordinateSequence *s, unsigned int idx, double val)
2839 return GEOSCoordSeq_setOrdinate_r(extHandle, s, idx, 0, val);
2843 GEOSCoordSeq_setY_r(GEOSContextHandle_t extHandle, CoordinateSequence *s, unsigned int idx, double val)
2845 return GEOSCoordSeq_setOrdinate_r(extHandle, s, idx, 1, val);
2849 GEOSCoordSeq_setZ_r(GEOSContextHandle_t extHandle, CoordinateSequence *s, unsigned int idx, double val)
2851 return GEOSCoordSeq_setOrdinate_r(extHandle, s, idx, 2, val);
2854 CoordinateSequence *
2855 GEOSCoordSeq_clone_r(GEOSContextHandle_t extHandle, const CoordinateSequence *cs)
2857 assert(0 != cs);
2859 if ( 0 == extHandle )
2861 return NULL;
2864 GEOSContextHandleInternal_t *handle = 0;
2865 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
2866 if ( 0 == handle->initialized )
2868 return NULL;
2873 return cs->clone();
2875 catch (const std::exception &e)
2877 handle->ERROR_MESSAGE("%s", e.what());
2879 catch (...)
2881 handle->ERROR_MESSAGE("Unknown exception thrown");
2884 return NULL;
2888 GEOSCoordSeq_getOrdinate_r(GEOSContextHandle_t extHandle, const CoordinateSequence *cs,
2889 unsigned int idx, unsigned int dim, double *val)
2891 assert(0 != cs);
2892 assert(0 != val);
2894 if ( 0 == extHandle )
2896 return 0;
2899 GEOSContextHandleInternal_t *handle = 0;
2900 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
2901 if ( 0 == handle->initialized )
2903 return 0;
2908 double d = cs->getOrdinate(static_cast<int>(idx), static_cast<int>(dim));
2909 *val = d;
2911 return 1;
2913 catch (const std::exception &e)
2915 handle->ERROR_MESSAGE("%s", e.what());
2917 catch (...)
2919 handle->ERROR_MESSAGE("Unknown exception thrown");
2922 return 0;
2926 GEOSCoordSeq_getX_r(GEOSContextHandle_t extHandle, const CoordinateSequence *s, unsigned int idx, double *val)
2928 return GEOSCoordSeq_getOrdinate_r(extHandle, s, idx, 0, val);
2932 GEOSCoordSeq_getY_r(GEOSContextHandle_t extHandle, const CoordinateSequence *s, unsigned int idx, double *val)
2934 return GEOSCoordSeq_getOrdinate_r(extHandle, s, idx, 1, val);
2938 GEOSCoordSeq_getZ_r(GEOSContextHandle_t extHandle, const CoordinateSequence *s, unsigned int idx, double *val)
2940 return GEOSCoordSeq_getOrdinate_r(extHandle, s, idx, 2, val);
2944 GEOSCoordSeq_getSize_r(GEOSContextHandle_t extHandle, const CoordinateSequence *cs, unsigned int *size)
2946 assert(0 != cs);
2947 assert(0 != size);
2949 if ( 0 == extHandle )
2951 return 0;
2954 GEOSContextHandleInternal_t *handle = 0;
2955 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
2956 if ( 0 == handle->initialized )
2958 return 0;
2963 const std::size_t sz = cs->getSize();
2964 *size = static_cast<unsigned int>(sz);
2965 return 1;
2967 catch (const std::exception &e)
2969 handle->ERROR_MESSAGE("%s", e.what());
2971 catch (...)
2973 handle->ERROR_MESSAGE("Unknown exception thrown");
2976 return 0;
2980 GEOSCoordSeq_getDimensions_r(GEOSContextHandle_t extHandle, const CoordinateSequence *cs, unsigned int *dims)
2982 assert(0 != cs);
2983 assert(0 != dims);
2985 if ( 0 == extHandle )
2987 return 0;
2990 GEOSContextHandleInternal_t *handle = 0;
2991 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
2992 if ( 0 == handle->initialized )
2994 return 0;
2999 const std::size_t dim = cs->getDimension();
3000 *dims = static_cast<unsigned int>(dim);
3002 return 1;
3004 catch (const std::exception &e)
3006 handle->ERROR_MESSAGE("%s", e.what());
3009 catch (...)
3011 handle->ERROR_MESSAGE("Unknown exception thrown");
3014 return 0;
3017 void
3018 GEOSCoordSeq_destroy_r(GEOSContextHandle_t extHandle, CoordinateSequence *s)
3020 GEOSContextHandleInternal_t *handle = 0;
3024 delete s;
3026 catch (const std::exception &e)
3028 if ( 0 == extHandle )
3030 return;
3033 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
3034 if ( 0 == handle->initialized )
3036 return;
3039 handle->ERROR_MESSAGE("%s", e.what());
3041 catch (...)
3043 if ( 0 == extHandle )
3045 return;
3048 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
3049 if ( 0 == handle->initialized )
3051 return;
3054 handle->ERROR_MESSAGE("Unknown exception thrown");
3058 const CoordinateSequence *
3059 GEOSGeom_getCoordSeq_r(GEOSContextHandle_t extHandle, const Geometry *g)
3061 if ( 0 == extHandle )
3063 return 0;
3066 GEOSContextHandleInternal_t *handle = 0;
3067 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
3068 if ( 0 == handle->initialized )
3070 return 0;
3075 using geos::geom::Point;
3077 const LineString *ls = dynamic_cast<const LineString *>(g);
3078 if ( ls )
3080 return ls->getCoordinatesRO();
3083 const Point *p = dynamic_cast<const Point *>(g);
3084 if ( p )
3086 return p->getCoordinatesRO();
3089 handle->ERROR_MESSAGE("Geometry must be a Point or LineString");
3090 return 0;
3092 catch (const std::exception &e)
3094 handle->ERROR_MESSAGE("%s", e.what());
3096 catch (...)
3098 handle->ERROR_MESSAGE("Unknown exception thrown");
3101 return 0;
3104 Geometry *
3105 GEOSGeom_createEmptyPoint_r(GEOSContextHandle_t extHandle)
3107 if ( 0 == extHandle )
3109 return NULL;
3112 GEOSContextHandleInternal_t *handle = 0;
3113 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
3114 if ( 0 == handle->initialized )
3116 return NULL;
3121 const GeometryFactory *gf = handle->geomFactory;
3122 return gf->createPoint();
3124 catch (const std::exception &e)
3126 handle->ERROR_MESSAGE("%s", e.what());
3128 catch (...)
3130 handle->ERROR_MESSAGE("Unknown exception thrown");
3133 return NULL;
3136 Geometry *
3137 GEOSGeom_createPoint_r(GEOSContextHandle_t extHandle, CoordinateSequence *cs)
3139 if ( 0 == extHandle )
3141 return 0;
3144 GEOSContextHandleInternal_t *handle = 0;
3145 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
3146 if ( 0 == handle->initialized )
3148 return 0;
3153 const GeometryFactory *gf = handle->geomFactory;
3154 return gf->createPoint(cs);
3156 catch (const std::exception &e)
3158 handle->ERROR_MESSAGE("%s", e.what());
3160 catch (...)
3162 handle->ERROR_MESSAGE("Unknown exception thrown");
3165 return 0;
3168 Geometry *
3169 GEOSGeom_createLinearRing_r(GEOSContextHandle_t extHandle, CoordinateSequence *cs)
3171 if ( 0 == extHandle )
3173 return NULL;
3176 GEOSContextHandleInternal_t *handle = 0;
3177 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
3178 if ( 0 == handle->initialized )
3180 return NULL;
3185 const GeometryFactory *gf = handle->geomFactory;
3187 return gf->createLinearRing(cs);
3189 catch (const std::exception &e)
3191 handle->ERROR_MESSAGE("%s", e.what());
3193 catch (...)
3195 handle->ERROR_MESSAGE("Unknown exception thrown");
3198 return NULL;
3201 Geometry *
3202 GEOSGeom_createEmptyLineString_r(GEOSContextHandle_t extHandle)
3204 if ( 0 == extHandle )
3206 return NULL;
3209 GEOSContextHandleInternal_t *handle = 0;
3210 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
3211 if ( 0 == handle->initialized )
3213 return NULL;
3218 const GeometryFactory *gf = handle->geomFactory;
3220 return gf->createLineString();
3222 catch (const std::exception &e)
3224 handle->ERROR_MESSAGE("%s", e.what());
3226 catch (...)
3228 handle->ERROR_MESSAGE("Unknown exception thrown");
3231 return NULL;
3234 Geometry *
3235 GEOSGeom_createLineString_r(GEOSContextHandle_t extHandle, CoordinateSequence *cs)
3237 if ( 0 == extHandle )
3239 return NULL;
3242 GEOSContextHandleInternal_t *handle = 0;
3243 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
3244 if ( 0 == handle->initialized )
3246 return NULL;
3251 const GeometryFactory *gf = handle->geomFactory;
3253 return gf->createLineString(cs);
3255 catch (const std::exception &e)
3257 handle->ERROR_MESSAGE("%s", e.what());
3259 catch (...)
3261 handle->ERROR_MESSAGE("Unknown exception thrown");
3264 return NULL;
3267 Geometry *
3268 GEOSGeom_createEmptyPolygon_r(GEOSContextHandle_t extHandle)
3270 if ( 0 == extHandle )
3272 return NULL;
3275 GEOSContextHandleInternal_t *handle = 0;
3276 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
3277 if ( 0 == handle->initialized )
3279 return NULL;
3284 const GeometryFactory *gf = handle->geomFactory;
3285 return gf->createPolygon();
3287 catch (const std::exception &e)
3289 handle->ERROR_MESSAGE("%s", e.what());
3291 catch (...)
3293 handle->ERROR_MESSAGE("Unknown exception thrown");
3296 return NULL;
3299 Geometry *
3300 GEOSGeom_createPolygon_r(GEOSContextHandle_t extHandle, Geometry *shell, Geometry **holes, unsigned int nholes)
3302 // FIXME: holes must be non-nullptr or may be nullptr?
3303 //assert(0 != holes);
3305 if ( 0 == extHandle )
3307 return NULL;
3310 GEOSContextHandleInternal_t *handle = 0;
3311 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
3312 if ( 0 == handle->initialized )
3314 return NULL;
3319 using geos::geom::LinearRing;
3321 std::vector<Geometry *> *vholes = new std::vector<Geometry *>(holes, holes + nholes);
3323 LinearRing *nshell = dynamic_cast<LinearRing *>(shell);
3324 if ( ! nshell )
3326 handle->ERROR_MESSAGE("Shell is not a LinearRing");
3327 return NULL;
3329 const GeometryFactory *gf = handle->geomFactory;
3331 return gf->createPolygon(nshell, vholes);
3333 catch (const std::exception &e)
3335 handle->ERROR_MESSAGE("%s", e.what());
3337 catch (...)
3339 handle->ERROR_MESSAGE("Unknown exception thrown");
3342 return NULL;
3345 Geometry *
3346 GEOSGeom_clone_r(GEOSContextHandle_t extHandle, const Geometry *g)
3348 assert(0 != g);
3350 if ( 0 == extHandle )
3352 return NULL;
3355 GEOSContextHandleInternal_t *handle = 0;
3356 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
3357 if ( 0 == handle->initialized )
3359 return NULL;
3364 return g->clone();
3366 catch (const std::exception &e)
3368 handle->ERROR_MESSAGE("%s", e.what());
3370 catch (...)
3372 handle->ERROR_MESSAGE("Unknown exception thrown");
3375 return NULL;
3379 GEOSGeom_getDimensions_r(GEOSContextHandle_t extHandle, const Geometry *g)
3381 if ( 0 == extHandle )
3383 return 0;
3386 GEOSContextHandleInternal_t *handle = 0;
3387 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
3388 if ( 0 == handle->initialized )
3390 return 0;
3395 using geos::geom::Point;
3396 using geos::geom::GeometryCollection;
3398 if ( g->isEmpty() )
3400 return 0;
3403 std::size_t dim = 0;
3404 const LineString *ls = dynamic_cast<const LineString *>(g);
3405 if ( ls )
3407 dim = ls->getCoordinatesRO()->getDimension();
3408 return static_cast<int>(dim);
3411 const Point *p = dynamic_cast<const Point *>(g);
3412 if ( p )
3414 dim = p->getCoordinatesRO()->getDimension();
3415 return static_cast<int>(dim);
3418 const Polygon *poly = dynamic_cast<const Polygon *>(g);
3419 if ( poly )
3421 return GEOSGeom_getDimensions_r(extHandle, poly->getExteriorRing());
3424 const GeometryCollection *coll = dynamic_cast<const GeometryCollection *>(g);
3425 if ( coll )
3427 return GEOSGeom_getDimensions_r(extHandle, coll->getGeometryN(0));
3430 handle->ERROR_MESSAGE("Unknown geometry type");
3431 return 0;
3433 catch (const std::exception &e)
3435 handle->ERROR_MESSAGE("%s", e.what());
3437 catch (...)
3439 handle->ERROR_MESSAGE("Unknown exception thrown");
3442 return 0;
3445 Geometry *
3446 GEOSSimplify_r(GEOSContextHandle_t extHandle, const Geometry *g1, double tolerance)
3448 if ( 0 == extHandle )
3450 return NULL;
3453 GEOSContextHandleInternal_t *handle = 0;
3454 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
3455 if ( 0 == handle->initialized )
3457 return NULL;
3462 using namespace geos::simplify;
3463 Geometry::AutoPtr g(DouglasPeuckerSimplifier::simplify(g1, tolerance));
3464 return g.release();
3466 catch (const std::exception &e)
3468 handle->ERROR_MESSAGE("%s", e.what());
3470 catch (...)
3472 handle->ERROR_MESSAGE("Unknown exception thrown");
3475 return NULL;
3478 Geometry *
3479 GEOSTopologyPreserveSimplify_r(GEOSContextHandle_t extHandle, const Geometry *g1, double tolerance)
3481 if ( 0 == extHandle )
3483 return NULL;
3486 GEOSContextHandleInternal_t *handle = 0;
3487 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
3488 if ( 0 == handle->initialized )
3490 return NULL;
3495 using namespace geos::simplify;
3496 Geometry::AutoPtr g(TopologyPreservingSimplifier::simplify(g1, tolerance));
3497 return g.release();
3499 catch (const std::exception &e)
3501 handle->ERROR_MESSAGE("%s", e.what());
3503 catch (...)
3505 handle->ERROR_MESSAGE("Unknown exception thrown");
3508 return NULL;
3512 /* WKT Reader */
3513 WKTReader *
3514 GEOSWKTReader_create_r(GEOSContextHandle_t extHandle)
3516 if ( 0 == extHandle )
3518 return NULL;
3521 GEOSContextHandleInternal_t *handle = 0;
3522 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
3523 if ( 0 == handle->initialized )
3525 return NULL;
3530 using geos::io::WKTReader;
3531 return new WKTReader((GeometryFactory*)handle->geomFactory);
3533 catch (const std::exception &e)
3535 handle->ERROR_MESSAGE("%s", e.what());
3537 catch (...)
3539 handle->ERROR_MESSAGE("Unknown exception thrown");
3542 return NULL;
3545 void
3546 GEOSWKTReader_destroy_r(GEOSContextHandle_t extHandle, WKTReader *reader)
3548 GEOSContextHandleInternal_t *handle = 0;
3552 delete reader;
3554 catch (const std::exception &e)
3556 if ( 0 == extHandle )
3558 return;
3561 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
3562 if ( 0 == handle->initialized )
3564 return;
3567 handle->ERROR_MESSAGE("%s", e.what());
3569 catch (...)
3571 if ( 0 == extHandle )
3573 return;
3576 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
3577 if ( 0 == handle->initialized )
3579 return;
3582 handle->ERROR_MESSAGE("Unknown exception thrown");
3587 Geometry*
3588 GEOSWKTReader_read_r(GEOSContextHandle_t extHandle, WKTReader *reader, const char *wkt)
3590 assert(0 != reader);
3592 if ( 0 == extHandle )
3594 return 0;
3597 GEOSContextHandleInternal_t *handle = 0;
3598 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
3599 if ( 0 == handle->initialized )
3601 return 0;
3606 const std::string wktstring(wkt);
3607 Geometry *g = reader->read(wktstring);
3608 return g;
3610 catch (const std::exception &e)
3612 handle->ERROR_MESSAGE("%s", e.what());
3614 catch (...)
3616 handle->ERROR_MESSAGE("Unknown exception thrown");
3619 return 0;
3622 /* WKT Writer */
3623 WKTWriter *
3624 GEOSWKTWriter_create_r(GEOSContextHandle_t extHandle)
3626 if ( 0 == extHandle )
3628 return 0;
3631 GEOSContextHandleInternal_t *handle = 0;
3632 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
3633 if ( 0 == handle->initialized )
3635 return 0;
3640 using geos::io::WKTWriter;
3641 return new WKTWriter();
3643 catch (const std::exception &e)
3645 handle->ERROR_MESSAGE("%s", e.what());
3647 catch (...)
3649 handle->ERROR_MESSAGE("Unknown exception thrown");
3652 return 0;
3655 void
3656 GEOSWKTWriter_destroy_r(GEOSContextHandle_t extHandle, WKTWriter *Writer)
3659 GEOSContextHandleInternal_t *handle = 0;
3663 delete Writer;
3665 catch (const std::exception &e)
3667 if ( 0 == extHandle )
3669 return;
3672 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
3673 if ( 0 == handle->initialized )
3675 return;
3678 handle->ERROR_MESSAGE("%s", e.what());
3680 catch (...)
3682 if ( 0 == extHandle )
3684 return;
3687 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
3688 if ( 0 == handle->initialized )
3690 return;
3693 handle->ERROR_MESSAGE("Unknown exception thrown");
3698 char*
3699 GEOSWKTWriter_write_r(GEOSContextHandle_t extHandle, WKTWriter *writer, const Geometry *geom)
3701 assert(0 != writer);
3703 if ( 0 == extHandle )
3705 return NULL;
3708 GEOSContextHandleInternal_t *handle = 0;
3709 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
3710 if ( 0 == handle->initialized )
3712 return NULL;
3717 std::string sgeom(writer->write(geom));
3718 char *result = gstrdup(sgeom);
3719 return result;
3721 catch (const std::exception &e)
3723 handle->ERROR_MESSAGE("%s", e.what());
3725 catch (...)
3727 handle->ERROR_MESSAGE("Unknown exception thrown");
3730 return NULL;
3733 /* WKB Reader */
3734 WKBReader *
3735 GEOSWKBReader_create_r(GEOSContextHandle_t extHandle)
3737 if ( 0 == extHandle )
3739 return NULL;
3742 GEOSContextHandleInternal_t *handle = 0;
3743 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
3744 if ( 0 == handle->initialized )
3746 return NULL;
3749 using geos::io::WKBReader;
3752 return new WKBReader(*(GeometryFactory*)handle->geomFactory);
3754 catch (const std::exception &e)
3756 handle->ERROR_MESSAGE("%s", e.what());
3758 catch (...)
3760 handle->ERROR_MESSAGE("Unknown exception thrown");
3763 return NULL;
3766 void
3767 GEOSWKBReader_destroy_r(GEOSContextHandle_t extHandle, WKBReader *reader)
3769 GEOSContextHandleInternal_t *handle = 0;
3773 delete reader;
3775 catch (const std::exception &e)
3777 if ( 0 == extHandle )
3779 return;
3782 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
3783 if ( 0 == handle->initialized )
3785 return;
3788 handle->ERROR_MESSAGE("%s", e.what());
3790 catch (...)
3792 if ( 0 == extHandle )
3794 return;
3797 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
3798 if ( 0 == handle->initialized )
3800 return;
3803 handle->ERROR_MESSAGE("Unknown exception thrown");
3808 Geometry*
3809 GEOSWKBReader_read_r(GEOSContextHandle_t extHandle, WKBReader *reader, const unsigned char *wkb, size_t size)
3811 assert(0 != reader);
3812 assert(0 != wkb);
3814 if ( 0 == extHandle )
3816 return 0;
3819 GEOSContextHandleInternal_t *handle = 0;
3820 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
3821 if ( 0 == handle->initialized )
3823 return 0;
3828 std::string wkbstring(reinterpret_cast<const char*>(wkb), size); // make it binary !
3829 std::istringstream is(std::ios_base::binary);
3830 is.str(wkbstring);
3831 is.seekg(0, std::ios::beg); // rewind reader pointer
3833 Geometry *g = reader->read(is);
3834 return g;
3836 catch (const std::exception &e)
3838 handle->ERROR_MESSAGE("%s", e.what());
3840 catch (...)
3842 handle->ERROR_MESSAGE("Unknown exception thrown");
3845 return 0;
3848 Geometry*
3849 GEOSWKBReader_readHEX_r(GEOSContextHandle_t extHandle, WKBReader *reader, const unsigned char *hex, size_t size)
3851 assert(0 != reader);
3852 assert(0 != hex);
3854 if ( 0 == extHandle )
3856 return 0;
3859 GEOSContextHandleInternal_t *handle = 0;
3860 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
3861 if ( 0 == handle->initialized )
3863 return 0;
3868 std::string hexstring(reinterpret_cast<const char*>(hex), size);
3869 std::istringstream is(std::ios_base::binary);
3870 is.str(hexstring);
3871 is.seekg(0, std::ios::beg); // rewind reader pointer
3873 Geometry *g = reader->readHEX(is);
3874 return g;
3876 catch (const std::exception &e)
3878 handle->ERROR_MESSAGE("%s", e.what());
3880 catch (...)
3882 handle->ERROR_MESSAGE("Unknown exception thrown");
3885 return 0;
3888 /* WKB Writer */
3889 WKBWriter *
3890 GEOSWKBWriter_create_r(GEOSContextHandle_t extHandle)
3892 if ( 0 == extHandle )
3894 return NULL;
3897 GEOSContextHandleInternal_t *handle = 0;
3898 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
3899 if ( 0 == handle->initialized )
3901 return NULL;
3906 using geos::io::WKBWriter;
3907 return new WKBWriter();
3909 catch (const std::exception &e)
3911 handle->ERROR_MESSAGE("%s", e.what());
3913 catch (...)
3915 handle->ERROR_MESSAGE("Unknown exception thrown");
3918 return NULL;
3921 void
3922 GEOSWKBWriter_destroy_r(GEOSContextHandle_t extHandle, WKBWriter *Writer)
3924 GEOSContextHandleInternal_t *handle = 0;
3928 delete Writer;
3930 catch (const std::exception &e)
3932 if ( 0 == extHandle )
3934 return;
3937 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
3938 if ( 0 == handle->initialized )
3940 return;
3943 handle->ERROR_MESSAGE("%s", e.what());
3945 catch (...)
3947 if ( 0 == extHandle )
3949 return;
3952 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
3953 if ( 0 == handle->initialized )
3955 return;
3958 handle->ERROR_MESSAGE("Unknown exception thrown");
3963 /* The caller owns the result */
3964 unsigned char*
3965 GEOSWKBWriter_write_r(GEOSContextHandle_t extHandle, WKBWriter *writer, const Geometry *geom, size_t *size)
3967 assert(0 != writer);
3968 assert(0 != geom);
3969 assert(0 != size);
3971 if ( 0 == extHandle )
3973 return NULL;
3976 GEOSContextHandleInternal_t *handle = 0;
3977 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
3978 if ( 0 == handle->initialized )
3980 return NULL;
3985 std::ostringstream os(std::ios_base::binary);
3986 writer->write(*geom, os);
3987 std::string wkbstring(os.str());
3988 const std::size_t len = wkbstring.length();
3990 unsigned char *result = NULL;
3991 result = (unsigned char*) std::malloc(len);
3992 std::memcpy(result, wkbstring.c_str(), len);
3993 *size = len;
3994 return result;
3996 catch (const std::exception &e)
3998 handle->ERROR_MESSAGE("%s", e.what());
4000 catch (...)
4002 handle->ERROR_MESSAGE("Unknown exception thrown");
4004 return NULL;
4007 /* The caller owns the result */
4008 unsigned char*
4009 GEOSWKBWriter_writeHEX_r(GEOSContextHandle_t extHandle, WKBWriter *writer, const Geometry *geom, size_t *size)
4011 assert(0 != writer);
4012 assert(0 != geom);
4013 assert(0 != size);
4015 if ( 0 == extHandle )
4017 return NULL;
4020 GEOSContextHandleInternal_t *handle = 0;
4021 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
4022 if ( 0 == handle->initialized )
4024 return NULL;
4029 std::ostringstream os(std::ios_base::binary);
4030 writer->writeHEX(*geom, os);
4031 std::string wkbstring(os.str());
4032 const std::size_t len = wkbstring.length();
4034 unsigned char *result = NULL;
4035 result = (unsigned char*) std::malloc(len);
4036 std::memcpy(result, wkbstring.c_str(), len);
4037 *size = len;
4038 return result;
4040 catch (const std::exception &e)
4042 handle->ERROR_MESSAGE("%s", e.what());
4044 catch (...)
4046 handle->ERROR_MESSAGE("Unknown exception thrown");
4049 return NULL;
4053 GEOSWKBWriter_getOutputDimension_r(GEOSContextHandle_t extHandle, const GEOSWKBWriter* writer)
4055 assert(0 != writer);
4057 if ( 0 == extHandle )
4059 return 0;
4062 int ret = 0;
4064 GEOSContextHandleInternal_t *handle = 0;
4065 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
4066 if ( 0 != handle->initialized )
4070 ret = writer->getOutputDimension();
4072 catch (...)
4074 handle->ERROR_MESSAGE("Unknown exception thrown");
4078 return ret;
4081 void
4082 GEOSWKBWriter_setOutputDimension_r(GEOSContextHandle_t extHandle, GEOSWKBWriter* writer, int newDimension)
4084 assert(0 != writer);
4086 if ( 0 == extHandle )
4088 return;
4091 GEOSContextHandleInternal_t *handle = 0;
4092 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
4093 if ( 0 != handle->initialized )
4097 writer->setOutputDimension(newDimension);
4099 catch (...)
4101 handle->ERROR_MESSAGE("Unknown exception thrown");
4107 GEOSWKBWriter_getByteOrder_r(GEOSContextHandle_t extHandle, const GEOSWKBWriter* writer)
4109 assert(0 != writer);
4111 if ( 0 == extHandle )
4113 return 0;
4116 int ret = 0;
4118 GEOSContextHandleInternal_t *handle = 0;
4119 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
4120 if ( 0 != handle->initialized )
4124 ret = writer->getByteOrder();
4127 catch (...)
4129 handle->ERROR_MESSAGE("Unknown exception thrown");
4133 return ret;
4136 void
4137 GEOSWKBWriter_setByteOrder_r(GEOSContextHandle_t extHandle, GEOSWKBWriter* writer, int newByteOrder)
4139 assert(0 != writer);
4141 if ( 0 == extHandle )
4143 return;
4146 GEOSContextHandleInternal_t *handle = 0;
4147 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
4148 if ( 0 != handle->initialized )
4152 writer->setByteOrder(newByteOrder);
4154 catch (...)
4156 handle->ERROR_MESSAGE("Unknown exception thrown");
4161 char
4162 GEOSWKBWriter_getIncludeSRID_r(GEOSContextHandle_t extHandle, const GEOSWKBWriter* writer)
4164 assert(0 != writer);
4166 if ( 0 == extHandle )
4168 return -1;
4171 int ret = -1;
4173 GEOSContextHandleInternal_t *handle = 0;
4174 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
4175 if ( 0 != handle->initialized )
4179 int srid = writer->getIncludeSRID();
4180 ret = static_cast<char>(srid);
4182 catch (...)
4184 handle->ERROR_MESSAGE("Unknown exception thrown");
4188 return static_cast<char>(ret);
4191 void
4192 GEOSWKBWriter_setIncludeSRID_r(GEOSContextHandle_t extHandle, GEOSWKBWriter* writer, const char newIncludeSRID)
4194 assert(0 != writer);
4196 if ( 0 == extHandle )
4198 return;
4201 GEOSContextHandleInternal_t *handle = 0;
4202 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
4203 if ( 0 != handle->initialized )
4207 writer->setIncludeSRID(newIncludeSRID);
4209 catch (...)
4211 handle->ERROR_MESSAGE("Unknown exception thrown");
4217 //-----------------------------------------------------------------
4218 // Prepared Geometry
4219 //-----------------------------------------------------------------
4221 const geos::geom::prep::PreparedGeometry*
4222 GEOSPrepare_r(GEOSContextHandle_t extHandle, const Geometry *g)
4224 if ( 0 == extHandle )
4226 return 0;
4229 GEOSContextHandleInternal_t *handle = 0;
4230 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
4231 if ( 0 == handle->initialized )
4233 return 0;
4236 const geos::geom::prep::PreparedGeometry* prep = 0;
4240 prep = geos::geom::prep::PreparedGeometryFactory::prepare(g);
4242 catch (const std::exception &e)
4244 handle->ERROR_MESSAGE("%s", e.what());
4246 catch (...)
4248 handle->ERROR_MESSAGE("Unknown exception thrown");
4251 return prep;
4254 void
4255 GEOSPreparedGeom_destroy_r(GEOSContextHandle_t extHandle, const geos::geom::prep::PreparedGeometry *a)
4257 GEOSContextHandleInternal_t *handle = 0;
4261 delete a;
4263 catch (const std::exception &e)
4265 if ( 0 == extHandle )
4267 return;
4270 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
4271 if ( 0 == handle->initialized )
4273 return;
4276 handle->ERROR_MESSAGE("%s", e.what());
4278 catch (...)
4280 if ( 0 == extHandle )
4282 return;
4285 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
4286 if ( 0 == handle->initialized )
4288 return;
4291 handle->ERROR_MESSAGE("Unknown exception thrown");
4295 char
4296 GEOSPreparedContains_r(GEOSContextHandle_t extHandle,
4297 const geos::geom::prep::PreparedGeometry *pg, const Geometry *g)
4299 assert(0 != pg);
4300 assert(0 != g);
4302 if ( 0 == extHandle )
4304 return 2;
4307 GEOSContextHandleInternal_t *handle = 0;
4308 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
4309 if ( 0 == handle->initialized )
4311 return 2;
4314 try
4316 bool result = pg->contains(g);
4317 return result;
4319 catch (const std::exception &e)
4321 handle->ERROR_MESSAGE("%s", e.what());
4323 catch (...)
4325 handle->ERROR_MESSAGE("Unknown exception thrown");
4328 return 2;
4331 char
4332 GEOSPreparedContainsProperly_r(GEOSContextHandle_t extHandle,
4333 const geos::geom::prep::PreparedGeometry *pg, const Geometry *g)
4335 assert(0 != pg);
4336 assert(0 != g);
4338 if ( 0 == extHandle )
4340 return 2;
4343 GEOSContextHandleInternal_t *handle = 0;
4344 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
4345 if ( 0 == handle->initialized )
4347 return 2;
4350 try
4352 bool result = pg->containsProperly(g);
4353 return result;
4355 catch (const std::exception &e)
4357 handle->ERROR_MESSAGE("%s", e.what());
4359 catch (...)
4361 handle->ERROR_MESSAGE("Unknown exception thrown");
4364 return 2;
4367 char
4368 GEOSPreparedCovers_r(GEOSContextHandle_t extHandle,
4369 const geos::geom::prep::PreparedGeometry *pg, const Geometry *g)
4371 assert(0 != pg);
4372 assert(0 != g);
4374 if ( 0 == extHandle )
4376 return 2;
4379 GEOSContextHandleInternal_t *handle = 0;
4380 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
4381 if ( 0 == handle->initialized )
4383 return 2;
4386 try
4388 bool result = pg->covers(g);
4389 return result;
4391 catch (const std::exception &e)
4393 handle->ERROR_MESSAGE("%s", e.what());
4395 catch (...)
4397 handle->ERROR_MESSAGE("Unknown exception thrown");
4400 return 2;
4403 char
4404 GEOSPreparedIntersects_r(GEOSContextHandle_t extHandle,
4405 const geos::geom::prep::PreparedGeometry *pg, const Geometry *g)
4407 assert(0 != pg);
4408 assert(0 != g);
4410 if ( 0 == extHandle )
4412 return 2;
4415 GEOSContextHandleInternal_t *handle = 0;
4416 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
4417 if ( 0 == handle->initialized )
4419 return 2;
4422 try
4424 bool result = pg->intersects(g);
4425 return result;
4427 catch (const std::exception &e)
4429 handle->ERROR_MESSAGE("%s", e.what());
4431 catch (...)
4433 handle->ERROR_MESSAGE("Unknown exception thrown");
4436 return 2;
4439 //-----------------------------------------------------------------
4440 // STRtree
4441 //-----------------------------------------------------------------
4443 geos::index::strtree::STRtree *
4444 GEOSSTRtree_create_r(GEOSContextHandle_t extHandle,
4445 size_t nodeCapacity)
4447 if ( 0 == extHandle )
4449 return 0;
4452 GEOSContextHandleInternal_t *handle = 0;
4453 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
4454 if ( 0 == handle->initialized )
4456 return 0;
4459 geos::index::strtree::STRtree *tree = 0;
4463 tree = new geos::index::strtree::STRtree(nodeCapacity);
4465 catch (const std::exception &e)
4467 handle->ERROR_MESSAGE("%s", e.what());
4469 catch (...)
4471 handle->ERROR_MESSAGE("Unknown exception thrown");
4474 return tree;
4477 void
4478 GEOSSTRtree_insert_r(GEOSContextHandle_t extHandle,
4479 geos::index::strtree::STRtree *tree,
4480 const geos::geom::Geometry *g,
4481 void *item)
4483 GEOSContextHandleInternal_t *handle = 0;
4484 assert(tree != 0);
4485 assert(g != 0);
4489 tree->insert(g->getEnvelopeInternal(), item);
4491 catch (const std::exception &e)
4493 if ( 0 == extHandle )
4495 return;
4498 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
4499 if ( 0 == handle->initialized )
4501 return;
4504 handle->ERROR_MESSAGE("%s", e.what());
4506 catch (...)
4508 if ( 0 == extHandle )
4510 return;
4513 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
4514 if ( 0 == handle->initialized )
4516 return;
4519 handle->ERROR_MESSAGE("Unknown exception thrown");
4523 void
4524 GEOSSTRtree_query_r(GEOSContextHandle_t extHandle,
4525 geos::index::strtree::STRtree *tree,
4526 const geos::geom::Geometry *g,
4527 GEOSQueryCallback callback,
4528 void *userdata)
4530 GEOSContextHandleInternal_t *handle = 0;
4531 assert(tree != 0);
4532 assert(g != 0);
4533 assert(callback != 0);
4537 CAPI_ItemVisitor visitor(callback, userdata);
4538 tree->query(g->getEnvelopeInternal(), visitor);
4540 catch (const std::exception &e)
4542 if ( 0 == extHandle )
4544 return;
4547 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
4548 if ( 0 == handle->initialized )
4550 return;
4553 handle->ERROR_MESSAGE("%s", e.what());
4555 catch (...)
4557 if ( 0 == extHandle )
4559 return;
4562 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
4563 if ( 0 == handle->initialized )
4565 return;
4568 handle->ERROR_MESSAGE("Unknown exception thrown");
4572 void
4573 GEOSSTRtree_iterate_r(GEOSContextHandle_t extHandle,
4574 geos::index::strtree::STRtree *tree,
4575 GEOSQueryCallback callback,
4576 void *userdata)
4578 GEOSContextHandleInternal_t *handle = 0;
4579 assert(tree != 0);
4580 assert(callback != 0);
4584 CAPI_ItemVisitor visitor(callback, userdata);
4585 tree->iterate(visitor);
4587 catch (const std::exception &e)
4589 if ( 0 == extHandle )
4591 return;
4594 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
4595 if ( 0 == handle->initialized )
4597 return;
4600 handle->ERROR_MESSAGE("%s", e.what());
4602 catch (...)
4604 if ( 0 == extHandle )
4606 return;
4609 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
4610 if ( 0 == handle->initialized )
4612 return;
4615 handle->ERROR_MESSAGE("Unknown exception thrown");
4619 char
4620 GEOSSTRtree_remove_r(GEOSContextHandle_t extHandle,
4621 geos::index::strtree::STRtree *tree,
4622 const geos::geom::Geometry *g,
4623 void *item)
4625 assert(0 != tree);
4626 assert(0 != g);
4628 if ( 0 == extHandle )
4630 return 2;
4633 GEOSContextHandleInternal_t *handle = 0;
4634 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
4635 if ( 0 == handle->initialized )
4637 return 2;
4640 try
4642 bool result = tree->remove(g->getEnvelopeInternal(), item);
4643 return result;
4645 catch (const std::exception &e)
4647 handle->ERROR_MESSAGE("%s", e.what());
4649 catch (...)
4651 handle->ERROR_MESSAGE("Unknown exception thrown");
4654 return 2;
4657 void
4658 GEOSSTRtree_destroy_r(GEOSContextHandle_t extHandle,
4659 geos::index::strtree::STRtree *tree)
4661 GEOSContextHandleInternal_t *handle = 0;
4665 delete tree;
4667 catch (const std::exception &e)
4669 if ( 0 == extHandle )
4671 return;
4674 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
4675 if ( 0 == handle->initialized )
4677 return;
4680 handle->ERROR_MESSAGE("%s", e.what());
4682 catch (...)
4684 if ( 0 == extHandle )
4686 return;
4689 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
4690 if ( 0 == handle->initialized )
4692 return;
4695 handle->ERROR_MESSAGE("Unknown exception thrown");
4699 double
4700 GEOSProject_r(GEOSContextHandle_t extHandle,
4701 const Geometry *g,
4702 const Geometry *p)
4705 const geos::geom::Point* point = dynamic_cast<const geos::geom::Point*>(p);
4706 if (!point) {
4707 if ( 0 == extHandle )
4709 return -1.0;
4711 GEOSContextHandleInternal_t *handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
4712 if ( 0 == handle->initialized )
4714 return -1.0;
4717 handle->ERROR_MESSAGE("third argument of GEOSProject_r must be Point*");
4718 return -1.0;
4720 const geos::geom::Coordinate* inputPt = p->getCoordinate();
4721 return geos::linearref::LengthIndexedLine(g).project(*inputPt);
4725 Geometry*
4726 GEOSInterpolate_r(GEOSContextHandle_t extHandle, const Geometry *g, double d)
4728 geos::linearref::LengthIndexedLine lil(g);
4729 geos::geom::Coordinate coord = lil.extractPoint(d);
4730 GEOSContextHandleInternal_t *handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
4731 const GeometryFactory *gf = handle->geomFactory;
4732 Geometry* point = gf->createPoint(coord);
4733 return point;
4737 double
4738 GEOSProjectNormalized_r(GEOSContextHandle_t extHandle, const Geometry *g,
4739 const Geometry *p)
4742 double length;
4743 GEOSLength_r(extHandle, g, &length);
4744 return GEOSProject_r(extHandle, g, p) / length;
4748 Geometry*
4749 GEOSInterpolateNormalized_r(GEOSContextHandle_t extHandle, const Geometry *g,
4750 double d)
4752 double length;
4753 GEOSLength_r(extHandle, g, &length);
4754 return GEOSInterpolate_r(extHandle, g, d * length);
4757 GEOSGeometry*
4758 GEOSGeom_extractUniquePoints_r(GEOSContextHandle_t extHandle,
4759 const GEOSGeometry* g)
4761 if ( 0 == extHandle ) return 0;
4762 GEOSContextHandleInternal_t *handle = 0;
4763 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
4764 if ( handle->initialized == 0 ) return 0;
4766 using namespace geos::geom;
4767 using namespace geos::util;
4772 /* 1: extract points */
4773 std::vector<const Coordinate*> coords;
4774 UniqueCoordinateArrayFilter filter(coords);
4775 g->apply_ro(&filter);
4777 /* 2: for each point, create a geometry and put into a vector */
4778 std::vector<Geometry*>* points = new std::vector<Geometry*>();
4779 points->reserve(coords.size());
4780 const GeometryFactory* factory = g->getFactory();
4781 for (std::vector<const Coordinate*>::iterator it=coords.begin(),
4782 itE=coords.end();
4783 it != itE; ++it)
4785 Geometry* point = factory->createPoint(*(*it));
4786 points->push_back(point);
4789 /* 3: create a multipoint */
4790 return factory->createMultiPoint(points);
4793 catch (const std::exception &e)
4795 handle->ERROR_MESSAGE("%s", e.what());
4796 return 0;
4798 catch (...)
4800 handle->ERROR_MESSAGE("Unknown exception thrown");
4801 return 0;
4805 int GEOSOrientationIndex_r(GEOSContextHandle_t extHandle,
4806 double Ax, double Ay, double Bx, double By, double Px, double Py)
4808 GEOSContextHandleInternal_t *handle = 0;
4810 using geos::geom::Coordinate;
4811 using geos::algorithm::CGAlgorithms;
4813 if ( 0 == extHandle )
4815 return 2;
4818 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
4819 if ( 0 == handle->initialized )
4821 return 2;
4826 Coordinate A(Ax, Ay);
4827 Coordinate B(Bx, By);
4828 Coordinate P(Px, Py);
4829 return CGAlgorithms::orientationIndex(A, B, P);
4831 catch (const std::exception &e)
4833 handle->ERROR_MESSAGE("%s", e.what());
4834 return 2;
4836 catch (...)
4838 handle->ERROR_MESSAGE("Unknown exception thrown");
4839 return 2;
4844 } /* extern "C" */