def generate_map_image(self, **kwargs):
     user = self.user
     project = self.project
     # Attempting to make fake image
     image = Image.new('RGB', (200, 100))
     tmp_file = 'test.jpg'
     image.save(tmp_file)
     author_string = 'Author of the media file'
     tags = "j,k,l"
     with open(tmp_file, 'rb') as data:
         photo_data = {
             'attribution':
             user.username,
             'host':
             settings.SERVER_HOST,
             'owner':
             user,
             'last_updated_by':
             user,
             'project':
             self.project,
             # generic does not have 'generateID'
             # according to test error despite that
             # generic has the function
             'uuid':
             generic.generateID(),
             'status':
             StatusCode.objects.get(id=StatusCode.READY_FOR_PROCESSING),
         }
         map_image = MapImage.objects.create(**photo_data)
         map_image.process_mapImage_to_S3(File(data))
     return map_image
 def generate_map_image(self, **kwargs):
     user = self.user
     project = self.project
     # Attempting to make fake image
     image = Image.new('RGB', (200, 100))
     tmp_file = 'test.jpg'
     image.save(tmp_file)
     author_string = 'Author of the media file'
     tags = "j,k,l"
     with open(tmp_file, 'rb') as data:
         photo_data = {
             'attribution': user.username,
             'host': settings.SERVER_HOST,
             'owner': user,
             'last_updated_by': user,
             'project': self.project,
             # generic does not have 'generateID'
             # according to test error despite that
             # generic has the function
             'uuid': generic.generateID(),
             'status': StatusCode.objects.get(
                 id=StatusCode.READY_FOR_PROCESSING),
         }
         map_image = MapImage.objects.create(**photo_data)
         map_image.process_mapImage_to_S3(File(data))
     return map_image
Exemple #3
0
    def create(self, validated_data):
        # Overriding the create method to handle file processing
        # owner = self.context.get('request').user
        f = self.initial_data.get('media_file')

        # ensure filetype is valid:
        upload_helpers.validate_file(f, self.ext_whitelist)

        owner = self.context.get('request').user
        validated_data = {}
        validated_data.update(self.validated_data)
        validated_data.update(self.get_presave_create_dictionary())
        validated_data.update({
            'uuid':
            generic.generateID(),
            'status':
            models.StatusCode.objects.get(
                id=models.StatusCode.READY_FOR_PROCESSING),
            'upload_source':
            models.UploadSource.objects.get(id=models.UploadSource.WEB_FORM),
            'attribution':
            validated_data.get('attribution') or owner.username,
            'host':
            settings.SERVER_HOST
        })
        self.instance = self.Meta.model.objects.create(**validated_data)

        # process map onto the instance
        # from localground.apps.tasks import process_map
        # result = process_map.delay(self.instance)

        # now save the map_image to S3
        self.instance.process_mapImage_to_S3(f)
        return self.instance
