Exemple #1
0
def write_iso19115_metadata(layer_uri, keywords, version_35=False):
    """Create metadata  object from a layer path and keywords dictionary.

    This function will save these keywords to the file system or the database.

    :param version_35: If we write keywords version 3.5. Default to False.
    :type version_35: bool

    :param layer_uri: Uri to layer.
    :type layer_uri: basestring

    :param keywords: Dictionary of keywords.
    :type keywords: dict
    """
    active_metadata_classes = METADATA_CLASSES
    if version_35:
        active_metadata_classes = METADATA_CLASSES35

    if 'layer_purpose' in keywords:
        if keywords['layer_purpose'] in active_metadata_classes:
            metadata = active_metadata_classes[
                keywords['layer_purpose']](layer_uri)
        else:
            LOGGER.info(
                'The layer purpose is not supported explicitly. It will use '
                'generic metadata. The layer purpose is %s' %
                keywords['layer_purpose'])
            if version_35:
                metadata = GenericLayerMetadata35(layer_uri)
            else:
                metadata = GenericLayerMetadata(layer_uri)
    else:
        if version_35:
            metadata = GenericLayerMetadata35(layer_uri)
        else:
            metadata = GenericLayerMetadata(layer_uri)

    metadata.update_from_dict(keywords)
    # Always set keyword_version to the latest one.
    if not version_35:
        metadata.update_from_dict({'keyword_version': inasafe_keyword_version})

    if metadata.layer_is_file_based:
        xml_file_path = os.path.splitext(layer_uri)[0] + '.xml'
        metadata.write_to_file(xml_file_path)
    else:
        metadata.write_to_db()

    return metadata
Exemple #2
0
def read_iso19115_metadata(layer_uri, keyword=None, version_35=False):
    """Retrieve keywords from a metadata object

    :param layer_uri: Uri to layer.
    :type layer_uri: basestring

    :param keyword: The key of keyword that want to be read. If None, return
        all keywords in dictionary.

    :returns: Dictionary of keywords or value of key as string.
    :rtype: dict, basestring
    """
    xml_uri = os.path.splitext(layer_uri)[0] + '.xml'
    if not os.path.exists(xml_uri):
        xml_uri = None
    if not xml_uri and os.path.exists(layer_uri):
        message = 'Layer based file but no xml file.\n'
        message += 'Layer path: %s.' % layer_uri
        raise NoKeywordsFoundError(message)
    metadata = GenericLayerMetadata(layer_uri, xml_uri)
    if version_35:
        metadata = GenericLayerMetadata35(layer_uri, xml_uri)
    else:
        metadata = GenericLayerMetadata(layer_uri, xml_uri)

    active_metadata_classes = METADATA_CLASSES
    if version_35:
        active_metadata_classes = METADATA_CLASSES35

    if metadata.layer_purpose in active_metadata_classes:
        metadata = active_metadata_classes[metadata.layer_purpose](layer_uri,
                                                                   xml_uri)

    # dictionary comprehension
    keywords = {
        x[0]: x[1]['value']
        for x in metadata.dict['properties'].iteritems()
        if x[1]['value'] is not None
    }
    if 'keyword_version' not in keywords.keys() and xml_uri:
        message = 'No keyword version found. Metadata xml file is invalid.\n'
        message += 'Layer uri: %s\n' % layer_uri
        message += 'Keywords file: %s\n' % os.path.exists(
            os.path.splitext(layer_uri)[0] + '.xml')
        message += 'keywords:\n'
        for k, v in keywords.iteritems():
            message += '%s: %s\n' % (k, v)
        raise MetadataReadError(message)

    # Get dictionary keywords that has value != None
    keywords = {
        x[0]: x[1]['value']
        for x in metadata.dict['properties'].iteritems()
        if x[1]['value'] is not None
    }

    if keyword:
        try:
            return keywords[keyword]
        except KeyError:
            message = 'Keyword with key %s is not found. ' % keyword
            message += 'Layer path: %s' % layer_uri
            raise KeywordNotFoundError(message)

    if isinstance(metadata, OutputLayerMetadata):
        keywords['if_provenance'] = metadata.provenance
    return keywords