~ubuntu-branches/ubuntu/quantal/python-django/quantal-security

« back to all changes in this revision

Viewing changes to django/contrib/gis/geos/libgeos.py

  • Committer: Bazaar Package Importer
  • Author(s): Chris Lamb
  • Date: 2010-05-21 07:52:55 UTC
  • mfrom: (1.3.6 upstream)
  • mto: This revision was merged to the branch mainline in revision 28.
  • Revision ID: james.westby@ubuntu.com-20100521075255-ii78v1dyfmyu3uzx
Tags: upstream-1.2
ImportĀ upstreamĀ versionĀ 1.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
6
6
 This module also houses GEOS Pointer utilities, including
7
7
 get_pointer_arr(), and GEOM_PTR.
8
8
"""
9
 
import atexit, os, re, sys
 
9
import os, re, sys
10
10
from ctypes import c_char_p, Structure, CDLL, CFUNCTYPE, POINTER
11
11
from ctypes.util import find_library
12
12
from django.contrib.gis.geos.error import GEOSException
45
45
                        '", "'.join(lib_names))
46
46
 
47
47
# Getting the GEOS C library.  The C interface (CDLL) is used for
48
 
#  both *NIX and Windows.
 
48
# both *NIX and Windows.
49
49
# See the GEOS C API source code for more details on the library function calls:
50
50
#  http://geos.refractions.net/ro/doxygen_docs/html/geos__c_8h-source.html
51
51
lgeos = CDLL(lib_path)
52
52
 
53
53
# The notice and error handler C function callback definitions.
54
 
#  Supposed to mimic the GEOS message handler (C below):
55
 
#  "typedef void (*GEOSMessageHandler)(const char *fmt, ...);"
 
54
# Supposed to mimic the GEOS message handler (C below):
 
55
#  typedef void (*GEOSMessageHandler)(const char *fmt, ...);
56
56
NOTICEFUNC = CFUNCTYPE(None, c_char_p, c_char_p)
57
57
def notice_h(fmt, lst, output_h=sys.stdout):
58
58
    try:
71
71
    output_h.write('GEOS_ERROR: %s\n' % err_msg)
72
72
error_h = ERRORFUNC(error_h)
73
73
 
74
 
# The initGEOS routine should be called first, however, that routine takes
75
 
#  the notice and error functions as parameters.  Here is the C code that
76
 
#  is wrapped:
77
 
#  "extern void GEOS_DLL initGEOS(GEOSMessageHandler notice_function, GEOSMessageHandler error_function);"
78
 
lgeos.initGEOS(notice_h, error_h)
79
 
 
80
74
#### GEOS Geometry C data structures, and utility functions. ####
81
75
 
82
76
# Opaque GEOS geometry structures, used for GEOM_PTR and CS_PTR
83
77
class GEOSGeom_t(Structure): pass
84
78
class GEOSPrepGeom_t(Structure): pass
85
79
class GEOSCoordSeq_t(Structure): pass
 
80
class GEOSContextHandle_t(Structure): pass
86
81
 
87
82
# Pointers to opaque GEOS geometry structures.
88
83
GEOM_PTR = POINTER(GEOSGeom_t)
89
84
PREPGEOM_PTR = POINTER(GEOSPrepGeom_t)
90
85
CS_PTR = POINTER(GEOSCoordSeq_t)
 
86
CONTEXT_PTR  = POINTER(GEOSContextHandle_t)
91
87
 
92
88
# Used specifically by the GEOSGeom_createPolygon and GEOSGeom_createCollection
93
89
#  GEOS routines
104
100
 
105
101
# Regular expression should be able to parse version strings such as
106
102
# '3.0.0rc4-CAPI-1.3.3', or '3.0.0-CAPI-1.4.1'
107
 
version_regex = re.compile(r'^(?P<version>(?P<major>\d+)\.(?P<minor>\d+)\.\d+)(rc(?P<release_candidate>\d+))?-CAPI-(?P<capi_version>\d+\.\d+\.\d+)$')
 
103
version_regex = re.compile(r'^(?P<version>(?P<major>\d+)\.(?P<minor>\d+)\.(?P<subminor>\d+))(rc(?P<release_candidate>\d+))?-CAPI-(?P<capi_version>\d+\.\d+\.\d+)$')
108
104
def geos_version_info():
109
105
    """
110
106
    Returns a dictionary containing the various version metadata parsed from
115
111
    ver = geos_version()
116
112
    m = version_regex.match(ver)
117
113
    if not m: raise GEOSException('Could not parse version info string "%s"' % ver)
118
 
    return dict((key, m.group(key)) for key in ('version', 'release_candidate', 'capi_version', 'major', 'minor'))
 
114
    return dict((key, m.group(key)) for key in ('version', 'release_candidate', 'capi_version', 'major', 'minor', 'subminor'))
119
115
 
120
116
# Version numbers and whether or not prepared geometry support is available.
121
117
_verinfo = geos_version_info()
122
118
GEOS_MAJOR_VERSION = int(_verinfo['major'])
123
119
GEOS_MINOR_VERSION = int(_verinfo['minor'])
 
120
GEOS_SUBMINOR_VERSION = int(_verinfo['subminor'])
124
121
del _verinfo
125
 
GEOS_PREPARE = GEOS_MAJOR_VERSION > 3 or GEOS_MAJOR_VERSION == 3 and GEOS_MINOR_VERSION >= 1
 
122
GEOS_VERSION = (GEOS_MAJOR_VERSION, GEOS_MINOR_VERSION, GEOS_SUBMINOR_VERSION)
 
123
GEOS_PREPARE = GEOS_VERSION >= (3, 1, 0)
126
124
 
127
 
# Calling the finishGEOS() upon exit of the interpreter.
128
 
atexit.register(lgeos.finishGEOS)
 
125
if GEOS_PREPARE:
 
126
    # Here we set up the prototypes for the initGEOS_r and finishGEOS_r
 
127
    # routines.  These functions aren't actually called until they are
 
128
    # attached to a GEOS context handle -- this actually occurs in
 
129
    # geos/prototypes/threadsafe.py.
 
130
    lgeos.initGEOS_r.restype = CONTEXT_PTR
 
131
    lgeos.finishGEOS_r.argtypes = [CONTEXT_PTR]
 
132
else:
 
133
    # When thread-safety isn't available, the initGEOS routine must be called
 
134
    # first.  This function takes the notice and error functions, defined
 
135
    # as Python callbacks above, as parameters. Here is the C code that is
 
136
    # wrapped:
 
137
    #  extern void GEOS_DLL initGEOS(GEOSMessageHandler notice_function, GEOSMessageHandler error_function);
 
138
    lgeos.initGEOS(notice_h, error_h)
 
139
    # Calling finishGEOS() upon exit of the interpreter.
 
140
    import atexit
 
141
    atexit.register(lgeos.finishGEOS)