GEOSGeom_extractUniquePoints [RT-SIGTA]
[geos.git] / capi / geos_ts_c.cpp
blobcadfbe91c39d564d2845a3ec4e6d816637604441
1 /************************************************************************
3 * $Id$
5 * C-Wrapper for GEOS library
7 * Copyright (C) 2010 Sandro Santilli <strk@keybit.net>
8 * Copyright (C) 2005-2006 Refractions Research Inc.
10 * This is free software; you can redistribute and/or modify it under
11 * the terms of the GNU Lesser General Public Licence as published
12 * by the Free Software Foundation.
13 * See the COPYING file for more information.
15 * Author: Sandro Santilli <strk@refractions.net>
16 * Thread Safety modifications: Chuck Thibert <charles.thibert@ingres.com>
18 ***********************************************************************/
20 #include <geos/platform.h> // for FINITE
21 #include <geos/geom/Geometry.h>
22 #include <geos/geom/prep/PreparedGeometry.h>
23 #include <geos/geom/prep/PreparedGeometryFactory.h>
24 #include <geos/geom/GeometryCollection.h>
25 #include <geos/geom/Polygon.h>
26 #include <geos/geom/Point.h>
27 #include <geos/geom/MultiPoint.h>
28 #include <geos/geom/MultiLineString.h>
29 #include <geos/geom/MultiPolygon.h>
30 #include <geos/geom/LinearRing.h>
31 #include <geos/geom/LineString.h>
32 #include <geos/geom/PrecisionModel.h>
33 #include <geos/geom/GeometryFactory.h>
34 #include <geos/geom/CoordinateSequenceFactory.h>
35 #include <geos/geom/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
63 #include <cstddef>
64 #include <cstdio>
65 #include <cstdlib>
66 #include <cstring>
67 #include <fstream>
68 #include <iostream>
69 #include <sstream>
70 #include <string>
71 #include <memory>
73 #ifdef _MSC_VER
74 #pragma warning(disable : 4099)
75 #endif
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
88 #include "geos_c.h"
90 // Intentional, to allow non-standard C elements like C99 functions to be
91 // imported through C++ headers of C library, like <cmath>.
92 using namespace std;
94 /// Define this if you want operations triggering Exceptions to
95 /// be printed.
96 /// (will use the NOTIFY channel - only implemented for GEOSUnion so far)
97 ///
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;
128 int WKBOutputDims;
129 int WKBByteOrder;
130 int initialized;
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
135 // extern "C" block.
136 class CAPI_ItemVisitor : public geos::index::ItemVisitor {
137 GEOSQueryCallback callback;
138 void *userdata;
139 public:
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);
151 extern "C" {
153 char* gstrdup_s(const char* str, const std::size_t size)
155 char* out = static_cast<char*>(std::malloc(size + 1));
156 if (0 != out)
158 // as no strlen call necessary, memcpy may be faster than strcpy
159 std::memcpy(out, str, size + 1);
162 assert(0 != out);
163 return out;
166 char* gstrdup(std::string const& str)
168 return gstrdup_s(str.c_str(), str.size());
172 GEOSContextHandle_t
173 initGEOS_r(GEOSMessageHandler nf, GEOSMessageHandler ef)
175 GEOSContextHandleInternal_t *handle = 0;
176 void *extHandle = 0;
178 extHandle = std::malloc(sizeof(GEOSContextHandleInternal_t));
179 if (0 != extHandle)
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);
193 GEOSMessageHandler
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 )
201 return NULL;
204 f = handle->NOTICE_MESSAGE;
205 handle->NOTICE_MESSAGE = nf;
207 return f;
210 GEOSMessageHandler
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 )
218 return NULL;
221 f = handle->ERROR_MESSAGE;
222 handle->ERROR_MESSAGE = nf;
224 return f;
227 void
228 finishGEOS_r(GEOSContextHandle_t extHandle)
230 // Fix up freeing handle w.r.t. malloc above
231 std::free(extHandle);
232 extHandle = NULL;
235 void
236 GEOSFree_r (GEOSContextHandle_t extHandle, void* buffer)
238 assert(0 != extHandle);
240 std::free(buffer);
243 //-----------------------------------------------------------
244 // relate()-related functions
245 // return 0 = false, 1 = true, 2 = error occured
246 //-----------------------------------------------------------
248 char
249 GEOSDisjoint_r(GEOSContextHandle_t extHandle, const Geometry *g1, const Geometry *g2)
251 if ( 0 == extHandle )
253 return 2;
256 GEOSContextHandleInternal_t *handle = 0;
257 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
258 if ( handle->initialized == 0 )
260 return 2;
265 bool result = g1->disjoint(g2);
266 return result;
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());
276 catch (...)
278 handle->ERROR_MESSAGE("Unknown exception thrown");
281 return 2;
284 char
285 GEOSTouches_r(GEOSContextHandle_t extHandle, const Geometry *g1, const Geometry *g2)
287 if ( 0 == extHandle )
289 return 2;
292 GEOSContextHandleInternal_t *handle = 0;
293 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
294 if ( 0 == handle->initialized )
296 return 2;
301 bool result = g1->touches(g2);
302 return result;
304 catch (const std::exception &e)
306 handle->ERROR_MESSAGE("%s", e.what());
308 catch (...)
310 handle->ERROR_MESSAGE("Unknown exception thrown");
313 return 2;
316 char
317 GEOSIntersects_r(GEOSContextHandle_t extHandle, const Geometry *g1, const Geometry *g2)
319 if ( 0 == extHandle )
321 return 2;
324 GEOSContextHandleInternal_t *handle = 0;
325 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
326 if ( 0 == handle->initialized )
328 return 2;
333 bool result = g1->intersects(g2);
334 return result;
336 catch (const std::exception &e)
338 handle->ERROR_MESSAGE("%s", e.what());
340 catch (...)
342 handle->ERROR_MESSAGE("Unknown exception thrown");
345 return 2;
348 char
349 GEOSCrosses_r(GEOSContextHandle_t extHandle, const Geometry *g1, const Geometry *g2)
351 if ( 0 == extHandle )
353 return 2;
356 GEOSContextHandleInternal_t *handle = 0;
357 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
358 if ( 0 == handle->initialized )
360 return 2;
365 bool result = g1->crosses(g2);
366 return result;
368 catch (const std::exception &e)
370 handle->ERROR_MESSAGE("%s", e.what());
372 catch (...)
374 handle->ERROR_MESSAGE("Unknown exception thrown");
377 return 2;
380 char
381 GEOSWithin_r(GEOSContextHandle_t extHandle, const Geometry *g1, const Geometry *g2)
383 if ( 0 == extHandle )
385 return 2;
388 GEOSContextHandleInternal_t *handle = 0;
389 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
390 if ( 0 == handle->initialized )
392 return 2;
397 bool result = g1->within(g2);
398 return result;
400 catch (const std::exception &e)
402 handle->ERROR_MESSAGE("%s", e.what());
404 catch (...)
406 handle->ERROR_MESSAGE("Unknown exception thrown");
409 return 2;
412 // call g1->contains(g2)
413 // returns 0 = false
414 // 1 = true
415 // 2 = error was trapped
416 char
417 GEOSContains_r(GEOSContextHandle_t extHandle, const Geometry *g1, const Geometry *g2)
419 if ( 0 == extHandle )
421 return 2;
424 GEOSContextHandleInternal_t *handle = 0;
425 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
426 if ( 0 == handle->initialized )
428 return 2;
433 bool result = g1->contains(g2);
434 return result;
436 catch (const std::exception &e)
438 handle->ERROR_MESSAGE("%s", e.what());
440 catch (...)
442 handle->ERROR_MESSAGE("Unknown exception thrown");
445 return 2;
448 char
449 GEOSOverlaps_r(GEOSContextHandle_t extHandle, const Geometry *g1, const Geometry *g2)
451 if ( 0 == extHandle )
453 return 2;
456 GEOSContextHandleInternal_t *handle = 0;
457 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
458 if ( 0 == handle->initialized )
460 return 2;
465 bool result = g1->overlaps(g2);
466 return result;
468 catch (const std::exception &e)
470 handle->ERROR_MESSAGE("%s", e.what());
472 catch (...)
474 handle->ERROR_MESSAGE("Unknown exception thrown");
477 return 2;
481 //-------------------------------------------------------------------
482 // low-level relate functions
483 //------------------------------------------------------------------
485 char
486 GEOSRelatePattern_r(GEOSContextHandle_t extHandle, const Geometry *g1, const Geometry *g2, const char *pat)
488 if ( 0 == extHandle )
490 return 2;
493 GEOSContextHandleInternal_t *handle = 0;
494 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
495 if ( 0 == handle->initialized )
497 return 2;
502 std::string s(pat);
503 bool result = g1->relate(g2, s);
504 return result;
506 catch (const std::exception &e)
508 handle->ERROR_MESSAGE("%s", e.what());
510 catch (...)
512 handle->ERROR_MESSAGE("Unknown exception thrown");
515 return 2;
518 char *
519 GEOSRelate_r(GEOSContextHandle_t extHandle, const Geometry *g1, const Geometry *g2)
521 if ( 0 == extHandle )
523 return NULL;
526 GEOSContextHandleInternal_t *handle = 0;
527 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
528 if ( 0 == handle->initialized )
530 return NULL;
535 using geos::geom::IntersectionMatrix;
537 IntersectionMatrix* im = g1->relate(g2);
538 if (0 == im)
540 return 0;
543 char *result = gstrdup(im->toString());
545 delete im;
546 im = 0;
548 return result;
550 catch (const std::exception &e)
552 handle->ERROR_MESSAGE("%s", e.what());
554 catch (...)
556 handle->ERROR_MESSAGE("Unknown exception thrown");
559 return NULL;
564 //-----------------------------------------------------------------
565 // isValid
566 //-----------------------------------------------------------------
569 char
570 GEOSisValid_r(GEOSContextHandle_t extHandle, const Geometry *g1)
572 if ( 0 == extHandle )
574 return 2;
577 GEOSContextHandleInternal_t *handle = 0;
578 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
579 if ( 0 == handle->initialized )
581 return 2;
586 using geos::operation::valid::IsValidOp;
587 using geos::operation::valid::TopologyValidationError;
589 IsValidOp ivo(g1);
590 bool valid = ivo.isValid();
591 if (!valid)
593 TopologyValidationError *err = ivo.getValidationError();
594 if ( err )
596 handle->NOTICE_MESSAGE("%s", err->toString().c_str());
599 return valid;
601 catch (const std::exception &e)
603 handle->ERROR_MESSAGE("%s", e.what());
605 catch (...)
607 handle->ERROR_MESSAGE("Unknown exception thrown");
610 return 2;
613 char *
614 GEOSisValidReason_r(GEOSContextHandle_t extHandle, const Geometry *g1)
616 if ( 0 == extHandle )
618 return NULL;
621 GEOSContextHandleInternal_t *handle = 0;
622 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
623 if ( 0 == handle->initialized )
625 return NULL;
630 using geos::operation::valid::IsValidOp;
631 using geos::operation::valid::TopologyValidationError;
633 char* result = 0;
634 char const* const validstr = "Valid Geometry";
636 IsValidOp ivo(g1);
637 bool isvalid = ivo.isValid();
638 if ( ! isvalid )
640 TopologyValidationError *err = ivo.getValidationError();
641 if (0 != err)
643 const std::string errloc = err->getCoordinate().toString();
644 std::string errmsg(err->getMessage());
645 errmsg += "[" + errloc + "]";
646 result = gstrdup(errmsg);
649 else
651 result = gstrdup(std::string(validstr));
654 return result;
656 catch (const std::exception &e)
658 handle->ERROR_MESSAGE("%s", e.what());
660 catch (...)
662 handle->ERROR_MESSAGE("Unknown exception thrown");
665 return 0;
668 char
669 GEOSisValidDetail_r(GEOSContextHandle_t extHandle, const Geometry *g,
670 char** reason, const Geometry ** location)
672 if ( 0 == extHandle )
674 return 0;
677 GEOSContextHandleInternal_t *handle = 0;
678 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
679 if ( 0 == handle->initialized )
681 return 0;
686 using geos::operation::valid::IsValidOp;
687 using geos::operation::valid::TopologyValidationError;
689 IsValidOp ivo(g);
690 bool isvalid = ivo.isValid();
691 if ( ! isvalid )
693 TopologyValidationError *err = ivo.getValidationError();
694 if (0 != err)
696 *location = handle->geomFactory->createPoint(err->getCoordinate());
697 std::string errmsg(err->getMessage());
698 *reason = gstrdup(errmsg);
700 else {
701 /* is it ever possible for getValidationError to be 0 ? */
703 return 0; /* invalid */
706 *location = 0;
707 *reason = 0;
708 return 1; /* valid */
711 catch (const std::exception &e)
713 handle->ERROR_MESSAGE("%s", e.what());
715 catch (...)
717 handle->ERROR_MESSAGE("Unknown exception thrown");
720 return 2; /* exception */
723 //-----------------------------------------------------------------
724 // general purpose
725 //-----------------------------------------------------------------
727 char
728 GEOSEquals_r(GEOSContextHandle_t extHandle, const Geometry *g1, const Geometry *g2)
730 if ( 0 == extHandle )
732 return 2;
735 GEOSContextHandleInternal_t *handle = 0;
736 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
737 if ( 0 == handle->initialized )
739 return 2;
744 bool result = g1->equals(g2);
745 return result;
747 catch (const std::exception &e)
749 handle->ERROR_MESSAGE("%s", e.what());
751 catch (...)
753 handle->ERROR_MESSAGE("Unknown exception thrown");
756 return 2;
759 char
760 GEOSEqualsExact_r(GEOSContextHandle_t extHandle, const Geometry *g1, const Geometry *g2, double tolerance)
762 if ( 0 == extHandle )
764 return 2;
767 GEOSContextHandleInternal_t *handle = 0;
768 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
769 if ( 0 == handle->initialized )
771 return 2;
776 bool result = g1->equalsExact(g2, tolerance);
777 return result;
779 catch (const std::exception &e)
781 handle->ERROR_MESSAGE("%s", e.what());
783 catch (...)
785 handle->ERROR_MESSAGE("Unknown exception thrown");
788 return 2;
792 GEOSDistance_r(GEOSContextHandle_t extHandle, const Geometry *g1, const Geometry *g2, double *dist)
794 assert(0 != dist);
796 if ( 0 == extHandle )
798 return 0;
801 GEOSContextHandleInternal_t *handle = 0;
802 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
803 if ( 0 == handle->initialized )
805 return 0;
810 *dist = g1->distance(g2);
811 return 1;
813 catch (const std::exception &e)
815 handle->ERROR_MESSAGE("%s", e.what());
817 catch (...)
819 handle->ERROR_MESSAGE("Unknown exception thrown");
822 return 0;
826 GEOSHausdorffDistance_r(GEOSContextHandle_t extHandle, const Geometry *g1, const Geometry *g2, double *dist)
828 assert(0 != dist);
830 if ( 0 == extHandle )
832 return 0;
835 GEOSContextHandleInternal_t *handle = 0;
836 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
837 if ( 0 == handle->initialized )
839 return 0;
844 *dist = DiscreteHausdorffDistance::distance(*g1, *g2);
845 return 1;
847 catch (const std::exception &e)
849 handle->ERROR_MESSAGE("%s", e.what());
851 catch (...)
853 handle->ERROR_MESSAGE("Unknown exception thrown");
856 return 0;
860 GEOSHausdorffDistanceDensify_r(GEOSContextHandle_t extHandle, const Geometry *g1, const Geometry *g2, double densifyFrac, double *dist)
862 assert(0 != dist);
864 if ( 0 == extHandle )
866 return 0;
869 GEOSContextHandleInternal_t *handle = 0;
870 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
871 if ( 0 == handle->initialized )
873 return 0;
878 *dist = DiscreteHausdorffDistance::distance(*g1, *g2, densifyFrac);
879 return 1;
881 catch (const std::exception &e)
883 handle->ERROR_MESSAGE("%s", e.what());
885 catch (...)
887 handle->ERROR_MESSAGE("Unknown exception thrown");
890 return 0;
894 GEOSArea_r(GEOSContextHandle_t extHandle, const Geometry *g, double *area)
896 assert(0 != area);
898 if ( 0 == extHandle )
900 return 0;
903 GEOSContextHandleInternal_t *handle = 0;
904 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
905 if ( 0 == handle->initialized )
907 return 0;
912 *area = g->getArea();
913 return 1;
915 catch (const std::exception &e)
917 handle->ERROR_MESSAGE("%s", e.what());
919 catch (...)
921 handle->ERROR_MESSAGE("Unknown exception thrown");
924 return 0;
928 GEOSLength_r(GEOSContextHandle_t extHandle, const Geometry *g, double *length)
930 assert(0 != length);
932 if ( 0 == extHandle )
934 return 2;
937 GEOSContextHandleInternal_t *handle = 0;
938 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
939 if ( 0 == handle->initialized )
941 return 0;
946 *length = g->getLength();
947 return 1;
949 catch (const std::exception &e)
951 handle->ERROR_MESSAGE("%s", e.what());
953 catch (...)
955 handle->ERROR_MESSAGE("Unknown exception thrown");
958 return 0;
961 Geometry *
962 GEOSGeomFromWKT_r(GEOSContextHandle_t extHandle, const char *wkt)
964 if ( 0 == extHandle )
966 return NULL;
969 GEOSContextHandleInternal_t *handle = 0;
970 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
971 if ( 0 == handle->initialized )
973 return NULL;
978 const std::string wktstring(wkt);
979 WKTReader r(static_cast<GeometryFactory const*>(handle->geomFactory));
981 Geometry *g = r.read(wktstring);
982 return g;
984 catch (const std::exception &e)
986 handle->ERROR_MESSAGE("%s", e.what());
988 catch (...)
990 handle->ERROR_MESSAGE("Unknown exception thrown");
993 return NULL;
996 char *
997 GEOSGeomToWKT_r(GEOSContextHandle_t extHandle, const Geometry *g1)
999 if ( 0 == extHandle )
1001 return NULL;
1004 GEOSContextHandleInternal_t *handle = 0;
1005 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
1006 if ( 0 == handle->initialized )
1008 return NULL;
1014 char *result = gstrdup(g1->toString());
1015 return result;
1017 catch (const std::exception &e)
1019 handle->ERROR_MESSAGE("%s", e.what());
1021 catch (...)
1023 handle->ERROR_MESSAGE("Unknown exception thrown");
1025 return NULL;
1028 // Remember to free the result!
1029 unsigned char *
1030 GEOSGeomToWKB_buf_r(GEOSContextHandle_t extHandle, const Geometry *g, size_t *size)
1032 assert(0 != size);
1034 if ( 0 == extHandle )
1036 return NULL;
1039 GEOSContextHandleInternal_t *handle = 0;
1040 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
1041 if ( 0 == handle->initialized )
1043 return NULL;
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);
1052 w.write(*g, os);
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));
1058 if (0 != result)
1060 std::memcpy(result, wkbstring.c_str(), len);
1061 *size = len;
1063 return result;
1065 catch (const std::exception &e)
1067 handle->ERROR_MESSAGE("%s", e.what());
1069 catch (...)
1071 handle->ERROR_MESSAGE("Unknown exception thrown");
1074 return NULL;
1077 Geometry *
1078 GEOSGeomFromWKB_buf_r(GEOSContextHandle_t extHandle, const unsigned char *wkb, size_t size)
1080 if ( 0 == extHandle )
1082 return NULL;
1085 GEOSContextHandleInternal_t *handle = 0;
1086 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
1087 if ( 0 == handle->initialized )
1089 return NULL;
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);
1098 is.str(wkbstring);
1099 is.seekg(0, std::ios::beg); // rewind reader pointer
1100 Geometry *g = r.read(is);
1101 return g;
1103 catch (const std::exception &e)
1105 handle->ERROR_MESSAGE("%s", e.what());
1107 catch (...)
1109 handle->ERROR_MESSAGE("Unknown exception thrown");
1112 return NULL;
1115 /* Read/write wkb hex values. Returned geometries are
1116 owned by the caller.*/
1117 unsigned char *
1118 GEOSGeomToHEX_buf_r(GEOSContextHandle_t extHandle, const Geometry *g, size_t *size)
1120 if ( 0 == extHandle )
1122 return NULL;
1125 GEOSContextHandleInternal_t *handle = 0;
1126 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
1127 if ( 0 == handle->initialized )
1129 return NULL;
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);
1138 w.writeHEX(*g, os);
1139 std::string hexstring(os.str());
1141 char *result = gstrdup(hexstring);
1142 if (0 != result)
1144 *size = hexstring.length();
1147 return reinterpret_cast<unsigned char*>(result);
1149 catch (const std::exception &e)
1151 handle->ERROR_MESSAGE("%s", e.what());
1153 catch (...)
1155 handle->ERROR_MESSAGE("Unknown exception thrown");
1158 return NULL;
1161 Geometry *
1162 GEOSGeomFromHEX_buf_r(GEOSContextHandle_t extHandle, const unsigned char *hex, size_t size)
1164 if ( 0 == extHandle )
1166 return NULL;
1169 GEOSContextHandleInternal_t *handle = 0;
1170 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
1171 if ( 0 == handle->initialized )
1173 return NULL;
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);
1182 is.str(hexstring);
1183 is.seekg(0, std::ios::beg); // rewind reader pointer
1185 Geometry *g = r.readHEX(is);
1186 return g;
1188 catch (const std::exception &e)
1190 handle->ERROR_MESSAGE("%s", e.what());
1192 catch (...)
1194 handle->ERROR_MESSAGE("Unknown exception thrown");
1197 return NULL;
1200 char
1201 GEOSisEmpty_r(GEOSContextHandle_t extHandle, const Geometry *g1)
1203 if ( 0 == extHandle )
1205 return 2;
1208 GEOSContextHandleInternal_t *handle = 0;
1209 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
1210 if ( 0 == handle->initialized )
1212 return 2;
1217 return g1->isEmpty();
1219 catch (const std::exception &e)
1221 handle->ERROR_MESSAGE("%s", e.what());
1223 catch (...)
1225 handle->ERROR_MESSAGE("Unknown exception thrown");
1228 return 2;
1231 char
1232 GEOSisSimple_r(GEOSContextHandle_t extHandle, const Geometry *g1)
1234 if ( 0 == extHandle )
1236 return 2;
1239 GEOSContextHandleInternal_t *handle = 0;
1240 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
1241 if ( 0 == handle->initialized )
1243 return 2;
1248 return g1->isSimple();
1250 catch (const std::exception &e)
1252 handle->ERROR_MESSAGE("%s", e.what());
1253 return 2;
1256 catch (...)
1258 handle->ERROR_MESSAGE("Unknown exception thrown");
1259 return 2;
1263 char
1264 GEOSisRing_r(GEOSContextHandle_t extHandle, const Geometry *g)
1266 if ( 0 == extHandle )
1268 return 2;
1271 GEOSContextHandleInternal_t *handle = 0;
1272 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
1273 if ( 0 == handle->initialized )
1275 return 2;
1280 const LineString *ls = dynamic_cast<const LineString *>(g);
1281 if ( ls ) {
1282 return (ls->isRing());
1283 } else {
1284 return 0;
1287 catch (const std::exception &e)
1289 handle->ERROR_MESSAGE("%s", e.what());
1290 return 2;
1293 catch (...)
1295 handle->ERROR_MESSAGE("Unknown exception thrown");
1296 return 2;
1302 //free the result of this
1303 char *
1304 GEOSGeomType_r(GEOSContextHandle_t extHandle, const Geometry *g1)
1306 if ( 0 == extHandle )
1308 return NULL;
1311 GEOSContextHandleInternal_t *handle = 0;
1312 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
1313 if ( 0 == handle->initialized )
1315 return NULL;
1320 std::string s = g1->getGeometryType();
1322 char *result = gstrdup(s);
1323 return result;
1325 catch (const std::exception &e)
1327 handle->ERROR_MESSAGE("%s", e.what());
1329 catch (...)
1331 handle->ERROR_MESSAGE("Unknown exception thrown");
1334 return NULL;
1337 // Return postgis geometry type index
1339 GEOSGeomTypeId_r(GEOSContextHandle_t extHandle, const Geometry *g1)
1341 if ( 0 == extHandle )
1343 return -1;
1346 GEOSContextHandleInternal_t *handle = 0;
1347 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
1348 if ( 0 == handle->initialized )
1350 return -1;
1355 return g1->getGeometryTypeId();
1357 catch (const std::exception &e)
1359 handle->ERROR_MESSAGE("%s", e.what());
1361 catch (...)
1363 handle->ERROR_MESSAGE("Unknown exception thrown");
1366 return -1;
1369 //-------------------------------------------------------------------
1370 // GEOS functions that return geometries
1371 //-------------------------------------------------------------------
1373 Geometry *
1374 GEOSEnvelope_r(GEOSContextHandle_t extHandle, const Geometry *g1)
1376 if ( 0 == extHandle )
1378 return NULL;
1381 GEOSContextHandleInternal_t *handle = 0;
1382 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
1383 if ( 0 == handle->initialized )
1385 return NULL;
1390 Geometry *g3 = g1->getEnvelope();
1391 return g3;
1393 catch (const std::exception &e)
1395 handle->ERROR_MESSAGE("%s", e.what());
1397 catch (...)
1399 handle->ERROR_MESSAGE("Unknown exception thrown");
1402 return NULL;
1405 Geometry *
1406 GEOSIntersection_r(GEOSContextHandle_t extHandle, const Geometry *g1, const Geometry *g2)
1408 if ( 0 == extHandle )
1410 return NULL;
1413 GEOSContextHandleInternal_t *handle = 0;
1414 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
1415 if ( 0 == handle->initialized )
1417 return NULL;
1422 GeomAutoPtr g3(BinaryOp(g1, g2, overlayOp(OverlayOp::opINTERSECTION)));
1423 return g3.release();
1425 // XXX: old version
1426 //Geometry *g3 = g1->intersection(g2);
1427 //return g3;
1429 catch (const std::exception &e)
1431 handle->ERROR_MESSAGE("%s", e.what());
1433 catch (...)
1435 handle->ERROR_MESSAGE("Unknown exception thrown");
1438 return NULL;
1441 Geometry *
1442 GEOSBuffer_r(GEOSContextHandle_t extHandle, const Geometry *g1, double width, int quadrantsegments)
1444 if ( 0 == extHandle )
1446 return NULL;
1449 GEOSContextHandleInternal_t *handle = 0;
1450 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
1451 if ( 0 == handle->initialized )
1453 return NULL;
1458 Geometry *g3 = g1->buffer(width, quadrantsegments);
1459 return g3;
1461 catch (const std::exception &e)
1463 handle->ERROR_MESSAGE("%s", e.what());
1465 catch (...)
1467 handle->ERROR_MESSAGE("Unknown exception thrown");
1470 return NULL;
1473 Geometry *
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 )
1482 return NULL;
1485 GEOSContextHandleInternal_t *handle = 0;
1486 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
1487 if ( 0 == handle->initialized )
1489 return NULL;
1494 BufferParameters bp;
1495 bp.setQuadrantSegments(quadsegs);
1497 if ( endCapStyle > BufferParameters::CAP_SQUARE )
1499 throw IllegalArgumentException("Invalid buffer endCap style");
1501 bp.setEndCapStyle(
1502 static_cast<BufferParameters::EndCapStyle>(endCapStyle)
1505 if ( joinStyle > BufferParameters::JOIN_BEVEL )
1507 throw IllegalArgumentException("Invalid buffer join style");
1509 bp.setJoinStyle(
1510 static_cast<BufferParameters::JoinStyle>(joinStyle)
1512 bp.setMitreLimit(mitreLimit);
1513 BufferOp op(g1, bp);
1514 Geometry *g3 = op.getResultGeometry(width);
1515 return g3;
1517 catch (const std::exception &e)
1519 handle->ERROR_MESSAGE("%s", e.what());
1521 catch (...)
1523 handle->ERROR_MESSAGE("Unknown exception thrown");
1526 return NULL;
1529 Geometry *
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 )
1539 return NULL;
1542 GEOSContextHandleInternal_t *handle = 0;
1543 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
1544 if ( 0 == handle->initialized )
1546 return NULL;
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");
1559 bp.setJoinStyle(
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);
1568 return g3;
1570 catch (const std::exception &e)
1572 handle->ERROR_MESSAGE("%s", e.what());
1574 catch (...)
1576 handle->ERROR_MESSAGE("Unknown exception thrown");
1579 return NULL;
1582 Geometry *
1583 GEOSConvexHull_r(GEOSContextHandle_t extHandle, const Geometry *g1)
1585 if ( 0 == extHandle )
1587 return NULL;
1590 GEOSContextHandleInternal_t *handle = 0;
1591 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
1592 if ( 0 == handle->initialized )
1594 return NULL;
1599 Geometry *g3 = g1->convexHull();
1600 return g3;
1602 catch (const std::exception &e)
1604 handle->ERROR_MESSAGE("%s", e.what());
1606 catch (...)
1608 handle->ERROR_MESSAGE("Unknown exception thrown");
1611 return NULL;
1614 Geometry *
1615 GEOSDifference_r(GEOSContextHandle_t extHandle, const Geometry *g1, const Geometry *g2)
1617 if ( 0 == extHandle )
1619 return NULL;
1622 GEOSContextHandleInternal_t *handle = 0;
1623 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
1624 if ( 0 == handle->initialized )
1626 return NULL;
1631 GeomAutoPtr g3(BinaryOp(g1, g2, overlayOp(OverlayOp::opDIFFERENCE)));
1632 return g3.release();
1634 // XXX: old version
1635 //Geometry *g3 = g1->difference(g2);
1636 //return g3;
1638 catch (const std::exception &e)
1640 handle->ERROR_MESSAGE("%s", e.what());
1642 catch (...)
1644 handle->ERROR_MESSAGE("Unknown exception thrown");
1647 return NULL;
1650 Geometry *
1651 GEOSBoundary_r(GEOSContextHandle_t extHandle, const Geometry *g1)
1653 if ( 0 == extHandle )
1655 return NULL;
1658 GEOSContextHandleInternal_t *handle = 0;
1659 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
1660 if ( 0 == handle->initialized )
1662 return NULL;
1667 Geometry *g3 = g1->getBoundary();
1668 return g3;
1670 catch (const std::exception &e)
1672 handle->ERROR_MESSAGE("%s", e.what());
1674 catch (...)
1676 handle->ERROR_MESSAGE("Unknown exception thrown");
1679 return NULL;
1682 Geometry *
1683 GEOSSymDifference_r(GEOSContextHandle_t extHandle, const Geometry *g1, const Geometry *g2)
1685 if ( 0 == extHandle )
1687 return NULL;
1690 GEOSContextHandleInternal_t *handle = 0;
1691 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
1692 if ( 0 == handle->initialized )
1694 return NULL;
1699 GeomAutoPtr g3 = BinaryOp(g1, g2, overlayOp(OverlayOp::opSYMDIFFERENCE));
1700 return g3.release();
1701 //Geometry *g3 = g1->symDifference(g2);
1702 //return g3;
1704 catch (const std::exception &e)
1706 handle->ERROR_MESSAGE("%s", e.what());
1707 return NULL;
1710 catch (...)
1712 handle->ERROR_MESSAGE("Unknown exception thrown");
1713 return NULL;
1717 Geometry *
1718 GEOSUnion_r(GEOSContextHandle_t extHandle, const Geometry *g1, const Geometry *g2)
1720 if ( 0 == extHandle )
1722 return NULL;
1725 GEOSContextHandleInternal_t *handle = 0;
1726 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
1727 if ( 0 == handle->initialized )
1729 return NULL;
1734 GeomAutoPtr g3 = BinaryOp(g1, g2, overlayOp(OverlayOp::opUNION));
1735 return g3.release();
1737 // XXX: old version
1738 //Geometry *g3 = g1->Union(g2);
1739 //return g3;
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());
1752 catch (...)
1754 handle->ERROR_MESSAGE("Unknown exception thrown");
1757 return NULL;
1760 Geometry *
1761 GEOSUnionCascaded_r(GEOSContextHandle_t extHandle, const Geometry *g1)
1763 if ( 0 == extHandle )
1765 return NULL;
1768 GEOSContextHandleInternal_t *handle = 0;
1769 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
1770 if ( 0 == handle->initialized )
1772 return NULL;
1777 const geos::geom::MultiPolygon *p = dynamic_cast<const geos::geom::MultiPolygon *>(g1);
1778 if ( ! p )
1780 handle->ERROR_MESSAGE("Invalid argument (must be a MultiPolygon)");
1781 return NULL;
1784 using geos::operation::geounion::CascadedPolygonUnion;
1785 return CascadedPolygonUnion::Union(p);
1787 catch (const std::exception &e)
1789 handle->ERROR_MESSAGE("%s", e.what());
1791 catch (...)
1793 handle->ERROR_MESSAGE("Unknown exception thrown");
1796 return NULL;
1799 Geometry *
1800 GEOSPointOnSurface_r(GEOSContextHandle_t extHandle, const Geometry *g1)
1802 if ( 0 == extHandle )
1804 return NULL;
1807 GEOSContextHandleInternal_t *handle = 0;
1808 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
1809 if ( 0 == handle->initialized )
1811 return NULL;
1816 Geometry *ret = g1->getInteriorPoint();
1817 if ( ! ret )
1819 const GeometryFactory* gf = handle->geomFactory;
1820 // return an empty collection
1821 return gf->createGeometryCollection();
1823 return ret;
1825 catch (const std::exception &e)
1827 handle->ERROR_MESSAGE("%s", e.what());
1829 catch (...)
1831 handle->ERROR_MESSAGE("Unknown exception thrown");
1834 return NULL;
1837 //-------------------------------------------------------------------
1838 // memory management functions
1839 //------------------------------------------------------------------
1841 void
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
1852 delete a;
1854 catch (const std::exception &e)
1856 if ( 0 == extHandle )
1858 return;
1861 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
1862 if ( 0 == handle->initialized )
1864 return;
1867 handle->ERROR_MESSAGE("%s", e.what());
1869 catch (...)
1871 if ( 0 == extHandle )
1873 return;
1876 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
1877 if ( 0 == handle->initialized )
1879 return;
1882 handle->ERROR_MESSAGE("Unknown exception thrown");
1886 void
1887 GEOSSetSRID_r(GEOSContextHandle_t extHandle, Geometry *g, int srid)
1889 assert(0 != g);
1891 if ( 0 == extHandle )
1893 return;
1896 GEOSContextHandleInternal_t *handle = 0;
1897 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
1898 if ( 0 == handle->initialized )
1900 return;
1903 g->setSRID(srid);
1908 GEOSGetNumCoordinates_r(GEOSContextHandle_t extHandle, const Geometry *g)
1910 assert(0 != g);
1912 if ( 0 == extHandle )
1914 return -1;
1917 GEOSContextHandleInternal_t *handle = 0;
1918 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
1919 if ( 0 == handle->initialized )
1921 return -1;
1926 return static_cast<int>(g->getNumPoints());
1928 catch (const std::exception &e)
1930 handle->ERROR_MESSAGE("%s", e.what());
1932 catch (...)
1934 handle->ERROR_MESSAGE("Unknown exception thrown");
1937 return -1;
1941 * Return -1 on exception, 0 otherwise.
1942 * Converts Geometry to normal form (or canonical form).
1945 GEOSNormalize_r(GEOSContextHandle_t extHandle, Geometry *g)
1947 assert(0 != g);
1949 if ( 0 == extHandle )
1951 return -1;
1954 GEOSContextHandleInternal_t *handle = 0;
1955 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
1956 if ( 0 == handle->initialized )
1958 return -1;
1963 g->normalize();
1964 return 0; // SUCCESS
1966 catch (const std::exception &e)
1968 handle->ERROR_MESSAGE("%s", e.what());
1970 catch (...)
1972 handle->ERROR_MESSAGE("Unknown exception thrown");
1975 return -1;
1979 GEOSGetNumInteriorRings_r(GEOSContextHandle_t extHandle, const Geometry *g1)
1981 if ( 0 == extHandle )
1983 return -1;
1986 GEOSContextHandleInternal_t *handle = 0;
1987 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
1988 if ( 0 == handle->initialized )
1990 return -1;
1995 const Polygon *p = dynamic_cast<const Polygon *>(g1);
1996 if ( ! p )
1998 handle->ERROR_MESSAGE("Argument is not a Polygon");
1999 return -1;
2001 return static_cast<int>(p->getNumInteriorRing());
2003 catch (const std::exception &e)
2005 handle->ERROR_MESSAGE("%s", e.what());
2007 catch (...)
2009 handle->ERROR_MESSAGE("Unknown exception thrown");
2012 return -1;
2016 // returns -1 on error and 1 for non-multi geometries
2018 GEOSGetNumGeometries_r(GEOSContextHandle_t extHandle, const Geometry *g1)
2020 if ( 0 == extHandle )
2022 return -1;
2025 GEOSContextHandleInternal_t *handle = 0;
2026 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
2027 if ( 0 == handle->initialized )
2029 return -1;
2034 return static_cast<int>(g1->getNumGeometries());
2036 catch (const std::exception &e)
2038 handle->ERROR_MESSAGE("%s", e.what());
2040 catch (...)
2042 handle->ERROR_MESSAGE("Unknown exception thrown");
2045 return -1;
2050 * Call only on GEOMETRYCOLLECTION or MULTI*.
2051 * Return a pointer to the internal Geometry.
2053 const Geometry *
2054 GEOSGetGeometryN_r(GEOSContextHandle_t extHandle, const Geometry *g1, int n)
2056 if ( 0 == extHandle )
2058 return NULL;
2061 GEOSContextHandleInternal_t *handle = 0;
2062 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
2063 if ( 0 == handle->initialized )
2065 return NULL;
2070 using geos::geom::GeometryCollection;
2071 const GeometryCollection *gc = dynamic_cast<const GeometryCollection *>(g1);
2072 if ( ! gc )
2074 handle->ERROR_MESSAGE("Argument is not a GeometryCollection");
2075 return NULL;
2077 return gc->getGeometryN(n);
2079 catch (const std::exception &e)
2081 handle->ERROR_MESSAGE("%s", e.what());
2083 catch (...)
2085 handle->ERROR_MESSAGE("Unknown exception thrown");
2088 return NULL;
2093 * Call only on polygon
2094 * Return a copy of the internal Geometry.
2096 const Geometry *
2097 GEOSGetExteriorRing_r(GEOSContextHandle_t extHandle, const Geometry *g1)
2099 if ( 0 == extHandle )
2101 return NULL;
2104 GEOSContextHandleInternal_t *handle = 0;
2105 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
2106 if ( 0 == handle->initialized )
2108 return NULL;
2113 const Polygon *p = dynamic_cast<const Polygon *>(g1);
2114 if ( ! p )
2116 handle->ERROR_MESSAGE("Invalid argument (must be a Polygon)");
2117 return NULL;
2119 return p->getExteriorRing();
2121 catch (const std::exception &e)
2123 handle->ERROR_MESSAGE("%s", e.what());
2125 catch (...)
2127 handle->ERROR_MESSAGE("Unknown exception thrown");
2130 return NULL;
2134 * Call only on polygon
2135 * Return a pointer to internal storage, do not destroy it.
2137 const Geometry *
2138 GEOSGetInteriorRingN_r(GEOSContextHandle_t extHandle, const Geometry *g1, int n)
2140 if ( 0 == extHandle )
2142 return NULL;
2145 GEOSContextHandleInternal_t *handle = 0;
2146 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
2147 if ( 0 == handle->initialized )
2149 return NULL;
2154 const Polygon *p = dynamic_cast<const Polygon *>(g1);
2155 if ( ! p )
2157 handle->ERROR_MESSAGE("Invalid argument (must be a Polygon)");
2158 return NULL;
2160 return p->getInteriorRingN(n);
2162 catch (const std::exception &e)
2164 handle->ERROR_MESSAGE("%s", e.what());
2166 catch (...)
2168 handle->ERROR_MESSAGE("Unknown exception thrown");
2171 return NULL;
2174 Geometry *
2175 GEOSGetCentroid_r(GEOSContextHandle_t extHandle, const Geometry *g)
2177 if ( 0 == extHandle )
2179 return NULL;
2182 GEOSContextHandleInternal_t *handle = 0;
2183 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
2184 if ( 0 == handle->initialized )
2186 return NULL;
2191 Geometry *ret = g->getCentroid();
2192 if (0 == ret)
2194 const GeometryFactory *gf = handle->geomFactory;
2195 return gf->createGeometryCollection();
2197 return ret;
2199 catch (const std::exception &e)
2201 handle->ERROR_MESSAGE("%s", e.what());
2203 catch (...)
2205 handle->ERROR_MESSAGE("Unknown exception thrown");
2208 return NULL;
2211 Geometry *
2212 GEOSGeom_createEmptyCollection_r(GEOSContextHandle_t extHandle, int type)
2214 if ( 0 == extHandle )
2216 return NULL;
2219 GEOSContextHandleInternal_t *handle = 0;
2220 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
2221 if ( 0 == handle->initialized )
2223 return NULL;
2226 #ifdef GEOS_DEBUG
2227 char buf[256];
2228 sprintf(buf, "createCollection: requested type %d, ngeoms: %d",
2229 type, ngeoms);
2230 handle->NOTICE_MESSAGE("%s", buf);// TODO: Can handle->NOTICE_MESSAGE format that directly?
2231 #endif
2235 const GeometryFactory* gf = handle->geomFactory;
2237 Geometry *g = 0;
2238 switch (type)
2240 case GEOS_GEOMETRYCOLLECTION:
2241 g = gf->createGeometryCollection();
2242 break;
2243 case GEOS_MULTIPOINT:
2244 g = gf->createMultiPoint();
2245 break;
2246 case GEOS_MULTILINESTRING:
2247 g = gf->createMultiLineString();
2248 break;
2249 case GEOS_MULTIPOLYGON:
2250 g = gf->createMultiPolygon();
2251 break;
2252 default:
2253 handle->ERROR_MESSAGE("Unsupported type request for GEOSGeom_createEmptyCollection_r");
2254 g = 0;
2258 return g;
2260 catch (const std::exception &e)
2262 handle->ERROR_MESSAGE("%s", e.what());
2264 catch (...)
2266 handle->ERROR_MESSAGE("Unknown exception thrown");
2269 return 0;
2272 Geometry *
2273 GEOSGeom_createCollection_r(GEOSContextHandle_t extHandle, int type, Geometry **geoms, unsigned int ngeoms)
2275 if ( 0 == extHandle )
2277 return NULL;
2280 GEOSContextHandleInternal_t *handle = 0;
2281 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
2282 if ( 0 == handle->initialized )
2284 return NULL;
2287 #ifdef GEOS_DEBUG
2288 char buf[256];
2289 sprintf(buf, "PostGIS2GEOS_collection: requested type %d, ngeoms: %d",
2290 type, ngeoms);
2291 handle->NOTICE_MESSAGE("%s", buf);// TODO: Can handle->NOTICE_MESSAGE format that directly?
2292 #endif
2296 const GeometryFactory* gf = handle->geomFactory;
2297 std::vector<Geometry*>* vgeoms = new std::vector<Geometry*>(geoms, geoms + ngeoms);
2299 Geometry *g = 0;
2300 switch (type)
2302 case GEOS_GEOMETRYCOLLECTION:
2303 g = gf->createGeometryCollection(vgeoms);
2304 break;
2305 case GEOS_MULTIPOINT:
2306 g = gf->createMultiPoint(vgeoms);
2307 break;
2308 case GEOS_MULTILINESTRING:
2309 g = gf->createMultiLineString(vgeoms);
2310 break;
2311 case GEOS_MULTIPOLYGON:
2312 g = gf->createMultiPolygon(vgeoms);
2313 break;
2314 default:
2315 handle->ERROR_MESSAGE("Unsupported type request for PostGIS2GEOS_collection");
2316 g = 0;
2320 return g;
2322 catch (const std::exception &e)
2324 handle->ERROR_MESSAGE("%s", e.what());
2326 catch (...)
2328 handle->ERROR_MESSAGE("Unknown exception thrown");
2331 return 0;
2334 Geometry *
2335 GEOSPolygonize_r(GEOSContextHandle_t extHandle, const Geometry * const * g, unsigned int ngeoms)
2337 if ( 0 == extHandle )
2339 return 0;
2342 GEOSContextHandleInternal_t *handle = 0;
2343 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
2344 if ( 0 == handle->initialized )
2346 return 0;
2349 Geometry *out = 0;
2353 // Polygonize
2354 using geos::operation::polygonize::Polygonizer;
2355 Polygonizer plgnzr;
2356 for (std::size_t i = 0; i < ngeoms; ++i)
2358 plgnzr.add(g[i]);
2361 #if GEOS_DEBUG
2362 handle->NOTICE_MESSAGE("geometry vector added to polygonizer");
2363 #endif
2365 std::vector<Polygon*> *polys = plgnzr.getPolygons();
2366 assert(0 != polys);
2368 #if GEOS_DEBUG
2369 handle->NOTICE_MESSAGE("output polygons got");
2370 #endif
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];
2387 delete polys;
2388 polys = 0;
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());
2400 catch (...)
2402 handle->ERROR_MESSAGE("Unknown exception thrown");
2405 return out;
2408 Geometry *
2409 GEOSPolygonizer_getCutEdges_r(GEOSContextHandle_t extHandle, const Geometry * const * g, unsigned int ngeoms)
2411 if ( 0 == extHandle )
2413 return 0;
2416 GEOSContextHandleInternal_t *handle = 0;
2417 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
2418 if ( 0 == handle->initialized )
2420 return 0;
2423 Geometry *out = 0;
2427 // Polygonize
2428 using geos::operation::polygonize::Polygonizer;
2429 Polygonizer plgnzr;
2430 for (std::size_t i = 0; i < ngeoms; ++i)
2432 plgnzr.add(g[i]);
2435 #if GEOS_DEBUG
2436 handle->NOTICE_MESSAGE("geometry vector added to polygonizer");
2437 #endif
2439 std::vector<const LineString *>* lines = plgnzr.getCutEdges();
2440 assert(0 != lines);
2442 #if GEOS_DEBUG
2443 handle->NOTICE_MESSAGE("output polygons got");
2444 #endif
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());
2469 catch (...)
2471 handle->ERROR_MESSAGE("Unknown exception thrown");
2474 return out;
2477 Geometry *
2478 GEOSLineMerge_r(GEOSContextHandle_t extHandle, const Geometry *g)
2480 if ( 0 == extHandle )
2482 return 0;
2485 GEOSContextHandleInternal_t *handle = 0;
2486 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
2487 if ( 0 == handle->initialized )
2489 return 0;
2492 Geometry *out = 0;
2496 using geos::operation::linemerge::LineMerger;
2497 LineMerger lmrgr;
2498 lmrgr.add(g);
2500 std::vector<LineString *>* lines = lmrgr.getMergedLineStrings();
2501 assert(0 != lines);
2503 #if GEOS_DEBUG
2504 handle->NOTICE_MESSAGE("output lines got");
2505 #endif
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];
2512 delete lines;
2513 lines = 0;
2515 const GeometryFactory *gf = handle->geomFactory;
2516 out = gf->buildGeometry(geoms);
2518 // XXX: old version
2519 //out = gf->createGeometryCollection(geoms);
2521 catch (const std::exception &e)
2523 handle->ERROR_MESSAGE("%s", e.what());
2525 catch (...)
2527 handle->ERROR_MESSAGE("Unknown exception thrown");
2530 return out;
2534 GEOSGetSRID_r(GEOSContextHandle_t extHandle, const Geometry *g)
2536 assert(0 != g);
2538 if ( 0 == extHandle )
2540 return 0;
2543 GEOSContextHandleInternal_t *handle = 0;
2544 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
2545 if ( 0 == handle->initialized )
2547 return 0;
2552 return g->getSRID();
2554 catch (const std::exception &e)
2556 handle->ERROR_MESSAGE("%s", e.what());
2558 catch (...)
2560 handle->ERROR_MESSAGE("Unknown exception thrown");
2563 return 0;
2566 const char* GEOSversion()
2568 return GEOS_CAPI_VERSION;
2571 const char* GEOSjtsport()
2573 return GEOS_JTS_PORT;
2576 char
2577 GEOSHasZ_r(GEOSContextHandle_t extHandle, const Geometry *g)
2579 assert(0 != g);
2581 if ( 0 == extHandle )
2583 return -1;
2586 GEOSContextHandleInternal_t *handle = 0;
2587 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
2588 if ( 0 == handle->initialized )
2590 return -1;
2593 if (g->isEmpty())
2595 return false;
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 )
2610 return -1;
2613 GEOSContextHandleInternal_t *handle = 0;
2614 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
2615 if ( 0 == handle->initialized )
2617 return -1;
2620 return handle->WKBOutputDims;
2624 GEOS_setWKBOutputDims_r(GEOSContextHandle_t extHandle, int newdims)
2626 if ( 0 == extHandle )
2628 return -1;
2631 GEOSContextHandleInternal_t *handle = 0;
2632 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
2633 if ( 0 == handle->initialized )
2635 return -1;
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;
2646 return olddims;
2650 GEOS_getWKBByteOrder_r(GEOSContextHandle_t extHandle)
2652 if ( 0 == extHandle )
2654 return -1;
2657 GEOSContextHandleInternal_t *handle = 0;
2658 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
2659 if ( 0 == handle->initialized )
2661 return -1;
2664 return handle->WKBByteOrder;
2668 GEOS_setWKBByteOrder_r(GEOSContextHandle_t extHandle, int byteOrder)
2670 if ( 0 == extHandle )
2672 return -1;
2675 GEOSContextHandleInternal_t *handle = 0;
2676 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
2677 if ( 0 == handle->initialized )
2679 return -1;
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 )
2694 return NULL;
2697 GEOSContextHandleInternal_t *handle = 0;
2698 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
2699 if ( 0 == handle->initialized )
2701 return NULL;
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());
2713 catch (...)
2715 handle->ERROR_MESSAGE("Unknown exception thrown");
2718 return NULL;
2722 GEOSCoordSeq_setOrdinate_r(GEOSContextHandle_t extHandle, CoordinateSequence *cs,
2723 unsigned int idx, unsigned int dim, double val)
2725 assert(0 != cs);
2726 if ( 0 == extHandle )
2728 return 0;
2731 GEOSContextHandleInternal_t *handle = 0;
2732 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
2733 if ( 0 == handle->initialized )
2735 return 0;
2740 cs->setOrdinate(static_cast<int>(idx), static_cast<int>(dim), val);
2741 return 1;
2743 catch (const std::exception &e)
2745 handle->ERROR_MESSAGE("%s", e.what());
2747 catch (...)
2749 handle->ERROR_MESSAGE("Unknown exception thrown");
2752 return 0;
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)
2776 assert(0 != cs);
2778 if ( 0 == extHandle )
2780 return NULL;
2783 GEOSContextHandleInternal_t *handle = 0;
2784 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
2785 if ( 0 == handle->initialized )
2787 return NULL;
2792 return cs->clone();
2794 catch (const std::exception &e)
2796 handle->ERROR_MESSAGE("%s", e.what());
2798 catch (...)
2800 handle->ERROR_MESSAGE("Unknown exception thrown");
2803 return NULL;
2807 GEOSCoordSeq_getOrdinate_r(GEOSContextHandle_t extHandle, const CoordinateSequence *cs,
2808 unsigned int idx, unsigned int dim, double *val)
2810 assert(0 != cs);
2811 assert(0 != val);
2813 if ( 0 == extHandle )
2815 return 0;
2818 GEOSContextHandleInternal_t *handle = 0;
2819 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
2820 if ( 0 == handle->initialized )
2822 return 0;
2827 double d = cs->getOrdinate(static_cast<int>(idx), static_cast<int>(dim));
2828 *val = d;
2830 return 1;
2832 catch (const std::exception &e)
2834 handle->ERROR_MESSAGE("%s", e.what());
2836 catch (...)
2838 handle->ERROR_MESSAGE("Unknown exception thrown");
2841 return 0;
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)
2865 assert(0 != cs);
2866 assert(0 != size);
2868 if ( 0 == extHandle )
2870 return 0;
2873 GEOSContextHandleInternal_t *handle = 0;
2874 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
2875 if ( 0 == handle->initialized )
2877 return 0;
2882 const std::size_t sz = cs->getSize();
2883 *size = static_cast<unsigned int>(sz);
2884 return 1;
2886 catch (const std::exception &e)
2888 handle->ERROR_MESSAGE("%s", e.what());
2890 catch (...)
2892 handle->ERROR_MESSAGE("Unknown exception thrown");
2895 return 0;
2899 GEOSCoordSeq_getDimensions_r(GEOSContextHandle_t extHandle, const CoordinateSequence *cs, unsigned int *dims)
2901 assert(0 != cs);
2902 assert(0 != dims);
2904 if ( 0 == extHandle )
2906 return 0;
2909 GEOSContextHandleInternal_t *handle = 0;
2910 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
2911 if ( 0 == handle->initialized )
2913 return 0;
2918 const std::size_t dim = cs->getDimension();
2919 *dims = static_cast<unsigned int>(dim);
2921 return 1;
2923 catch (const std::exception &e)
2925 handle->ERROR_MESSAGE("%s", e.what());
2928 catch (...)
2930 handle->ERROR_MESSAGE("Unknown exception thrown");
2933 return 0;
2936 void
2937 GEOSCoordSeq_destroy_r(GEOSContextHandle_t extHandle, CoordinateSequence *s)
2939 GEOSContextHandleInternal_t *handle = 0;
2943 delete s;
2945 catch (const std::exception &e)
2947 if ( 0 == extHandle )
2949 return;
2952 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
2953 if ( 0 == handle->initialized )
2955 return;
2958 handle->ERROR_MESSAGE("%s", e.what());
2960 catch (...)
2962 if ( 0 == extHandle )
2964 return;
2967 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
2968 if ( 0 == handle->initialized )
2970 return;
2973 handle->ERROR_MESSAGE("Unknown exception thrown");
2977 const CoordinateSequence *
2978 GEOSGeom_getCoordSeq_r(GEOSContextHandle_t extHandle, const Geometry *g)
2980 if ( 0 == extHandle )
2982 return 0;
2985 GEOSContextHandleInternal_t *handle = 0;
2986 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
2987 if ( 0 == handle->initialized )
2989 return 0;
2994 using geos::geom::Point;
2996 const LineString *ls = dynamic_cast<const LineString *>(g);
2997 if ( ls )
2999 return ls->getCoordinatesRO();
3002 const Point *p = dynamic_cast<const Point *>(g);
3003 if ( p )
3005 return p->getCoordinatesRO();
3008 handle->ERROR_MESSAGE("Geometry must be a Point or LineString");
3009 return 0;
3011 catch (const std::exception &e)
3013 handle->ERROR_MESSAGE("%s", e.what());
3015 catch (...)
3017 handle->ERROR_MESSAGE("Unknown exception thrown");
3020 return 0;
3023 Geometry *
3024 GEOSGeom_createEmptyPoint_r(GEOSContextHandle_t extHandle)
3026 if ( 0 == extHandle )
3028 return NULL;
3031 GEOSContextHandleInternal_t *handle = 0;
3032 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
3033 if ( 0 == handle->initialized )
3035 return NULL;
3040 const GeometryFactory *gf = handle->geomFactory;
3041 return gf->createPoint();
3043 catch (const std::exception &e)
3045 handle->ERROR_MESSAGE("%s", e.what());
3047 catch (...)
3049 handle->ERROR_MESSAGE("Unknown exception thrown");
3052 return NULL;
3055 Geometry *
3056 GEOSGeom_createPoint_r(GEOSContextHandle_t extHandle, CoordinateSequence *cs)
3058 if ( 0 == extHandle )
3060 return 0;
3063 GEOSContextHandleInternal_t *handle = 0;
3064 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
3065 if ( 0 == handle->initialized )
3067 return 0;
3072 const GeometryFactory *gf = handle->geomFactory;
3073 return gf->createPoint(cs);
3075 catch (const std::exception &e)
3077 handle->ERROR_MESSAGE("%s", e.what());
3079 catch (...)
3081 handle->ERROR_MESSAGE("Unknown exception thrown");
3084 return 0;
3087 Geometry *
3088 GEOSGeom_createLinearRing_r(GEOSContextHandle_t extHandle, CoordinateSequence *cs)
3090 if ( 0 == extHandle )
3092 return NULL;
3095 GEOSContextHandleInternal_t *handle = 0;
3096 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
3097 if ( 0 == handle->initialized )
3099 return NULL;
3104 const GeometryFactory *gf = handle->geomFactory;
3106 return gf->createLinearRing(cs);
3108 catch (const std::exception &e)
3110 handle->ERROR_MESSAGE("%s", e.what());
3112 catch (...)
3114 handle->ERROR_MESSAGE("Unknown exception thrown");
3117 return NULL;
3120 Geometry *
3121 GEOSGeom_createEmptyLineString_r(GEOSContextHandle_t extHandle)
3123 if ( 0 == extHandle )
3125 return NULL;
3128 GEOSContextHandleInternal_t *handle = 0;
3129 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
3130 if ( 0 == handle->initialized )
3132 return NULL;
3137 const GeometryFactory *gf = handle->geomFactory;
3139 return gf->createLineString();
3141 catch (const std::exception &e)
3143 handle->ERROR_MESSAGE("%s", e.what());
3145 catch (...)
3147 handle->ERROR_MESSAGE("Unknown exception thrown");
3150 return NULL;
3153 Geometry *
3154 GEOSGeom_createLineString_r(GEOSContextHandle_t extHandle, CoordinateSequence *cs)
3156 if ( 0 == extHandle )
3158 return NULL;
3161 GEOSContextHandleInternal_t *handle = 0;
3162 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
3163 if ( 0 == handle->initialized )
3165 return NULL;
3170 const GeometryFactory *gf = handle->geomFactory;
3172 return gf->createLineString(cs);
3174 catch (const std::exception &e)
3176 handle->ERROR_MESSAGE("%s", e.what());
3178 catch (...)
3180 handle->ERROR_MESSAGE("Unknown exception thrown");
3183 return NULL;
3186 Geometry *
3187 GEOSGeom_createEmptyPolygon_r(GEOSContextHandle_t extHandle)
3189 if ( 0 == extHandle )
3191 return NULL;
3194 GEOSContextHandleInternal_t *handle = 0;
3195 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
3196 if ( 0 == handle->initialized )
3198 return NULL;
3203 const GeometryFactory *gf = handle->geomFactory;
3204 return gf->createPolygon();
3206 catch (const std::exception &e)
3208 handle->ERROR_MESSAGE("%s", e.what());
3210 catch (...)
3212 handle->ERROR_MESSAGE("Unknown exception thrown");
3215 return NULL;
3218 Geometry *
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 )
3226 return NULL;
3229 GEOSContextHandleInternal_t *handle = 0;
3230 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
3231 if ( 0 == handle->initialized )
3233 return NULL;
3238 using geos::geom::LinearRing;
3240 std::vector<Geometry *> *vholes = new std::vector<Geometry *>(holes, holes + nholes);
3242 LinearRing *nshell = dynamic_cast<LinearRing *>(shell);
3243 if ( ! nshell )
3245 handle->ERROR_MESSAGE("Shell is not a LinearRing");
3246 return NULL;
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());
3256 catch (...)
3258 handle->ERROR_MESSAGE("Unknown exception thrown");
3261 return NULL;
3264 Geometry *
3265 GEOSGeom_clone_r(GEOSContextHandle_t extHandle, const Geometry *g)
3267 assert(0 != g);
3269 if ( 0 == extHandle )
3271 return NULL;
3274 GEOSContextHandleInternal_t *handle = 0;
3275 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
3276 if ( 0 == handle->initialized )
3278 return NULL;
3283 return g->clone();
3285 catch (const std::exception &e)
3287 handle->ERROR_MESSAGE("%s", e.what());
3289 catch (...)
3291 handle->ERROR_MESSAGE("Unknown exception thrown");
3294 return NULL;
3298 GEOSGeom_getDimensions_r(GEOSContextHandle_t extHandle, const Geometry *g)
3300 if ( 0 == extHandle )
3302 return 0;
3305 GEOSContextHandleInternal_t *handle = 0;
3306 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
3307 if ( 0 == handle->initialized )
3309 return 0;
3314 using geos::geom::Point;
3315 using geos::geom::GeometryCollection;
3317 if ( g->isEmpty() )
3319 return 0;
3322 std::size_t dim = 0;
3323 const LineString *ls = dynamic_cast<const LineString *>(g);
3324 if ( ls )
3326 dim = ls->getCoordinatesRO()->getDimension();
3327 return static_cast<int>(dim);
3330 const Point *p = dynamic_cast<const Point *>(g);
3331 if ( p )
3333 dim = p->getCoordinatesRO()->getDimension();
3334 return static_cast<int>(dim);
3337 const Polygon *poly = dynamic_cast<const Polygon *>(g);
3338 if ( poly )
3340 return GEOSGeom_getDimensions_r(extHandle, poly->getExteriorRing());
3343 const GeometryCollection *coll = dynamic_cast<const GeometryCollection *>(g);
3344 if ( coll )
3346 return GEOSGeom_getDimensions_r(extHandle, coll->getGeometryN(0));
3349 handle->ERROR_MESSAGE("Unknown geometry type");
3350 return 0;
3352 catch (const std::exception &e)
3354 handle->ERROR_MESSAGE("%s", e.what());
3356 catch (...)
3358 handle->ERROR_MESSAGE("Unknown exception thrown");
3361 return 0;
3364 Geometry *
3365 GEOSSimplify_r(GEOSContextHandle_t extHandle, const Geometry *g1, double tolerance)
3367 if ( 0 == extHandle )
3369 return NULL;
3372 GEOSContextHandleInternal_t *handle = 0;
3373 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
3374 if ( 0 == handle->initialized )
3376 return NULL;
3381 using namespace geos::simplify;
3382 Geometry::AutoPtr g(DouglasPeuckerSimplifier::simplify(g1, tolerance));
3383 return g.release();
3385 catch (const std::exception &e)
3387 handle->ERROR_MESSAGE("%s", e.what());
3389 catch (...)
3391 handle->ERROR_MESSAGE("Unknown exception thrown");
3394 return NULL;
3397 Geometry *
3398 GEOSTopologyPreserveSimplify_r(GEOSContextHandle_t extHandle, const Geometry *g1, double tolerance)
3400 if ( 0 == extHandle )
3402 return NULL;
3405 GEOSContextHandleInternal_t *handle = 0;
3406 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
3407 if ( 0 == handle->initialized )
3409 return NULL;
3414 using namespace geos::simplify;
3415 Geometry::AutoPtr g(TopologyPreservingSimplifier::simplify(g1, tolerance));
3416 return g.release();
3418 catch (const std::exception &e)
3420 handle->ERROR_MESSAGE("%s", e.what());
3422 catch (...)
3424 handle->ERROR_MESSAGE("Unknown exception thrown");
3427 return NULL;
3431 /* WKT Reader */
3432 WKTReader *
3433 GEOSWKTReader_create_r(GEOSContextHandle_t extHandle)
3435 if ( 0 == extHandle )
3437 return NULL;
3440 GEOSContextHandleInternal_t *handle = 0;
3441 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
3442 if ( 0 == handle->initialized )
3444 return NULL;
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());
3456 catch (...)
3458 handle->ERROR_MESSAGE("Unknown exception thrown");
3461 return NULL;
3464 void
3465 GEOSWKTReader_destroy_r(GEOSContextHandle_t extHandle, WKTReader *reader)
3467 GEOSContextHandleInternal_t *handle = 0;
3471 delete reader;
3473 catch (const std::exception &e)
3475 if ( 0 == extHandle )
3477 return;
3480 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
3481 if ( 0 == handle->initialized )
3483 return;
3486 handle->ERROR_MESSAGE("%s", e.what());
3488 catch (...)
3490 if ( 0 == extHandle )
3492 return;
3495 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
3496 if ( 0 == handle->initialized )
3498 return;
3501 handle->ERROR_MESSAGE("Unknown exception thrown");
3506 Geometry*
3507 GEOSWKTReader_read_r(GEOSContextHandle_t extHandle, WKTReader *reader, const char *wkt)
3509 assert(0 != reader);
3511 if ( 0 == extHandle )
3513 return 0;
3516 GEOSContextHandleInternal_t *handle = 0;
3517 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
3518 if ( 0 == handle->initialized )
3520 return 0;
3525 const std::string wktstring(wkt);
3526 Geometry *g = reader->read(wktstring);
3527 return g;
3529 catch (const std::exception &e)
3531 handle->ERROR_MESSAGE("%s", e.what());
3533 catch (...)
3535 handle->ERROR_MESSAGE("Unknown exception thrown");
3538 return 0;
3541 /* WKT Writer */
3542 WKTWriter *
3543 GEOSWKTWriter_create_r(GEOSContextHandle_t extHandle)
3545 if ( 0 == extHandle )
3547 return 0;
3550 GEOSContextHandleInternal_t *handle = 0;
3551 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
3552 if ( 0 == handle->initialized )
3554 return 0;
3559 using geos::io::WKTWriter;
3560 return new WKTWriter();
3562 catch (const std::exception &e)
3564 handle->ERROR_MESSAGE("%s", e.what());
3566 catch (...)
3568 handle->ERROR_MESSAGE("Unknown exception thrown");
3571 return 0;
3574 void
3575 GEOSWKTWriter_destroy_r(GEOSContextHandle_t extHandle, WKTWriter *Writer)
3578 GEOSContextHandleInternal_t *handle = 0;
3582 delete Writer;
3584 catch (const std::exception &e)
3586 if ( 0 == extHandle )
3588 return;
3591 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
3592 if ( 0 == handle->initialized )
3594 return;
3597 handle->ERROR_MESSAGE("%s", e.what());
3599 catch (...)
3601 if ( 0 == extHandle )
3603 return;
3606 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
3607 if ( 0 == handle->initialized )
3609 return;
3612 handle->ERROR_MESSAGE("Unknown exception thrown");
3617 char*
3618 GEOSWKTWriter_write_r(GEOSContextHandle_t extHandle, WKTWriter *writer, const Geometry *geom)
3620 assert(0 != writer);
3622 if ( 0 == extHandle )
3624 return NULL;
3627 GEOSContextHandleInternal_t *handle = 0;
3628 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
3629 if ( 0 == handle->initialized )
3631 return NULL;
3636 std::string sgeom(writer->write(geom));
3637 char *result = gstrdup(sgeom);
3638 return result;
3640 catch (const std::exception &e)
3642 handle->ERROR_MESSAGE("%s", e.what());
3644 catch (...)
3646 handle->ERROR_MESSAGE("Unknown exception thrown");
3649 return NULL;
3652 /* WKB Reader */
3653 WKBReader *
3654 GEOSWKBReader_create_r(GEOSContextHandle_t extHandle)
3656 if ( 0 == extHandle )
3658 return NULL;
3661 GEOSContextHandleInternal_t *handle = 0;
3662 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
3663 if ( 0 == handle->initialized )
3665 return NULL;
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());
3677 catch (...)
3679 handle->ERROR_MESSAGE("Unknown exception thrown");
3682 return NULL;
3685 void
3686 GEOSWKBReader_destroy_r(GEOSContextHandle_t extHandle, WKBReader *reader)
3688 GEOSContextHandleInternal_t *handle = 0;
3692 delete reader;
3694 catch (const std::exception &e)
3696 if ( 0 == extHandle )
3698 return;
3701 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
3702 if ( 0 == handle->initialized )
3704 return;
3707 handle->ERROR_MESSAGE("%s", e.what());
3709 catch (...)
3711 if ( 0 == extHandle )
3713 return;
3716 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
3717 if ( 0 == handle->initialized )
3719 return;
3722 handle->ERROR_MESSAGE("Unknown exception thrown");
3727 Geometry*
3728 GEOSWKBReader_read_r(GEOSContextHandle_t extHandle, WKBReader *reader, const unsigned char *wkb, size_t size)
3730 assert(0 != reader);
3731 assert(0 != wkb);
3733 if ( 0 == extHandle )
3735 return 0;
3738 GEOSContextHandleInternal_t *handle = 0;
3739 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
3740 if ( 0 == handle->initialized )
3742 return 0;
3747 std::string wkbstring(reinterpret_cast<const char*>(wkb), size); // make it binary !
3748 std::istringstream is(std::ios_base::binary);
3749 is.str(wkbstring);
3750 is.seekg(0, std::ios::beg); // rewind reader pointer
3752 Geometry *g = reader->read(is);
3753 return g;
3755 catch (const std::exception &e)
3757 handle->ERROR_MESSAGE("%s", e.what());
3759 catch (...)
3761 handle->ERROR_MESSAGE("Unknown exception thrown");
3764 return 0;
3767 Geometry*
3768 GEOSWKBReader_readHEX_r(GEOSContextHandle_t extHandle, WKBReader *reader, const unsigned char *hex, size_t size)
3770 assert(0 != reader);
3771 assert(0 != hex);
3773 if ( 0 == extHandle )
3775 return 0;
3778 GEOSContextHandleInternal_t *handle = 0;
3779 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
3780 if ( 0 == handle->initialized )
3782 return 0;
3787 std::string hexstring(reinterpret_cast<const char*>(hex), size);
3788 std::istringstream is(std::ios_base::binary);
3789 is.str(hexstring);
3790 is.seekg(0, std::ios::beg); // rewind reader pointer
3792 Geometry *g = reader->readHEX(is);
3793 return g;
3795 catch (const std::exception &e)
3797 handle->ERROR_MESSAGE("%s", e.what());
3799 catch (...)
3801 handle->ERROR_MESSAGE("Unknown exception thrown");
3804 return 0;
3807 /* WKB Writer */
3808 WKBWriter *
3809 GEOSWKBWriter_create_r(GEOSContextHandle_t extHandle)
3811 if ( 0 == extHandle )
3813 return NULL;
3816 GEOSContextHandleInternal_t *handle = 0;
3817 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
3818 if ( 0 == handle->initialized )
3820 return NULL;
3825 using geos::io::WKBWriter;
3826 return new WKBWriter();
3828 catch (const std::exception &e)
3830 handle->ERROR_MESSAGE("%s", e.what());
3832 catch (...)
3834 handle->ERROR_MESSAGE("Unknown exception thrown");
3837 return NULL;
3840 void
3841 GEOSWKBWriter_destroy_r(GEOSContextHandle_t extHandle, WKBWriter *Writer)
3843 GEOSContextHandleInternal_t *handle = 0;
3847 delete Writer;
3849 catch (const std::exception &e)
3851 if ( 0 == extHandle )
3853 return;
3856 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
3857 if ( 0 == handle->initialized )
3859 return;
3862 handle->ERROR_MESSAGE("%s", e.what());
3864 catch (...)
3866 if ( 0 == extHandle )
3868 return;
3871 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
3872 if ( 0 == handle->initialized )
3874 return;
3877 handle->ERROR_MESSAGE("Unknown exception thrown");
3882 /* The caller owns the result */
3883 unsigned char*
3884 GEOSWKBWriter_write_r(GEOSContextHandle_t extHandle, WKBWriter *writer, const Geometry *geom, size_t *size)
3886 assert(0 != writer);
3887 assert(0 != geom);
3888 assert(0 != size);
3890 if ( 0 == extHandle )
3892 return NULL;
3895 GEOSContextHandleInternal_t *handle = 0;
3896 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
3897 if ( 0 == handle->initialized )
3899 return NULL;
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);
3912 *size = len;
3913 return result;
3915 catch (const std::exception &e)
3917 handle->ERROR_MESSAGE("%s", e.what());
3919 catch (...)
3921 handle->ERROR_MESSAGE("Unknown exception thrown");
3923 return NULL;
3926 /* The caller owns the result */
3927 unsigned char*
3928 GEOSWKBWriter_writeHEX_r(GEOSContextHandle_t extHandle, WKBWriter *writer, const Geometry *geom, size_t *size)
3930 assert(0 != writer);
3931 assert(0 != geom);
3932 assert(0 != size);
3934 if ( 0 == extHandle )
3936 return NULL;
3939 GEOSContextHandleInternal_t *handle = 0;
3940 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
3941 if ( 0 == handle->initialized )
3943 return NULL;
3948 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);
3956 *size = len;
3957 return result;
3959 catch (const std::exception &e)
3961 handle->ERROR_MESSAGE("%s", e.what());
3963 catch (...)
3965 handle->ERROR_MESSAGE("Unknown exception thrown");
3968 return NULL;
3972 GEOSWKBWriter_getOutputDimension_r(GEOSContextHandle_t extHandle, const GEOSWKBWriter* writer)
3974 assert(0 != writer);
3976 if ( 0 == extHandle )
3978 return 0;
3981 int ret = 0;
3983 GEOSContextHandleInternal_t *handle = 0;
3984 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
3985 if ( 0 != handle->initialized )
3989 ret = writer->getOutputDimension();
3991 catch (...)
3993 handle->ERROR_MESSAGE("Unknown exception thrown");
3997 return ret;
4000 void
4001 GEOSWKBWriter_setOutputDimension_r(GEOSContextHandle_t extHandle, GEOSWKBWriter* writer, int newDimension)
4003 assert(0 != writer);
4005 if ( 0 == extHandle )
4007 return;
4010 GEOSContextHandleInternal_t *handle = 0;
4011 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
4012 if ( 0 != handle->initialized )
4016 writer->setOutputDimension(newDimension);
4018 catch (...)
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 )
4032 return 0;
4035 int ret = 0;
4037 GEOSContextHandleInternal_t *handle = 0;
4038 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
4039 if ( 0 != handle->initialized )
4043 ret = writer->getByteOrder();
4046 catch (...)
4048 handle->ERROR_MESSAGE("Unknown exception thrown");
4052 return ret;
4055 void
4056 GEOSWKBWriter_setByteOrder_r(GEOSContextHandle_t extHandle, GEOSWKBWriter* writer, int newByteOrder)
4058 assert(0 != writer);
4060 if ( 0 == extHandle )
4062 return;
4065 GEOSContextHandleInternal_t *handle = 0;
4066 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
4067 if ( 0 != handle->initialized )
4071 writer->setByteOrder(newByteOrder);
4073 catch (...)
4075 handle->ERROR_MESSAGE("Unknown exception thrown");
4080 char
4081 GEOSWKBWriter_getIncludeSRID_r(GEOSContextHandle_t extHandle, const GEOSWKBWriter* writer)
4083 assert(0 != writer);
4085 if ( 0 == extHandle )
4087 return -1;
4090 int ret = -1;
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);
4101 catch (...)
4103 handle->ERROR_MESSAGE("Unknown exception thrown");
4107 return static_cast<char>(ret);
4110 void
4111 GEOSWKBWriter_setIncludeSRID_r(GEOSContextHandle_t extHandle, GEOSWKBWriter* writer, const char newIncludeSRID)
4113 assert(0 != writer);
4115 if ( 0 == extHandle )
4117 return;
4120 GEOSContextHandleInternal_t *handle = 0;
4121 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
4122 if ( 0 != handle->initialized )
4126 writer->setIncludeSRID(newIncludeSRID);
4128 catch (...)
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 )
4145 return 0;
4148 GEOSContextHandleInternal_t *handle = 0;
4149 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
4150 if ( 0 == handle->initialized )
4152 return 0;
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());
4165 catch (...)
4167 handle->ERROR_MESSAGE("Unknown exception thrown");
4170 return prep;
4173 void
4174 GEOSPreparedGeom_destroy_r(GEOSContextHandle_t extHandle, const geos::geom::prep::PreparedGeometry *a)
4176 GEOSContextHandleInternal_t *handle = 0;
4180 delete a;
4182 catch (const std::exception &e)
4184 if ( 0 == extHandle )
4186 return;
4189 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
4190 if ( 0 == handle->initialized )
4192 return;
4195 handle->ERROR_MESSAGE("%s", e.what());
4197 catch (...)
4199 if ( 0 == extHandle )
4201 return;
4204 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
4205 if ( 0 == handle->initialized )
4207 return;
4210 handle->ERROR_MESSAGE("Unknown exception thrown");
4214 char
4215 GEOSPreparedContains_r(GEOSContextHandle_t extHandle,
4216 const geos::geom::prep::PreparedGeometry *pg, const Geometry *g)
4218 assert(0 != pg);
4219 assert(0 != g);
4221 if ( 0 == extHandle )
4223 return 2;
4226 GEOSContextHandleInternal_t *handle = 0;
4227 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
4228 if ( 0 == handle->initialized )
4230 return 2;
4233 try
4235 bool result = pg->contains(g);
4236 return result;
4238 catch (const std::exception &e)
4240 handle->ERROR_MESSAGE("%s", e.what());
4242 catch (...)
4244 handle->ERROR_MESSAGE("Unknown exception thrown");
4247 return 2;
4250 char
4251 GEOSPreparedContainsProperly_r(GEOSContextHandle_t extHandle,
4252 const geos::geom::prep::PreparedGeometry *pg, const Geometry *g)
4254 assert(0 != pg);
4255 assert(0 != g);
4257 if ( 0 == extHandle )
4259 return 2;
4262 GEOSContextHandleInternal_t *handle = 0;
4263 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
4264 if ( 0 == handle->initialized )
4266 return 2;
4269 try
4271 bool result = pg->containsProperly(g);
4272 return result;
4274 catch (const std::exception &e)
4276 handle->ERROR_MESSAGE("%s", e.what());
4278 catch (...)
4280 handle->ERROR_MESSAGE("Unknown exception thrown");
4283 return 2;
4286 char
4287 GEOSPreparedCovers_r(GEOSContextHandle_t extHandle,
4288 const geos::geom::prep::PreparedGeometry *pg, const Geometry *g)
4290 assert(0 != pg);
4291 assert(0 != g);
4293 if ( 0 == extHandle )
4295 return 2;
4298 GEOSContextHandleInternal_t *handle = 0;
4299 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
4300 if ( 0 == handle->initialized )
4302 return 2;
4305 try
4307 bool result = pg->covers(g);
4308 return result;
4310 catch (const std::exception &e)
4312 handle->ERROR_MESSAGE("%s", e.what());
4314 catch (...)
4316 handle->ERROR_MESSAGE("Unknown exception thrown");
4319 return 2;
4322 char
4323 GEOSPreparedIntersects_r(GEOSContextHandle_t extHandle,
4324 const geos::geom::prep::PreparedGeometry *pg, const Geometry *g)
4326 assert(0 != pg);
4327 assert(0 != g);
4329 if ( 0 == extHandle )
4331 return 2;
4334 GEOSContextHandleInternal_t *handle = 0;
4335 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
4336 if ( 0 == handle->initialized )
4338 return 2;
4341 try
4343 bool result = pg->intersects(g);
4344 return result;
4346 catch (const std::exception &e)
4348 handle->ERROR_MESSAGE("%s", e.what());
4350 catch (...)
4352 handle->ERROR_MESSAGE("Unknown exception thrown");
4355 return 2;
4358 //-----------------------------------------------------------------
4359 // STRtree
4360 //-----------------------------------------------------------------
4362 geos::index::strtree::STRtree *
4363 GEOSSTRtree_create_r(GEOSContextHandle_t extHandle,
4364 size_t nodeCapacity)
4366 if ( 0 == extHandle )
4368 return 0;
4371 GEOSContextHandleInternal_t *handle = 0;
4372 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
4373 if ( 0 == handle->initialized )
4375 return 0;
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());
4388 catch (...)
4390 handle->ERROR_MESSAGE("Unknown exception thrown");
4393 return tree;
4396 void
4397 GEOSSTRtree_insert_r(GEOSContextHandle_t extHandle,
4398 geos::index::strtree::STRtree *tree,
4399 const geos::geom::Geometry *g,
4400 void *item)
4402 GEOSContextHandleInternal_t *handle = 0;
4403 assert(tree != 0);
4404 assert(g != 0);
4408 tree->insert(g->getEnvelopeInternal(), item);
4410 catch (const std::exception &e)
4412 if ( 0 == extHandle )
4414 return;
4417 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
4418 if ( 0 == handle->initialized )
4420 return;
4423 handle->ERROR_MESSAGE("%s", e.what());
4425 catch (...)
4427 if ( 0 == extHandle )
4429 return;
4432 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
4433 if ( 0 == handle->initialized )
4435 return;
4438 handle->ERROR_MESSAGE("Unknown exception thrown");
4442 void
4443 GEOSSTRtree_query_r(GEOSContextHandle_t extHandle,
4444 geos::index::strtree::STRtree *tree,
4445 const geos::geom::Geometry *g,
4446 GEOSQueryCallback callback,
4447 void *userdata)
4449 GEOSContextHandleInternal_t *handle = 0;
4450 assert(tree != 0);
4451 assert(g != 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 )
4463 return;
4466 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
4467 if ( 0 == handle->initialized )
4469 return;
4472 handle->ERROR_MESSAGE("%s", e.what());
4474 catch (...)
4476 if ( 0 == extHandle )
4478 return;
4481 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
4482 if ( 0 == handle->initialized )
4484 return;
4487 handle->ERROR_MESSAGE("Unknown exception thrown");
4491 void
4492 GEOSSTRtree_iterate_r(GEOSContextHandle_t extHandle,
4493 geos::index::strtree::STRtree *tree,
4494 GEOSQueryCallback callback,
4495 void *userdata)
4497 GEOSContextHandleInternal_t *handle = 0;
4498 assert(tree != 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 )
4510 return;
4513 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
4514 if ( 0 == handle->initialized )
4516 return;
4519 handle->ERROR_MESSAGE("%s", e.what());
4521 catch (...)
4523 if ( 0 == extHandle )
4525 return;
4528 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
4529 if ( 0 == handle->initialized )
4531 return;
4534 handle->ERROR_MESSAGE("Unknown exception thrown");
4538 char
4539 GEOSSTRtree_remove_r(GEOSContextHandle_t extHandle,
4540 geos::index::strtree::STRtree *tree,
4541 const geos::geom::Geometry *g,
4542 void *item)
4544 assert(0 != tree);
4545 assert(0 != g);
4547 if ( 0 == extHandle )
4549 return 2;
4552 GEOSContextHandleInternal_t *handle = 0;
4553 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
4554 if ( 0 == handle->initialized )
4556 return 2;
4559 try
4561 bool result = tree->remove(g->getEnvelopeInternal(), item);
4562 return result;
4564 catch (const std::exception &e)
4566 handle->ERROR_MESSAGE("%s", e.what());
4568 catch (...)
4570 handle->ERROR_MESSAGE("Unknown exception thrown");
4573 return 2;
4576 void
4577 GEOSSTRtree_destroy_r(GEOSContextHandle_t extHandle,
4578 geos::index::strtree::STRtree *tree)
4580 GEOSContextHandleInternal_t *handle = 0;
4584 delete tree;
4586 catch (const std::exception &e)
4588 if ( 0 == extHandle )
4590 return;
4593 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
4594 if ( 0 == handle->initialized )
4596 return;
4599 handle->ERROR_MESSAGE("%s", e.what());
4601 catch (...)
4603 if ( 0 == extHandle )
4605 return;
4608 handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
4609 if ( 0 == handle->initialized )
4611 return;
4614 handle->ERROR_MESSAGE("Unknown exception thrown");
4618 double
4619 GEOSProject_r(GEOSContextHandle_t extHandle,
4620 const Geometry *g,
4621 const Geometry *p)
4624 const geos::geom::Point* point = dynamic_cast<const geos::geom::Point*>(p);
4625 if (!point) {
4626 if ( 0 == extHandle )
4628 return -1.0;
4630 GEOSContextHandleInternal_t *handle = reinterpret_cast<GEOSContextHandleInternal_t*>(extHandle);
4631 if ( 0 == handle->initialized )
4633 return -1.0;
4636 handle->ERROR_MESSAGE("third argument of GEOSProject_r must be Point*");
4637 return -1.0;
4639 const geos::geom::Coordinate* inputPt = p->getCoordinate();
4640 return geos::linearref::LengthIndexedLine(g).project(*inputPt);
4644 Geometry*
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);
4652 return point;
4656 double
4657 GEOSProjectNormalized_r(GEOSContextHandle_t extHandle, const Geometry *g,
4658 const Geometry *p)
4661 double length;
4662 GEOSLength_r(extHandle, g, &length);
4663 return GEOSProject_r(extHandle, g, p) / length;
4667 Geometry*
4668 GEOSInterpolateNormalized_r(GEOSContextHandle_t extHandle, const Geometry *g,
4669 double d)
4671 double length;
4672 GEOSLength_r(extHandle, g, &length);
4673 return GEOSInterpolate_r(extHandle, g, d * length);
4676 GEOSGeometry*
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(),
4702 itE=coords.end();
4703 it != itE; ++it)
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());
4716 return 0;
4718 catch (...)
4720 handle->ERROR_MESSAGE("Unknown exception thrown");
4721 return 0;
4726 } /* extern "C" */