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 warnings |
16 |
import weakref |
17 |
|
18 |
import shapelib |
19 |
import table |
20 |
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: |
50 |
|
51 |
"""Combine a shapefile and the corresponding DBF file into one object""" |
52 |
|
53 |
def __init__(self, session, filename): |
54 |
# Make the filename absolute. The filename will be |
55 |
# interpreted relative to that anyway, but when saving a |
56 |
# session we need to compare absolute paths and it's usually |
57 |
# safer to always work with absolute paths. |
58 |
self.filename = os.path.abspath(filename) |
59 |
|
60 |
self.shapefile = shapelib.ShapeFile(self.filename) |
61 |
self.dbftable = table.DBFTable(filename) |
62 |
self.table = ShapeTable(self, session.TransientDB(), self.dbftable) |
63 |
|
64 |
def Table(self): |
65 |
"""Return the table containing the attribute data""" |
66 |
return self.table |
67 |
|
68 |
def Shapefile(self): |
69 |
"""Return the shapefile object""" |
70 |
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 |