summaryrefslogtreecommitdiffabout
path: root/ocitysmap/street_index.py (plain)
blob: 0b7a8c90e80192d83234de54bef80742b9c08990
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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
# -*- coding: utf-8; mode: Python -*-

# ocitysmap, city map and street index generator from OpenStreetMap data
# Copyright (C) 2009  David Decotigny
# Copyright (C) 2009  Frédéric Lehobey
# Copyright (C) 2009  David Mentré
# Copyright (C) 2009  Maxime Petazzoni
# Copyright (C) 2009  Thomas Petazzoni
# Copyright (C) 2009  Gaël Utard

# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or any later version.

# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.

# You should have received a copy of the GNU Affero General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

import logging, traceback
import sys, os, tempfile, psycopg2, re, math, cairo, locale, gzip, csv
import ConfigParser
import i18n
from coords import BoundingBox

import map_canvas, grid, utils

from draw_utils import enclose_in_frame

import psycopg2.extensions
# compatibility with django: see http://code.djangoproject.com/ticket/5996
psycopg2.extensions.register_type(psycopg2.extensions.UNICODE)
# SQL string escaping routine
sql_escape_unicode = lambda s: psycopg2.extensions.adapt(s.encode('utf-8'))

LOG = logging.getLogger('ocitysmap')
STATEMENT_TIMEOUT_MINUTES = 18

class BaseOCitySMapError(Exception):
    """Base class for exceptions thrown by OCitySMap."""

class UnsufficientDataError(BaseOCitySMapError):
    """Not enough data in the OSM database to proceed."""

def _user_readable_label(squares):
    """Creates a label usable in the street index adjacent to the map
       from a square list."""

    def couple_compare(x,y):
        a = y[0] - x[0]
        if a:
            return a
        return y[1] - x[1]

    def distance(a,b):
        return (b[0]-a[0])**2 + (b[1]-a[1])**2

    minx = min([x[0] for x in squares])
    maxx = max([x[0] for x in squares])
    miny = min([x[1] for x in squares])
    maxy = max([x[1] for x in squares])
    if len(squares) == 1:
        label = (utils.gen_vertical_square_label(squares[0][0]) +
                 utils.gen_horizontal_square_label(squares[0][1]))
    elif minx == maxx:
        label = ('%s%s-%s' % (utils.gen_vertical_square_label(minx),
                              utils.gen_horizontal_square_label(miny),
                              utils.gen_horizontal_square_label(maxy)))
    elif miny == maxy:
        label = ('%s-%s%s' % (utils.gen_vertical_square_label(minx),
                              utils.gen_vertical_square_label(maxx),
                              utils.gen_horizontal_square_label(miny)))
    elif (maxx - minx + 1) * (maxy - miny + 1) == len(squares):
        label = ('%s-%s%s-%s' % (utils.gen_vertical_square_label(minx),
                                 utils.gen_vertical_square_label(maxx),
                                 utils.gen_horizontal_square_label(miny),
                                 utils.gen_horizontal_square_label(maxy)))
    else:
        squares_x_first = sorted(squares, couple_compare)
        squares_y_first = sorted(squares, lambda x,y: couple_compare(y,x))
        if (distance(squares_x_first[0], squares_x_first[-1]) >
            distance(squares_y_first[0], squares_y_first[-1])):
            first = squares_x_first[0]
            last = squares_x_first[-1]
        else:
            first = squares_y_first[0]
            last = squares_y_first[-1]

        label = '%s%s...%s%s' % (utils.gen_vertical_square_label(first[0]),
                                 utils.gen_horizontal_square_label(first[1]),
                                 utils.gen_vertical_square_label(last[0]),
                                 utils.gen_horizontal_square_label(last[1]))
    return label

