<commit_before>__author__ = '<NAME>'
## Note: this module has been imported in the models.py in order to receive signals
## at the end of the models.py for the import of this module
from django.dispatch import receiver
from hs_core.signals import *
from forms import *

# signal handler to extract metadata from uploaded geotiff file and return template contexts
# to populate create-resource.html template page
@receiver(pre_create_resource, sender=RasterResource)
def raster_pre_create_resource_trigger(sender, **kwargs):
    if(sender is RasterResource):
        files = kwargs['files']
        title = kwargs['title']

        metadata = kwargs['metadata']
        from collections import OrderedDict
        if(files):
            import raster_meta_extract
            infile = files[0]
            res_md_dict = raster_meta_extract.get_raster_meta_dict(infile.file.name)

            wgs_cov_info = res_md_dict['spatial_coverage_info']['wgs84_coverage_info']
            # add core metadata coverage - box
            box = {'coverage': {'type': 'box', 'value': wgs_cov_info }}
            metadata.append(box)

            # Save extended meta to metadata variable
            ori_cov = {'OriginalCoverage': {'value': res_md_dict['spatial_coverage_info']['original_coverage_info'] }}
            metadata.append(ori_cov)

            # Save extended meta to metadata variable
            cellInfo = OrderedDict([
                ('name', infile.name),
                ('rows', res_md_dict['cell_and_band_info']['rows']),
                ('columns', res_md_dict['cell_and_band_info']['columns']),
                ('cellSizeXValue', res_md_dict['cell_and_band_info']['cellSizeXValue']),
                ('cellSizeYValue', res_md_dict['cell_and_band_info']['cellSizeYValue']),
                ('cellSizeUnit', res_md_dict['cell_and_band_info']['cellSizeUnit']),
                ('cellDataType', res_md_dict['cell_and_band_info']['cellDataType']),
                ('noDataValue', res_md_dict['cell_and_band_info']['noDataValue'])
                ])
            metadata.append({'CellInformation': cellInfo})
            bcount = res_md_dict['cell_and_band_info']['bandCount']
        else:
            # initialize required raster metadata to be place holders to be edited later by users
            cell_info = OrderedDict([
                ('name', title),
                ('rows', 0),
                ('columns', 0),
                ('cellSizeXValue', 0),
                ('cellSizeYValue', 0),
                ('cellSizeUnit', "NA"),
                ('cellDataType', "NA"),
                ('noDataValue', 0)
            ])
            metadata.append({'CellInformation': cell_info})
            bcount = 1
            spatial_coverage_info = OrderedDict([
                 ('units', "NA"),
                 ('projection', 'NA'),
                 ('northlimit', 'NA'),
                 ('southlimit', 'NA'),
                 ('eastlimit', 'NA'),
                 ('westlimit', 'NA')
            ])
            # # add core metadata coverage - box
            # box = {'coverage': {'type': 'box', 'value': spatial_coverage_info}}
            # metadata.append(box)

            # Save extended meta to metadata variable
            ori_cov = {'OriginalCoverage': {'value': spatial_coverage_info }}
            metadata.append(ori_cov)

        for i in range(bcount):
            band_dict = OrderedDict()
            band_dict['name'] = 'Band_' + str(i+1)
            band_dict['variableName'] = 'Unnamed'
            band_dict['variableUnit'] = 'Unnamed'
            band_dict['method'] = ''
            band_dict['comment'] = ''
            metadata.append({'BandInformation': band_dict})

