Exemplo n.º 1
0
    def generate_pdf(self):
        from localground.apps.site import models
        from localground.apps.lib.helpers import Extents, generic, StaticMap, \
            Report, AcetateLayer
        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 = '{0}/media/{1}/{2}'.format(settings.USER_MEDIA_ROOT,
                                          self.owner.username, self.uuid)

        os.mkdir(path)  # create new directory
        file_name = 'Print_' + self.uuid + '.pdf'

        mapimages = self.embedded_mapimages
        map_image = m.get_basemap(self.map_provider, self.zoom, self.center,
                                  map_width, map_height)
        extents = Extents.get_extents_from_center_lat_lng(
            self.center, self.zoom, map_width, map_height)

        bbox = (extents.northeast.coords, extents.southwest.coords)
        bbox = [element for tupl in bbox for element in tupl]
        qr_size = self.layout.qr_size_pixels
        border_width = self.layout.border_width
        '''

        #TODO: Replace w/new acetate layer functionality:
        overlay_image = m.get_map(
            layers,
            southwest=extents.southwest,
            northeast=extents.northeast,
            mapimages=mapimages,
            height=map_height,
            width=map_width,
            show_north_arrow=True)
        map_image.paste(overlay_image, (0, 0), overlay_image)
        '''
        '''
        a = AcetateLayer(
            file_path=path,
            center=self.center,
            project_id=self.project.id,
            zoom=self.zoom,
            width=map_width,
            height=map_height
        )
        overlay_image = a.generate_acetate_layer()
        if overlay_image is not None:
            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,
                            file_name_S3=self.pdf_path_S3,
                            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()
        return pdf_report
Exemplo n.º 2
0
    def insert_print_record(cls,
                            user,
                            project,
                            layout,
                            map_provider,
                            zoom,
                            center,
                            host,
                            map_title=None,
                            instructions=None,
                            mapimage_ids=None,
                            do_save=True):
        from localground.apps.site import models
        from localground.apps.lib.helpers import generic, StaticMap, \
            Extents, AcetateLayer

        layers, mapimages = None, None
        if mapimage_ids is not None:
            mapimages = models.MapImage.objects.filter(id__in=mapimage_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()
        map_image = m.get_basemap(map_provider, zoom, center,
                                  layout.map_width_pixels,
                                  layout.map_height_pixels)
        extents = Extents.get_extents_from_center_lat_lng(
            center, zoom, layout.map_width_pixels, layout.map_height_pixels)
        bbox = (extents.northeast.coords, extents.southwest.coords)
        bbox = [element for tupl in bbox for element in tupl]
        extents_polygon = 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_S3 = 'map.jpg'
        p.map_image_path = 'map.jpg'
        p.pdf_path_S3 = 'Print_' + p.uuid + '.pdf'
        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 = extents.northeast
        p.southwest = extents.southwest
        p.extents = extents_polygon
        p.virtual_path = p.generate_relative_path()

        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 mapimages:
                for mapimage in mapimages:
                    p.stash(mapimage, user)
        return p
Exemplo n.º 3
0
    def generate_pdf(self):
        from localground.apps.site import models
        from localground.apps.lib.helpers import Extents, generic, StaticMap, \
            Report, AcetateLayer
        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 = '{0}/media/{1}/{2}'.format(
            settings.USER_MEDIA_ROOT,
            self.owner.username,
            self.uuid
        )

        os.mkdir(path)  # create new directory
        file_name = 'Print_' + self.uuid + '.pdf'

        mapimages = self.embedded_mapimages
        map_image = m.get_basemap(
            self.map_provider, self.zoom, self.center, map_width, map_height
        )
        extents = Extents.get_extents_from_center_lat_lng(
            self.center, self.zoom, map_width, map_height
        )

        bbox = (extents.northeast.coords, extents.southwest.coords)
        bbox = [element for tupl in bbox for element in tupl]
        qr_size = self.layout.qr_size_pixels
        border_width = self.layout.border_width

        '''

        #TODO: Replace w/new acetate layer functionality:
        overlay_image = m.get_map(
            layers,
            southwest=extents.southwest,
            northeast=extents.northeast,
            mapimages=mapimages,
            height=map_height,
            width=map_width,
            show_north_arrow=True)
        map_image.paste(overlay_image, (0, 0), overlay_image)
        '''

        '''
        a = AcetateLayer(
            file_path=path,
            center=self.center,
            project_id=self.project.id,
            zoom=self.zoom,
            width=map_width,
            height=map_height
        )
        overlay_image = a.generate_acetate_layer()
        if overlay_image is not None:
            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,
            file_name_S3=self.pdf_path_S3,
            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()
        return pdf_report
Exemplo n.º 4
0
    def insert_print_record(
        cls, user, project, layout, map_provider, zoom, center, host,
            map_title=None,  instructions=None, mapimage_ids=None,
            do_save=True):
        from localground.apps.site import models
        from localground.apps.lib.helpers import generic, StaticMap, \
            Extents, AcetateLayer

        layers, mapimages = None, None
        if mapimage_ids is not None:
            mapimages = models.MapImage.objects.filter(id__in=mapimage_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()
        map_image = m.get_basemap(
            map_provider, zoom, center,
            layout.map_width_pixels, layout.map_height_pixels
        )
        extents = Extents.get_extents_from_center_lat_lng(
            center, zoom,
            layout.map_width_pixels, layout.map_height_pixels
        )
        bbox = (extents.northeast.coords, extents.southwest.coords)
        bbox = [element for tupl in bbox for element in tupl]
        extents_polygon = 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_S3 = 'map.jpg'
        p.map_image_path = 'map.jpg'
        p.pdf_path_S3 = 'Print_' + p.uuid + '.pdf'
        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 = extents.northeast
        p.southwest = extents.southwest
        p.extents = extents_polygon
        p.virtual_path = p.generate_relative_path()

        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 mapimages:
                for mapimage in mapimages:
                    p.stash(mapimage, user)
        return p
Exemplo n.º 5
0
    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()
Exemplo n.º 6
0
    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