Exemple #4
0
    def create_print(self, layout_id=1, map_provider=1,
                     lat=55, lng=61.4, zoom=17,
                     map_title='A title',
                     instructions='A description',
                     tags=[], generate_pdf=True):
        from localground.apps.site import models
        from django.contrib.gis.geos import Point
        uuid = generic.generateID()
        p = models.Print.insert_print_record(
            self.user,
            self.project,
            models.Layout.objects.get(id=layout_id),
            models.TileSet.objects.get(id=map_provider),
            zoom,
            Point(lng, lat, srid=4326),
            settings.SERVER_HOST,
            map_title=map_title,
            instructions=instructions,
            mapimage_ids=None
        )
        d = {
            'uuid': p.uuid,
            'virtual_path': p.virtual_path,
            'layout': models.Layout.objects.get(id=layout_id),
            'name': 'Map Title!',
            'description': 'Instructions!!!',
            'map_provider': models.TileSet.objects.get(id=map_provider),
            'zoom': 10,
            'center': Point(lng, lat, srid=4326),
            'project': self.project,
            'northeast': p.northeast,
            'southwest': p.southwest,
            'extents': p.extents,
            'host': p.host,
            'map_image_path': p.map_image_path,
            'pdf_path': p.pdf_path,
            'preview_image_path': p.preview_image_path,
            'map_width': p.map_width,
            'map_height': p.map_height
        }
        p.tags = tags
        p.save()
        if generate_pdf:
            # This comes straight from print serializer
            # because it is needed to work for test data

            # create the instance with valid data
            # the problem is that how can I get a uuid for use in test?

            # create the report and save
            pdf_report = p.generate_pdf()
            pdf_file_path = pdf_report.path + '/' + pdf_report.file_name
            thumb_file_path = pdf_report.path + '/' + 'thumbnail.jpg'
            p.pdf_path_S3.save(
                pdf_report.file_name, File(open(pdf_file_path)))
            p.map_image_path_S3.save(
                'thumbnail_' + uuid + '.jpg', File(open(thumb_file_path)))
        return p
Exemple #5
0
    def delete(self, *args, **kwargs):
        # first remove directory, then delete from db:
        import shutil
        import os
        path = self.get_abs_directory_path()
        if os.path.exists(path):
            dest = '%s/deleted/%s' % (settings.USER_MEDIA_ROOT, self.uuid)
            if os.path.exists(dest):
                from localground.apps.lib.helpers import generic
                dest = dest + '.dup.' + generic.generateID()
            try:
                shutil.move(path, dest)
            except:
                #pass
                raise Exception('error moving path from %s to %s' % (path, dest))

        super(Print, self).delete(*args, **kwargs)
Exemple #6
0
    def save(self, user=None, *args, **kwargs):
        from localground.apps.lib.helpers import generic

        is_new = self.pk is None

        # 1. ensure that user doesn't inadvertently change the data type of the
        # column
        if is_new:
            if user and not hasattr(self, 'owner'):
                self.owner = user
            self.date_created = get_timestamp_no_milliseconds()
            self.table_name = 'table_%s_%s' % (
                self.owner.username.lower(), generic.generateID(num_digits=10))

        if user:
            self.last_updated_by = user
        self.time_stamp = get_timestamp_no_milliseconds()
        super(Dataset, self).save(*args, **kwargs)
Exemple #7
0
    def save(self, user=None, *args, **kwargs):
        from localground.apps.lib.helpers import generic

        is_new = self.pk is None

        # 1. ensure that user doesn't inadvertently change the data type of the
        # column
        if is_new:
            if user and not hasattr(self, 'owner'):
                self.owner = user
            self.date_created = get_timestamp_no_milliseconds()
            self.table_name = 'table_%s_%s' % (
                self.owner.username.lower(), generic.generateID(num_digits=10))

        if user:
            self.last_updated_by = user
        self.time_stamp = get_timestamp_no_milliseconds()
        super(Dataset, self).save(*args, **kwargs)
Exemple #8
0
 def create_mapimage(self, user, project, tags=[], name='MapImage Name'):
     p = self.create_print(map_title='A mapimage-linked print')
     mapimage = models.MapImage(
         project=project,
         owner=user,
         last_updated_by=user,
         source_print=p,
         name=name,
         uuid=generic.generateID(),
         tags=tags,
         description='MapImage Description',
         status=models.StatusCode.get_status(
             models.StatusCode.PROCESSED_SUCCESSFULLY),
         upload_source=models.UploadSource.get_source(
             models.UploadSource.WEB_FORM))
     mapimage.save()
     mapimage.processed_image = self.create_imageopt(mapimage)
     mapimage.save()
     return mapimage
