for i in [ 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'Schools', 'Public buildings' ]: items = [] for label, location_str in [(rnd_str(40).capitalize(), '%s%d-%s%d' \ % (rnd_str(2, string.ascii_uppercase), random.randint(1,19), rnd_str(2, string.ascii_uppercase), random.randint(1,19), ))]*random.randint(1, 20): item = commons.IndexItem(label, None, None) item.location_str = location_str item.page_number = random.randint(1, 100) items.append(item) streets.append(commons.IndexCategory(i, items)) ctxtmp = cairo.Context(surface) rendering_area = \ (15, 15, width - 2 * 15, height - 2 * 15) mpsir = MultiPageStreetIndexRenderer(i18nMock(False), ctxtmp, surface, streets, rendering_area, 1) mpsir.render() surface.show_page()
def _list_amenities(self, db, polygon_wkt): """Get the list of amenities inside the given polygon. Don't try to map them onto the grid of squares (there location_str field remains undefined). Args: db (psycopg2 DB): The GIS database polygon_wkt (str): The WKT of the surrounding polygon of interest Returns a list of commons.IndexCategory objects, with their IndexItems having no specific grid square location """ cursor = db.cursor() result = [] for catname, db_amenity, label in self._get_selected_amenities(): l.info("Getting amenities for %s/%s..." % (catname, db_amenity)) # 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, is_street=False) result.append(current_category) else: current_category = result[-1] query = """ select amenity_name, st_astext(st_transform(ST_LongestLine(amenity_contour, amenity_contour), 4002)) as longest_linestring from ( select name as amenity_name, st_intersection(%(wkb_limits)s, %%(way)s) as amenity_contour from planet_osm_point where trim(name) != '' and amenity = %(amenity)s and ST_intersects(%%(way)s, %(wkb_limits)s) union select name as amenity_name, st_intersection(%(wkb_limits)s , %%(way)s) as amenity_contour from planet_osm_polygon where trim(name) != '' and amenity = %(amenity)s and ST_intersects(%%(way)s, %(wkb_limits)s) ) as foo order by amenity_name""" \ % {'amenity': _sql_escape_unicode(db_amenity), 'wkb_limits': ("st_transform(ST_GeomFromText('%s' , 4002), 900913)" % (polygon_wkt,))} # l.debug("Amenity query for for %s/%s (nogrid): %s" \ # % (catname, db_amenity, query)) try: cursor.execute(query % {'way': 'way'}) except psycopg2.InternalError: # This exception generaly occurs when inappropriate ways have # to be cleaned. Using a buffer of 0 generaly helps to clean # them. This operation is not applied by default for # performance. db.rollback() cursor.execute(query % {'way': 'st_buffer(way, 0)'}) for amenity_name, linestring in cursor.fetchall(): # Parse the WKT from the largest linestring in shape try: s_endpoint1, s_endpoint2 = map( lambda s: s.split(), linestring[11:-1].split(',')) except (ValueError, TypeError): l.exception("Error parsing %s for %s/%s/%s" % (repr(linestring), catname, db_amenity, repr(amenity_name))) continue ## raise endpoint1 = ocitysmap.coords.Point(s_endpoint1[1], s_endpoint1[0]) endpoint2 = ocitysmap.coords.Point(s_endpoint2[1], s_endpoint2[0]) current_category.items.append( commons.IndexItem(amenity_name, endpoint1, endpoint2, self._page_number)) l.debug("Got %d amenities for %s/%s." % (len(current_category.items), catname, db_amenity)) return [category for category in result if category.items]
def _list_villages(self, db, polygon_wkt): """Get the list of villages inside the given polygon. Don't try to map them onto the grid of squares (there location_str field remains undefined). Args: db (psycopg2 DB): The GIS database polygon_wkt (str): The WKT of the surrounding polygon of interest Returns a list of commons.IndexCategory objects, with their IndexItems having no specific grid square location """ cursor = db.cursor() result = [] current_category = commons.IndexCategory(_(u"Villages"), is_street=False) result.append(current_category) query = """ select village_name, st_astext(st_transform(ST_LongestLine(village_contour, village_contour), 4002)) as longest_linestring from ( select name as village_name, st_intersection(%(wkb_limits)s, %%(way)s) as village_contour from planet_osm_point where trim(name) != '' and (place = 'locality' or place = 'hamlet' or place = 'isolated_dwelling') and ST_intersects(%%(way)s, %(wkb_limits)s) ) as foo order by village_name""" \ % {'wkb_limits': ("st_transform(ST_GeomFromText('%s', 4002), 900913)" % (polygon_wkt,))} # l.debug("Villages query for %s (nogrid): %s" \ # % ('Villages', query)) try: cursor.execute(query % {'way': 'way'}) except psycopg2.InternalError: # This exception generaly occurs when inappropriate ways have # to be cleaned. Using a buffer of 0 generaly helps to clean # them. This operation is not applied by default for # performance. db.rollback() cursor.execute(query % {'way': 'st_buffer(way, 0)'}) for village_name, linestring in cursor.fetchall(): # Parse the WKT from the largest linestring in shape try: s_endpoint1, s_endpoint2 = map(lambda s: s.split(), linestring[11:-1].split(',')) except (ValueError, TypeError): l.exception("Error parsing %s for %s/%s" % (repr(linestring), 'Villages', repr(village_name))) continue ## raise endpoint1 = ocitysmap.coords.Point(s_endpoint1[1], s_endpoint1[0]) endpoint2 = ocitysmap.coords.Point(s_endpoint2[1], s_endpoint2[0]) current_category.items.append( commons.IndexItem(village_name, endpoint1, endpoint2, self._page_number)) l.debug("Got %d villages for %s." % (len(current_category.items), 'Villages')) return [category for category in result if category.items]
def _convert_street_index(self, sl): """Given a list of street names, do some cleanup and pass it through the internationalization layer to get proper sorting, filtering of common prefixes, etc. Args: sl (list of tuple): list tuples of the form (street_name, linestring_wkt) where linestring_wkt is a WKT for the linestring between the 2 most distant point of the street, in 4326 SRID Returns the list of IndexCategory objects. Each IndexItem will have its square location still undefined at that point """ # Street prefixes are postfixed, a human readable label is # built to represent the list of squares, and the list is # alphabetically-sorted. prev_locale = locale.getlocale(locale.LC_COLLATE) locale.setlocale(locale.LC_COLLATE, self._i18n.language_code()) try: sorted_sl = sorted( [(self._i18n.user_readable_street(name), linestring) for name, linestring in sl], lambda x, y: locale.strcoll(x[0].lower(), y[0].lower())) finally: locale.setlocale(locale.LC_COLLATE, prev_locale) result = [] current_category = None NUMBER_LIST = [str(i) for i in xrange(10)] for street_name, linestring in sorted_sl: # Create new category if needed if (not current_category or (not self._i18n.first_letter_equal( street_name[0], current_category.name) and (current_category.name != commons.NUMBER_CATEGORY_NAME or street_name[0] not in NUMBER_LIST))): if street_name[0] in NUMBER_LIST: cat_name = commons.NUMBER_CATEGORY_NAME else: cat_name = self._i18n.upper_unaccent_string(street_name[0]) current_category = commons.IndexCategory(cat_name) result.append(current_category) # Parse the WKT from the largest linestring in shape try: s_endpoint1, s_endpoint2 = map(lambda s: s.split(), linestring[11:-1].split(',')) except (ValueError, TypeError): l.exception("Error parsing %s for %s" % (repr(linestring), repr(street_name))) raise endpoint1 = ocitysmap.coords.Point(s_endpoint1[1], s_endpoint1[0]) endpoint2 = ocitysmap.coords.Point(s_endpoint2[1], s_endpoint2[0]) current_category.items.append( commons.IndexItem(street_name, endpoint1, endpoint2, self._page_number)) return result