-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathreport.py
executable file
·303 lines (281 loc) · 13.6 KB
/
report.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
# -*- coding: utf-8 -*-
"""Statistics report"""
from collections import OrderedDict, Counter
from datetime import datetime
import codecs
import locale
import platform
import time
import setup
TAB = ' '
SEP = ': '
MEMORY_UNIT = 1048576.0
int_format = lambda v: locale.format('%d', v, True)
class Report(object):
def __init__(self, **kwargs):
self.values = {
'date': datetime.now().strftime('%x'),
'fixme_counter': Counter(),
'warnings': [],
'errors': [],
'min_level': {},
'max_level': {},
}
for k,v in kwargs.items():
self.values[k] = v
self.titles = OrderedDict([
('mun_name', _('Municipality')),
('cat_mun', _('Cadastre name')),
('mun_code', _('Code')),
('date', _('Date')),
('options', _('Options')),
('mun_area', _(u'Area')),
('mun_population', _('Population')),
('mun_wikipedia', _('Wikipedia')),
('mun_wikidata', _('Wikidata')),
('group_system_info', _('System info')),
('app_version', _('Application version')),
('platform', _('Platform')),
('qgs_version', _('QGIS version')),
('cpu_count', _('CPU count')),
('cpu_freq', _('CPU frequency')),
('ex_time', _('Execution time')),
('memory', _('Total memory')),
('rss', _('Physical memory usage')),
('vms', _('Virtual memory usage')),
('group_address', _('Addresses')),
('subgroup_ad_input', _('Input data')),
('address_date', _('Source date')),
('inp_address', _('Feature count')),
('inp_address_entrance', TAB + _('Type entrance')),
('inp_address_parcel', TAB + _('Type parcel')),
('inp_zip_codes', _('Postal codes')),
('inp_street_names', _('Street names')),
('subgroup_ad_process', _('Process')),
('ignored_addresses', _('Addresses deleted by street name')),
('addresses_without_number', _('Addresses without house number deleted')),
('orphand_addresses', _('Addresses without associated building deleted')),
('multiple_addresses', _('Addresses belonging to multiple buildings deleted')),
('not_unique_addresses', _("'Parcel' addresses not unique for it building deleted")),
('subgroup_ad_conflation', _("Conflation")),
('osm_addresses', _("OSM addresses ")),
('osm_addresses_whithout_number', TAB + _("Without house number")),
('refused_addresses', _("Addresses rejected because they exist in OSM")),
('subgroup_ad_output', _('Output data')),
('out_address', _('Addresses')),
('out_address_entrance', TAB + _('In entrance nodes')),
('out_address_building', TAB + _('In buildings')),
('out_addr_str', TAB + _('Type addr:street')),
('out_addr_plc', TAB + _('Type addr:place')),
('group_buildings', _('Buildings')),
('subgroup_bu_input', _('Input data')),
('building_date', _('Source date')),
('inp_features', _('Feature count')),
('inp_buildings', TAB + _('Buildings')),
('inp_parts', TAB + _('Buildings parts')),
('inp_pools', TAB + _('Swimming pools')),
('subgroup_bu_process', _('Process')),
('orphand_parts', _("Parts outside footprint deleted")),
('underground_parts', _("Parts with no floors above ground")),
('new_footprints', _("Building footprints created")),
('multipart_geoms_building', _("Buildings with multipart geometries")),
('exploded_parts_building', _("Buildings resulting from spliting multiparts")),
('parts_to_footprint', _("Parts merged to the footprint")),
('adjacent_parts', _("Adjacent parts merged")),
('buildings_in_pools', _("Buildings coincidents with a swimming pool deleted")),
('geom_rings_building', _('Invalid geometry rings deleted')),
('geom_invalid_building', _('Invalid geometries deleted')),
('vertex_zigzag_building', _('Zig-zag vertices deleted')),
('vertex_spike_building', _('Spike vertices deleted')),
('vertex_close_building', _('Close vertices merged')),
('vertex_topo_building', _('Topological points created')),
('vertex_simplify_building', _("Simplified vertices")),
('subgroup_bu_conflation', _("Conflation")),
('osm_buildings', _("Buildings/pools in OSM")),
('osm_building_conflicts', TAB + _("With conflic")),
('subgroup_bu_output', _('Output data')),
('nodes', _("Nodes")),
('ways', _("Ways")),
('relations', _("Relations")),
('out_features', _("Feature count")),
('out_buildings', TAB + _('Buildings')),
('out_parts', TAB + _('Buildings parts')),
('out_pools', TAB + _('Swimming pools')),
('pools_on_roofs', TAB + TAB + _("Over buildings")),
('building_types', _("Building types counter")),
('dlag', _("Max. levels above ground (level: # of buildings)")),
('dlbg', _("Min. levels below ground (level: # of buildings)")),
('tasks_r', _("Rustic tasks files")),
('tasks_u', _("Urban tasks files")),
('group_problems', _("Problems")),
('errors', _("Report validation:")),
('fixme_count', _("Fixmes")),
('fixmes', ''),
('warnings', _("Warnings:")),
])
self.formats = {
'mun_area': lambda v: locale.format_string(u'%.1f km²', v, True),
'mun_population': lambda v: '{} hab. ({})'.format(*v),
'mun_wikipedia': lambda v: 'https://www.wikipedia.org/wiki/' + v,
'mun_wikidata': lambda v: 'https://www.wikidata.org/wiki/' + v,
'cpu_freq': lambda v: locale.format_string('%.1f Mhz', v, True),
'ex_time': lambda v: locale.format_string('%.1f seconds', v, True),
'memory': lambda v: locale.format_string('%.2f GB', v, True),
'rss': lambda v: locale.format_string('%.2f GB', v, True),
'vms': lambda v: locale.format_string('%.2f GB', v, True),
}
def __setattr__(self, key, value):
if key in ['values', 'titles', 'groups']:
super(Report, self).__setattr__(key, value)
else:
self.values[key] = value
def __getattr__(self, key):
return self.values[key]
def address_stats(self, address_osm):
for el in address_osm.elements:
if 'addr:street' in el.tags:
self.inc('out_addr_str')
if 'addr:place' in el.tags:
self.inc('out_addr_plc')
if 'addr:street' in el.tags or 'addr:place' in el.tags:
self.inc('out_address')
if el.type == 'node' and 'entrance' in el.tags:
self.inc('out_address_entrance')
if not 'entrance' in el.tags:
self.inc('out_address_building')
def cons_stats(self, data):
for el in data.elements:
if 'leisure' in el.tags and el.tags['leisure'] == 'swimming_pool':
self.inc('out_pools')
self.inc('out_features')
if 'building' in el.tags:
self.inc('out_buildings')
self.building_counter[el.tags['building']] += 1
self.inc('out_features')
if 'building:part' in el.tags:
self.inc('out_parts')
self.inc('out_features')
if 'fixme' in el.tags:
self.fixme_counter[el.tags['fixme']] += 1
def osm_stats(self, data):
self.inc('nodes', len(data.nodes))
self.inc('ways', len(data.ways))
self.inc('relations', len(data.relations))
def cons_end_stats(self):
self.dlag = ', '.join(["%d: %d" % (l, c) for (l, c) in \
OrderedDict(Counter(self.max_level.values())).items()])
self.dlbg = ', '.join(["%d: %d" % (l, c) for (l, c) in \
OrderedDict(Counter(self.min_level.values())).items()])
self.building_types = ', '.join(['%s: %d' % (b, c) \
for (b, c) in self.building_counter.items()])
def fixme_stats(self):
fixme_count = sum(self.fixme_counter.values())
if fixme_count:
self.fixme_count = fixme_count
self.fixmes = ['%s: %d' % (f, c) \
for (f, c) in self.fixme_counter.items()]
return fixme_count
def get(self, key, default=0):
return self.values.get(key, default)
def inc(self, key, step=1):
self.values[key] = self.get(key) + step
def sum(self, *args):
return sum(self.get(key) for key in args)
def get_sys_info(self):
try:
import psutil
p = psutil.Process()
v = list(platform.uname())
v.pop(1)
self.platform = ' '.join(v)
self.app_version = setup.app_name + ' ' + setup.app_version
self.cpu_count = psutil.cpu_count(logical=False)
self.cpu_freq = psutil.cpu_freq().max
self.memory = psutil.virtual_memory().total / MEMORY_UNIT
self.rss = p.memory_info().rss / MEMORY_UNIT
self.vms = p.memory_info().vms / MEMORY_UNIT
self.ex_time = time.time() - p.create_time()
except ImportError:
pass
def validate(self):
if self.sum('inp_address_entrance', 'inp_address_parcel') != \
self.get('inp_address'):
self.errors.append(_("Sum of address types should be equal "
"to the input addresses"))
if self.sum('addresses_without_number', 'orphand_addresses',
'multiple_addresses', 'refused_addresses', 'ignored_addresses',
'not_unique_addresses', 'out_address') != self.get('inp_address'):
self.errors.append(_("Sum of output and deleted addresses "
"should be equal to the input addresses"))
if self.sum('out_address_entrance', 'out_address_building') > 0 and \
self.sum('out_address_entrance', 'out_address_building') != \
self.get('out_address'):
self.errors.append(_("Sum of entrance and building address "
"should be equal to output addresses"))
if self.sum('out_addr_str', 'out_addr_plc') != \
self.get('out_address'):
self.errors.append(_("Sum of street and place addresses "
"should be equal to output addresses"))
if self.sum('inp_buildings', 'inp_parts', 'inp_pools') != \
self.get('inp_features'):
self.errors.append(_("Sum of buildings, parts and pools should "
"be equal to the feature count"))
if self.sum('out_features', 'orphand_parts', 'underground_parts',
'multipart_geoms_building', 'parts_to_footprint',
'adjacent_parts', 'geom_invalid_building', 'buildings_in_pools') - \
self.sum('new_footprints', 'exploded_parts_building') != \
self.get('inp_features'):
self.errors.append(_("Sum of output and deleted minus created "
"building features should be equal to input features"))
if 'building_counter' in self.values:
if sum(self.values['building_counter'].values()) != \
self.get('out_buildings'):
self.errors.append(_("Sum of building types should be equal "
"to the number of buildings"))
def to_string(self):
self.validate()
if self.get('sys_info'):
self.get_sys_info()
groups = set()
last_group = False
last_subgroup = False
for key in self.titles.keys():
exists = key in self.values
if exists and isinstance(self.values[key], list) and len(self.values[key]) == 0:
exists = False
if key.startswith('group_'):
last_group = key
last_subgroup = False
elif key.startswith('subgroup_'):
last_subgroup = key
if last_group and exists:
groups.add(last_group)
if last_subgroup and exists:
groups.add(last_subgroup)
output = u''
for key, title in self.titles.items():
if key.startswith('group_') and key in groups:
output += setup.eol + '=' + self.titles[key] + '=' + setup.eol
elif key.startswith('subgroup_') and key in groups:
output += setup.eol + '==' + self.titles[key] + '==' + setup.eol
elif key in self.values:
if isinstance(self.values[key], list):
if len(self.values[key]) > 0:
if title:
output += title + ' ' + \
int_format(len(self.values[key])) + setup.eol
for value in self.values[key]:
output += TAB + value + setup.eol
else:
value = self.values[key]
if key in self.formats:
value = self.formats[key](value)
elif isinstance(value, int) or isinstance(value, long):
value = int_format(value)
output += title + SEP + value
output += setup.eol
return output
def to_file(self, fn):
with codecs.open(fn, "w", setup.encoding) as fo:
fo.write(self.to_string())
instance = Report()