source: trunk/swig/geos.i.in

Last change on this file was 3475, checked in by strk, 5 years ago

Fix out-of-place build for python binding (#332)

File size: 31.0 KB
Line 
1/* =========================================================================
2 * Copyright 2005-2007 Charlie Savage, cfis@interserv.com
3 *
4 * Interface for a SWIG generated geos module.
5 *
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.
10 *
11 * ========================================================================= */
12
13%module geos
14
15%include "attribute.i"
16%include "exception.i"
17%include "std_string.i"
18%include "std_vector.i"
19%include "std_except.i"
20
21%{
22#include "geos_c.h"
23/* Needed for va_start, etc. */
24#include <stdarg.h>
25%}
26
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@"
35
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@"
42
43/* Supported geometry types */
44enum 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
53};
54
55enum GEOSByteOrders {
56        GEOS_WKB_XDR = 0, /* Big Endian */
57        GEOS_WKB_NDR = 1 /* Little Endian */
58};
59
60/* From OffsetCurveSetBuilder.h for buffer operations. */
61%{
62    static const int DEFAULT_QUADRANT_SEGMENTS=8;
63%}
64
65/* Message and Error Handling */
66%{
67
68/* This is not thread safe ! */
69static const int MESSAGE_SIZE = 1000;
70static char message[MESSAGE_SIZE];
71
72void noticeHandler(const char *fmt, ...)
73{
74    va_list args;
75    va_start(args, fmt);
76    vsnprintf(message, sizeof(message) - 1, fmt, args);
77    va_end(args);
78}
79
80void errorHandler(const char *fmt, ...)
81{
82    va_list args;
83    va_start(args, fmt);
84    vsnprintf(message, sizeof(message) - 1, fmt, args);
85    va_end(args);
86}
87%}
88
89
90/* First initialize geos */
91%init %{
92    initGEOS(noticeHandler, errorHandler);
93%}
94
95
96/* Module level methods */
97%rename("version") GEOSversion;
98const char *GEOSversion();
99
100/* Exception handler */
101%exception
102{
103    try
104    {
105        $action
106    }
107    catch (const std::exception& e)
108    {
109        SWIG_exception(SWIG_RuntimeError, e.what());
110    }
111}
112
113
114/* ==============  Language Specific Files ============ */
115
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) */
123
124
125#ifdef SWIGPYTHON
126        %include @top_srcdir@/swig/python/python.i
127#endif
128
129#ifdef SWIGRUBY
130        %include @top_srcdir@/swig/ruby/ruby.i
131#endif
132
133
134
135
136// ===  CoordinateSequence ===
137%{
138typedef void GeosCoordinateSequence;
139
140void checkCoordSeqBounds(const GEOSCoordSeq coordSeq, const size_t index)
141{
142    unsigned int size = 0;
143    GEOSCoordSeq_getSize(coordSeq, &size);
144
145    if (index < 0 || index >= size)
146        throw std::runtime_error("Index out of bounds");
147}
148%}
149
150%newobject GeosCoordinateSequence::clone;
151%rename (CoordinateSequence) GeosCoordinateSequence;
152class GeosCoordinateSequence
153{
154public:
155%extend
156{
157    GeosCoordinateSequence(size_t size, size_t dims)
158    {
159        return (GeosCoordinateSequence*) GEOSCoordSeq_create(size, dims);
160    }
161
162    ~GeosCoordinateSequence()
163    {
164        GEOSCoordSeq coords = (GEOSCoordSeq) self;
165        return GEOSCoordSeq_destroy(coords);
166    }
167
168    GeosCoordinateSequence *clone()
169    {
170        GEOSCoordSeq coords = (GEOSCoordSeq) self;
171        return (GeosCoordinateSequence*) GEOSCoordSeq_clone(coords);
172    }
173
174    int setX(size_t idx, double val)
175    {
176        GEOSCoordSeq coords = (GEOSCoordSeq) self;
177        checkCoordSeqBounds(coords, idx);
178        return GEOSCoordSeq_setX(coords, idx, val);
179    }
180
181    int setY(size_t idx, double val)
182    {
183        GEOSCoordSeq coords = (GEOSCoordSeq) self;
184        checkCoordSeqBounds(coords, idx);
185        return GEOSCoordSeq_setY(coords, idx, val);
186    }
187
188    int setZ(size_t idx, double val)
189    {
190        GEOSCoordSeq coords = (GEOSCoordSeq) self;
191        checkCoordSeqBounds(coords, idx);
192        return GEOSCoordSeq_setZ(coords, idx, val);
193    }
194
195    int setOrdinate(size_t idx, size_t dim, double val)
196    {
197        GEOSCoordSeq coords = (GEOSCoordSeq) self;
198        checkCoordSeqBounds(coords, idx);
199        return GEOSCoordSeq_setOrdinate(coords, idx, dim, val);
200    }
201
202    double getX(size_t idx)
203    {
204        double result;
205        GEOSCoordSeq coords = (GEOSCoordSeq) self;
206        checkCoordSeqBounds(coords, idx);
207        GEOSCoordSeq_getX(coords, idx, &result);
208        return result;
209    }
210
211    double getY(size_t idx)
212    {
213        double result;
214        GEOSCoordSeq coords = (GEOSCoordSeq) self;
215        checkCoordSeqBounds(coords, idx);
216        GEOSCoordSeq_getY(coords, idx, &result);
217        return result;
218    }
219   
220    double getZ(size_t idx)
221    {
222        double result;
223        GEOSCoordSeq coords = (GEOSCoordSeq) self;
224        checkCoordSeqBounds(coords, idx);
225        GEOSCoordSeq_getZ(coords, idx, &result);
226        return result;
227    }
228   
229    double getOrdinate(size_t idx, size_t dim)
230    {
231        double result;
232        GEOSCoordSeq coords = (GEOSCoordSeq) self;
233        checkCoordSeqBounds(coords, idx);
234        GEOSCoordSeq_getOrdinate(coords, idx, dim, &result);
235        return result;
236    }
237
238    unsigned int getSize()
239    {
240        unsigned int result;
241        GEOSCoordSeq coords = (GEOSCoordSeq) self;
242        GEOSCoordSeq_getSize(coords, &result);
243        return result;
244    }
245
246    unsigned int getDimensions()
247    {
248        unsigned int result;
249        GEOSCoordSeq coords = (GEOSCoordSeq) self;
250        GEOSCoordSeq_getDimensions(coords, &result);
251        return result;
252    }
253}
254};
255
256
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;
272
273
274%rename("union") GeosGeometry::geomUnion;
275
276%{
277typedef void GeosGeometry;
278typedef void GeosPoint;
279typedef void GeosLineString;
280typedef void GeosLinearRing;
281typedef void GeosPolygon;
282typedef void GeosGeometryCollection;
283typedef void GeosMultiPoint;
284typedef void GeosMultiLineString;
285typedef void GeosMultiLinearRing;
286typedef void GeosMultiPolygon;
287
288typedef void GeosWktReader;
289typedef void GeosWktWriter;
290typedef void GeosWkbReader;
291typedef void GeosWkbWriter;
292%}
293
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;
307
308
309%typemap(out) GeosGeometry*
310{
311    /* %typemap(out) GeosGeometry */
312
313    if ($1 == NULL)
314        SWIG_exception(SWIG_RuntimeError, message);
315
316    GeosGeometry *geom = $1;
317    GEOSGeomTypes geomId = (GEOSGeomTypes)GEOSGeomTypeId((GEOSGeom) geom);
318
319    switch (geomId)
320    {
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;
345    }
346}
347
348/* Setup a default typemap for buffer. */
349%typemap(default) int quadsegs
350{
351    $1 = DEFAULT_QUADRANT_SEGMENTS;
352}
353
354%{
355bool checkBoolResult(char result)
356{
357    int intResult = (int) result;
358
359    if (intResult == 1)
360        return true;
361    else if (intResult == 0)
362        return false;
363    else
364        throw std::runtime_error(message);
365}
366%}
367
368%newobject GeosGeometry::clone;
369class GeosGeometry
370{
371private:
372    GeosGeometry();
373public:
374%extend
375{
376    ~GeosGeometry()
377    {
378        GEOSGeom geom = (GEOSGeom) self;
379        GEOSGeom_destroy(geom);
380    }
381
382    GeosGeometry *clone()
383    {
384        GEOSGeom geom = (GEOSGeom) self;
385        return GEOSGeom_clone(geom);
386    }
387   
388    char *geomType()
389    {
390        GEOSGeom geom = (GEOSGeom) self;
391        return GEOSGeomType(geom);
392    }
393
394    int typeId()
395    {
396        GEOSGeom geom = (GEOSGeom) self;
397        return GEOSGeomTypeId(geom);
398    }
399   
400    void normalize()
401    {
402        GEOSGeom geom = (GEOSGeom) self;
403        int result = GEOSNormalize(geom);
404
405        if (result == -1)
406            throw std::runtime_error(message);
407    }
408
409    int getSRID()
410    {
411        GEOSGeom geom = (GEOSGeom) self;
412        return GEOSGetSRID(geom);
413    }
414
415    void setSRID(int SRID)
416    {
417        GEOSGeom geom = (GEOSGeom) self;
418        return GEOSSetSRID(geom, SRID);
419    }
420
421    size_t getDimensions()
422    {
423        GEOSGeom geom = (GEOSGeom) self;
424        return GEOSGeom_getDimensions(geom);
425    }
426
427    size_t getNumGeometries()
428    {
429        GEOSGeom geom = (GEOSGeom) self;
430        size_t result = GEOSGetNumGeometries(geom);
431       
432        if ((int)result == -1)
433            throw std::runtime_error(message);
434
435        return result;
436    }
437
438    /* Topology Operations */
439    GeosGeometry *intersection(GeosGeometry *other)
440    {
441        GEOSGeom geom = (GEOSGeom) self;
442        GEOSGeom otherGeom = (GEOSGeom) other;
443        return (GeosGeometry*) GEOSIntersection(geom, otherGeom);
444    }
445
446    GeosGeometry *buffer(double width, int quadsegs)
447    {
448        GEOSGeom geom = (GEOSGeom) self;
449        return (GeosGeometry*) GEOSBuffer(geom, width, quadsegs);
450    }
451
452    GeosGeometry *convexHull()
453    {
454        GEOSGeom geom = (GEOSGeom) self;
455        return (GeosGeometry*) GEOSConvexHull(geom);
456    }
457
458    GeosGeometry *difference(GeosGeometry *other)
459    {
460        GEOSGeom geom = (GEOSGeom) self;
461        GEOSGeom otherGeom = (GEOSGeom) other;
462        return (GeosGeometry*) GEOSDifference(geom, otherGeom);
463    }
464
465    GeosGeometry *symDifference(GeosGeometry *other)
466    {
467        GEOSGeom geom = (GEOSGeom) self;
468        GEOSGeom otherGeom = (GEOSGeom) other;
469        return (GeosGeometry*) GEOSSymDifference(geom, otherGeom);
470    }
471
472    GeosGeometry *boundary()
473    {
474        GEOSGeom geom = (GEOSGeom) self;
475        return (GeosGeometry*) GEOSBoundary(geom);
476    }
477
478    GeosGeometry *geomUnion(GeosGeometry *other)
479    {
480        GEOSGeom geom = (GEOSGeom) self;
481        GEOSGeom otherGeom = (GEOSGeom) other;
482        return (GeosGeometry*) GEOSUnion(geom, otherGeom);
483    }
484   
485    GeosGeometry *pointOnSurface()
486    {
487        GEOSGeom geom = (GEOSGeom) self;
488        return (GeosGeometry*) GEOSPointOnSurface(geom);
489    }
490
491    GeosGeometry *getCentroid()
492    {
493        GEOSGeom geom = (GEOSGeom) self;
494        return (GeosGeometry*) GEOSGetCentroid(geom);
495    }
496
497    GeosGeometry *getEnvelope()
498    {
499        GEOSGeom geom = (GEOSGeom) self;
500        return (GeosGeometry*) GEOSEnvelope(geom);
501    }
502
503    char *relate(GeosGeometry *other)
504    {
505        GEOSGeom geom = (GEOSGeom) self;
506        GEOSGeom otherGeom = (GEOSGeom) other;
507        return GEOSRelate(geom, otherGeom);
508    }
509
510    /* TODO - expose GEOSPolygonize*/
511    GeosGeometry *lineMerge()
512    {
513        GEOSGeom geom = (GEOSGeom) self;
514        return GEOSLineMerge(geom);
515    }
516
517    GeosGeometry *simplify(double tolerance)
518    {
519        GEOSGeom geom = (GEOSGeom) self;
520        return (GeosGeometry*) GEOSSimplify(geom, tolerance);
521    }
522
523    GeosGeometry *topologyPreserveSimplify(double tolerance)
524    {
525        GEOSGeom geom = (GEOSGeom) self;
526        return (GeosGeometry*) GEOSTopologyPreserveSimplify(geom, tolerance);
527    }
528
529    /* Binary predicates - return 2 on exception, 1 on true, 0 on false */
530    bool relatePattern(const GeosGeometry* other, const char *pat)
531    {
532        GEOSGeom geom = (GEOSGeom) self;
533        GEOSGeom otherGeom = (GEOSGeom) other;
534        return checkBoolResult(GEOSRelatePattern(geom, otherGeom, pat));
535    }
536
537    bool disjoint(const GeosGeometry* other)
538    {
539        GEOSGeom geom = (GEOSGeom) self;
540        GEOSGeom otherGeom = (GEOSGeom) other;
541        return checkBoolResult(GEOSDisjoint(geom, otherGeom));
542    }
543
544    bool touches(const GeosGeometry* other)
545    {
546        GEOSGeom geom = (GEOSGeom) self;
547        GEOSGeom otherGeom = (GEOSGeom) other;
548        return checkBoolResult(GEOSTouches(geom, otherGeom));
549    }
550
551    bool intersects(const GeosGeometry* other)
552    {
553        GEOSGeom geom = (GEOSGeom) self;
554        GEOSGeom otherGeom = (GEOSGeom) other;
555        return checkBoolResult(GEOSIntersects(geom, otherGeom));
556    }
557
558    bool crosses(const GeosGeometry* other)
559    {
560        GEOSGeom geom = (GEOSGeom) self;
561        GEOSGeom otherGeom = (GEOSGeom) other;
562        return checkBoolResult(GEOSCrosses(geom, otherGeom));
563    }
564
565    bool within(const GeosGeometry* other)
566    {
567        GEOSGeom geom = (GEOSGeom) self;
568        GEOSGeom otherGeom = (GEOSGeom) other;
569        return checkBoolResult(GEOSWithin(geom, otherGeom));
570    }
571
572    bool contains(const GeosGeometry* other)
573    {
574        GEOSGeom geom = (GEOSGeom) self;
575        GEOSGeom otherGeom = (GEOSGeom) other;
576        return checkBoolResult(GEOSContains(geom, otherGeom));
577    }
578
579    bool overlaps(const GeosGeometry* other)
580    {
581        GEOSGeom geom = (GEOSGeom) self;
582        GEOSGeom otherGeom = (GEOSGeom) other;
583        return checkBoolResult(GEOSOverlaps(geom, otherGeom));
584    }
585
586    bool equals(const GeosGeometry* other)
587    {
588        GEOSGeom geom = (GEOSGeom) self;
589        GEOSGeom otherGeom = (GEOSGeom) other;
590        return checkBoolResult(GEOSEquals(geom, otherGeom));
591    }
592
593    bool equalsExact(const GeosGeometry* other, double tolerance)
594    {
595        GEOSGeom geom = (GEOSGeom) self;
596        GEOSGeom otherGeom = (GEOSGeom) other;
597        return checkBoolResult(GEOSEqualsExact(geom, otherGeom, tolerance));
598    }
599
600    /* Unary predicate - return 2 on exception, 1 on true, 0 on false */
601    bool isEmpty()
602    {
603        GEOSGeom geom = (GEOSGeom) self;
604        return checkBoolResult(GEOSisEmpty(geom));
605    }
606
607    bool isValid()
608    {
609        GEOSGeom geom = (GEOSGeom) self;
610        return checkBoolResult(GEOSisValid(geom));
611    }
612
613    bool isSimple()
614    {
615        GEOSGeom geom = (GEOSGeom) self;
616        return checkBoolResult(GEOSisSimple(geom));
617    }
618
619    bool isRing()
620    {
621        GEOSGeom geom = (GEOSGeom) self;
622        return checkBoolResult(GEOSisRing(geom));
623    }
624
625    bool hasZ()
626    {
627        GEOSGeom geom = (GEOSGeom) self;
628        return checkBoolResult(GEOSHasZ(geom));
629    }
630
631    /* Miscellaneous Functions.
632       Return 0 on exception, 1 otherwise */
633    double area()
634    {
635        GEOSGeom geom = (GEOSGeom) self;
636        double result;
637
638        int code = GEOSArea(geom, &result);
639
640        if (code == 0)
641            throw std::runtime_error(message);
642
643        return result;
644    }
645
646    double length()
647    {
648        GEOSGeom geom = (GEOSGeom) self;
649        double result;
650
651        int code = GEOSLength(geom, &result);
652
653        if (code == 0)
654            throw std::runtime_error(message);
655
656        return result;
657    }
658
659    double distance(const GeosGeometry* other)
660    {
661        GEOSGeom geom = (GEOSGeom) self;
662        GEOSGeom otherGeom = (GEOSGeom) other;
663        double result;
664
665        int code = GEOSDistance(geom, otherGeom, &result);
666
667        if (code == 0)
668            throw std::runtime_error(message);
669
670        return result;
671    }
672}
673};
674
675class GeosPoint: public GeosGeometry
676{
677private:
678    GeosPoint();
679public:
680%extend
681{   
682    ~GeosPoint()
683    {
684        GEOSGeom geom = (GEOSGeom) self;
685        GEOSGeom_destroy(geom);
686    }
687   
688    const GeosCoordinateSequence* getCoordSeq()
689    {
690        GEOSGeom geom = (GEOSGeom) self;
691        const GEOSCoordSeq result = (const GEOSCoordSeq) GEOSGeom_getCoordSeq(geom);
692
693        if (result == NULL)
694            throw std::runtime_error(message);
695
696        return (const GeosCoordinateSequence*) result;
697    }
698}
699};
700
701class GeosLineString: public GeosGeometry
702{
703public:
704%extend
705{   
706    ~GeosLineString()
707    {
708        GEOSGeom geom = (GEOSGeom) self;
709        GEOSGeom_destroy(geom);
710    }
711   
712    const GeosCoordinateSequence* getCoordSeq()
713    {
714        GEOSGeom geom = (GEOSGeom) self;
715        const GEOSCoordSeq result = (const GEOSCoordSeq) GEOSGeom_getCoordSeq(geom);
716
717        if (result == NULL)
718            throw std::runtime_error(message);
719
720        return (const GeosCoordinateSequence*) result;
721    }
722}
723};
724
725class GeosLinearRing: public GeosGeometry
726{
727public:
728%extend
729{   
730    ~GeosLinearRing()
731    {
732        GEOSGeom geom = (GEOSGeom) self;
733        GEOSGeom_destroy(geom);
734    }
735   
736    const GeosCoordinateSequence* getCoordSeq()
737    {
738        GEOSGeom geom = (GEOSGeom) self;
739        const GEOSCoordSeq result = (const GEOSCoordSeq) GEOSGeom_getCoordSeq(geom);
740
741        if (result == NULL)
742            throw std::runtime_error(message);
743
744        return (const GeosCoordinateSequence*) result;
745    }
746}
747};
748
749
750class GeosPolygon: public GeosGeometry
751{
752public:
753%extend
754{   
755    ~GeosPolygon()
756    {
757        GEOSGeom geom = (GEOSGeom) self;
758        GEOSGeom_destroy(geom);
759    }
760   
761    const GeosGeometry* getExteriorRing()
762    {
763        GEOSGeom geom = (GEOSGeom) self;
764        const GEOSGeom result = (const GEOSGeom) GEOSGetExteriorRing(geom);
765
766        if (result == NULL)
767            throw std::runtime_error(message);
768
769        return (const GeosGeometry*) result;
770    }
771
772    size_t getNumInteriorRings()
773    {
774        GEOSGeom geom = (GEOSGeom) self;
775        size_t result = GEOSGetNumInteriorRings(geom);
776
777        if ((int)result == -1)
778            throw std::runtime_error(message);
779
780        return result;
781    }
782
783    const GeosGeometry* getInteriorRingN(size_t n)
784    {
785        GEOSGeom geom = (GEOSGeom) self;
786
787        size_t size = GEOSGetNumInteriorRings(geom);
788
789        if (n < 0 || n >= size)
790            throw std::runtime_error("Index out of bounds");
791
792        const GEOSGeom result = (const GEOSGeom) GEOSGetInteriorRingN(geom, n);
793
794        if (result == NULL)
795            throw std::runtime_error(message);
796
797        return (const GeosGeometry*) result;
798    }
799}
800};
801
802class GeosGeometryCollection: public GeosGeometry
803{
804public:
805%extend
806{   
807    ~GeosGeometryCollection()
808    {
809        GEOSGeom geom = (GEOSGeom) self;
810        GEOSGeom_destroy(geom);
811    }
812   
813    const GeosGeometry* getGeometryN(size_t n)
814    {
815        GEOSGeom geom = (GEOSGeom) self;
816        const GEOSGeom result = (const GEOSGeom) GEOSGetGeometryN(geom, n);
817
818        if (result == NULL)
819            throw std::runtime_error(message);
820
821        return (const GeosGeometry*) result;
822    }
823}
824};
825
826class GeosMultiPoint: public GeosGeometryCollection
827{
828public:
829%extend
830{   
831    ~GeosMultiPoint()
832    {
833        GEOSGeom geom = (GEOSGeom) self;
834        GEOSGeom_destroy(geom);
835    }
836}
837};
838
839class GeosMultiLineString: public GeosGeometryCollection
840{
841public:
842%extend
843{   
844    ~GeosMultiLineString()
845    {
846        GEOSGeom geom = (GEOSGeom) self;
847        GEOSGeom_destroy(geom);
848    }
849}
850};
851
852class GeosMultiLinearRing: public GeosGeometryCollection
853{
854public:
855%extend
856{   
857    ~GeosMultiLinearRing()
858    {
859        GEOSGeom geom = (GEOSGeom) self;
860        GEOSGeom_destroy(geom);
861    }
862}
863};
864
865class GeosMultiPolygon: public GeosGeometryCollection
866{
867public:
868%extend
869{   
870    ~GeosMultiPolygon()
871    {
872        GEOSGeom geom = (GEOSGeom) self;
873        GEOSGeom_destroy(geom);
874    }
875}
876};
877
878
879// ==== Geometry Constructors ===========
880%newobject createPoint;
881%newobject createLineString;
882%newobject createLinearRing;
883%newobject createPolygon;
884
885%apply SWIGTYPE *DISOWN {GeosCoordinateSequence *s};
886%apply SWIGTYPE *DISOWN {GeosLinearRing *shell};
887
888%typemap(default) (GeosLinearRing **holes, size_t nholes)
889{
890    $1 = NULL;
891    $2 = 0;
892}
893
894%inline %{
895GeosGeometry *createPoint(GeosCoordinateSequence *s)
896{
897    GEOSCoordSeq coords = (GEOSCoordSeq) s;
898    GEOSGeom geom = GEOSGeom_createPoint(coords);
899
900    if(geom == NULL)
901        throw std::runtime_error(message);
902
903    return (GeosGeometry*) geom;
904}
905
906GeosGeometry *createLineString(GeosCoordinateSequence *s)
907{
908    GEOSCoordSeq coords = (GEOSCoordSeq) s;
909    GEOSGeom geom = GEOSGeom_createLineString(coords);
910
911    if(geom == NULL)
912        throw std::runtime_error(message);
913
914    return (GeosGeometry*) geom;
915}
916
917GeosGeometry *createLinearRing(GeosCoordinateSequence *s)
918{
919    GEOSCoordSeq coords = (GEOSCoordSeq) s;
920    GEOSGeom geom = GEOSGeom_createLinearRing(coords);
921
922    if(geom == NULL)
923        throw std::runtime_error(message);
924
925    return (GeosGeometry*) geom;
926}
927
928GeosGeometry *createPolygon(GeosLinearRing *shell, GeosLinearRing **holes, size_t nholes)
929{
930    GEOSGeom shellGeom = (GEOSGeom) shell;
931    GEOSGeom* holeGeoms = (GEOSGeom*) holes;
932    GEOSGeom geom = GEOSGeom_createPolygon(shellGeom, holeGeoms, nholes);
933
934    if(geom == NULL)
935        throw std::runtime_error(message);
936
937    return (GeosGeometry*) geom;
938}
939
940%}
941
942/*
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.
946 
947extern GEOSGeom GEOS_DLL GEOSGeom_createCollection(int type,
948        GEOSGeom *geoms, size_t ngeoms);
949*/
950
951%clear GeosCoordinateSequence *s;
952
953// === Prepared Geometry ===
954
955%{
956typedef void GeosPreparedGeometry;
957%}
958
959%rename (Prepared) GeosPreparedGeometry;
960class GeosPreparedGeometry
961{
962public:
963%extend
964{
965    GeosPreparedGeometry(const GeosGeometry *source)
966    {
967        const GEOSPreparedGeometry *prep = GEOSPrepare((const GEOSGeometry *)source);
968        if(prep == NULL)
969            throw std::runtime_error(message);
970        return (GeosPreparedGeometry *) prep;
971    }
972
973    ~GeosPreparedGeometry()
974    {
975        GEOSPreparedGeometry *prep = (GEOSPreparedGeometry *) self;
976        return GEOSPreparedGeom_destroy(prep);
977    }
978
979    bool contains (const GeosGeometry* other)
980    {
981        GEOSPreparedGeometry *prep = (GEOSPreparedGeometry *) self;
982        GEOSGeom otherGeom = (GEOSGeom) other;
983        return checkBoolResult(GEOSPreparedContains(prep, otherGeom));
984    }
985
986    bool containsProperly(const GeosGeometry* other)
987    {
988        GEOSPreparedGeometry *prep = (GEOSPreparedGeometry *) self;
989        GEOSGeom otherGeom = (GEOSGeom) other;
990        return checkBoolResult(GEOSPreparedContainsProperly(prep, otherGeom));
991    }
992
993    bool covers (const GeosGeometry* other)
994    {
995        GEOSPreparedGeometry *prep = (GEOSPreparedGeometry *) self;
996        GEOSGeom otherGeom = (GEOSGeom) other;
997        return checkBoolResult(GEOSPreparedCovers(prep, otherGeom));
998    }
999
1000    bool intersects (const GeosGeometry* other)
1001    {
1002        GEOSPreparedGeometry *prep = (GEOSPreparedGeometry *) self;
1003        GEOSGeom otherGeom = (GEOSGeom) other;
1004        return checkBoolResult(GEOSPreparedIntersects(prep, otherGeom));
1005    }
1006}
1007};
1008
1009// === STRtree ===
1010
1011%{
1012typedef void GeosSTRtree;
1013/* GeosIndexItem typedef'd here so it can be %typemap(typecheck)'d
1014   as a native object by each language specially */
1015typedef void *GeosIndexItem;
1016typedef GEOSQueryCallback GeosQueryCallback;
1017%}
1018
1019%rename (STRtree) GeosSTRtree;
1020class GeosSTRtree
1021{
1022public:
1023%extend
1024{
1025    %typemap(default) int nodeCapacity {
1026     $1 = 10;
1027    };
1028    GeosSTRtree(int nodeCapacity)
1029    {
1030        GEOSSTRtree *tree = GEOSSTRtree_create(nodeCapacity);
1031        if(tree == NULL)
1032            throw std::runtime_error(message);
1033        return (GeosSTRtree *) tree;
1034    }
1035
1036    ~GeosSTRtree()
1037    {
1038        GEOSSTRtree *tree = (GEOSSTRtree *) self;
1039        return GEOSSTRtree_destroy(tree);
1040    }
1041
1042    void insert (const GeosGeometry* g, GeosIndexItem item)
1043    {
1044        GEOSSTRtree *tree = (GEOSSTRtree *) self;
1045        const GEOSGeometry *geom = (const GEOSGeometry *) g;
1046        GEOSSTRtree_insert(tree, geom, item);
1047    }
1048
1049    void remove (const GeosGeometry* g, GeosIndexItem item)
1050    {
1051        GEOSSTRtree *tree = (GEOSSTRtree *) self;
1052        const GEOSGeometry *geom = (const GEOSGeometry *) g;
1053        GEOSSTRtree_remove(tree, geom, item);
1054    }
1055
1056    void query (const GeosGeometry* g, GeosQueryCallback callback,
1057                GeosIndexItem accumulator)
1058    {
1059        GEOSSTRtree *tree = (GEOSSTRtree *) self;
1060        const GEOSGeometry *geom = (const GEOSGeometry *) g;
1061        GEOSSTRtree_query(tree, geom, callback, accumulator);
1062    }
1063
1064    void iterate (GeosQueryCallback callback, GeosIndexItem accumulator)
1065    {
1066        GEOSSTRtree *tree = (GEOSSTRtree *) self;
1067        GEOSSTRtree_iterate(tree, callback, accumulator);
1068    }
1069}
1070};
1071
1072
1073// === Input/Output ===
1074
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)
1078{
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--;
1084}
1085
1086/* These three type maps are for geomToWKB and geomToHEX.  We need
1087to ignore the size input argument, then create a new string in the
1088scripting language of the correct size, and then free the
1089provided string. */
1090
1091/* set the size parameter to a temporary variable. */
1092%typemap(in, numinputs=0) size_t *size (size_t temp = 0)
1093{
1094        /* %typemap(in, numinputs=0) size_t *size (size_t temp = 0) */
1095        $1 = &temp;
1096}
1097
1098/* Disable SWIG's normally generated code so we can replace it
1099   with the argout typemap below. */
1100%typemap(out) unsigned char*
1101{
1102    /* %typemap(out) unsigned char* */
1103}
1104
1105/* Create a new target string of the correct size. */
1106%typemap(argout) size_t *size
1107{
1108    /* %typemap(argout) size_t *size */
1109    $result = SWIG_FromCharPtrAndSize((const char*)result, *$1);
1110}
1111
1112/* Free the c-string returned  by the function. */
1113%typemap(freearg) size_t *size
1114{
1115    /* %typemap(freearg) size_t *size */
1116    std::free(result);
1117}
1118
1119
1120%newobject GeosWktReader::read;
1121class GeosWktReader
1122{
1123public:
1124%extend
1125{
1126    GeosWktReader()
1127    {
1128        return GEOSWKTReader_create();
1129    }
1130       
1131    ~GeosWktReader()
1132    {
1133        GEOSWKTReader *reader = (GEOSWKTReader*) self;
1134        GEOSWKTReader_destroy(reader);
1135    }
1136   
1137    GeosGeometry* read(const char *wkt)
1138    {
1139        if(wkt == NULL)
1140            throw std::runtime_error("Trying to create geometry from a NULL string");
1141           
1142        GEOSWKTReader *reader = (GEOSWKTReader*) self;
1143        GEOSGeometry *geom = GEOSWKTReader_read(reader, wkt);
1144
1145        if(geom == NULL)
1146            throw std::runtime_error(message);
1147
1148        return (GeosGeometry*) geom;
1149    }
1150}
1151};
1152
1153class GeosWktWriter
1154{
1155public:
1156%extend
1157{
1158    GeosWktWriter()
1159    {
1160        return GEOSWKTWriter_create();
1161    }
1162       
1163    ~GeosWktWriter()
1164    {
1165        GEOSWKTWriter *writer = (GEOSWKTWriter*) self;
1166        GEOSWKTWriter_destroy(writer);
1167    }
1168   
1169    char* write(const GeosGeometry* g)
1170    {
1171        GEOSWKTWriter *writer = (GEOSWKTWriter*) self;
1172        GEOSGeom geom = (GEOSGeom) g;
1173        return GEOSWKTWriter_write(writer, geom);
1174    }
1175}
1176};
1177
1178
1179%newobject GeosWkbReader::read;
1180%newobject GeosWkbReader::readHEX;
1181class GeosWkbReader
1182{
1183public:
1184%extend
1185{
1186    GeosWkbReader()
1187    {
1188        return GEOSWKBReader_create();
1189    }
1190       
1191    ~GeosWkbReader()
1192    {
1193        GEOSWKBReader *reader = (GEOSWKBReader*) self;
1194        GEOSWKBReader_destroy(reader);
1195    }
1196   
1197    GeosGeometry* read(const unsigned char *wkb, size_t size)
1198    {
1199        if(wkb == NULL)
1200          throw std::runtime_error("Trying to create geometry from a NULL string");
1201         
1202        GEOSWKBReader *reader = (GEOSWKBReader*) self;
1203        GEOSGeometry *geom = GEOSWKBReader_read(reader, wkb, size);
1204       
1205        if(geom == NULL)
1206          throw std::runtime_error(message);
1207
1208        return (GeosGeometry*) geom;
1209    }
1210   
1211    GeosGeometry* readHEX(const unsigned char *wkb, size_t size)
1212    {
1213        if(wkb == NULL)
1214          throw std::runtime_error("Trying to create geometry from a NULL string");
1215         
1216        GEOSWKBReader *reader = (GEOSWKBReader*) self;
1217        GEOSGeometry *geom = GEOSWKBReader_readHEX(reader, wkb, size);
1218       
1219        if(geom == NULL)
1220          throw std::runtime_error(message);
1221
1222        return (GeosGeometry*) geom;
1223    }
1224}
1225};
1226
1227class GeosWkbWriter
1228{
1229public:
1230%extend
1231{
1232    GeosWkbWriter()
1233    {
1234        return GEOSWKBWriter_create();
1235    }
1236       
1237    ~GeosWkbWriter()
1238    {
1239        GEOSWKBWriter *writer = (GEOSWKBWriter*) self;
1240        GEOSWKBWriter_destroy(writer);
1241    }
1242   
1243    int getOutputDimension()
1244    {
1245        GEOSWKBWriter *writer = (GEOSWKBWriter*) self;
1246        return GEOSWKBWriter_getOutputDimension(writer);
1247    }
1248
1249    void setOutputDimension(int newDimension)
1250    {
1251        GEOSWKBWriter *writer = (GEOSWKBWriter*) self;
1252        GEOSWKBWriter_setOutputDimension(writer, newDimension);
1253    }
1254
1255    int getByteOrder()
1256    {
1257        GEOSWKBWriter *writer = (GEOSWKBWriter*) self;
1258        return GEOSWKBWriter_getByteOrder(writer);
1259    }
1260
1261    void setByteOrder(int newByteOrder)
1262    {
1263        GEOSWKBWriter *writer = (GEOSWKBWriter*) self;
1264        return GEOSWKBWriter_setByteOrder(writer, newByteOrder);
1265    }
1266
1267    bool getIncludeSRID()
1268    {
1269        GEOSWKBWriter *writer = (GEOSWKBWriter*) self;
1270        return GEOSWKBWriter_getIncludeSRID(writer);
1271    }
1272
1273    void setIncludeSRID(bool newIncludeSRID)
1274    {
1275        GEOSWKBWriter *writer = (GEOSWKBWriter*) self;
1276        return GEOSWKBWriter_setIncludeSRID(writer, newIncludeSRID);
1277    }
1278   
1279    unsigned char* write(const GeosGeometry* g, size_t *size)
1280    {
1281        GEOSWKBWriter *writer = (GEOSWKBWriter*) self;
1282        GEOSGeom geom = (GEOSGeom) g;
1283        return GEOSWKBWriter_write(writer, geom, size);
1284    }
1285   
1286    unsigned char* writeHEX(const GeosGeometry* g, size_t *size)
1287    {
1288        GEOSWKBWriter *writer = (GEOSWKBWriter*) self;
1289        GEOSGeom geom = (GEOSGeom) g;
1290        return GEOSWKBWriter_writeHEX(writer, geom, size);
1291    }
1292}
1293};
Note: See TracBrowser for help on using the repository browser.