Beispiel #1
0
def mapByExtentZoomAspect(prov, locA, locB, zoom, aspect):
    """ Get a map by extent and zoom, and adjust it to the desired aspect ratio.
    
        Adjustments always increase the size. Return a ModestMaps.Map instance.
    """
    mmap = mapByExtentZoom(prov, locA, locB, zoom)
    center = mmap.pointLocation(Point(mmap.dimensions.x/2, mmap.dimensions.y/2))
    
    if aspect < float(mmap.dimensions.x) / float(mmap.dimensions.y):
        # make taller
        dimensions = Point(mmap.dimensions.x, int(mmap.dimensions.x / aspect))

    else:
        # make wider
        dimensions = Point(int(mmap.dimensions.y * aspect), mmap.dimensions.y)
    
    return mapByCenterZoom(prov, center, zoom, dimensions)
Beispiel #2
0
def get_mmap_page(mmap, row, col, rows, cols):
    """ Get a mmap instance for a sub-page in an atlas layout.
    """
    dim = mmap.dimensions
    
    # aim for ~5% overlap, vary dep. on total rows/cols
    overlap = 0.1 / rows
    overlap *= (dim.x + dim.y) / 2
    
    # inner width and height of sub-page
    _w = (dim.x - (cols + 1) * overlap) / cols
    _h = (dim.y - (rows + 1) * overlap) / rows
    
    # pixel offset of page center
    x = (col * _w) + (_w / 2) + (col * overlap) + overlap
    y = (row * _h) + (_h / 2) + (row * overlap) + overlap
    
    location = mmap.pointLocation(Point(x, y))
    zoom = mmap.coordinate.zoom + (log(rows) / log(2))
    
    return mapByCenterZoom(mmap.provider, location, zoom, mmap.dimensions)
