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