Ejemplo n.º 1
0
    <ul>"""
    
    cities.sort(key=lambda city: city['name'])

    for city in cities:
        slug = city['slug']
        name = city['name']
        
        try:
            ul = Location(float(city['top']), float(city['left']))
            lr = Location(float(city['bottom']), float(city['right']))
        except ValueError:
            print >> stderr, 'Failed on %(name)s (%(slug)s)' % city
            raise
        else:
            mmap = mapByExtent(provider, ul, lr, dimensions)
        
        if slug in files:
            list = ['<li class="file"><a href="%s">%s</a> (%s)</li>' % (href, file, nice_size(size))
                    for (ext, (file, size, href))
                    in sorted(files[slug].items())]
        
            list = ''.join(list)
            
            center = mmap.pointLocation(Point(dimensions.x/2, dimensions.y/2))
            zoom = mmap.coordinate.zoom
            href = 'http://www.openstreetmap.org/?lat=%.3f&amp;lon=%.3f&amp;zoom=%d&amp;layers=M' % (center.lat, center.lon, zoom)
            
            print >> index, """
                <li class="city">
                    <a name="%(slug)s" href="%(href)s"><img src="previews/%(slug)s.jpg"></a>
Ejemplo n.º 2
0
    '''

    cities.sort(key=lambda city: city['name'])

    for city in cities:
        slug = city['slug']
        name = city['name']

        try:
            ul = Location(float(city['top']), float(city['left']))
            lr = Location(float(city['bottom']), float(city['right']))
        except ValueError:
            print >> stderr, 'Failed on %(name)s (%(slug)s)' % city
            raise
        else:
            mmap = mapByExtent(provider, ul, lr, dimensions)

        if slug in files:
            bz2_file, bz2_size, bz2_href = files[slug]['bz2']
            pbf_file, pbf_size, pbf_href = files[slug]['pbf']

            list = ('<li class="file"><a href="%s">%s %s OSM data</a></li>' * 2) \
                 % (bz2_href, nice_size(bz2_size), 'bzip’ed XML',
                    pbf_href, nice_size(pbf_size), 'binary PBF')

            if 'coastline' in files[slug]:
                coast_file, coast_size, coast_href = files[slug]['coastline']
                list += '<li class="file"><a href="%s">%s coastline shapefile</a></li>' % (coast_href, nice_size(coast_size))

            if 'osm2pgsql shapefiles' in files[slug]:
                shape_file, shape_size, shape_href = files[slug]['osm2pgsql shapefiles']
Ejemplo n.º 3
0
cities = list(DictReader(open('cities.txt'), dialect='excel-tab'))

try:
    (previews, ) = argv[1:]
except ValueError:
    print >> stderr, 'Usage: compose-city-previews.py <previews directory>'
    exit(1)

for city in cities:
    print >> stderr, city['name'], '...',

    north, west = float(city['top']), float(city['left'])
    south, east = float(city['bottom']), float(city['right'])

    mmap = mapByExtent(provider, Location(north, west), Location(south, east), dimensions)
    
    ul = mmap.locationPoint(Location(north, west))
    lr = mmap.locationPoint(Location(south, east))
    bbox = [(p.x, p.y) for p in (ul, lr)]

    img = mmap.draw()

    mask = Image.new('L', img.size, 0x99)
    ImageDraw(mask).rectangle(bbox, fill=0x00)
    img.paste((0xFF, 0xFF, 0xFF), (0, 0), mask)
    
    frame = Image.new('L', img.size, 0x00)
    ImageDraw(frame).rectangle(bbox, outline=0x33)
    img.paste((0x00, 0x00, 0x00), (0, 0), frame)
    
Ejemplo n.º 4
0
def main(apibase, password, print_id, pages, paper_size, orientation, layout):
    """
    """
    print_path = "atlas.php?" + urlencode({"id": print_id})
    print_href = print_id and urljoin(apibase.rstrip("/") + "/", print_path) or None
    print_info = {}

    #
    # Prepare a shorthands for pushing data.
    #

    _append_file = lambda name, body: print_id and append_print_file(print_id, name, body, apibase, password) or None
    _finish_print = lambda print_info: print_id and finish_print(apibase, password, print_id, print_info) or None
    _update_print = lambda progress: print_id and update_print(apibase, password, print_id, progress) or None

    print "Print:", print_id
    print "Paper:", orientation, paper_size, layout

    #
    # Prepare output context.
    #

    handle, print_filename = mkstemp(suffix=".pdf")
    close(handle)

    page_width_pt, page_height_pt, points_FG, hm2pt_ratio = paper_info(paper_size, orientation)
    print_context, finish_drawing = get_drawing_context(print_filename, page_width_pt, page_height_pt)

    try:
        map_xmin_pt = 0.5 * ptpin
        map_ymin_pt = 1 * ptpin
        map_xmax_pt = page_width_pt - 0.5 * ptpin
        map_ymax_pt = page_height_pt - 0.5 * ptpin

        map_bounds_pt = map_xmin_pt, map_ymin_pt, map_xmax_pt, map_ymax_pt

        #
        # Add pages to the PDF one by one.
        #

        for (index, page) in enumerate(pages):
            _update_print(0.1 + 0.9 * float(index) / len(pages))

            page_href = print_href and (print_href + "/%(number)s" % page) or None

            provider = TemplatedMercatorProvider(page["provider"])
            zoom = page["zoom"]

            mark = page.get("mark", None) or None
            fuzzy = page.get("fuzzy", None) or None
            text = unicode(page.get("text", None) or "").encode("utf8")
            role = page.get("role", None) or None

            north, west, south, east = page["bounds"]
            northwest = Location(north, west)
            southeast = Location(south, east)

            page_mmap = mapByExtentZoom(provider, northwest, southeast, zoom)

            if role == "index":
                indexees = [pages[other] for other in range(len(pages)) if other != index]
            else:
                indexees = []

            add_print_page(
                print_context,
                page_mmap,
                page_href,
                map_bounds_pt,
                points_FG,
                hm2pt_ratio,
                layout,
                text,
                mark,
                fuzzy,
                indexees,
            )

            #
            # Now make a smaller preview map for the page,
            # 600px looking like a reasonable upper bound.
            #

            preview_mmap = copy(page_mmap)

            while preview_mmap.dimensions.x > 600:
                preview_zoom = preview_mmap.coordinate.zoom - 1
                preview_mmap = mapByExtentZoom(provider, northwest, southeast, preview_zoom)

            out = StringIO()
            preview_mmap.draw(fatbits_ok=True).save(out, format="JPEG", quality=85)
            preview_url = _append_file("preview-p%(number)s.jpg" % page, out.getvalue())
            print_info["pages[%(number)s][preview_url]" % page] = preview_url

        #
        # Complete the PDF and upload it.
        #

        finish_drawing()

        pdf_name = "field-paper-%s.pdf" % print_id
        pdf_url = _append_file(pdf_name, open(print_filename, "r").read())
        print_info["pdf_url"] = pdf_url

    except:
        raise

    finally:
        unlink(print_filename)

    #
    # Make a small preview map of the whole print coverage area.
    #

    provider = TemplatedMercatorProvider(pages[0]["provider"])

    norths, wests, souths, easts = zip(*[page["bounds"] for page in pages])
    northwest = Location(max(norths), min(wests))
    southeast = Location(min(souths), max(easts))

    dimensions = Point(*get_preview_map_size(orientation, paper_size))

    preview_mmap = mapByExtent(provider, northwest, southeast, dimensions)

    out = StringIO()
    preview_mmap.draw(fatbits_ok=True).save(out, format="JPEG", quality=85)
    preview_url = _append_file("preview.jpg" % page, out.getvalue())
    print_info["preview_url"] = preview_url

    #
    # All done, wrap it up.
    #

    _finish_print(print_info)
try:
    (previews, ) = argv[1:]
except ValueError:
    print >> stderr, 'Usage: compose-city-previews.py <previews directory>'
    exit(1)

for city in cities:
    if not city['name']:
        raise Exception('Need a name for ' + str(city))

    print >> stderr, city['name'], '...',

    north, west = float(city['top']), float(city['left'])
    south, east = float(city['bottom']), float(city['right'])

    mmap = mapByExtent(provider, Location(north, west), Location(south, east),
                       dimensions)

    ul = mmap.locationPoint(Location(north, west))
    lr = mmap.locationPoint(Location(south, east))
    bbox = [(p.x, p.y) for p in (ul, lr)]

    img = mmap.draw()

    mask = Image.new('L', img.size, 0x99)
    ImageDraw(mask).rectangle(bbox, fill=0x00)
    img.paste((0xFF, 0xFF, 0xFF), (0, 0), mask)

    frame = Image.new('L', img.size, 0x00)
    ImageDraw(frame).rectangle(bbox, outline=0x33)
    img.paste((0x00, 0x00, 0x00), (0, 0), frame)
Ejemplo n.º 6
0
def main(apibase, password, print_id, pages, paper_size, orientation, layout):
    """
    """
    print_path = 'atlas.php?' + urlencode({'id': print_id})
    print_href = print_id and urljoin(apibase.rstrip('/')+'/', print_path) or None
    print_info = {}

    #
    # Prepare a shorthands for pushing data.
    #

    _append_file = lambda name, body: print_id and append_print_file(print_id, name, body, apibase, password) or None
    _finish_print = lambda print_info: print_id and finish_print(apibase, password, print_id, print_info) or None
    _update_print = lambda progress: print_id and update_print(apibase, password, print_id, progress) or None

    print 'Print:', print_id
    print 'Paper:', orientation, paper_size, layout

    #
    # Prepare output context.
    #

    handle, print_filename = mkstemp(suffix='.pdf')
    close(handle)

    page_width_pt, page_height_pt, points_FG, hm2pt_ratio = paper_info(paper_size, orientation)
    print_context, finish_drawing = get_drawing_context(print_filename, page_width_pt, page_height_pt)

    try:
        map_xmin_pt = .5 * ptpin
        map_ymin_pt = 1 * ptpin
        map_xmax_pt = page_width_pt - .5 * ptpin
        map_ymax_pt = page_height_pt - .5 * ptpin

        map_bounds_pt = map_xmin_pt, map_ymin_pt, map_xmax_pt, map_ymax_pt

        #
        # Add pages to the PDF one by one.
        #

        for (index, page) in enumerate(pages):
            _update_print(0.1 + 0.9 * float(index) / len(pages))

            page_href = print_href and (print_href + '/%(number)s' % page) or None

            provider = TemplatedMercatorProvider(page['provider'])
            zoom = page['zoom']

            mark = page.get('mark', None) or None
            fuzzy = page.get('fuzzy', None) or None
            text = unicode(page.get('text', None) or '').encode('utf8')
            role = page.get('role', None) or None

            north, west, south, east = page['bounds']
            northwest = Location(north, west)
            southeast = Location(south, east)

            page_mmap = mapByExtentZoom(provider, northwest, southeast, zoom)

            if role == 'index':
                indexees = [pages[other] for other in range(len(pages)) if other != index]
            else:
                indexees = []

            add_print_page(print_context, page_mmap, page_href, map_bounds_pt, points_FG, hm2pt_ratio, layout, text, mark, fuzzy, indexees)

            #
            # Now make a smaller preview map for the page,
            # 600px looking like a reasonable upper bound.
            #

            preview_mmap = copy(page_mmap)

            while preview_mmap.dimensions.x > 600:
                preview_zoom = preview_mmap.coordinate.zoom - 1
                preview_mmap = mapByExtentZoom(provider, northwest, southeast, preview_zoom)

            out = StringIO()
            preview_mmap.draw(fatbits_ok=True).save(out, format='JPEG', quality=85)
            preview_url = _append_file('preview-p%(number)s.jpg' % page, out.getvalue())
            print_info['pages[%(number)s][preview_url]' % page] = preview_url

        #
        # Complete the PDF and upload it.
        #

        finish_drawing()

        pdf_name = 'field-paper-%s.pdf' % print_id
        pdf_url = _append_file(pdf_name, open(print_filename, 'r').read())
        print_info['pdf_url'] = pdf_url

    except:
        raise

    finally:
        unlink(print_filename)

    #
    # Make a small preview map of the whole print coverage area.
    #

    provider = TemplatedMercatorProvider(pages[0]['provider'])

    norths, wests, souths, easts = zip(*[page['bounds'] for page in pages])
    northwest = Location(max(norths), min(wests))
    southeast = Location(min(souths), max(easts))

    dimensions = Point(*get_preview_map_size(orientation, paper_size))

    preview_mmap = mapByExtent(provider, northwest, southeast, dimensions)

    out = StringIO()
    preview_mmap.draw(fatbits_ok=True).save(out, format='JPEG', quality=85)
    preview_url = _append_file('preview.jpg' % page, out.getvalue())
    print_info['preview_url'] = preview_url

    #
    # All done, wrap it up.
    #

    _finish_print(print_info)