class IndexPageGenerator:
    def __init__(self, streets, i18n):
        self.streets = streets
        self.i18n = i18n

    def _get_font_parameters(self, cr, fontsize):
        cr.select_font_face("DejaVu", cairo.FONT_SLANT_NORMAL,
                            cairo.FONT_WEIGHT_NORMAL)
        cr.set_font_size(fontsize * 1.2)
        heading_fascent, heading_fdescent, heading_fheight = cr.font_extents()[:3]

        cr.select_font_face("DejaVu", cairo.FONT_SLANT_NORMAL, cairo.FONT_WEIGHT_NORMAL)
        cr.set_font_size(fontsize)
        fascent, fdescent, fheight, fxadvance, fyadvance = cr.font_extents()

        em = cr.text_extents("m")[2]
        widths = map(lambda x: cr.text_extents(x[1])[2] + cr.text_extents(x[2])[2], self.streets)
        maxwidth = max(widths)
        colwidth = maxwidth + 3 * em

        return {
            'colwidth' : colwidth,
            'heading_fascent' : heading_fascent,
            'heading_fheight' : heading_fheight,
            'fheight' : fheight,
            'em' : em,
            }

    def _fits_in_page(self, cr, paperwidth, paperheight, fontsize):
        fp = self._get_font_parameters(cr, fontsize)

        prevletter = u''
        heading_letter_count = 0
        for street in self.streets:
            if not self.i18n.first_letter_equal(street[0], prevletter):
                heading_letter_count += 1
                prevletter = street[0]

        colheight = len(self.streets) * fp['fheight'] + heading_letter_count * fp['heading_fheight']

        paperncols = math.floor(paperwidth / fp['colwidth'])
        if paperncols == 0:
            return False
        # Add a small space before/after each column
        colheight += paperncols * fp['fheight']
        colheight /= paperncols
        return colheight < paperheight

    def _compute_font_size(self, cr, paperwidth, paperheight):
        minfontsize = 6
        maxfontsize = 128

        if not self._fits_in_page(cr, paperwidth, paperheight, minfontsize):
            print "Index does not fit even with font size %d" % minfontsize
            sys.exit(1)

        while maxfontsize - minfontsize != 1:
            meanfontsize = int((maxfontsize + minfontsize) / 2)
            if self._fits_in_page(cr, paperwidth, paperheight, meanfontsize):
                minfontsize = meanfontsize
            else:
                maxfontsize = meanfontsize

        return minfontsize

    def render(self, cr, paperwidth, paperheight):
        cr.set_source_rgb(1, 1, 1)
        cr.paint()
        cr.set_source_rgb(0.0, 0.0, 0.0)

        fontsize = self._compute_font_size(cr, paperwidth, paperheight)

        fp = self._get_font_parameters(cr, fontsize)
        heading_fheight = fp['heading_fheight']
        heading_fascent = fp['heading_fascent']
        fheight = fp['fheight']
        colwidth = fp['colwidth']
        em = fp['em']

        remaining = paperwidth % colwidth
        colwidth += (remaining / int(paperwidth / colwidth))

        y = 0
        x = em
        prevletter = u''
        for street in self.streets:
            # Letter label
            firstletter = street[0]
            if not self.i18n.first_letter_equal(firstletter, prevletter):
                # Make sure we have no orphelin heading letter label at the
                # end of a column
                if y + heading_fheight + fheight > paperheight:
                    y = 0
                    x += colwidth
                # Reserve height for the heading letter label
                y += heading_fheight

                cr.set_source_rgb(0.9, 0.9, 0.9)
                cr.rectangle(x, y - heading_fascent, colwidth - em, heading_fheight)
                cr.fill()

                cr.set_source_rgb(0, 0, 0)

                # Draw the heading letter label
                cr.select_font_face("DejaVu", cairo.FONT_SLANT_NORMAL, cairo.FONT_WEIGHT_NORMAL)
                cr.set_font_size(fontsize * 1.2)
                w = cr.text_extents(firstletter)[2]
                indent = (colwidth - 2 * em - w) / 2
                cr.move_to(x + indent, y)
                cr.show_text(firstletter)
                prevletter = firstletter

            # Reserve height for the street
            y += fheight
            cr.select_font_face("DejaVu", cairo.FONT_SLANT_NORMAL, cairo.FONT_WEIGHT_NORMAL)
            cr.set_font_size(fontsize)
            # Compute length of the dashed line between the street name and
            # the squares label
            street_name_width = cr.text_extents(street[1])[4]
            squares_label_width = cr.text_extents(street[2])[2]
            line_width = colwidth - street_name_width - squares_label_width - 2 * em
            # Draw street name
            cr.move_to(x, y)
            cr.show_text(street[1])
            # Draw dashed line
            strokewidth = max(fontsize / 12, 1)
            cr.set_line_width(strokewidth)
            cr.set_dash([ strokewidth, strokewidth * 2 ])
            cr.move_to(x + street_name_width + em / 2, y - 0.1 * em)
            cr.rel_line_to(line_width, 0)
            cr.stroke()
            # Draw squares label
            cr.move_to(x + colwidth - em - squares_label_width, y)
            cr.show_text(street[2])
            if y + fheight > paperheight:
                y = 0
                x += colwidth

