[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Maposmatic-dev] [PATCH] Index improvment: group locations whith the sam
From: |
Étienne Loks |
Subject: |
[Maposmatic-dev] [PATCH] Index improvment: group locations whith the same name and the same position |
Date: |
Sun, 25 Mar 2012 16:45:31 +0200 |
---
ocitysmap2/__init__.py | 1 +
ocitysmap2/indexlib/__init__.py | 2 ++
ocitysmap2/indexlib/commons.py | 7 ++++---
ocitysmap2/indexlib/indexer.py | 24 ++++++++++++++++++++++--
4 files changed, 29 insertions(+), 5 deletions(-)
diff --git a/ocitysmap2/__init__.py b/ocitysmap2/__init__.py
index b7a648a..a0f48d6 100644
--- a/ocitysmap2/__init__.py
+++ b/ocitysmap2/__init__.py
@@ -442,6 +442,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:
diff --git a/ocitysmap2/indexlib/__init__.py b/ocitysmap2/indexlib/__init__.py
index 23a8bc6..e0ca557 100644
--- a/ocitysmap2/indexlib/__init__.py
+++ b/ocitysmap2/indexlib/__init__.py
@@ -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')
diff --git a/ocitysmap2/indexlib/commons.py b/ocitysmap2/indexlib/commons.py
index c1503b4..6bbc88c 100644
--- a/ocitysmap2/indexlib/commons.py
+++ b/ocitysmap2/indexlib/commons.py
@@ -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
diff --git a/ocitysmap2/indexlib/indexer.py b/ocitysmap2/indexlib/indexer.py
index a5f969e..5427753 100644
--- a/ocitysmap2/indexlib/indexer.py
+++ b/ocitysmap2/indexlib/indexer.py
@@ -28,6 +28,7 @@ import locale
import psycopg2
import csv
import datetime
+from itertools import groupby
import commons
from ocitysmap2 import coords
@@ -80,12 +81,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
@@ -282,7 +301,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]
--
1.7.9.1
- [Maposmatic-dev] [PATCH] Index improvment: group locations whith the same name and the same position,
Étienne Loks <=