Index improvment: group locations whith the same name and the same position

stable
Étienne Loks 2012-03-25 16:45:31 +02:00 zatwierdzone przez Thomas Petazzoni
rodzic c5a0813d6b
commit 01b7253e07
4 zmienionych plików z 29 dodań i 5 usunięć

Wyświetl plik

@ -447,6 +447,7 @@ SELECT ST_AsText(ST_LongestLine(
# Update the street_index to reflect the grid's actual position
if renderer.grid and street_index:
street_index.apply_grid(renderer.grid)
street_index.group_identical_grid_locations()
# Perform the actual rendering to the Cairo devices
for output_format in output_formats:

Wyświetl plik

@ -76,6 +76,7 @@ if __name__ == '__main__':
# Map index to grid
grid = Grid(bbox, rtl = False)
street_index.apply_grid(grid)
street_index.group_identical_grid_locations()
index = StreetIndexRenderer(i18nMock(False), street_index.categories)
@ -121,6 +122,7 @@ if __name__ == '__main__':
# Map index to grid
grid = Grid(bbox, rtl = True)
street_index.apply_grid(grid)
street_index.group_identical_grid_locations()
index = StreetIndexRenderer(i18nMock(True), street_index.categories)
_render('height', 'top')

Wyświetl plik

@ -45,11 +45,13 @@ class IndexCategory:
"""
name = None
items = None
is_street = False
def __init__(self, name, items = None):
def __init__(self, name, items=None, is_street=True):
assert name is not None
self.name = name
self.name = name
self.items = items or list()
self.is_street = is_street
def __str__(self):
return '<%s (%s)>' % (self.name, map(str, self.items))
@ -197,7 +199,6 @@ class IndexItem:
self.location_str = "%s-%s" % (min(ep1_label, ep2_label),
max(ep1_label, ep2_label))
if __name__ == "__main__":
import cairo
import pangocairo

Wyświetl plik

@ -28,6 +28,7 @@ import locale
import psycopg2
import csv
import datetime
from itertools import groupby
import commons
from ocitysmap2 import coords
@ -81,12 +82,30 @@ class StreetIndex:
compute the location strings
Returns:
Nothing, but self._categories will have been modified !
Nothing, but self._categories has been modified!
"""
for category in self._categories:
for item in category.items:
item.update_location_str(grid)
def group_identical_grid_locations(self):
"""
Group locations whith the same name and the same position on the grid.
Returns:
Nothing, but self._categories has been modified!
"""
categories = []
for category in self._categories:
if category.is_street:
categories.append(category)
continue
grouped_items = []
sort_key = lambda item:(item.label, item.location_str)
items = sorted(category.items, key=sort_key)
for label, same_items in groupby(items, key=sort_key):
grouped_items.append(same_items.next())
category.items = grouped_items
def write_to_csv(self, title, output_filename):
# TODO: implement writing the index to CSV
@ -283,7 +302,8 @@ from
# Get the current IndexCategory object, or create one if
# different than previous
if (not result or result[-1].name != catname):
current_category = commons.IndexCategory(catname)
current_category = commons.IndexCategory(catname,
is_street=False)
result.append(current_category)
else:
current_category = result[-1]