class OCitySMap:
    def __init__(self, config_file=None, city_name=None, boundingbox=None,
                 osmid=None, language=None):
        """Creates a new OCitySMap renderer instance for the given city.

        Args:
            config_file: location of the config file
            city_name (string): The name of the city we're created the map of.
            boundingbox (BoundingBox): An optional BoundingBox object defining
                the city's bounding box. If not given, OCitySMap will try to
                guess the bounding box from the OSM data. An UnsufficientDataError
                exception will be raised in the bounding box can't be guessed.
            osmid (integer): The OSM id of the polygon of the city to render
        """

        optcnt = 0
        for var in city_name, boundingbox, osmid:
            if var:
                optcnt += 1

        assert optcnt == 1

        (self.city_name, self.boundingbox, self.osmid) = (city_name, boundingbox, osmid)

        if self.city_name:
            LOG.info('OCitySMap renderer for %s.' % self.city_name)
        elif self.boundingbox:
            LOG.info('OCitySMap renderer for %s.' % self.boundingbox)
        else:
            LOG.info('OCitySMap renderer for %s.' % self.osmid)

        LOG.info('Reading config file.')
        self.parser = ConfigParser.RawConfigParser()
        if config_file:
            config_files = [config_file]
        else:
            config_files = ['/etc/ocitysmap.conf',
                            os.getenv('HOME') + '/.ocitysmap.conf']
        if not self.parser.read(config_files):
            raise IOError, 'Failed to load the config file'

        locale_path = self.parser.get('ocitysmap', 'locale_path')
        self.i18n = i18n.install_translation(language, locale_path)
        LOG.info('Language: ' + self.i18n.language_code())

        self._map_areas_table_name = "map_areas"
        if self.parser.has_option('ocitysmap', 'map_areas_table_name'):
            self._map_areas_table_name = self.parser.get('ocitysmap',
                                                         'map_areas_table_name')

        self.SELECTED_AMENITIES = [
            (_(u"Places of worship"), "place_of_worship", _(u"Place of worship")),
            (_(u"Education"), "kindergarten", _(u"Kindergarten")),
            (_(u"Education"), "school", _(u"School")),
            (_(u"Education"), "college", _(u"College")),
            (_(u"Education"), "university", _(u"University")),
            (_(u"Education"), "library", _(u"Library")),
            (_(u"Public buildings"), "townhall", _(u"Town hall")),
            (_(u"Public buildings"), "post_office", _(u"Post office")),
            (_(u"Public buildings"), "public_building", _(u"Public building")),
            (_(u"Public buildings"), "police", _(u"Police"))]

        datasource = dict(self.parser.items('datasource'))

        LOG.info('Connecting to database %s at %s (user: %s)...' %
                 (datasource['dbname'], datasource['host'], datasource['user']))
        db = psycopg2.connect(user=datasource['user'],
                              password=datasource['password'],
                              host=datasource['host'],
                              database=datasource['dbname'])

        # Force everything to be unicode-encoded, in case we run along
        # django (which loads the unicode extensions for psycopg2)
        db.set_client_encoding('utf8')

        # Set session timeout parameter (18mn)
        cursor = db.cursor()
        cursor.execute("show statement_timeout;")
        LOG.debug("Initial statement timeout: %s" % cursor.fetchall()[0][0])
        cursor.execute("set session statement_timeout=%d;"
                       % (STATEMENT_TIMEOUT_MINUTES*60*1000))
        cursor.execute("show statement_timeout;")
        LOG.info("Configured statement timeout: %s" % cursor.fetchall()[0][0])
        del cursor

        if self.city_name:
            self.boundingbox = self.find_bounding_box_by_name(db, self.city_name)
        elif self.osmid:
            self.boundingbox = self.find_bounding_box_by_osmid(db, self.osmid)

        self.griddesc = grid.GridDescriptor(self.boundingbox, db)

        self._gen_map_areas(db)

        if self.osmid:
            self.streets = self.get_streets_by_osmid(db, self.osmid)
            self.amenities = self.get_amenities_by_osmid(db, self.osmid)
        elif self.city_name:
            self.streets = self.get_streets_by_name(db, self.city_name)
            self.amenities = self.get_amenities_by_name(db, self.city_name)
        else:
            self.streets = self.get_streets_by_name(db, None)
            self.amenities = self.get_amenities_by_name(db, None)

        if self.city_name:
            self.contour = self.get_city_contour_by_name(db, self.city_name)
        elif self.osmid:
            self.contour = self.get_city_contour_by_osmid(db, self.osmid)
        else:
            self.contour = None

        LOG.info('City bounding box is %s.' % str(self.boundingbox))


    def find_bounding_box_by_name(self, db, name):
        """Find the bounding box of a city from its name.

        Args:
            db: connection to the database
            name (string): The city name.
        Returns a BoundingBox object describing the bounding box around
        the given city.
        """

        LOG.info('Looking for bounding box around %s...' % name)

        cursor = db.cursor()
        cursor.execute("""select osm_id, st_astext(st_transform(st_envelope(way), 4002))
                          from planet_osm_line
                          where boundary='administrative' and
                                admin_level='8' and
                                name=%s;""" % \
                           sql_escape_unicode(name))
        records = cursor.fetchall()
        if not records:
            raise UnsufficientDataError, "Wrong city name (%s) or missing administrative boundary in database!" % (repr(name))

        osm_id, wkt = records[0]
        LOG.info("osm_id for %s is: %s, bbox: %s" % (repr(name), osm_id, wkt))
        return BoundingBox.parse_wkt(wkt)

    def find_bounding_box_by_osmid(self, db, osmid):
        """Find the bounding box of a city from its OSM id.

        Args:
            db: connection to the database
            osmid (integer): The city OSM id.
        Returns a BoundingBox object describing the bounding box around
        the given city.
        """

        LOG.info('Looking for bounding box around %s...' % osmid)

        cursor = db.cursor()
        cursor.execute("""select st_astext(st_transform(st_envelope(way), 4002))
                          from planet_osm_polygon
                          where osm_id=%d;""" % \
                           osmid)
        records = cursor.fetchall()
        if not records:
            raise UnsufficientDataError, "Wrong OSM id (%s) !" % (repr(osmid))

        LOG.info("bbox is: %s" % records[0][0])
        return BoundingBox.parse_wkt(records[0][0])

    _regexp_contour = re.compile('^POLYGON\(\(([^)]*)\),\(([^)]*)\)\)$')
    _regexp_coords  = re.compile('^(\S*)\s+(\S*)$')

    def parse_city_contour(self, contour):
        try:
            cell00 = contour[0][0].strip()
        except (KeyError,IndexError,AttributeError):
            LOG.error("Invalid DB contour structure")
            return None

        # Got nothing usable
        if not cell00:
            return None

        # Parse the answer, in order to add a margin around the area
        prev_locale = locale.getlocale(locale.LC_ALL)
        locale.setlocale(locale.LC_ALL, "C")
        try:
            matches = self._regexp_contour.match(cell00)
            if not matches:
                print "Area not conformant"
                LOG.error("Area not conformant")
                return None
            scoords, inside = matches.groups()

            # Determine bbox envelope
            xmin, ymin, ymax, xmax = (None,)*4
            lcoords = scoords.split(',')
            if len(lcoords) != 5:
                print "Coords look atypical"
                LOG.warning("Coords look atypical: %s", lcoords)
            for scoord in lcoords:
                matches = self._regexp_coords.match(scoord)
                y,x = map(float,matches.groups())
                if (xmax is None) or xmax < x: xmax = x
                if (xmin is None) or xmin > x: xmin = x
                if (ymax is None) or ymax < y: ymax = y
                if (ymin is None) or ymin > y: ymin = y

            # Add one degree around the area
            xmin -= 1. ; xmax += 1.
            ymin -= 1. ; ymax += 1.
            l = "POLYGON((%f %f, %f %f, %f %f, %f %f, %f %f),(%s))" \
                % (ymin, xmin, ymin, xmax, ymax, xmax, ymax, xmin, ymin, xmin,
                  inside)
            return l
        except:
            # Regexp error: area is not a "simple" polygon
            LOG.exception("Unexpected exception")
            return None
        finally:
            locale.setlocale(locale.LC_ALL, prev_locale)

    def get_city_contour_by_name(self, db, city):
        assert city is not None
        LOG.info('Looking for contour around %s...' % repr(city))
        cursor = db.cursor()
        cursor.execute("""select st_astext(st_transform(
                                    st_difference(st_envelope(way),
                                                  st_buildarea(way)), 4002))
                              from planet_osm_line
                              where boundary='administrative'
                                 and admin_level='8' and name=%s;""" % \
                               sql_escape_unicode(city))
        contour = cursor.fetchall()
        LOG.debug('Got contour.')
        return self.parse_city_contour(contour)

    def get_city_contour_by_osmid(self, db, osmid):
        LOG.info('Looking for contour around osm id %s...' % repr(osmid))
        cursor = db.cursor()
        cursor.execute("""select st_astext(st_transform(
                                    st_difference(st_envelope(way),
                                                  st_buildarea(way)), 4002))
                              from planet_osm_polygon
                              where osm_id=%d;""" % \
                           osmid)
        contour = cursor.fetchall()
        LOG.debug('Got contour.')
        return self.parse_city_contour(contour)

    # This function creates a map_areas table that contains the list
    # of the squares used to divide the global city map. Each entry of
    # this table represent one of these square, with x, y being the
    # square identifiers, and geom being its geographical
    # geometry. This temporary table allows to conveniently perform a
    # joint with the planet_osm_line or planet_osm_polygon tables, so
    # that getting the list of squares for a given set of streets can
    # be performed in a single query
    def _gen_map_areas(self, db):
        cursor = db.cursor()
        LOG.debug('Call gen_map_areas table: %s...'
                  % self._map_areas_table_name)

        LOG.debug('drop...')
        cursor.execute("drop table if exists %s" % self._map_areas_table_name)
        LOG.debug('create table...')
        cursor.execute("create table %s (x integer, y integer)"
                       % self._map_areas_table_name)
        LOG.debug('addgeometrycolumn...')
        cursor.execute("select addgeometrycolumn('%s', 'geom', 4002, 'POLYGON', 2)" % self._map_areas_table_name)
        for i in xrange(0, int(math.ceil(self.griddesc.width_square_count))):
            for j in xrange(0, int(math.ceil(self.griddesc.height_square_count))):
                LOG.debug('add square %d,%d...' % (i,j))
                lon1 = (self.boundingbox.get_top_left()[1] +
                        i * self.griddesc.width_square_angle)
                lon2 = (self.boundingbox.get_top_left()[1] +
                        (i + 1) * self.griddesc.width_square_angle)
                lat1 = (self.boundingbox.get_top_left()[0] -
                        j * self.griddesc.height_square_angle)
                lat2 = (self.boundingbox.get_top_left()[0] -
                        (j + 1) * self.griddesc.height_square_angle)
                poly = ("POLYGON((%f %f, %f %f, %f %f, %f %f, %f %f))" %
                        (lon1, lat1, lon1, lat2, lon2,
                         lat2, lon2, lat1, lon1, lat1))
                cursor.execute("""insert into %s (x, y, geom)
                                  values (%d, %d, st_geomfromtext('%s', 4002))""" %
                               (self._map_areas_table_name, i, j, poly))

        LOG.debug('Commit gen_map_areas...')
        db.commit()
        LOG.debug('Done with gen_map_areas for %s.'
                  % self._map_areas_table_name)

    # Given a list of street and their corresponding squares, do some
    # cleanup and pass it through the internationalization layer to
    # get proper sorting, filtering of common prefixes, etc. Returns a
    # updated street list.
    def humanize_street_list(self, sl):
        # We transform the string representing the squares list into a
        # Python list
        sl = [( street[0],
                [ map(int, x.split(',')) for x in street[1].split(';')[:-1] ] )
              for street in sl]

        # 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())

        def _humanize_street_label(street):
            return (self.i18n.user_readable_street(street[0]),
                    _user_readable_label(street[1]))

        try:
            sl = sorted(map(_humanize_street_label, sl),
                        lambda x, y: locale.strcoll(x[0].lower(), y[0].lower()))
        finally:
            locale.setlocale(locale.LC_COLLATE, prev_locale)

        # Add the first letter of the street name as category
        sl = [(street[0][0], street[0], street[1]) for street in sl]

        return sl

    def get_streets_by_name(self, db, city):

        """Get the list of streets in the administrative area if city is
        defined or in the bounding box otherwise, and for each
        street, the list of squares that it intersects.

        Returns a list of the form [(street_name, 'A-B1'),
                                    (street2_name, 'B3')]
        """

        cursor = db.cursor()
	LOG.info("Getting streets for %s..." % repr(city))

        # pgdb.escape_string() doesn't like None strings, and when the
        # city is not passed, we don't want to match any existing
        # city. So the empty string doesn't sound like a good
        # candidate, and the "-1" string is probably better.
        #
        # TODO: improve the following request to remove this hack
        if city is None:
            city = "-1"

        # The inner select query creates the list of (street, square)
        # for all the squares in the temporary map_areas table. The
        # left_join + the test on cities_area is used to filter out
        # the streets outside the city administrative boundaries. The
        # outer select builds an easy to parse list of the squares for
        # each street.
        #
        # A typical result entry is:
        #  [ "Rue du Moulin", "0,1;1,2;1,3" ]
        #
        # REMARKS:
        #
        #  * The cities_area view is created once for all at
        #    installation. It associates the name of a city with the
        #    area covering it. As of today, only parts of the french
        #    cities have these administrative boundaries available in
        #    OSM. When available, this boundary is used to filter out
        #    the streets that are not inside the selected city but
        #    still in the bounding box rendered on the map. So these
        #    streets will be shown but not listed in the street index.
        #
        #  * The textcat_all() aggregate must also be installed in the
        #    database
        #
        # See ocitysmap-init.sql for details
        cursor.execute("""select name, textcat_all(x || ',' || y || ';')
                          from (select distinct name, x, y
                                from planet_osm_line
                                join %s
                                on st_intersects(way, st_transform(geom, 900913))
                                left join cities_area_by_name on city=%s
                                where trim(name) != '' and highway is not null
                                and case when cities_area_by_name.area is null
                                then
                                  true
                                else
                                  st_intersects(way, cities_area_by_name.area)
                                end)
                          as foo
                          group by name
                          order by name;""" % \
                           (self._map_areas_table_name,
                            sql_escape_unicode(city)))

        sl = cursor.fetchall()
	LOG.debug("Got %d streets." % len(sl))
        return self.humanize_street_list(sl)

    def get_streets_by_osmid(self, db, osmid):

        """Get the list of streets in the administrative area if city is
        defined or in the bounding box otherwise, and for each
        street, the list of squares that it intersects.

        Returns a list of the form [(street_name, 'A-B1'),
                                    (street2_name, 'B3')]
        """

        cursor = db.cursor()
	LOG.info("Getting streets for %s..." % osmid)

        # The inner select query creates the list of (street, square)
        # for all the squares in the temporary map_areas table. The
        # left_join + the test on cities_area is used to filter out
        # the streets outside the city administrative boundaries. The
        # outer select builds an easy to parse list of the squares for
        # each street.
        #
        # A typical result entry is:
        #  [ "Rue du Moulin", "0,1;1,2;1,3" ]
        #
        # REMARKS:
        #
        #  * The cities_area view is created once for all at
        #    installation. It associates the name of a city with the
        #    area covering it. As of today, only parts of the french
        #    cities have these administrative boundaries available in
        #    OSM. When available, this boundary is used to filter out
        #    the streets that are not inside the selected city but
        #    still in the bounding box rendered on the map. So these
        #    streets will be shown but not listed in the street index.
        #
        #  * The textcat_all() aggregate must also be installed in the
        #    database
        #
        # See ocitysmap-init.sql for details
        cursor.execute("""select name, textcat_all(x || ',' || y || ';')
                          from (select distinct name, x, y
                                from planet_osm_line
                                join %s
                                on st_intersects(way, st_transform(geom, 900913))
                                left join cities_area_by_osmid on cities_area_by_osmid.osm_id=%d
                                where trim(name) != '' and highway is not null
                                and case when cities_area_by_osmid.area is null
                                then
                                  true
                                else
                                  st_intersects(way, cities_area_by_osmid.area)
                                end)
                          as foo
                          group by name
                          order by name;""" % \
                           (self._map_areas_table_name,osmid))

        sl = cursor.fetchall()

	LOG.debug("Got %d streets." % len(sl))
        return self.humanize_street_list(sl)

    # Given a list of amenities and their corresponding squares, do some
    # cleanup and pass it through the internationalization layer to
    # get proper sorting, filtering of common prefixes, etc. Returns a
    # updated amenity list.
    def humanize_amenity_list(self, am):
        # We transform the string representing the squares list into a
        # Python list
        am = [( amenity[0], amenity[1],
                [ map(int, x.split(',')) for x in amenity[2].split(';')[:-1] ] )
              for amenity in am]

        # Street prefixes are postfixed and a human readable label is
        # built to represent the list of squares.
        def _humanize_amenity_label(amenity):
            return (amenity[0], amenity[1],
                    _user_readable_label(amenity[2]))
        
        am = map(_humanize_amenity_label, am)

        return am

    def get_amenities_by_name(self, db, city):

        """Get the list of amenities in the administrative area if city is
        defined or in the bounding box otherwise, and for each
        amenity, the list of squares that it intersects.

        Returns a list of the form [(category, name, 'A-B1'),
                                    (category, name, 'B3')]
        """

        cursor = db.cursor()

        # pgdb.escape_string() doesn't like None strings, and when the
        # city is not passed, we don't want to match any existing
        # city. So the empty string doesn't sound like a good
        # candidate, and the "-1" string is probably better.
        #
        # TODO: improve the following request to remove this hack
        if city is None:
            city = "-1"

        # The inner select query creates the list of (amenity, square)
        # for all the squares in the temporary map_areas table. The
        # left_join + the test on cities_area is used to filter out
        # the amenities outside the city administrative boundaries. The
        # outer select builds an easy to parse list of the squares for
        # each amenity.
        #
        # A typical result entry is:
        #  [ "Places of worship", "Basilique Sainte Germaine", "0,1;1,2;1,3" ]
        #
        # REMARKS:
        #
        #  * The cities_area view is created once for all at
        #    installation. It associates the name of a city with the
        #    area covering it. As of today, only parts of the french
        #    cities have these administrative boundaries available in
        #    OSM. When available, this boundary is used to filter out
        #    the streets that are not inside the selected city but
        #    still in the bounding box rendered on the map. So these
        #    streets will be shown but not listed in the street index.
        #
        #  * The textcat_all() aggregate must also be installed in the
        #    database
        #
        # See ocitysmap-init.sql for details
        al = []
        for cat, amenity, human in self.SELECTED_AMENITIES:
	    LOG.info("Get amenities %s for %s..." % (repr(amenity), repr(city)))
            cursor.execute("""select %(category)s, name, textcat_all(x || ',' || y || ';')
                              from (select distinct amenity, name, x, y, osm_id
                                    from planet_osm_point join %(tmp_tblname)s
                                    on st_intersects(way, st_transform(geom, 900913))
                                    left join cities_area_by_name on city=%(city)s
                                    where amenity = %(amenity)s and
                                    case when cities_area_by_name.area is null
                                    then
                                      true
                                    else
                                      st_intersects(way, cities_area_by_name.area)
                                    end
                                  union
                                    select distinct amenity, name, x, y, osm_id
                                    from planet_osm_polygon join %(tmp_tblname)s
                                    on st_intersects(way, st_transform(geom, 900913))
                                    left join cities_area_by_name on city=%(city)s
                                    where amenity = %(amenity)s and
                                    case when cities_area_by_name.area is null
                                    then
                                      true
                                    else
                                      st_intersects(way, cities_area_by_name.area)
                                    end)
                              as foo
                              group by amenity, osm_id, name
                              order by amenity, name
                              """ % dict(category=sql_escape_unicode(cat),
                                         tmp_tblname=self._map_areas_table_name,
                                         amenity=sql_escape_unicode(amenity),
                                         city=sql_escape_unicode(city)))
	    LOG.debug("Got amenities.")
            sub_al = [(c,a or human,h) for c,a,h in cursor.fetchall()]
            sub_al = self.humanize_amenity_list(sub_al)
            al.extend(sub_al)
        return al

    def get_amenities_by_osmid(self, db, osmid):

        """Get the list of amenities in the administrative area if city is
        defined or in the bounding box otherwise, and for each
        amenity, the list of squares that it intersects.

        Returns a list of the form [(category, name, 'A-B1'),
                                    (category, name2, 'B3')]
        """

        cursor = db.cursor()

        # The inner select query creates the list of (amenity, square)
        # for all the squares in the temporary map_areas table. The
        # left_join + the test on cities_area is used to filter out
        # the streets outside the city administrative boundaries. The
        # outer select builds an easy to parse list of the squares for
        # each amenity.
        #
        # A typical result entry is:
        #  [ "Place of worship", "Basilique Sainte Germaine", "0,1;1,2;1,3" ]
        #
        # REMARKS:
        #
        #  * The cities_area view is created once for all at
        #    installation. It associates the name of a city with the
        #    area covering it. As of today, only parts of the french
        #    cities have these administrative boundaries available in
        #    OSM. When available, this boundary is used to filter out
        #    the streets that are not inside the selected city but
        #    still in the bounding box rendered on the map. So these
        #    streets will be shown but not listed in the street index.
        #
        #  * The textcat_all() aggregate must also be installed in the
        #    database
        #
        # See ocitysmap-init.sql for details
        al = []
        for cat, amenity, human in self.SELECTED_AMENITIES:
	    LOG.info("Get amenities %s for %s..." % (repr(amenity), repr(osmid)))
            cursor.execute("""select %(category)s, name, textcat_all(x || ',' || y || ';')
                              from (select distinct amenity, name, x, y, planet_osm_point.osm_id
                                    from planet_osm_point join %(tmp_tblname)s
                                    on st_intersects(way, st_transform(geom, 900913))
                                    left join cities_area_by_osmid on cities_area_by_osmid.osm_id=%(osm_id)d
                                    where amenity = %(amenity)s and
                                    case when cities_area_by_osmid.area is null
                                    then
                                      true
                                    else
                                      st_intersects(way, cities_area_by_osmid.area)
                                    end
                                  union
                                    select distinct amenity, name, x, y, planet_osm_polygon.osm_id
                                    from planet_osm_polygon join %(tmp_tblname)s
                                    on st_intersects(way, st_transform(geom, 900913))
                                    left join cities_area_by_osmid on cities_area_by_osmid.osm_id=%(osm_id)d
                                    where amenity = %(amenity)s and
                                    case when cities_area_by_osmid.area is null
                                    then
                                      true
                                    else
                                      st_intersects(way, cities_area_by_osmid.area)
                                    end)
                              as foo
                              group by amenity, osm_id, name
                              order by amenity, name
                              """ % dict(category=sql_escape_unicode(cat),
                                         tmp_tblname=self._map_areas_table_name,
                                         amenity=sql_escape_unicode(amenity),
                                         osm_id=osmid))
	    LOG.debug("Got amenities.")
            sub_al = sub_al = [(c,a or human,h) for c,a,h in cursor.fetchall()]
            sub_al = self.humanize_amenity_list(sub_al)
            al.extend(sub_al)

        return al

    def _render_one_prefix(self, title, output_prefix, file_type,
                           paperwidth, paperheight):
        file_type   = file_type.lower()
        frame_width = int(max(paperheight / 20., 30))

        output_filename = "%s_index.%s" % (output_prefix, file_type)
        LOG.debug("rendering " + output_filename + "...")

        generator = IndexPageGenerator(self.streets + self.amenities, self.i18n)

        if file_type == 'xml':
            LOG.debug('not rendering index as xml (not supported)')
            return

        elif file_type == 'csv':
            try:
                writer = csv.writer(open(output_filename, 'w'))
            except Exception,ex:
                LOG.warning('error while opening destination file %s: %s'
                          % (output_filename, ex))
            else:
                # Try to treat indifferently unicode and str in CSV rows
                def csv_writerow(row):
                    _r = []
                    for e in row:
                        if type(e) is unicode:
                            _r.append(e.encode('UTF-8'))
                        else:
                            _r.append(e)
                    return writer.writerow(_r)

                copyright_notice = (u'© 2009 MapOSMatic/ocitysmap authors. '
                                    u'Map data © 2009 OpenStreetMap.org '
                                    u'and contributors (CC-BY-SA)')
                if title is not None:
                    csv_writerow(['# (UTF-8)', title, copyright_notice])
                else:
                    csv_writerow(['# (UTF-8)', '', copyright_notice])

                for entry in self.streets + self.amenities:
                    csv_writerow(entry)
            return

        if file_type in ('png', 'png24'):
            cairo_factory = \
                lambda w,h: cairo.ImageSurface(cairo.FORMAT_RGB24, w, h)

        elif file_type == 'svg':
            cairo_factory = lambda w,h: cairo.SVGSurface(output_filename, w, h)

        elif file_type == 'svgz':
            def cairo_factory(w,h):
                gz = gzip.GzipFile(output_filename, 'wb')
                return cairo.SVGSurface(gz, w, h)

        elif file_type == 'pdf':
            cairo_factory = lambda w,h: cairo.PDFSurface(output_filename, w, h)

        elif file_type == 'ps':
            cairo_factory = lambda w,h: cairo.PSSurface(output_filename, w, h)

        else:
            raise ValueError('Unsupported output format: %s' % file_type)

        if title is not None:
            surface = cairo_factory(paperwidth + frame_width*2,
                                    paperheight + frame_width*2)
            enclose_in_frame(lambda ctx: generator.render(ctx, paperwidth,
                                                          paperheight),
                             paperwidth, paperheight,
                             title, surface,
                             paperwidth + frame_width*2,
                             paperheight + frame_width*2, frame_width)
        else:
            surface = cairo_factory(paperwidth, paperheight)
            ctx = cairo.Context(surface)
            generator.render(ctx, paperwidth, paperheight)

        surface.flush()

        if file_type in ('png', 'png24'):
            surface.write_to_png(output_filename)

        surface.finish()

    def render_index(self, title, output_prefix, output_format,
                     paperwidth, paperheight):

        if not self.streets:
            LOG.warning('No street to write to index')
            return

        for fmt in output_format:
            LOG.debug('saving %s.%s...' % (output_prefix, fmt))
            try:
                self._render_one_prefix(title, output_prefix, fmt,
                                        paperwidth, paperheight)
            except:
                print >>sys.stderr, \
                    "Error while rendering %s:" % (fmt)
                traceback.print_exc()
                # Not fatal !


    def render_map_into_files(self, title,
                              out_prefix, out_formats,
                              zoom_factor):
        """
        Render the current boundingbox into the destination files.
        @param title (string/None) title of the map, or None: no frame
        @param out_prefix (string) prefix to use for generated files
        @param out_formats (iterable of strings) format of image files
        to generate
        @param zoom_factor None, a tuple (pixels_x, pixel_y) or
        'zoom:X' with X an integer [1..18]
        returns the MApnik map object used to render the map
        """
        # Create a temporary dir for the shapefiles and call _render_into_files

        osm_map_file = self.parser.get('mapnik', 'map')
        if not os.path.exists(osm_map_file):
            raise IOError, 'Invalid path to the osm.xml file (%s)' % osm_map_file

        tmpdir = tempfile.mkdtemp(prefix='ocitysmap')
        LOG.debug('rendering tmp dir: %s' % tmpdir)
        try:
            return self._render_map_into_files(tmpdir, osm_map_file,
                                               title,
                                               out_prefix, out_formats,
                                               zoom_factor)
        finally:
            for root, dirs, files in os.walk(tmpdir, topdown=False):
                for name in files:
                    os.remove(os.path.join(root, name))
                for name in dirs:
                    os.rmdir(os.path.join(root, name))
            os.rmdir(tmpdir)

    def _render_map_into_files(self, tmpdir,
                               osm_map_file, title, out_prefix, out_formats,
                               zoom_factor):
        # Does the real job of rendering the map
        GRID_COLOR = '#8BB381'
        LOG.debug('rendering from %s to %s.%s...' % (osm_map_file,
                                                   out_prefix,
                                                   out_formats))
        bbox = self.boundingbox.create_expanded(self.griddesc.height_square_angle*.7,
                                                self.griddesc.width_square_angle*.7)
        LOG.debug('bbox is: %s' % bbox)
        city = map_canvas.MapCanvas(osm_map_file, bbox, zoom_factor)
        LOG.debug('adding labels...')

        # Add the greyed-out area
        if self.contour is not None:
            path_contour = os.path.join(tmpdir, 'contour.shp')
            map_canvas.create_shapefile_polygon_from_wkt(path_contour,
                                                         self.contour)
            city.add_shapefile(path_contour, str_color = 'grey', alpha = .1)

        # Determine font size, depending on the zoom factor
        half_km_in_pixels = city.one_meter_in_pixels * 500.
        LOG.debug('500m = %f pixels' % half_km_in_pixels)
        if half_km_in_pixels < 10:
            font_size  = 6
            line_width = 1
        elif half_km_in_pixels < 25:
            font_size = 10
            line_width = 1
        elif half_km_in_pixels < 50:
            font_size = 20
            line_width = 2
        elif half_km_in_pixels < 100:
            font_size = 40
            line_width = 3
        elif half_km_in_pixels < 150:
            font_size = 65
            line_width = 4
        elif half_km_in_pixels < 200:
            font_size = 80
            line_width = 5
        elif half_km_in_pixels < 400:
            font_size = 120
            line_width = 6
        else:
            font_size = 200
            line_width = 7

        # Add the grid
        g = self.griddesc.generate_shape_file(os.path.join(tmpdir,
                                                           'grid.shp'), bbox)
        city.add_shapefile(g.get_filepath(), GRID_COLOR, .6, line_width)

        # Add the labels
        for idx, label in enumerate(self.griddesc.vertical_labels):
            x = self.griddesc.vertical_lines[idx] \
                + self.griddesc.width_square_angle/2.
            y = self.griddesc.horizontal_lines[0] \
                + self.griddesc.height_square_angle*.35
            city.add_label(x, y, label,
                           str_color = GRID_COLOR,
                           alpha = .9,
                           font_size = font_size,
                           font_family = "DejaVu Sans Bold")
        for idx, label in enumerate(self.griddesc.horizontal_labels):
            x = self.griddesc.vertical_lines[0] \
                - self.griddesc.width_square_angle*.35
            y = self.griddesc.horizontal_lines[idx] \
                - self.griddesc.height_square_angle/2.
            city.add_label(x, y, label,
                           str_color = GRID_COLOR,
                           alpha = .9,
                           font_size = font_size,
                           font_family = "DejaVu Sans Bold")

        # Add the scale
        T = self.griddesc.generate_scale_shape_file(os.path.join(tmpdir,
                                                                 'scale.shp'),
                                                    bbox.get_bottom_right()[0])
        if T is not None:
            s, lat, lg = T
            city.add_shapefile(s.get_filepath(), 'black', .9, 1)
            city.add_label(lg, lat, "500m", font_size = 16, str_color = 'black')

        # Determine parameters
        try:
            copyright_logo = self.parser.get('ocitysmap', 'copyright_logo')
        except Exception:
            copyright_logo = None

        # Rendering...
        LOG.debug('rendering map...')
        _map = city.render_map()
        for fmt in out_formats:
            LOG.debug('saving %s.%s...' % (out_prefix, fmt))
            try:
                city.save_map("%s.%s" % (out_prefix, fmt),
                              title,
                              file_type = fmt,
                              copyright_logo_png = copyright_logo)
            except:
                print >>sys.stderr, \
                    "Error while rendering %s:" % (fmt)
                traceback.print_exc()
                # Not fatal !

        return _map