1 |
bh |
723 |
# 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 |
bh |
984 |
import warnings |
16 |
|
|
import weakref |
17 |
bh |
723 |
|
18 |
|
|
import shapelib |
19 |
|
|
import table |
20 |
bh |
765 |
import transientdb |
21 |
bh |
723 |
|
22 |
jonathan |
1261 |
from Thuban import _ |
23 |
bh |
765 |
|
24 |
jonathan |
1261 |
|
25 |
bh |
984 |
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 |
bh |
723 |
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 |
bh |
765 |
self.dbftable = table.DBFTable(filename) |
62 |
bh |
984 |
self.table = ShapeTable(self, session.TransientDB(), self.dbftable) |
63 |
bh |
723 |
|
64 |
|
|
def Table(self): |
65 |
bh |
984 |
"""Return the table containing the attribute data""" |
66 |
bh |
723 |
return self.table |
67 |
|
|
|
68 |
|
|
def Shapefile(self): |
69 |
bh |
984 |
"""Return the shapefile object""" |
70 |
bh |
723 |
return self.shapefile |
71 |
bh |
765 |
|
72 |
bh |
984 |
def FileName(self): |
73 |
|
|
"""Return the filename used to open the shapefile""" |
74 |
|
|
return self.filename |
75 |
bh |
765 |
|
76 |
bh |
984 |
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 |
bh |
1074 |
def OrigShapeStore(self): |
88 |
|
|
"""Return None. |
89 |
bh |
984 |
|
90 |
bh |
1074 |
The ShapefileStore was not derived from another shapestore. |
91 |
|
|
""" |
92 |
|
|
return None |
93 |
|
|
|
94 |
|
|
|
95 |
bh |
984 |
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 |
jonathan |
1261 |
|
105 |
|
|
Raises ValueError if the number of shapes in the shapestore |
106 |
|
|
is different from the number of rows in the table. |
107 |
bh |
984 |
""" |
108 |
jonathan |
1261 |
|
109 |
|
|
numShapes = shapestore.Shapefile().info()[0] |
110 |
|
|
if numShapes != table.NumRows(): |
111 |
|
|
raise ValueError(_("Table not compatible with shapestore.")) |
112 |
|
|
|
113 |
bh |
984 |
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 |
bh |
1074 |
"""Return the shapefile of the underlying shapestore""" |
122 |
bh |
984 |
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 |
bh |
1074 |
def OrigShapeStore(self): |
129 |
|
|
""" |
130 |
|
|
Return the original shapestore the derived store was instantiated with |
131 |
|
|
""" |
132 |
|
|
return self.shapestore |