1 |
jan |
1728 |
# Copyright (C) 2003 by Intevation GmbH |
2 |
|
|
# Authors: |
3 |
|
|
# Jan-Oliver Wagner <[email protected]> |
4 |
|
|
# |
5 |
|
|
# This program is free software under the GPL (>=v2) |
6 |
|
|
# Read the file COPYING coming with Thuban for details. |
7 |
|
|
|
8 |
|
|
""" |
9 |
|
|
Import a ArcView project file (.apr) and convert it |
10 |
|
|
to Thuban. |
11 |
|
|
""" |
12 |
|
|
|
13 |
|
|
__version__ = "$Revision$" |
14 |
|
|
|
15 |
|
|
import os, sys |
16 |
|
|
|
17 |
|
|
from types import StringType |
18 |
|
|
|
19 |
|
|
from wxPython.wx import * |
20 |
|
|
|
21 |
|
|
from Thuban.Model.extension import Extension |
22 |
|
|
from Thuban.Model.base import TitledObject, Modifiable |
23 |
|
|
from Thuban.UI.command import registry, Command |
24 |
|
|
import Thuban.UI.mainwindow |
25 |
|
|
from Thuban import _ |
26 |
|
|
from Thuban.Model.layer import Layer |
27 |
jan |
1884 |
from Thuban.Model.classification import ClassGroupRange, ClassGroupSingleton |
28 |
jan |
1728 |
|
29 |
|
|
from odb import ODBBaseObject |
30 |
jan |
1884 |
from apr import APR_LClass, APR_TClr, APR_BLnSym, APR_BMkSym, APR_BShSym |
31 |
jan |
1728 |
|
32 |
|
|
class ODBExtension(Extension): |
33 |
|
|
def TreeInfo(self): |
34 |
|
|
return (_("Extension: %s") % self.title, |
35 |
|
|
[ object.TreeInfo() for object in self.objects ]) |
36 |
|
|
|
37 |
|
|
class APR_FTheme(ODBBaseObject): |
38 |
|
|
_obj_refs = [ 'Source', 'Legend' ] |
39 |
|
|
|
40 |
|
|
class APR_Legend(ODBBaseObject): |
41 |
|
|
"""Legend object. |
42 |
|
|
There could one or more Class objects. Each class corresponds to a |
43 |
|
|
Child in the Symbols. |
44 |
|
|
""" |
45 |
|
|
_obj_refs = [ 'FieldNames', 'Symbols', 'Class', 'NullSym', |
46 |
|
|
'NullValues', 'StatValues' ] |
47 |
|
|
|
48 |
|
|
class APR_Project(ODBBaseObject): |
49 |
|
|
_obj_refs = [ 'Doc' ] |
50 |
|
|
|
51 |
|
|
class APR_ShpSrc(ODBBaseObject): |
52 |
|
|
_obj_refs = [ 'Name' ] |
53 |
|
|
|
54 |
|
|
class APR_SrcName(ODBBaseObject): |
55 |
|
|
_obj_refs = [ 'FileName' ] |
56 |
|
|
|
57 |
|
|
class APR_SymList(ODBBaseObject): |
58 |
|
|
_obj_refs = [ 'Child' ] |
59 |
|
|
|
60 |
|
|
class APR_View(ODBBaseObject): |
61 |
jan |
1884 |
_obj_refs = [ 'Theme', 'ITheme' ] |
62 |
jan |
1728 |
|
63 |
|
|
class ODB(TitledObject, Modifiable): |
64 |
|
|
|
65 |
|
|
def __init__(self): |
66 |
|
|
TitledObject.__init__(self, 'ODB Object') |
67 |
|
|
self._objects = {} |
68 |
|
|
self._version = None |
69 |
|
|
self._filename = None |
70 |
|
|
self.name = None # required for Thuban.model.extension.Extension |
71 |
|
|
|
72 |
|
|
def SetFileName(self, fname): |
73 |
|
|
self._filename = fname |
74 |
|
|
self.name = fname |
75 |
|
|
|
76 |
|
|
def AddObject(self, object): |
77 |
|
|
self._objects[object.number] = object |
78 |
|
|
|
79 |
|
|
def GetObjects(self): |
80 |
|
|
return self._objects |
81 |
|
|
|
82 |
|
|
def GetProject(self): |
83 |
|
|
"""Return the main Root if it is a Project, else None.""" |
84 |
|
|
# it is assumed that the first object is the ODB object |
85 |
|
|
if self._objects[1].type != 'ODB': |
86 |
|
|
return None |
87 |
|
|
if self._objects[1].FirstRootClassName != 'Project': |
88 |
|
|
return None |
89 |
|
|
|
90 |
|
|
# it is assumed that the second object is the first root |
91 |
|
|
o = self._objects[2] |
92 |
|
|
if o.type != 'Project': |
93 |
|
|
return None |
94 |
|
|
return o |
95 |
|
|
|
96 |
|
|
def TreeInfo(self): |
97 |
|
|
items = [] |
98 |
|
|
items.append(_('Format version: %s') % self._version) |
99 |
|
|
p = self.GetProject() |
100 |
|
|
items.append(_('Project Name: %s') % p.Name) |
101 |
|
|
for doc in p.Get('Doc'): |
102 |
|
|
items.append(doc.TreeInfo()) |
103 |
|
|
return [_("ODB File '%s'" % self._filename), items] |
104 |
|
|
|
105 |
|
|
def parse_apr(fname): |
106 |
|
|
"""Load a ArcView project file. |
107 |
|
|
|
108 |
|
|
fname -- Filename of the .apr file |
109 |
|
|
|
110 |
|
|
Return: the ODB class object. |
111 |
|
|
""" |
112 |
|
|
odb = ODB() |
113 |
|
|
odb.SetFileName(fname) |
114 |
|
|
|
115 |
|
|
apr = open(fname, 'r').readlines() |
116 |
|
|
|
117 |
|
|
# get the version from the first line (eg. from "/3.1") |
118 |
|
|
odb._version = apr.pop(0)[1:] |
119 |
|
|
|
120 |
|
|
i = 0 |
121 |
|
|
in_object = False |
122 |
|
|
for line in apr: |
123 |
|
|
i += 1 |
124 |
|
|
if line[0] == '(': |
125 |
|
|
line = line[1:] |
126 |
|
|
type, number = line.split('.') |
127 |
|
|
number = int(number) |
128 |
|
|
class_name = 'APR_' + type |
129 |
|
|
try: |
130 |
|
|
clazz = eval(class_name) |
131 |
|
|
object = clazz(odb, type, number) |
132 |
|
|
except: |
133 |
|
|
object = ODBBaseObject(odb, type, number) |
134 |
|
|
in_object = True |
135 |
|
|
continue |
136 |
|
|
if line[0] == ')': |
137 |
|
|
in_object = False |
138 |
|
|
odb.AddObject(object) |
139 |
|
|
if in_object: |
140 |
|
|
line = line.strip() |
141 |
|
|
if len(line) == 0: continue |
142 |
|
|
try: |
143 |
|
|
property, value = line.split(':', 1) |
144 |
|
|
property = property.strip() |
145 |
|
|
value = value.strip() |
146 |
|
|
except: |
147 |
|
|
print "Error in line %d:" % i, line |
148 |
|
|
continue |
149 |
|
|
if value[0] == '"': |
150 |
|
|
value = value[1:-1] |
151 |
|
|
setattr(object, property, value) |
152 |
|
|
return odb |
153 |
|
|
|
154 |
|
|
def import_apr_dialog(context): |
155 |
|
|
"""Request filename from user and run importing of apr file. |
156 |
|
|
|
157 |
|
|
context -- The Thuban context. |
158 |
|
|
""" |
159 |
|
|
dlg = wxFileDialog(context.mainwindow, |
160 |
|
|
_("Select APR file"), ".", "", |
161 |
|
|
_("ArcView Project Files (*.apr)|*.apr|") + |
162 |
|
|
_("All Files (*.*)|*.*"), |
163 |
|
|
wxOPEN|wxOVERWRITE_PROMPT) |
164 |
|
|
if dlg.ShowModal() == wxID_OK: |
165 |
|
|
filename = dlg.GetPath() |
166 |
|
|
dlg.Destroy() |
167 |
|
|
else: |
168 |
|
|
return |
169 |
|
|
|
170 |
|
|
odb = parse_apr(filename) |
171 |
|
|
if odb is None: |
172 |
|
|
context.mainwindow.RunMessageBox(_("Import APR"), _("Loading failed")) |
173 |
|
|
return |
174 |
|
|
else: |
175 |
|
|
context.mainwindow.RunMessageBox(_("Import APR"), |
176 |
|
|
_("%d objects loaded" % |
177 |
|
|
len(odb.GetObjects().keys()))) |
178 |
|
|
|
179 |
|
|
# find the views of the APR file |
180 |
|
|
views = {} |
181 |
|
|
p = odb.GetProject() |
182 |
|
|
for doc in p.Get('Doc'): |
183 |
|
|
if doc.type != 'View': |
184 |
|
|
continue |
185 |
|
|
views[doc.Name] = doc |
186 |
|
|
|
187 |
|
|
# it is possible that a APR file has no view at all |
188 |
|
|
if len(views) == 0: |
189 |
|
|
context.mainwindow.RunMessageBox(_("Import APR"), |
190 |
|
|
_("No view found in APR file")) |
191 |
|
|
return |
192 |
|
|
|
193 |
|
|
# let the user select one of the views |
194 |
|
|
if len(views) > 1: |
195 |
|
|
titles = views.keys() |
196 |
|
|
dlg = wxSingleChoiceDialog(context.mainwindow, |
197 |
|
|
_('Pick a View to import:'), |
198 |
|
|
_('Import APR'), titles, |
199 |
|
|
style = wxDEFAULT_DIALOG_STYLE | |
200 |
|
|
wxRESIZE_BORDER) |
201 |
|
|
if dlg.ShowModal() == wxID_OK: |
202 |
|
|
view = views[views.keys()[dlg.GetSelection()]] |
203 |
|
|
else: |
204 |
|
|
return |
205 |
|
|
else: |
206 |
|
|
view = views[views.keys()[0]] |
207 |
|
|
|
208 |
|
|
# load the themes of the View as layers into Thuban |
209 |
|
|
count_theme = 0 |
210 |
|
|
count_theme_fail = 0 |
211 |
|
|
for theme in view.Get('Theme'): |
212 |
|
|
if theme.type != 'FTheme': |
213 |
|
|
continue |
214 |
|
|
count_theme += 1 |
215 |
|
|
filename = theme.Get('Source').Get('Name').Get('FileName').Path |
216 |
|
|
try: |
217 |
|
|
store = context.application.Session().OpenShapefile(filename) |
218 |
|
|
except IOError: |
219 |
|
|
# the layer couldn't be opened |
220 |
|
|
context.mainwindow.RunMessageBox(_('Add Layer'), |
221 |
|
|
_("Can't open the file '%s'.") % filename) |
222 |
|
|
count_theme_fail += 1 |
223 |
|
|
else: |
224 |
|
|
title = theme.Name |
225 |
|
|
apr_legend = theme.Get('Legend') |
226 |
|
|
|
227 |
|
|
map = context.mainwindow.canvas.Map() |
228 |
|
|
|
229 |
|
|
# create layer |
230 |
|
|
layer = Layer(title, store) |
231 |
|
|
|
232 |
|
|
# set the field for classification (if there is one in the apr) |
233 |
|
|
if hasattr(apr_legend, 'FieldNames'): |
234 |
|
|
apr_fieldname = apr_legend.Get('FieldNames').S |
235 |
|
|
# unfortunately, the APR file does not store the actual |
236 |
|
|
# Name of the column, but always makes the first character |
237 |
|
|
# a capital letter, the rest lower case. |
238 |
|
|
# Therefore, we have to search through the table of the |
239 |
|
|
# layer to find out the correct spelling. |
240 |
|
|
table_columns = layer.ShapeStore().Table().Columns() |
241 |
|
|
for column in table_columns: |
242 |
|
|
if apr_fieldname.lower() == column.name.lower(): |
243 |
|
|
layer.SetClassificationColumn(column.name) |
244 |
|
|
break |
245 |
|
|
|
246 |
|
|
clazz = layer.GetClassification() |
247 |
|
|
apr_classes = apr_legend.Get('Class') |
248 |
|
|
if not isinstance(apr_classes, list): |
249 |
|
|
apr_classes = [ apr_classes ] |
250 |
|
|
apr_symbols = apr_legend.Get('Symbols').Get('Child') |
251 |
|
|
if not isinstance(apr_symbols, list): |
252 |
|
|
apr_symbols = [ apr_symbols ] |
253 |
|
|
i = -1 |
254 |
|
|
for symbol in apr_symbols: |
255 |
|
|
i += 1 |
256 |
|
|
|
257 |
|
|
if hasattr(apr_classes[i], 'IsNoData'): |
258 |
|
|
group = clazz.GetDefaultGroup() |
259 |
|
|
group.SetLabel(apr_classes[i].Label) |
260 |
|
|
continue |
261 |
|
|
|
262 |
|
|
# create a new group property from the symbol |
263 |
|
|
prop = symbol.GetThubanProp() |
264 |
|
|
|
265 |
jan |
1734 |
# build range |
266 |
|
|
range = apr_classes[i].GetThubanRange() |
267 |
|
|
|
268 |
|
|
if isinstance(range, StringType): |
269 |
|
|
new_group = ClassGroupSingleton(value = range, |
270 |
jan |
1728 |
props = prop, |
271 |
|
|
label = apr_classes[i].GetLabel()) |
272 |
|
|
else: |
273 |
|
|
new_group = ClassGroupRange(_range = range, |
274 |
|
|
props = prop, |
275 |
|
|
label = apr_classes[i].GetLabel()) |
276 |
|
|
clazz.AppendGroup(new_group) |
277 |
|
|
|
278 |
|
|
map.AddLayer(layer) |
279 |
|
|
|
280 |
|
|
map.SetTitle(view.Name) |
281 |
|
|
|
282 |
|
|
# fit the new map to the window |
283 |
|
|
context.mainwindow.canvas.FitMapToWindow() |
284 |
|
|
|
285 |
|
|
context.mainwindow.RunMessageBox(_('Import APR'), |
286 |
|
|
_('Imported %d out of %d themes of view "%s" ...') % \ |
287 |
|
|
(count_theme - count_theme_fail, count_theme, view.Name)) |
288 |
|
|
|
289 |
|
|
# import_apr as an extension to Thuban |
290 |
|
|
if context.session.HasExtensions(): |
291 |
|
|
for ext in context.session.Extensions(): |
292 |
|
|
if ext.Title() == apr_import_extension.Title(): |
293 |
|
|
ext.AddObject(odb) |
294 |
|
|
return |
295 |
|
|
|
296 |
|
|
# no extension found, so lets make a new |
297 |
|
|
context.session.AddExtension(apr_import_extension) |
298 |
|
|
apr_import_extension.AddObject(odb) |
299 |
|
|
|
300 |
|
|
if __name__ == "__main__": # import_apr executed as a command line tool |
301 |
|
|
if len(sys.argv) == 2: |
302 |
|
|
odb = parse_apr(sys.argv[1]) |
303 |
|
|
print "%d objects loaded" % len(odb.GetObjects().keys()) |
304 |
|
|
|
305 |
|
|
def print_structured_list(lst, indent = ''): |
306 |
|
|
for item in lst: |
307 |
|
|
if isinstance(item, StringType): |
308 |
|
|
print indent + item |
309 |
|
|
elif isinstance(item, list): |
310 |
|
|
print_structured_list(item, indent + ' ') |
311 |
|
|
|
312 |
|
|
print_structured_list(odb.TreeInfo()) |
313 |
|
|
sys.exit(0) |
314 |
|
|
else: |
315 |
|
|
print 'usage: %s apr-file' % sys.argv[0] |
316 |
|
|
sys.exit(1) |
317 |
|
|
|
318 |
|
|
apr_import_extension = ODBExtension('APR Import') |
319 |
|
|
|
320 |
|
|
# register the new command |
321 |
|
|
registry.Add(Command('import-apr', _('Import apr-file...'), import_apr_dialog, |
322 |
|
|
helptext = _('Import a ArcView project file'))) |
323 |
|
|
|
324 |
|
|
# find the experimental menu (create it anew if not found) |
325 |
|
|
main_menu = Thuban.UI.mainwindow.main_menu |
326 |
|
|
experimental_menu = main_menu.find_menu('experimental') |
327 |
|
|
if experimental_menu is None: |
328 |
|
|
experimental_menu = main_menu.InsertMenu('experimental', _('Experimenta&l')) |
329 |
|
|
|
330 |
|
|
# finally add the new entry to the experimental menu |
331 |
|
|
experimental_menu.InsertItem('import-apr') |