def generate_pdf(self): from localground.apps.site import models from localground.apps.lib.helpers import generic, StaticMap, Report import os # use static map helper function to calculate additional geometric # calculations m = StaticMap() map_width = self.layout.map_width_pixels map_height = self.layout.map_height_pixels path = settings.USER_MEDIA_ROOT + '/prints/' + self.uuid os.mkdir(path) # create new directory file_name = 'Print_' + self.uuid + '.pdf' layers = self.embedded_layers scans = self.embedded_scans info = m.get_basemap_and_extents( self.map_provider, self.zoom, self.center, map_width, map_height) map_image = info.get('map_image') northeast = info.get('northeast') southwest = info.get('southwest') bbox = (northeast.coords, southwest.coords) bbox = [element for tupl in bbox for element in tupl] extents = Polygon.from_bbox(bbox) qr_size = self.layout.qr_size_pixels border_width = self.layout.border_width overlay_image = m.get_map( layers, southwest=southwest, northeast=northeast, scans=scans, height=map_height, width=map_width, show_north_arrow=True) map_image.paste(overlay_image, (0, 0), overlay_image) if self.layout.is_data_entry: map_image = map_image.convert("L") # convert to black and white map_image.save(path + '/map.jpg') # add border around map: map_image = StaticMap.draw_border(map_image, border_width) map_width, map_height = map_image.size map_image.save(path + '/map_with_border.jpg') # generate thumbnail: size = map_width / 3, map_height / 3 thumbnail = map_image.copy() thumbnail.thumbnail(size, Image.ANTIALIAS) thumbnail.save(path + '/thumbnail.jpg') # generate QR code qr_image_1 = StaticMap.generate_qrcode( self.uuid, 1, path, qr_size, border_width) qr_size = qr_image_1.size[0] # generate PDF pdf_report = Report( path, file_name=self.pdf_path, is_landscape=self.layout.is_landscape, author=self.owner.username, title=self.name) ########## # Page 1 # ########## # build from the bottom up: # (x & y dependencies are additive from bottom up) # add footer: if self.layout.is_data_entry: pdf_report.add_footer(qr_image_1, self.uuid, self.description) # add map: pdf_report.add_map(map_image, is_data_entry=self.layout.is_data_entry) # add header: pdf_report.add_header( is_data_entry=self.layout.is_data_entry, is_map_page=True) pdf_report.save()
def insert_print_record(cls, user, project, layout, map_provider, zoom, center, host, map_title=None, instructions=None, layer_ids=None, scan_ids=None, do_save=True): from localground.apps.site import models from localground.apps.lib.helpers import generic, StaticMap layers, scans = None, None if layer_ids is not None: layers = models.WMSOverlay.objects.filter(id__in=layer_ids) if scan_ids is not None: scans = models.Scan.objects.filter(id__in=scan_ids) if instructions is not None: # preserve line breaks in the pdf report instructions = '<br/>'.join(instructions.splitlines()) # use static map helper function to calculate additional geometric # calculations m = StaticMap() info = m.get_basemap_and_extents( map_provider, zoom, center, layout.map_width_pixels, layout.map_height_pixels ) map_image = info.get('map_image') northeast = info.get('northeast') southwest = info.get('southwest') bbox = (northeast.coords, southwest.coords) bbox = [element for tupl in bbox for element in tupl] extents = Polygon.from_bbox(bbox) # Save the print p = Print() p.uuid = generic.generateID() p.project = project p.zoom = zoom p.map_width = layout.map_width_pixels p.map_height = layout.map_height_pixels p.map_provider = map_provider p.owner = user p.last_updated_by = user p.layout = layout p.host = host p.map_image_path = 'map.jpg' p.pdf_path = 'Print_' + p.uuid + '.pdf' p.preview_image_path = 'thumbnail.jpg' p.name = map_title p.description = instructions p.center = center p.northeast = northeast p.southwest = southwest p.extents = extents p.virtual_path = p.generate_relative_path() #raise Exception(p.to_dict()) if do_save: p.save() # todo: come back to this (if we want MapServer to render layers) if layers: for layer in layers: p.stash(l, user) if scans: for scan in scans: p.stash(scan, user) return p