Exemple #9
0
 def create_print_without_image(self,
                                layout_id=1,
                                map_provider=1,
                                lat=55,
                                lng=61.4,
                                zoom=17,
                                map_title='A title',
                                instructions='A description',
                                tags=[],
                                generate_pdf=True):
     from django.conf import settings
     from localground.apps.site import models
     layout = models.Layout.objects.get(id=layout_id)
     tileset = models.TileSet.objects.get(id=map_provider)
     uuid = generic.generateID()
     user = self.user
     p = models.Print(uuid=uuid,
                      project=self.project,
                      zoom=zoom,
                      map_width=layout.map_width_pixels,
                      map_height=layout.map_height_pixels,
                      map_provider=tileset,
                      owner=user,
                      last_updated_by=user,
                      layout=layout,
                      host=settings.SERVER_HOST,
                      map_image_path='map.jpg',
                      pdf_path='Print_' + uuid + '.pdf',
                      preview_image_path='thumbnail.jpg',
                      name=map_title,
                      description=instructions,
                      center=None,
                      northeast=None,
                      southwest=None,
                      extents=None,
                      virtual_path='/%s/%s/%s/' %
                      (settings.USER_MEDIA_DIR, 'prints', uuid))
     p.save()
     return p
Exemple #10
0
    def create_mapimage(self, user=None, project=None, tags=[],
                        name='MapImage Name', generate_print=False):

        from localground.apps.site import models
        if generate_print:
            p = self.create_print(
                map_title='A mapimage-linked print'
            )
        else:
            p = self.create_print_without_image(
                map_title='A mapimage-linked print'
            )
        user = user or self.user
        project = project or self.project
        mapimage = models.MapImage(
            project=project,
            owner=user,
            last_updated_by=user,
            source_print=p,
            name=name,
            uuid=generic.generateID(),
            tags=tags,
            description='MapImage Description',
            status=models.StatusCode.get_status(
                models.StatusCode.PROCESSED_SUCCESSFULLY),
            upload_source=models.UploadSource.get_source(
                models.UploadSource.WEB_FORM),
            virtual_path='/userdata/media/' + user.username + '/map-images/',
            host=settings.SERVER_HOST,
            file_name_orig='map_image.jpg',
            file_name_thumb='map_image_thumb.jpg'
        )
        mapimage.save()
        mapimage.processed_image = self.create_imageopt(mapimage)
        mapimage.save()
        return mapimage
Exemple #11
0
 def create_print_without_image(
         self, layout_id=1, map_provider=1, lat=55, lng=61.4, zoom=17,
         map_title='A title', instructions='A description', tags=[],
         generate_pdf=True):
     from django.conf import settings
     from localground.apps.site import models
     layout = models.Layout.objects.get(id=layout_id)
     tileset = models.TileSet.objects.get(id=map_provider)
     uuid = generic.generateID()
     user = self.user
     p = models.Print(
         uuid=uuid,
         project=self.project,
         zoom=zoom,
         map_width=layout.map_width_pixels,
         map_height=layout.map_height_pixels,
         map_provider=tileset,
         owner=user,
         last_updated_by=user,
         layout=layout,
         host=settings.SERVER_HOST,
         map_image_path='map.jpg',
         pdf_path='Print_' + uuid + '.pdf',
         preview_image_path='thumbnail.jpg',
         name=map_title,
         description=instructions,
         center=None,
         northeast=None,
         southwest=None,
         extents=None,
         virtual_path='/%s/%s/%s/' % (
             settings.USER_MEDIA_DIR, 'prints', uuid
         )
     )
     p.save()
     return p
 def process_file(self, file, owner):
     #save to disk:
     model_name_plural = models.Scan.model_name_plural
     uuid = generic.generateID()
     file_name_new = upload_helpers.save_file_to_disk(owner, model_name_plural, file, uuid=uuid)
     file_name, ext = os.path.splitext(file_name_new)
     
     # create thumbnail:
     from PIL import Image
     thumbnail_name = '%s_thumb.png' % file_name
     media_path = upload_helpers.generate_absolute_path(owner, model_name_plural, uuid=uuid)
     im = Image.open(media_path + '/' + file_name_new)
     im.thumbnail([500, 500], Image.ANTIALIAS)
     im.save('%s/%s' % (media_path, thumbnail_name))
     
     return {
         'uuid': uuid,
         'file_name_orig': file.name,
         'name': self.initial_data.get('name') or file.name,
         'file_name_new': file_name_new,
         'file_name_thumb': thumbnail_name,
         'content_type': ext.replace('.', ''),
         'virtual_path': upload_helpers.generate_relative_path(owner, model_name_plural, uuid=uuid)
     }
