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