summaryrefslogtreecommitdiff
path: root/django/contrib/gis/geos/prototypes/topology.py
blob: 83d6706eaa50df7bce015ab1ed3a92e82fdbc095 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
"""
 This module houses the GEOS ctypes prototype functions for the
 topological operations on geometries.
"""
__all__ = ['geos_boundary', 'geos_buffer', 'geos_cascaded_union',
           'geos_centroid', 'geos_convexhull', 'geos_difference',
           'geos_envelope', 'geos_intersection', 'geos_linemerge',
           'geos_pointonsurface', 'geos_preservesimplify', 'geos_simplify',
           'geos_symdifference', 'geos_union', 'geos_relate',
           'geos_project', 'geos_interpolate', 'geos_project_normalized',
           'geos_interpolate_normalized']

from ctypes import c_double, c_int
from django.contrib.gis.geos.libgeos import GEOM_PTR
from django.contrib.gis.geos.prototypes.errcheck import check_geom, check_minus_one, check_string
from django.contrib.gis.geos.prototypes.geom import geos_char_p
from django.contrib.gis.geos.prototypes.threadsafe import GEOSFunc


def topology(func, *args, **kwargs):
    "For GEOS unary topology functions."
    argtypes = [GEOM_PTR]
    if args:
        argtypes += args
    func.argtypes = argtypes
    func.restype = kwargs.get('restype', GEOM_PTR)
    func.errcheck = kwargs.get('errcheck', check_geom)
    return func

### Topology Routines ###
geos_boundary = topology(GEOSFunc('GEOSBoundary'))
geos_buffer = topology(GEOSFunc('GEOSBuffer'), c_double, c_int)
geos_centroid = topology(GEOSFunc('GEOSGetCentroid'))
geos_convexhull = topology(GEOSFunc('GEOSConvexHull'))
geos_difference = topology(GEOSFunc('GEOSDifference'), GEOM_PTR)
geos_envelope = topology(GEOSFunc('GEOSEnvelope'))
geos_intersection = topology(GEOSFunc('GEOSIntersection'), GEOM_PTR)
geos_linemerge = topology(GEOSFunc('GEOSLineMerge'))
geos_pointonsurface = topology(GEOSFunc('GEOSPointOnSurface'))
geos_preservesimplify = topology(GEOSFunc('GEOSTopologyPreserveSimplify'), c_double)
geos_simplify = topology(GEOSFunc('GEOSSimplify'), c_double)
geos_symdifference = topology(GEOSFunc('GEOSSymDifference'), GEOM_PTR)
geos_union = topology(GEOSFunc('GEOSUnion'), GEOM_PTR)

geos_cascaded_union = GEOSFunc('GEOSUnionCascaded')
geos_cascaded_union.argtypes = [GEOM_PTR]
geos_cascaded_union.restype = GEOM_PTR

# GEOSRelate returns a string, not a geometry.
geos_relate = GEOSFunc('GEOSRelate')
geos_relate.argtypes = [GEOM_PTR, GEOM_PTR]
geos_relate.restype = geos_char_p
geos_relate.errcheck = check_string

# Linear referencing routines
geos_project = topology(GEOSFunc('GEOSProject'), GEOM_PTR,
    restype=c_double, errcheck=check_minus_one)
geos_interpolate = topology(GEOSFunc('GEOSInterpolate'), c_double)

geos_project_normalized = topology(GEOSFunc('GEOSProjectNormalized'),
    GEOM_PTR, restype=c_double, errcheck=check_minus_one)
geos_interpolate_normalized = topology(GEOSFunc('GEOSInterpolateNormalized'), c_double)