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/IntersectionMatrix.h>
36 #include <geos/geom/Envelope.h>
37 #include <geos/index/strtree/STRtree.h>
38 #include <geos/index/ItemVisitor.h>
39 #include <geos/io/WKTReader.h>
40 #include <geos/io/WKBReader.h>
41 #include <geos/io/WKTWriter.h>
42 #include <geos/io/WKBWriter.h>
43 #include <geos/algorithm/distance/DiscreteHausdorffDistance.h>
44 #include <geos/simplify/DouglasPeuckerSimplifier.h>
45 #include <geos/simplify/TopologyPreservingSimplifier.h>
46 #include <geos/operation/valid/IsValidOp.h>
47 #include <geos/operation/polygonize/Polygonizer.h>
48 #include <geos/operation/linemerge/LineMerger.h>
49 #include <geos/operation/overlay/OverlayOp.h>
50 #include <geos/operation/union/CascadedPolygonUnion.h>
51 #include <geos/operation/buffer/BufferOp.h>
52 #include <geos/operation/buffer/BufferParameters.h>
53 #include <geos/operation/buffer/BufferBuilder.h>
54 #include <geos/linearref/LengthIndexedLine.h>
55 #include <geos/geom/BinaryOp.h>
56 #include <geos/util/IllegalArgumentException.h>
57 #include <geos/util/UniqueCoordinateArrayFilter.h>
58 #include <geos/util/Machine.h>
59 #include <geos/version.h>
61 // This should go away
62 #include <cmath> // finite
74 #pragma warning(disable : 4099)
77 // Some extra magic to make type declarations in geos_c.h work -
78 // for cross-checking of types in header.
79 #define GEOSGeometry geos::geom::Geometry
80 #define GEOSPreparedGeometry geos::geom::prep::PreparedGeometry
81 #define GEOSCoordSequence geos::geom::CoordinateSequence
82 #define GEOSSTRtree geos::index::strtree::STRtree
83 #define GEOSWKTReader_t geos::io::WKTReader
84 #define GEOSWKTWriter_t geos::io::WKTWriter
85 #define GEOSWKBReader_t geos::io::WKBReader
86 #define GEOSWKBWriter_t geos::io::WKBWriter
90 // Intentional, to allow non-standard C elements like C99 functions to be
91 // imported through C++ headers of C library, like <cmath>.
94 /// Define this if you want operations triggering Exceptions to
96 /// (will use the NOTIFY channel - only implemented for GEOSUnion so far)
98 #undef VERBOSE_EXCEPTIONS
100 #include <geos/export.h>
103 // import the most frequently used definitions globally
104 using geos::geom::Geometry
;
105 using geos::geom::LineString
;
106 using geos::geom::Polygon
;
107 using geos::geom::CoordinateSequence
;
108 using geos::geom::GeometryFactory
;
110 using geos::io::WKTReader
;
111 using geos::io::WKTWriter
;
112 using geos::io::WKBReader
;
113 using geos::io::WKBWriter
;
115 using geos::operation::overlay::OverlayOp
;
116 using geos::operation::overlay::overlayOp
;
117 using geos::operation::geounion::CascadedPolygonUnion
;
119 using geos::algorithm::distance::DiscreteHausdorffDistance
;
121 typedef std::auto_ptr
<Geometry
> GeomAutoPtr
;
123 typedef struct GEOSContextHandleInternal
125 const GeometryFactory
*geomFactory
;
126 GEOSMessageHandler NOTICE_MESSAGE
;
127 GEOSMessageHandler ERROR_MESSAGE
;
131 } GEOSContextHandleInternal_t
;
133 // CAPI_ItemVisitor is used internally by the CAPI STRtree
134 // wrappers. It's defined here just to keep it out of the
136 class CAPI_ItemVisitor
: public geos::index::ItemVisitor
{
137 GEOSQueryCallback callback
;
140 CAPI_ItemVisitor (GEOSQueryCallback cb
, void *ud
)
141 : ItemVisitor(), callback(cb
), userdata(ud
) {};
142 void visitItem (void *item
) { callback(item
, userdata
); };
146 //## PROTOTYPES #############################################
148 extern "C" const char GEOS_DLL
*GEOSjtsport();
149 extern "C" char GEOS_DLL
*GEOSasText(Geometry
*g1
);
153 char* gstrdup_s(const char* str
, const std::size_t size
)
155 char* out
= static_cast<char*>(std::malloc(size
+ 1));
158 // as no strlen call necessary, memcpy may be faster than strcpy
159 std::memcpy(out
, str
, size
+ 1);
166 char* gstrdup(std::string
const& str
)
168 return gstrdup_s(str
.c_str(), str
.size());
173 initGEOS_r(GEOSMessageHandler nf
, GEOSMessageHandler ef
)
175 GEOSContextHandleInternal_t
*handle
= 0;
178 extHandle
= std::malloc(sizeof(GEOSContextHandleInternal_t
));
181 handle
= static_cast<GEOSContextHandleInternal_t
*>(extHandle
);
182 handle
->NOTICE_MESSAGE
= nf
;
183 handle
->ERROR_MESSAGE
= ef
;
184 handle
->geomFactory
= GeometryFactory::getDefaultInstance();
185 handle
->WKBOutputDims
= 2;
186 handle
->WKBByteOrder
= getMachineByteOrder();
187 handle
->initialized
= 1;
190 return static_cast<GEOSContextHandle_t
>(extHandle
);
194 GEOSContext_setNoticeHandler_r(GEOSContextHandle_t extHandle
, GEOSMessageHandler nf
)
196 GEOSMessageHandler f
;
197 GEOSContextHandleInternal_t
*handle
= 0;
198 handle
= reinterpret_cast<GEOSContextHandleInternal_t
*>(extHandle
);
199 if ( 0 == handle
->initialized
)
204 f
= handle
->NOTICE_MESSAGE
;
205 handle
->NOTICE_MESSAGE
= nf
;
211 GEOSContext_setErrorHandler_r(GEOSContextHandle_t extHandle
, GEOSMessageHandler nf
)
213 GEOSMessageHandler f
;
214 GEOSContextHandleInternal_t
*handle
= 0;
215 handle
= reinterpret_cast<GEOSContextHandleInternal_t
*>(extHandle
);
216 if ( 0 == handle
->initialized
)
221 f
= handle
->ERROR_MESSAGE
;
222 handle
->ERROR_MESSAGE
= nf
;
228 finishGEOS_r(GEOSContextHandle_t extHandle
)
230 // Fix up freeing handle w.r.t. malloc above
231 std::free(extHandle
);
236 GEOSFree_r (GEOSContextHandle_t extHandle
, void* buffer
)
238 assert(0 != extHandle
);
243 //-----------------------------------------------------------
244 // relate()-related functions
245 // return 0 = false, 1 = true, 2 = error occured
246 //-----------------------------------------------------------
249 GEOSDisjoint_r(GEOSContextHandle_t extHandle
, const Geometry
*g1
, const Geometry
*g2
)
251 if ( 0 == extHandle
)
256 GEOSContextHandleInternal_t
*handle
= 0;
257 handle
= reinterpret_cast<GEOSContextHandleInternal_t
*>(extHandle
);
258 if ( handle
->initialized
== 0 )
265 bool result
= g1
->disjoint(g2
);
269 // TODO: mloskot is going to replace these double-catch block
270 // with a macro to remove redundant code in this and
271 // following functions.
272 catch (const std::exception
&e
)
274 handle
->ERROR_MESSAGE("%s", e
.what());
278 handle
->ERROR_MESSAGE("Unknown exception thrown");
285 GEOSTouches_r(GEOSContextHandle_t extHandle
, const Geometry
*g1
, const Geometry
*g2
)
287 if ( 0 == extHandle
)
292 GEOSContextHandleInternal_t
*handle
= 0;
293 handle
= reinterpret_cast<GEOSContextHandleInternal_t
*>(extHandle
);
294 if ( 0 == handle
->initialized
)
301 bool result
= g1
->touches(g2
);
304 catch (const std::exception
&e
)
306 handle
->ERROR_MESSAGE("%s", e
.what());
310 handle
->ERROR_MESSAGE("Unknown exception thrown");
317 GEOSIntersects_r(GEOSContextHandle_t extHandle
, const Geometry
*g1
, const Geometry
*g2
)
319 if ( 0 == extHandle
)
324 GEOSContextHandleInternal_t
*handle
= 0;
325 handle
= reinterpret_cast<GEOSContextHandleInternal_t
*>(extHandle
);
326 if ( 0 == handle
->initialized
)
333 bool result
= g1
->intersects(g2
);
336 catch (const std::exception
&e
)
338 handle
->ERROR_MESSAGE("%s", e
.what());
342 handle
->ERROR_MESSAGE("Unknown exception thrown");
349 GEOSCrosses_r(GEOSContextHandle_t extHandle
, const Geometry
*g1
, const Geometry
*g2
)
351 if ( 0 == extHandle
)
356 GEOSContextHandleInternal_t
*handle
= 0;
357 handle
= reinterpret_cast<GEOSContextHandleInternal_t
*>(extHandle
);
358 if ( 0 == handle
->initialized
)
365 bool result
= g1
->crosses(g2
);
368 catch (const std::exception
&e
)
370 handle
->ERROR_MESSAGE("%s", e
.what());
374 handle
->ERROR_MESSAGE("Unknown exception thrown");
381 GEOSWithin_r(GEOSContextHandle_t extHandle
, const Geometry
*g1
, const Geometry
*g2
)
383 if ( 0 == extHandle
)
388 GEOSContextHandleInternal_t
*handle
= 0;
389 handle
= reinterpret_cast<GEOSContextHandleInternal_t
*>(extHandle
);
390 if ( 0 == handle
->initialized
)
397 bool result
= g1
->within(g2
);
400 catch (const std::exception
&e
)
402 handle
->ERROR_MESSAGE("%s", e
.what());
406 handle
->ERROR_MESSAGE("Unknown exception thrown");
412 // call g1->contains(g2)
415 // 2 = error was trapped
417 GEOSContains_r(GEOSContextHandle_t extHandle
, const Geometry
*g1
, const Geometry
*g2
)
419 if ( 0 == extHandle
)
424 GEOSContextHandleInternal_t
*handle
= 0;
425 handle
= reinterpret_cast<GEOSContextHandleInternal_t
*>(extHandle
);
426 if ( 0 == handle
->initialized
)
433 bool result
= g1
->contains(g2
);
436 catch (const std::exception
&e
)
438 handle
->ERROR_MESSAGE("%s", e
.what());
442 handle
->ERROR_MESSAGE("Unknown exception thrown");
449 GEOSOverlaps_r(GEOSContextHandle_t extHandle
, const Geometry
*g1
, const Geometry
*g2
)
451 if ( 0 == extHandle
)
456 GEOSContextHandleInternal_t
*handle
= 0;
457 handle
= reinterpret_cast<GEOSContextHandleInternal_t
*>(extHandle
);
458 if ( 0 == handle
->initialized
)
465 bool result
= g1
->overlaps(g2
);
468 catch (const std::exception
&e
)
470 handle
->ERROR_MESSAGE("%s", e
.what());
474 handle
->ERROR_MESSAGE("Unknown exception thrown");
481 //-------------------------------------------------------------------
482 // low-level relate functions
483 //------------------------------------------------------------------
486 GEOSRelatePattern_r(GEOSContextHandle_t extHandle
, const Geometry
*g1
, const Geometry
*g2
, const char *pat
)
488 if ( 0 == extHandle
)
493 GEOSContextHandleInternal_t
*handle
= 0;
494 handle
= reinterpret_cast<GEOSContextHandleInternal_t
*>(extHandle
);
495 if ( 0 == handle
->initialized
)
503 bool result
= g1
->relate(g2
, s
);
506 catch (const std::exception
&e
)
508 handle
->ERROR_MESSAGE("%s", e
.what());
512 handle
->ERROR_MESSAGE("Unknown exception thrown");
519 GEOSRelate_r(GEOSContextHandle_t extHandle
, const Geometry
*g1
, const Geometry
*g2
)
521 if ( 0 == extHandle
)
526 GEOSContextHandleInternal_t
*handle
= 0;
527 handle
= reinterpret_cast<GEOSContextHandleInternal_t
*>(extHandle
);
528 if ( 0 == handle
->initialized
)
535 using geos::geom::IntersectionMatrix
;
537 IntersectionMatrix
* im
= g1
->relate(g2
);
543 char *result
= gstrdup(im
->toString());
550 catch (const std::exception
&e
)
552 handle
->ERROR_MESSAGE("%s", e
.what());
556 handle
->ERROR_MESSAGE("Unknown exception thrown");
564 //-----------------------------------------------------------------
566 //-----------------------------------------------------------------
570 GEOSisValid_r(GEOSContextHandle_t extHandle
, const Geometry
*g1
)
572 if ( 0 == extHandle
)
577 GEOSContextHandleInternal_t
*handle
= 0;
578 handle
= reinterpret_cast<GEOSContextHandleInternal_t
*>(extHandle
);
579 if ( 0 == handle
->initialized
)
586 using geos::operation::valid::IsValidOp
;
587 using geos::operation::valid::TopologyValidationError
;
590 bool valid
= ivo
.isValid();
593 TopologyValidationError
*err
= ivo
.getValidationError();
596 handle
->NOTICE_MESSAGE("%s", err
->toString().c_str());
601 catch (const std::exception
&e
)
603 handle
->ERROR_MESSAGE("%s", e
.what());
607 handle
->ERROR_MESSAGE("Unknown exception thrown");
614 GEOSisValidReason_r(GEOSContextHandle_t extHandle
, const Geometry
*g1
)
616 if ( 0 == extHandle
)
621 GEOSContextHandleInternal_t
*handle
= 0;
622 handle
= reinterpret_cast<GEOSContextHandleInternal_t
*>(extHandle
);
623 if ( 0 == handle
->initialized
)
630 using geos::operation::valid::IsValidOp
;
631 using geos::operation::valid::TopologyValidationError
;
634 char const* const validstr
= "Valid Geometry";
637 bool isvalid
= ivo
.isValid();
640 TopologyValidationError
*err
= ivo
.getValidationError();
643 const std::string errloc
= err
->getCoordinate().toString();
644 std::string
errmsg(err
->getMessage());
645 errmsg
+= "[" + errloc
+ "]";
646 result
= gstrdup(errmsg
);
651 result
= gstrdup(std::string(validstr
));
656 catch (const std::exception
&e
)
658 handle
->ERROR_MESSAGE("%s", e
.what());
662 handle
->ERROR_MESSAGE("Unknown exception thrown");
669 GEOSisValidDetail_r(GEOSContextHandle_t extHandle
, const Geometry
*g
,
670 char** reason
, const Geometry
** location
)
672 if ( 0 == extHandle
)
677 GEOSContextHandleInternal_t
*handle
= 0;
678 handle
= reinterpret_cast<GEOSContextHandleInternal_t
*>(extHandle
);
679 if ( 0 == handle
->initialized
)
686 using geos::operation::valid::IsValidOp
;
687 using geos::operation::valid::TopologyValidationError
;
690 bool isvalid
= ivo
.isValid();
693 TopologyValidationError
*err
= ivo
.getValidationError();
696 *location
= handle
->geomFactory
->createPoint(err
->getCoordinate());
697 std::string
errmsg(err
->getMessage());
698 *reason
= gstrdup(errmsg
);
701 /* is it ever possible for getValidationError to be 0 ? */
703 return 0; /* invalid */
708 return 1; /* valid */
711 catch (const std::exception
&e
)
713 handle
->ERROR_MESSAGE("%s", e
.what());
717 handle
->ERROR_MESSAGE("Unknown exception thrown");
720 return 2; /* exception */
723 //-----------------------------------------------------------------
725 //-----------------------------------------------------------------
728 GEOSEquals_r(GEOSContextHandle_t extHandle
, const Geometry
*g1
, const Geometry
*g2
)
730 if ( 0 == extHandle
)
735 GEOSContextHandleInternal_t
*handle
= 0;
736 handle
= reinterpret_cast<GEOSContextHandleInternal_t
*>(extHandle
);
737 if ( 0 == handle
->initialized
)
744 bool result
= g1
->equals(g2
);
747 catch (const std::exception
&e
)
749 handle
->ERROR_MESSAGE("%s", e
.what());
753 handle
->ERROR_MESSAGE("Unknown exception thrown");
760 GEOSEqualsExact_r(GEOSContextHandle_t extHandle
, const Geometry
*g1
, const Geometry
*g2
, double tolerance
)
762 if ( 0 == extHandle
)
767 GEOSContextHandleInternal_t
*handle
= 0;
768 handle
= reinterpret_cast<GEOSContextHandleInternal_t
*>(extHandle
);
769 if ( 0 == handle
->initialized
)
776 bool result
= g1
->equalsExact(g2
, tolerance
);
779 catch (const std::exception
&e
)
781 handle
->ERROR_MESSAGE("%s", e
.what());
785 handle
->ERROR_MESSAGE("Unknown exception thrown");
792 GEOSDistance_r(GEOSContextHandle_t extHandle
, const Geometry
*g1
, const Geometry
*g2
, double *dist
)
796 if ( 0 == extHandle
)
801 GEOSContextHandleInternal_t
*handle
= 0;
802 handle
= reinterpret_cast<GEOSContextHandleInternal_t
*>(extHandle
);
803 if ( 0 == handle
->initialized
)
810 *dist
= g1
->distance(g2
);
813 catch (const std::exception
&e
)
815 handle
->ERROR_MESSAGE("%s", e
.what());
819 handle
->ERROR_MESSAGE("Unknown exception thrown");
826 GEOSHausdorffDistance_r(GEOSContextHandle_t extHandle
, const Geometry
*g1
, const Geometry
*g2
, double *dist
)
830 if ( 0 == extHandle
)
835 GEOSContextHandleInternal_t
*handle
= 0;
836 handle
= reinterpret_cast<GEOSContextHandleInternal_t
*>(extHandle
);
837 if ( 0 == handle
->initialized
)
844 *dist
= DiscreteHausdorffDistance::distance(*g1
, *g2
);
847 catch (const std::exception
&e
)
849 handle
->ERROR_MESSAGE("%s", e
.what());
853 handle
->ERROR_MESSAGE("Unknown exception thrown");
860 GEOSHausdorffDistanceDensify_r(GEOSContextHandle_t extHandle
, const Geometry
*g1
, const Geometry
*g2
, double densifyFrac
, double *dist
)
864 if ( 0 == extHandle
)
869 GEOSContextHandleInternal_t
*handle
= 0;
870 handle
= reinterpret_cast<GEOSContextHandleInternal_t
*>(extHandle
);
871 if ( 0 == handle
->initialized
)
878 *dist
= DiscreteHausdorffDistance::distance(*g1
, *g2
, densifyFrac
);
881 catch (const std::exception
&e
)
883 handle
->ERROR_MESSAGE("%s", e
.what());
887 handle
->ERROR_MESSAGE("Unknown exception thrown");
894 GEOSArea_r(GEOSContextHandle_t extHandle
, const Geometry
*g
, double *area
)
898 if ( 0 == extHandle
)
903 GEOSContextHandleInternal_t
*handle
= 0;
904 handle
= reinterpret_cast<GEOSContextHandleInternal_t
*>(extHandle
);
905 if ( 0 == handle
->initialized
)
912 *area
= g
->getArea();
915 catch (const std::exception
&e
)
917 handle
->ERROR_MESSAGE("%s", e
.what());
921 handle
->ERROR_MESSAGE("Unknown exception thrown");
928 GEOSLength_r(GEOSContextHandle_t extHandle
, const Geometry
*g
, double *length
)
932 if ( 0 == extHandle
)
937 GEOSContextHandleInternal_t
*handle
= 0;
938 handle
= reinterpret_cast<GEOSContextHandleInternal_t
*>(extHandle
);
939 if ( 0 == handle
->initialized
)
946 *length
= g
->getLength();
949 catch (const std::exception
&e
)
951 handle
->ERROR_MESSAGE("%s", e
.what());
955 handle
->ERROR_MESSAGE("Unknown exception thrown");
962 GEOSGeomFromWKT_r(GEOSContextHandle_t extHandle
, const char *wkt
)
964 if ( 0 == extHandle
)
969 GEOSContextHandleInternal_t
*handle
= 0;
970 handle
= reinterpret_cast<GEOSContextHandleInternal_t
*>(extHandle
);
971 if ( 0 == handle
->initialized
)
978 const std::string
wktstring(wkt
);
979 WKTReader
r(static_cast<GeometryFactory
const*>(handle
->geomFactory
));
981 Geometry
*g
= r
.read(wktstring
);
984 catch (const std::exception
&e
)
986 handle
->ERROR_MESSAGE("%s", e
.what());
990 handle
->ERROR_MESSAGE("Unknown exception thrown");
997 GEOSGeomToWKT_r(GEOSContextHandle_t extHandle
, const Geometry
*g1
)
999 if ( 0 == extHandle
)
1004 GEOSContextHandleInternal_t
*handle
= 0;
1005 handle
= reinterpret_cast<GEOSContextHandleInternal_t
*>(extHandle
);
1006 if ( 0 == handle
->initialized
)
1014 char *result
= gstrdup(g1
->toString());
1017 catch (const std::exception
&e
)
1019 handle
->ERROR_MESSAGE("%s", e
.what());
1023 handle
->ERROR_MESSAGE("Unknown exception thrown");
1028 // Remember to free the result!
1030 GEOSGeomToWKB_buf_r(GEOSContextHandle_t extHandle
, const Geometry
*g
, size_t *size
)
1034 if ( 0 == extHandle
)
1039 GEOSContextHandleInternal_t
*handle
= 0;
1040 handle
= reinterpret_cast<GEOSContextHandleInternal_t
*>(extHandle
);
1041 if ( 0 == handle
->initialized
)
1046 using geos::io::WKBWriter
;
1049 int byteOrder
= static_cast<int>(handle
->WKBByteOrder
);
1050 WKBWriter
w(handle
->WKBOutputDims
, byteOrder
);
1051 std::ostringstream
os(std::ios_base::binary
);
1053 std::string
wkbstring(os
.str());
1054 const std::size_t len
= wkbstring
.length();
1056 unsigned char* result
= 0;
1057 result
= static_cast<unsigned char*>(std::malloc(len
));
1060 std::memcpy(result
, wkbstring
.c_str(), len
);
1065 catch (const std::exception
&e
)
1067 handle
->ERROR_MESSAGE("%s", e
.what());
1071 handle
->ERROR_MESSAGE("Unknown exception thrown");
1078 GEOSGeomFromWKB_buf_r(GEOSContextHandle_t extHandle
, const unsigned char *wkb
, size_t size
)
1080 if ( 0 == extHandle
)
1085 GEOSContextHandleInternal_t
*handle
= 0;
1086 handle
= reinterpret_cast<GEOSContextHandleInternal_t
*>(extHandle
);
1087 if ( 0 == handle
->initialized
)
1092 using geos::io::WKBReader
;
1095 std::string
wkbstring(reinterpret_cast<const char*>(wkb
), size
); // make it binary !
1096 WKBReader
r(*(static_cast<GeometryFactory
const*>(handle
->geomFactory
)));
1097 std::istringstream
is(std::ios_base::binary
);
1099 is
.seekg(0, std::ios::beg
); // rewind reader pointer
1100 Geometry
*g
= r
.read(is
);
1103 catch (const std::exception
&e
)
1105 handle
->ERROR_MESSAGE("%s", e
.what());
1109 handle
->ERROR_MESSAGE("Unknown exception thrown");
1115 /* Read/write wkb hex values. Returned geometries are
1116 owned by the caller.*/
1118 GEOSGeomToHEX_buf_r(GEOSContextHandle_t extHandle
, const Geometry
*g
, size_t *size
)
1120 if ( 0 == extHandle
)
1125 GEOSContextHandleInternal_t
*handle
= 0;
1126 handle
= reinterpret_cast<GEOSContextHandleInternal_t
*>(extHandle
);
1127 if ( 0 == handle
->initialized
)
1132 using geos::io::WKBWriter
;
1135 int byteOrder
= static_cast<int>(handle
->WKBByteOrder
);
1136 WKBWriter
w(handle
->WKBOutputDims
, byteOrder
);
1137 std::ostringstream
os(std::ios_base::binary
);
1139 std::string
hexstring(os
.str());
1141 char *result
= gstrdup(hexstring
);
1144 *size
= hexstring
.length();
1147 return reinterpret_cast<unsigned char*>(result
);
1149 catch (const std::exception
&e
)
1151 handle
->ERROR_MESSAGE("%s", e
.what());
1155 handle
->ERROR_MESSAGE("Unknown exception thrown");
1162 GEOSGeomFromHEX_buf_r(GEOSContextHandle_t extHandle
, const unsigned char *hex
, size_t size
)
1164 if ( 0 == extHandle
)
1169 GEOSContextHandleInternal_t
*handle
= 0;
1170 handle
= reinterpret_cast<GEOSContextHandleInternal_t
*>(extHandle
);
1171 if ( 0 == handle
->initialized
)
1176 using geos::io::WKBReader
;
1179 std::string
hexstring(reinterpret_cast<const char*>(hex
), size
);
1180 WKBReader
r(*(static_cast<GeometryFactory
const*>(handle
->geomFactory
)));
1181 std::istringstream
is(std::ios_base::binary
);
1183 is
.seekg(0, std::ios::beg
); // rewind reader pointer
1185 Geometry
*g
= r
.readHEX(is
);
1188 catch (const std::exception
&e
)
1190 handle
->ERROR_MESSAGE("%s", e
.what());
1194 handle
->ERROR_MESSAGE("Unknown exception thrown");
1201 GEOSisEmpty_r(GEOSContextHandle_t extHandle
, const Geometry
*g1
)
1203 if ( 0 == extHandle
)
1208 GEOSContextHandleInternal_t
*handle
= 0;
1209 handle
= reinterpret_cast<GEOSContextHandleInternal_t
*>(extHandle
);
1210 if ( 0 == handle
->initialized
)
1217 return g1
->isEmpty();
1219 catch (const std::exception
&e
)
1221 handle
->ERROR_MESSAGE("%s", e
.what());
1225 handle
->ERROR_MESSAGE("Unknown exception thrown");
1232 GEOSisSimple_r(GEOSContextHandle_t extHandle
, const Geometry
*g1
)
1234 if ( 0 == extHandle
)
1239 GEOSContextHandleInternal_t
*handle
= 0;
1240 handle
= reinterpret_cast<GEOSContextHandleInternal_t
*>(extHandle
);
1241 if ( 0 == handle
->initialized
)
1248 return g1
->isSimple();
1250 catch (const std::exception
&e
)
1252 handle
->ERROR_MESSAGE("%s", e
.what());
1258 handle
->ERROR_MESSAGE("Unknown exception thrown");
1264 GEOSisRing_r(GEOSContextHandle_t extHandle
, const Geometry
*g
)
1266 if ( 0 == extHandle
)
1271 GEOSContextHandleInternal_t
*handle
= 0;
1272 handle
= reinterpret_cast<GEOSContextHandleInternal_t
*>(extHandle
);
1273 if ( 0 == handle
->initialized
)
1280 const LineString
*ls
= dynamic_cast<const LineString
*>(g
);
1282 return (ls
->isRing());
1287 catch (const std::exception
&e
)
1289 handle
->ERROR_MESSAGE("%s", e
.what());
1295 handle
->ERROR_MESSAGE("Unknown exception thrown");
1302 //free the result of this
1304 GEOSGeomType_r(GEOSContextHandle_t extHandle
, const Geometry
*g1
)
1306 if ( 0 == extHandle
)
1311 GEOSContextHandleInternal_t
*handle
= 0;
1312 handle
= reinterpret_cast<GEOSContextHandleInternal_t
*>(extHandle
);
1313 if ( 0 == handle
->initialized
)
1320 std::string s
= g1
->getGeometryType();
1322 char *result
= gstrdup(s
);
1325 catch (const std::exception
&e
)
1327 handle
->ERROR_MESSAGE("%s", e
.what());
1331 handle
->ERROR_MESSAGE("Unknown exception thrown");
1337 // Return postgis geometry type index
1339 GEOSGeomTypeId_r(GEOSContextHandle_t extHandle
, const Geometry
*g1
)
1341 if ( 0 == extHandle
)
1346 GEOSContextHandleInternal_t
*handle
= 0;
1347 handle
= reinterpret_cast<GEOSContextHandleInternal_t
*>(extHandle
);
1348 if ( 0 == handle
->initialized
)
1355 return g1
->getGeometryTypeId();
1357 catch (const std::exception
&e
)
1359 handle
->ERROR_MESSAGE("%s", e
.what());
1363 handle
->ERROR_MESSAGE("Unknown exception thrown");
1369 //-------------------------------------------------------------------
1370 // GEOS functions that return geometries
1371 //-------------------------------------------------------------------
1374 GEOSEnvelope_r(GEOSContextHandle_t extHandle
, const Geometry
*g1
)
1376 if ( 0 == extHandle
)
1381 GEOSContextHandleInternal_t
*handle
= 0;
1382 handle
= reinterpret_cast<GEOSContextHandleInternal_t
*>(extHandle
);
1383 if ( 0 == handle
->initialized
)
1390 Geometry
*g3
= g1
->getEnvelope();
1393 catch (const std::exception
&e
)
1395 handle
->ERROR_MESSAGE("%s", e
.what());
1399 handle
->ERROR_MESSAGE("Unknown exception thrown");
1406 GEOSIntersection_r(GEOSContextHandle_t extHandle
, const Geometry
*g1
, const Geometry
*g2
)
1408 if ( 0 == extHandle
)
1413 GEOSContextHandleInternal_t
*handle
= 0;
1414 handle
= reinterpret_cast<GEOSContextHandleInternal_t
*>(extHandle
);
1415 if ( 0 == handle
->initialized
)
1422 GeomAutoPtr
g3(BinaryOp(g1
, g2
, overlayOp(OverlayOp::opINTERSECTION
)));
1423 return g3
.release();
1426 //Geometry *g3 = g1->intersection(g2);
1429 catch (const std::exception
&e
)
1431 handle
->ERROR_MESSAGE("%s", e
.what());
1435 handle
->ERROR_MESSAGE("Unknown exception thrown");
1442 GEOSBuffer_r(GEOSContextHandle_t extHandle
, const Geometry
*g1
, double width
, int quadrantsegments
)
1444 if ( 0 == extHandle
)
1449 GEOSContextHandleInternal_t
*handle
= 0;
1450 handle
= reinterpret_cast<GEOSContextHandleInternal_t
*>(extHandle
);
1451 if ( 0 == handle
->initialized
)
1458 Geometry
*g3
= g1
->buffer(width
, quadrantsegments
);
1461 catch (const std::exception
&e
)
1463 handle
->ERROR_MESSAGE("%s", e
.what());
1467 handle
->ERROR_MESSAGE("Unknown exception thrown");
1474 GEOSBufferWithStyle_r(GEOSContextHandle_t extHandle
, const Geometry
*g1
, double width
, int quadsegs
, int endCapStyle
, int joinStyle
, double mitreLimit
)
1476 using geos::operation::buffer::BufferParameters
;
1477 using geos::operation::buffer::BufferOp
;
1478 using geos::util::IllegalArgumentException
;
1480 if ( 0 == extHandle
)
1485 GEOSContextHandleInternal_t
*handle
= 0;
1486 handle
= reinterpret_cast<GEOSContextHandleInternal_t
*>(extHandle
);
1487 if ( 0 == handle
->initialized
)
1494 BufferParameters bp
;
1495 bp
.setQuadrantSegments(quadsegs
);
1497 if ( endCapStyle
> BufferParameters::CAP_SQUARE
)
1499 throw IllegalArgumentException("Invalid buffer endCap style");
1502 static_cast<BufferParameters::EndCapStyle
>(endCapStyle
)
1505 if ( joinStyle
> BufferParameters::JOIN_BEVEL
)
1507 throw IllegalArgumentException("Invalid buffer join style");
1510 static_cast<BufferParameters::JoinStyle
>(joinStyle
)
1512 bp
.setMitreLimit(mitreLimit
);
1513 BufferOp
op(g1
, bp
);
1514 Geometry
*g3
= op
.getResultGeometry(width
);
1517 catch (const std::exception
&e
)
1519 handle
->ERROR_MESSAGE("%s", e
.what());
1523 handle
->ERROR_MESSAGE("Unknown exception thrown");
1530 GEOSSingleSidedBuffer_r(GEOSContextHandle_t extHandle
, const Geometry
*g1
, double width
, int quadsegs
, int joinStyle
, double mitreLimit
, int leftSide
)
1532 using geos::operation::buffer::BufferParameters
;
1533 using geos::operation::buffer::BufferBuilder
;
1534 using geos::operation::buffer::BufferOp
;
1535 using geos::util::IllegalArgumentException
;
1537 if ( 0 == extHandle
)
1542 GEOSContextHandleInternal_t
*handle
= 0;
1543 handle
= reinterpret_cast<GEOSContextHandleInternal_t
*>(extHandle
);
1544 if ( 0 == handle
->initialized
)
1551 BufferParameters bp
;
1552 bp
.setEndCapStyle( BufferParameters::CAP_FLAT
);
1553 bp
.setQuadrantSegments(quadsegs
);
1555 if ( joinStyle
> BufferParameters::JOIN_BEVEL
)
1557 throw IllegalArgumentException("Invalid buffer join style");
1560 static_cast<BufferParameters::JoinStyle
>(joinStyle
)
1562 bp
.setMitreLimit(mitreLimit
);
1564 bool isLeftSide
= leftSide
== 0 ? false : true;
1565 BufferBuilder
bufBuilder (bp
);
1566 Geometry
*g3
= bufBuilder
.bufferLineSingleSided(g1
, width
, isLeftSide
);
1570 catch (const std::exception
&e
)
1572 handle
->ERROR_MESSAGE("%s", e
.what());
1576 handle
->ERROR_MESSAGE("Unknown exception thrown");
1583 GEOSConvexHull_r(GEOSContextHandle_t extHandle
, const Geometry
*g1
)
1585 if ( 0 == extHandle
)
1590 GEOSContextHandleInternal_t
*handle
= 0;
1591 handle
= reinterpret_cast<GEOSContextHandleInternal_t
*>(extHandle
);
1592 if ( 0 == handle
->initialized
)
1599 Geometry
*g3
= g1
->convexHull();
1602 catch (const std::exception
&e
)
1604 handle
->ERROR_MESSAGE("%s", e
.what());
1608 handle
->ERROR_MESSAGE("Unknown exception thrown");
1615 GEOSDifference_r(GEOSContextHandle_t extHandle
, const Geometry
*g1
, const Geometry
*g2
)
1617 if ( 0 == extHandle
)
1622 GEOSContextHandleInternal_t
*handle
= 0;
1623 handle
= reinterpret_cast<GEOSContextHandleInternal_t
*>(extHandle
);
1624 if ( 0 == handle
->initialized
)
1631 GeomAutoPtr
g3(BinaryOp(g1
, g2
, overlayOp(OverlayOp::opDIFFERENCE
)));
1632 return g3
.release();
1635 //Geometry *g3 = g1->difference(g2);
1638 catch (const std::exception
&e
)
1640 handle
->ERROR_MESSAGE("%s", e
.what());
1644 handle
->ERROR_MESSAGE("Unknown exception thrown");
1651 GEOSBoundary_r(GEOSContextHandle_t extHandle
, const Geometry
*g1
)
1653 if ( 0 == extHandle
)
1658 GEOSContextHandleInternal_t
*handle
= 0;
1659 handle
= reinterpret_cast<GEOSContextHandleInternal_t
*>(extHandle
);
1660 if ( 0 == handle
->initialized
)
1667 Geometry
*g3
= g1
->getBoundary();
1670 catch (const std::exception
&e
)
1672 handle
->ERROR_MESSAGE("%s", e
.what());
1676 handle
->ERROR_MESSAGE("Unknown exception thrown");
1683 GEOSSymDifference_r(GEOSContextHandle_t extHandle
, const Geometry
*g1
, const Geometry
*g2
)
1685 if ( 0 == extHandle
)
1690 GEOSContextHandleInternal_t
*handle
= 0;
1691 handle
= reinterpret_cast<GEOSContextHandleInternal_t
*>(extHandle
);
1692 if ( 0 == handle
->initialized
)
1699 GeomAutoPtr g3
= BinaryOp(g1
, g2
, overlayOp(OverlayOp::opSYMDIFFERENCE
));
1700 return g3
.release();
1701 //Geometry *g3 = g1->symDifference(g2);
1704 catch (const std::exception
&e
)
1706 handle
->ERROR_MESSAGE("%s", e
.what());
1712 handle
->ERROR_MESSAGE("Unknown exception thrown");
1718 GEOSUnion_r(GEOSContextHandle_t extHandle
, const Geometry
*g1
, const Geometry
*g2
)
1720 if ( 0 == extHandle
)
1725 GEOSContextHandleInternal_t
*handle
= 0;
1726 handle
= reinterpret_cast<GEOSContextHandleInternal_t
*>(extHandle
);
1727 if ( 0 == handle
->initialized
)
1734 GeomAutoPtr g3
= BinaryOp(g1
, g2
, overlayOp(OverlayOp::opUNION
));
1735 return g3
.release();
1738 //Geometry *g3 = g1->Union(g2);
1741 catch (const std::exception
&e
)
1743 #if VERBOSE_EXCEPTIONS
1744 std::ostringstream s
;
1745 s
<< "Exception on GEOSUnion with following inputs:" << std::endl
;
1746 s
<< "A: "<<g1
->toString() << std::endl
;
1747 s
<< "B: "<<g2
->toString() << std::endl
;
1748 handle
->NOTICE_MESSAGE("%s", s
.str().c_str());
1749 #endif // VERBOSE_EXCEPTIONS
1750 handle
->ERROR_MESSAGE("%s", e
.what());
1754 handle
->ERROR_MESSAGE("Unknown exception thrown");
1761 GEOSUnionCascaded_r(GEOSContextHandle_t extHandle
, const Geometry
*g1
)
1763 if ( 0 == extHandle
)
1768 GEOSContextHandleInternal_t
*handle
= 0;
1769 handle
= reinterpret_cast<GEOSContextHandleInternal_t
*>(extHandle
);
1770 if ( 0 == handle
->initialized
)
1777 const geos::geom::MultiPolygon
*p
= dynamic_cast<const geos::geom::MultiPolygon
*>(g1
);
1780 handle
->ERROR_MESSAGE("Invalid argument (must be a MultiPolygon)");
1784 using geos::operation::geounion::CascadedPolygonUnion
;
1785 return CascadedPolygonUnion::Union(p
);
1787 catch (const std::exception
&e
)
1789 handle
->ERROR_MESSAGE("%s", e
.what());
1793 handle
->ERROR_MESSAGE("Unknown exception thrown");
1800 GEOSPointOnSurface_r(GEOSContextHandle_t extHandle
, const Geometry
*g1
)
1802 if ( 0 == extHandle
)
1807 GEOSContextHandleInternal_t
*handle
= 0;
1808 handle
= reinterpret_cast<GEOSContextHandleInternal_t
*>(extHandle
);
1809 if ( 0 == handle
->initialized
)
1816 Geometry
*ret
= g1
->getInteriorPoint();
1819 const GeometryFactory
* gf
= handle
->geomFactory
;
1820 // return an empty collection
1821 return gf
->createGeometryCollection();
1825 catch (const std::exception
&e
)
1827 handle
->ERROR_MESSAGE("%s", e
.what());
1831 handle
->ERROR_MESSAGE("Unknown exception thrown");
1837 //-------------------------------------------------------------------
1838 // memory management functions
1839 //------------------------------------------------------------------
1842 GEOSGeom_destroy_r(GEOSContextHandle_t extHandle
, Geometry
*a
)
1844 GEOSContextHandleInternal_t
*handle
= 0;
1846 // FIXME: mloskot: Does this try-catch around delete means that
1847 // destructors in GEOS may throw? If it does, this is a serious
1848 // violation of "never throw an exception from a destructor" principle
1854 catch (const std::exception
&e
)
1856 if ( 0 == extHandle
)
1861 handle
= reinterpret_cast<GEOSContextHandleInternal_t
*>(extHandle
);
1862 if ( 0 == handle
->initialized
)
1867 handle
->ERROR_MESSAGE("%s", e
.what());
1871 if ( 0 == extHandle
)
1876 handle
= reinterpret_cast<GEOSContextHandleInternal_t
*>(extHandle
);
1877 if ( 0 == handle
->initialized
)
1882 handle
->ERROR_MESSAGE("Unknown exception thrown");
1887 GEOSSetSRID_r(GEOSContextHandle_t extHandle
, Geometry
*g
, int srid
)
1891 if ( 0 == extHandle
)
1896 GEOSContextHandleInternal_t
*handle
= 0;
1897 handle
= reinterpret_cast<GEOSContextHandleInternal_t
*>(extHandle
);
1898 if ( 0 == handle
->initialized
)
1908 GEOSGetNumCoordinates_r(GEOSContextHandle_t extHandle
, const Geometry
*g
)
1912 if ( 0 == extHandle
)
1917 GEOSContextHandleInternal_t
*handle
= 0;
1918 handle
= reinterpret_cast<GEOSContextHandleInternal_t
*>(extHandle
);
1919 if ( 0 == handle
->initialized
)
1926 return static_cast<int>(g
->getNumPoints());
1928 catch (const std::exception
&e
)
1930 handle
->ERROR_MESSAGE("%s", e
.what());
1934 handle
->ERROR_MESSAGE("Unknown exception thrown");
1941 * Return -1 on exception, 0 otherwise.
1942 * Converts Geometry to normal form (or canonical form).
1945 GEOSNormalize_r(GEOSContextHandle_t extHandle
, Geometry
*g
)
1949 if ( 0 == extHandle
)
1954 GEOSContextHandleInternal_t
*handle
= 0;
1955 handle
= reinterpret_cast<GEOSContextHandleInternal_t
*>(extHandle
);
1956 if ( 0 == handle
->initialized
)
1964 return 0; // SUCCESS
1966 catch (const std::exception
&e
)
1968 handle
->ERROR_MESSAGE("%s", e
.what());
1972 handle
->ERROR_MESSAGE("Unknown exception thrown");
1979 GEOSGetNumInteriorRings_r(GEOSContextHandle_t extHandle
, const Geometry
*g1
)
1981 if ( 0 == extHandle
)
1986 GEOSContextHandleInternal_t
*handle
= 0;
1987 handle
= reinterpret_cast<GEOSContextHandleInternal_t
*>(extHandle
);
1988 if ( 0 == handle
->initialized
)
1995 const Polygon
*p
= dynamic_cast<const Polygon
*>(g1
);
1998 handle
->ERROR_MESSAGE("Argument is not a Polygon");
2001 return static_cast<int>(p
->getNumInteriorRing());
2003 catch (const std::exception
&e
)
2005 handle
->ERROR_MESSAGE("%s", e
.what());
2009 handle
->ERROR_MESSAGE("Unknown exception thrown");
2016 // returns -1 on error and 1 for non-multi geometries
2018 GEOSGetNumGeometries_r(GEOSContextHandle_t extHandle
, const Geometry
*g1
)
2020 if ( 0 == extHandle
)
2025 GEOSContextHandleInternal_t
*handle
= 0;
2026 handle
= reinterpret_cast<GEOSContextHandleInternal_t
*>(extHandle
);
2027 if ( 0 == handle
->initialized
)
2034 return static_cast<int>(g1
->getNumGeometries());
2036 catch (const std::exception
&e
)
2038 handle
->ERROR_MESSAGE("%s", e
.what());
2042 handle
->ERROR_MESSAGE("Unknown exception thrown");
2050 * Call only on GEOMETRYCOLLECTION or MULTI*.
2051 * Return a pointer to the internal Geometry.
2054 GEOSGetGeometryN_r(GEOSContextHandle_t extHandle
, const Geometry
*g1
, int n
)
2056 if ( 0 == extHandle
)
2061 GEOSContextHandleInternal_t
*handle
= 0;
2062 handle
= reinterpret_cast<GEOSContextHandleInternal_t
*>(extHandle
);
2063 if ( 0 == handle
->initialized
)
2070 using geos::geom::GeometryCollection
;
2071 const GeometryCollection
*gc
= dynamic_cast<const GeometryCollection
*>(g1
);
2074 handle
->ERROR_MESSAGE("Argument is not a GeometryCollection");
2077 return gc
->getGeometryN(n
);
2079 catch (const std::exception
&e
)
2081 handle
->ERROR_MESSAGE("%s", e
.what());
2085 handle
->ERROR_MESSAGE("Unknown exception thrown");
2093 * Call only on polygon
2094 * Return a copy of the internal Geometry.
2097 GEOSGetExteriorRing_r(GEOSContextHandle_t extHandle
, const Geometry
*g1
)
2099 if ( 0 == extHandle
)
2104 GEOSContextHandleInternal_t
*handle
= 0;
2105 handle
= reinterpret_cast<GEOSContextHandleInternal_t
*>(extHandle
);
2106 if ( 0 == handle
->initialized
)
2113 const Polygon
*p
= dynamic_cast<const Polygon
*>(g1
);
2116 handle
->ERROR_MESSAGE("Invalid argument (must be a Polygon)");
2119 return p
->getExteriorRing();
2121 catch (const std::exception
&e
)
2123 handle
->ERROR_MESSAGE("%s", e
.what());
2127 handle
->ERROR_MESSAGE("Unknown exception thrown");
2134 * Call only on polygon
2135 * Return a pointer to internal storage, do not destroy it.
2138 GEOSGetInteriorRingN_r(GEOSContextHandle_t extHandle
, const Geometry
*g1
, int n
)
2140 if ( 0 == extHandle
)
2145 GEOSContextHandleInternal_t
*handle
= 0;
2146 handle
= reinterpret_cast<GEOSContextHandleInternal_t
*>(extHandle
);
2147 if ( 0 == handle
->initialized
)
2154 const Polygon
*p
= dynamic_cast<const Polygon
*>(g1
);
2157 handle
->ERROR_MESSAGE("Invalid argument (must be a Polygon)");
2160 return p
->getInteriorRingN(n
);
2162 catch (const std::exception
&e
)
2164 handle
->ERROR_MESSAGE("%s", e
.what());
2168 handle
->ERROR_MESSAGE("Unknown exception thrown");
2175 GEOSGetCentroid_r(GEOSContextHandle_t extHandle
, const Geometry
*g
)
2177 if ( 0 == extHandle
)
2182 GEOSContextHandleInternal_t
*handle
= 0;
2183 handle
= reinterpret_cast<GEOSContextHandleInternal_t
*>(extHandle
);
2184 if ( 0 == handle
->initialized
)
2191 Geometry
*ret
= g
->getCentroid();
2194 const GeometryFactory
*gf
= handle
->geomFactory
;
2195 return gf
->createGeometryCollection();
2199 catch (const std::exception
&e
)
2201 handle
->ERROR_MESSAGE("%s", e
.what());
2205 handle
->ERROR_MESSAGE("Unknown exception thrown");
2212 GEOSGeom_createEmptyCollection_r(GEOSContextHandle_t extHandle
, int type
)
2214 if ( 0 == extHandle
)
2219 GEOSContextHandleInternal_t
*handle
= 0;
2220 handle
= reinterpret_cast<GEOSContextHandleInternal_t
*>(extHandle
);
2221 if ( 0 == handle
->initialized
)
2228 sprintf(buf
, "createCollection: requested type %d, ngeoms: %d",
2230 handle
->NOTICE_MESSAGE("%s", buf
);// TODO: Can handle->NOTICE_MESSAGE format that directly?
2235 const GeometryFactory
* gf
= handle
->geomFactory
;
2240 case GEOS_GEOMETRYCOLLECTION
:
2241 g
= gf
->createGeometryCollection();
2243 case GEOS_MULTIPOINT
:
2244 g
= gf
->createMultiPoint();
2246 case GEOS_MULTILINESTRING
:
2247 g
= gf
->createMultiLineString();
2249 case GEOS_MULTIPOLYGON
:
2250 g
= gf
->createMultiPolygon();
2253 handle
->ERROR_MESSAGE("Unsupported type request for GEOSGeom_createEmptyCollection_r");
2260 catch (const std::exception
&e
)
2262 handle
->ERROR_MESSAGE("%s", e
.what());
2266 handle
->ERROR_MESSAGE("Unknown exception thrown");
2273 GEOSGeom_createCollection_r(GEOSContextHandle_t extHandle
, int type
, Geometry
**geoms
, unsigned int ngeoms
)
2275 if ( 0 == extHandle
)
2280 GEOSContextHandleInternal_t
*handle
= 0;
2281 handle
= reinterpret_cast<GEOSContextHandleInternal_t
*>(extHandle
);
2282 if ( 0 == handle
->initialized
)
2289 sprintf(buf
, "PostGIS2GEOS_collection: requested type %d, ngeoms: %d",
2291 handle
->NOTICE_MESSAGE("%s", buf
);// TODO: Can handle->NOTICE_MESSAGE format that directly?
2296 const GeometryFactory
* gf
= handle
->geomFactory
;
2297 std::vector
<Geometry
*>* vgeoms
= new std::vector
<Geometry
*>(geoms
, geoms
+ ngeoms
);
2302 case GEOS_GEOMETRYCOLLECTION
:
2303 g
= gf
->createGeometryCollection(vgeoms
);
2305 case GEOS_MULTIPOINT
:
2306 g
= gf
->createMultiPoint(vgeoms
);
2308 case GEOS_MULTILINESTRING
:
2309 g
= gf
->createMultiLineString(vgeoms
);
2311 case GEOS_MULTIPOLYGON
:
2312 g
= gf
->createMultiPolygon(vgeoms
);
2315 handle
->ERROR_MESSAGE("Unsupported type request for PostGIS2GEOS_collection");
2322 catch (const std::exception
&e
)
2324 handle
->ERROR_MESSAGE("%s", e
.what());
2328 handle
->ERROR_MESSAGE("Unknown exception thrown");
2335 GEOSPolygonize_r(GEOSContextHandle_t extHandle
, const Geometry
* const * g
, unsigned int ngeoms
)
2337 if ( 0 == extHandle
)
2342 GEOSContextHandleInternal_t
*handle
= 0;
2343 handle
= reinterpret_cast<GEOSContextHandleInternal_t
*>(extHandle
);
2344 if ( 0 == handle
->initialized
)
2354 using geos::operation::polygonize::Polygonizer
;
2356 for (std::size_t i
= 0; i
< ngeoms
; ++i
)
2362 handle
->NOTICE_MESSAGE("geometry vector added to polygonizer");
2365 std::vector
<Polygon
*> *polys
= plgnzr
.getPolygons();
2369 handle
->NOTICE_MESSAGE("output polygons got");
2372 // We need a vector of Geometry pointers, not Polygon pointers.
2373 // STL vector doesn't allow transparent upcast of this
2374 // nature, so we explicitly convert.
2375 // (it's just a waste of processor and memory, btw)
2377 // XXX mloskot: Why not to extent GeometryFactory to accept
2378 // vector of polygons or extend Polygonizer to return list of Geometry*
2379 // or add a wrapper which semantic is similar to:
2380 // std::vector<as_polygon<Geometry*> >
2381 std::vector
<Geometry
*> *polyvec
= new std::vector
<Geometry
*>(polys
->size());
2383 for (std::size_t i
= 0; i
< polys
->size(); ++i
)
2385 (*polyvec
)[i
] = (*polys
)[i
];
2390 const GeometryFactory
*gf
= handle
->geomFactory
;
2392 // The below takes ownership of the passed vector,
2393 // so we must *not* delete it
2394 out
= gf
->createGeometryCollection(polyvec
);
2396 catch (const std::exception
&e
)
2398 handle
->ERROR_MESSAGE("%s", e
.what());
2402 handle
->ERROR_MESSAGE("Unknown exception thrown");
2409 GEOSPolygonizer_getCutEdges_r(GEOSContextHandle_t extHandle
, const Geometry
* const * g
, unsigned int ngeoms
)
2411 if ( 0 == extHandle
)
2416 GEOSContextHandleInternal_t
*handle
= 0;
2417 handle
= reinterpret_cast<GEOSContextHandleInternal_t
*>(extHandle
);
2418 if ( 0 == handle
->initialized
)
2428 using geos::operation::polygonize::Polygonizer
;
2430 for (std::size_t i
= 0; i
< ngeoms
; ++i
)
2436 handle
->NOTICE_MESSAGE("geometry vector added to polygonizer");
2439 std::vector
<const LineString
*>* lines
= plgnzr
.getCutEdges();
2443 handle
->NOTICE_MESSAGE("output polygons got");
2446 // We need a vector of Geometry pointers, not Polygon pointers.
2447 // STL vector doesn't allow transparent upcast of this
2448 // nature, so we explicitly convert.
2449 // (it's just a waste of processor and memory, btw)
2450 // XXX mloskot: See comment for GEOSPolygonize_r
2451 std::vector
<Geometry
*> *linevec
= new std::vector
<Geometry
*>(lines
->size());
2453 for (std::size_t i
= 0; i
< lines
->size(); ++i
)
2455 (*linevec
)[i
] = (*lines
)[i
]->clone();
2457 // FIXME mloskot: Who deallocates vector pointed by lines* ?
2459 const GeometryFactory
*gf
= handle
->geomFactory
;
2461 // The below takes ownership of the passed vector,
2462 // so we must *not* delete it
2463 out
= gf
->createGeometryCollection(linevec
);
2465 catch (const std::exception
&e
)
2467 handle
->ERROR_MESSAGE("%s", e
.what());
2471 handle
->ERROR_MESSAGE("Unknown exception thrown");
2478 GEOSLineMerge_r(GEOSContextHandle_t extHandle
, const Geometry
*g
)
2480 if ( 0 == extHandle
)
2485 GEOSContextHandleInternal_t
*handle
= 0;
2486 handle
= reinterpret_cast<GEOSContextHandleInternal_t
*>(extHandle
);
2487 if ( 0 == handle
->initialized
)
2496 using geos::operation::linemerge::LineMerger
;
2500 std::vector
<LineString
*>* lines
= lmrgr
.getMergedLineStrings();
2504 handle
->NOTICE_MESSAGE("output lines got");
2507 std::vector
<Geometry
*>*geoms
= new std::vector
<Geometry
*>(lines
->size());
2508 for (std::vector
<Geometry
*>::size_type i
= 0; i
< lines
->size(); ++i
)
2510 (*geoms
)[i
] = (*lines
)[i
];
2515 const GeometryFactory
*gf
= handle
->geomFactory
;
2516 out
= gf
->buildGeometry(geoms
);
2519 //out = gf->createGeometryCollection(geoms);
2521 catch (const std::exception
&e
)
2523 handle
->ERROR_MESSAGE("%s", e
.what());
2527 handle
->ERROR_MESSAGE("Unknown exception thrown");
2534 GEOSGetSRID_r(GEOSContextHandle_t extHandle
, const Geometry
*g
)
2538 if ( 0 == extHandle
)
2543 GEOSContextHandleInternal_t
*handle
= 0;
2544 handle
= reinterpret_cast<GEOSContextHandleInternal_t
*>(extHandle
);
2545 if ( 0 == handle
->initialized
)
2552 return g
->getSRID();
2554 catch (const std::exception
&e
)
2556 handle
->ERROR_MESSAGE("%s", e
.what());
2560 handle
->ERROR_MESSAGE("Unknown exception thrown");
2566 const char* GEOSversion()
2568 return GEOS_CAPI_VERSION
;
2571 const char* GEOSjtsport()
2573 return GEOS_JTS_PORT
;
2577 GEOSHasZ_r(GEOSContextHandle_t extHandle
, const Geometry
*g
)
2581 if ( 0 == extHandle
)
2586 GEOSContextHandleInternal_t
*handle
= 0;
2587 handle
= reinterpret_cast<GEOSContextHandleInternal_t
*>(extHandle
);
2588 if ( 0 == handle
->initialized
)
2597 assert(0 != g
->getCoordinate());
2599 double az
= g
->getCoordinate()->z
;
2600 //handle->ERROR_MESSAGE("ZCoord: %g", az);
2602 return static_cast<char>(FINITE(az
));
2606 GEOS_getWKBOutputDims_r(GEOSContextHandle_t extHandle
)
2608 if ( 0 == extHandle
)
2613 GEOSContextHandleInternal_t
*handle
= 0;
2614 handle
= reinterpret_cast<GEOSContextHandleInternal_t
*>(extHandle
);
2615 if ( 0 == handle
->initialized
)
2620 return handle
->WKBOutputDims
;
2624 GEOS_setWKBOutputDims_r(GEOSContextHandle_t extHandle
, int newdims
)
2626 if ( 0 == extHandle
)
2631 GEOSContextHandleInternal_t
*handle
= 0;
2632 handle
= reinterpret_cast<GEOSContextHandleInternal_t
*>(extHandle
);
2633 if ( 0 == handle
->initialized
)
2638 if ( newdims
< 2 || newdims
> 3 )
2640 handle
->ERROR_MESSAGE("WKB output dimensions out of range 2..3");
2643 const int olddims
= handle
->WKBOutputDims
;
2644 handle
->WKBOutputDims
= newdims
;
2650 GEOS_getWKBByteOrder_r(GEOSContextHandle_t extHandle
)
2652 if ( 0 == extHandle
)
2657 GEOSContextHandleInternal_t
*handle
= 0;
2658 handle
= reinterpret_cast<GEOSContextHandleInternal_t
*>(extHandle
);
2659 if ( 0 == handle
->initialized
)
2664 return handle
->WKBByteOrder
;
2668 GEOS_setWKBByteOrder_r(GEOSContextHandle_t extHandle
, int byteOrder
)
2670 if ( 0 == extHandle
)
2675 GEOSContextHandleInternal_t
*handle
= 0;
2676 handle
= reinterpret_cast<GEOSContextHandleInternal_t
*>(extHandle
);
2677 if ( 0 == handle
->initialized
)
2682 const int oldByteOrder
= handle
->WKBByteOrder
;
2683 handle
->WKBByteOrder
= byteOrder
;
2685 return oldByteOrder
;
2689 CoordinateSequence
*
2690 GEOSCoordSeq_create_r(GEOSContextHandle_t extHandle
, unsigned int size
, unsigned int dims
)
2692 if ( 0 == extHandle
)
2697 GEOSContextHandleInternal_t
*handle
= 0;
2698 handle
= reinterpret_cast<GEOSContextHandleInternal_t
*>(extHandle
);
2699 if ( 0 == handle
->initialized
)
2706 const GeometryFactory
*gf
= handle
->geomFactory
;
2707 return gf
->getCoordinateSequenceFactory()->create(size
, dims
);
2709 catch (const std::exception
&e
)
2711 handle
->ERROR_MESSAGE("%s", e
.what());
2715 handle
->ERROR_MESSAGE("Unknown exception thrown");
2722 GEOSCoordSeq_setOrdinate_r(GEOSContextHandle_t extHandle
, CoordinateSequence
*cs
,
2723 unsigned int idx
, unsigned int dim
, double val
)
2726 if ( 0 == extHandle
)
2731 GEOSContextHandleInternal_t
*handle
= 0;
2732 handle
= reinterpret_cast<GEOSContextHandleInternal_t
*>(extHandle
);
2733 if ( 0 == handle
->initialized
)
2740 cs
->setOrdinate(static_cast<int>(idx
), static_cast<int>(dim
), val
);
2743 catch (const std::exception
&e
)
2745 handle
->ERROR_MESSAGE("%s", e
.what());
2749 handle
->ERROR_MESSAGE("Unknown exception thrown");
2756 GEOSCoordSeq_setX_r(GEOSContextHandle_t extHandle
, CoordinateSequence
*s
, unsigned int idx
, double val
)
2758 return GEOSCoordSeq_setOrdinate_r(extHandle
, s
, idx
, 0, val
);
2762 GEOSCoordSeq_setY_r(GEOSContextHandle_t extHandle
, CoordinateSequence
*s
, unsigned int idx
, double val
)
2764 return GEOSCoordSeq_setOrdinate_r(extHandle
, s
, idx
, 1, val
);
2768 GEOSCoordSeq_setZ_r(GEOSContextHandle_t extHandle
, CoordinateSequence
*s
, unsigned int idx
, double val
)
2770 return GEOSCoordSeq_setOrdinate_r(extHandle
, s
, idx
, 2, val
);
2773 CoordinateSequence
*
2774 GEOSCoordSeq_clone_r(GEOSContextHandle_t extHandle
, const CoordinateSequence
*cs
)
2778 if ( 0 == extHandle
)
2783 GEOSContextHandleInternal_t
*handle
= 0;
2784 handle
= reinterpret_cast<GEOSContextHandleInternal_t
*>(extHandle
);
2785 if ( 0 == handle
->initialized
)
2794 catch (const std::exception
&e
)
2796 handle
->ERROR_MESSAGE("%s", e
.what());
2800 handle
->ERROR_MESSAGE("Unknown exception thrown");
2807 GEOSCoordSeq_getOrdinate_r(GEOSContextHandle_t extHandle
, const CoordinateSequence
*cs
,
2808 unsigned int idx
, unsigned int dim
, double *val
)
2813 if ( 0 == extHandle
)
2818 GEOSContextHandleInternal_t
*handle
= 0;
2819 handle
= reinterpret_cast<GEOSContextHandleInternal_t
*>(extHandle
);
2820 if ( 0 == handle
->initialized
)
2827 double d
= cs
->getOrdinate(static_cast<int>(idx
), static_cast<int>(dim
));
2832 catch (const std::exception
&e
)
2834 handle
->ERROR_MESSAGE("%s", e
.what());
2838 handle
->ERROR_MESSAGE("Unknown exception thrown");
2845 GEOSCoordSeq_getX_r(GEOSContextHandle_t extHandle
, const CoordinateSequence
*s
, unsigned int idx
, double *val
)
2847 return GEOSCoordSeq_getOrdinate_r(extHandle
, s
, idx
, 0, val
);
2851 GEOSCoordSeq_getY_r(GEOSContextHandle_t extHandle
, const CoordinateSequence
*s
, unsigned int idx
, double *val
)
2853 return GEOSCoordSeq_getOrdinate_r(extHandle
, s
, idx
, 1, val
);
2857 GEOSCoordSeq_getZ_r(GEOSContextHandle_t extHandle
, const CoordinateSequence
*s
, unsigned int idx
, double *val
)
2859 return GEOSCoordSeq_getOrdinate_r(extHandle
, s
, idx
, 2, val
);
2863 GEOSCoordSeq_getSize_r(GEOSContextHandle_t extHandle
, const CoordinateSequence
*cs
, unsigned int *size
)
2868 if ( 0 == extHandle
)
2873 GEOSContextHandleInternal_t
*handle
= 0;
2874 handle
= reinterpret_cast<GEOSContextHandleInternal_t
*>(extHandle
);
2875 if ( 0 == handle
->initialized
)
2882 const std::size_t sz
= cs
->getSize();
2883 *size
= static_cast<unsigned int>(sz
);
2886 catch (const std::exception
&e
)
2888 handle
->ERROR_MESSAGE("%s", e
.what());
2892 handle
->ERROR_MESSAGE("Unknown exception thrown");
2899 GEOSCoordSeq_getDimensions_r(GEOSContextHandle_t extHandle
, const CoordinateSequence
*cs
, unsigned int *dims
)
2904 if ( 0 == extHandle
)
2909 GEOSContextHandleInternal_t
*handle
= 0;
2910 handle
= reinterpret_cast<GEOSContextHandleInternal_t
*>(extHandle
);
2911 if ( 0 == handle
->initialized
)
2918 const std::size_t dim
= cs
->getDimension();
2919 *dims
= static_cast<unsigned int>(dim
);
2923 catch (const std::exception
&e
)
2925 handle
->ERROR_MESSAGE("%s", e
.what());
2930 handle
->ERROR_MESSAGE("Unknown exception thrown");
2937 GEOSCoordSeq_destroy_r(GEOSContextHandle_t extHandle
, CoordinateSequence
*s
)
2939 GEOSContextHandleInternal_t
*handle
= 0;
2945 catch (const std::exception
&e
)
2947 if ( 0 == extHandle
)
2952 handle
= reinterpret_cast<GEOSContextHandleInternal_t
*>(extHandle
);
2953 if ( 0 == handle
->initialized
)
2958 handle
->ERROR_MESSAGE("%s", e
.what());
2962 if ( 0 == extHandle
)
2967 handle
= reinterpret_cast<GEOSContextHandleInternal_t
*>(extHandle
);
2968 if ( 0 == handle
->initialized
)
2973 handle
->ERROR_MESSAGE("Unknown exception thrown");
2977 const CoordinateSequence
*
2978 GEOSGeom_getCoordSeq_r(GEOSContextHandle_t extHandle
, const Geometry
*g
)
2980 if ( 0 == extHandle
)
2985 GEOSContextHandleInternal_t
*handle
= 0;
2986 handle
= reinterpret_cast<GEOSContextHandleInternal_t
*>(extHandle
);
2987 if ( 0 == handle
->initialized
)
2994 using geos::geom::Point
;
2996 const LineString
*ls
= dynamic_cast<const LineString
*>(g
);
2999 return ls
->getCoordinatesRO();
3002 const Point
*p
= dynamic_cast<const Point
*>(g
);
3005 return p
->getCoordinatesRO();
3008 handle
->ERROR_MESSAGE("Geometry must be a Point or LineString");
3011 catch (const std::exception
&e
)
3013 handle
->ERROR_MESSAGE("%s", e
.what());
3017 handle
->ERROR_MESSAGE("Unknown exception thrown");
3024 GEOSGeom_createEmptyPoint_r(GEOSContextHandle_t extHandle
)
3026 if ( 0 == extHandle
)
3031 GEOSContextHandleInternal_t
*handle
= 0;
3032 handle
= reinterpret_cast<GEOSContextHandleInternal_t
*>(extHandle
);
3033 if ( 0 == handle
->initialized
)
3040 const GeometryFactory
*gf
= handle
->geomFactory
;
3041 return gf
->createPoint();
3043 catch (const std::exception
&e
)
3045 handle
->ERROR_MESSAGE("%s", e
.what());
3049 handle
->ERROR_MESSAGE("Unknown exception thrown");
3056 GEOSGeom_createPoint_r(GEOSContextHandle_t extHandle
, CoordinateSequence
*cs
)
3058 if ( 0 == extHandle
)
3063 GEOSContextHandleInternal_t
*handle
= 0;
3064 handle
= reinterpret_cast<GEOSContextHandleInternal_t
*>(extHandle
);
3065 if ( 0 == handle
->initialized
)
3072 const GeometryFactory
*gf
= handle
->geomFactory
;
3073 return gf
->createPoint(cs
);
3075 catch (const std::exception
&e
)
3077 handle
->ERROR_MESSAGE("%s", e
.what());
3081 handle
->ERROR_MESSAGE("Unknown exception thrown");
3088 GEOSGeom_createLinearRing_r(GEOSContextHandle_t extHandle
, CoordinateSequence
*cs
)
3090 if ( 0 == extHandle
)
3095 GEOSContextHandleInternal_t
*handle
= 0;
3096 handle
= reinterpret_cast<GEOSContextHandleInternal_t
*>(extHandle
);
3097 if ( 0 == handle
->initialized
)
3104 const GeometryFactory
*gf
= handle
->geomFactory
;
3106 return gf
->createLinearRing(cs
);
3108 catch (const std::exception
&e
)
3110 handle
->ERROR_MESSAGE("%s", e
.what());
3114 handle
->ERROR_MESSAGE("Unknown exception thrown");
3121 GEOSGeom_createEmptyLineString_r(GEOSContextHandle_t extHandle
)
3123 if ( 0 == extHandle
)
3128 GEOSContextHandleInternal_t
*handle
= 0;
3129 handle
= reinterpret_cast<GEOSContextHandleInternal_t
*>(extHandle
);
3130 if ( 0 == handle
->initialized
)
3137 const GeometryFactory
*gf
= handle
->geomFactory
;
3139 return gf
->createLineString();
3141 catch (const std::exception
&e
)
3143 handle
->ERROR_MESSAGE("%s", e
.what());
3147 handle
->ERROR_MESSAGE("Unknown exception thrown");
3154 GEOSGeom_createLineString_r(GEOSContextHandle_t extHandle
, CoordinateSequence
*cs
)
3156 if ( 0 == extHandle
)
3161 GEOSContextHandleInternal_t
*handle
= 0;
3162 handle
= reinterpret_cast<GEOSContextHandleInternal_t
*>(extHandle
);
3163 if ( 0 == handle
->initialized
)
3170 const GeometryFactory
*gf
= handle
->geomFactory
;
3172 return gf
->createLineString(cs
);
3174 catch (const std::exception
&e
)
3176 handle
->ERROR_MESSAGE("%s", e
.what());
3180 handle
->ERROR_MESSAGE("Unknown exception thrown");
3187 GEOSGeom_createEmptyPolygon_r(GEOSContextHandle_t extHandle
)
3189 if ( 0 == extHandle
)
3194 GEOSContextHandleInternal_t
*handle
= 0;
3195 handle
= reinterpret_cast<GEOSContextHandleInternal_t
*>(extHandle
);
3196 if ( 0 == handle
->initialized
)
3203 const GeometryFactory
*gf
= handle
->geomFactory
;
3204 return gf
->createPolygon();
3206 catch (const std::exception
&e
)
3208 handle
->ERROR_MESSAGE("%s", e
.what());
3212 handle
->ERROR_MESSAGE("Unknown exception thrown");
3219 GEOSGeom_createPolygon_r(GEOSContextHandle_t extHandle
, Geometry
*shell
, Geometry
**holes
, unsigned int nholes
)
3221 // FIXME: holes must be non-nullptr or may be nullptr?
3222 //assert(0 != holes);
3224 if ( 0 == extHandle
)
3229 GEOSContextHandleInternal_t
*handle
= 0;
3230 handle
= reinterpret_cast<GEOSContextHandleInternal_t
*>(extHandle
);
3231 if ( 0 == handle
->initialized
)
3238 using geos::geom::LinearRing
;
3240 std::vector
<Geometry
*> *vholes
= new std::vector
<Geometry
*>(holes
, holes
+ nholes
);
3242 LinearRing
*nshell
= dynamic_cast<LinearRing
*>(shell
);
3245 handle
->ERROR_MESSAGE("Shell is not a LinearRing");
3248 const GeometryFactory
*gf
= handle
->geomFactory
;
3250 return gf
->createPolygon(nshell
, vholes
);
3252 catch (const std::exception
&e
)
3254 handle
->ERROR_MESSAGE("%s", e
.what());
3258 handle
->ERROR_MESSAGE("Unknown exception thrown");
3265 GEOSGeom_clone_r(GEOSContextHandle_t extHandle
, const Geometry
*g
)
3269 if ( 0 == extHandle
)
3274 GEOSContextHandleInternal_t
*handle
= 0;
3275 handle
= reinterpret_cast<GEOSContextHandleInternal_t
*>(extHandle
);
3276 if ( 0 == handle
->initialized
)
3285 catch (const std::exception
&e
)
3287 handle
->ERROR_MESSAGE("%s", e
.what());
3291 handle
->ERROR_MESSAGE("Unknown exception thrown");
3298 GEOSGeom_getDimensions_r(GEOSContextHandle_t extHandle
, const Geometry
*g
)
3300 if ( 0 == extHandle
)
3305 GEOSContextHandleInternal_t
*handle
= 0;
3306 handle
= reinterpret_cast<GEOSContextHandleInternal_t
*>(extHandle
);
3307 if ( 0 == handle
->initialized
)
3314 using geos::geom::Point
;
3315 using geos::geom::GeometryCollection
;
3322 std::size_t dim
= 0;
3323 const LineString
*ls
= dynamic_cast<const LineString
*>(g
);
3326 dim
= ls
->getCoordinatesRO()->getDimension();
3327 return static_cast<int>(dim
);
3330 const Point
*p
= dynamic_cast<const Point
*>(g
);
3333 dim
= p
->getCoordinatesRO()->getDimension();
3334 return static_cast<int>(dim
);
3337 const Polygon
*poly
= dynamic_cast<const Polygon
*>(g
);
3340 return GEOSGeom_getDimensions_r(extHandle
, poly
->getExteriorRing());
3343 const GeometryCollection
*coll
= dynamic_cast<const GeometryCollection
*>(g
);
3346 return GEOSGeom_getDimensions_r(extHandle
, coll
->getGeometryN(0));
3349 handle
->ERROR_MESSAGE("Unknown geometry type");
3352 catch (const std::exception
&e
)
3354 handle
->ERROR_MESSAGE("%s", e
.what());
3358 handle
->ERROR_MESSAGE("Unknown exception thrown");
3365 GEOSSimplify_r(GEOSContextHandle_t extHandle
, const Geometry
*g1
, double tolerance
)
3367 if ( 0 == extHandle
)
3372 GEOSContextHandleInternal_t
*handle
= 0;
3373 handle
= reinterpret_cast<GEOSContextHandleInternal_t
*>(extHandle
);
3374 if ( 0 == handle
->initialized
)
3381 using namespace geos::simplify
;
3382 Geometry::AutoPtr
g(DouglasPeuckerSimplifier::simplify(g1
, tolerance
));
3385 catch (const std::exception
&e
)
3387 handle
->ERROR_MESSAGE("%s", e
.what());
3391 handle
->ERROR_MESSAGE("Unknown exception thrown");
3398 GEOSTopologyPreserveSimplify_r(GEOSContextHandle_t extHandle
, const Geometry
*g1
, double tolerance
)
3400 if ( 0 == extHandle
)
3405 GEOSContextHandleInternal_t
*handle
= 0;
3406 handle
= reinterpret_cast<GEOSContextHandleInternal_t
*>(extHandle
);
3407 if ( 0 == handle
->initialized
)
3414 using namespace geos::simplify
;
3415 Geometry::AutoPtr
g(TopologyPreservingSimplifier::simplify(g1
, tolerance
));
3418 catch (const std::exception
&e
)
3420 handle
->ERROR_MESSAGE("%s", e
.what());
3424 handle
->ERROR_MESSAGE("Unknown exception thrown");
3433 GEOSWKTReader_create_r(GEOSContextHandle_t extHandle
)
3435 if ( 0 == extHandle
)
3440 GEOSContextHandleInternal_t
*handle
= 0;
3441 handle
= reinterpret_cast<GEOSContextHandleInternal_t
*>(extHandle
);
3442 if ( 0 == handle
->initialized
)
3449 using geos::io::WKTReader
;
3450 return new WKTReader((GeometryFactory
*)handle
->geomFactory
);
3452 catch (const std::exception
&e
)
3454 handle
->ERROR_MESSAGE("%s", e
.what());
3458 handle
->ERROR_MESSAGE("Unknown exception thrown");
3465 GEOSWKTReader_destroy_r(GEOSContextHandle_t extHandle
, WKTReader
*reader
)
3467 GEOSContextHandleInternal_t
*handle
= 0;
3473 catch (const std::exception
&e
)
3475 if ( 0 == extHandle
)
3480 handle
= reinterpret_cast<GEOSContextHandleInternal_t
*>(extHandle
);
3481 if ( 0 == handle
->initialized
)
3486 handle
->ERROR_MESSAGE("%s", e
.what());
3490 if ( 0 == extHandle
)
3495 handle
= reinterpret_cast<GEOSContextHandleInternal_t
*>(extHandle
);
3496 if ( 0 == handle
->initialized
)
3501 handle
->ERROR_MESSAGE("Unknown exception thrown");
3507 GEOSWKTReader_read_r(GEOSContextHandle_t extHandle
, WKTReader
*reader
, const char *wkt
)
3509 assert(0 != reader
);
3511 if ( 0 == extHandle
)
3516 GEOSContextHandleInternal_t
*handle
= 0;
3517 handle
= reinterpret_cast<GEOSContextHandleInternal_t
*>(extHandle
);
3518 if ( 0 == handle
->initialized
)
3525 const std::string
wktstring(wkt
);
3526 Geometry
*g
= reader
->read(wktstring
);
3529 catch (const std::exception
&e
)
3531 handle
->ERROR_MESSAGE("%s", e
.what());
3535 handle
->ERROR_MESSAGE("Unknown exception thrown");
3543 GEOSWKTWriter_create_r(GEOSContextHandle_t extHandle
)
3545 if ( 0 == extHandle
)
3550 GEOSContextHandleInternal_t
*handle
= 0;
3551 handle
= reinterpret_cast<GEOSContextHandleInternal_t
*>(extHandle
);
3552 if ( 0 == handle
->initialized
)
3559 using geos::io::WKTWriter
;
3560 return new WKTWriter();
3562 catch (const std::exception
&e
)
3564 handle
->ERROR_MESSAGE("%s", e
.what());
3568 handle
->ERROR_MESSAGE("Unknown exception thrown");
3575 GEOSWKTWriter_destroy_r(GEOSContextHandle_t extHandle
, WKTWriter
*Writer
)
3578 GEOSContextHandleInternal_t
*handle
= 0;
3584 catch (const std::exception
&e
)
3586 if ( 0 == extHandle
)
3591 handle
= reinterpret_cast<GEOSContextHandleInternal_t
*>(extHandle
);
3592 if ( 0 == handle
->initialized
)
3597 handle
->ERROR_MESSAGE("%s", e
.what());
3601 if ( 0 == extHandle
)
3606 handle
= reinterpret_cast<GEOSContextHandleInternal_t
*>(extHandle
);
3607 if ( 0 == handle
->initialized
)
3612 handle
->ERROR_MESSAGE("Unknown exception thrown");
3618 GEOSWKTWriter_write_r(GEOSContextHandle_t extHandle
, WKTWriter
*writer
, const Geometry
*geom
)
3620 assert(0 != writer
);
3622 if ( 0 == extHandle
)
3627 GEOSContextHandleInternal_t
*handle
= 0;
3628 handle
= reinterpret_cast<GEOSContextHandleInternal_t
*>(extHandle
);
3629 if ( 0 == handle
->initialized
)
3636 std::string
sgeom(writer
->write(geom
));
3637 char *result
= gstrdup(sgeom
);
3640 catch (const std::exception
&e
)
3642 handle
->ERROR_MESSAGE("%s", e
.what());
3646 handle
->ERROR_MESSAGE("Unknown exception thrown");
3654 GEOSWKBReader_create_r(GEOSContextHandle_t extHandle
)
3656 if ( 0 == extHandle
)
3661 GEOSContextHandleInternal_t
*handle
= 0;
3662 handle
= reinterpret_cast<GEOSContextHandleInternal_t
*>(extHandle
);
3663 if ( 0 == handle
->initialized
)
3668 using geos::io::WKBReader
;
3671 return new WKBReader(*(GeometryFactory
*)handle
->geomFactory
);
3673 catch (const std::exception
&e
)
3675 handle
->ERROR_MESSAGE("%s", e
.what());
3679 handle
->ERROR_MESSAGE("Unknown exception thrown");
3686 GEOSWKBReader_destroy_r(GEOSContextHandle_t extHandle
, WKBReader
*reader
)
3688 GEOSContextHandleInternal_t
*handle
= 0;
3694 catch (const std::exception
&e
)
3696 if ( 0 == extHandle
)
3701 handle
= reinterpret_cast<GEOSContextHandleInternal_t
*>(extHandle
);
3702 if ( 0 == handle
->initialized
)
3707 handle
->ERROR_MESSAGE("%s", e
.what());
3711 if ( 0 == extHandle
)
3716 handle
= reinterpret_cast<GEOSContextHandleInternal_t
*>(extHandle
);
3717 if ( 0 == handle
->initialized
)
3722 handle
->ERROR_MESSAGE("Unknown exception thrown");
3728 GEOSWKBReader_read_r(GEOSContextHandle_t extHandle
, WKBReader
*reader
, const unsigned char *wkb
, size_t size
)
3730 assert(0 != reader
);
3733 if ( 0 == extHandle
)
3738 GEOSContextHandleInternal_t
*handle
= 0;
3739 handle
= reinterpret_cast<GEOSContextHandleInternal_t
*>(extHandle
);
3740 if ( 0 == handle
->initialized
)
3747 std::string
wkbstring(reinterpret_cast<const char*>(wkb
), size
); // make it binary !
3748 std::istringstream
is(std::ios_base::binary
);
3750 is
.seekg(0, std::ios::beg
); // rewind reader pointer
3752 Geometry
*g
= reader
->read(is
);
3755 catch (const std::exception
&e
)
3757 handle
->ERROR_MESSAGE("%s", e
.what());
3761 handle
->ERROR_MESSAGE("Unknown exception thrown");
3768 GEOSWKBReader_readHEX_r(GEOSContextHandle_t extHandle
, WKBReader
*reader
, const unsigned char *hex
, size_t size
)
3770 assert(0 != reader
);
3773 if ( 0 == extHandle
)
3778 GEOSContextHandleInternal_t
*handle
= 0;
3779 handle
= reinterpret_cast<GEOSContextHandleInternal_t
*>(extHandle
);
3780 if ( 0 == handle
->initialized
)
3787 std::string
hexstring(reinterpret_cast<const char*>(hex
), size
);
3788 std::istringstream
is(std::ios_base::binary
);
3790 is
.seekg(0, std::ios::beg
); // rewind reader pointer
3792 Geometry
*g
= reader
->readHEX(is
);
3795 catch (const std::exception
&e
)
3797 handle
->ERROR_MESSAGE("%s", e
.what());
3801 handle
->ERROR_MESSAGE("Unknown exception thrown");
3809 GEOSWKBWriter_create_r(GEOSContextHandle_t extHandle
)
3811 if ( 0 == extHandle
)
3816 GEOSContextHandleInternal_t
*handle
= 0;
3817 handle
= reinterpret_cast<GEOSContextHandleInternal_t
*>(extHandle
);
3818 if ( 0 == handle
->initialized
)
3825 using geos::io::WKBWriter
;
3826 return new WKBWriter();
3828 catch (const std::exception
&e
)
3830 handle
->ERROR_MESSAGE("%s", e
.what());
3834 handle
->ERROR_MESSAGE("Unknown exception thrown");
3841 GEOSWKBWriter_destroy_r(GEOSContextHandle_t extHandle
, WKBWriter
*Writer
)
3843 GEOSContextHandleInternal_t
*handle
= 0;
3849 catch (const std::exception
&e
)
3851 if ( 0 == extHandle
)
3856 handle
= reinterpret_cast<GEOSContextHandleInternal_t
*>(extHandle
);
3857 if ( 0 == handle
->initialized
)
3862 handle
->ERROR_MESSAGE("%s", e
.what());
3866 if ( 0 == extHandle
)
3871 handle
= reinterpret_cast<GEOSContextHandleInternal_t
*>(extHandle
);
3872 if ( 0 == handle
->initialized
)
3877 handle
->ERROR_MESSAGE("Unknown exception thrown");
3882 /* The caller owns the result */
3884 GEOSWKBWriter_write_r(GEOSContextHandle_t extHandle
, WKBWriter
*writer
, const Geometry
*geom
, size_t *size
)
3886 assert(0 != writer
);
3890 if ( 0 == extHandle
)
3895 GEOSContextHandleInternal_t
*handle
= 0;
3896 handle
= reinterpret_cast<GEOSContextHandleInternal_t
*>(extHandle
);
3897 if ( 0 == handle
->initialized
)
3904 std::ostringstream
os(std::ios_base::binary
);
3905 writer
->write(*geom
, os
);
3906 std::string
wkbstring(os
.str());
3907 const std::size_t len
= wkbstring
.length();
3909 unsigned char *result
= NULL
;
3910 result
= (unsigned char*) std::malloc(len
);
3911 std::memcpy(result
, wkbstring
.c_str(), len
);
3915 catch (const std::exception
&e
)
3917 handle
->ERROR_MESSAGE("%s", e
.what());
3921 handle
->ERROR_MESSAGE("Unknown exception thrown");
3926 /* The caller owns the result */
3928 GEOSWKBWriter_writeHEX_r(GEOSContextHandle_t extHandle
, WKBWriter
*writer
, const Geometry
*geom
, size_t *size
)
3930 assert(0 != writer
);
3934 if ( 0 == extHandle
)
3939 GEOSContextHandleInternal_t
*handle
= 0;
3940 handle
= reinterpret_cast<GEOSContextHandleInternal_t
*>(extHandle
);
3941 if ( 0 == handle
->initialized
)
3948 std::ostringstream
os(std::ios_base::binary
);
3949 writer
->writeHEX(*geom
, os
);
3950 std::string
wkbstring(os
.str());
3951 const std::size_t len
= wkbstring
.length();
3953 unsigned char *result
= NULL
;
3954 result
= (unsigned char*) std::malloc(len
);
3955 std::memcpy(result
, wkbstring
.c_str(), len
);
3959 catch (const std::exception
&e
)
3961 handle
->ERROR_MESSAGE("%s", e
.what());
3965 handle
->ERROR_MESSAGE("Unknown exception thrown");
3972 GEOSWKBWriter_getOutputDimension_r(GEOSContextHandle_t extHandle
, const GEOSWKBWriter
* writer
)
3974 assert(0 != writer
);
3976 if ( 0 == extHandle
)
3983 GEOSContextHandleInternal_t
*handle
= 0;
3984 handle
= reinterpret_cast<GEOSContextHandleInternal_t
*>(extHandle
);
3985 if ( 0 != handle
->initialized
)
3989 ret
= writer
->getOutputDimension();
3993 handle
->ERROR_MESSAGE("Unknown exception thrown");
4001 GEOSWKBWriter_setOutputDimension_r(GEOSContextHandle_t extHandle
, GEOSWKBWriter
* writer
, int newDimension
)
4003 assert(0 != writer
);
4005 if ( 0 == extHandle
)
4010 GEOSContextHandleInternal_t
*handle
= 0;
4011 handle
= reinterpret_cast<GEOSContextHandleInternal_t
*>(extHandle
);
4012 if ( 0 != handle
->initialized
)
4016 writer
->setOutputDimension(newDimension
);
4020 handle
->ERROR_MESSAGE("Unknown exception thrown");
4026 GEOSWKBWriter_getByteOrder_r(GEOSContextHandle_t extHandle
, const GEOSWKBWriter
* writer
)
4028 assert(0 != writer
);
4030 if ( 0 == extHandle
)
4037 GEOSContextHandleInternal_t
*handle
= 0;
4038 handle
= reinterpret_cast<GEOSContextHandleInternal_t
*>(extHandle
);
4039 if ( 0 != handle
->initialized
)
4043 ret
= writer
->getByteOrder();
4048 handle
->ERROR_MESSAGE("Unknown exception thrown");
4056 GEOSWKBWriter_setByteOrder_r(GEOSContextHandle_t extHandle
, GEOSWKBWriter
* writer
, int newByteOrder
)
4058 assert(0 != writer
);
4060 if ( 0 == extHandle
)
4065 GEOSContextHandleInternal_t
*handle
= 0;
4066 handle
= reinterpret_cast<GEOSContextHandleInternal_t
*>(extHandle
);
4067 if ( 0 != handle
->initialized
)
4071 writer
->setByteOrder(newByteOrder
);
4075 handle
->ERROR_MESSAGE("Unknown exception thrown");
4081 GEOSWKBWriter_getIncludeSRID_r(GEOSContextHandle_t extHandle
, const GEOSWKBWriter
* writer
)
4083 assert(0 != writer
);
4085 if ( 0 == extHandle
)
4092 GEOSContextHandleInternal_t
*handle
= 0;
4093 handle
= reinterpret_cast<GEOSContextHandleInternal_t
*>(extHandle
);
4094 if ( 0 != handle
->initialized
)
4098 int srid
= writer
->getIncludeSRID();
4099 ret
= static_cast<char>(srid
);
4103 handle
->ERROR_MESSAGE("Unknown exception thrown");
4107 return static_cast<char>(ret
);
4111 GEOSWKBWriter_setIncludeSRID_r(GEOSContextHandle_t extHandle
, GEOSWKBWriter
* writer
, const char newIncludeSRID
)
4113 assert(0 != writer
);
4115 if ( 0 == extHandle
)
4120 GEOSContextHandleInternal_t
*handle
= 0;
4121 handle
= reinterpret_cast<GEOSContextHandleInternal_t
*>(extHandle
);
4122 if ( 0 != handle
->initialized
)
4126 writer
->setIncludeSRID(newIncludeSRID
);
4130 handle
->ERROR_MESSAGE("Unknown exception thrown");
4136 //-----------------------------------------------------------------
4137 // Prepared Geometry
4138 //-----------------------------------------------------------------
4140 const geos::geom::prep::PreparedGeometry
*
4141 GEOSPrepare_r(GEOSContextHandle_t extHandle
, const Geometry
*g
)
4143 if ( 0 == extHandle
)
4148 GEOSContextHandleInternal_t
*handle
= 0;
4149 handle
= reinterpret_cast<GEOSContextHandleInternal_t
*>(extHandle
);
4150 if ( 0 == handle
->initialized
)
4155 const geos::geom::prep::PreparedGeometry
* prep
= 0;
4159 prep
= geos::geom::prep::PreparedGeometryFactory::prepare(g
);
4161 catch (const std::exception
&e
)
4163 handle
->ERROR_MESSAGE("%s", e
.what());
4167 handle
->ERROR_MESSAGE("Unknown exception thrown");
4174 GEOSPreparedGeom_destroy_r(GEOSContextHandle_t extHandle
, const geos::geom::prep::PreparedGeometry
*a
)
4176 GEOSContextHandleInternal_t
*handle
= 0;
4182 catch (const std::exception
&e
)
4184 if ( 0 == extHandle
)
4189 handle
= reinterpret_cast<GEOSContextHandleInternal_t
*>(extHandle
);
4190 if ( 0 == handle
->initialized
)
4195 handle
->ERROR_MESSAGE("%s", e
.what());
4199 if ( 0 == extHandle
)
4204 handle
= reinterpret_cast<GEOSContextHandleInternal_t
*>(extHandle
);
4205 if ( 0 == handle
->initialized
)
4210 handle
->ERROR_MESSAGE("Unknown exception thrown");
4215 GEOSPreparedContains_r(GEOSContextHandle_t extHandle
,
4216 const geos::geom::prep::PreparedGeometry
*pg
, const Geometry
*g
)
4221 if ( 0 == extHandle
)
4226 GEOSContextHandleInternal_t
*handle
= 0;
4227 handle
= reinterpret_cast<GEOSContextHandleInternal_t
*>(extHandle
);
4228 if ( 0 == handle
->initialized
)
4235 bool result
= pg
->contains(g
);
4238 catch (const std::exception
&e
)
4240 handle
->ERROR_MESSAGE("%s", e
.what());
4244 handle
->ERROR_MESSAGE("Unknown exception thrown");
4251 GEOSPreparedContainsProperly_r(GEOSContextHandle_t extHandle
,
4252 const geos::geom::prep::PreparedGeometry
*pg
, const Geometry
*g
)
4257 if ( 0 == extHandle
)
4262 GEOSContextHandleInternal_t
*handle
= 0;
4263 handle
= reinterpret_cast<GEOSContextHandleInternal_t
*>(extHandle
);
4264 if ( 0 == handle
->initialized
)
4271 bool result
= pg
->containsProperly(g
);
4274 catch (const std::exception
&e
)
4276 handle
->ERROR_MESSAGE("%s", e
.what());
4280 handle
->ERROR_MESSAGE("Unknown exception thrown");
4287 GEOSPreparedCovers_r(GEOSContextHandle_t extHandle
,
4288 const geos::geom::prep::PreparedGeometry
*pg
, const Geometry
*g
)
4293 if ( 0 == extHandle
)
4298 GEOSContextHandleInternal_t
*handle
= 0;
4299 handle
= reinterpret_cast<GEOSContextHandleInternal_t
*>(extHandle
);
4300 if ( 0 == handle
->initialized
)
4307 bool result
= pg
->covers(g
);
4310 catch (const std::exception
&e
)
4312 handle
->ERROR_MESSAGE("%s", e
.what());
4316 handle
->ERROR_MESSAGE("Unknown exception thrown");
4323 GEOSPreparedIntersects_r(GEOSContextHandle_t extHandle
,
4324 const geos::geom::prep::PreparedGeometry
*pg
, const Geometry
*g
)
4329 if ( 0 == extHandle
)
4334 GEOSContextHandleInternal_t
*handle
= 0;
4335 handle
= reinterpret_cast<GEOSContextHandleInternal_t
*>(extHandle
);
4336 if ( 0 == handle
->initialized
)
4343 bool result
= pg
->intersects(g
);
4346 catch (const std::exception
&e
)
4348 handle
->ERROR_MESSAGE("%s", e
.what());
4352 handle
->ERROR_MESSAGE("Unknown exception thrown");
4358 //-----------------------------------------------------------------
4360 //-----------------------------------------------------------------
4362 geos::index::strtree::STRtree
*
4363 GEOSSTRtree_create_r(GEOSContextHandle_t extHandle
,
4364 size_t nodeCapacity
)
4366 if ( 0 == extHandle
)
4371 GEOSContextHandleInternal_t
*handle
= 0;
4372 handle
= reinterpret_cast<GEOSContextHandleInternal_t
*>(extHandle
);
4373 if ( 0 == handle
->initialized
)
4378 geos::index::strtree::STRtree
*tree
= 0;
4382 tree
= new geos::index::strtree::STRtree(nodeCapacity
);
4384 catch (const std::exception
&e
)
4386 handle
->ERROR_MESSAGE("%s", e
.what());
4390 handle
->ERROR_MESSAGE("Unknown exception thrown");
4397 GEOSSTRtree_insert_r(GEOSContextHandle_t extHandle
,
4398 geos::index::strtree::STRtree
*tree
,
4399 const geos::geom::Geometry
*g
,
4402 GEOSContextHandleInternal_t
*handle
= 0;
4408 tree
->insert(g
->getEnvelopeInternal(), item
);
4410 catch (const std::exception
&e
)
4412 if ( 0 == extHandle
)
4417 handle
= reinterpret_cast<GEOSContextHandleInternal_t
*>(extHandle
);
4418 if ( 0 == handle
->initialized
)
4423 handle
->ERROR_MESSAGE("%s", e
.what());
4427 if ( 0 == extHandle
)
4432 handle
= reinterpret_cast<GEOSContextHandleInternal_t
*>(extHandle
);
4433 if ( 0 == handle
->initialized
)
4438 handle
->ERROR_MESSAGE("Unknown exception thrown");
4443 GEOSSTRtree_query_r(GEOSContextHandle_t extHandle
,
4444 geos::index::strtree::STRtree
*tree
,
4445 const geos::geom::Geometry
*g
,
4446 GEOSQueryCallback callback
,
4449 GEOSContextHandleInternal_t
*handle
= 0;
4452 assert(callback
!= 0);
4456 CAPI_ItemVisitor
visitor(callback
, userdata
);
4457 tree
->query(g
->getEnvelopeInternal(), visitor
);
4459 catch (const std::exception
&e
)
4461 if ( 0 == extHandle
)
4466 handle
= reinterpret_cast<GEOSContextHandleInternal_t
*>(extHandle
);
4467 if ( 0 == handle
->initialized
)
4472 handle
->ERROR_MESSAGE("%s", e
.what());
4476 if ( 0 == extHandle
)
4481 handle
= reinterpret_cast<GEOSContextHandleInternal_t
*>(extHandle
);
4482 if ( 0 == handle
->initialized
)
4487 handle
->ERROR_MESSAGE("Unknown exception thrown");
4492 GEOSSTRtree_iterate_r(GEOSContextHandle_t extHandle
,
4493 geos::index::strtree::STRtree
*tree
,
4494 GEOSQueryCallback callback
,
4497 GEOSContextHandleInternal_t
*handle
= 0;
4499 assert(callback
!= 0);
4503 CAPI_ItemVisitor
visitor(callback
, userdata
);
4504 tree
->iterate(visitor
);
4506 catch (const std::exception
&e
)
4508 if ( 0 == extHandle
)
4513 handle
= reinterpret_cast<GEOSContextHandleInternal_t
*>(extHandle
);
4514 if ( 0 == handle
->initialized
)
4519 handle
->ERROR_MESSAGE("%s", e
.what());
4523 if ( 0 == extHandle
)
4528 handle
= reinterpret_cast<GEOSContextHandleInternal_t
*>(extHandle
);
4529 if ( 0 == handle
->initialized
)
4534 handle
->ERROR_MESSAGE("Unknown exception thrown");
4539 GEOSSTRtree_remove_r(GEOSContextHandle_t extHandle
,
4540 geos::index::strtree::STRtree
*tree
,
4541 const geos::geom::Geometry
*g
,
4547 if ( 0 == extHandle
)
4552 GEOSContextHandleInternal_t
*handle
= 0;
4553 handle
= reinterpret_cast<GEOSContextHandleInternal_t
*>(extHandle
);
4554 if ( 0 == handle
->initialized
)
4561 bool result
= tree
->remove(g
->getEnvelopeInternal(), item
);
4564 catch (const std::exception
&e
)
4566 handle
->ERROR_MESSAGE("%s", e
.what());
4570 handle
->ERROR_MESSAGE("Unknown exception thrown");
4577 GEOSSTRtree_destroy_r(GEOSContextHandle_t extHandle
,
4578 geos::index::strtree::STRtree
*tree
)
4580 GEOSContextHandleInternal_t
*handle
= 0;
4586 catch (const std::exception
&e
)
4588 if ( 0 == extHandle
)
4593 handle
= reinterpret_cast<GEOSContextHandleInternal_t
*>(extHandle
);
4594 if ( 0 == handle
->initialized
)
4599 handle
->ERROR_MESSAGE("%s", e
.what());
4603 if ( 0 == extHandle
)
4608 handle
= reinterpret_cast<GEOSContextHandleInternal_t
*>(extHandle
);
4609 if ( 0 == handle
->initialized
)
4614 handle
->ERROR_MESSAGE("Unknown exception thrown");
4619 GEOSProject_r(GEOSContextHandle_t extHandle
,
4624 const geos::geom::Point
* point
= dynamic_cast<const geos::geom::Point
*>(p
);
4626 if ( 0 == extHandle
)
4630 GEOSContextHandleInternal_t
*handle
= reinterpret_cast<GEOSContextHandleInternal_t
*>(extHandle
);
4631 if ( 0 == handle
->initialized
)
4636 handle
->ERROR_MESSAGE("third argument of GEOSProject_r must be Point*");
4639 const geos::geom::Coordinate
* inputPt
= p
->getCoordinate();
4640 return geos::linearref::LengthIndexedLine(g
).project(*inputPt
);
4645 GEOSInterpolate_r(GEOSContextHandle_t extHandle
, const Geometry
*g
, double d
)
4647 geos::linearref::LengthIndexedLine
lil(g
);
4648 geos::geom::Coordinate coord
= lil
.extractPoint(d
);
4649 GEOSContextHandleInternal_t
*handle
= reinterpret_cast<GEOSContextHandleInternal_t
*>(extHandle
);
4650 const GeometryFactory
*gf
= handle
->geomFactory
;
4651 Geometry
* point
= gf
->createPoint(coord
);
4657 GEOSProjectNormalized_r(GEOSContextHandle_t extHandle
, const Geometry
*g
,
4662 GEOSLength_r(extHandle
, g
, &length
);
4663 return GEOSProject_r(extHandle
, g
, p
) / length
;
4668 GEOSInterpolateNormalized_r(GEOSContextHandle_t extHandle
, const Geometry
*g
,
4672 GEOSLength_r(extHandle
, g
, &length
);
4673 return GEOSInterpolate_r(extHandle
, g
, d
* length
);
4677 GEOSGeom_extractUniquePoints_r(GEOSContextHandle_t extHandle
,
4678 const GEOSGeometry
* g
)
4680 if ( 0 == extHandle
) return 0;
4681 GEOSContextHandleInternal_t
*handle
= 0;
4682 handle
= reinterpret_cast<GEOSContextHandleInternal_t
*>(extHandle
);
4683 if ( handle
->initialized
== 0 ) return 0;
4685 using namespace geos::geom
;
4686 using namespace geos::util
;
4687 using namespace std
;
4692 /* 1: extract points */
4693 vector
<const Coordinate
*> coords
;
4694 UniqueCoordinateArrayFilter
filter(coords
);
4695 g
->apply_ro(&filter
);
4697 /* 2: for each point, create a geometry and put into a vector */
4698 vector
<Geometry
*>* points
= new vector
<Geometry
*>();
4699 points
->reserve(coords
.size());
4700 const GeometryFactory
* factory
= g
->getFactory();
4701 for (vector
<const Coordinate
*>::iterator it
=coords
.begin(),
4705 Geometry
* point
= factory
->createPoint(*(*it
));
4706 points
->push_back(point
);
4709 /* 3: create a multipoint */
4710 return factory
->createMultiPoint(points
);
4713 catch (const std::exception
&e
)
4715 handle
->ERROR_MESSAGE("%s", e
.what());
4720 handle
->ERROR_MESSAGE("Unknown exception thrown");