1 |
# Copyright (C) 2003 by Intevation GmbH |
2 |
# Authors: |
3 |
# Bernhard Herzog <[email protected]> |
4 |
# |
5 |
# This program is free software under the GPL (>=v2) |
6 |
# Read the file COPYING coming with the software for details. |
7 |
|
8 |
"""Data source abstractions""" |
9 |
|
10 |
__version__ = "$Revision$" |
11 |
# $Source$ |
12 |
# $Id$ |
13 |
|
14 |
import os |
15 |
import weakref |
16 |
|
17 |
import shapelib |
18 |
import table |
19 |
import transientdb |
20 |
|
21 |
from Thuban import _ |
22 |
|
23 |
|
24 |
class ShapeTable(transientdb.AutoTransientTable): |
25 |
|
26 |
"""A Table that depends on a ShapefileStore |
27 |
|
28 |
Intended use is by the ShapefileStore for the table associated with |
29 |
the shapefiles. |
30 |
""" |
31 |
|
32 |
def __init__(self, store, db, table): |
33 |
"""Initialize the ShapeTable. |
34 |
|
35 |
Parameters: |
36 |
store -- the ShapefileStore the table is to depend on |
37 |
db -- The transient database to use |
38 |
table -- the table |
39 |
""" |
40 |
transientdb.AutoTransientTable.__init__(self, db, table) |
41 |
self.store = weakref.ref(store) |
42 |
|
43 |
def Dependencies(self): |
44 |
"""Return a tuple containing the shapestore""" |
45 |
return (self.store(),) |
46 |
|
47 |
|
48 |
class ShapefileStore: |
49 |
|
50 |
"""Combine a shapefile and the corresponding DBF file into one object""" |
51 |
|
52 |
def __init__(self, session, filename): |
53 |
# Make the filename absolute. The filename will be |
54 |
# interpreted relative to that anyway, but when saving a |
55 |
# session we need to compare absolute paths and it's usually |
56 |
# safer to always work with absolute paths. |
57 |
self.filename = os.path.abspath(filename) |
58 |
|
59 |
self.shapefile = shapelib.ShapeFile(self.filename) |
60 |
self.dbftable = table.DBFTable(filename) |
61 |
self.table = ShapeTable(self, session.TransientDB(), self.dbftable) |
62 |
|
63 |
def Table(self): |
64 |
"""Return the table containing the attribute data""" |
65 |
return self.table |
66 |
|
67 |
def Shapefile(self): |
68 |
"""Return the shapefile object""" |
69 |
return self.shapefile |
70 |
|
71 |
def FileName(self): |
72 |
"""Return the filename used to open the shapefile""" |
73 |
return self.filename |
74 |
|
75 |
def FileType(self): |
76 |
"""Return the filetype. This is always the string 'shapefile'""" |
77 |
return "shapefile" |
78 |
|
79 |
def Dependencies(self): |
80 |
"""Return the empty tuple. |
81 |
|
82 |
The ShapefileStore doesn't depend on anything else. |
83 |
""" |
84 |
return () |
85 |
|
86 |
def OrigShapeStore(self): |
87 |
"""Return None. |
88 |
|
89 |
The ShapefileStore was not derived from another shapestore. |
90 |
""" |
91 |
return None |
92 |
|
93 |
|
94 |
class DerivedShapeStore: |
95 |
|
96 |
"""A ShapeStore derived from other shapestores or tables""" |
97 |
|
98 |
def __init__(self, shapestore, table): |
99 |
"""Initialize the derived shapestore. |
100 |
|
101 |
The arguments are a shapestore for the shapedata and a table for |
102 |
the tabular data. |
103 |
|
104 |
Raises ValueError if the number of shapes in the shapestore |
105 |
is different from the number of rows in the table. |
106 |
""" |
107 |
|
108 |
numShapes = shapestore.Shapefile().info()[0] |
109 |
if numShapes != table.NumRows(): |
110 |
raise ValueError(_("Table not compatible with shapestore.")) |
111 |
|
112 |
self.shapestore = shapestore |
113 |
self.table = table |
114 |
|
115 |
def Table(self): |
116 |
"""Return the table""" |
117 |
return self.table |
118 |
|
119 |
def Shapefile(self): |
120 |
"""Return the shapefile of the underlying shapestore""" |
121 |
return self.shapestore.Shapefile() |
122 |
|
123 |
def Dependencies(self): |
124 |
"""Return a tuple containing the shapestore and the table""" |
125 |
return (self.shapestore, self.table) |
126 |
|
127 |
def OrigShapeStore(self): |
128 |
""" |
129 |
Return the original shapestore the derived store was instantiated with |
130 |
""" |
131 |
return self.shapestore |