12 |
# $Id$ |
# $Id$ |
13 |
|
|
14 |
import os |
import os |
15 |
|
import warnings |
16 |
|
import weakref |
17 |
|
|
18 |
import shapelib |
import shapelib |
19 |
import table |
import table |
20 |
import transientdb |
import transientdb |
21 |
|
|
22 |
|
from Thuban import _ |
23 |
|
|
24 |
|
|
25 |
|
class ShapeTable(transientdb.AutoTransientTable): |
26 |
|
|
27 |
|
"""A Table that depends on a ShapefileStore |
28 |
|
|
29 |
|
Intended use is by the ShapefileStore for the table associated with |
30 |
|
the shapefiles. |
31 |
|
""" |
32 |
|
|
33 |
|
def __init__(self, store, db, table): |
34 |
|
"""Initialize the ShapeTable. |
35 |
|
|
36 |
|
Parameters: |
37 |
|
store -- the ShapefileStore the table is to depend on |
38 |
|
db -- The transient database to use |
39 |
|
table -- the table |
40 |
|
""" |
41 |
|
transientdb.AutoTransientTable.__init__(self, db, table) |
42 |
|
self.store = weakref.ref(store) |
43 |
|
|
44 |
|
def Dependencies(self): |
45 |
|
"""Return a tuple containing the shapestore""" |
46 |
|
return (self.store(),) |
47 |
|
|
48 |
|
|
49 |
class ShapefileStore: |
class ShapefileStore: |
50 |
|
|
59 |
|
|
60 |
self.shapefile = shapelib.ShapeFile(self.filename) |
self.shapefile = shapelib.ShapeFile(self.filename) |
61 |
self.dbftable = table.DBFTable(filename) |
self.dbftable = table.DBFTable(filename) |
62 |
self.table = transientdb.AutoTransientTable(session.TransientDB(), |
self.table = ShapeTable(self, session.TransientDB(), self.dbftable) |
|
self.dbftable) |
|
63 |
|
|
64 |
def Table(self): |
def Table(self): |
65 |
|
"""Return the table containing the attribute data""" |
66 |
return self.table |
return self.table |
67 |
|
|
68 |
def Shapefile(self): |
def Shapefile(self): |
69 |
|
"""Return the shapefile object""" |
70 |
return self.shapefile |
return self.shapefile |
71 |
|
|
72 |
|
def FileName(self): |
73 |
|
"""Return the filename used to open the shapefile""" |
74 |
|
return self.filename |
75 |
|
|
76 |
|
def FileType(self): |
77 |
|
"""Return the filetype. This is always the string 'shapefile'""" |
78 |
|
return "shapefile" |
79 |
|
|
80 |
|
def Dependencies(self): |
81 |
|
"""Return the empty tuple. |
82 |
|
|
83 |
|
The ShapefileStore doesn't depend on anything else. |
84 |
|
""" |
85 |
|
return () |
86 |
|
|
87 |
|
def OrigShapeStore(self): |
88 |
|
"""Return None. |
89 |
|
|
90 |
|
The ShapefileStore was not derived from another shapestore. |
91 |
|
""" |
92 |
|
return None |
93 |
|
|
94 |
|
|
95 |
|
class DerivedShapeStore: |
96 |
|
|
97 |
|
"""A ShapeStore derived from other shapestores or tables""" |
98 |
|
|
99 |
|
def __init__(self, shapestore, table): |
100 |
|
"""Initialize the derived shapestore. |
101 |
|
|
102 |
|
The arguments are a shapestore for the shapedata and a table for |
103 |
|
the tabular data. |
104 |
|
|
105 |
|
Raises ValueError if the number of shapes in the shapestore |
106 |
|
is different from the number of rows in the table. |
107 |
|
""" |
108 |
|
|
109 |
|
numShapes = shapestore.Shapefile().info()[0] |
110 |
|
if numShapes != table.NumRows(): |
111 |
|
raise ValueError(_("Table not compatible with shapestore.")) |
112 |
|
|
113 |
|
self.shapestore = shapestore |
114 |
|
self.table = table |
115 |
|
|
116 |
|
def Table(self): |
117 |
|
"""Return the table""" |
118 |
|
return self.table |
119 |
|
|
120 |
|
def Shapefile(self): |
121 |
|
"""Return the shapefile of the underlying shapestore""" |
122 |
|
return self.shapestore.Shapefile() |
123 |
|
|
124 |
|
def Dependencies(self): |
125 |
|
"""Return a tuple containing the shapestore and the table""" |
126 |
|
return (self.shapestore, self.table) |
127 |
|
|
128 |
|
def OrigShapeStore(self): |
129 |
|
""" |
130 |
|
Return the original shapestore the derived store was instantiated with |
131 |
|
""" |
132 |
|
return self.shapestore |
133 |
|
|
134 |
|
|
135 |
class SimpleStore: |
class SimpleStore: |
136 |
|
|
137 |
"""Combine a shapefile and a table object""" |
"""Combine a shapefile and a table object""" |
138 |
|
|
139 |
def __init__(self, shapefile, table): |
def __init__(self, shapefile, table): |
140 |
|
warnings.warn("The SimpleStore is deprecated." |
141 |
|
" Use DerivedShapeStore instead", |
142 |
|
DeprecationWarning, stacklevel = 2) |
143 |
self.shapefile = shapefile |
self.shapefile = shapefile |
144 |
self.table = table |
self.table = table |
145 |
self.filename = None |
self.filename = None |