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 |
bh |
765 |
|
23 |
bh |
984 |
class ShapeTable(transientdb.AutoTransientTable): |
24 |
|
|
|
25 |
|
|
"""A Table that depends on a ShapefileStore |
26 |
|
|
|
27 |
|
|
Intended use is by the ShapefileStore for the table associated with |
28 |
|
|
the shapefiles. |
29 |
|
|
""" |
30 |
|
|
|
31 |
|
|
def __init__(self, store, db, table): |
32 |
|
|
"""Initialize the ShapeTable. |
33 |
|
|
|
34 |
|
|
Parameters: |
35 |
|
|
store -- the ShapefileStore the table is to depend on |
36 |
|
|
db -- The transient database to use |
37 |
|
|
table -- the table |
38 |
|
|
""" |
39 |
|
|
transientdb.AutoTransientTable.__init__(self, db, table) |
40 |
|
|
self.store = weakref.ref(store) |
41 |
|
|
|
42 |
|
|
def Dependencies(self): |
43 |
|
|
"""Return a tuple containing the shapestore""" |
44 |
|
|
return (self.store(),) |
45 |
|
|
|
46 |
|
|
|
47 |
bh |
723 |
class ShapefileStore: |
48 |
|
|
|
49 |
|
|
"""Combine a shapefile and the corresponding DBF file into one object""" |
50 |
|
|
|
51 |
|
|
def __init__(self, session, filename): |
52 |
|
|
# Make the filename absolute. The filename will be |
53 |
|
|
# interpreted relative to that anyway, but when saving a |
54 |
|
|
# session we need to compare absolute paths and it's usually |
55 |
|
|
# safer to always work with absolute paths. |
56 |
|
|
self.filename = os.path.abspath(filename) |
57 |
|
|
|
58 |
|
|
self.shapefile = shapelib.ShapeFile(self.filename) |
59 |
bh |
765 |
self.dbftable = table.DBFTable(filename) |
60 |
bh |
984 |
self.table = ShapeTable(self, session.TransientDB(), self.dbftable) |
61 |
bh |
723 |
|
62 |
|
|
def Table(self): |
63 |
bh |
984 |
"""Return the table containing the attribute data""" |
64 |
bh |
723 |
return self.table |
65 |
|
|
|
66 |
|
|
def Shapefile(self): |
67 |
bh |
984 |
"""Return the shapefile object""" |
68 |
bh |
723 |
return self.shapefile |
69 |
bh |
765 |
|
70 |
bh |
984 |
def FileName(self): |
71 |
|
|
"""Return the filename used to open the shapefile""" |
72 |
|
|
return self.filename |
73 |
bh |
765 |
|
74 |
bh |
984 |
def FileType(self): |
75 |
|
|
"""Return the filetype. This is always the string 'shapefile'""" |
76 |
|
|
return "shapefile" |
77 |
|
|
|
78 |
|
|
def Dependencies(self): |
79 |
|
|
"""Return the empty tuple. |
80 |
|
|
|
81 |
|
|
The ShapefileStore doesn't depend on anything else. |
82 |
|
|
""" |
83 |
|
|
return () |
84 |
|
|
|
85 |
bh |
1074 |
def OrigShapeStore(self): |
86 |
|
|
"""Return None. |
87 |
bh |
984 |
|
88 |
bh |
1074 |
The ShapefileStore was not derived from another shapestore. |
89 |
|
|
""" |
90 |
|
|
return None |
91 |
|
|
|
92 |
|
|
|
93 |
bh |
984 |
class DerivedShapeStore: |
94 |
|
|
|
95 |
|
|
"""A ShapeStore derived from other shapestores or tables""" |
96 |
|
|
|
97 |
|
|
def __init__(self, shapestore, table): |
98 |
|
|
"""Initialize the derived shapestore. |
99 |
|
|
|
100 |
|
|
The arguments are a shapestore for the shapedata and a table for |
101 |
|
|
the tabular data. |
102 |
|
|
""" |
103 |
|
|
self.shapestore = shapestore |
104 |
|
|
self.table = table |
105 |
|
|
|
106 |
|
|
def Table(self): |
107 |
|
|
"""Return the table""" |
108 |
|
|
return self.table |
109 |
|
|
|
110 |
|
|
def Shapefile(self): |
111 |
bh |
1074 |
"""Return the shapefile of the underlying shapestore""" |
112 |
bh |
984 |
return self.shapestore.Shapefile() |
113 |
|
|
|
114 |
|
|
def Dependencies(self): |
115 |
|
|
"""Return a tuple containing the shapestore and the table""" |
116 |
|
|
return (self.shapestore, self.table) |
117 |
|
|
|
118 |
bh |
1074 |
def OrigShapeStore(self): |
119 |
|
|
""" |
120 |
|
|
Return the original shapestore the derived store was instantiated with |
121 |
|
|
""" |
122 |
|
|
return self.shapestore |
123 |
bh |
984 |
|
124 |
bh |
1074 |
|
125 |
bh |
765 |
class SimpleStore: |
126 |
|
|
|
127 |
|
|
"""Combine a shapefile and a table object""" |
128 |
|
|
|
129 |
|
|
def __init__(self, shapefile, table): |
130 |
bh |
984 |
warnings.warn("The SimpleStore is deprecated." |
131 |
|
|
" Use DerivedShapeStore instead", |
132 |
|
|
DeprecationWarning, stacklevel = 2) |
133 |
bh |
765 |
self.shapefile = shapefile |
134 |
|
|
self.table = table |
135 |
|
|
self.filename = None |
136 |
|
|
|
137 |
|
|
def Table(self): |
138 |
|
|
return self.table |
139 |
|
|
|
140 |
|
|
def Shapefile(self): |
141 |
|
|
return self.shapefile |