Use spaces under tests/unit
[geos.git] / swig / geos.i.in
blob2d98816cd566cbce43ad0adbfb0612f3aba620ac
1 /* =========================================================================
2 * Copyright 2005-2007 Charlie Savage, cfis@interserv.com
4 * Interface for a SWIG generated geos module.
6 * This is free software; you can redistribute and/or modify it under
7 * the terms of the GNU Lesser General Public Licence as published
8 * by the Free Software Foundation.
9 * See the COPYING file for more information.
11 * ========================================================================= */
13 %module geos
15 %include "attribute.i"
16 %include "exception.i"
17 %include "std_string.i"
18 %include "std_vector.i"
19 %include "std_except.i"
21 %{
22 #include "geos_c.h"
23 /* Needed for va_start, etc. */
24 #include <stdarg.h>
27 /* Constants copied from geos_c.h. Would be nice
28 to reuse the originals but we can't without exposing
29 the whole c api. */
30 #define GEOS_VERSION_MAJOR @VERSION_MAJOR@
31 #define GEOS_VERSION_MINOR @VERSION_MINOR@
32 #define GEOS_VERSION_PATCH @VERSION_PATCH@
33 #define GEOS_VERSION "@VERSION@"
34 #define GEOS_JTS_PORT "@JTS_PORT@"
36 #define GEOS_CAPI_VERSION_MAJOR @CAPI_VERSION_MAJOR@
37 #define GEOS_CAPI_VERSION_MINOR @CAPI_VERSION_MINOR@
38 #define GEOS_CAPI_VERSION_PATCH @CAPI_VERSION_PATCH@
39 #define GEOS_CAPI_FIRST_INTERFACE GEOS_CAPI_VERSION_MAJOR
40 #define GEOS_CAPI_LAST_INTERFACE (GEOS_CAPI_VERSION_MAJOR+GEOS_CAPI_VERSION_MINOR)
41 #define GEOS_CAPI_VERSION "@VERSION@-CAPI-@CAPI_VERSION@"
43 /* Supported geometry types */
44 enum GEOSGeomTypes {
45 GEOS_POINT,
46 GEOS_LINESTRING,
47 GEOS_LINEARRING,
48 GEOS_POLYGON,
49 GEOS_MULTIPOINT,
50 GEOS_MULTILINESTRING,
51 GEOS_MULTIPOLYGON,
52 GEOS_GEOMETRYCOLLECTION
55 enum GEOSByteOrders {
56 GEOS_WKB_XDR = 0, /* Big Endian */
57 GEOS_WKB_NDR = 1 /* Little Endian */
60 /* From OffsetCurveSetBuilder.h for buffer operations. */
62 static const int DEFAULT_QUADRANT_SEGMENTS=8;
65 /* Message and Error Handling */
68 /* This is not thread safe ! */
69 static const int MESSAGE_SIZE = 1000;
70 static char message[MESSAGE_SIZE];
72 void noticeHandler(const char *fmt, ...)
74 va_list args;
75 va_start(args, fmt);
76 vsnprintf(message, sizeof(message) - 1, fmt, args);
77 va_end(args);
80 void errorHandler(const char *fmt, ...)
82 va_list args;
83 va_start(args, fmt);
84 vsnprintf(message, sizeof(message) - 1, fmt, args);
85 va_end(args);
90 /* First initialize geos */
91 %init %{
92 initGEOS(noticeHandler, errorHandler);
96 /* Module level methods */
97 %rename("version") GEOSversion;
98 const char *GEOSversion();
100 /* Exception handler */
101 %exception
105 $action
107 catch (const std::exception& e)
109 SWIG_exception(SWIG_RuntimeError, e.what());
114 /* ============== Language Specific Files ============ */
116 /* Import language specific SWIG files. This allows each language
117 to define its own renames as well as any special functionality
118 such as language specific iterators for collections. Note
119 that %include allows the included files to generate interface
120 wrapper code while %import does not. Thus use %include since
121 this is an important feature (for example, Ruby needs it to #undef
122 the select macro) */
125 #ifdef SWIGPYTHON
126 %include @top_srcdir@/swig/python/python.i
127 #endif
129 #ifdef SWIGRUBY
130 %include @top_srcdir@/swig/ruby/ruby.i
131 #endif
136 // === CoordinateSequence ===
138 typedef void GeosCoordinateSequence;
140 void checkCoordSeqBounds(const GEOSCoordSeq coordSeq, const size_t index)
142 unsigned int size = 0;
143 GEOSCoordSeq_getSize(coordSeq, &size);
145 if (index < 0 || index >= size)
146 throw std::runtime_error("Index out of bounds");
150 %newobject GeosCoordinateSequence::clone;
151 %rename (CoordinateSequence) GeosCoordinateSequence;
152 class GeosCoordinateSequence
154 public:
155 %extend
157 GeosCoordinateSequence(size_t size, size_t dims)
159 return (GeosCoordinateSequence*) GEOSCoordSeq_create(size, dims);
162 ~GeosCoordinateSequence()
164 GEOSCoordSeq coords = (GEOSCoordSeq) self;
165 return GEOSCoordSeq_destroy(coords);
168 GeosCoordinateSequence *clone()
170 GEOSCoordSeq coords = (GEOSCoordSeq) self;
171 return (GeosCoordinateSequence*) GEOSCoordSeq_clone(coords);
174 int setX(size_t idx, double val)
176 GEOSCoordSeq coords = (GEOSCoordSeq) self;
177 checkCoordSeqBounds(coords, idx);
178 return GEOSCoordSeq_setX(coords, idx, val);
181 int setY(size_t idx, double val)
183 GEOSCoordSeq coords = (GEOSCoordSeq) self;
184 checkCoordSeqBounds(coords, idx);
185 return GEOSCoordSeq_setY(coords, idx, val);
188 int setZ(size_t idx, double val)
190 GEOSCoordSeq coords = (GEOSCoordSeq) self;
191 checkCoordSeqBounds(coords, idx);
192 return GEOSCoordSeq_setZ(coords, idx, val);
195 int setOrdinate(size_t idx, size_t dim, double val)
197 GEOSCoordSeq coords = (GEOSCoordSeq) self;
198 checkCoordSeqBounds(coords, idx);
199 return GEOSCoordSeq_setOrdinate(coords, idx, dim, val);
202 double getX(size_t idx)
204 double result;
205 GEOSCoordSeq coords = (GEOSCoordSeq) self;
206 checkCoordSeqBounds(coords, idx);
207 GEOSCoordSeq_getX(coords, idx, &result);
208 return result;
211 double getY(size_t idx)
213 double result;
214 GEOSCoordSeq coords = (GEOSCoordSeq) self;
215 checkCoordSeqBounds(coords, idx);
216 GEOSCoordSeq_getY(coords, idx, &result);
217 return result;
220 double getZ(size_t idx)
222 double result;
223 GEOSCoordSeq coords = (GEOSCoordSeq) self;
224 checkCoordSeqBounds(coords, idx);
225 GEOSCoordSeq_getZ(coords, idx, &result);
226 return result;
229 double getOrdinate(size_t idx, size_t dim)
231 double result;
232 GEOSCoordSeq coords = (GEOSCoordSeq) self;
233 checkCoordSeqBounds(coords, idx);
234 GEOSCoordSeq_getOrdinate(coords, idx, dim, &result);
235 return result;
238 unsigned int getSize()
240 unsigned int result;
241 GEOSCoordSeq coords = (GEOSCoordSeq) self;
242 GEOSCoordSeq_getSize(coords, &result);
243 return result;
246 unsigned int getDimensions()
248 unsigned int result;
249 GEOSCoordSeq coords = (GEOSCoordSeq) self;
250 GEOSCoordSeq_getDimensions(coords, &result);
251 return result;
257 /* ======== Fake Classes to Create Geom Hierarchy ====== */
258 %rename(Geometry) GeosGeometry;
259 %rename(Point) GeosPoint;
260 %rename(LineString) GeosLineString;
261 %rename(LinearRing) GeosLinearRing;
262 %rename(Polygon) GeosPolygon;
263 %rename(GeometryCollection) GeosGeometryCollection;
264 %rename(MultiPoint) GeosMultiPoint;
265 %rename(MultiLineString) GeosMultiLineString;
266 %rename(MultiLinearRing) GeosMultiLinearRing;
267 %rename(MultiPolygon) GeosMultiPolygon;
268 %rename(WktReader) GeosWktReader;
269 %rename(WktWriter) GeosWktWriter;
270 %rename(WkbReader) GeosWkbReader;
271 %rename(WkbWriter) GeosWkbWriter;
274 %rename("union") GeosGeometry::geomUnion;
277 typedef void GeosGeometry;
278 typedef void GeosPoint;
279 typedef void GeosLineString;
280 typedef void GeosLinearRing;
281 typedef void GeosPolygon;
282 typedef void GeosGeometryCollection;
283 typedef void GeosMultiPoint;
284 typedef void GeosMultiLineString;
285 typedef void GeosMultiLinearRing;
286 typedef void GeosMultiPolygon;
288 typedef void GeosWktReader;
289 typedef void GeosWktWriter;
290 typedef void GeosWkbReader;
291 typedef void GeosWkbWriter;
294 %newobject GeosGeometry::intersection;
295 %newobject GeosGeometry::buffer;
296 %newobject GeosGeometry::convexHull;
297 %newobject GeosGeometry::difference;
298 %newobject GeosGeometry::symDifference;
299 %newobject GeosGeometry::boundary;
300 %newobject GeosGeometry::geomUnion;
301 %newobject GeosGeometry::pointOnSurface;
302 %newobject GeosGeometry::getCentroid;
303 %newobject GeosGeometry::relate;
304 %newobject GeosGeometry::lineMerge;
305 %newobject GeosGeometry::simplify;
306 %newobject GeosGeometry::topologyPreserveSimplify;
309 %typemap(out) GeosGeometry*
311 /* %typemap(out) GeosGeometry */
313 if ($1 == NULL)
314 SWIG_exception(SWIG_RuntimeError, message);
316 GeosGeometry *geom = $1;
317 GEOSGeomTypes geomId = (GEOSGeomTypes)GEOSGeomTypeId((GEOSGeom) geom);
319 switch (geomId)
321 case GEOS_POINT:
322 $result = SWIG_NewPointerObj(SWIG_as_voidptr(result), $descriptor(GeosPoint*), 0 | $owner);
323 break;
324 case GEOS_LINESTRING:
325 $result = SWIG_NewPointerObj(SWIG_as_voidptr(result), $descriptor(GeosLineString*), 0 | $owner);
326 break;
327 case GEOS_LINEARRING:
328 $result = SWIG_NewPointerObj(SWIG_as_voidptr(result), $descriptor(GeosLinearRing*), 0 | $owner);
329 break;
330 case GEOS_POLYGON:
331 $result = SWIG_NewPointerObj(SWIG_as_voidptr(result), $descriptor(GeosPolygon*), 0 | $owner);
332 break;
333 case GEOS_MULTIPOINT:
334 $result = SWIG_NewPointerObj(SWIG_as_voidptr(result), $descriptor(GeosMultiPoint*), 0 | $owner);
335 break;
336 case GEOS_MULTILINESTRING:
337 $result = SWIG_NewPointerObj(SWIG_as_voidptr(result), $descriptor(GeosMultiLineString*), 0 | $owner);
338 break;
339 case GEOS_MULTIPOLYGON:
340 $result = SWIG_NewPointerObj(SWIG_as_voidptr(result), $descriptor(GeosMultiPolygon*), 0 | $owner);
341 break;
342 case GEOS_GEOMETRYCOLLECTION:
343 $result = SWIG_NewPointerObj(SWIG_as_voidptr(result), $descriptor(GeosGeometryCollection*), 0 | $owner);
344 break;
348 /* Setup a default typemap for buffer. */
349 %typemap(default) int quadsegs
351 $1 = DEFAULT_QUADRANT_SEGMENTS;
355 bool checkBoolResult(char result)
357 int intResult = (int) result;
359 if (intResult == 1)
360 return true;
361 else if (intResult == 0)
362 return false;
363 else
364 throw std::runtime_error(message);
368 %newobject GeosGeometry::clone;
369 class GeosGeometry
371 private:
372 GeosGeometry();
373 public:
374 %extend
376 ~GeosGeometry()
378 GEOSGeom geom = (GEOSGeom) self;
379 GEOSGeom_destroy(geom);
382 GeosGeometry *clone()
384 GEOSGeom geom = (GEOSGeom) self;
385 return GEOSGeom_clone(geom);
388 char *geomType()
390 GEOSGeom geom = (GEOSGeom) self;
391 return GEOSGeomType(geom);
394 int typeId()
396 GEOSGeom geom = (GEOSGeom) self;
397 return GEOSGeomTypeId(geom);
400 void normalize()
402 GEOSGeom geom = (GEOSGeom) self;
403 int result = GEOSNormalize(geom);
405 if (result == -1)
406 throw std::runtime_error(message);
409 int getSRID()
411 GEOSGeom geom = (GEOSGeom) self;
412 return GEOSGetSRID(geom);
415 void setSRID(int SRID)
417 GEOSGeom geom = (GEOSGeom) self;
418 return GEOSSetSRID(geom, SRID);
421 size_t getDimensions()
423 GEOSGeom geom = (GEOSGeom) self;
424 return GEOSGeom_getDimensions(geom);
427 size_t getNumGeometries()
429 GEOSGeom geom = (GEOSGeom) self;
430 size_t result = GEOSGetNumGeometries(geom);
432 if ((int)result == -1)
433 throw std::runtime_error(message);
435 return result;
438 /* Topology Operations */
439 GeosGeometry *intersection(GeosGeometry *other)
441 GEOSGeom geom = (GEOSGeom) self;
442 GEOSGeom otherGeom = (GEOSGeom) other;
443 return (GeosGeometry*) GEOSIntersection(geom, otherGeom);
446 GeosGeometry *buffer(double width, int quadsegs)
448 GEOSGeom geom = (GEOSGeom) self;
449 return (GeosGeometry*) GEOSBuffer(geom, width, quadsegs);
452 GeosGeometry *convexHull()
454 GEOSGeom geom = (GEOSGeom) self;
455 return (GeosGeometry*) GEOSConvexHull(geom);
458 GeosGeometry *difference(GeosGeometry *other)
460 GEOSGeom geom = (GEOSGeom) self;
461 GEOSGeom otherGeom = (GEOSGeom) other;
462 return (GeosGeometry*) GEOSDifference(geom, otherGeom);
465 GeosGeometry *symDifference(GeosGeometry *other)
467 GEOSGeom geom = (GEOSGeom) self;
468 GEOSGeom otherGeom = (GEOSGeom) other;
469 return (GeosGeometry*) GEOSSymDifference(geom, otherGeom);
472 GeosGeometry *boundary()
474 GEOSGeom geom = (GEOSGeom) self;
475 return (GeosGeometry*) GEOSBoundary(geom);
478 GeosGeometry *geomUnion(GeosGeometry *other)
480 GEOSGeom geom = (GEOSGeom) self;
481 GEOSGeom otherGeom = (GEOSGeom) other;
482 return (GeosGeometry*) GEOSUnion(geom, otherGeom);
485 GeosGeometry *pointOnSurface()
487 GEOSGeom geom = (GEOSGeom) self;
488 return (GeosGeometry*) GEOSPointOnSurface(geom);
491 GeosGeometry *getCentroid()
493 GEOSGeom geom = (GEOSGeom) self;
494 return (GeosGeometry*) GEOSGetCentroid(geom);
497 GeosGeometry *getEnvelope()
499 GEOSGeom geom = (GEOSGeom) self;
500 return (GeosGeometry*) GEOSEnvelope(geom);
503 char *relate(GeosGeometry *other)
505 GEOSGeom geom = (GEOSGeom) self;
506 GEOSGeom otherGeom = (GEOSGeom) other;
507 return GEOSRelate(geom, otherGeom);
510 /* TODO - expose GEOSPolygonize*/
511 GeosGeometry *lineMerge()
513 GEOSGeom geom = (GEOSGeom) self;
514 return GEOSLineMerge(geom);
517 GeosGeometry *simplify(double tolerance)
519 GEOSGeom geom = (GEOSGeom) self;
520 return (GeosGeometry*) GEOSSimplify(geom, tolerance);
523 GeosGeometry *topologyPreserveSimplify(double tolerance)
525 GEOSGeom geom = (GEOSGeom) self;
526 return (GeosGeometry*) GEOSTopologyPreserveSimplify(geom, tolerance);
529 /* Binary predicates - return 2 on exception, 1 on true, 0 on false */
530 bool relatePattern(const GeosGeometry* other, const char *pat)
532 GEOSGeom geom = (GEOSGeom) self;
533 GEOSGeom otherGeom = (GEOSGeom) other;
534 return checkBoolResult(GEOSRelatePattern(geom, otherGeom, pat));
537 bool disjoint(const GeosGeometry* other)
539 GEOSGeom geom = (GEOSGeom) self;
540 GEOSGeom otherGeom = (GEOSGeom) other;
541 return checkBoolResult(GEOSDisjoint(geom, otherGeom));
544 bool touches(const GeosGeometry* other)
546 GEOSGeom geom = (GEOSGeom) self;
547 GEOSGeom otherGeom = (GEOSGeom) other;
548 return checkBoolResult(GEOSTouches(geom, otherGeom));
551 bool intersects(const GeosGeometry* other)
553 GEOSGeom geom = (GEOSGeom) self;
554 GEOSGeom otherGeom = (GEOSGeom) other;
555 return checkBoolResult(GEOSIntersects(geom, otherGeom));
558 bool crosses(const GeosGeometry* other)
560 GEOSGeom geom = (GEOSGeom) self;
561 GEOSGeom otherGeom = (GEOSGeom) other;
562 return checkBoolResult(GEOSCrosses(geom, otherGeom));
565 bool within(const GeosGeometry* other)
567 GEOSGeom geom = (GEOSGeom) self;
568 GEOSGeom otherGeom = (GEOSGeom) other;
569 return checkBoolResult(GEOSWithin(geom, otherGeom));
572 bool contains(const GeosGeometry* other)
574 GEOSGeom geom = (GEOSGeom) self;
575 GEOSGeom otherGeom = (GEOSGeom) other;
576 return checkBoolResult(GEOSContains(geom, otherGeom));
579 bool overlaps(const GeosGeometry* other)
581 GEOSGeom geom = (GEOSGeom) self;
582 GEOSGeom otherGeom = (GEOSGeom) other;
583 return checkBoolResult(GEOSOverlaps(geom, otherGeom));
586 bool equals(const GeosGeometry* other)
588 GEOSGeom geom = (GEOSGeom) self;
589 GEOSGeom otherGeom = (GEOSGeom) other;
590 return checkBoolResult(GEOSEquals(geom, otherGeom));
593 bool equalsExact(const GeosGeometry* other, double tolerance)
595 GEOSGeom geom = (GEOSGeom) self;
596 GEOSGeom otherGeom = (GEOSGeom) other;
597 return checkBoolResult(GEOSEqualsExact(geom, otherGeom, tolerance));
600 /* Unary predicate - return 2 on exception, 1 on true, 0 on false */
601 bool isEmpty()
603 GEOSGeom geom = (GEOSGeom) self;
604 return checkBoolResult(GEOSisEmpty(geom));
607 bool isValid()
609 GEOSGeom geom = (GEOSGeom) self;
610 return checkBoolResult(GEOSisValid(geom));
613 bool isSimple()
615 GEOSGeom geom = (GEOSGeom) self;
616 return checkBoolResult(GEOSisSimple(geom));
619 bool isRing()
621 GEOSGeom geom = (GEOSGeom) self;
622 return checkBoolResult(GEOSisRing(geom));
625 bool hasZ()
627 GEOSGeom geom = (GEOSGeom) self;
628 return checkBoolResult(GEOSHasZ(geom));
631 /* Miscellaneous Functions.
632 Return 0 on exception, 1 otherwise */
633 double area()
635 GEOSGeom geom = (GEOSGeom) self;
636 double result;
638 int code = GEOSArea(geom, &result);
640 if (code == 0)
641 throw std::runtime_error(message);
643 return result;
646 double length()
648 GEOSGeom geom = (GEOSGeom) self;
649 double result;
651 int code = GEOSLength(geom, &result);
653 if (code == 0)
654 throw std::runtime_error(message);
656 return result;
659 double distance(const GeosGeometry* other)
661 GEOSGeom geom = (GEOSGeom) self;
662 GEOSGeom otherGeom = (GEOSGeom) other;
663 double result;
665 int code = GEOSDistance(geom, otherGeom, &result);
667 if (code == 0)
668 throw std::runtime_error(message);
670 return result;
675 class GeosPoint: public GeosGeometry
677 private:
678 GeosPoint();
679 public:
680 %extend
682 ~GeosPoint()
684 GEOSGeom geom = (GEOSGeom) self;
685 GEOSGeom_destroy(geom);
688 const GeosCoordinateSequence* getCoordSeq()
690 GEOSGeom geom = (GEOSGeom) self;
691 const GEOSCoordSeq result = (const GEOSCoordSeq) GEOSGeom_getCoordSeq(geom);
693 if (result == NULL)
694 throw std::runtime_error(message);
696 return (const GeosCoordinateSequence*) result;
701 class GeosLineString: public GeosGeometry
703 public:
704 %extend
706 ~GeosLineString()
708 GEOSGeom geom = (GEOSGeom) self;
709 GEOSGeom_destroy(geom);
712 const GeosCoordinateSequence* getCoordSeq()
714 GEOSGeom geom = (GEOSGeom) self;
715 const GEOSCoordSeq result = (const GEOSCoordSeq) GEOSGeom_getCoordSeq(geom);
717 if (result == NULL)
718 throw std::runtime_error(message);
720 return (const GeosCoordinateSequence*) result;
725 class GeosLinearRing: public GeosGeometry
727 public:
728 %extend
730 ~GeosLinearRing()
732 GEOSGeom geom = (GEOSGeom) self;
733 GEOSGeom_destroy(geom);
736 const GeosCoordinateSequence* getCoordSeq()
738 GEOSGeom geom = (GEOSGeom) self;
739 const GEOSCoordSeq result = (const GEOSCoordSeq) GEOSGeom_getCoordSeq(geom);
741 if (result == NULL)
742 throw std::runtime_error(message);
744 return (const GeosCoordinateSequence*) result;
750 class GeosPolygon: public GeosGeometry
752 public:
753 %extend
755 ~GeosPolygon()
757 GEOSGeom geom = (GEOSGeom) self;
758 GEOSGeom_destroy(geom);
761 const GeosGeometry* getExteriorRing()
763 GEOSGeom geom = (GEOSGeom) self;
764 const GEOSGeom result = (const GEOSGeom) GEOSGetExteriorRing(geom);
766 if (result == NULL)
767 throw std::runtime_error(message);
769 return (const GeosGeometry*) result;
772 size_t getNumInteriorRings()
774 GEOSGeom geom = (GEOSGeom) self;
775 size_t result = GEOSGetNumInteriorRings(geom);
777 if ((int)result == -1)
778 throw std::runtime_error(message);
780 return result;
783 const GeosGeometry* getInteriorRingN(size_t n)
785 GEOSGeom geom = (GEOSGeom) self;
787 size_t size = GEOSGetNumInteriorRings(geom);
789 if (n < 0 || n >= size)
790 throw std::runtime_error("Index out of bounds");
792 const GEOSGeom result = (const GEOSGeom) GEOSGetInteriorRingN(geom, n);
794 if (result == NULL)
795 throw std::runtime_error(message);
797 return (const GeosGeometry*) result;
802 class GeosGeometryCollection: public GeosGeometry
804 public:
805 %extend
807 ~GeosGeometryCollection()
809 GEOSGeom geom = (GEOSGeom) self;
810 GEOSGeom_destroy(geom);
813 const GeosGeometry* getGeometryN(size_t n)
815 GEOSGeom geom = (GEOSGeom) self;
816 const GEOSGeom result = (const GEOSGeom) GEOSGetGeometryN(geom, n);
818 if (result == NULL)
819 throw std::runtime_error(message);
821 return (const GeosGeometry*) result;
826 class GeosMultiPoint: public GeosGeometryCollection
828 public:
829 %extend
831 ~GeosMultiPoint()
833 GEOSGeom geom = (GEOSGeom) self;
834 GEOSGeom_destroy(geom);
839 class GeosMultiLineString: public GeosGeometryCollection
841 public:
842 %extend
844 ~GeosMultiLineString()
846 GEOSGeom geom = (GEOSGeom) self;
847 GEOSGeom_destroy(geom);
852 class GeosMultiLinearRing: public GeosGeometryCollection
854 public:
855 %extend
857 ~GeosMultiLinearRing()
859 GEOSGeom geom = (GEOSGeom) self;
860 GEOSGeom_destroy(geom);
865 class GeosMultiPolygon: public GeosGeometryCollection
867 public:
868 %extend
870 ~GeosMultiPolygon()
872 GEOSGeom geom = (GEOSGeom) self;
873 GEOSGeom_destroy(geom);
879 // ==== Geometry Constructors ===========
880 %newobject createPoint;
881 %newobject createLineString;
882 %newobject createLinearRing;
883 %newobject createPolygon;
885 %apply SWIGTYPE *DISOWN {GeosCoordinateSequence *s};
886 %apply SWIGTYPE *DISOWN {GeosLinearRing *shell};
888 %typemap(default) (GeosLinearRing **holes, size_t nholes)
890 $1 = NULL;
891 $2 = 0;
894 %inline %{
895 GeosGeometry *createPoint(GeosCoordinateSequence *s)
897 GEOSCoordSeq coords = (GEOSCoordSeq) s;
898 GEOSGeom geom = GEOSGeom_createPoint(coords);
900 if(geom == NULL)
901 throw std::runtime_error(message);
903 return (GeosGeometry*) geom;
906 GeosGeometry *createLineString(GeosCoordinateSequence *s)
908 GEOSCoordSeq coords = (GEOSCoordSeq) s;
909 GEOSGeom geom = GEOSGeom_createLineString(coords);
911 if(geom == NULL)
912 throw std::runtime_error(message);
914 return (GeosGeometry*) geom;
917 GeosGeometry *createLinearRing(GeosCoordinateSequence *s)
919 GEOSCoordSeq coords = (GEOSCoordSeq) s;
920 GEOSGeom geom = GEOSGeom_createLinearRing(coords);
922 if(geom == NULL)
923 throw std::runtime_error(message);
925 return (GeosGeometry*) geom;
928 GeosGeometry *createPolygon(GeosLinearRing *shell, GeosLinearRing **holes, size_t nholes)
930 GEOSGeom shellGeom = (GEOSGeom) shell;
931 GEOSGeom* holeGeoms = (GEOSGeom*) holes;
932 GEOSGeom geom = GEOSGeom_createPolygon(shellGeom, holeGeoms, nholes);
934 if(geom == NULL)
935 throw std::runtime_error(message);
937 return (GeosGeometry*) geom;
943 * Second argument is an array of GEOSGeom objects.
944 * The caller remains owner of the array, but pointed-to
945 * objects become ownership of the returned GEOSGeom.
947 extern GEOSGeom GEOS_DLL GEOSGeom_createCollection(int type,
948 GEOSGeom *geoms, size_t ngeoms);
951 %clear GeosCoordinateSequence *s;
953 // === Prepared Geometry ===
956 typedef void GeosPreparedGeometry;
959 %rename (Prepared) GeosPreparedGeometry;
960 class GeosPreparedGeometry
962 public:
963 %extend
965 GeosPreparedGeometry(const GeosGeometry *source)
967 const GEOSPreparedGeometry *prep = GEOSPrepare((const GEOSGeometry *)source);
968 if(prep == NULL)
969 throw std::runtime_error(message);
970 return (GeosPreparedGeometry *) prep;
973 ~GeosPreparedGeometry()
975 GEOSPreparedGeometry *prep = (GEOSPreparedGeometry *) self;
976 return GEOSPreparedGeom_destroy(prep);
979 bool contains (const GeosGeometry* other)
981 GEOSPreparedGeometry *prep = (GEOSPreparedGeometry *) self;
982 GEOSGeom otherGeom = (GEOSGeom) other;
983 return checkBoolResult(GEOSPreparedContains(prep, otherGeom));
986 bool containsProperly(const GeosGeometry* other)
988 GEOSPreparedGeometry *prep = (GEOSPreparedGeometry *) self;
989 GEOSGeom otherGeom = (GEOSGeom) other;
990 return checkBoolResult(GEOSPreparedContainsProperly(prep, otherGeom));
993 bool covers (const GeosGeometry* other)
995 GEOSPreparedGeometry *prep = (GEOSPreparedGeometry *) self;
996 GEOSGeom otherGeom = (GEOSGeom) other;
997 return checkBoolResult(GEOSPreparedCovers(prep, otherGeom));
1000 bool intersects (const GeosGeometry* other)
1002 GEOSPreparedGeometry *prep = (GEOSPreparedGeometry *) self;
1003 GEOSGeom otherGeom = (GEOSGeom) other;
1004 return checkBoolResult(GEOSPreparedIntersects(prep, otherGeom));
1009 // === STRtree ===
1012 typedef void GeosSTRtree;
1013 /* GeosIndexItem typedef'd here so it can be %typemap(typecheck)'d
1014 as a native object by each language specially */
1015 typedef void *GeosIndexItem;
1016 typedef GEOSQueryCallback GeosQueryCallback;
1019 %rename (STRtree) GeosSTRtree;
1020 class GeosSTRtree
1022 public:
1023 %extend
1025 %typemap(default) int nodeCapacity {
1026 $1 = 10;
1028 GeosSTRtree(int nodeCapacity)
1030 GEOSSTRtree *tree = GEOSSTRtree_create(nodeCapacity);
1031 if(tree == NULL)
1032 throw std::runtime_error(message);
1033 return (GeosSTRtree *) tree;
1036 ~GeosSTRtree()
1038 GEOSSTRtree *tree = (GEOSSTRtree *) self;
1039 return GEOSSTRtree_destroy(tree);
1042 void insert (const GeosGeometry* g, GeosIndexItem item)
1044 GEOSSTRtree *tree = (GEOSSTRtree *) self;
1045 const GEOSGeometry *geom = (const GEOSGeometry *) g;
1046 GEOSSTRtree_insert(tree, geom, item);
1049 void remove (const GeosGeometry* g, GeosIndexItem item)
1051 GEOSSTRtree *tree = (GEOSSTRtree *) self;
1052 const GEOSGeometry *geom = (const GEOSGeometry *) g;
1053 GEOSSTRtree_remove(tree, geom, item);
1056 void query (const GeosGeometry* g, GeosQueryCallback callback,
1057 GeosIndexItem accumulator)
1059 GEOSSTRtree *tree = (GEOSSTRtree *) self;
1060 const GEOSGeometry *geom = (const GEOSGeometry *) g;
1061 GEOSSTRtree_query(tree, geom, callback, accumulator);
1064 void iterate (GeosQueryCallback callback, GeosIndexItem accumulator)
1066 GEOSSTRtree *tree = (GEOSSTRtree *) self;
1067 GEOSSTRtree_iterate(tree, callback, accumulator);
1073 // === Input/Output ===
1075 /* This typemap allows the scripting language to pass in buffers
1076 to the geometry write methods. */
1077 %typemap(in) (const unsigned char* wkb, size_t size) (int alloc = 0)
1079 /* %typemap(in) (const unsigned char* wkb, size_t size) (int alloc = 0) */
1080 if (SWIG_AsCharPtrAndSize($input, (char**)&$1, &$2, &alloc) != SWIG_OK)
1081 SWIG_exception(SWIG_RuntimeError, "Expecting a string");
1082 /* Don't want to include last null character! */
1083 $2--;
1086 /* These three type maps are for geomToWKB and geomToHEX. We need
1087 to ignore the size input argument, then create a new string in the
1088 scripting language of the correct size, and then free the
1089 provided string. */
1091 /* set the size parameter to a temporary variable. */
1092 %typemap(in, numinputs=0) size_t *size (size_t temp = 0)
1094 /* %typemap(in, numinputs=0) size_t *size (size_t temp = 0) */
1095 $1 = &temp;
1098 /* Disable SWIG's normally generated code so we can replace it
1099 with the argout typemap below. */
1100 %typemap(out) unsigned char*
1102 /* %typemap(out) unsigned char* */
1105 /* Create a new target string of the correct size. */
1106 %typemap(argout) size_t *size
1108 /* %typemap(argout) size_t *size */
1109 $result = SWIG_FromCharPtrAndSize((const char*)result, *$1);
1112 /* Free the c-string returned by the function. */
1113 %typemap(freearg) size_t *size
1115 /* %typemap(freearg) size_t *size */
1116 std::free(result);
1120 %newobject GeosWktReader::read;
1121 class GeosWktReader
1123 public:
1124 %extend
1126 GeosWktReader()
1128 return GEOSWKTReader_create();
1131 ~GeosWktReader()
1133 GEOSWKTReader *reader = (GEOSWKTReader*) self;
1134 GEOSWKTReader_destroy(reader);
1137 GeosGeometry* read(const char *wkt)
1139 if(wkt == NULL)
1140 throw std::runtime_error("Trying to create geometry from a NULL string");
1142 GEOSWKTReader *reader = (GEOSWKTReader*) self;
1143 GEOSGeometry *geom = GEOSWKTReader_read(reader, wkt);
1145 if(geom == NULL)
1146 throw std::runtime_error(message);
1148 return (GeosGeometry*) geom;
1153 class GeosWktWriter
1155 public:
1156 %extend
1158 GeosWktWriter()
1160 return GEOSWKTWriter_create();
1163 ~GeosWktWriter()
1165 GEOSWKTWriter *writer = (GEOSWKTWriter*) self;
1166 GEOSWKTWriter_destroy(writer);
1169 char* write(const GeosGeometry* g)
1171 GEOSWKTWriter *writer = (GEOSWKTWriter*) self;
1172 GEOSGeom geom = (GEOSGeom) g;
1173 return GEOSWKTWriter_write(writer, geom);
1179 %newobject GeosWkbReader::read;
1180 %newobject GeosWkbReader::readHEX;
1181 class GeosWkbReader
1183 public:
1184 %extend
1186 GeosWkbReader()
1188 return GEOSWKBReader_create();
1191 ~GeosWkbReader()
1193 GEOSWKBReader *reader = (GEOSWKBReader*) self;
1194 GEOSWKBReader_destroy(reader);
1197 GeosGeometry* read(const unsigned char *wkb, size_t size)
1199 if(wkb == NULL)
1200 throw std::runtime_error("Trying to create geometry from a NULL string");
1202 GEOSWKBReader *reader = (GEOSWKBReader*) self;
1203 GEOSGeometry *geom = GEOSWKBReader_read(reader, wkb, size);
1205 if(geom == NULL)
1206 throw std::runtime_error(message);
1208 return (GeosGeometry*) geom;
1211 GeosGeometry* readHEX(const unsigned char *wkb, size_t size)
1213 if(wkb == NULL)
1214 throw std::runtime_error("Trying to create geometry from a NULL string");
1216 GEOSWKBReader *reader = (GEOSWKBReader*) self;
1217 GEOSGeometry *geom = GEOSWKBReader_readHEX(reader, wkb, size);
1219 if(geom == NULL)
1220 throw std::runtime_error(message);
1222 return (GeosGeometry*) geom;
1227 class GeosWkbWriter
1229 public:
1230 %extend
1232 GeosWkbWriter()
1234 return GEOSWKBWriter_create();
1237 ~GeosWkbWriter()
1239 GEOSWKBWriter *writer = (GEOSWKBWriter*) self;
1240 GEOSWKBWriter_destroy(writer);
1243 int getOutputDimension()
1245 GEOSWKBWriter *writer = (GEOSWKBWriter*) self;
1246 return GEOSWKBWriter_getOutputDimension(writer);
1249 void setOutputDimension(int newDimension)
1251 GEOSWKBWriter *writer = (GEOSWKBWriter*) self;
1252 GEOSWKBWriter_setOutputDimension(writer, newDimension);
1255 int getByteOrder()
1257 GEOSWKBWriter *writer = (GEOSWKBWriter*) self;
1258 return GEOSWKBWriter_getByteOrder(writer);
1261 void setByteOrder(int newByteOrder)
1263 GEOSWKBWriter *writer = (GEOSWKBWriter*) self;
1264 return GEOSWKBWriter_setByteOrder(writer, newByteOrder);
1267 bool getIncludeSRID()
1269 GEOSWKBWriter *writer = (GEOSWKBWriter*) self;
1270 return GEOSWKBWriter_getIncludeSRID(writer);
1273 void setIncludeSRID(bool newIncludeSRID)
1275 GEOSWKBWriter *writer = (GEOSWKBWriter*) self;
1276 return GEOSWKBWriter_setIncludeSRID(writer, newIncludeSRID);
1279 unsigned char* write(const GeosGeometry* g, size_t *size)
1281 GEOSWKBWriter *writer = (GEOSWKBWriter*) self;
1282 GEOSGeom geom = (GEOSGeom) g;
1283 return GEOSWKBWriter_write(writer, geom, size);
1286 unsigned char* writeHEX(const GeosGeometry* g, size_t *size)
1288 GEOSWKBWriter *writer = (GEOSWKBWriter*) self;
1289 GEOSGeom geom = (GEOSGeom) g;
1290 return GEOSWKBWriter_writeHEX(writer, geom, size);