def map_by_extent_zoom_size(provider, northwest, southeast, zoom, width, height): """ """ # we need it to cover a specific area mmap = mapByExtentZoom(provider, northwest, southeast, zoom) # but we also we need it at a specific size mmap = Map(mmap.provider, Point(width, height), mmap.coordinate, mmap.offset) return mmap
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)
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)
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)