示例#1
0
    def metadata(self):
        """
        Return the metadata for this raster or band. The return value is a
        nested dictionary, where the first-level key is the metadata domain and
        the second-level is the metadata item names and values for that domain.
        """
        if not capi.get_ds_metadata_domain_list:
            raise ValueError(
                'GDAL ≥ 1.11 is required for using the metadata property.')

        # The initial metadata domain list contains the default domain.
        # The default is returned if domain name is None.
        domain_list = ['DEFAULT']

        # Get additional metadata domains from the raster.
        meta_list = capi.get_ds_metadata_domain_list(self._ptr)
        if meta_list:
            # The number of domains is unknown, so retrieve data until there
            # are no more values in the ctypes array.
            counter = 0
            domain = meta_list[counter]
            while domain:
                domain_list.append(domain.decode())
                counter += 1
                domain = meta_list[counter]

        # Free domain list array.
        capi.free_dsl(meta_list)

        # Retrieve metadata values for each domain.
        result = {}
        for domain in domain_list:
            # Get metadata for this domain.
            data = capi.get_ds_metadata(
                self._ptr,
                (None if domain == 'DEFAULT' else domain.encode()),
            )
            if not data:
                continue
            # The number of metadata items is unknown, so retrieve data until
            # there are no more values in the ctypes array.
            domain_meta = {}
            counter = 0
            item = data[counter]
            while item:
                key, val = item.decode().split('=')
                domain_meta[key] = val
                counter += 1
                item = data[counter]
            # The default domain values are returned if domain is None.
            result[domain or 'DEFAULT'] = domain_meta
        return result
示例#2
0
    def metadata(self):
        """
        Return the metadata for this raster or band. The return value is a
        nested dictionary, where the first-level key is the metadata domain and
        the second-level is the metadata item names and values for that domain.
        """
        if not capi.get_ds_metadata_domain_list:
            raise ValueError('GDAL ≥ 1.11 is required for using the metadata property.')

        # The initial metadata domain list contains the default domain.
        # The default is returned if domain name is None.
        domain_list = ['DEFAULT']

        # Get additional metadata domains from the raster.
        meta_list = capi.get_ds_metadata_domain_list(self._ptr)
        if meta_list:
            # The number of domains is unknown, so retrieve data until there
            # are no more values in the ctypes array.
            counter = 0
            domain = meta_list[counter]
            while domain:
                domain_list.append(domain.decode())
                counter += 1
                domain = meta_list[counter]

        # Free domain list array.
        capi.free_dsl(meta_list)

        # Retrieve metadata values for each domain.
        result = {}
        for domain in domain_list:
            # Get metadata for this domain.
            data = capi.get_ds_metadata(
                self._ptr,
                (None if domain == 'DEFAULT' else domain.encode()),
            )
            if not data:
                continue
            # The number of metadata items is unknown, so retrieve data until
            # there are no more values in the ctypes array.
            domain_meta = {}
            counter = 0
            item = data[counter]
            while item:
                key, val = item.decode().split('=')
                domain_meta[key] = val
                counter += 1
                item = data[counter]
            # The default domain values are returned if domain is None.
            result[domain or 'DEFAULT'] = domain_meta
        return result