@receiver(pre_add_files_to_resource, sender=RasterResource)
def raster_pre_add_files_to_resource_trigger(sender, **kwargs):
    if(sender is RasterResource):
        files = kwargs['files']
        res = kwargs['resource']
        if(files):
            infile = files[0]
            import raster_meta_extract
            res_md_dict = raster_meta_extract.get_raster_meta_dict(infile.file.name)

            # update core metadata coverage - box
            #cov_box = res.metadata.coverages.all().filter(type='box').first()
            #res.metadata.update_element('coverage', cov_box.id, type='box', value=res_md_dict['spatial_coverage_info']['wgs84_coverage_info'])

            wgs_cov_info = res_md_dict['spatial_coverage_info']['wgs84_coverage_info']
            res.metadata.create_element('Coverage', type='box', value=res_md_dict['spatial_coverage_info']['wgs84_coverage_info'])

            # update extended original box coverage
            #ori_cov_box = res.metadata.originalCoverage
            #res.metadata.update_element('OriginalCoverage', ori_cov_box.id, type='box', value=res_md_dict['spatial_coverage_info']['original_coverage_info'])
            if res.metadata.originalCoverage is None:
                v = {'value': res_md_dict['spatial_coverage_info']['original_coverage_info'] }
                res.metadata.create_element('OriginalCoverage', **v)

            # update extended metadata CellInformation
            res.metadata.cellInformation.delete()
            res.metadata.create_element('CellInformation', name=infile.name, rows=res_md_dict['cell_and_band_info']['rows'],
                                        columns = res_md_dict['cell_and_band_info']['columns'],
                                        cellSizeXValue = res_md_dict['cell_and_band_info']['cellSizeXValue'],
                                        cellSizeYValue = res_md_dict['cell_and_band_info']['cellSizeYValue'],
                                        cellSizeUnit = res_md_dict['cell_and_band_info']['cellSizeUnit'],
                                        cellDataType = res_md_dict['cell_and_band_info']['cellDataType'],<commit_msg>[#435] the 'Coverage' meta won't be created or updated when there is no valid wgs84 bounding box info<commit_after>__author__ = '<NAME>'
## Note: this module has been imported in the models.py in order to receive signals
## at the end of the models.py for the import of this module
from django.dispatch import receiver
from hs_core.signals import *
from forms import *

# signal handler to extract metadata from uploaded geotiff file and return template contexts
# to populate create-resource.html template page
@receiver(pre_create_resource, sender=RasterResource)
def raster_pre_create_resource_trigger(sender, **kwargs):
    if(sender is RasterResource):
        files = kwargs['files']
        title = kwargs['title']

        metadata = kwargs['metadata']
        from collections import OrderedDict
        if(files):
            import raster_meta_extract
            infile = files[0]
            res_md_dict = raster_meta_extract.get_raster_meta_dict(infile.file.name)

            wgs_cov_info = res_md_dict['spatial_coverage_info']['wgs84_coverage_info']
            # add core metadata coverage - box
            if wgs_cov_info:
                box = {'coverage': {'type': 'box', 'value': wgs_cov_info}}
                metadata.append(box)

            # Save extended meta to metadata variable
            ori_cov = {'OriginalCoverage': {'value': res_md_dict['spatial_coverage_info']['original_coverage_info'] }}
            metadata.append(ori_cov)

            # Save extended meta to metadata variable
            cellInfo = OrderedDict([
                ('name', infile.name),
                ('rows', res_md_dict['cell_and_band_info']['rows']),
                ('columns', res_md_dict['cell_and_band_info']['columns']),
                ('cellSizeXValue', res_md_dict['cell_and_band_info']['cellSizeXValue']),
                ('cellSizeYValue', res_md_dict['cell_and_band_info']['cellSizeYValue']),
                ('cellSizeUnit', res_md_dict['cell_and_band_info']['cellSizeUnit']),
                ('cellDataType', res_md_dict['cell_and_band_info']['cellDataType']),
                ('noDataValue', res_md_dict['cell_and_band_info']['noDataValue'])
                ])
            metadata.append({'CellInformation': cellInfo})
            bcount = res_md_dict['cell_and_band_info']['bandCount']
        else:
            # initialize required raster metadata to be place holders to be edited later by users
            cell_info = OrderedDict([
                ('name', title),
                ('rows', 0),
                ('columns', 0),
                ('cellSizeXValue', 0),
                ('cellSizeYValue', 0),
                ('cellSizeUnit', "NA"),
                ('cellDataType', "NA"),
                ('noDataValue', 0)
            ])
            metadata.append({'CellInformation': cell_info})
            bcount = 1
            spatial_coverage_info = OrderedDict([
                 ('units', "NA"),
                 ('projection', 'NA'),
                 ('northlimit', 'NA'),
                 ('southlimit', 'NA'),
                 ('eastlimit', 'NA'),
                 ('westlimit', 'NA')
            ])
            # # add core metadata coverage - box
            # box = {'coverage': {'type': 'box', 'value': spatial_coverage_info}}
            # metadata.append(box)

            # Save extended meta to metadata variable
            ori_cov = {'OriginalCoverage': {'value': spatial_coverage_info }}
            metadata.append(ori_cov)

        for i in range(bcount):
            band_dict = OrderedDict()
            band_dict['name'] = 'Band_' + str(i+1)
            band_dict['variableName'] = 'Unnamed'
            band_dict['variableUnit'] = 'Unnamed'
            band_dict['method'] = ''
            band_dict['comment'] = ''
            metadata.append({'BandInformation': band_dict})

@receiver(pre_add_files_to_resource, sender=RasterResource)
def raster_pre_add_files_to_resource_trigger(sender, **kwargs):
    if(sender is RasterResource):
        files = kwargs['files']
        res = kwargs['resource']
        if(files):
            infile = files[0]
            import raster_meta_extract
            res_md_dict = raster_meta_extract.get_raster_meta_dict(infile.file.name)

            # update core metadata coverage - box
            #cov_box = res.metadata.coverages.all().filter(type='box').first()
            #res.metadata.update_element('coverage', cov_box.id, type='box', value=res_md_dict['spatial_coverage_info']['wgs84_coverage_info'])

            wgs_cov_info = res_md_dict['spatial_coverage_info']['wgs84_coverage_info']
            if wgs_cov_info:
                res.metadata.create_element('Coverage', type='box', value=res_md_dict['spatial_coverage_info']['wgs84_coverage_info'])

            # update extended original box coverage
            #ori_cov_box = res.metadata.originalCoverage
            #res.metadata.update_element('OriginalCoverage', ori_cov_box.id, type='box', value=res_md_dict['spatial_coverage_info']['original_coverage_info'])
            if res.metadata.originalCoverage is None:
                v = {'value': res_md_dict['spatial_coverage_info']['original_coverage_info'] }
                res.metadata.create_element('OriginalCoverage', **v)

            # update extended metadata CellInformation
            res.metadata.cellInformation.delete()
            res.metadata.create_element('CellInformation', name=infile.name, rows=res_md_dict['cell_and_band_info']['rows'],
                                        columns = res_md_dict['cell_and_band_info']['columns'],
                                        cellSizeXValue = res_md_dict['cell_and_band_info']['cellSizeXValue'],
                                        cellSizeYValue = res_md_dict['cell_and_band_info']['cellSizeYValue'],
                                        cellSizeUnit = res_md_dict['cell_and_band_info']['cellSizeUnit'],
                                        cellDataType = res_md_dict['cell_and_band_info']['cellDataType'],
{'fraction_non_alphanumeric': 0.11747826820828762, 'fraction_numerical': 0.003291416997214955, 'mean_word_length': 2.4179405826362848, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 10, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7301912', 'n_tokens_mistral': 3310, 'n_tokens_neox': 3197, 'n_words': 616}