Exemple #13
0
    def create_mapimage(self,
                        user=None,
                        project=None,
                        tags=[],
                        name='MapImage Name',
                        generate_print=False):

        from localground.apps.site import models
        if generate_print:
            p = self.create_print(map_title='A mapimage-linked print')
        else:
            p = self.create_print_without_image(
                map_title='A mapimage-linked print')
        user = user or self.user
        project = project or self.project
        mapimage = models.MapImage(
            project=project,
            owner=user,
            last_updated_by=user,
            source_print=p,
            name=name,
            uuid=generic.generateID(),
            tags=tags,
            description='MapImage Description',
            status=models.StatusCode.get_status(
                models.StatusCode.PROCESSED_SUCCESSFULLY),
            upload_source=models.UploadSource.get_source(
                models.UploadSource.WEB_FORM),
            virtual_path='/userdata/media/' + user.username + '/map-images/',
            host=settings.SERVER_HOST,
            file_name_orig='map_image.jpg',
            file_name_thumb='map_image_thumb.jpg')
        mapimage.save()
        mapimage.processed_image = self.create_imageopt(mapimage)
        mapimage.save()
        return mapimage
Exemple #14
0
    def create_print(self,
                     layout_id=1,
                     map_provider=1,
                     lat=55,
                     lng=61.4,
                     zoom=17,
                     map_title='A title',
                     instructions='A description',
                     tags=[],
                     generate_pdf=True):
        from localground.apps.site import models
        from django.contrib.gis.geos import Point
        uuid = generic.generateID()
        p = models.Print.insert_print_record(
            self.user,
            self.project,
            models.Layout.objects.get(id=layout_id),
            models.TileSet.objects.get(id=map_provider),
            zoom,
            Point(lng, lat, srid=4326),
            settings.SERVER_HOST,
            map_title=map_title,
            instructions=instructions,
            mapimage_ids=None)
        d = {
            'uuid': p.uuid,
            'virtual_path': p.virtual_path,
            'layout': models.Layout.objects.get(id=layout_id),
            'name': 'Map Title!',
            'description': 'Instructions!!!',
            'map_provider': models.TileSet.objects.get(id=map_provider),
            'zoom': 10,
            'center': Point(lng, lat, srid=4326),
            'project': self.project,
            'northeast': p.northeast,
            'southwest': p.southwest,
            'extents': p.extents,
            'host': p.host,
            'map_image_path': p.map_image_path,
            'pdf_path': p.pdf_path,
            'preview_image_path': p.preview_image_path,
            'map_width': p.map_width,
            'map_height': p.map_height
        }
        p.tags = tags
        p.save()
        if generate_pdf:
            # This comes straight from print serializer
            # because it is needed to work for test data

            # create the instance with valid data
            # the problem is that how can I get a uuid for use in test?

            # create the report and save
            pdf_report = p.generate_pdf()
            pdf_file_path = pdf_report.path + '/' + pdf_report.file_name
            thumb_file_path = pdf_report.path + '/' + 'thumbnail.jpg'
            p.pdf_path_S3.save(pdf_report.file_name, File(open(pdf_file_path)))
            p.map_image_path_S3.save('thumbnail_' + uuid + '.jpg',
                                     File(open(thumb_file_path)))
        return p
Exemple #15
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
Exemple #16
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
Exemple #17
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