1 /************************************************************************
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
76 #pragma warning(disable : 4099)
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
92 // Intentional, to allow non-standard C elements like C99 functions to be
93 // imported through C++ headers of C library, like <cmath>.
96 /// Define this if you want operations triggering Exceptions to
98 /// (will use the NOTIFY channel - only implemented for GEOSUnion so far)
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
;
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
138 class CAPI_ItemVisitor
: public geos::index::ItemVisitor
{
139 GEOSQueryCallback callback
;
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
);
155 char* gstrdup_s(const char* str
, const std::size_t size
)
157 char* out
= static_cast<char*>(std::malloc(size
+ 1));
160 // as no strlen call necessary, memcpy may be faster than strcpy
161 std::memcpy(out
, str
, size
+ 1);
168 char* gstrdup(std::string
const& str
)
170 return gstrdup_s(str
.c_str(), str
.size());
175 initGEOS_r(GEOSMessageHandler nf
, GEOSMessageHandler ef
)
177 GEOSContextHandleInternal_t
*handle
= 0;
180 extHandle
= std::malloc(sizeof(GEOSContextHandleInternal_t
));
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
);
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
)
206 f
= handle
->NOTICE_MESSAGE
;
207 handle
->NOTICE_MESSAGE
= nf
;
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
)
223 f
= handle
->ERROR_MESSAGE
;
224 handle
->ERROR_MESSAGE
= nf
;
230 finishGEOS_r(GEOSContextHandle_t extHandle
)
232 // Fix up freeing handle w.r.t. malloc above
233 std::free(extHandle
);
238 GEOSFree_r (GEOSContextHandle_t extHandle
, void* buffer
)
240 assert(0 != extHandle
);
245 //-----------------------------------------------------------
246 // relate()-related functions
247 // return 0 = false, 1 = true, 2 = error occured
248 //-----------------------------------------------------------
251 GEOSDisjoint_r(GEOSContextHandle_t extHandle
, const Geometry
*g1
, const Geometry
*g2
)
253 if ( 0 == extHandle
)
258 GEOSContextHandleInternal_t
*handle
= 0;
259 handle
= reinterpret_cast<GEOSContextHandleInternal_t
*>(extHandle
);
260 if ( handle
->initialized
== 0 )
267 bool result
= g1
->disjoint(g2
);
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());
280 handle
->ERROR_MESSAGE("Unknown exception thrown");
287 GEOSTouches_r(GEOSContextHandle_t extHandle
, const Geometry
*g1
, const Geometry
*g2
)
289 if ( 0 == extHandle
)
294 GEOSContextHandleInternal_t
*handle
= 0;
295 handle
= reinterpret_cast<GEOSContextHandleInternal_t
*>(extHandle
);
296 if ( 0 == handle
->initialized
)
303 bool result
= g1
->touches(g2
);
306 catch (const std::exception
&e
)
308 handle
->ERROR_MESSAGE("%s", e
.what());
312 handle
->ERROR_MESSAGE("Unknown exception thrown");
319 GEOSIntersects_r(GEOSContextHandle_t extHandle
, const Geometry
*g1
, const Geometry
*g2
)
321 if ( 0 == extHandle
)
326 GEOSContextHandleInternal_t
*handle
= 0;
327 handle
= reinterpret_cast<GEOSContextHandleInternal_t
*>(extHandle
);
328 if ( 0 == handle
->initialized
)
335 bool result
= g1
->intersects(g2
);
338 catch (const std::exception
&e
)
340 handle
->ERROR_MESSAGE("%s", e
.what());
344 handle
->ERROR_MESSAGE("Unknown exception thrown");
351 GEOSCrosses_r(GEOSContextHandle_t extHandle
, const Geometry
*g1
, const Geometry
*g2
)
353 if ( 0 == extHandle
)
358 GEOSContextHandleInternal_t
*handle
= 0;
359 handle
= reinterpret_cast<GEOSContextHandleInternal_t
*>(extHandle
);
360 if ( 0 == handle
->initialized
)
367 bool result
= g1
->crosses(g2
);
370 catch (const std::exception
&e
)
372 handle
->ERROR_MESSAGE("%s", e
.what());
376 handle
->ERROR_MESSAGE("Unknown exception thrown");
383 GEOSWithin_r(GEOSContextHandle_t extHandle
, const Geometry
*g1
, const Geometry
*g2
)
385 if ( 0 == extHandle
)
390 GEOSContextHandleInternal_t
*handle
= 0;
391 handle
= reinterpret_cast<GEOSContextHandleInternal_t
*>(extHandle
);
392 if ( 0 == handle
->initialized
)
399 bool result
= g1
->within(g2
);
402 catch (const std::exception
&e
)
404 handle
->ERROR_MESSAGE("%s", e
.what());
408 handle
->ERROR_MESSAGE("Unknown exception thrown");
414 // call g1->contains(g2)
417 // 2 = error was trapped
419 GEOSContains_r(GEOSContextHandle_t extHandle
, const Geometry
*g1
, const Geometry
*g2
)
421 if ( 0 == extHandle
)
426 GEOSContextHandleInternal_t
*handle
= 0;
427 handle
= reinterpret_cast<GEOSContextHandleInternal_t
*>(extHandle
);
428 if ( 0 == handle
->initialized
)
435 bool result
= g1
->contains(g2
);
438 catch (const std::exception
&e
)
440 handle
->ERROR_MESSAGE("%s", e
.what());
444 handle
->ERROR_MESSAGE("Unknown exception thrown");
451 GEOSOverlaps_r(GEOSContextHandle_t extHandle
, const Geometry
*g1
, const Geometry
*g2
)
453 if ( 0 == extHandle
)
458 GEOSContextHandleInternal_t
*handle
= 0;
459 handle
= reinterpret_cast<GEOSContextHandleInternal_t
*>(extHandle
);
460 if ( 0 == handle
->initialized
)
467 bool result
= g1
->overlaps(g2
);
470 catch (const std::exception
&e
)
472 handle
->ERROR_MESSAGE("%s", e
.what());
476 handle
->ERROR_MESSAGE("Unknown exception thrown");
483 //-------------------------------------------------------------------
484 // low-level relate functions
485 //------------------------------------------------------------------
488 GEOSRelatePattern_r(GEOSContextHandle_t extHandle
, const Geometry
*g1
, const Geometry
*g2
, const char *pat
)
490 if ( 0 == extHandle
)
495 GEOSContextHandleInternal_t
*handle
= 0;
496 handle
= reinterpret_cast<GEOSContextHandleInternal_t
*>(extHandle
);
497 if ( 0 == handle
->initialized
)
505 bool result
= g1
->relate(g2
, s
);
508 catch (const std::exception
&e
)
510 handle
->ERROR_MESSAGE("%s", e
.what());
514 handle
->ERROR_MESSAGE("Unknown exception thrown");
521 GEOSRelate_r(GEOSContextHandle_t extHandle
, const Geometry
*g1
, const Geometry
*g2
)
523 if ( 0 == extHandle
)
528 GEOSContextHandleInternal_t
*handle
= 0;
529 handle
= reinterpret_cast<GEOSContextHandleInternal_t
*>(extHandle
);
530 if ( 0 == handle
->initialized
)
537 using geos::geom::IntersectionMatrix
;
539 IntersectionMatrix
* im
= g1
->relate(g2
);
545 char *result
= gstrdup(im
->toString());
552 catch (const std::exception
&e
)
554 handle
->ERROR_MESSAGE("%s", e
.what());
558 handle
->ERROR_MESSAGE("Unknown exception thrown");
566 //-----------------------------------------------------------------
568 //-----------------------------------------------------------------
572 GEOSisValid_r(GEOSContextHandle_t extHandle
, const Geometry
*g1
)
574 if ( 0 == extHandle
)
579 GEOSContextHandleInternal_t
*handle
= 0;
580 handle
= reinterpret_cast<GEOSContextHandleInternal_t
*>(extHandle
);
581 if ( 0 == handle
->initialized
)
588 using geos::operation::valid::IsValidOp
;
589 using geos::operation::valid::TopologyValidationError
;
592 TopologyValidationError
*err
= ivo
.getValidationError();
595 handle
->NOTICE_MESSAGE("%s", err
->toString().c_str());
603 catch (const std::exception
&e
)
605 handle
->ERROR_MESSAGE("%s", e
.what());
609 handle
->ERROR_MESSAGE("Unknown exception thrown");
616 GEOSisValidReason_r(GEOSContextHandle_t extHandle
, const Geometry
*g1
)
618 if ( 0 == extHandle
)
623 GEOSContextHandleInternal_t
*handle
= 0;
624 handle
= reinterpret_cast<GEOSContextHandleInternal_t
*>(extHandle
);
625 if ( 0 == handle
->initialized
)
632 using geos::operation::valid::IsValidOp
;
633 using geos::operation::valid::TopologyValidationError
;
636 char const* const validstr
= "Valid Geometry";
639 TopologyValidationError
*err
= ivo
.getValidationError();
642 std::ostringstream ss
;
644 ss
<< err
->getCoordinate();
645 const std::string errloc
= ss
.str();
646 std::string
errmsg(err
->getMessage());
647 errmsg
+= "[" + errloc
+ "]";
648 result
= gstrdup(errmsg
);
652 result
= gstrdup(std::string(validstr
));
657 catch (const std::exception
&e
)
659 handle
->ERROR_MESSAGE("%s", e
.what());
663 handle
->ERROR_MESSAGE("Unknown exception thrown");
670 GEOSisValidDetail_r(GEOSContextHandle_t extHandle
, const Geometry
*g
,
671 char** reason
, const Geometry
** location
)
673 if ( 0 == extHandle
)
678 GEOSContextHandleInternal_t
*handle
= 0;
679 handle
= reinterpret_cast<GEOSContextHandleInternal_t
*>(extHandle
);
680 if ( 0 == handle
->initialized
)
687 using geos::operation::valid::IsValidOp
;
688 using geos::operation::valid::TopologyValidationError
;
691 TopologyValidationError
*err
= ivo
.getValidationError();
694 *location
= handle
->geomFactory
->createPoint(err
->getCoordinate());
695 std::string
errmsg(err
->getMessage());
696 *reason
= gstrdup(errmsg
);
702 return 1; /* valid */
705 catch (const std::exception
&e
)
707 handle
->ERROR_MESSAGE("%s", e
.what());
711 handle
->ERROR_MESSAGE("Unknown exception thrown");
714 return 2; /* exception */
717 //-----------------------------------------------------------------
719 //-----------------------------------------------------------------
722 GEOSEquals_r(GEOSContextHandle_t extHandle
, const Geometry
*g1
, const Geometry
*g2
)
724 if ( 0 == extHandle
)
729 GEOSContextHandleInternal_t
*handle
= 0;
730 handle
= reinterpret_cast<GEOSContextHandleInternal_t
*>(extHandle
);
731 if ( 0 == handle
->initialized
)
738 bool result
= g1
->equals(g2
);
741 catch (const std::exception
&e
)
743 handle
->ERROR_MESSAGE("%s", e
.what());
747 handle
->ERROR_MESSAGE("Unknown exception thrown");
754 GEOSEqualsExact_r(GEOSContextHandle_t extHandle
, const Geometry
*g1
, const Geometry
*g2
, double tolerance
)
756 if ( 0 == extHandle
)
761 GEOSContextHandleInternal_t
*handle
= 0;
762 handle
= reinterpret_cast<GEOSContextHandleInternal_t
*>(extHandle
);
763 if ( 0 == handle
->initialized
)
770 bool result
= g1
->equalsExact(g2
, tolerance
);
773 catch (const std::exception
&e
)
775 handle
->ERROR_MESSAGE("%s", e
.what());
779 handle
->ERROR_MESSAGE("Unknown exception thrown");
786 GEOSDistance_r(GEOSContextHandle_t extHandle
, const Geometry
*g1
, const Geometry
*g2
, double *dist
)
790 if ( 0 == extHandle
)
795 GEOSContextHandleInternal_t
*handle
= 0;
796 handle
= reinterpret_cast<GEOSContextHandleInternal_t
*>(extHandle
);
797 if ( 0 == handle
->initialized
)
804 *dist
= g1
->distance(g2
);
807 catch (const std::exception
&e
)
809 handle
->ERROR_MESSAGE("%s", e
.what());
813 handle
->ERROR_MESSAGE("Unknown exception thrown");
820 GEOSHausdorffDistance_r(GEOSContextHandle_t extHandle
, const Geometry
*g1
, const Geometry
*g2
, double *dist
)
824 if ( 0 == extHandle
)
829 GEOSContextHandleInternal_t
*handle
= 0;
830 handle
= reinterpret_cast<GEOSContextHandleInternal_t
*>(extHandle
);
831 if ( 0 == handle
->initialized
)
838 *dist
= DiscreteHausdorffDistance::distance(*g1
, *g2
);
841 catch (const std::exception
&e
)
843 handle
->ERROR_MESSAGE("%s", e
.what());
847 handle
->ERROR_MESSAGE("Unknown exception thrown");
854 GEOSHausdorffDistanceDensify_r(GEOSContextHandle_t extHandle
, const Geometry
*g1
, const Geometry
*g2
, double densifyFrac
, double *dist
)
858 if ( 0 == extHandle
)
863 GEOSContextHandleInternal_t
*handle
= 0;
864 handle
= reinterpret_cast<GEOSContextHandleInternal_t
*>(extHandle
);
865 if ( 0 == handle
->initialized
)
872 *dist
= DiscreteHausdorffDistance::distance(*g1
, *g2
, densifyFrac
);
875 catch (const std::exception
&e
)
877 handle
->ERROR_MESSAGE("%s", e
.what());
881 handle
->ERROR_MESSAGE("Unknown exception thrown");
888 GEOSArea_r(GEOSContextHandle_t extHandle
, const Geometry
*g
, double *area
)
892 if ( 0 == extHandle
)
897 GEOSContextHandleInternal_t
*handle
= 0;
898 handle
= reinterpret_cast<GEOSContextHandleInternal_t
*>(extHandle
);
899 if ( 0 == handle
->initialized
)
906 *area
= g
->getArea();
909 catch (const std::exception
&e
)
911 handle
->ERROR_MESSAGE("%s", e
.what());
915 handle
->ERROR_MESSAGE("Unknown exception thrown");
922 GEOSLength_r(GEOSContextHandle_t extHandle
, const Geometry
*g
, double *length
)
926 if ( 0 == extHandle
)
931 GEOSContextHandleInternal_t
*handle
= 0;
932 handle
= reinterpret_cast<GEOSContextHandleInternal_t
*>(extHandle
);
933 if ( 0 == handle
->initialized
)
940 *length
= g
->getLength();
943 catch (const std::exception
&e
)
945 handle
->ERROR_MESSAGE("%s", e
.what());
949 handle
->ERROR_MESSAGE("Unknown exception thrown");
956 GEOSGeomFromWKT_r(GEOSContextHandle_t extHandle
, const char *wkt
)
958 if ( 0 == extHandle
)
963 GEOSContextHandleInternal_t
*handle
= 0;
964 handle
= reinterpret_cast<GEOSContextHandleInternal_t
*>(extHandle
);
965 if ( 0 == handle
->initialized
)
972 const std::string
wktstring(wkt
);
973 WKTReader
r(static_cast<GeometryFactory
const*>(handle
->geomFactory
));
975 Geometry
*g
= r
.read(wktstring
);
978 catch (const std::exception
&e
)
980 handle
->ERROR_MESSAGE("%s", e
.what());
984 handle
->ERROR_MESSAGE("Unknown exception thrown");
991 GEOSGeomToWKT_r(GEOSContextHandle_t extHandle
, const Geometry
*g1
)
993 if ( 0 == extHandle
)
998 GEOSContextHandleInternal_t
*handle
= 0;
999 handle
= reinterpret_cast<GEOSContextHandleInternal_t
*>(extHandle
);
1000 if ( 0 == handle
->initialized
)
1008 char *result
= gstrdup(g1
->toString());
1011 catch (const std::exception
&e
)
1013 handle
->ERROR_MESSAGE("%s", e
.what());
1017 handle
->ERROR_MESSAGE("Unknown exception thrown");
1022 // Remember to free the result!
1024 GEOSGeomToWKB_buf_r(GEOSContextHandle_t extHandle
, const Geometry
*g
, size_t *size
)
1028 if ( 0 == extHandle
)
1033 GEOSContextHandleInternal_t
*handle
= 0;
1034 handle
= reinterpret_cast<GEOSContextHandleInternal_t
*>(extHandle
);
1035 if ( 0 == handle
->initialized
)
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
);
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
));
1054 std::memcpy(result
, wkbstring
.c_str(), len
);
1059 catch (const std::exception
&e
)
1061 handle
->ERROR_MESSAGE("%s", e
.what());
1065 handle
->ERROR_MESSAGE("Unknown exception thrown");
1072 GEOSGeomFromWKB_buf_r(GEOSContextHandle_t extHandle
, const unsigned char *wkb
, size_t size
)
1074 if ( 0 == extHandle
)
1079 GEOSContextHandleInternal_t
*handle
= 0;
1080 handle
= reinterpret_cast<GEOSContextHandleInternal_t
*>(extHandle
);
1081 if ( 0 == handle
->initialized
)
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
);
1093 is
.seekg(0, std::ios::beg
); // rewind reader pointer
1094 Geometry
*g
= r
.read(is
);
1097 catch (const std::exception
&e
)
1099 handle
->ERROR_MESSAGE("%s", e
.what());
1103 handle
->ERROR_MESSAGE("Unknown exception thrown");
1109 /* Read/write wkb hex values. Returned geometries are
1110 owned by the caller.*/
1112 GEOSGeomToHEX_buf_r(GEOSContextHandle_t extHandle
, const Geometry
*g
, size_t *size
)
1114 if ( 0 == extHandle
)
1119 GEOSContextHandleInternal_t
*handle
= 0;
1120 handle
= reinterpret_cast<GEOSContextHandleInternal_t
*>(extHandle
);
1121 if ( 0 == handle
->initialized
)
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
);
1133 std::string
hexstring(os
.str());
1135 char *result
= gstrdup(hexstring
);
1138 *size
= hexstring
.length();
1141 return reinterpret_cast<unsigned char*>(result
);
1143 catch (const std::exception
&e
)
1145 handle
->ERROR_MESSAGE("%s", e
.what());
1149 handle
->ERROR_MESSAGE("Unknown exception thrown");
1156 GEOSGeomFromHEX_buf_r(GEOSContextHandle_t extHandle
, const unsigned char *hex
, size_t size
)
1158 if ( 0 == extHandle
)
1163 GEOSContextHandleInternal_t
*handle
= 0;
1164 handle
= reinterpret_cast<GEOSContextHandleInternal_t
*>(extHandle
);
1165 if ( 0 == handle
->initialized
)
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
);
1177 is
.seekg(0, std::ios::beg
); // rewind reader pointer
1179 Geometry
*g
= r
.readHEX(is
);
1182 catch (const std::exception
&e
)
1184 handle
->ERROR_MESSAGE("%s", e
.what());
1188 handle
->ERROR_MESSAGE("Unknown exception thrown");
1195 GEOSisEmpty_r(GEOSContextHandle_t extHandle
, const Geometry
*g1
)
1197 if ( 0 == extHandle
)
1202 GEOSContextHandleInternal_t
*handle
= 0;
1203 handle
= reinterpret_cast<GEOSContextHandleInternal_t
*>(extHandle
);
1204 if ( 0 == handle
->initialized
)
1211 return g1
->isEmpty();
1213 catch (const std::exception
&e
)
1215 handle
->ERROR_MESSAGE("%s", e
.what());
1219 handle
->ERROR_MESSAGE("Unknown exception thrown");
1226 GEOSisSimple_r(GEOSContextHandle_t extHandle
, const Geometry
*g1
)
1228 if ( 0 == extHandle
)
1233 GEOSContextHandleInternal_t
*handle
= 0;
1234 handle
= reinterpret_cast<GEOSContextHandleInternal_t
*>(extHandle
);
1235 if ( 0 == handle
->initialized
)
1242 return g1
->isSimple();
1244 catch (const std::exception
&e
)
1246 handle
->ERROR_MESSAGE("%s", e
.what());
1252 handle
->ERROR_MESSAGE("Unknown exception thrown");
1258 GEOSisRing_r(GEOSContextHandle_t extHandle
, const Geometry
*g
)
1260 if ( 0 == extHandle
)
1265 GEOSContextHandleInternal_t
*handle
= 0;
1266 handle
= reinterpret_cast<GEOSContextHandleInternal_t
*>(extHandle
);
1267 if ( 0 == handle
->initialized
)
1274 const LineString
*ls
= dynamic_cast<const LineString
*>(g
);
1276 return (ls
->isRing());
1281 catch (const std::exception
&e
)
1283 handle
->ERROR_MESSAGE("%s", e
.what());
1289 handle
->ERROR_MESSAGE("Unknown exception thrown");
1296 //free the result of this
1298 GEOSGeomType_r(GEOSContextHandle_t extHandle
, const Geometry
*g1
)
1300 if ( 0 == extHandle
)
1305 GEOSContextHandleInternal_t
*handle
= 0;
1306 handle
= reinterpret_cast<GEOSContextHandleInternal_t
*>(extHandle
);
1307 if ( 0 == handle
->initialized
)
1314 std::string s
= g1
->getGeometryType();
1316 char *result
= gstrdup(s
);
1319 catch (const std::exception
&e
)
1321 handle
->ERROR_MESSAGE("%s", e
.what());
1325 handle
->ERROR_MESSAGE("Unknown exception thrown");
1331 // Return postgis geometry type index
1333 GEOSGeomTypeId_r(GEOSContextHandle_t extHandle
, const Geometry
*g1
)
1335 if ( 0 == extHandle
)
1340 GEOSContextHandleInternal_t
*handle
= 0;
1341 handle
= reinterpret_cast<GEOSContextHandleInternal_t
*>(extHandle
);
1342 if ( 0 == handle
->initialized
)
1349 return g1
->getGeometryTypeId();
1351 catch (const std::exception
&e
)
1353 handle
->ERROR_MESSAGE("%s", e
.what());
1357 handle
->ERROR_MESSAGE("Unknown exception thrown");
1363 //-------------------------------------------------------------------
1364 // GEOS functions that return geometries
1365 //-------------------------------------------------------------------
1368 GEOSEnvelope_r(GEOSContextHandle_t extHandle
, const Geometry
*g1
)
1370 if ( 0 == extHandle
)
1375 GEOSContextHandleInternal_t
*handle
= 0;
1376 handle
= reinterpret_cast<GEOSContextHandleInternal_t
*>(extHandle
);
1377 if ( 0 == handle
->initialized
)
1384 Geometry
*g3
= g1
->getEnvelope();
1387 catch (const std::exception
&e
)
1389 handle
->ERROR_MESSAGE("%s", e
.what());
1393 handle
->ERROR_MESSAGE("Unknown exception thrown");
1400 GEOSIntersection_r(GEOSContextHandle_t extHandle
, const Geometry
*g1
, const Geometry
*g2
)
1402 if ( 0 == extHandle
)
1407 GEOSContextHandleInternal_t
*handle
= 0;
1408 handle
= reinterpret_cast<GEOSContextHandleInternal_t
*>(extHandle
);
1409 if ( 0 == handle
->initialized
)
1416 GeomAutoPtr
g3(BinaryOp(g1
, g2
, overlayOp(OverlayOp::opINTERSECTION
)));
1417 return g3
.release();
1420 //Geometry *g3 = g1->intersection(g2);
1423 catch (const std::exception
&e
)
1425 handle
->ERROR_MESSAGE("%s", e
.what());
1429 handle
->ERROR_MESSAGE("Unknown exception thrown");
1436 GEOSBuffer_r(GEOSContextHandle_t extHandle
, const Geometry
*g1
, double width
, int quadrantsegments
)
1438 if ( 0 == extHandle
)
1443 GEOSContextHandleInternal_t
*handle
= 0;
1444 handle
= reinterpret_cast<GEOSContextHandleInternal_t
*>(extHandle
);
1445 if ( 0 == handle
->initialized
)
1452 Geometry
*g3
= g1
->buffer(width
, quadrantsegments
);
1455 catch (const std::exception
&e
)
1457 handle
->ERROR_MESSAGE("%s", e
.what());
1461 handle
->ERROR_MESSAGE("Unknown exception thrown");
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
)
1479 GEOSContextHandleInternal_t
*handle
= 0;
1480 handle
= reinterpret_cast<GEOSContextHandleInternal_t
*>(extHandle
);
1481 if ( 0 == handle
->initialized
)
1488 BufferParameters bp
;
1489 bp
.setQuadrantSegments(quadsegs
);
1491 if ( endCapStyle
> BufferParameters::CAP_SQUARE
)
1493 throw IllegalArgumentException("Invalid buffer endCap style");
1496 static_cast<BufferParameters::EndCapStyle
>(endCapStyle
)
1499 if ( joinStyle
> BufferParameters::JOIN_BEVEL
)
1501 throw IllegalArgumentException("Invalid buffer join style");
1504 static_cast<BufferParameters::JoinStyle
>(joinStyle
)
1506 bp
.setMitreLimit(mitreLimit
);
1507 BufferOp
op(g1
, bp
);
1508 Geometry
*g3
= op
.getResultGeometry(width
);
1511 catch (const std::exception
&e
)
1513 handle
->ERROR_MESSAGE("%s", e
.what());
1517 handle
->ERROR_MESSAGE("Unknown exception thrown");
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
)
1536 GEOSContextHandleInternal_t
*handle
= 0;
1537 handle
= reinterpret_cast<GEOSContextHandleInternal_t
*>(extHandle
);
1538 if ( 0 == handle
->initialized
)
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");
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
);
1564 catch (const std::exception
&e
)
1566 handle
->ERROR_MESSAGE("%s", e
.what());
1570 handle
->ERROR_MESSAGE("Unknown exception thrown");
1577 GEOSConvexHull_r(GEOSContextHandle_t extHandle
, const Geometry
*g1
)
1579 if ( 0 == extHandle
)
1584 GEOSContextHandleInternal_t
*handle
= 0;
1585 handle
= reinterpret_cast<GEOSContextHandleInternal_t
*>(extHandle
);
1586 if ( 0 == handle
->initialized
)
1593 Geometry
*g3
= g1
->convexHull();
1596 catch (const std::exception
&e
)
1598 handle
->ERROR_MESSAGE("%s", e
.what());
1602 handle
->ERROR_MESSAGE("Unknown exception thrown");
1609 GEOSDifference_r(GEOSContextHandle_t extHandle
, const Geometry
*g1
, const Geometry
*g2
)
1611 if ( 0 == extHandle
)
1616 GEOSContextHandleInternal_t
*handle
= 0;
1617 handle
= reinterpret_cast<GEOSContextHandleInternal_t
*>(extHandle
);
1618 if ( 0 == handle
->initialized
)
1625 GeomAutoPtr
g3(BinaryOp(g1
, g2
, overlayOp(OverlayOp::opDIFFERENCE
)));
1626 return g3
.release();
1629 //Geometry *g3 = g1->difference(g2);
1632 catch (const std::exception
&e
)
1634 handle
->ERROR_MESSAGE("%s", e
.what());
1638 handle
->ERROR_MESSAGE("Unknown exception thrown");
1645 GEOSBoundary_r(GEOSContextHandle_t extHandle
, const Geometry
*g1
)
1647 if ( 0 == extHandle
)
1652 GEOSContextHandleInternal_t
*handle
= 0;
1653 handle
= reinterpret_cast<GEOSContextHandleInternal_t
*>(extHandle
);
1654 if ( 0 == handle
->initialized
)
1661 Geometry
*g3
= g1
->getBoundary();
1664 catch (const std::exception
&e
)
1666 handle
->ERROR_MESSAGE("%s", e
.what());
1670 handle
->ERROR_MESSAGE("Unknown exception thrown");
1677 GEOSSymDifference_r(GEOSContextHandle_t extHandle
, const Geometry
*g1
, const Geometry
*g2
)
1679 if ( 0 == extHandle
)
1684 GEOSContextHandleInternal_t
*handle
= 0;
1685 handle
= reinterpret_cast<GEOSContextHandleInternal_t
*>(extHandle
);
1686 if ( 0 == handle
->initialized
)
1693 GeomAutoPtr g3
= BinaryOp(g1
, g2
, overlayOp(OverlayOp::opSYMDIFFERENCE
));
1694 return g3
.release();
1695 //Geometry *g3 = g1->symDifference(g2);
1698 catch (const std::exception
&e
)
1700 handle
->ERROR_MESSAGE("%s", e
.what());
1706 handle
->ERROR_MESSAGE("Unknown exception thrown");
1712 GEOSUnion_r(GEOSContextHandle_t extHandle
, const Geometry
*g1
, const Geometry
*g2
)
1714 if ( 0 == extHandle
)
1719 GEOSContextHandleInternal_t
*handle
= 0;
1720 handle
= reinterpret_cast<GEOSContextHandleInternal_t
*>(extHandle
);
1721 if ( 0 == handle
->initialized
)
1728 GeomAutoPtr g3
= BinaryOp(g1
, g2
, overlayOp(OverlayOp::opUNION
));
1729 return g3
.release();
1732 //Geometry *g3 = g1->Union(g2);
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());
1748 handle
->ERROR_MESSAGE("Unknown exception thrown");
1755 GEOSUnionCascaded_r(GEOSContextHandle_t extHandle
, const Geometry
*g1
)
1757 if ( 0 == extHandle
)
1762 GEOSContextHandleInternal_t
*handle
= 0;
1763 handle
= reinterpret_cast<GEOSContextHandleInternal_t
*>(extHandle
);
1764 if ( 0 == handle
->initialized
)
1771 const geos::geom::MultiPolygon
*p
= dynamic_cast<const geos::geom::MultiPolygon
*>(g1
);
1774 handle
->ERROR_MESSAGE("Invalid argument (must be a MultiPolygon)");
1778 using geos::operation::geounion::CascadedPolygonUnion
;
1779 return CascadedPolygonUnion::Union(p
);
1781 catch (const std::exception
&e
)
1783 handle
->ERROR_MESSAGE("%s", e
.what());
1787 handle
->ERROR_MESSAGE("Unknown exception thrown");
1794 GEOSPointOnSurface_r(GEOSContextHandle_t extHandle
, const Geometry
*g1
)
1796 if ( 0 == extHandle
)
1801 GEOSContextHandleInternal_t
*handle
= 0;
1802 handle
= reinterpret_cast<GEOSContextHandleInternal_t
*>(extHandle
);
1803 if ( 0 == handle
->initialized
)
1810 Geometry
*ret
= g1
->getInteriorPoint();
1813 const GeometryFactory
* gf
= handle
->geomFactory
;
1814 // return an empty collection
1815 return gf
->createGeometryCollection();
1819 catch (const std::exception
&e
)
1821 handle
->ERROR_MESSAGE("%s", e
.what());
1825 handle
->ERROR_MESSAGE("Unknown exception thrown");
1831 //-------------------------------------------------------------------
1832 // memory management functions
1833 //------------------------------------------------------------------
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
1848 catch (const std::exception
&e
)
1850 if ( 0 == extHandle
)
1855 handle
= reinterpret_cast<GEOSContextHandleInternal_t
*>(extHandle
);
1856 if ( 0 == handle
->initialized
)
1861 handle
->ERROR_MESSAGE("%s", e
.what());
1865 if ( 0 == extHandle
)
1870 handle
= reinterpret_cast<GEOSContextHandleInternal_t
*>(extHandle
);
1871 if ( 0 == handle
->initialized
)
1876 handle
->ERROR_MESSAGE("Unknown exception thrown");
1881 GEOSSetSRID_r(GEOSContextHandle_t extHandle
, Geometry
*g
, int srid
)
1885 if ( 0 == extHandle
)
1890 GEOSContextHandleInternal_t
*handle
= 0;
1891 handle
= reinterpret_cast<GEOSContextHandleInternal_t
*>(extHandle
);
1892 if ( 0 == handle
->initialized
)
1902 GEOSGetNumCoordinates_r(GEOSContextHandle_t extHandle
, const Geometry
*g
)
1906 if ( 0 == extHandle
)
1911 GEOSContextHandleInternal_t
*handle
= 0;
1912 handle
= reinterpret_cast<GEOSContextHandleInternal_t
*>(extHandle
);
1913 if ( 0 == handle
->initialized
)
1920 return static_cast<int>(g
->getNumPoints());
1922 catch (const std::exception
&e
)
1924 handle
->ERROR_MESSAGE("%s", e
.what());
1928 handle
->ERROR_MESSAGE("Unknown exception thrown");
1935 * Return -1 on exception, 0 otherwise.
1936 * Converts Geometry to normal form (or canonical form).
1939 GEOSNormalize_r(GEOSContextHandle_t extHandle
, Geometry
*g
)
1943 if ( 0 == extHandle
)
1948 GEOSContextHandleInternal_t
*handle
= 0;
1949 handle
= reinterpret_cast<GEOSContextHandleInternal_t
*>(extHandle
);
1950 if ( 0 == handle
->initialized
)
1958 return 0; // SUCCESS
1960 catch (const std::exception
&e
)
1962 handle
->ERROR_MESSAGE("%s", e
.what());
1966 handle
->ERROR_MESSAGE("Unknown exception thrown");
1973 GEOSGetNumInteriorRings_r(GEOSContextHandle_t extHandle
, const Geometry
*g1
)
1975 if ( 0 == extHandle
)
1980 GEOSContextHandleInternal_t
*handle
= 0;
1981 handle
= reinterpret_cast<GEOSContextHandleInternal_t
*>(extHandle
);
1982 if ( 0 == handle
->initialized
)
1989 const Polygon
*p
= dynamic_cast<const Polygon
*>(g1
);
1992 handle
->ERROR_MESSAGE("Argument is not a Polygon");
1995 return static_cast<int>(p
->getNumInteriorRing());
1997 catch (const std::exception
&e
)
1999 handle
->ERROR_MESSAGE("%s", e
.what());
2003 handle
->ERROR_MESSAGE("Unknown exception thrown");
2010 // returns -1 on error and 1 for non-multi geometries
2012 GEOSGetNumGeometries_r(GEOSContextHandle_t extHandle
, const Geometry
*g1
)
2014 if ( 0 == extHandle
)
2019 GEOSContextHandleInternal_t
*handle
= 0;
2020 handle
= reinterpret_cast<GEOSContextHandleInternal_t
*>(extHandle
);
2021 if ( 0 == handle
->initialized
)
2028 return static_cast<int>(g1
->getNumGeometries());
2030 catch (const std::exception
&e
)
2032 handle
->ERROR_MESSAGE("%s", e
.what());
2036 handle
->ERROR_MESSAGE("Unknown exception thrown");
2044 * Call only on GEOMETRYCOLLECTION or MULTI*.
2045 * Return a pointer to the internal Geometry.
2048 GEOSGetGeometryN_r(GEOSContextHandle_t extHandle
, const Geometry
*g1
, int n
)
2050 if ( 0 == extHandle
)
2055 GEOSContextHandleInternal_t
*handle
= 0;
2056 handle
= reinterpret_cast<GEOSContextHandleInternal_t
*>(extHandle
);
2057 if ( 0 == handle
->initialized
)
2064 return g1
->getGeometryN(n
);
2066 catch (const std::exception
&e
)
2068 handle
->ERROR_MESSAGE("%s", e
.what());
2072 handle
->ERROR_MESSAGE("Unknown exception thrown");
2080 * Call only on polygon
2081 * Return a copy of the internal Geometry.
2084 GEOSGetExteriorRing_r(GEOSContextHandle_t extHandle
, const Geometry
*g1
)
2086 if ( 0 == extHandle
)
2091 GEOSContextHandleInternal_t
*handle
= 0;
2092 handle
= reinterpret_cast<GEOSContextHandleInternal_t
*>(extHandle
);
2093 if ( 0 == handle
->initialized
)
2100 const Polygon
*p
= dynamic_cast<const Polygon
*>(g1
);
2103 handle
->ERROR_MESSAGE("Invalid argument (must be a Polygon)");
2106 return p
->getExteriorRing();
2108 catch (const std::exception
&e
)
2110 handle
->ERROR_MESSAGE("%s", e
.what());
2114 handle
->ERROR_MESSAGE("Unknown exception thrown");
2121 * Call only on polygon
2122 * Return a pointer to internal storage, do not destroy it.
2125 GEOSGetInteriorRingN_r(GEOSContextHandle_t extHandle
, const Geometry
*g1
, int n
)
2127 if ( 0 == extHandle
)
2132 GEOSContextHandleInternal_t
*handle
= 0;
2133 handle
= reinterpret_cast<GEOSContextHandleInternal_t
*>(extHandle
);
2134 if ( 0 == handle
->initialized
)
2141 const Polygon
*p
= dynamic_cast<const Polygon
*>(g1
);
2144 handle
->ERROR_MESSAGE("Invalid argument (must be a Polygon)");
2147 return p
->getInteriorRingN(n
);
2149 catch (const std::exception
&e
)
2151 handle
->ERROR_MESSAGE("%s", e
.what());
2155 handle
->ERROR_MESSAGE("Unknown exception thrown");
2162 GEOSGetCentroid_r(GEOSContextHandle_t extHandle
, const Geometry
*g
)
2164 if ( 0 == extHandle
)
2169 GEOSContextHandleInternal_t
*handle
= 0;
2170 handle
= reinterpret_cast<GEOSContextHandleInternal_t
*>(extHandle
);
2171 if ( 0 == handle
->initialized
)
2178 Geometry
*ret
= g
->getCentroid();
2181 const GeometryFactory
*gf
= handle
->geomFactory
;
2182 return gf
->createGeometryCollection();
2186 catch (const std::exception
&e
)
2188 handle
->ERROR_MESSAGE("%s", e
.what());
2192 handle
->ERROR_MESSAGE("Unknown exception thrown");
2199 GEOSGeom_createEmptyCollection_r(GEOSContextHandle_t extHandle
, int type
)
2201 if ( 0 == extHandle
)
2206 GEOSContextHandleInternal_t
*handle
= 0;
2207 handle
= reinterpret_cast<GEOSContextHandleInternal_t
*>(extHandle
);
2208 if ( 0 == handle
->initialized
)
2215 sprintf(buf
, "createCollection: requested type %d, ngeoms: %d",
2217 handle
->NOTICE_MESSAGE("%s", buf
);// TODO: Can handle->NOTICE_MESSAGE format that directly?
2222 const GeometryFactory
* gf
= handle
->geomFactory
;
2227 case GEOS_GEOMETRYCOLLECTION
:
2228 g
= gf
->createGeometryCollection();
2230 case GEOS_MULTIPOINT
:
2231 g
= gf
->createMultiPoint();
2233 case GEOS_MULTILINESTRING
:
2234 g
= gf
->createMultiLineString();
2236 case GEOS_MULTIPOLYGON
:
2237 g
= gf
->createMultiPolygon();
2240 handle
->ERROR_MESSAGE("Unsupported type request for GEOSGeom_createEmptyCollection_r");
2247 catch (const std::exception
&e
)
2249 handle
->ERROR_MESSAGE("%s", e
.what());
2253 handle
->ERROR_MESSAGE("Unknown exception thrown");
2260 GEOSGeom_createCollection_r(GEOSContextHandle_t extHandle
, int type
, Geometry
**geoms
, unsigned int ngeoms
)
2262 if ( 0 == extHandle
)
2267 GEOSContextHandleInternal_t
*handle
= 0;
2268 handle
= reinterpret_cast<GEOSContextHandleInternal_t
*>(extHandle
);
2269 if ( 0 == handle
->initialized
)
2276 sprintf(buf
, "PostGIS2GEOS_collection: requested type %d, ngeoms: %d",
2278 handle
->NOTICE_MESSAGE("%s", buf
);// TODO: Can handle->NOTICE_MESSAGE format that directly?
2283 const GeometryFactory
* gf
= handle
->geomFactory
;
2284 std::vector
<Geometry
*>* vgeoms
= new std::vector
<Geometry
*>(geoms
, geoms
+ ngeoms
);
2289 case GEOS_GEOMETRYCOLLECTION
:
2290 g
= gf
->createGeometryCollection(vgeoms
);
2292 case GEOS_MULTIPOINT
:
2293 g
= gf
->createMultiPoint(vgeoms
);
2295 case GEOS_MULTILINESTRING
:
2296 g
= gf
->createMultiLineString(vgeoms
);
2298 case GEOS_MULTIPOLYGON
:
2299 g
= gf
->createMultiPolygon(vgeoms
);
2302 handle
->ERROR_MESSAGE("Unsupported type request for PostGIS2GEOS_collection");
2309 catch (const std::exception
&e
)
2311 handle
->ERROR_MESSAGE("%s", e
.what());
2315 handle
->ERROR_MESSAGE("Unknown exception thrown");
2322 GEOSPolygonize_r(GEOSContextHandle_t extHandle
, const Geometry
* const * g
, unsigned int ngeoms
)
2324 if ( 0 == extHandle
)
2329 GEOSContextHandleInternal_t
*handle
= 0;
2330 handle
= reinterpret_cast<GEOSContextHandleInternal_t
*>(extHandle
);
2331 if ( 0 == handle
->initialized
)
2341 using geos::operation::polygonize::Polygonizer
;
2343 for (std::size_t i
= 0; i
< ngeoms
; ++i
)
2349 handle
->NOTICE_MESSAGE("geometry vector added to polygonizer");
2352 std::vector
<Polygon
*> *polys
= plgnzr
.getPolygons();
2356 handle
->NOTICE_MESSAGE("output polygons got");
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
];
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());
2389 handle
->ERROR_MESSAGE("Unknown exception thrown");
2396 GEOSPolygonizer_getCutEdges_r(GEOSContextHandle_t extHandle
, const Geometry
* const * g
, unsigned int ngeoms
)
2398 if ( 0 == extHandle
)
2403 GEOSContextHandleInternal_t
*handle
= 0;
2404 handle
= reinterpret_cast<GEOSContextHandleInternal_t
*>(extHandle
);
2405 if ( 0 == handle
->initialized
)
2415 using geos::operation::polygonize::Polygonizer
;
2417 for (std::size_t i
= 0; i
< ngeoms
; ++i
)
2423 handle
->NOTICE_MESSAGE("geometry vector added to polygonizer");
2426 const std::vector
<const LineString
*>& lines
= plgnzr
.getCutEdges();
2429 handle
->NOTICE_MESSAGE("output polygons got");
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());
2456 handle
->ERROR_MESSAGE("Unknown exception thrown");
2463 GEOSPolygonize_full_r(GEOSContextHandle_t extHandle
, const Geometry
* g
,
2464 Geometry
** cuts
, Geometry
** dangles
, Geometry
** invalid
)
2466 if ( 0 == extHandle
)
2471 GEOSContextHandleInternal_t
*handle
= 0;
2472 handle
= reinterpret_cast<GEOSContextHandleInternal_t
*>(extHandle
);
2473 if ( 0 == handle
->initialized
)
2481 using geos::operation::polygonize::Polygonizer
;
2483 for (std::size_t i
= 0; i
<g
->getNumGeometries(); ++i
)
2485 plgnzr
.add(g
->getGeometryN(i
));
2489 handle
->NOTICE_MESSAGE("geometry vector added to polygonizer");
2491 const GeometryFactory
*gf
= handle
->geomFactory
;
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
);
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
);
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
];
2543 return gf
->createGeometryCollection(polyvec
);
2546 catch (const std::exception
&e
)
2548 handle
->ERROR_MESSAGE("%s", e
.what());
2553 handle
->ERROR_MESSAGE("Unknown exception thrown");
2559 GEOSLineMerge_r(GEOSContextHandle_t extHandle
, const Geometry
*g
)
2561 if ( 0 == extHandle
)
2566 GEOSContextHandleInternal_t
*handle
= 0;
2567 handle
= reinterpret_cast<GEOSContextHandleInternal_t
*>(extHandle
);
2568 if ( 0 == handle
->initialized
)
2577 using geos::operation::linemerge::LineMerger
;
2581 std::vector
<LineString
*>* lines
= lmrgr
.getMergedLineStrings();
2585 handle
->NOTICE_MESSAGE("output lines got");
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
];
2596 const GeometryFactory
*gf
= handle
->geomFactory
;
2597 out
= gf
->buildGeometry(geoms
);
2600 //out = gf->createGeometryCollection(geoms);
2602 catch (const std::exception
&e
)
2604 handle
->ERROR_MESSAGE("%s", e
.what());
2608 handle
->ERROR_MESSAGE("Unknown exception thrown");
2615 GEOSGetSRID_r(GEOSContextHandle_t extHandle
, const Geometry
*g
)
2619 if ( 0 == extHandle
)
2624 GEOSContextHandleInternal_t
*handle
= 0;
2625 handle
= reinterpret_cast<GEOSContextHandleInternal_t
*>(extHandle
);
2626 if ( 0 == handle
->initialized
)
2633 return g
->getSRID();
2635 catch (const std::exception
&e
)
2637 handle
->ERROR_MESSAGE("%s", e
.what());
2641 handle
->ERROR_MESSAGE("Unknown exception thrown");
2647 const char* GEOSversion()
2649 return GEOS_CAPI_VERSION
;
2652 const char* GEOSjtsport()
2654 return GEOS_JTS_PORT
;
2658 GEOSHasZ_r(GEOSContextHandle_t extHandle
, const Geometry
*g
)
2662 if ( 0 == extHandle
)
2667 GEOSContextHandleInternal_t
*handle
= 0;
2668 handle
= reinterpret_cast<GEOSContextHandleInternal_t
*>(extHandle
);
2669 if ( 0 == handle
->initialized
)
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
)
2694 GEOSContextHandleInternal_t
*handle
= 0;
2695 handle
= reinterpret_cast<GEOSContextHandleInternal_t
*>(extHandle
);
2696 if ( 0 == handle
->initialized
)
2701 return handle
->WKBOutputDims
;
2705 GEOS_setWKBOutputDims_r(GEOSContextHandle_t extHandle
, int newdims
)
2707 if ( 0 == extHandle
)
2712 GEOSContextHandleInternal_t
*handle
= 0;
2713 handle
= reinterpret_cast<GEOSContextHandleInternal_t
*>(extHandle
);
2714 if ( 0 == handle
->initialized
)
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
;
2731 GEOS_getWKBByteOrder_r(GEOSContextHandle_t extHandle
)
2733 if ( 0 == extHandle
)
2738 GEOSContextHandleInternal_t
*handle
= 0;
2739 handle
= reinterpret_cast<GEOSContextHandleInternal_t
*>(extHandle
);
2740 if ( 0 == handle
->initialized
)
2745 return handle
->WKBByteOrder
;
2749 GEOS_setWKBByteOrder_r(GEOSContextHandle_t extHandle
, int byteOrder
)
2751 if ( 0 == extHandle
)
2756 GEOSContextHandleInternal_t
*handle
= 0;
2757 handle
= reinterpret_cast<GEOSContextHandleInternal_t
*>(extHandle
);
2758 if ( 0 == handle
->initialized
)
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
)
2778 GEOSContextHandleInternal_t
*handle
= 0;
2779 handle
= reinterpret_cast<GEOSContextHandleInternal_t
*>(extHandle
);
2780 if ( 0 == handle
->initialized
)
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());
2796 handle
->ERROR_MESSAGE("Unknown exception thrown");
2803 GEOSCoordSeq_setOrdinate_r(GEOSContextHandle_t extHandle
, CoordinateSequence
*cs
,
2804 unsigned int idx
, unsigned int dim
, double val
)
2807 if ( 0 == extHandle
)
2812 GEOSContextHandleInternal_t
*handle
= 0;
2813 handle
= reinterpret_cast<GEOSContextHandleInternal_t
*>(extHandle
);
2814 if ( 0 == handle
->initialized
)
2821 cs
->setOrdinate(static_cast<int>(idx
), static_cast<int>(dim
), val
);
2824 catch (const std::exception
&e
)
2826 handle
->ERROR_MESSAGE("%s", e
.what());
2830 handle
->ERROR_MESSAGE("Unknown exception thrown");
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
)
2859 if ( 0 == extHandle
)
2864 GEOSContextHandleInternal_t
*handle
= 0;
2865 handle
= reinterpret_cast<GEOSContextHandleInternal_t
*>(extHandle
);
2866 if ( 0 == handle
->initialized
)
2875 catch (const std::exception
&e
)
2877 handle
->ERROR_MESSAGE("%s", e
.what());
2881 handle
->ERROR_MESSAGE("Unknown exception thrown");
2888 GEOSCoordSeq_getOrdinate_r(GEOSContextHandle_t extHandle
, const CoordinateSequence
*cs
,
2889 unsigned int idx
, unsigned int dim
, double *val
)
2894 if ( 0 == extHandle
)
2899 GEOSContextHandleInternal_t
*handle
= 0;
2900 handle
= reinterpret_cast<GEOSContextHandleInternal_t
*>(extHandle
);
2901 if ( 0 == handle
->initialized
)
2908 double d
= cs
->getOrdinate(static_cast<int>(idx
), static_cast<int>(dim
));
2913 catch (const std::exception
&e
)
2915 handle
->ERROR_MESSAGE("%s", e
.what());
2919 handle
->ERROR_MESSAGE("Unknown exception thrown");
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
)
2949 if ( 0 == extHandle
)
2954 GEOSContextHandleInternal_t
*handle
= 0;
2955 handle
= reinterpret_cast<GEOSContextHandleInternal_t
*>(extHandle
);
2956 if ( 0 == handle
->initialized
)
2963 const std::size_t sz
= cs
->getSize();
2964 *size
= static_cast<unsigned int>(sz
);
2967 catch (const std::exception
&e
)
2969 handle
->ERROR_MESSAGE("%s", e
.what());
2973 handle
->ERROR_MESSAGE("Unknown exception thrown");
2980 GEOSCoordSeq_getDimensions_r(GEOSContextHandle_t extHandle
, const CoordinateSequence
*cs
, unsigned int *dims
)
2985 if ( 0 == extHandle
)
2990 GEOSContextHandleInternal_t
*handle
= 0;
2991 handle
= reinterpret_cast<GEOSContextHandleInternal_t
*>(extHandle
);
2992 if ( 0 == handle
->initialized
)
2999 const std::size_t dim
= cs
->getDimension();
3000 *dims
= static_cast<unsigned int>(dim
);
3004 catch (const std::exception
&e
)
3006 handle
->ERROR_MESSAGE("%s", e
.what());
3011 handle
->ERROR_MESSAGE("Unknown exception thrown");
3018 GEOSCoordSeq_destroy_r(GEOSContextHandle_t extHandle
, CoordinateSequence
*s
)
3020 GEOSContextHandleInternal_t
*handle
= 0;
3026 catch (const std::exception
&e
)
3028 if ( 0 == extHandle
)
3033 handle
= reinterpret_cast<GEOSContextHandleInternal_t
*>(extHandle
);
3034 if ( 0 == handle
->initialized
)
3039 handle
->ERROR_MESSAGE("%s", e
.what());
3043 if ( 0 == extHandle
)
3048 handle
= reinterpret_cast<GEOSContextHandleInternal_t
*>(extHandle
);
3049 if ( 0 == handle
->initialized
)
3054 handle
->ERROR_MESSAGE("Unknown exception thrown");
3058 const CoordinateSequence
*
3059 GEOSGeom_getCoordSeq_r(GEOSContextHandle_t extHandle
, const Geometry
*g
)
3061 if ( 0 == extHandle
)
3066 GEOSContextHandleInternal_t
*handle
= 0;
3067 handle
= reinterpret_cast<GEOSContextHandleInternal_t
*>(extHandle
);
3068 if ( 0 == handle
->initialized
)
3075 using geos::geom::Point
;
3077 const LineString
*ls
= dynamic_cast<const LineString
*>(g
);
3080 return ls
->getCoordinatesRO();
3083 const Point
*p
= dynamic_cast<const Point
*>(g
);
3086 return p
->getCoordinatesRO();
3089 handle
->ERROR_MESSAGE("Geometry must be a Point or LineString");
3092 catch (const std::exception
&e
)
3094 handle
->ERROR_MESSAGE("%s", e
.what());
3098 handle
->ERROR_MESSAGE("Unknown exception thrown");
3105 GEOSGeom_createEmptyPoint_r(GEOSContextHandle_t extHandle
)
3107 if ( 0 == extHandle
)
3112 GEOSContextHandleInternal_t
*handle
= 0;
3113 handle
= reinterpret_cast<GEOSContextHandleInternal_t
*>(extHandle
);
3114 if ( 0 == handle
->initialized
)
3121 const GeometryFactory
*gf
= handle
->geomFactory
;
3122 return gf
->createPoint();
3124 catch (const std::exception
&e
)
3126 handle
->ERROR_MESSAGE("%s", e
.what());
3130 handle
->ERROR_MESSAGE("Unknown exception thrown");
3137 GEOSGeom_createPoint_r(GEOSContextHandle_t extHandle
, CoordinateSequence
*cs
)
3139 if ( 0 == extHandle
)
3144 GEOSContextHandleInternal_t
*handle
= 0;
3145 handle
= reinterpret_cast<GEOSContextHandleInternal_t
*>(extHandle
);
3146 if ( 0 == handle
->initialized
)
3153 const GeometryFactory
*gf
= handle
->geomFactory
;
3154 return gf
->createPoint(cs
);
3156 catch (const std::exception
&e
)
3158 handle
->ERROR_MESSAGE("%s", e
.what());
3162 handle
->ERROR_MESSAGE("Unknown exception thrown");
3169 GEOSGeom_createLinearRing_r(GEOSContextHandle_t extHandle
, CoordinateSequence
*cs
)
3171 if ( 0 == extHandle
)
3176 GEOSContextHandleInternal_t
*handle
= 0;
3177 handle
= reinterpret_cast<GEOSContextHandleInternal_t
*>(extHandle
);
3178 if ( 0 == handle
->initialized
)
3185 const GeometryFactory
*gf
= handle
->geomFactory
;
3187 return gf
->createLinearRing(cs
);
3189 catch (const std::exception
&e
)
3191 handle
->ERROR_MESSAGE("%s", e
.what());
3195 handle
->ERROR_MESSAGE("Unknown exception thrown");
3202 GEOSGeom_createEmptyLineString_r(GEOSContextHandle_t extHandle
)
3204 if ( 0 == extHandle
)
3209 GEOSContextHandleInternal_t
*handle
= 0;
3210 handle
= reinterpret_cast<GEOSContextHandleInternal_t
*>(extHandle
);
3211 if ( 0 == handle
->initialized
)
3218 const GeometryFactory
*gf
= handle
->geomFactory
;
3220 return gf
->createLineString();
3222 catch (const std::exception
&e
)
3224 handle
->ERROR_MESSAGE("%s", e
.what());
3228 handle
->ERROR_MESSAGE("Unknown exception thrown");
3235 GEOSGeom_createLineString_r(GEOSContextHandle_t extHandle
, CoordinateSequence
*cs
)
3237 if ( 0 == extHandle
)
3242 GEOSContextHandleInternal_t
*handle
= 0;
3243 handle
= reinterpret_cast<GEOSContextHandleInternal_t
*>(extHandle
);
3244 if ( 0 == handle
->initialized
)
3251 const GeometryFactory
*gf
= handle
->geomFactory
;
3253 return gf
->createLineString(cs
);
3255 catch (const std::exception
&e
)
3257 handle
->ERROR_MESSAGE("%s", e
.what());
3261 handle
->ERROR_MESSAGE("Unknown exception thrown");
3268 GEOSGeom_createEmptyPolygon_r(GEOSContextHandle_t extHandle
)
3270 if ( 0 == extHandle
)
3275 GEOSContextHandleInternal_t
*handle
= 0;
3276 handle
= reinterpret_cast<GEOSContextHandleInternal_t
*>(extHandle
);
3277 if ( 0 == handle
->initialized
)
3284 const GeometryFactory
*gf
= handle
->geomFactory
;
3285 return gf
->createPolygon();
3287 catch (const std::exception
&e
)
3289 handle
->ERROR_MESSAGE("%s", e
.what());
3293 handle
->ERROR_MESSAGE("Unknown exception thrown");
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
)
3310 GEOSContextHandleInternal_t
*handle
= 0;
3311 handle
= reinterpret_cast<GEOSContextHandleInternal_t
*>(extHandle
);
3312 if ( 0 == handle
->initialized
)
3319 using geos::geom::LinearRing
;
3321 std::vector
<Geometry
*> *vholes
= new std::vector
<Geometry
*>(holes
, holes
+ nholes
);
3323 LinearRing
*nshell
= dynamic_cast<LinearRing
*>(shell
);
3326 handle
->ERROR_MESSAGE("Shell is not a LinearRing");
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());
3339 handle
->ERROR_MESSAGE("Unknown exception thrown");
3346 GEOSGeom_clone_r(GEOSContextHandle_t extHandle
, const Geometry
*g
)
3350 if ( 0 == extHandle
)
3355 GEOSContextHandleInternal_t
*handle
= 0;
3356 handle
= reinterpret_cast<GEOSContextHandleInternal_t
*>(extHandle
);
3357 if ( 0 == handle
->initialized
)
3366 catch (const std::exception
&e
)
3368 handle
->ERROR_MESSAGE("%s", e
.what());
3372 handle
->ERROR_MESSAGE("Unknown exception thrown");
3379 GEOSGeom_getDimensions_r(GEOSContextHandle_t extHandle
, const Geometry
*g
)
3381 if ( 0 == extHandle
)
3386 GEOSContextHandleInternal_t
*handle
= 0;
3387 handle
= reinterpret_cast<GEOSContextHandleInternal_t
*>(extHandle
);
3388 if ( 0 == handle
->initialized
)
3395 using geos::geom::Point
;
3396 using geos::geom::GeometryCollection
;
3403 std::size_t dim
= 0;
3404 const LineString
*ls
= dynamic_cast<const LineString
*>(g
);
3407 dim
= ls
->getCoordinatesRO()->getDimension();
3408 return static_cast<int>(dim
);
3411 const Point
*p
= dynamic_cast<const Point
*>(g
);
3414 dim
= p
->getCoordinatesRO()->getDimension();
3415 return static_cast<int>(dim
);
3418 const Polygon
*poly
= dynamic_cast<const Polygon
*>(g
);
3421 return GEOSGeom_getDimensions_r(extHandle
, poly
->getExteriorRing());
3424 const GeometryCollection
*coll
= dynamic_cast<const GeometryCollection
*>(g
);
3427 return GEOSGeom_getDimensions_r(extHandle
, coll
->getGeometryN(0));
3430 handle
->ERROR_MESSAGE("Unknown geometry type");
3433 catch (const std::exception
&e
)
3435 handle
->ERROR_MESSAGE("%s", e
.what());
3439 handle
->ERROR_MESSAGE("Unknown exception thrown");
3446 GEOSSimplify_r(GEOSContextHandle_t extHandle
, const Geometry
*g1
, double tolerance
)
3448 if ( 0 == extHandle
)
3453 GEOSContextHandleInternal_t
*handle
= 0;
3454 handle
= reinterpret_cast<GEOSContextHandleInternal_t
*>(extHandle
);
3455 if ( 0 == handle
->initialized
)
3462 using namespace geos::simplify
;
3463 Geometry::AutoPtr
g(DouglasPeuckerSimplifier::simplify(g1
, tolerance
));
3466 catch (const std::exception
&e
)
3468 handle
->ERROR_MESSAGE("%s", e
.what());
3472 handle
->ERROR_MESSAGE("Unknown exception thrown");
3479 GEOSTopologyPreserveSimplify_r(GEOSContextHandle_t extHandle
, const Geometry
*g1
, double tolerance
)
3481 if ( 0 == extHandle
)
3486 GEOSContextHandleInternal_t
*handle
= 0;
3487 handle
= reinterpret_cast<GEOSContextHandleInternal_t
*>(extHandle
);
3488 if ( 0 == handle
->initialized
)
3495 using namespace geos::simplify
;
3496 Geometry::AutoPtr
g(TopologyPreservingSimplifier::simplify(g1
, tolerance
));
3499 catch (const std::exception
&e
)
3501 handle
->ERROR_MESSAGE("%s", e
.what());
3505 handle
->ERROR_MESSAGE("Unknown exception thrown");
3514 GEOSWKTReader_create_r(GEOSContextHandle_t extHandle
)
3516 if ( 0 == extHandle
)
3521 GEOSContextHandleInternal_t
*handle
= 0;
3522 handle
= reinterpret_cast<GEOSContextHandleInternal_t
*>(extHandle
);
3523 if ( 0 == handle
->initialized
)
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());
3539 handle
->ERROR_MESSAGE("Unknown exception thrown");
3546 GEOSWKTReader_destroy_r(GEOSContextHandle_t extHandle
, WKTReader
*reader
)
3548 GEOSContextHandleInternal_t
*handle
= 0;
3554 catch (const std::exception
&e
)
3556 if ( 0 == extHandle
)
3561 handle
= reinterpret_cast<GEOSContextHandleInternal_t
*>(extHandle
);
3562 if ( 0 == handle
->initialized
)
3567 handle
->ERROR_MESSAGE("%s", e
.what());
3571 if ( 0 == extHandle
)
3576 handle
= reinterpret_cast<GEOSContextHandleInternal_t
*>(extHandle
);
3577 if ( 0 == handle
->initialized
)
3582 handle
->ERROR_MESSAGE("Unknown exception thrown");
3588 GEOSWKTReader_read_r(GEOSContextHandle_t extHandle
, WKTReader
*reader
, const char *wkt
)
3590 assert(0 != reader
);
3592 if ( 0 == extHandle
)
3597 GEOSContextHandleInternal_t
*handle
= 0;
3598 handle
= reinterpret_cast<GEOSContextHandleInternal_t
*>(extHandle
);
3599 if ( 0 == handle
->initialized
)
3606 const std::string
wktstring(wkt
);
3607 Geometry
*g
= reader
->read(wktstring
);
3610 catch (const std::exception
&e
)
3612 handle
->ERROR_MESSAGE("%s", e
.what());
3616 handle
->ERROR_MESSAGE("Unknown exception thrown");
3624 GEOSWKTWriter_create_r(GEOSContextHandle_t extHandle
)
3626 if ( 0 == extHandle
)
3631 GEOSContextHandleInternal_t
*handle
= 0;
3632 handle
= reinterpret_cast<GEOSContextHandleInternal_t
*>(extHandle
);
3633 if ( 0 == handle
->initialized
)
3640 using geos::io::WKTWriter
;
3641 return new WKTWriter();
3643 catch (const std::exception
&e
)
3645 handle
->ERROR_MESSAGE("%s", e
.what());
3649 handle
->ERROR_MESSAGE("Unknown exception thrown");
3656 GEOSWKTWriter_destroy_r(GEOSContextHandle_t extHandle
, WKTWriter
*Writer
)
3659 GEOSContextHandleInternal_t
*handle
= 0;
3665 catch (const std::exception
&e
)
3667 if ( 0 == extHandle
)
3672 handle
= reinterpret_cast<GEOSContextHandleInternal_t
*>(extHandle
);
3673 if ( 0 == handle
->initialized
)
3678 handle
->ERROR_MESSAGE("%s", e
.what());
3682 if ( 0 == extHandle
)
3687 handle
= reinterpret_cast<GEOSContextHandleInternal_t
*>(extHandle
);
3688 if ( 0 == handle
->initialized
)
3693 handle
->ERROR_MESSAGE("Unknown exception thrown");
3699 GEOSWKTWriter_write_r(GEOSContextHandle_t extHandle
, WKTWriter
*writer
, const Geometry
*geom
)
3701 assert(0 != writer
);
3703 if ( 0 == extHandle
)
3708 GEOSContextHandleInternal_t
*handle
= 0;
3709 handle
= reinterpret_cast<GEOSContextHandleInternal_t
*>(extHandle
);
3710 if ( 0 == handle
->initialized
)
3717 std::string
sgeom(writer
->write(geom
));
3718 char *result
= gstrdup(sgeom
);
3721 catch (const std::exception
&e
)
3723 handle
->ERROR_MESSAGE("%s", e
.what());
3727 handle
->ERROR_MESSAGE("Unknown exception thrown");
3735 GEOSWKBReader_create_r(GEOSContextHandle_t extHandle
)
3737 if ( 0 == extHandle
)
3742 GEOSContextHandleInternal_t
*handle
= 0;
3743 handle
= reinterpret_cast<GEOSContextHandleInternal_t
*>(extHandle
);
3744 if ( 0 == handle
->initialized
)
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());
3760 handle
->ERROR_MESSAGE("Unknown exception thrown");
3767 GEOSWKBReader_destroy_r(GEOSContextHandle_t extHandle
, WKBReader
*reader
)
3769 GEOSContextHandleInternal_t
*handle
= 0;
3775 catch (const std::exception
&e
)
3777 if ( 0 == extHandle
)
3782 handle
= reinterpret_cast<GEOSContextHandleInternal_t
*>(extHandle
);
3783 if ( 0 == handle
->initialized
)
3788 handle
->ERROR_MESSAGE("%s", e
.what());
3792 if ( 0 == extHandle
)
3797 handle
= reinterpret_cast<GEOSContextHandleInternal_t
*>(extHandle
);
3798 if ( 0 == handle
->initialized
)
3803 handle
->ERROR_MESSAGE("Unknown exception thrown");
3809 GEOSWKBReader_read_r(GEOSContextHandle_t extHandle
, WKBReader
*reader
, const unsigned char *wkb
, size_t size
)
3811 assert(0 != reader
);
3814 if ( 0 == extHandle
)
3819 GEOSContextHandleInternal_t
*handle
= 0;
3820 handle
= reinterpret_cast<GEOSContextHandleInternal_t
*>(extHandle
);
3821 if ( 0 == handle
->initialized
)
3828 std::string
wkbstring(reinterpret_cast<const char*>(wkb
), size
); // make it binary !
3829 std::istringstream
is(std::ios_base::binary
);
3831 is
.seekg(0, std::ios::beg
); // rewind reader pointer
3833 Geometry
*g
= reader
->read(is
);
3836 catch (const std::exception
&e
)
3838 handle
->ERROR_MESSAGE("%s", e
.what());
3842 handle
->ERROR_MESSAGE("Unknown exception thrown");
3849 GEOSWKBReader_readHEX_r(GEOSContextHandle_t extHandle
, WKBReader
*reader
, const unsigned char *hex
, size_t size
)
3851 assert(0 != reader
);
3854 if ( 0 == extHandle
)
3859 GEOSContextHandleInternal_t
*handle
= 0;
3860 handle
= reinterpret_cast<GEOSContextHandleInternal_t
*>(extHandle
);
3861 if ( 0 == handle
->initialized
)
3868 std::string
hexstring(reinterpret_cast<const char*>(hex
), size
);
3869 std::istringstream
is(std::ios_base::binary
);
3871 is
.seekg(0, std::ios::beg
); // rewind reader pointer
3873 Geometry
*g
= reader
->readHEX(is
);
3876 catch (const std::exception
&e
)
3878 handle
->ERROR_MESSAGE("%s", e
.what());
3882 handle
->ERROR_MESSAGE("Unknown exception thrown");
3890 GEOSWKBWriter_create_r(GEOSContextHandle_t extHandle
)
3892 if ( 0 == extHandle
)
3897 GEOSContextHandleInternal_t
*handle
= 0;
3898 handle
= reinterpret_cast<GEOSContextHandleInternal_t
*>(extHandle
);
3899 if ( 0 == handle
->initialized
)
3906 using geos::io::WKBWriter
;
3907 return new WKBWriter();
3909 catch (const std::exception
&e
)
3911 handle
->ERROR_MESSAGE("%s", e
.what());
3915 handle
->ERROR_MESSAGE("Unknown exception thrown");
3922 GEOSWKBWriter_destroy_r(GEOSContextHandle_t extHandle
, WKBWriter
*Writer
)
3924 GEOSContextHandleInternal_t
*handle
= 0;
3930 catch (const std::exception
&e
)
3932 if ( 0 == extHandle
)
3937 handle
= reinterpret_cast<GEOSContextHandleInternal_t
*>(extHandle
);
3938 if ( 0 == handle
->initialized
)
3943 handle
->ERROR_MESSAGE("%s", e
.what());
3947 if ( 0 == extHandle
)
3952 handle
= reinterpret_cast<GEOSContextHandleInternal_t
*>(extHandle
);
3953 if ( 0 == handle
->initialized
)
3958 handle
->ERROR_MESSAGE("Unknown exception thrown");
3963 /* The caller owns the result */
3965 GEOSWKBWriter_write_r(GEOSContextHandle_t extHandle
, WKBWriter
*writer
, const Geometry
*geom
, size_t *size
)
3967 assert(0 != writer
);
3971 if ( 0 == extHandle
)
3976 GEOSContextHandleInternal_t
*handle
= 0;
3977 handle
= reinterpret_cast<GEOSContextHandleInternal_t
*>(extHandle
);
3978 if ( 0 == handle
->initialized
)
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
);
3996 catch (const std::exception
&e
)
3998 handle
->ERROR_MESSAGE("%s", e
.what());
4002 handle
->ERROR_MESSAGE("Unknown exception thrown");
4007 /* The caller owns the result */
4009 GEOSWKBWriter_writeHEX_r(GEOSContextHandle_t extHandle
, WKBWriter
*writer
, const Geometry
*geom
, size_t *size
)
4011 assert(0 != writer
);
4015 if ( 0 == extHandle
)
4020 GEOSContextHandleInternal_t
*handle
= 0;
4021 handle
= reinterpret_cast<GEOSContextHandleInternal_t
*>(extHandle
);
4022 if ( 0 == handle
->initialized
)
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
);
4040 catch (const std::exception
&e
)
4042 handle
->ERROR_MESSAGE("%s", e
.what());
4046 handle
->ERROR_MESSAGE("Unknown exception thrown");
4053 GEOSWKBWriter_getOutputDimension_r(GEOSContextHandle_t extHandle
, const GEOSWKBWriter
* writer
)
4055 assert(0 != writer
);
4057 if ( 0 == extHandle
)
4064 GEOSContextHandleInternal_t
*handle
= 0;
4065 handle
= reinterpret_cast<GEOSContextHandleInternal_t
*>(extHandle
);
4066 if ( 0 != handle
->initialized
)
4070 ret
= writer
->getOutputDimension();
4074 handle
->ERROR_MESSAGE("Unknown exception thrown");
4082 GEOSWKBWriter_setOutputDimension_r(GEOSContextHandle_t extHandle
, GEOSWKBWriter
* writer
, int newDimension
)
4084 assert(0 != writer
);
4086 if ( 0 == extHandle
)
4091 GEOSContextHandleInternal_t
*handle
= 0;
4092 handle
= reinterpret_cast<GEOSContextHandleInternal_t
*>(extHandle
);
4093 if ( 0 != handle
->initialized
)
4097 writer
->setOutputDimension(newDimension
);
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
)
4118 GEOSContextHandleInternal_t
*handle
= 0;
4119 handle
= reinterpret_cast<GEOSContextHandleInternal_t
*>(extHandle
);
4120 if ( 0 != handle
->initialized
)
4124 ret
= writer
->getByteOrder();
4129 handle
->ERROR_MESSAGE("Unknown exception thrown");
4137 GEOSWKBWriter_setByteOrder_r(GEOSContextHandle_t extHandle
, GEOSWKBWriter
* writer
, int newByteOrder
)
4139 assert(0 != writer
);
4141 if ( 0 == extHandle
)
4146 GEOSContextHandleInternal_t
*handle
= 0;
4147 handle
= reinterpret_cast<GEOSContextHandleInternal_t
*>(extHandle
);
4148 if ( 0 != handle
->initialized
)
4152 writer
->setByteOrder(newByteOrder
);
4156 handle
->ERROR_MESSAGE("Unknown exception thrown");
4162 GEOSWKBWriter_getIncludeSRID_r(GEOSContextHandle_t extHandle
, const GEOSWKBWriter
* writer
)
4164 assert(0 != writer
);
4166 if ( 0 == extHandle
)
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
);
4184 handle
->ERROR_MESSAGE("Unknown exception thrown");
4188 return static_cast<char>(ret
);
4192 GEOSWKBWriter_setIncludeSRID_r(GEOSContextHandle_t extHandle
, GEOSWKBWriter
* writer
, const char newIncludeSRID
)
4194 assert(0 != writer
);
4196 if ( 0 == extHandle
)
4201 GEOSContextHandleInternal_t
*handle
= 0;
4202 handle
= reinterpret_cast<GEOSContextHandleInternal_t
*>(extHandle
);
4203 if ( 0 != handle
->initialized
)
4207 writer
->setIncludeSRID(newIncludeSRID
);
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
)
4229 GEOSContextHandleInternal_t
*handle
= 0;
4230 handle
= reinterpret_cast<GEOSContextHandleInternal_t
*>(extHandle
);
4231 if ( 0 == handle
->initialized
)
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());
4248 handle
->ERROR_MESSAGE("Unknown exception thrown");
4255 GEOSPreparedGeom_destroy_r(GEOSContextHandle_t extHandle
, const geos::geom::prep::PreparedGeometry
*a
)
4257 GEOSContextHandleInternal_t
*handle
= 0;
4263 catch (const std::exception
&e
)
4265 if ( 0 == extHandle
)
4270 handle
= reinterpret_cast<GEOSContextHandleInternal_t
*>(extHandle
);
4271 if ( 0 == handle
->initialized
)
4276 handle
->ERROR_MESSAGE("%s", e
.what());
4280 if ( 0 == extHandle
)
4285 handle
= reinterpret_cast<GEOSContextHandleInternal_t
*>(extHandle
);
4286 if ( 0 == handle
->initialized
)
4291 handle
->ERROR_MESSAGE("Unknown exception thrown");
4296 GEOSPreparedContains_r(GEOSContextHandle_t extHandle
,
4297 const geos::geom::prep::PreparedGeometry
*pg
, const Geometry
*g
)
4302 if ( 0 == extHandle
)
4307 GEOSContextHandleInternal_t
*handle
= 0;
4308 handle
= reinterpret_cast<GEOSContextHandleInternal_t
*>(extHandle
);
4309 if ( 0 == handle
->initialized
)
4316 bool result
= pg
->contains(g
);
4319 catch (const std::exception
&e
)
4321 handle
->ERROR_MESSAGE("%s", e
.what());
4325 handle
->ERROR_MESSAGE("Unknown exception thrown");
4332 GEOSPreparedContainsProperly_r(GEOSContextHandle_t extHandle
,
4333 const geos::geom::prep::PreparedGeometry
*pg
, const Geometry
*g
)
4338 if ( 0 == extHandle
)
4343 GEOSContextHandleInternal_t
*handle
= 0;
4344 handle
= reinterpret_cast<GEOSContextHandleInternal_t
*>(extHandle
);
4345 if ( 0 == handle
->initialized
)
4352 bool result
= pg
->containsProperly(g
);
4355 catch (const std::exception
&e
)
4357 handle
->ERROR_MESSAGE("%s", e
.what());
4361 handle
->ERROR_MESSAGE("Unknown exception thrown");
4368 GEOSPreparedCovers_r(GEOSContextHandle_t extHandle
,
4369 const geos::geom::prep::PreparedGeometry
*pg
, const Geometry
*g
)
4374 if ( 0 == extHandle
)
4379 GEOSContextHandleInternal_t
*handle
= 0;
4380 handle
= reinterpret_cast<GEOSContextHandleInternal_t
*>(extHandle
);
4381 if ( 0 == handle
->initialized
)
4388 bool result
= pg
->covers(g
);
4391 catch (const std::exception
&e
)
4393 handle
->ERROR_MESSAGE("%s", e
.what());
4397 handle
->ERROR_MESSAGE("Unknown exception thrown");
4404 GEOSPreparedIntersects_r(GEOSContextHandle_t extHandle
,
4405 const geos::geom::prep::PreparedGeometry
*pg
, const Geometry
*g
)
4410 if ( 0 == extHandle
)
4415 GEOSContextHandleInternal_t
*handle
= 0;
4416 handle
= reinterpret_cast<GEOSContextHandleInternal_t
*>(extHandle
);
4417 if ( 0 == handle
->initialized
)
4424 bool result
= pg
->intersects(g
);
4427 catch (const std::exception
&e
)
4429 handle
->ERROR_MESSAGE("%s", e
.what());
4433 handle
->ERROR_MESSAGE("Unknown exception thrown");
4439 //-----------------------------------------------------------------
4441 //-----------------------------------------------------------------
4443 geos::index::strtree::STRtree
*
4444 GEOSSTRtree_create_r(GEOSContextHandle_t extHandle
,
4445 size_t nodeCapacity
)
4447 if ( 0 == extHandle
)
4452 GEOSContextHandleInternal_t
*handle
= 0;
4453 handle
= reinterpret_cast<GEOSContextHandleInternal_t
*>(extHandle
);
4454 if ( 0 == handle
->initialized
)
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());
4471 handle
->ERROR_MESSAGE("Unknown exception thrown");
4478 GEOSSTRtree_insert_r(GEOSContextHandle_t extHandle
,
4479 geos::index::strtree::STRtree
*tree
,
4480 const geos::geom::Geometry
*g
,
4483 GEOSContextHandleInternal_t
*handle
= 0;
4489 tree
->insert(g
->getEnvelopeInternal(), item
);
4491 catch (const std::exception
&e
)
4493 if ( 0 == extHandle
)
4498 handle
= reinterpret_cast<GEOSContextHandleInternal_t
*>(extHandle
);
4499 if ( 0 == handle
->initialized
)
4504 handle
->ERROR_MESSAGE("%s", e
.what());
4508 if ( 0 == extHandle
)
4513 handle
= reinterpret_cast<GEOSContextHandleInternal_t
*>(extHandle
);
4514 if ( 0 == handle
->initialized
)
4519 handle
->ERROR_MESSAGE("Unknown exception thrown");
4524 GEOSSTRtree_query_r(GEOSContextHandle_t extHandle
,
4525 geos::index::strtree::STRtree
*tree
,
4526 const geos::geom::Geometry
*g
,
4527 GEOSQueryCallback callback
,
4530 GEOSContextHandleInternal_t
*handle
= 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
)
4547 handle
= reinterpret_cast<GEOSContextHandleInternal_t
*>(extHandle
);
4548 if ( 0 == handle
->initialized
)
4553 handle
->ERROR_MESSAGE("%s", e
.what());
4557 if ( 0 == extHandle
)
4562 handle
= reinterpret_cast<GEOSContextHandleInternal_t
*>(extHandle
);
4563 if ( 0 == handle
->initialized
)
4568 handle
->ERROR_MESSAGE("Unknown exception thrown");
4573 GEOSSTRtree_iterate_r(GEOSContextHandle_t extHandle
,
4574 geos::index::strtree::STRtree
*tree
,
4575 GEOSQueryCallback callback
,
4578 GEOSContextHandleInternal_t
*handle
= 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
)
4594 handle
= reinterpret_cast<GEOSContextHandleInternal_t
*>(extHandle
);
4595 if ( 0 == handle
->initialized
)
4600 handle
->ERROR_MESSAGE("%s", e
.what());
4604 if ( 0 == extHandle
)
4609 handle
= reinterpret_cast<GEOSContextHandleInternal_t
*>(extHandle
);
4610 if ( 0 == handle
->initialized
)
4615 handle
->ERROR_MESSAGE("Unknown exception thrown");
4620 GEOSSTRtree_remove_r(GEOSContextHandle_t extHandle
,
4621 geos::index::strtree::STRtree
*tree
,
4622 const geos::geom::Geometry
*g
,
4628 if ( 0 == extHandle
)
4633 GEOSContextHandleInternal_t
*handle
= 0;
4634 handle
= reinterpret_cast<GEOSContextHandleInternal_t
*>(extHandle
);
4635 if ( 0 == handle
->initialized
)
4642 bool result
= tree
->remove(g
->getEnvelopeInternal(), item
);
4645 catch (const std::exception
&e
)
4647 handle
->ERROR_MESSAGE("%s", e
.what());
4651 handle
->ERROR_MESSAGE("Unknown exception thrown");
4658 GEOSSTRtree_destroy_r(GEOSContextHandle_t extHandle
,
4659 geos::index::strtree::STRtree
*tree
)
4661 GEOSContextHandleInternal_t
*handle
= 0;
4667 catch (const std::exception
&e
)
4669 if ( 0 == extHandle
)
4674 handle
= reinterpret_cast<GEOSContextHandleInternal_t
*>(extHandle
);
4675 if ( 0 == handle
->initialized
)
4680 handle
->ERROR_MESSAGE("%s", e
.what());
4684 if ( 0 == extHandle
)
4689 handle
= reinterpret_cast<GEOSContextHandleInternal_t
*>(extHandle
);
4690 if ( 0 == handle
->initialized
)
4695 handle
->ERROR_MESSAGE("Unknown exception thrown");
4700 GEOSProject_r(GEOSContextHandle_t extHandle
,
4705 const geos::geom::Point
* point
= dynamic_cast<const geos::geom::Point
*>(p
);
4707 if ( 0 == extHandle
)
4711 GEOSContextHandleInternal_t
*handle
= reinterpret_cast<GEOSContextHandleInternal_t
*>(extHandle
);
4712 if ( 0 == handle
->initialized
)
4717 handle
->ERROR_MESSAGE("third argument of GEOSProject_r must be Point*");
4720 const geos::geom::Coordinate
* inputPt
= p
->getCoordinate();
4721 return geos::linearref::LengthIndexedLine(g
).project(*inputPt
);
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
);
4738 GEOSProjectNormalized_r(GEOSContextHandle_t extHandle
, const Geometry
*g
,
4743 GEOSLength_r(extHandle
, g
, &length
);
4744 return GEOSProject_r(extHandle
, g
, p
) / length
;
4749 GEOSInterpolateNormalized_r(GEOSContextHandle_t extHandle
, const Geometry
*g
,
4753 GEOSLength_r(extHandle
, g
, &length
);
4754 return GEOSInterpolate_r(extHandle
, g
, d
* length
);
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(),
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());
4800 handle
->ERROR_MESSAGE("Unknown exception thrown");
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
)
4818 handle
= reinterpret_cast<GEOSContextHandleInternal_t
*>(extHandle
);
4819 if ( 0 == handle
->initialized
)
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());
4838 handle
->ERROR_MESSAGE("Unknown exception thrown");