WIP-1: basig get_streets query

Signed-off-by: David Decotigny <d2@maposmatic.org>
stable
David Decotigny 2010-08-07 14:39:07 +02:00
rodzic 89caded1f0
commit d3146f1223
1 zmienionych plików z 75 dodań i 4 usunięć

Wyświetl plik

@ -23,6 +23,14 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import logging
import locale
from ocitysmap2.index import commons
l = logging.getLogger('ocitysmap')
class StreetIndex:
def __init__(self, db, osmid, bounding_box, i18n, grid, polygon):
@ -35,7 +43,7 @@ class StreetIndex:
def _humanize_street_label(self, street):
return (self._i18n.user_readable_street(street[0]),
self._user_readable_label(street[1]))
self._i18n.user_readable_label(street[1]))
def _humanize_street_list(self, sl):
"""Given a list of street and their corresponding squares, do some
@ -70,12 +78,54 @@ class StreetIndex:
current_category = None
for street in sl:
if not self._i18n.first_letter_equal(street[0][0], first_letter):
current_category = IndexCategory(street[0])
current_category = commons.IndexCategory(street[0])
result.append(current_category)
current_category.items.append(IndexItem(street[0], street[1]))
current_category.items.append(commons.IndexItem(street[0],
street[1]))
return result
def get_streets_nogrid(self):
"""Get the list of streets in the administrative area if city
is defined or in the bounding box otherwise. Don't try to map
these streets onto the grid of squares.
Returns a list of commons.IndexCategory objects, with their IndexItems
having no specific grid location
"""
cursor = self._db.cursor()
l.info("Getting streets...")
intersect = 'true'
if self._polygon:
# Limit to the polygon
intersect = """st_intersects(way, st_transform(
GeomFromText('%s', 4002), 900913))""" \
% self._polygon
elif self._bounding_box:
# Limit to the bounding box
intersect = """st_intersects(way, st_transform(
GeomFromText('%s', 4002), 900913))""" \
% self._bounding_box.as_wkt()
else:
raise ValueError("No suitable bounding box provided")
query = """select distinct name
from planet_osm_line
where trim(name) != ''
and highway is not null
and %s
group by name
order by name;""" % intersect
cursor.execute(query)
sl = cursor.fetchall()
l.debug("Got streets (%d)." % len(sl))
return sl
def get_streets(self):
"""Get the list of streets in the administrative area if city is
defined or in the bounding box otherwise, and for each
@ -108,4 +158,25 @@ class StreetIndex:
sl = cursor.fetchall()
l.debug("Got streets (%d)." % len(sl))
return self.humanize_street_list(sl)
return self._humanize_street_list(sl)
if __name__ == "__main__":
import os
import psycopg2
from ocitysmap2 import i18n, coords
db = psycopg2.connect(user='maposmatic',
password='waeleephoo3Aew3u',
host='localhost',
database='maposmatic')
i18n = i18n.install_translation("fr_FR",
os.path.join(os.path.dirname(__file__),
"..", "..", "locale"))
idx_polygon = coords.BoundingBox(48.7097, 2.0333, 48.7048, 2.0462)
street_index = StreetIndex(db, None, None, i18n, None,
idx_polygon.as_wkt())
sl = street_index.get_streets_nogrid()
### print sl