root/Core/trunk/bycycle/core/model/data/sqltypes.py

Revision 1186, 1.5 KB (checked in by wyatt, 3 years ago)

More movement toward 'modernization'.

Line 
1import sqlalchemy
2
3from shapely import geometry, wkb
4
5
6class Geometry(sqlalchemy.types.TypeEngine):
7    """PostGIS Geometry Type."""
8
9    def __init__(self, SRID, type_, dimension):
10        super(Geometry, self).__init__()
11        self.SRID = SRID
12        self.type = type_.upper()
13        self.dimension = dimension
14
15    def get_col_spec(self):
16        return 'GEOMETRY'
17
18    def bind_processor(self, dialect):
19        """Convert from Python type to database type."""
20        def process(value):
21            """``value`` is a Python/Shapely geometry object."""
22            if value is None:
23                return None
24            else:
25                return 'SRID=%s;%s' % (self.SRID, value)
26        return process
27
28    def result_processor(self, dialect):
29        """Convert from database type to Python type."""
30        def process(value):
31            """``value`` is a hex-encoded WKB string."""
32            if value is None:
33                return None
34            else:
35                return wkb.loads(value.decode('hex'))
36        return process
37
38
39class POINT(Geometry):
40    def __init__(self, SRID):
41        super(POINT, self).__init__(SRID, 'POINT', 2)
42
43
44class LINESTRING(Geometry):
45    def __init__(self, SRID):
46        super(LINESTRING, self).__init__(SRID, 'LINESTRING', 2)
47
48
49class MULTILINESTRING(Geometry):
50    def __init__(self, SRID):
51        super(MULTILINESTRING, self).__init__(SRID, 'MULTILINESTRING', 2)
52
53
54class MULTIPOLYGON(Geometry):
55    def __init__(self, SRID):
56        super(MULTIPOLYGON, self).__init__(SRID, 'MULTIPOLYGON', 2)
Note: See TracBrowser for help on using the browser.