Beispiel #3
0
        
        label_geometry = bbox_polygon(place.label_bbox(), osm, zoom).__geo_interface__
        
        label_features.append({'type': 'Feature',
                               'geometry': label_geometry,
                               'properties': properties
                              })
    
    dumpjson({'type': 'FeatureCollection', 'features': point_features}, open(pointsfile, 'w'))
    dumpjson({'type': 'FeatureCollection', 'features': label_features}, open(labelsfile, 'w'))
    
    print 'Wrote %d points to %s and %s.' % (len(point_features), pointsfile, labelsfile)
    
    print '-' * 80
    
    map = mapByCenterZoom(osm, Location(0, 0), zoom, Point(2 ** (zoom + 8), 2 ** (zoom + 8)))
    
    if zoom > 5:
        map = mapByCenterZoom(osm, Location(40.078, -96.987), zoom, Point(1400, 800))
        map = mapByCenterZoom(osm, Location(38.889, -77.050), zoom, Point(1200, 900))
    
    img = map.draw(False) # newimg('RGB', (map.dimensions.x, map.dimensions.y), (0xFF, 0xFF, 0xFF))
    draw = drawimg(img)

    print '-' * 80
    
    sw = map.pointLocation(Point(-100, map.dimensions.y + 100))
    ne = map.pointLocation(Point(map.dimensions.x + 100, -100))
    
    previewed_places = [place for place in visible_places
                        if (sw.lat < place.location.lat and place.location.lat < ne.lat
Beispiel #4
0
def main(apibase, password, print_id, paper_size, orientation=None, layout=None, provider=None, bounds=None, zoom=None, geotiff_url=None):
    """
    """
    yield 5
    
    print_path = 'print.php?' + urlencode({'id': print_id})
    print_href = print_id and urljoin(apibase.rstrip('/')+'/', print_path) or None
    print_data = {'pages': []}
    
    #
    # Prepare a shorthand 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 pdf, prev, data: print_id and finish_print(apibase, password, print_id, pdf, prev, data) or None
    
    print 'Print:', print_id
    print 'Paper:', paper_size
    
    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
    
        if orientation and bounds and zoom and provider and layout:
        
            print 'Orientation:', orientation
            print 'Bounds:', bounds
            print 'Zoom:', zoom
            print 'Layout:', layout
            print 'Provider:', provider
            print 'Size:', get_preview_map_size(orientation, paper_size)
            
            print_pages = print_data['pages']
            
            north, west, south, east = bounds
            width, height = get_preview_map_size(orientation, paper_size)
            
            northwest = Location(north, west)
            southeast = Location(south, east)
            
            #
            # Prepare preview.jpg
            #
            mmap = map_by_extent_zoom_size(TemplatedMercatorProvider(provider),
                                           northwest, southeast, zoom,
                                           width, height)
            
            yield 30
            
            out = StringIO()
            mmap.draw(fatbits_ok=True).save(out, format='JPEG')
            preview_url = _append_file('preview.jpg', out.getvalue())
            
            print 'Sent preview.jpg'
            
            #
            # Prepare full-size image
            #
            zdiff = min(18, zoom + 2) - zoom
            print 'Zoom diff:', zdiff
            
            mmap = map_by_extent_zoom_size(TemplatedMercatorProvider(provider),
                                           northwest, southeast, zoom + zdiff,
                                           width * 2**zdiff, height * 2**zdiff)
            
            yield 60
            
            add_print_page(print_context, mmap, print_href, map_bounds_pt, points_FG, hm2pt_ratio)
            
            # out = StringIO()
            # mmap.draw().save(out, format='JPEG')
            # print_url = append_print_file(print_id, 'print.jpg', out.getvalue(), apibase, password)
            # 
            # print 'Sent print.jpg'
            
            page_nw = mmap.pointLocation(Point(0, 0))
            page_se = mmap.pointLocation(mmap.dimensions)
            
            page_data = {'print': 'print.jpg', 'preview': 'preview.jpg', 'bounds': {}}
            page_data['bounds'].update({'north': page_nw.lat, 'west': page_nw.lon})
            page_data['bounds'].update({'south': page_se.lat, 'east': page_se.lon})
            print_pages.append(page_data)
            
            rows, cols = map(int, layout.split(','))
            
            if rows > 1 and cols > 1:
                for (row, col) in product(range(rows), range(cols)):
                    
                    sub_mmap = get_mmap_page(mmap, row, col, rows, cols)
                    sub_part = '%(row)d,%(col)d' % locals()
                    sub_name = 'print-%(sub_part)s.jpg' % locals()
                    sub_href = print_href + '/' + sub_part
                    
                    yield 60
                    
                    add_print_page(print_context, sub_mmap, sub_href, map_bounds_pt, points_FG, hm2pt_ratio)
            
                    #
                    # Prepare preview image
                    #
                    prev_cen = sub_mmap.pointLocation(Point(sub_mmap.dimensions.x / 2, sub_mmap.dimensions.y / 2))
                    prev_dim = Point(sub_mmap.dimensions.x / 2**zdiff, sub_mmap.dimensions.y / 2**zdiff)
                    prev_mmap = mapByCenterZoom(sub_mmap.provider, prev_cen, sub_mmap.coordinate.zoom - zdiff, prev_dim)
                    prev_name = 'preview-%(sub_part)s.jpg' % locals()
            
                    out = StringIO()
                    prev_mmap.draw(fatbits_ok=True).save(out, format='JPEG')
                    prev_href = _append_file(prev_name, out.getvalue())
                    
                    #
                    # Populate page data
                    #
                    page_nw = sub_mmap.pointLocation(Point(0, 0))
                    page_se = sub_mmap.pointLocation(sub_mmap.dimensions)
                    
                    page_data = {'part': sub_part, 'preview_href': prev_href, 'bounds': {}}
                    page_data['bounds'].update({'north': page_nw.lat, 'west': page_nw.lon})
                    page_data['bounds'].update({'south': page_se.lat, 'east': page_se.lon})
    
                    print_pages.append(page_data)
        
        else:
            yield ALL_FINISHED
            return
    
    except:
        raise
    
    finally:
        finish_drawing()
    
    yield 60
    
    pdf_name = 'walking-paper-%s.pdf' % print_id
    pdf_url = _append_file(pdf_name, open(print_filename, 'r').read())
    
    print json_encode(print_data)

    yield 10
    
    move(print_filename, 'out.pdf')
    
    _finish_print(pdf_url, preview_url, json_encode(print_data))
    
    yield ALL_FINISHED